Changesets · Branches · Tags · Compare
| Changeset | fcd94f559e2a074f53809cfb87b21c4291ee66922893cc6d8cc6d71c49f9d6dc |
|---|---|
| Author | Paul Latzelsperger <43503240+paullatzelsperger@users.noreply.github.com> · 2024-07-12 12:26 +02:00 |
| Committer | GitHub <noreply@github.com> · 2024-07-12 12:26 +02:00 |
| Parents | d3972eab6f92 |
10 files changed
modified core/common/connector-core/src/test/java/org/eclipse/edc/connector/core/LocalPublicKeyDefaultExtensionTest.java +2 −2
| @@ -58,7 +58,7 @@ | ||
| 58 | 58 | "key1.id", "key1", |
| 59 | 59 | "key1.value", "value"); |
| 60 | 60 | |
| 61 | - when(keyParserRegistry.parse("value")).thenReturn(Result.success(mock(PublicKey.class))); | |
| 61 | + when(keyParserRegistry.parsePublic("value")).thenReturn(Result.success(mock(PublicKey.class))); | |
| 62 | 62 | when(context.getConfig(EDC_PUBLIC_KEYS_PREFIX)).thenReturn(ConfigFactory.fromMap(keys)); |
| 63 | 63 | var localPublicKeyService = extension.localPublicKeyService(); |
| 64 | 64 | extension.initialize(context); |
| @@ -75,7 +75,7 @@ | ||
| 75 | 75 | "key1.id", "key1", |
| 76 | 76 | "key1.path", path.getPath()); |
| 77 | 77 | |
| 78 | - when(keyParserRegistry.parse(value)).thenReturn(Result.success(mock(PublicKey.class))); | |
| 78 | + when(keyParserRegistry.parsePublic(value)).thenReturn(Result.success(mock(PublicKey.class))); | |
| 79 | 79 | when(context.getConfig(EDC_PUBLIC_KEYS_PREFIX)).thenReturn(ConfigFactory.fromMap(keys)); |
| 80 | 80 | var localPublicKeyService = extension.localPublicKeyService(); |
| 81 | 81 | extension.initialize(context); |
modified core/common/lib/keys-lib/src/main/java/org/eclipse/edc/keys/KeyParserRegistryImpl.java +9 −0
| @@ -19,6 +19,7 @@ | ||
| 19 | 19 | import org.eclipse.edc.spi.result.Result; |
| 20 | 20 | |
| 21 | 21 | import java.security.Key; |
| 22 | +import java.security.PublicKey; | |
| 22 | 23 | import java.util.ArrayList; |
| 23 | 24 | import java.util.List; |
| 24 | 25 | |
| @@ -37,4 +38,12 @@ | ||
| 37 | 38 | .map(kp -> kp.parse(encoded)) |
| 38 | 39 | .orElseGet(() -> Result.failure("No parser found that can handle that format.")); |
| 39 | 40 | } |
| 41 | + | |
| 42 | + @Override | |
| 43 | + public Result<PublicKey> parsePublic(String encoded) { | |
| 44 | + return parsers.stream().filter(kp -> kp.canHandle(encoded)) | |
| 45 | + .findFirst() | |
| 46 | + .map(kp -> kp.parsePublic(encoded).compose(k -> Result.success((PublicKey) k))) | |
| 47 | + .orElseGet(() -> Result.failure("No parser found that can handle that format.")); | |
| 48 | + } | |
| 40 | 49 | } |
modified core/common/lib/keys-lib/src/main/java/org/eclipse/edc/keys/LocalPublicKeyServiceImpl.java +5 −11
| @@ -48,6 +48,10 @@ | ||
| 48 | 48 | .orElseGet(() -> Result.failure("No public key could be resolved for key-ID '%s'".formatted(id))); |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | + public Result<Void> addRawKey(String id, String rawKey) { | |
| 52 | + return parseKey(rawKey).onSuccess((pk) -> cachedKeys.put(id, pk)).mapEmpty(); | |
| 53 | + } | |
| 54 | + | |
| 51 | 55 | private Optional<String> resolveFromVault(String id) { |
| 52 | 56 | return Optional.ofNullable(vault.resolveSecret(id)); |
| 53 | 57 | } |
| @@ -57,16 +61,6 @@ | ||
| 57 | 61 | } |
| 58 | 62 | |
| 59 | 63 | private Result<PublicKey> parseKey(String encodedKey) { |
| 60 | - return registry.parse(encodedKey).compose(pk -> { | |
| 61 | - if (pk instanceof PublicKey publicKey) { | |
| 62 | - return Result.success(publicKey); | |
| 63 | - } else { | |
| 64 | - return Result.failure("The specified resource did not contain public key material."); | |
| 65 | - } | |
| 66 | - }); | |
| 67 | - } | |
| 68 | - | |
| 69 | - public Result<Void> addRawKey(String id, String rawKey) { | |
| 70 | - return parseKey(rawKey).onSuccess((pk) -> cachedKeys.put(id, pk)).mapTo(); | |
| 64 | + return registry.parsePublic(encodedKey); | |
| 71 | 65 | } |
| 72 | 66 | } |
modified core/common/lib/keys-lib/src/main/java/org/eclipse/edc/keys/keyparsers/JwkParser.java +22 −1
| @@ -86,8 +86,9 @@ | ||
| 86 | 86 | @Override |
| 87 | 87 | public Result<Key> parse(String encoded) { |
| 88 | 88 | try { |
| 89 | - var jwk = JWK.parse(encoded); | |
| 90 | 89 | |
| 90 | + | |
| 91 | + var jwk = JWK.parse(encoded); | |
| 91 | 92 | // OctetKeyPairs (OKP) need special handling, as they can't be easily converted to a Java PrivateKey |
| 92 | 93 | if (jwk instanceof OctetKeyPair okp) { |
| 93 | 94 | return parseOctetKeyPair(okp).map(key -> key); |
| @@ -107,6 +108,26 @@ | ||
| 107 | 108 | } |
| 108 | 109 | } |
| 109 | 110 | |
| 111 | + @Override | |
| 112 | + public Result<Key> parsePublic(String encoded) { | |
| 113 | + try { | |
| 114 | + var jwk = JWK.parse(encoded).toPublicJWK(); | |
| 115 | + // OctetKeyPairs (OKP) need special handling, as they can't be easily converted to a Java PrivateKey | |
| 116 | + if (jwk instanceof OctetKeyPair okp) { | |
| 117 | + return parseOctetKeyPair(okp.toPublicJWK()).map(key -> key); | |
| 118 | + } | |
| 119 | + var list = KeyConverter.toJavaKeys(List.of(jwk)); // contains an entry each for public and private key | |
| 120 | + | |
| 121 | + return list.stream() | |
| 122 | + .findFirst() | |
| 123 | + .map(Result::success) | |
| 124 | + .orElse(Result.failure(ERROR_NO_KEY)); | |
| 125 | + } catch (ParseException | NoSuchAlgorithmException | IOException | InvalidKeySpecException e) { | |
| 126 | + monitor.warning("Parser error", e); | |
| 127 | + return Result.failure("Parser error: " + e.getMessage()); | |
| 128 | + } | |
| 129 | + } | |
| 130 | + | |
| 110 | 131 | private Result<? extends Key> parseOctetKeyPair(OctetKeyPair okp) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException { |
| 111 | 132 | var d = okp.getDecodedD(); |
| 112 | 133 | var x = okp.getDecodedX(); |
modified core/common/lib/keys-lib/src/main/java/org/eclipse/edc/keys/keyparsers/PemParser.java +33 −6
| @@ -62,11 +62,7 @@ | ||
| 62 | 62 | @Override |
| 63 | 63 | public Result<Key> parse(String encoded) { |
| 64 | 64 | |
| 65 | - var matcher = PEM_FORMAT_REGEX.matcher(encoded); | |
| 66 | - if (!matcher.find()) { | |
| 67 | - return Result.failure("The given input is not valid PEM."); | |
| 68 | - } | |
| 69 | - var keypair = parseKeys(encoded); | |
| 65 | + var keypair = parsePem(encoded); | |
| 70 | 66 | |
| 71 | 67 | if (keypair.succeeded()) { |
| 72 | 68 | |
| @@ -83,7 +79,38 @@ | ||
| 83 | 79 | .orElseGet(() -> Result.failure("PEM-encoded structure did not contain a private key.")); |
| 84 | 80 | } |
| 85 | 81 | |
| 86 | - return keypair.mapTo(); | |
| 82 | + return keypair.mapEmpty(); | |
| 83 | + } | |
| 84 | + | |
| 85 | + @Override | |
| 86 | + public Result<Key> parsePublic(String encoded) { | |
| 87 | + | |
| 88 | + var keypair = parsePem(encoded); | |
| 89 | + if (keypair.succeeded()) { | |
| 90 | + | |
| 91 | + var keyPairList = keypair.getContent(); | |
| 92 | + if (keyPairList.size() > 1) { | |
| 93 | + monitor.warning("PEM expected to contain exactly 1 key(-pair), but contained %s. Will take the first one. Please consider re-structuring your PEM document.".formatted(keyPairList.size())); | |
| 94 | + } | |
| 95 | + return keyPairList | |
| 96 | + .stream() | |
| 97 | + .filter(Objects::nonNull) // PEM strings that only contain public keys would get eliminated here | |
| 98 | + .map(keyPair -> (Key) keyPair.getPublic()) | |
| 99 | + .filter(Objects::nonNull) | |
| 100 | + .findFirst() | |
| 101 | + .map(Result::success) | |
| 102 | + .orElseGet(() -> Result.failure("PEM-encoded structure did not contain a public key.")); | |
| 103 | + } | |
| 104 | + | |
| 105 | + return keypair.mapEmpty(); | |
| 106 | + } | |
| 107 | + | |
| 108 | + private Result<List<KeyPair>> parsePem(String pemEncoded) { | |
| 109 | + var matcher = PEM_FORMAT_REGEX.matcher(pemEncoded); | |
| 110 | + if (!matcher.find()) { | |
| 111 | + return Result.failure("The given input is not valid PEM."); | |
| 112 | + } | |
| 113 | + return parseKeys(pemEncoded); | |
| 87 | 114 | } |
| 88 | 115 | |
| 89 | 116 | /** |
modified core/common/lib/keys-lib/src/test/java/org/eclipse/edc/keys/LocalPublicKeyServiceImplTest.java +4 −14
| @@ -22,7 +22,6 @@ | ||
| 22 | 22 | import org.junit.jupiter.api.Test; |
| 23 | 23 | import org.mockito.Mockito; |
| 24 | 24 | |
| 25 | -import java.security.PrivateKey; | |
| 26 | 25 | import java.security.PublicKey; |
| 27 | 26 | |
| 28 | 27 | import static org.mockito.Mockito.mock; |
| @@ -31,9 +30,9 @@ | ||
| 31 | 30 | |
| 32 | 31 | class LocalPublicKeyServiceImplTest { |
| 33 | 32 | |
| 34 | - private LocalPublicKeyServiceImpl localPublicKeyService; | |
| 35 | 33 | private final Vault vault = mock(); |
| 36 | 34 | private final KeyParserRegistry keyParserRegistry = mock(); |
| 35 | + private LocalPublicKeyServiceImpl localPublicKeyService; | |
| 37 | 36 | |
| 38 | 37 | @BeforeEach |
| 39 | 38 | void setup() { |
| @@ -42,7 +41,7 @@ | ||
| 42 | 41 | |
| 43 | 42 | @Test |
| 44 | 43 | void resolve_withCache() { |
| 45 | - when(keyParserRegistry.parse("value")).thenReturn(Result.success(mock(PublicKey.class))); | |
| 44 | + when(keyParserRegistry.parsePublic("value")).thenReturn(Result.success(mock(PublicKey.class))); | |
| 46 | 45 | localPublicKeyService.addRawKey("id", "value"); |
| 47 | 46 | AbstractResultAssert.assertThat(localPublicKeyService.resolveKey("id")).isSucceeded(); |
| 48 | 47 | Mockito.verifyNoInteractions(vault); |
| @@ -51,7 +50,7 @@ | ||
| 51 | 50 | @Test |
| 52 | 51 | void resolve_withVault() { |
| 53 | 52 | when(vault.resolveSecret("id")).thenReturn("value"); |
| 54 | - when(keyParserRegistry.parse("value")).thenReturn(Result.success(mock(PublicKey.class))); | |
| 53 | + when(keyParserRegistry.parsePublic("value")).thenReturn(Result.success(mock(PublicKey.class))); | |
| 55 | 54 | AbstractResultAssert.assertThat(localPublicKeyService.resolveKey("id")).isSucceeded(); |
| 56 | 55 | |
| 57 | 56 | verify(vault).resolveSecret("id"); |
| @@ -63,19 +62,10 @@ | ||
| 63 | 62 | verify(vault).resolveSecret("id"); |
| 64 | 63 | } |
| 65 | 64 | |
| 66 | - @Test | |
| 67 | - void resolve_wrongKeyType() { | |
| 68 | - when(vault.resolveSecret("id")).thenReturn("value"); | |
| 69 | - when(keyParserRegistry.parse("value")).thenReturn(Result.success(mock(PrivateKey.class))); | |
| 70 | - | |
| 71 | - AbstractResultAssert.assertThat(localPublicKeyService.resolveKey("id")).isFailed(); | |
| 72 | - verify(vault).resolveSecret("id"); | |
| 73 | - } | |
| 74 | - | |
| 75 | 65 | @Test |
| 76 | 66 | void resolve_wrongKeyFormat() { |
| 77 | 67 | when(vault.resolveSecret("id")).thenReturn("value"); |
| 78 | - when(keyParserRegistry.parse("value")).thenReturn(Result.failure("failure")); | |
| 68 | + when(keyParserRegistry.parsePublic("value")).thenReturn(Result.failure("failure")); | |
| 79 | 69 | |
| 80 | 70 | AbstractResultAssert.assertThat(localPublicKeyService.resolveKey("id")).isFailed(); |
| 81 | 71 | verify(vault).resolveSecret("id"); |
modified core/common/lib/keys-lib/src/test/java/org/eclipse/edc/keys/keyparsers/JwkParserTest.java +17 −0
| @@ -63,6 +63,23 @@ | ||
| 63 | 63 | assertThat(result).isSucceeded().isInstanceOf(PublicKey.class); |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | + | |
| 67 | + @ParameterizedTest | |
| 68 | + @ArgumentsSource(KeyProvider.class) | |
| 69 | + void parsePublic_withPublicKey(JWK jwk) { | |
| 70 | + var publickey = jwk.toPublicJWK(); | |
| 71 | + var result = parser.parsePublic(publickey.toJSONString()); | |
| 72 | + assertThat(result).isSucceeded().isInstanceOf(PublicKey.class); | |
| 73 | + } | |
| 74 | + | |
| 75 | + | |
| 76 | + @ParameterizedTest | |
| 77 | + @ArgumentsSource(KeyProvider.class) | |
| 78 | + void parsePublic_withPrivateKey(JWK jwk) { | |
| 79 | + var result = parser.parsePublic(jwk.toJSONString()); | |
| 80 | + assertThat(result).isSucceeded().isInstanceOf(PublicKey.class); | |
| 81 | + } | |
| 82 | + | |
| 66 | 83 | private static class KeyProvider implements ArgumentsProvider { |
| 67 | 84 | @Override |
| 68 | 85 | public Stream<? extends Arguments> provideArguments(ExtensionContext context) { |
modified core/common/lib/keys-lib/src/test/java/org/eclipse/edc/keys/keyparsers/PemParserTest.java +46 −0
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | import org.assertj.core.api.Assertions; |
| 18 | 18 | import org.eclipse.edc.junit.testfixtures.TestUtils; |
| 19 | 19 | import org.junit.jupiter.api.Named; |
| 20 | +import org.junit.jupiter.api.Test; | |
| 20 | 21 | import org.junit.jupiter.api.extension.ExtensionContext; |
| 21 | 22 | import org.junit.jupiter.params.ParameterizedTest; |
| 22 | 23 | import org.junit.jupiter.params.provider.Arguments; |
| @@ -74,6 +75,38 @@ | ||
| 74 | 75 | .isInstanceOf(PublicKey.class); |
| 75 | 76 | } |
| 76 | 77 | |
| 78 | + | |
| 79 | + @ParameterizedTest | |
| 80 | + @ArgumentsSource(PemConvertiblePrivateKeyProvider.class) | |
| 81 | + void parsePublic_withPrivateKey(String pem) { | |
| 82 | + var result = parser.parsePublic(pem); | |
| 83 | + assertThat(result) | |
| 84 | + .isSucceeded() | |
| 85 | + .isNotNull() | |
| 86 | + .isInstanceOf(PublicKey.class); | |
| 87 | + | |
| 88 | + } | |
| 89 | + | |
| 90 | + @Test | |
| 91 | + void parsePublic_withPrivateKey_whenEd25519_shouldFail() { | |
| 92 | + var pem = TestUtils.getResourceFileContentAsString("ed25519.pem"); | |
| 93 | + var result = parser.parsePublic(pem); | |
| 94 | + assertThat(result) | |
| 95 | + .isFailed() | |
| 96 | + .detail().isEqualTo("PEM-encoded structure did not contain a public key."); | |
| 97 | + } | |
| 98 | + | |
| 99 | + | |
| 100 | + @ParameterizedTest | |
| 101 | + @ArgumentsSource(PemPublicKeyProvider.class) | |
| 102 | + void parsePublic_withPublicKey(String pem) { | |
| 103 | + | |
| 104 | + assertThat(parser.parsePublic(pem)) | |
| 105 | + .isSucceeded() | |
| 106 | + .isNotNull() | |
| 107 | + .isInstanceOf(PublicKey.class); | |
| 108 | + } | |
| 109 | + | |
| 77 | 110 | private static class PemPrivateKeyProvider implements ArgumentsProvider { |
| 78 | 111 | |
| 79 | 112 | @Override |
| @@ -88,6 +121,19 @@ | ||
| 88 | 121 | } |
| 89 | 122 | } |
| 90 | 123 | |
| 124 | + private static class PemConvertiblePrivateKeyProvider implements ArgumentsProvider { | |
| 125 | + | |
| 126 | + @Override | |
| 127 | + public Stream<? extends Arguments> provideArguments(ExtensionContext context) { | |
| 128 | + return Stream.of( | |
| 129 | + Arguments.of(Named.named("RSA PrivateKey", TestUtils.getResourceFileContentAsString("rsa_2048.pem"))), | |
| 130 | + Arguments.of(Named.named("EC PrivateKey (P256)", TestUtils.getResourceFileContentAsString("ec_p256.pem"))), | |
| 131 | + Arguments.of(Named.named("EC PrivateKey (P384)", TestUtils.getResourceFileContentAsString("ec_p384.pem"))), | |
| 132 | + Arguments.of(Named.named("EC PrivateKey (P512)", TestUtils.getResourceFileContentAsString("ec_p512.pem"))) | |
| 133 | + ); | |
| 134 | + } | |
| 135 | + } | |
| 136 | + | |
| 91 | 137 | private static class PemPublicKeyProvider implements ArgumentsProvider { |
| 92 | 138 | |
| 93 | 139 | @Override |
modified spi/common/keys-spi/src/main/java/org/eclipse/edc/keys/spi/KeyParser.java +16 −0
| @@ -49,4 +49,20 @@ | ||
| 49 | 49 | * @return Either a {@link java.security.PrivateKey}, a {@link java.security.PublicKey} or a failure. |
| 50 | 50 | */ |
| 51 | 51 | Result<Key> parse(String encoded); |
| 52 | + | |
| 53 | + /** | |
| 54 | + * Parses the encoded key as public key. If the encoded string is invalid, or the parser can't handle the input, | |
| 55 | + * it must return a {@link Result#failure(String)}, it must never throw an exception. | |
| 56 | + * <p> | |
| 57 | + * If the given key material contains public and private key data, the parser attempts to remove the private key data, | |
| 58 | + * returning only the public part of the key as {@link java.security.PublicKey}. | |
| 59 | + * If the given key material does not contain private key data, just public key data, returns a {@link java.security.PublicKey}. In all | |
| 60 | + * other cases, a {@link Result#failure(String)} is returned, for example, when a private key cannot be converted into a public key. | |
| 61 | + * | |
| 62 | + * @param encoded serialized/encoded key material. | |
| 63 | + * @return Either a {@link java.security.PublicKey} or a failure. | |
| 64 | + */ | |
| 65 | + default Result<Key> parsePublic(String encoded) { | |
| 66 | + return parse(encoded); | |
| 67 | + } | |
| 52 | 68 | } |
modified spi/common/keys-spi/src/main/java/org/eclipse/edc/keys/spi/KeyParserRegistry.java +12 −2
| @@ -17,7 +17,7 @@ | ||
| 17 | 17 | import org.eclipse.edc.spi.result.Result; |
| 18 | 18 | |
| 19 | 19 | import java.security.Key; |
| 20 | -import java.security.PrivateKey; | |
| 20 | +import java.security.PublicKey; | |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | 23 | * Registry that holds multiple {@link KeyParser} instances that are used to deserialize a private key from their |
| @@ -30,11 +30,21 @@ | ||
| 30 | 30 | void register(KeyParser parser); |
| 31 | 31 | |
| 32 | 32 | /** |
| 33 | - * Attempts to parse the String representation of a private key into a {@link PrivateKey}. If no parser can handle | |
| 33 | + * Attempts to parse the String representation of a private key into a {@link Key}. If no parser can handle | |
| 34 | 34 | * the encoded format, or it is corrupt etc. then a failure is returned. |
| 35 | 35 | * |
| 36 | 36 | * @param encoded The private key in encoded format (PEM, OpenSSH, JWK, PKCS8,...) |
| 37 | 37 | * @return a success result containing the private key, a failure if the encoded private key could not be deserialized. |
| 38 | 38 | */ |
| 39 | 39 | Result<Key> parse(String encoded); |
| 40 | + | |
| 41 | + /** | |
| 42 | + * Attempts to parse the String representation of a public or private key into a {@link Key}. If no parser can handle | |
| 43 | + * the encoded format, if the encoded format contains a private key that cannot be converted to a public key, | |
| 44 | + * or if the input is corrupt etc. then a failure is returned. | |
| 45 | + * | |
| 46 | + * @param encoded The private key in encoded format (PEM, OpenSSH, JWK, PKCS8,...) | |
| 47 | + * @return a success result containing the public key, a failure if the encoded public key could not be deserialized. | |
| 48 | + */ | |
| 49 | + Result<PublicKey> parsePublic(String encoded); | |
| 40 | 50 | } |