Skip to content

Commit

Permalink
Support more customization for ssh session, also disabling strict hos…
Browse files Browse the repository at this point in the history
…t key check #95
  • Loading branch information
pwielgolaski committed Apr 23, 2016
1 parent 04b288f commit 19b458b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/main/groovy/org/ajoberstar/grgit/auth/AuthConfig.groovy
Expand Up @@ -57,6 +57,14 @@ import org.ajoberstar.grgit.exception.GrgitException
* <ul>
* <li>{@code org.ajoberstar.grgit.auth.ssh.private=<path.to.private.key>}</li>
* </ul>
* <p>
* In order to add a non-standard session config,
* use the following property.
* </p>
*
* <ul>
* <li>{@code org.ajoberstar.grgit.auth.session.config.<key>=<value>}</li>
* </ul>
*
* <p>
* The following order is used to determine which authentication option
Expand All @@ -80,6 +88,7 @@ class AuthConfig {
static final String USERNAME_OPTION = 'org.ajoberstar.grgit.auth.username'
static final String PASSWORD_OPTION = 'org.ajoberstar.grgit.auth.password'
static final String SSH_PRIVATE_KEY_OPTION = 'org.ajoberstar.grgit.auth.ssh.private'
static final String SSH_SESSION_CONFIG_OPTION_PREFIX = 'org.ajoberstar.grgit.auth.session.config.'

/**
* Set of all authentication options that are allowed in this
Expand Down Expand Up @@ -127,6 +136,16 @@ class AuthConfig {
return System.properties[SSH_PRIVATE_KEY_OPTION]
}

/**
* Gets session config override for SSH session that is used underneath by JGit
* @return map with configuration or empty if nothing was specified in system property
*/
Map<String, String> getSessionConfig() {
return System.properties
.findAll { it -> it.key.startsWith(SSH_SESSION_CONFIG_OPTION_PREFIX) }
.collectEntries { it -> [ it.key.substring(SSH_SESSION_CONFIG_OPTION_PREFIX.length()), it.value] }
}

/**
* Factory method to construct an authentication configuration from the
* given properties.
Expand Down
Expand Up @@ -48,10 +48,10 @@ class JschAgentProxySessionFactory extends JschConfigSessionFactory {
}

/**
* No actions performed by this.
* Customize session
*/
protected void configure(Host hc, Session session) {
// no action
config.sessionConfig.each { key, value -> session.setConfig(key, value) }
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/test/groovy/org/ajoberstar/grgit/auth/AuthConfigSpec.groovy
Expand Up @@ -69,4 +69,16 @@ class AuthConfigSpec extends Specification {
expect:
AuthConfig.fromMap([:]).getHardcodedCreds() == null
}

def 'getSessionConfig returns empty map if nothing specified'() {
expect:
AuthConfig.fromMap(Collections.emptyMap()).sessionConfig.isEmpty()
}

def 'getSessionConfig returns session config based on system property'() {
given:
System.setProperty(AuthConfig.SSH_SESSION_CONFIG_OPTION_PREFIX + 'StrictHostKeyChecking', 'no')
expect:
AuthConfig.fromMap(Collections.emptyMap()).sessionConfig == [ StrictHostKeyChecking: 'no' ]
}
}

0 comments on commit 19b458b

Please sign in to comment.