diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/HostKeyAlgoSupportedValidator.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/HostKeyAlgoSupportedValidator.java index 335fe3a4..d75d2e48 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/HostKeyAlgoSupportedValidator.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/HostKeyAlgoSupportedValidator.java @@ -52,9 +52,9 @@ public class HostKeyAlgoSupportedValidator implements ConstraintValidator validationResults = new HashSet<>(); - List extractedProperties = sshPropertyValidator.extractRepoProperties(sshUriProperties); + List extractedProperties = sshPropertyValidator.extractRepoProperties(sshUriProperties); - for (SshUriProperties extractedProperty : extractedProperties) { + for (SshUri extractedProperty : extractedProperties) { if (sshUriProperties.isIgnoreLocalSshSettings() && isSshUri(extractedProperty.getUri())) { validationResults.add(isHostKeySpecifiedWhenAlgorithmSet(extractedProperty, context)); } @@ -62,7 +62,7 @@ public class HostKeyAlgoSupportedValidator implements ConstraintValidator validationResults = new HashSet<>(); - List extractedProperties = sshPropertyValidator.extractRepoProperties(sshUriProperties); + List extractedProperties = sshPropertyValidator.extractRepoProperties(sshUriProperties); - for (SshUriProperties extractedProperty : extractedProperties) { + for (SshUri extractedProperty : extractedProperties) { if (sshUriProperties.isIgnoreLocalSshSettings() && isSshUri(extractedProperty.getUri())) { validationResults.add( isAlgorithmSpecifiedWhenHostKeySet(extractedProperty, context) @@ -60,7 +60,7 @@ public class HostKeyAndAlgoBothExistValidator implements ConstraintValidator validationResults = new HashSet<>(); - List extractedProperties = sshPropertyValidator.extractRepoProperties(sshUriProperties); + List extractedProperties = sshPropertyValidator.extractRepoProperties(sshUriProperties); - for (SshUriProperties extractedProperty : extractedProperties) { + for (SshUri extractedProperty : extractedProperties) { if (sshUriProperties.isIgnoreLocalSshSettings() && isSshUri(extractedProperty.getUri())) { validationResults.add( isPrivateKeyPresent(extractedProperty, context) @@ -65,7 +65,7 @@ public class PrivateKeyValidator implements ConstraintValidator sshKeysByHostname; + private final Map sshKeysByHostname; private final JSch jSch; - public PropertyBasedSshSessionFactory(Map sshKeysByHostname, JSch jSch) { + public PropertyBasedSshSessionFactory(Map sshKeysByHostname, JSch jSch) { this.sshKeysByHostname = sshKeysByHostname; this.jSch = jSch; } @Override protected void configure(Host hc, Session session) { - SshUriProperties sshProperties = sshKeysByHostname.get(hc.getHostName()); + SshUri sshProperties = sshKeysByHostname.get(hc.getHostName()); String hostKeyAlgorithm = sshProperties.getHostKeyAlgorithm(); if (hostKeyAlgorithm != null) { session.setConfig(SERVER_HOST_KEY, hostKeyAlgorithm); @@ -64,7 +64,7 @@ public class PropertyBasedSshSessionFactory extends JschConfigSessionFactory { @Override protected Session createSession(Host hc, String user, String host, int port, FS fs) throws JSchException { if (sshKeysByHostname.containsKey(host)) { - SshUriProperties sshUriProperties = sshKeysByHostname.get(host); + SshUri sshUriProperties = sshKeysByHostname.get(host); jSch.addIdentity(host, sshUriProperties.getPrivateKey().getBytes(), null, null); if (sshUriProperties.getHostKey() != null) { HostKey hostkey = new HostKey(host, Base64.decode(sshUriProperties.getHostKey())); diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/SshPropertyValidator.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/SshPropertyValidator.java index b289587c..6b1b1475 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/SshPropertyValidator.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/SshPropertyValidator.java @@ -55,10 +55,10 @@ public class SshPropertyValidator { return false; } - protected List extractRepoProperties(SshUriProperties sshUriProperties) { - List allRepoProperties = new ArrayList<>(); + protected List extractRepoProperties(SshUriProperties sshUriProperties) { + List allRepoProperties = new ArrayList<>(); allRepoProperties.add(sshUriProperties); - Map repos = sshUriProperties.getRepos(); + Map repos = sshUriProperties.getRepos(); if (repos != null) { allRepoProperties.addAll(repos.values()); } diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/SshUri.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/SshUri.java new file mode 100644 index 00000000..109d6a31 --- /dev/null +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/SshUri.java @@ -0,0 +1,150 @@ +package org.springframework.cloud.config.server.ssh; + +import org.springframework.cloud.config.server.ssh.SshUriProperties.SshUriNestedRepoProperties; + +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * Base class that contains configuration properties for Git SSH properties + * + * @author Ollie Hughes + */ +public abstract class SshUri { + private String privateKey; + private String uri; + private String hostKeyAlgorithm; + private String hostKey; + private boolean ignoreLocalSshSettings; + private boolean strictHostKeyChecking = true; + + public static SshUriPropertiesBuilder builder() { + return new SshUriPropertiesBuilder(); + } + + public String getUri() { + return this.uri; + } + + public String getHostKeyAlgorithm() { + return this.hostKeyAlgorithm; + } + + public String getHostKey() { + return this.hostKey; + } + + public String getPrivateKey() { + return this.privateKey; + } + + public boolean isIgnoreLocalSshSettings() { + return this.ignoreLocalSshSettings; + } + + public boolean isStrictHostKeyChecking() { + return this.strictHostKeyChecking; + } + + public void setUri(String uri) { + this.uri = uri; + } + + public void setHostKeyAlgorithm(String hostKeyAlgorithm) { + this.hostKeyAlgorithm = hostKeyAlgorithm; + } + + public void setHostKey(String hostKey) { + this.hostKey = hostKey; + } + + public void setPrivateKey(String privateKey) { + this.privateKey = privateKey; + } + + public void setIgnoreLocalSshSettings(boolean ignoreLocalSshSettings) { + this.ignoreLocalSshSettings = ignoreLocalSshSettings; + } + + public void setStrictHostKeyChecking(boolean strictHostKeyChecking) { + this.strictHostKeyChecking = strictHostKeyChecking; + } + + public String toString() { + return "org.springframework.cloud.config.server.ssh.SshUriProperties(uri=" + this.getUri() + " hostKeyAlgorithm=" + this.getHostKeyAlgorithm() + ", hostKey=" + this.getHostKey() + ", privateKey=" + this.getPrivateKey() + ", ignoreLocalSshSettings=" + this.isIgnoreLocalSshSettings() + ", strictHostKeyChecking=" + this.isStrictHostKeyChecking() + ",)"; + } + + public static class SshUriPropertiesBuilder { + private String uri; + private String hostKeyAlgorithm; + private String hostKey; + private String privateKey; + private boolean ignoreLocalSshSettings; + private boolean strictHostKeyChecking = true; + private Map repos = new LinkedHashMap<>(); + + SshUriPropertiesBuilder() { + } + + public SshUri.SshUriPropertiesBuilder uri(String uri) { + this.uri = uri; + return this; + } + + public SshUri.SshUriPropertiesBuilder hostKeyAlgorithm(String hostKeyAlgorithm) { + this.hostKeyAlgorithm = hostKeyAlgorithm; + return this; + } + + public SshUri.SshUriPropertiesBuilder hostKey(String hostKey) { + this.hostKey = hostKey; + return this; + } + + public SshUri.SshUriPropertiesBuilder privateKey(String privateKey) { + this.privateKey = privateKey; + return this; + } + + public SshUri.SshUriPropertiesBuilder ignoreLocalSshSettings(boolean ignoreLocalSshSettings) { + this.ignoreLocalSshSettings = ignoreLocalSshSettings; + return this; + } + + public SshUri.SshUriPropertiesBuilder strictHostKeyChecking(boolean strictHostKeyChecking) { + this.strictHostKeyChecking = strictHostKeyChecking; + return this; + } + + public SshUri.SshUriPropertiesBuilder repos(Map repos) { + this.repos = repos; + return this; + } + + public SshUriProperties build() { + SshUriProperties sshUriProperties = new SshUriProperties(); + sshUriProperties.setRepos(repos); + build(sshUriProperties); + return sshUriProperties; + } + + public SshUriNestedRepoProperties buildAsNestedRepo() { + SshUriNestedRepoProperties sshUriNestedRepoProperties = new SshUriNestedRepoProperties(); + build(sshUriNestedRepoProperties); + return sshUriNestedRepoProperties; + } + + private void build(SshUri sshUriNestedRepoProperties) { + sshUriNestedRepoProperties.setUri(uri); + sshUriNestedRepoProperties.setHostKeyAlgorithm(hostKeyAlgorithm); + sshUriNestedRepoProperties.setHostKey(hostKey); + sshUriNestedRepoProperties.setPrivateKey(privateKey); + sshUriNestedRepoProperties.setIgnoreLocalSshSettings(ignoreLocalSshSettings); + sshUriNestedRepoProperties.setStrictHostKeyChecking(strictHostKeyChecking); + } + + public String toString() { + return "org.springframework.cloud.config.server.ssh.SshUriProperties.SshUriPropertiesBuilder(uri=" + this.uri + "hostKeyAlgorithm=" + this.hostKeyAlgorithm + ", hostKey=" + this.hostKey + ", privateKey=" + this.privateKey + ", ignoreLocalSshSettings=" + this.ignoreLocalSshSettings + ", strictHostKeyChecking=" + this.strictHostKeyChecking + ", repos=" + this.repos + ")"; + } + } +} diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/SshUriProperties.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/SshUriProperties.java index 5c8124d9..c5a25ddb 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/SshUriProperties.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/SshUriProperties.java @@ -15,7 +15,7 @@ */ package org.springframework.cloud.config.server.ssh; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -31,150 +31,33 @@ import org.springframework.validation.annotation.Validated; @PrivateKeyIsValid @HostKeyAndAlgoBothExist @HostKeyAlgoSupported -public class SshUriProperties { - private String privateKey; - private String uri; - private String hostKeyAlgorithm; - private String hostKey; - private boolean ignoreLocalSshSettings; - private boolean strictHostKeyChecking = true; +public class SshUriProperties extends SshUri { - private Map repos = new HashMap<>(); + private Map repos = new LinkedHashMap<>(); - public SshUriProperties(String uri, String hostKeyAlgorithm, String hostKey, String privateKey, boolean ignoreLocalSshSettings, boolean strictHostKeyChecking, Map repos) { - this.uri = uri; - this.hostKeyAlgorithm = hostKeyAlgorithm; - this.hostKey = hostKey; - this.privateKey = privateKey; - this.ignoreLocalSshSettings = ignoreLocalSshSettings; - this.strictHostKeyChecking = strictHostKeyChecking; - this.repos = repos; - } - - public SshUriProperties() { - } - - public static SshUriPropertiesBuilder builder() { - return new SshUriPropertiesBuilder(); - } - - public String getUri() { - return this.uri; - } - - public String getHostKeyAlgorithm() { - return this.hostKeyAlgorithm; - } - - public String getHostKey() { - return this.hostKey; - } - - public String getPrivateKey() { - return this.privateKey; - } - - public boolean isIgnoreLocalSshSettings() { - return this.ignoreLocalSshSettings; - } - - public boolean isStrictHostKeyChecking() { - return this.strictHostKeyChecking; - } - - public Map getRepos() { + public Map getRepos() { return this.repos; } - public void setUri(String uri) { - this.uri = uri; - } - - public void setHostKeyAlgorithm(String hostKeyAlgorithm) { - this.hostKeyAlgorithm = hostKeyAlgorithm; - } - - public void setHostKey(String hostKey) { - this.hostKey = hostKey; - } - - public void setPrivateKey(String privateKey) { - this.privateKey = privateKey; - } - - public void setIgnoreLocalSshSettings(boolean ignoreLocalSshSettings) { - this.ignoreLocalSshSettings = ignoreLocalSshSettings; - } - - public void setStrictHostKeyChecking(boolean strictHostKeyChecking) { - this.strictHostKeyChecking = strictHostKeyChecking; - } - - public void setRepos(Map repos) { + public void setRepos(Map repos) { this.repos = repos; } - public void addRepo(String repoName, SshUriProperties properties) { + public void addRepo(String repoName, SshUriProperties.SshUriNestedRepoProperties properties) { this.repos.put(repoName, properties); } + @Override public String toString() { - return "org.springframework.cloud.config.server.ssh.SshUriProperties(uri=" + this.getUri() + " hostKeyAlgorithm=" + this.getHostKeyAlgorithm() + ", hostKey=" + this.getHostKey() + ", privateKey=" + this.getPrivateKey() + ", ignoreLocalSshSettings=" + this.isIgnoreLocalSshSettings() + ", strictHostKeyChecking=" + this.isStrictHostKeyChecking() + ", repos=" + this.getRepos() + ")"; + return super.toString() + "{repos=" + repos + "}"; } - public static class SshUriPropertiesBuilder { - private String uri; - private String hostKeyAlgorithm; - private String hostKey; - private String privateKey; - private boolean ignoreLocalSshSettings; - private boolean strictHostKeyChecking = true; - private Map repos; + /** + * Differentiate between sets of properties that are defined in nested Git repos. + * This is to prevent boot from guarding against a potential infinite deserialization of nested properties. + * This sub class differentiates from {@link SshUriProperties} as it does not contain the self mao + */ + public static class SshUriNestedRepoProperties extends SshUri { - SshUriPropertiesBuilder() { - } - - public SshUriProperties.SshUriPropertiesBuilder uri(String uri) { - this.uri = uri; - return this; - } - - public SshUriProperties.SshUriPropertiesBuilder hostKeyAlgorithm(String hostKeyAlgorithm) { - this.hostKeyAlgorithm = hostKeyAlgorithm; - return this; - } - - public SshUriProperties.SshUriPropertiesBuilder hostKey(String hostKey) { - this.hostKey = hostKey; - return this; - } - - public SshUriProperties.SshUriPropertiesBuilder privateKey(String privateKey) { - this.privateKey = privateKey; - return this; - } - - public SshUriProperties.SshUriPropertiesBuilder ignoreLocalSshSettings(boolean ignoreLocalSshSettings) { - this.ignoreLocalSshSettings = ignoreLocalSshSettings; - return this; - } - - public SshUriProperties.SshUriPropertiesBuilder strictHostKeyChecking(boolean strictHostKeyChecking) { - this.strictHostKeyChecking = strictHostKeyChecking; - return this; - } - - public SshUriProperties.SshUriPropertiesBuilder repos(Map repos) { - this.repos = repos; - return this; - } - - public SshUriProperties build() { - return new SshUriProperties(uri, hostKeyAlgorithm, hostKey, privateKey, ignoreLocalSshSettings, strictHostKeyChecking, repos); - } - - public String toString() { - return "org.springframework.cloud.config.server.ssh.SshUriProperties.SshUriPropertiesBuilder(uri=" + this.uri + "hostKeyAlgorithm=" + this.hostKeyAlgorithm + ", hostKey=" + this.hostKey + ", privateKey=" + this.privateKey + ", ignoreLocalSshSettings=" + this.ignoreLocalSshSettings + ", strictHostKeyChecking=" + this.strictHostKeyChecking + ", repos=" + this.repos + ")"; - } } } diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/SshUriPropertyProcessor.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/SshUriPropertyProcessor.java index ca004475..36cbf85a 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/SshUriPropertyProcessor.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ssh/SshUriPropertyProcessor.java @@ -21,6 +21,7 @@ import java.util.HashMap; import java.util.Map; import org.eclipse.jgit.transport.URIish; +import org.springframework.cloud.config.server.ssh.SshUriProperties.SshUriNestedRepoProperties; import static org.springframework.cloud.config.server.ssh.SshPropertyValidator.isSshUri; @@ -37,19 +38,19 @@ public class SshUriPropertyProcessor { this.sshUriProperties = sshUriProperties; } - public Map getSshKeysByHostname() { + public Map getSshKeysByHostname() { return extractNestedProperties(sshUriProperties); } - private Map extractNestedProperties(SshUriProperties uriProperties) { - Map sshUriPropertyMap = new HashMap<>(); + private Map extractNestedProperties(SshUriProperties uriProperties) { + Map sshUriPropertyMap = new HashMap<>(); String parentUri = uriProperties.getUri(); if (isSshUri(parentUri) && getHostname(parentUri) != null) { sshUriPropertyMap.put(getHostname(parentUri), uriProperties); } - Map repos = uriProperties.getRepos(); + Map repos = uriProperties.getRepos(); if(repos != null) { - for (SshUriProperties repoProperties : repos.values()) { + for (SshUriNestedRepoProperties repoProperties : repos.values()) { String repoUri = repoProperties.getUri(); if (isSshUri(repoUri) && getHostname(repoUri) != null) { sshUriPropertyMap.put(getHostname(repoUri), repoProperties); diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/TransportConfigurationTest.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/TransportConfigurationTest.java index 649eab13..40296eef 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/TransportConfigurationTest.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/TransportConfigurationTest.java @@ -18,6 +18,7 @@ package org.springframework.cloud.config.server.config; import org.eclipse.jgit.api.TransportConfigCallback; import org.junit.Test; +import org.springframework.cloud.config.server.ssh.SshUri; import org.springframework.cloud.config.server.ssh.SshUriProperties; import static org.hamcrest.MatcherAssert.assertThat; @@ -29,7 +30,7 @@ import static org.hamcrest.Matchers.instanceOf; public class TransportConfigurationTest { @Test public void propertiesBasedSshTransportCallbackCreated() throws Exception { - SshUriProperties ignoreLocalSettings = SshUriProperties.builder() + SshUriProperties ignoreLocalSettings = SshUri.builder() .uri("user@gitrepo.com:proj/repo") .ignoreLocalSshSettings(true) .build(); @@ -40,7 +41,7 @@ public class TransportConfigurationTest { @Test public void fileBasedSshTransportCallbackCreated() throws Exception { - SshUriProperties dontIgnoreLocalSettings = SshUriProperties.builder() + SshUriProperties dontIgnoreLocalSettings = SshUri.builder() .uri("user@gitrepo.com:proj/repo") .ignoreLocalSshSettings(false) .build(); diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ssh/PropertyBasedSshSessionFactoryTest.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ssh/PropertyBasedSshSessionFactoryTest.java index 2836886c..cf41ce6d 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ssh/PropertyBasedSshSessionFactoryTest.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ssh/PropertyBasedSshSessionFactoryTest.java @@ -61,7 +61,7 @@ public class PropertyBasedSshSessionFactoryTest { @Test public void strictHostKeyCheckingIsOptional() { - SshUriProperties sshKey = new SshUriProperties.SshUriPropertiesBuilder() + SshUri sshKey = new SshUriProperties.SshUriPropertiesBuilder() .uri("ssh://gitlab.example.local:3322/somerepo.git") .privateKey(PRIVATE_KEY) .build(); @@ -75,7 +75,7 @@ public class PropertyBasedSshSessionFactoryTest { @Test public void strictHostKeyCheckingIsUsed() { - SshUriProperties sshKey = new SshUriProperties.SshUriPropertiesBuilder() + SshUri sshKey = new SshUriProperties.SshUriPropertiesBuilder() .uri("ssh://gitlab.example.local:3322/somerepo.git") .hostKey(HOST_KEY) .privateKey(PRIVATE_KEY) @@ -90,7 +90,7 @@ public class PropertyBasedSshSessionFactoryTest { @Test public void hostKeyAlgorithmIsSpecified() { - SshUriProperties sshKey = new SshUriProperties.SshUriPropertiesBuilder() + SshUri sshKey = new SshUriProperties.SshUriPropertiesBuilder() .uri("ssh://gitlab.example.local:3322/somerepo.git") .hostKeyAlgorithm(HOST_KEY_ALGORITHM) .hostKey(HOST_KEY) @@ -106,7 +106,7 @@ public class PropertyBasedSshSessionFactoryTest { @Test public void privateKeyIsUsed() throws Exception { - SshUriProperties sshKey = new SshUriProperties.SshUriPropertiesBuilder() + SshUri sshKey = new SshUriProperties.SshUriPropertiesBuilder() .uri("git@gitlab.example.local:someorg/somerepo.git") .privateKey(PRIVATE_KEY) .build(); @@ -118,7 +118,7 @@ public class PropertyBasedSshSessionFactoryTest { @Test public void hostKeyIsUsed() throws Exception { - SshUriProperties sshKey = new SshUriProperties.SshUriPropertiesBuilder() + SshUri sshKey = new SshUriProperties.SshUriPropertiesBuilder() .uri("git@gitlab.example.local:someorg/somerepo.git") .hostKey(HOST_KEY) .privateKey(PRIVATE_KEY) @@ -133,8 +133,8 @@ public class PropertyBasedSshSessionFactoryTest { Assert.assertEquals(HOST_KEY, hostKey.getKey()); } - private void setupSessionFactory(SshUriProperties sshKey) { - Map sshKeysByHostname = new HashMap<>(); + private void setupSessionFactory(SshUri sshKey) { + Map sshKeysByHostname = new HashMap<>(); sshKeysByHostname.put(SshUriPropertyProcessor.getHostname(sshKey.getUri()), sshKey); factory = new PropertyBasedSshSessionFactory(sshKeysByHostname, jSch) ; when(hc.getHostName()).thenReturn(SshUriPropertyProcessor.getHostname(sshKey.getUri())); diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ssh/SshPropertyValidatorTest.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ssh/SshPropertyValidatorTest.java index 7ce36ba6..154c139a 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ssh/SshPropertyValidatorTest.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ssh/SshPropertyValidatorTest.java @@ -76,7 +76,7 @@ public class SshPropertyValidatorTest { @Test public void supportedParametersSuccesful() throws Exception { - SshUriProperties validSettings = SshUriProperties.builder() + SshUriProperties validSettings = SshUri.builder() .uri(SSH_URI) .ignoreLocalSshSettings(true) .privateKey(VALID_PRIVATE_KEY) @@ -92,7 +92,7 @@ public class SshPropertyValidatorTest { @Test public void invalidPrivateKeyFails() throws Exception { - SshUriProperties invalidKey = SshUriProperties.builder() + SshUriProperties invalidKey = SshUri.builder() .uri(SSH_URI) .ignoreLocalSshSettings(true) .privateKey("invalid_key") @@ -106,7 +106,7 @@ public class SshPropertyValidatorTest { @Test public void missingPrivateKeyFails() throws Exception { - SshUriProperties missingKey = SshUriProperties.builder() + SshUriProperties missingKey = SshUri.builder() .uri(SSH_URI) .ignoreLocalSshSettings(true) .build(); @@ -118,7 +118,7 @@ public class SshPropertyValidatorTest { @Test public void hostKeyWithMissingAlgoFails() throws Exception { - SshUriProperties missingAlgo = SshUriProperties.builder() + SshUriProperties missingAlgo = SshUri.builder() .uri(SSH_URI) .ignoreLocalSshSettings(true) .privateKey(VALID_PRIVATE_KEY) @@ -132,7 +132,7 @@ public class SshPropertyValidatorTest { @Test public void algoWithMissingHostKeyFails() throws Exception { - SshUriProperties missingHostKey = SshUriProperties.builder() + SshUriProperties missingHostKey = SshUri.builder() .uri(SSH_URI) .ignoreLocalSshSettings(true) .privateKey(VALID_PRIVATE_KEY) @@ -146,7 +146,7 @@ public class SshPropertyValidatorTest { @Test public void unsupportedAlgoFails() throws Exception { - SshUriProperties unsupportedAlgo = SshUriProperties.builder() + SshUriProperties unsupportedAlgo = SshUri.builder() .uri(SSH_URI) .ignoreLocalSshSettings(true) .privateKey(VALID_PRIVATE_KEY) @@ -161,7 +161,7 @@ public class SshPropertyValidatorTest { @Test public void validatorNotRunIfIgnoreLocalSettingsFalse() throws Exception { - SshUriProperties useLocal = (SshUriProperties.builder() + SshUriProperties useLocal = (SshUri.builder() .uri(SSH_URI) .ignoreLocalSshSettings(false) .privateKey("invalid_key") @@ -175,7 +175,7 @@ public class SshPropertyValidatorTest { @Test public void validatorNotRunIfHttpsUri() throws Exception { - SshUriProperties httpsUri = (SshUriProperties.builder() + SshUriProperties httpsUri = (SshUri.builder() .uri("https://somerepo.com/team/project.git") .ignoreLocalSshSettings(true) .privateKey("invalid_key") diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ssh/SshUriPropertyProcessorTest.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ssh/SshUriPropertyProcessorTest.java index 3051abf2..8f7fd49a 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ssh/SshUriPropertyProcessorTest.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ssh/SshUriPropertyProcessorTest.java @@ -16,12 +16,13 @@ package org.springframework.cloud.config.server.ssh; + +import java.util.Map; import org.eclipse.jgit.transport.SshSessionFactory; import org.junit.After; import org.junit.Test; +import org.springframework.cloud.config.server.ssh.SshUriProperties.SshUriNestedRepoProperties; -import java.util.HashMap; -import java.util.Map; import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertThat; @@ -53,43 +54,43 @@ public class SshUriPropertyProcessorTest { @Test public void testSingleSshUriProperties() { SshUriPropertyProcessor sshUriPropertyProcessor = new SshUriPropertyProcessor(mainRepoPropertiesFixture()); - Map sshKeysByHostname = sshUriPropertyProcessor.getSshKeysByHostname(); + Map sshKeysByHostname = sshUriPropertyProcessor.getSshKeysByHostname(); assertThat(sshKeysByHostname.values(), hasSize(1)); - SshUriProperties sshKey = sshKeysByHostname.get(HOST1); + SshUri sshKey = sshKeysByHostname.get(HOST1); assertMainRepo(sshKey); } @Test public void testMultipleSshUriPropertiess() { SshUriProperties sshUriProperties = mainRepoPropertiesFixture(); - addRepoProperties(sshUriProperties, SshUriProperties.builder() + addRepoProperties(sshUriProperties, SshUri.builder() .uri(URI2) .privateKey(PRIVATE_KEY2) - .build(), "repo2"); - addRepoProperties(sshUriProperties, SshUriProperties.builder() + .buildAsNestedRepo(), "repo2"); + addRepoProperties(sshUriProperties, SshUri.builder() .uri(URI3) .privateKey(PRIVATE_KEY3) - .build(), "repo3"); + .buildAsNestedRepo(), "repo3"); SshUriPropertyProcessor sshUriPropertyProcessor = new SshUriPropertyProcessor(sshUriProperties); - Map sshKeysByHostname = sshUriPropertyProcessor.getSshKeysByHostname(); + Map sshKeysByHostname = sshUriPropertyProcessor.getSshKeysByHostname(); assertThat(sshKeysByHostname.values(), hasSize(3)); - SshUriProperties sshKey1 = sshKeysByHostname.get(HOST1); + SshUri sshKey1 = sshKeysByHostname.get(HOST1); assertMainRepo(sshKey1); - SshUriProperties sshKey2 = sshKeysByHostname.get(HOST2); + SshUri sshKey2 = sshKeysByHostname.get(HOST2); assertThat(SshUriPropertyProcessor.getHostname(sshKey2.getUri()), is(equalTo(HOST2))); assertThat(sshKey2.getHostKeyAlgorithm(), is(nullValue())); assertThat(sshKey2.getHostKey(), is(nullValue())); assertThat(sshKey2.getPrivateKey(), is(equalTo(PRIVATE_KEY2))); - SshUriProperties sshKey3 = sshKeysByHostname.get(HOST3); + SshUri sshKey3 = sshKeysByHostname.get(HOST3); assertThat(SshUriPropertyProcessor.getHostname(sshKey3.getUri()), is(equalTo(HOST3))); assertThat(sshKey3.getHostKeyAlgorithm(), is(nullValue())); @@ -100,45 +101,45 @@ public class SshUriPropertyProcessorTest { @Test public void testSameHostnameDifferentKeysFirstOneWins() { SshUriProperties sshUriProperties = mainRepoPropertiesFixture(); - addRepoProperties(sshUriProperties, SshUriProperties.builder().uri(URI1) + addRepoProperties(sshUriProperties, SshUri.builder().uri(URI1) .privateKey(PRIVATE_KEY1) .hostKey(HOST_KEY1) .hostKeyAlgorithm(ALGO1) - .build(), "repo2"); + .buildAsNestedRepo(), "repo2"); SshUriPropertyProcessor sshUriPropertyProcessor = new SshUriPropertyProcessor(sshUriProperties); - Map sshKeysByHostname = sshUriPropertyProcessor.getSshKeysByHostname(); + Map sshKeysByHostname = sshUriPropertyProcessor.getSshKeysByHostname(); assertThat(sshKeysByHostname.values(), hasSize(1)); - SshUriProperties sshKey = sshKeysByHostname.get(HOST1); + SshUri sshKey = sshKeysByHostname.get(HOST1); assertMainRepo(sshKey); } @Test public void testNoSshUriProperties() { SshUriPropertyProcessor sshUriPropertyProcessor = new SshUriPropertyProcessor(new SshUriProperties()); - Map sshKeysByHostname = sshUriPropertyProcessor.getSshKeysByHostname(); + Map sshKeysByHostname = sshUriPropertyProcessor.getSshKeysByHostname(); assertThat(sshKeysByHostname.values(), hasSize(0)); } @Test public void testInvalidUriDoesNotAddEntry() { - SshUriPropertyProcessor sshUriPropertyProcessor = new SshUriPropertyProcessor(SshUriProperties.builder().uri("invalid_uri").build()); - Map sshKeysByHostname = sshUriPropertyProcessor.getSshKeysByHostname(); + SshUriPropertyProcessor sshUriPropertyProcessor = new SshUriPropertyProcessor(SshUri.builder().uri("invalid_uri").build()); + Map sshKeysByHostname = sshUriPropertyProcessor.getSshKeysByHostname(); assertThat(sshKeysByHostname.values(), hasSize(0)); } @Test public void testHttpsUriDoesNotAddEntry() { - SshUriPropertyProcessor sshUriPropertyProcessor = new SshUriPropertyProcessor(SshUriProperties.builder().uri("https://user@github.com/proj/repo.git").build()); - Map sshKeysByHostname = sshUriPropertyProcessor.getSshKeysByHostname(); + SshUriPropertyProcessor sshUriPropertyProcessor = new SshUriPropertyProcessor(SshUri.builder().uri("https://user@github.com/proj/repo.git").build()); + Map sshKeysByHostname = sshUriPropertyProcessor.getSshKeysByHostname(); assertThat(sshKeysByHostname.values(), hasSize(0)); } private SshUriProperties mainRepoPropertiesFixture() { - return SshUriProperties.builder() + return SshUri.builder() .uri(URI1) .hostKeyAlgorithm(ALGO1) .hostKey(HOST_KEY1) @@ -146,18 +147,11 @@ public class SshUriPropertyProcessorTest { .build(); } - private void addRepoProperties(SshUriProperties mainRepoProperties, SshUriProperties repoProperties, String repoName) { - if (mainRepoProperties.getRepos() == null) { - Map repos = new HashMap<>(); - repos.put(repoName, repoProperties); - mainRepoProperties.setRepos(repos); - } - else { - mainRepoProperties.addRepo(repoName, repoProperties); - } + private void addRepoProperties(SshUriProperties mainRepoProperties, SshUriNestedRepoProperties repoProperties, String repoName) { + mainRepoProperties.addRepo(repoName, repoProperties); } - private void assertMainRepo(SshUriProperties sshKey) { + private void assertMainRepo(SshUri sshKey) { assertThat(sshKey, is(notNullValue())); assertThat(SshUriPropertyProcessor.getHostname(sshKey.getUri()), is(equalTo(HOST1))); assertThat(sshKey.getHostKeyAlgorithm(), is(equalTo(ALGO1)));