Skip to content
This repository has been archived by the owner on Mar 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #765 from spotify/mattbrown/fix-registryauth-forauth
Browse files Browse the repository at this point in the history
RegistryAuth: handle case where password contains colon
  • Loading branch information
mattnworb authored May 25, 2017
2 parents 682affa + 8b14122 commit f00168e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,13 @@ public static RegistryAuth create(@JsonProperty("username") String username,

/** Construct a Builder based upon the "auth" field of the docker client config file. */
public static Builder forAuth(String auth) {
final String[] authParams = Base64.decodeAsString(auth).split(":");
// split with limit=2 to catch case where password contains a colon
final String[] authParams = Base64.decodeAsString(auth).split(":", 2);

if (authParams.length != 2) {
return builder();
}

return builder()
.username(authParams[0].trim())
.password(authParams[1].trim());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*-
* -\-\-
* docker-client
* --
* Copyright (C) 2016 - 2017 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/

package com.spotify.docker.client.messages;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import com.google.common.io.BaseEncoding;
import org.junit.Test;

public class RegistryAuthTest {

@Test
public void testForAuth() {
final String username = "johndoe";
final String password = "pass123";
final String encoded = BaseEncoding.base64().encode((username + ":" + password).getBytes());

final RegistryAuth registryAuth = RegistryAuth.forAuth(encoded).build();
assertThat(registryAuth.username(), is(username));
assertThat(registryAuth.password(), is(password));
}

@Test
public void testForAuth_PasswordContainsColon() {
final String username = "johndoe";
final String password = "foo:bar";
final String encoded = BaseEncoding.base64().encode((username + ":" + password).getBytes());

final RegistryAuth registryAuth = RegistryAuth.forAuth(encoded).build();
assertThat(registryAuth.username(), is(username));
assertThat(registryAuth.password(), is(password));
}
}
3 changes: 1 addition & 2 deletions src/test/resources/dockerConfig/identityTokenConfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"auths": {
"docker.customdomain.com": {
"auth": "ZG9ja2VybWFuOg==",
"email": "[email protected]",
"identityToken": "52ce5fd5-eb60-42bf-931f-5eeec128211a"
}
}
}
}

0 comments on commit f00168e

Please sign in to comment.