Differentiate between SSH settings found at the top level of a Git configuration object and those found as a map under the Repos property. Do this by introducing common base class SshUri that holds common properties and SshUriNestedRepoProperties, that is used to contain properties in the repos property map. This avoids Boot from guarding against a potential infinite deserialization loop.

This commit is contained in:
Ollie Hughes
2017-07-20 12:02:31 +01:00
parent 67cdfab7fa
commit 537f6ce648
12 changed files with 232 additions and 203 deletions

View File

@@ -52,9 +52,9 @@ public class HostKeyAlgoSupportedValidator implements ConstraintValidator<HostKe
public boolean isValid(SshUriProperties sshUriProperties, ConstraintValidatorContext context) {
context.disableDefaultConstraintViolation();
Set<Boolean> validationResults = new HashSet<>();
List<SshUriProperties> extractedProperties = sshPropertyValidator.extractRepoProperties(sshUriProperties);
List<SshUri> 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<HostKe
return !validationResults.contains(false);
}
private boolean isHostKeySpecifiedWhenAlgorithmSet(SshUriProperties sshUriProperties, ConstraintValidatorContext context) {
private boolean isHostKeySpecifiedWhenAlgorithmSet(SshUri sshUriProperties, ConstraintValidatorContext context) {
if (hasText(sshUriProperties.getHostKeyAlgorithm())
&& !VALID_HOST_KEY_ALGORITHMS.contains(sshUriProperties.getHostKeyAlgorithm())) {

View File

@@ -48,9 +48,9 @@ public class HostKeyAndAlgoBothExistValidator implements ConstraintValidator<Hos
@Override
public boolean isValid(SshUriProperties sshUriProperties, ConstraintValidatorContext context) {
Set<Boolean> validationResults = new HashSet<>();
List<SshUriProperties> extractedProperties = sshPropertyValidator.extractRepoProperties(sshUriProperties);
List<SshUri> 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<Hos
return !validationResults.contains(false);
}
private boolean isHostKeySpecifiedWhenAlgorithmSet(SshUriProperties sshUriProperties, ConstraintValidatorContext context) {
private boolean isHostKeySpecifiedWhenAlgorithmSet(SshUri sshUriProperties, ConstraintValidatorContext context) {
if (hasText(sshUriProperties.getHostKeyAlgorithm()) && !hasText(sshUriProperties.getHostKey())) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(
@@ -71,7 +71,7 @@ public class HostKeyAndAlgoBothExistValidator implements ConstraintValidator<Hos
return true;
}
private boolean isAlgorithmSpecifiedWhenHostKeySet(SshUriProperties sshUriProperties, ConstraintValidatorContext context) {
private boolean isAlgorithmSpecifiedWhenHostKeySet(SshUri sshUriProperties, ConstraintValidatorContext context) {
if (hasText(sshUriProperties.getHostKey()) && !hasText(sshUriProperties.getHostKeyAlgorithm())) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(

View File

@@ -52,9 +52,9 @@ public class PrivateKeyValidator implements ConstraintValidator<PrivateKeyIsVali
public boolean isValid(SshUriProperties sshUriProperties, ConstraintValidatorContext context) {
context.disableDefaultConstraintViolation();
Set<Boolean> validationResults = new HashSet<>();
List<SshUriProperties> extractedProperties = sshPropertyValidator.extractRepoProperties(sshUriProperties);
List<SshUri> 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<PrivateKeyIsVali
}
private boolean isPrivateKeyPresent(SshUriProperties sshUriProperties, ConstraintValidatorContext context) {
private boolean isPrivateKeyPresent(SshUri sshUriProperties, ConstraintValidatorContext context) {
if (!hasText(sshUriProperties.getPrivateKey())) {
context.buildConstraintViolationWithTemplate(
format("Property '%shostKey' must be set when '%shostKeyAlgorithm' is specified", GIT_PROPERTY_PREFIX, GIT_PROPERTY_PREFIX))
@@ -75,7 +75,7 @@ public class PrivateKeyValidator implements ConstraintValidator<PrivateKeyIsVali
return true;
}
private boolean isPrivateKeyFormatCorrect(SshUriProperties sshUriProperties, ConstraintValidatorContext context) {
private boolean isPrivateKeyFormatCorrect(SshUri sshUriProperties, ConstraintValidatorContext context) {
try {
KeyPair.load(new JSch(), sshUriProperties.getPrivateKey().getBytes(), null);
return true;

View File

@@ -39,17 +39,17 @@ public class PropertyBasedSshSessionFactory extends JschConfigSessionFactory {
private static final String YES_OPTION = "yes";
private static final String NO_OPTION = "no";
private static final String SERVER_HOST_KEY = "server_host_key";
private final Map<String, SshUriProperties> sshKeysByHostname;
private final Map<String, SshUri> sshKeysByHostname;
private final JSch jSch;
public PropertyBasedSshSessionFactory(Map<String, SshUriProperties> sshKeysByHostname, JSch jSch) {
public PropertyBasedSshSessionFactory(Map<String, SshUri> 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()));

View File

@@ -55,10 +55,10 @@ public class SshPropertyValidator {
return false;
}
protected List<SshUriProperties> extractRepoProperties(SshUriProperties sshUriProperties) {
List<SshUriProperties> allRepoProperties = new ArrayList<>();
protected List<SshUri> extractRepoProperties(SshUriProperties sshUriProperties) {
List<SshUri> allRepoProperties = new ArrayList<>();
allRepoProperties.add(sshUriProperties);
Map<String, SshUriProperties> repos = sshUriProperties.getRepos();
Map<String, SshUriProperties.SshUriNestedRepoProperties> repos = sshUriProperties.getRepos();
if (repos != null) {
allRepoProperties.addAll(repos.values());
}

View File

@@ -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<String, SshUriNestedRepoProperties> 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<String, SshUriNestedRepoProperties> 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 + ")";
}
}
}

View File

@@ -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<String, SshUriProperties> repos = new HashMap<>();
private Map<String, SshUriProperties.SshUriNestedRepoProperties> repos = new LinkedHashMap<>();
public SshUriProperties(String uri, String hostKeyAlgorithm, String hostKey, String privateKey, boolean ignoreLocalSshSettings, boolean strictHostKeyChecking, Map<String, SshUriProperties> 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<String, SshUriProperties> getRepos() {
public Map<String, SshUriProperties.SshUriNestedRepoProperties> 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<String, SshUriProperties> repos) {
public void setRepos(Map<String, SshUriNestedRepoProperties> 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<String, SshUriProperties> 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<String, SshUriProperties> 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 + ")";
}
}
}

View File

@@ -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<String, SshUriProperties> getSshKeysByHostname() {
public Map<String, SshUri> getSshKeysByHostname() {
return extractNestedProperties(sshUriProperties);
}
private Map<String, SshUriProperties> extractNestedProperties(SshUriProperties uriProperties) {
Map<String, SshUriProperties> sshUriPropertyMap = new HashMap<>();
private Map<String, SshUri> extractNestedProperties(SshUriProperties uriProperties) {
Map<String, SshUri> sshUriPropertyMap = new HashMap<>();
String parentUri = uriProperties.getUri();
if (isSshUri(parentUri) && getHostname(parentUri) != null) {
sshUriPropertyMap.put(getHostname(parentUri), uriProperties);
}
Map<String, SshUriProperties> repos = uriProperties.getRepos();
Map<String, SshUriNestedRepoProperties> 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);

View File

@@ -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();

View File

@@ -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<String, SshUriProperties> sshKeysByHostname = new HashMap<>();
private void setupSessionFactory(SshUri sshKey) {
Map<String, SshUri> sshKeysByHostname = new HashMap<>();
sshKeysByHostname.put(SshUriPropertyProcessor.getHostname(sshKey.getUri()), sshKey);
factory = new PropertyBasedSshSessionFactory(sshKeysByHostname, jSch) ;
when(hc.getHostName()).thenReturn(SshUriPropertyProcessor.getHostname(sshKey.getUri()));

View File

@@ -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")

View File

@@ -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<String, SshUriProperties> sshKeysByHostname = sshUriPropertyProcessor.getSshKeysByHostname();
Map<String, SshUri> 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<String, SshUriProperties> sshKeysByHostname = sshUriPropertyProcessor.getSshKeysByHostname();
Map<String, SshUri> 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<String, SshUriProperties> sshKeysByHostname = sshUriPropertyProcessor.getSshKeysByHostname();
Map<String, SshUri> 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<String, SshUriProperties> sshKeysByHostname = sshUriPropertyProcessor.getSshKeysByHostname();
Map<String, SshUri> sshKeysByHostname = sshUriPropertyProcessor.getSshKeysByHostname();
assertThat(sshKeysByHostname.values(), hasSize(0));
}
@Test
public void testInvalidUriDoesNotAddEntry() {
SshUriPropertyProcessor sshUriPropertyProcessor = new SshUriPropertyProcessor(SshUriProperties.builder().uri("invalid_uri").build());
Map<String, SshUriProperties> sshKeysByHostname = sshUriPropertyProcessor.getSshKeysByHostname();
SshUriPropertyProcessor sshUriPropertyProcessor = new SshUriPropertyProcessor(SshUri.builder().uri("invalid_uri").build());
Map<String, SshUri> 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<String, SshUriProperties> sshKeysByHostname = sshUriPropertyProcessor.getSshKeysByHostname();
SshUriPropertyProcessor sshUriPropertyProcessor = new SshUriPropertyProcessor(SshUri.builder().uri("https://user@github.com/proj/repo.git").build());
Map<String, SshUri> 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<String, SshUriProperties> 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)));