Skip to content

Commit bc5a475

Browse files
authored
Adds authenticators for Docker registry. (GoogleContainerTools#67)
1 parent 9bf7ed0 commit bc5a475

24 files changed

Lines changed: 552 additions & 30 deletions

‎crepecake/build.gradle‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,26 @@ repositories {
1313
mavenCentral()
1414
}
1515

16+
sourceSets {
17+
integrationTest {
18+
java {
19+
compileClasspath += main.output + test.output
20+
runtimeClasspath += main.output + test.output
21+
srcDir file('src/integration-test/java')
22+
}
23+
resources.srcDir file('src/integration-test/resources')
24+
}
25+
}
26+
27+
configurations {
28+
integrationTestCompile.extendsFrom testCompile
29+
integrationTestRuntime.extendsFrom testRuntime
30+
}
31+
1632
dependencies {
1733
compile gradleApi()
1834
compile 'com.google.http-client:google-http-client:1.23.0'
35+
compile 'com.google.http-client:google-http-client-jackson2:1.23.0'
1936
compile 'org.apache.commons:commons-compress:1.15'
2037
compile 'com.google.guava:guava:23.5-jre'
2138
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.2'
@@ -27,13 +44,24 @@ task wrapper(type: Wrapper) {
2744
gradleVersion = "4.3.1"
2845
}
2946

47+
// Integration tests must be run explicitly
48+
task integrationTest(type: Test) {
49+
testClassesDirs = sourceSets.integrationTest.output.classesDirs
50+
classpath = sourceSets.integrationTest.runtimeClasspath
51+
}
52+
integrationTest.dependsOn test
53+
3054
test {
3155
testLogging {
3256
showStandardStreams = true
3357
exceptionFormat = 'full'
3458
}
3559
}
3660

61+
tasks.withType(Test) {
62+
reports.html.setDestination file("${reporting.baseDir}/${name}")
63+
}
64+
3765
/* GOOGLE JAVA FORMAT */
3866
check.dependsOn verifyGoogleJavaFormat
3967
/* GOOGLE JAVA FORMAT */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2017 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.crepecake.registry;
18+
19+
import com.google.cloud.tools.crepecake.http.Authorization;
20+
import java.io.IOException;
21+
import org.junit.Assert;
22+
import org.junit.Test;
23+
24+
/** Integration tests for {@link DockerCredentialRetriever}. */
25+
public class DockerCredentialRetrieverIntegrationTest {
26+
27+
/** Tests retrieval via {@code docker-credential-gcr} CLI. */
28+
@Test
29+
public void testRetrieveGCR()
30+
throws IOException, NonexistentServerUrlDockerCredentialHelperException,
31+
NonexistentDockerCredentialHelperException {
32+
DockerCredentialRetriever dockerCredentialRetriever =
33+
new DockerCredentialRetriever("gcr.io", "gcr");
34+
35+
Authorization authorization = dockerCredentialRetriever.retrieve();
36+
37+
// Checks that some token was received.
38+
Assert.assertTrue(0 < authorization.getToken().length());
39+
}
40+
41+
@Test
42+
public void testRetrieve_nonexistentCredentialHelper()
43+
throws IOException, NonexistentServerUrlDockerCredentialHelperException {
44+
try {
45+
DockerCredentialRetriever fakeDockerCredentialRetriever =
46+
new DockerCredentialRetriever("", "fake-cloud-provider");
47+
48+
fakeDockerCredentialRetriever.retrieve();
49+
50+
Assert.fail("Retrieve should have failed for nonexistent credential helper");
51+
52+
} catch (NonexistentDockerCredentialHelperException ex) {
53+
Assert.assertEquals(
54+
"The system does not have docker-credential-fake-cloud-provider CLI", ex.getMessage());
55+
}
56+
}
57+
58+
@Test
59+
public void testRetrieve_nonexistentServerUrl()
60+
throws IOException, NonexistentDockerCredentialHelperException {
61+
try {
62+
DockerCredentialRetriever fakeDockerCredentialRetriever =
63+
new DockerCredentialRetriever("fake.server.url", "gcr");
64+
65+
fakeDockerCredentialRetriever.retrieve();
66+
67+
Assert.fail("Retrieve should have failed for nonexistent server URL");
68+
69+
} catch (NonexistentServerUrlDockerCredentialHelperException ex) {
70+
Assert.assertEquals(
71+
"The credential helper (docker-credential-gcr) has nothing for server URL: fake.server.url",
72+
ex.getMessage());
73+
}
74+
}
75+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2017 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.crepecake.registry;
18+
19+
import com.google.cloud.tools.crepecake.http.Authorization;
20+
import org.junit.Assert;
21+
import org.junit.Test;
22+
23+
/** Integration tests for {@link RegistryAuthenticator}. */
24+
public class RegistryAuthenticatorIntegrationTest {
25+
26+
@Test
27+
public void testAuthenticate() throws RegistryAuthenticationFailedException {
28+
RegistryAuthenticator registryAuthenticator =
29+
RegistryAuthenticators.forDockerHub("library/busybox");
30+
Authorization authorization = registryAuthenticator.authenticate();
31+
32+
// Checks that some token was received.
33+
Assert.assertTrue(0 < authorization.getToken().length());
34+
}
35+
}

‎crepecake/src/main/java/com/google/cloud/tools/crepecake/blob/Blobs.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,6 @@ public static Blob from(String content, boolean hashing) {
4141
public static Blob from(BlobWriter writer) {
4242
return new HashingWriterBlob(writer);
4343
}
44+
45+
private Blobs() {}
4446
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2017 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.crepecake.http;
18+
19+
/**
20+
* Holds the credentials for an HTTP {@code Authorization} header.
21+
*
22+
* <p>The HTTP {@code Authorization} header is in the format:
23+
*
24+
* <pre>{@code Authorization: <scheme> <token>}</pre>
25+
*/
26+
public class Authorization {
27+
28+
private final String scheme;
29+
private final String token;
30+
31+
Authorization(String scheme, String token) {
32+
this.scheme = scheme;
33+
this.token = token;
34+
}
35+
36+
public String getScheme() {
37+
return scheme;
38+
}
39+
40+
public String getToken() {
41+
return token;
42+
}
43+
44+
/** Return the HTTP {@link Authorization} header value. */
45+
@Override
46+
public String toString() {
47+
return scheme + " " + token;
48+
}
49+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2017 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.tools.crepecake.http;
18+
19+
/** Static initializers for {@link Authorization}. */
20+
public class Authorizations {
21+
22+
public static Authorization withBearerToken(String token) {
23+
return new Authorization("Bearer", token);
24+
}
25+
26+
public static Authorization withBasicToken(String token) {
27+
return new Authorization("Basic", token);
28+
}
29+
30+
private Authorizations() {}
31+
}

‎crepecake/src/main/java/com/google/cloud/tools/crepecake/http/Connection.java‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
import com.google.api.client.http.HttpRequestFactory;
2222
import com.google.api.client.http.HttpResponse;
2323
import com.google.api.client.http.javanet.NetHttpTransport;
24-
import com.google.common.annotations.VisibleForTesting;
2524
import java.io.Closeable;
2625
import java.io.IOException;
2726
import java.net.URL;
27+
import javax.annotation.Nullable;
2828

2929
/**
3030
* Sends an HTTP {@link Request} and stores the {@link Response}.
@@ -43,24 +43,18 @@ public class Connection implements Closeable {
4343
private static final HttpRequestFactory HTTP_REQUEST_FACTORY =
4444
new NetHttpTransport().createRequestFactory();
4545

46-
private final HttpRequestFactory requestFactory;
46+
private HttpRequestFactory requestFactory = HTTP_REQUEST_FACTORY;
4747

48-
private HttpResponse httpResponse;
48+
@Nullable private HttpResponse httpResponse;
4949

5050
/** The URL to send the request to. */
51-
private GenericUrl url;
51+
private final GenericUrl url;
5252

5353
/**
5454
* Make sure to wrap with a try-with-resource to ensure that the connection is closed after usage.
5555
*/
56-
public Connection(URL url) throws IOException {
57-
this(url, HTTP_REQUEST_FACTORY);
58-
}
59-
60-
@VisibleForTesting
61-
Connection(URL url, HttpRequestFactory requestFactory) {
56+
public Connection(URL url) {
6257
this.url = new GenericUrl(url);
63-
this.requestFactory = requestFactory;
6458
}
6559

6660
@Override

‎crepecake/src/main/java/com/google/cloud/tools/crepecake/http/Request.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ public BlobHttpContent getHttpContent() {
3838
return body;
3939
}
4040

41+
/** Sets the {@code Authorization} header. */
42+
public Request setAuthorization(Authorization authorization) {
43+
headers.setAuthorization(authorization.toString());
44+
return this;
45+
}
46+
4147
/** Sets the {@code Content-Type} header. */
4248
public Request setContentType(String contentType) {
4349
headers.setContentType(contentType);

‎crepecake/src/main/java/com/google/cloud/tools/crepecake/json/ImageToJsonTranslator.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public Blob getContainerConfigurationBlob() throws IOException, LayerPropertyNot
7777
template.setContainerEntrypoint(image.getEntrypoint());
7878

7979
// Serializes into JSON.
80-
return JsonHelper.toBlob(template);
80+
return JsonTemplateMapper.toBlob(template);
8181
}
8282

8383
/**
@@ -102,6 +102,6 @@ public Blob getManifestBlob(BlobDescriptor containerConfigurationBlobDescriptor)
102102
}
103103

104104
// Serializes into JSON.
105-
return JsonHelper.toBlob(template);
105+
return JsonTemplateMapper.toBlob(template);
106106
}
107107
}

‎crepecake/src/main/java/com/google/cloud/tools/crepecake/json/JsonTemplate.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.fasterxml.jackson.annotation.JsonInclude;
2121

2222
/**
23-
* All JSON templates to be used with {@link JsonHelper} must extend this class.
23+
* All JSON templates to be used with {@link JsonTemplateMapper} must extend this class.
2424
*
2525
* <p>Json fields should be private fields and fields that are {@code null} will not be serialized.
2626
*/

0 commit comments

Comments
 (0)