Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix username-only creds #158

Merged
merged 1 commit into from
Apr 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/main/groovy/org/ajoberstar/grgit/Credentials.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,26 @@
*/
package org.ajoberstar.grgit

import groovy.transform.Immutable
import groovy.transform.Canonical

/**
* Credentials to use for remote operations.
* @since 0.2.0
*/
@Immutable
@Canonical
class Credentials {
String username
String password
final String username
final String password

String getUsername() {
return username ?: ''
}

String getPassword() {
return password ?: ''
}

boolean isPopulated() {
return username != null
}
}
6 changes: 1 addition & 5 deletions src/main/groovy/org/ajoberstar/grgit/auth/AuthConfig.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,7 @@ class AuthConfig {
if (allows(Option.HARDCODED)) {
String username = props[USERNAME_OPTION] ?: env[USERNAME_ENV_VAR]
String password = props[PASSWORD_OPTION] ?: env[PASSWORD_ENV_VAR]
if (username) {
return new Credentials(username, password)
} else {
return null
}
return new Credentials(username, password)
} else {
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ final class TransportOpUtil {

private static CredentialsProvider determineCredentialsProvider(AuthConfig config, Credentials creds) {
Credentials systemCreds = config.hardcodedCreds
if (config.allows(AuthConfig.Option.HARDCODED) && creds?.username) {
if (config.allows(AuthConfig.Option.HARDCODED) && creds?.populated) {
logger.info('using hardcoded credentials provided programmatically')
return new UsernamePasswordCredentialsProvider(creds.username, creds.password)
} else if (config.allows(AuthConfig.Option.HARDCODED) && systemCreds) {
} else if (config.allows(AuthConfig.Option.HARDCODED) && systemCreds?.populated) {
logger.info('using hardcoded credentials from system properties')
return new UsernamePasswordCredentialsProvider(systemCreds.username, systemCreds.password)
} else if (config.allows(AuthConfig.Option.INTERACTIVE) && !GraphicsEnvironment.isHeadless()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ class AuthConfigSpec extends Specification {
AuthConfig.fromMap(props).getHardcodedCreds() == new Credentials('myuser', null)
}

def 'getHardcodedCreds returns null if username is not set'() {
def 'getHardcodedCreds are not populated if username is not set'() {
expect:
AuthConfig.fromMap([:]).getHardcodedCreds() == null
!AuthConfig.fromMap([:]).getHardcodedCreds().isPopulated()
}

def 'getSessionConfig returns empty map if nothing specified'() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class LinuxAuthenticationSpec extends Specification {
private static final String HTTPS_URI = 'https://github.com/ajoberstar/grgit-test.git'

static Credentials hardcodedCreds
static Credentials partialCreds

@Rule TemporaryFolder tempDir = new TemporaryFolder()

Expand All @@ -45,6 +46,7 @@ class LinuxAuthenticationSpec extends Specification {
def username = System.properties['org.ajoberstar.grgit.test.username']
def password = System.properties['org.ajoberstar.grgit.test.password']
hardcodedCreds = new Credentials(username, password)
partialCreds = new Credentials(password, null)
assert hardcodedCreds.username && hardcodedCreds.password
}

Expand All @@ -61,7 +63,7 @@ class LinuxAuthenticationSpec extends Specification {
System.properties[AuthConfig.FORCE_OPTION] = method
assert System.properties[AuthConfig.USERNAME_OPTION] == null, 'Username should not already be set.'
assert System.properties[AuthConfig.PASSWORD_OPTION] == null, 'Password should not already be set.'
ready(ssh, creds)
ready(ssh)
if (!creds) {
System.properties[AuthConfig.USERNAME_OPTION] = hardcodedCreds.username
System.properties[AuthConfig.PASSWORD_OPTION] = hardcodedCreds.password
Expand All @@ -72,18 +74,18 @@ class LinuxAuthenticationSpec extends Specification {
then:
grgit.branch.status(branch: 'master').aheadCount == 0
where:
method | ssh | creds
method | ssh | creds
'hardcoded' | false | hardcodedCreds
'hardcoded' | false | null
'hardcoded' | false | partialCreds
'interactive' | false | null
'interactive' | true | null
'sshagent' | true | null
'sshagent' | true | null
}

private void ready(boolean ssh, Credentials credentials = null) {
private void ready(boolean ssh) {
File repoDir = tempDir.newFolder('repo')
String uri = ssh ? SSH_URI : HTTPS_URI
grgit = Grgit.clone(uri: uri, dir: repoDir, credentials: credentials)
grgit = Grgit.clone(uri: uri, dir: repoDir)
grgit.repository.jgit.repo.config.with {
setString('user', null, 'name', person.name)
setString('user', null, 'email', person.email)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class WindowsAuthenticationSpec extends Specification {
def username = System.properties['org.ajoberstar.grgit.test.username']
def password = System.properties['org.ajoberstar.grgit.test.password']
hardcodedCreds = new Credentials(username, password)
partialCreds = new Credentials(username, null)
partialCreds = new Credentials(password, null)
assert hardcodedCreds.username && hardcodedCreds.password
}

Expand All @@ -63,7 +63,7 @@ class WindowsAuthenticationSpec extends Specification {
assert System.properties[AuthConfig.USERNAME_OPTION] == null, 'Username should not already be set.'
assert System.properties[AuthConfig.PASSWORD_OPTION] == null, 'Password should not already be set.'
System.properties[AuthConfig.FORCE_OPTION] = method
ready(ssh, creds)
ready(ssh)
if (!creds) {
System.properties[AuthConfig.USERNAME_OPTION] = hardcodedCreds.username
System.properties[AuthConfig.PASSWORD_OPTION] = hardcodedCreds.password
Expand All @@ -74,20 +74,20 @@ class WindowsAuthenticationSpec extends Specification {
then:
grgit.branch.status(branch: 'master').aheadCount == 0
where:
method | ssh | creds
method | ssh | creds
'hardcoded' | false | hardcodedCreds
'hardcoded' | false | partialCreds
'hardcoded' | false | null
'interactive' | false | null
'interactive' | true | null
'sshagent' | true | null
'pageant' | true | null
'sshagent' | true | null
'pageant' | true | null
}

private void ready(boolean ssh, Credentials credentials = null) {
private void ready(boolean ssh) {
File repoDir = tempDir.newFolder('repo')
String uri = ssh ? SSH_URI : HTTPS_URI
grgit = Grgit.clone(uri: uri, dir: repoDir, credentials: credentials)
grgit = Grgit.clone(uri: uri, dir: repoDir)
grgit.repository.jgit.repo.config.with {
setString('user', null, 'name', person.name)
setString('user', null, 'email', person.email)
Expand Down