Skip to content

Commit

Permalink
Apply api changes for the recent docker-engine update
Browse files Browse the repository at this point in the history
  • Loading branch information
gesellix committed Nov 7, 2022
1 parent 1a8a1d2 commit 8f300ba
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class DockerClientImpl implements DockerClient {
this.repositoryTagParser = new RepositoryTagParser()

this.manageSystem = new ManageSystemClient(engineApiClient)
this.manageAuthentication = new ManageAuthenticationClient(engineApiClient, authConfigReader)
this.manageAuthentication = new ManageAuthenticationClient(engineApiClient, authConfigReader, env.dockerConfigReader)
this.manageImage = new ManageImageClient(engineApiClient, manageAuthentication)
this.manageDistribution = new ManageDistributionService(engineApiClient)
this.manageContainer = new ManageContainerClient(engineApiClient, httpClient)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import de.gesellix.docker.authentication.AuthConfig
import de.gesellix.docker.authentication.AuthConfigReader
import de.gesellix.docker.authentication.CredsStore
import de.gesellix.docker.client.EngineResponseContent
import de.gesellix.docker.engine.DockerConfigReader
import de.gesellix.docker.remote.api.EngineApiClient
import de.gesellix.docker.remote.api.SystemAuthResponse
import org.slf4j.Logger
Expand All @@ -14,22 +15,24 @@ class ManageAuthenticationClient implements ManageAuthentication {

private final Logger log = LoggerFactory.getLogger(ManageAuthenticationClient)

DockerConfigReader dockerConfigReader
AuthConfigReader authConfigReader
EngineApiClient client

private RegistryElection registryElection

private Moshi moshi = new Moshi.Builder().build()

ManageAuthenticationClient(EngineApiClient client, AuthConfigReader authConfigReader) {
ManageAuthenticationClient(EngineApiClient client, AuthConfigReader authConfigReader, DockerConfigReader dockerConfigReader) {
this.client = client
this.authConfigReader = authConfigReader
this.dockerConfigReader = dockerConfigReader
this.registryElection = new RegistryElection(client.getSystemApi(), authConfigReader)
}

@Override
Map<String, AuthConfig> getAllAuthConfigs(File dockerCfg = null) {
Map<String, Object> parsedDockerCfg = authConfigReader.readDockerConfigFile(dockerCfg)
Map<String, Object> parsedDockerCfg = dockerConfigReader.readDockerConfigFile(dockerCfg)
if (!parsedDockerCfg) {
return new HashMap<String, AuthConfig>()
}
Expand Down Expand Up @@ -87,8 +90,7 @@ class ManageAuthenticationClient implements ManageAuthentication {
String remoteName
if (remainder.contains(':')) {
remoteName = remainder.substring(0, remainder.indexOf(':'))
}
else {
} else {
remoteName = remainder
}
if (remoteName.toLowerCase() != remoteName) {
Expand Down Expand Up @@ -124,8 +126,7 @@ class ManageAuthenticationClient implements ManageAuthentication {
Map<String, Object> getNamed(Map<String, Object> ref) {
if (ref.domain) {
return ref
}
else if (ref.repo && ref.repo.domain) {
} else if (ref.repo && ref.repo.domain) {
return ref.repo
}
throw new IllegalStateException("reference ${ref} has no name")
Expand All @@ -148,8 +149,7 @@ class ManageAuthenticationClient implements ManageAuthentication {
int i = name.indexOf('/')
if (i == -1 || (!containsAny(name.substring(0, i), ".:") && name.substring(0, i) != 'localhost')) {
(domain, remainder) = [defaultDomain, name]
}
else {
} else {
(domain, remainder) = [name.substring(0, i), name.substring(i + 1)]
}
if (domain == legacyDefaultDomain) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.squareup.moshi.Types
import de.gesellix.docker.authentication.AuthConfig
import de.gesellix.docker.authentication.AuthConfigReader
import de.gesellix.docker.client.DockerClient
import de.gesellix.docker.engine.DockerConfigReader
import de.gesellix.docker.engine.DockerEnv
import de.gesellix.docker.remote.api.EngineApiClient
import de.gesellix.docker.remote.api.SystemAuthResponse
Expand All @@ -27,25 +28,40 @@ class ManageAuthenticationClientTest extends Specification {
Moshi moshi = new Moshi.Builder().build()

def setup() {
def dockerConfigReader = Mock(DockerConfigReader)
env = Mock(DockerEnv)
service = new ManageAuthenticationClient(Mock(EngineApiClient), new AuthConfigReader(env))
env.getDockerConfigReader() >> dockerConfigReader
service = new ManageAuthenticationClient(Mock(EngineApiClient), new AuthConfigReader(env), dockerConfigReader)
service.authConfigReader = Spy(AuthConfigReader, constructorArgs: [env])
}

def "read authConfig (new format)"() {
given:
env.indexUrl_v1 >> 'https://index.docker.io/v1/'
String oldDockerConfig = System.clearProperty("docker.config")
File expectedConfigFile = new ResourceReader().getClasspathResourceAsFile('/auth/config.json', DockerClient)
env.indexUrl_v1 >> 'https://index.docker.io/v1/'
env.getDockerConfigReader().getDockerConfigFile() >> expectedConfigFile
env.getDockerConfigReader().readDockerConfigFile(expectedConfigFile) >> [
auths: [
"https://index.docker.io/v1/": [
auth : "Z2VzZWxsaXg6LXlldC1hbm90aGVyLXBhc3N3b3JkLQ",
email: "[email protected]"
],
"quay.io" : [
auth : "Z2VzZWxsaXg6LWEtcGFzc3dvcmQtZm9yLXF1YXkt",
email: "[email protected]"
]
]
]

when:
def result = service.readAuthConfig(null, expectedConfigFile)

then:
result == new AuthConfig(username: "gesellix",
password: "-yet-another-password-",
email: "tobias@gesellix.de",
serveraddress: "https://index.docker.io/v1/")
password: "-yet-another-password-",
email: "[email protected]",
serveraddress: "https://index.docker.io/v1/")

cleanup:
if (oldDockerConfig) {
Expand All @@ -55,18 +71,29 @@ class ManageAuthenticationClientTest extends Specification {

def "read authConfig (legacy format)"() {
given:
env.indexUrl_v1 >> 'https://index.docker.io/v1/'
String oldDockerConfig = System.clearProperty("docker.config")
File expectedConfigFile = new ResourceReader().getClasspathResourceAsFile('/auth/dockercfg', DockerClient)
env.indexUrl_v1 >> 'https://index.docker.io/v1/'
env.getDockerConfigReader().getDockerConfigFile() >> expectedConfigFile
env.getDockerConfigReader().readDockerConfigFile(expectedConfigFile) >> [
"https://index.docker.io/v1/": [
auth : "Z2VzZWxsaXg6LXlldC1hbm90aGVyLXBhc3N3b3JkLQ==",
email: "[email protected]"
],
"quay.io" : [
auth : "Z2VzZWxsaXg6LWEtcGFzc3dvcmQtZm9yLXF1YXkt",
email: "[email protected]"
]
]

when:
def result = service.readAuthConfig(null, expectedConfigFile)

then:
result == new AuthConfig(username: "gesellix",
password: "-yet-another-password-",
email: "tobias@gesellix.de",
serveraddress: "https://index.docker.io/v1/")
password: "-yet-another-password-",
email: "[email protected]",
serveraddress: "https://index.docker.io/v1/")

cleanup:
if (oldDockerConfig) {
Expand Down Expand Up @@ -164,6 +191,19 @@ class ManageAuthenticationClientTest extends Specification {
given:
env.indexUrl_v1 >> 'https://index.docker.io/v1/'
File dockerCfg = new ResourceReader().getClasspathResourceAsFile('/auth/config.json', DockerClient)
env.getDockerConfigReader().getDockerConfigFile() >> dockerCfg
env.getDockerConfigReader().readDockerConfigFile(dockerCfg) >> [
auths: [
"https://index.docker.io/v1/": [
auth : "Z2VzZWxsaXg6LXlldC1hbm90aGVyLXBhc3N3b3JkLQ",
email: "[email protected]"
],
"quay.io" : [
auth : "Z2VzZWxsaXg6LWEtcGFzc3dvcmQtZm9yLXF1YXkt",
email: "[email protected]"
]
]
]

when:
def authDetails = service.readAuthConfig(null, dockerCfg)
Expand All @@ -181,6 +221,19 @@ class ManageAuthenticationClientTest extends Specification {
def "read auth config for quay.io"() {
given:
File dockerCfg = new ResourceReader().getClasspathResourceAsFile('/auth/config.json', DockerClient)
env.getDockerConfigReader().getDockerConfigFile() >> dockerCfg
env.getDockerConfigReader().readDockerConfigFile(dockerCfg) >> [
auths: [
"https://index.docker.io/v1/": [
auth : "Z2VzZWxsaXg6LXlldC1hbm90aGVyLXBhc3N3b3JkLQ",
email: "[email protected]"
],
"quay.io" : [
auth : "Z2VzZWxsaXg6LWEtcGFzc3dvcmQtZm9yLXF1YXkt",
email: "[email protected]"
]
]
]

when:
def authDetails = service.readAuthConfig("quay.io", dockerCfg)
Expand Down Expand Up @@ -248,16 +301,29 @@ class ManageAuthenticationClientTest extends Specification {
File expectedConfigFile = new ResourceReader().getClasspathResourceAsFile('/auth/config.json', DockerClient)
env.indexUrl_v1 >> 'https://index.docker.io/v1/'
env.getDockerConfigFile() >> expectedConfigFile
env.getDockerConfigReader().getDockerConfigFile() >> expectedConfigFile
env.getDockerConfigReader().readDockerConfigFile(expectedConfigFile) >> [
auths: [
"https://index.docker.io/v1/": [
auth : "Z2VzZWxsaXg6LXlldC1hbm90aGVyLXBhc3N3b3JkLQ",
email: "[email protected]"
],
"quay.io" : [
auth : "Z2VzZWxsaXg6LWEtcGFzc3dvcmQtZm9yLXF1YXkt",
email: "[email protected]"
]
]
]

when:
def result = service.readDefaultAuthConfig()

then:
1 * service.authConfigReader.readAuthConfig(null, expectedConfigFile)
result == new AuthConfig(username: "gesellix",
password: "-yet-another-password-",
email: "[email protected]",
serveraddress: "https://index.docker.io/v1/")
password: "-yet-another-password-",
email: "[email protected]",
serveraddress: "https://index.docker.io/v1/")

cleanup:
if (oldDockerConfig) {
Expand Down

0 comments on commit 8f300ba

Please sign in to comment.