diff --git a/encrypted-config-value/src/main/java/com/palantir/config/crypto/KeyWithAlgorithm.java b/encrypted-config-value/src/main/java/com/palantir/config/crypto/KeyWithAlgorithm.java index f7f0160c..5a81dac0 100644 --- a/encrypted-config-value/src/main/java/com/palantir/config/crypto/KeyWithAlgorithm.java +++ b/encrypted-config-value/src/main/java/com/palantir/config/crypto/KeyWithAlgorithm.java @@ -18,6 +18,8 @@ import static com.google.common.base.Preconditions.checkArgument; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; import com.google.common.io.BaseEncoding; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -34,6 +36,7 @@ public abstract class KeyWithAlgorithm { public abstract byte[] getKey(); @Override + @JsonValue public final String toString() { String encodedKeyString = BaseEncoding.base64().encode(getKey()); return getAlgorithm() + ":" + encodedKeyString; @@ -51,6 +54,7 @@ public static KeyWithAlgorithm from(String algorithm, byte[] key) { .build(); } + @JsonCreator public static KeyWithAlgorithm fromString(String keyWithAlgorithm) { checkArgument(keyWithAlgorithm.contains(":"), "Key must be in the format :"); diff --git a/encrypted-config-value/src/test/java/com/palantir/config/crypto/KeyWithAlgorithmTest.java b/encrypted-config-value/src/test/java/com/palantir/config/crypto/KeyWithAlgorithmTest.java new file mode 100644 index 00000000..5b823717 --- /dev/null +++ b/encrypted-config-value/src/test/java/com/palantir/config/crypto/KeyWithAlgorithmTest.java @@ -0,0 +1,45 @@ +/* + * Copyright 2015 Palantir Technologies, Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.palantir.config.crypto; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import org.junit.Test; + +public final class KeyWithAlgorithmTest { + private static final ObjectMapper mapper = new ObjectMapper(); + private static final String kwaString = "AES:rqrvWpLld+wKLOyxJYxQVg=="; + private static final KeyWithAlgorithm kwa = KeyWithAlgorithm.fromString(kwaString); + + @Test + public void testSerialization() + throws JsonParseException, JsonMappingException, JsonProcessingException, IOException { + String serialized = mapper.writeValueAsString(kwa); + + String expectedSerialization = String.format("\"%s\"", kwaString); + assertThat(serialized, is(expectedSerialization)); + + KeyWithAlgorithm deserialized = mapper.readValue(serialized, KeyWithAlgorithm.class); + assertThat(deserialized, is(kwa)); + } +}