Skip to content

Comments

issue #1107 do not ignore encryptKey and decryptKey exceptions + update readme#1120

Merged
DenBond7 merged 3 commits intomasterfrom
ip-1007-do-not-ignore-errors
Mar 25, 2021
Merged

issue #1107 do not ignore encryptKey and decryptKey exceptions + update readme#1120
DenBond7 merged 3 commits intomasterfrom
ip-1007-do-not-ignore-errors

Conversation

@IvanPizhenko
Copy link
Contributor

@IvanPizhenko IvanPizhenko commented Mar 24, 2021

close #1107
close #1106

@DenBond7 DenBond7 added the PR submitted PR is submitted for this issue label Mar 24, 2021
@DenBond7 DenBond7 added this to the 1.1.6 milestone Mar 24, 2021
@DenBond7 DenBond7 changed the title issue #1007 do not ignore encryptKey and decryptKey exceptions + update readme issue #1107 do not ignore encryptKey and decryptKey exceptions + update readme Mar 24, 2021
Copy link
Collaborator

@DenBond7 DenBond7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IvanPizhenko There are no bugs. Just need to add a few changes.

} catch (e: Exception) {
""
}
val keys = parseAndNormalizeKeyRings(armored)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IvanPizhenko

val key = parseAndNormalizeKeyRings(armored).firstOrNull()
        ?: throw NullPointerException("Keys not found")
    if (key !is PGPSecretKeyRing) throw IllegalArgumentException("Wrong PGPKeyRing")
    return encryptKey(key, passphrase).armor()

And I'd like to see different methods(public) for different input source (String, ByteArray, PGPSecretKeyRing)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need that now? I can see all current usages are with string. Let's add such methods right when they are first time needed. Methods for the PGPSecretKeyRing are available, but currently private. When they are needed to be called externally, we can make them public.

Copy link
Contributor Author

@IvanPizhenko IvanPizhenko Mar 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NullPointerException? I think IllegalArgumentException in more suitable, because you are actually never getting null value as input parameter, and error is better to be about the actual input parameter rather than about result of the some intermediate transformation which is intentionally designed so it can result a null value.

return decryptKey(keys[0] as PGPSecretKeyRing, passphrase).armor()
}

private fun decryptKey(key: PGPSecretKeyRing, passphrase: String): PGPSecretKeyRing {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same for decryptKey

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Including armor

""
}
val keys = parseAndNormalizeKeyRings(armored)
return encryptKey(keys[0] as PGPSecretKeyRing, passphrase).armor()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IvanPizhenko
It'll be better to don't armor a key right here. We called this method encryptKey but actually, we do encryption and armor, but I just expect encryptKey due to the method name. Please don't use armor here. As for me will be better to use PgpKey.encryptKey(private, passPhrase).armor() than PgpKey.encryptKey(private, passPhrase).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current uses of this provide key as armored string on the input and expect armored output. That's why it is done this way. I suggest to have overloaded methods with output type matching to the input type. But I suggest to implement them only when they are actually required.

private fun extractSecretKey(armored: String): PGPSecretKeyRing {
val key = parseAndNormalizeKeyRings(armored).firstOrNull()
?: throw IllegalArgumentException("Keys not found")
if (key is PGPSecretKeyRing)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomholub Sorry to disturb

  1. Could you clarify is it the same as we discussed at Kotlin code style consistency with other codebases #1122?
- if (key is PGPSecretKeyRing)
-      return key
-    else
-     throw IllegalArgumentException("Key is not a secret key")
+ return (key as? PGPSecretKeyRing)?:throw IllegalArgumentException("Key is not a secret key")
  1. And what about braces to 'if' and 'else' statements?
- if (key is PGPSecretKeyRing)
-      return key
-    else
-     throw IllegalArgumentException("Key is not a secret key")
+ if (key is PGPSecretKeyRing) {
+    return key
+ } else {
+    throw IllegalArgumentException("Key is not a secret key")
+ }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if/else statements do not have to have braces if on one line. But in that case it's only permitted to use for "consistent" result, eg like a ternary statement return if (c) 1 else 2 or val x = if (c) 1 else 2. Cannot mix return and throw on one line or without braces.

if/else statements MUST have braces if multiline. I wish there was some ktlint rule for this (maybe there is?)

// good
val x = if(c) 1 else 2

// good
return if(c) 1 else 2

// bad
if (key is PGPSecretKeyRing)
      return key
    else
     throw IllegalArgumentException("Key is not a secret key")

// good
if (key is PGPSecretKeyRing) {
  return key
} else {
  throw IllegalArgumentException("Key is not a secret key")
}

// also good but depends on style? I prefer this but other people prefer the above. Not much difference
if (key is PGPSecretKeyRing) {
  return key
}
throw IllegalArgumentException("Key is not a secret key")

This is also good. Maybe the best - I find it succinct and clear:

// good
return (key as? PGPSecretKeyRing) ?: throw IllegalArgumentException("Key is not a secret key")

// if it doesn't fit on one line, a `?: throw` on next line is also good
return (key as? PGPSecretKeyRing)
  ?: throw IllegalArgumentException("Key is not a secret key")

I think everybody will have a little different idea, but I think this is a decent compromise.

return encryptKey(decryptKey(key, oldPassphrase), newPassphrase)
}

private fun extractSecretKey(armored: String): PGPSecretKeyRing {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

DenBond7
DenBond7 previously approved these changes Mar 25, 2021
Copy link
Collaborator

@DenBond7 DenBond7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks good to me. Approved

@DenBond7 DenBond7 merged commit 9043ec4 into master Mar 25, 2021
@DenBond7 DenBond7 deleted the ip-1007-do-not-ignore-errors branch March 25, 2021 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR submitted PR is submitted for this issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

encryptKey and decryptKey exceptions should not be ignored update readme or add script to check for compilation issues

3 participants