Skip to content

Commit fbe68de

Browse files
committed
impl: load the public key from a resource file
Initially I thought about embedding it as a const string in the code but the string is too big, best to save it as resource file. The code changes are mostly related to loading the key from the file.
1 parent 4cd5148 commit fbe68de

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

src/main/kotlin/com/coder/toolbox/cli/gpg/GPGVerifier.kt

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import java.io.ByteArrayInputStream
1818
import java.nio.file.Files
1919
import java.nio.file.Path
2020

21+
2122
class GPGVerifier(
2223
private val context: CoderToolboxContext,
2324
) {
@@ -48,23 +49,29 @@ class GPGVerifier(
4849
}
4950

5051
private fun getCoderPublicKeyRing(): PGPPublicKeyRing {
51-
return try {
52-
getDefaultCoderPublicKeyRing()
52+
try {
53+
val coderPublicKey = javaClass.getResourceAsStream("/META-INF/trusted-keys/pgp-public.key")
54+
?.readAllBytes() ?: throw IllegalStateException("Trusted public key not found")
55+
return loadPublicKeyRing(coderPublicKey)
5356
} catch (e: Exception) {
5457
throw PGPException("Failed to load Coder public GPG key", e)
5558
}
5659
}
5760

58-
private fun getDefaultCoderPublicKeyRing(): PGPPublicKeyRing {
59-
val coderPublicKey = """
60-
-----BEGIN PGP PUBLIC KEY BLOCK-----
61-
62-
# Replace this with Coder's actual public key
63-
64-
-----END PGP PUBLIC KEY BLOCK-----
65-
""".trimIndent()
66-
67-
return loadPublicKeyRing(coderPublicKey.toByteArray())
61+
/**
62+
* Load public key ring from bytes
63+
*/
64+
fun loadPublicKeyRing(publicKeyBytes: ByteArray): PGPPublicKeyRing {
65+
return try {
66+
val keyInputStream = ArmoredInputStream(ByteArrayInputStream(publicKeyBytes))
67+
val keyRingCollection = PGPPublicKeyRingCollection(
68+
PGPUtil.getDecoderStream(keyInputStream),
69+
JcaKeyFingerprintCalculator()
70+
)
71+
keyRingCollection.keyRings.next()
72+
} catch (e: Exception) {
73+
throw PGPException("Failed to load public key ring", e)
74+
}
6875
}
6976

7077
/**
@@ -103,20 +110,4 @@ class GPGVerifier(
103110
return Failed(e)
104111
}
105112
}
106-
107-
/**
108-
* Load public key ring from bytes
109-
*/
110-
fun loadPublicKeyRing(publicKeyBytes: ByteArray): PGPPublicKeyRing {
111-
return try {
112-
val keyInputStream = ArmoredInputStream(ByteArrayInputStream(publicKeyBytes))
113-
val keyRingCollection = PGPPublicKeyRingCollection(
114-
PGPUtil.getDecoderStream(keyInputStream),
115-
JcaKeyFingerprintCalculator()
116-
)
117-
keyRingCollection.keyRings.next()
118-
} catch (e: Exception) {
119-
throw PGPException("Failed to load public key ring", e)
120-
}
121-
}
122113
}

0 commit comments

Comments
 (0)