Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
TT-1211: put json type annotations back
Browse files Browse the repository at this point in the history
Earlier commit removed JsonTypeInfo annotations but they are actually required
Still don't add the defaultImpl because then there are errors in the latest version of jackson.

Authors: @hugh-emerson, @MichaelWalker
  • Loading branch information
Hugh Emerson committed Sep 22, 2017
1 parent 9f28eeb commit 6791713
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ build
*.ipr
*.iws
out/**
security-utils/out/
# Package Files #
*.log
apps-home
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package uk.gov.ida.common.shared.configuration;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.dropwizard.jackson.Discoverable;

import java.security.PublicKey;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
public interface DeserializablePublicKeyConfiguration extends Discoverable {
PublicKey getPublicKey();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.security.PublicKey;

@JsonDeserialize(using=EncodedCertificateDeserializer.class)
@JsonTypeName("base64")
public class EncodedCertificateConfiguration implements DeserializablePublicKeyConfiguration {
private PublicKey publicKey;
private String cert;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

@SuppressWarnings("unused")
@JsonDeserialize(using=EncodedPrivateKeyDeserializer.class)
@JsonTypeName("base64")
public class EncodedPrivateKeyConfiguration implements PrivateKeyConfiguration {

public EncodedPrivateKeyConfiguration(PrivateKey privateKey, String key) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package uk.gov.ida.common.shared.configuration;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.dropwizard.jackson.Discoverable;
import java.security.PrivateKey;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
public interface PrivateKeyConfiguration extends Discoverable {
PrivateKey getPrivateKey();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.ida.common.shared.configuration;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import javax.validation.Valid;
Expand All @@ -10,6 +11,7 @@

@SuppressWarnings("unused")
@JsonDeserialize(using=PrivateKeyFileDeserializer.class)
@JsonTypeName("file")
public class PrivateKeyFileConfiguration implements PrivateKeyConfiguration {

public PrivateKeyFileConfiguration(PrivateKey privateKey, String keyFile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.security.PublicKey;

@JsonDeserialize(using=PublicKeyDeserializer.class)
@JsonTypeName("file")
public class PublicKeyFileConfiguration implements DeserializablePublicKeyConfiguration {
private PublicKey publicKey;
private String cert;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void should_loadPublicKeyFromJSON() throws Exception {
String path = Resources.getResource("public_key.crt").getFile();
byte[] cert = Files.readAllBytes(new File(path).toPath());
String encodedCert = Base64.getEncoder().encodeToString(cert);
String jsonConfig = "{\"cert\": \"" + encodedCert + "\", \"name\": \"someId\"}";
String jsonConfig = "{\"type\": \"base64\", \"cert\": \"" + encodedCert + "\", \"name\": \"someId\"}";
EncodedCertificateConfiguration config = objectMapper.readValue(jsonConfig, EncodedCertificateConfiguration.class);

assertThat(config.getPublicKey().getAlgorithm()).isEqualTo("RSA");
Expand All @@ -38,20 +38,20 @@ public void should_ThrowExceptionWhenStringDoesNotContainAPublicKey() throws Exc
String path = Resources.getResource("private_key.pk8").getFile();
byte[] key = Files.readAllBytes(new File(path).toPath());
String encodedKey = Base64.getEncoder().encodeToString(key);
objectMapper.readValue("{\"cert\": \"" + encodedKey + "\", \"name\": \"someId\"}", EncodedCertificateConfiguration.class);
objectMapper.readValue("{\"type\": \"base64\", \"cert\": \"" + encodedKey + "\", \"name\": \"someId\"}", EncodedCertificateConfiguration.class);
}

@Test
public void should_ThrowExceptionWhenStringIsNotBase64Encoded() throws Exception {
thrown.expect(IllegalArgumentException.class);

objectMapper.readValue("{\"cert\": \"" + "FOOBARBAZ" + "\", \"name\": \"someId\"}", EncodedCertificateConfiguration.class);
objectMapper.readValue("{\"type\": \"base64\", \"cert\": \"" + "FOOBARBAZ" + "\", \"name\": \"someId\"}", EncodedCertificateConfiguration.class);
}

@Test(expected = IllegalStateException.class)
public void should_ThrowExceptionWhenIncorrectKeySpecified() throws Exception {
String path = getClass().getClassLoader().getResource("empty_file").getPath();
String jsonConfig = "{\"certFileFoo\": \"" + path + "\", \"name\": \"someId\"}";
String jsonConfig = "{\"type\": \"base64\", \"certFileFoo\": \"" + path + "\", \"name\": \"someId\"}";
objectMapper.readValue(jsonConfig, EncodedCertificateConfiguration.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void should_loadPrivateKeyFromJSON() throws Exception {
String path = Resources.getResource("private_key.pk8").getFile();
byte[] key = Files.readAllBytes(new File(path).toPath());
String encodedKey = Base64.getEncoder().encodeToString(key);
String jsonConfig = "{\"key\": \"" + encodedKey + "\"}";
String jsonConfig = "{\"type\": \"base64\", \"key\": \"" + encodedKey + "\"}";
EncodedPrivateKeyConfiguration configuration = objectMapper.readValue(jsonConfig, EncodedPrivateKeyConfiguration.class);
assertThat(configuration.getPrivateKey().getAlgorithm()).isEqualTo("RSA");
}
Expand All @@ -36,11 +36,11 @@ public void should_ThrowFooExceptionWhenKeyIsNotAPrivateKey() throws Exception {
thrown.expectCause(any(InvalidKeySpecException.class));

String key = "";
objectMapper.readValue("{\"key\": \"" + key + "\"}", EncodedPrivateKeyConfiguration.class);
objectMapper.readValue("{\"type\": \"base64\", \"key\": \"" + key + "\"}", EncodedPrivateKeyConfiguration.class);
}

@Test(expected = EncodedPrivateKeyDeserializer.PrivateKeyNotSpecifiedException.class)
public void should_throwAnExceptionWhenIncorrectFieldSpecified() throws Exception {
objectMapper.readValue("{\"privateKeyFoo\": \"" + "foobar" + "\"}", EncodedPrivateKeyConfiguration.class);
objectMapper.readValue("{\"type\": \"base64\", \"privateKeyFoo\": \"" + "foobar" + "\"}", EncodedPrivateKeyConfiguration.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public class PrivateKeyFileConfigurationTest {
@Test
public void should_loadPrivateKeyFromJSON() throws Exception {
String path = getClass().getClassLoader().getResource("private_key.pk8").getPath();
PrivateKeyFileConfiguration privateKeyFileConfiguration = objectMapper.readValue("{\"keyFile\": \"" + path + "\"}", PrivateKeyFileConfiguration.class);
PrivateKeyFileConfiguration privateKeyFileConfiguration = objectMapper.readValue("{\"type\": \"file\", \"keyFile\": \"" + path + "\"}", PrivateKeyFileConfiguration.class);

assertThat(privateKeyFileConfiguration.getPrivateKey().getAlgorithm()).isEqualTo("RSA");
}

@Test(expected = FileNotFoundException.class)
public void should_ThrowFooExceptionWhenFileDoesNotExist() throws Exception {
objectMapper.readValue("{\"keyFile\": \"/foo/bar\"}", PrivateKeyFileConfiguration.class);
objectMapper.readValue("{\"type\": \"file\", \"keyFile\": \"/foo/bar\"}", PrivateKeyFileConfiguration.class);
}

@Test
Expand All @@ -38,12 +38,12 @@ public void should_ThrowFooExceptionWhenFileDoesNotContainAPrivateKey() throws E
thrown.expectCause(any(InvalidKeySpecException.class));

String path = getClass().getClassLoader().getResource("empty_file").getPath();
objectMapper.readValue("{\"keyFile\": \"" + path + "\"}", PrivateKeyFileConfiguration.class);
objectMapper.readValue("{\"type\": \"file\", \"keyFile\": \"" + path + "\"}", PrivateKeyFileConfiguration.class);
}

@Test(expected = PrivateKeyFileDeserializer.PrivateKeyPathNotSpecifiedException.class)
public void should_throwAnExceptionWhenIncorrectKeySpecified() throws Exception {
String path = getClass().getClassLoader().getResource("empty_file").getPath();
objectMapper.readValue("{\"privateKeyFoo\": \"" + path + "\"}", PrivateKeyFileConfiguration.class);
objectMapper.readValue("{\"type\": \"file\", \"privateKeyFoo\": \"" + path + "\"}", PrivateKeyFileConfiguration.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public class PublicKeyFileConfigurationTest {
@Test
public void should_loadPublicKeyFromJSON() throws Exception {
String path = getClass().getClassLoader().getResource("public_key.crt").getPath();
PublicKeyFileConfiguration publicKeyConfiguration = objectMapper.readValue("{\"certFile\": \"" + path + "\", \"name\": \"someId\"}", PublicKeyFileConfiguration.class);
PublicKeyFileConfiguration publicKeyConfiguration = objectMapper.readValue("{\"type\": \"file\", \"certFile\": \"" + path + "\", \"name\": \"someId\"}", PublicKeyFileConfiguration.class);

assertThat(publicKeyConfiguration.getPublicKey().getAlgorithm()).isEqualTo("RSA");
}

@Test(expected = NoSuchFileException.class)
public void should_ThrowExceptionWhenFileDoesNotExist() throws Exception {
objectMapper.readValue("{\"certFile\": \"/foo/bar\", \"name\": \"someId\"}", PublicKeyFileConfiguration.class);
objectMapper.readValue("{\"type\": \"file\", \"certFile\": \"/foo/bar\", \"name\": \"someId\"}", PublicKeyFileConfiguration.class);
}

@Test
Expand All @@ -37,12 +37,12 @@ public void should_ThrowExceptionWhenFileDoesNotContainAPublicKey() throws Excep
thrown.expectCause(any(CertificateException.class));

String path = getClass().getClassLoader().getResource("empty_file").getPath();
objectMapper.readValue("{\"certFile\": \"" + path + "\", \"name\": \"someId\"}", PublicKeyFileConfiguration.class);
objectMapper.readValue("{\"type\": \"file\", \"certFile\": \"" + path + "\", \"name\": \"someId\"}", PublicKeyFileConfiguration.class);
}

@Test(expected = IllegalStateException.class)
public void should_ThrowExceptionWhenIncorrectKeySpecified() throws Exception {
String path = getClass().getClassLoader().getResource("empty_file").getPath();
objectMapper.readValue("{\"certFileFoo\": \"" + path + "\", \"name\": \"someId\"}", PublicKeyFileConfiguration.class);
objectMapper.readValue("{\"type\": \"file\", \"certFileFoo\": \"" + path + "\", \"name\": \"someId\"}", PublicKeyFileConfiguration.class);
}
}

0 comments on commit 6791713

Please sign in to comment.