Search in sources :

Example 1 with FailoverHttpClient

use of com.google.cloud.tools.jib.http.FailoverHttpClient in project jib by GoogleContainerTools.

the class RegistryAuthenticatorTest method testUserAgent.

@Test
public void testUserAgent() throws IOException, InterruptedException, GeneralSecurityException, URISyntaxException, RegistryCredentialsNotSentException {
    try (TestWebServer server = new TestWebServer(false)) {
        try {
            RegistryAuthenticator authenticator = RegistryAuthenticator.fromAuthenticationMethod("Bearer realm=\"" + server.getEndpoint() + "\"", registryEndpointRequestProperties, "Competent-Agent", new FailoverHttpClient(true, false, ignored -> {
            })).get();
            authenticator.authenticatePush(null);
        } catch (RegistryAuthenticationFailedException ex) {
        // Doesn't matter if auth fails. We only examine what we sent.
        }
        MatcherAssert.assertThat(server.getInputRead(), CoreMatchers.containsString("User-Agent: Competent-Agent"));
    }
}
Also used : RegistryAuthenticationFailedException(com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException) FailoverHttpClient(com.google.cloud.tools.jib.http.FailoverHttpClient) TestWebServer(com.google.cloud.tools.jib.http.TestWebServer) Test(org.junit.Test)

Example 2 with FailoverHttpClient

use of com.google.cloud.tools.jib.http.FailoverHttpClient in project jib by GoogleContainerTools.

the class BuildImageMojoIntegrationTest method testExecute_multiPlatformBuild.

@Test
public void testExecute_multiPlatformBuild() throws IOException, VerificationException, RegistryException {
    String targetImage = "localhost:5000/multiplatform:maven" + System.nanoTime();
    Verifier verifier = new Verifier(simpleTestProject.getProjectRoot().toString());
    verifier.setSystemProperty("_TARGET_IMAGE", targetImage);
    verifier.setSystemProperty("jib.to.auth.username", "testuser");
    verifier.setSystemProperty("jib.to.auth.password", "testpassword");
    verifier.setSystemProperty("sendCredentialsOverHttp", "true");
    verifier.setSystemProperty("jib.allowInsecureRegistries", "true");
    verifier.setAutoclean(false);
    verifier.addCliOption("--file=pom-multiplatform-build.xml");
    verifier.executeGoals(Arrays.asList("clean", "compile", "jib:build"));
    verifier.verifyErrorFreeLog();
    FailoverHttpClient httpClient = new FailoverHttpClient(true, true, ignored -> {
    });
    RegistryClient registryClient = RegistryClient.factory(EventHandlers.NONE, "localhost:5000", "multiplatform", httpClient).setCredential(Credential.from("testuser", "testpassword")).newRegistryClient();
    registryClient.configureBasicAuth();
    // manifest list by tag ":latest"
    ManifestTemplate manifestList = registryClient.pullManifest("latest").getManifest();
    assertThat(manifestList).isInstanceOf(V22ManifestListTemplate.class);
    V22ManifestListTemplate v22ManifestList = (V22ManifestListTemplate) manifestList;
    assertThat(v22ManifestList.getManifests().size()).isEqualTo(2);
    ManifestDescriptorTemplate.Platform platform1 = v22ManifestList.getManifests().get(0).getPlatform();
    ManifestDescriptorTemplate.Platform platform2 = v22ManifestList.getManifests().get(1).getPlatform();
    assertThat(platform1.getArchitecture()).isEqualTo("arm64");
    assertThat(platform1.getOs()).isEqualTo("linux");
    assertThat(platform2.getArchitecture()).isEqualTo("amd64");
    assertThat(platform2.getOs()).isEqualTo("linux");
    // manifest list by tag ":another"
    ManifestTemplate anotherManifestList = registryClient.pullManifest("another").getManifest();
    assertThat(JsonTemplateMapper.toUtf8String(anotherManifestList)).isEqualTo(JsonTemplateMapper.toUtf8String(manifestList));
    // Check arm64/linux container config.
    List<String> arm64Digests = v22ManifestList.getDigestsForPlatform("arm64", "linux");
    assertThat(arm64Digests.size()).isEqualTo(1);
    String arm64Digest = arm64Digests.get(0);
    ManifestTemplate arm64Manifest = registryClient.pullManifest(arm64Digest).getManifest();
    assertThat(arm64Manifest).isInstanceOf(V22ManifestTemplate.class);
    V22ManifestTemplate arm64V22Manifest = (V22ManifestTemplate) arm64Manifest;
    DescriptorDigest arm64ConfigDigest = arm64V22Manifest.getContainerConfiguration().getDigest();
    Blob arm64ConfigBlob = registryClient.pullBlob(arm64ConfigDigest, ignored -> {
    }, ignored -> {
    });
    String arm64Config = Blobs.writeToString(arm64ConfigBlob);
    assertThat(arm64Config).contains("\"architecture\":\"arm64\"");
    assertThat(arm64Config).contains("\"os\":\"linux\"");
    // Check amd64/linux container config.
    List<String> amd64Digests = v22ManifestList.getDigestsForPlatform("amd64", "linux");
    assertThat(amd64Digests.size()).isEqualTo(1);
    String amd64Digest = amd64Digests.get(0);
    ManifestTemplate amd64Manifest = registryClient.pullManifest(amd64Digest).getManifest();
    assertThat(amd64Manifest).isInstanceOf(V22ManifestTemplate.class);
    V22ManifestTemplate amd64V22Manifest = (V22ManifestTemplate) amd64Manifest;
    DescriptorDigest amd64ConfigDigest = amd64V22Manifest.getContainerConfiguration().getDigest();
    Blob amd64ConfigBlob = registryClient.pullBlob(amd64ConfigDigest, ignored -> {
    }, ignored -> {
    });
    String amd64Config = Blobs.writeToString(amd64ConfigBlob);
    assertThat(amd64Config).contains("\"architecture\":\"amd64\"");
    assertThat(amd64Config).contains("\"os\":\"linux\"");
}
Also used : FailoverHttpClient(com.google.cloud.tools.jib.http.FailoverHttpClient) V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) Blob(com.google.cloud.tools.jib.blob.Blob) ManifestDescriptorTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate.ManifestDescriptorTemplate) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) Verifier(org.apache.maven.it.Verifier) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) Test(org.junit.Test)

Example 3 with FailoverHttpClient

use of com.google.cloud.tools.jib.http.FailoverHttpClient in project jib by google.

the class RegistryAuthenticatorTest method testUserAgent.

@Test
public void testUserAgent() throws IOException, InterruptedException, GeneralSecurityException, URISyntaxException, RegistryCredentialsNotSentException {
    try (TestWebServer server = new TestWebServer(false)) {
        try {
            RegistryAuthenticator authenticator = RegistryAuthenticator.fromAuthenticationMethod("Bearer realm=\"" + server.getEndpoint() + "\"", registryEndpointRequestProperties, "Competent-Agent", new FailoverHttpClient(true, false, ignored -> {
            })).get();
            authenticator.authenticatePush(null);
        } catch (RegistryAuthenticationFailedException ex) {
        // Doesn't matter if auth fails. We only examine what we sent.
        }
        MatcherAssert.assertThat(server.getInputRead(), CoreMatchers.containsString("User-Agent: Competent-Agent"));
    }
}
Also used : RegistryAuthenticationFailedException(com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException) FailoverHttpClient(com.google.cloud.tools.jib.http.FailoverHttpClient) TestWebServer(com.google.cloud.tools.jib.http.TestWebServer) Test(org.junit.Test)

Example 4 with FailoverHttpClient

use of com.google.cloud.tools.jib.http.FailoverHttpClient in project jib by google.

the class BuildImageMojoIntegrationTest method testExecute_multiPlatformBuild.

@Test
public void testExecute_multiPlatformBuild() throws IOException, VerificationException, RegistryException {
    String targetImage = "localhost:5000/multiplatform:maven" + System.nanoTime();
    Verifier verifier = new Verifier(simpleTestProject.getProjectRoot().toString());
    verifier.setSystemProperty("_TARGET_IMAGE", targetImage);
    verifier.setSystemProperty("jib.to.auth.username", "testuser");
    verifier.setSystemProperty("jib.to.auth.password", "testpassword");
    verifier.setSystemProperty("sendCredentialsOverHttp", "true");
    verifier.setSystemProperty("jib.allowInsecureRegistries", "true");
    verifier.setAutoclean(false);
    verifier.addCliOption("--file=pom-multiplatform-build.xml");
    verifier.executeGoals(Arrays.asList("clean", "compile", "jib:build"));
    verifier.verifyErrorFreeLog();
    FailoverHttpClient httpClient = new FailoverHttpClient(true, true, ignored -> {
    });
    RegistryClient registryClient = RegistryClient.factory(EventHandlers.NONE, "localhost:5000", "multiplatform", httpClient).setCredential(Credential.from("testuser", "testpassword")).newRegistryClient();
    registryClient.configureBasicAuth();
    // manifest list by tag ":latest"
    ManifestTemplate manifestList = registryClient.pullManifest("latest").getManifest();
    assertThat(manifestList).isInstanceOf(V22ManifestListTemplate.class);
    V22ManifestListTemplate v22ManifestList = (V22ManifestListTemplate) manifestList;
    assertThat(v22ManifestList.getManifests().size()).isEqualTo(2);
    ManifestDescriptorTemplate.Platform platform1 = v22ManifestList.getManifests().get(0).getPlatform();
    ManifestDescriptorTemplate.Platform platform2 = v22ManifestList.getManifests().get(1).getPlatform();
    assertThat(platform1.getArchitecture()).isEqualTo("arm64");
    assertThat(platform1.getOs()).isEqualTo("linux");
    assertThat(platform2.getArchitecture()).isEqualTo("amd64");
    assertThat(platform2.getOs()).isEqualTo("linux");
    // manifest list by tag ":another"
    ManifestTemplate anotherManifestList = registryClient.pullManifest("another").getManifest();
    assertThat(JsonTemplateMapper.toUtf8String(anotherManifestList)).isEqualTo(JsonTemplateMapper.toUtf8String(manifestList));
    // Check arm64/linux container config.
    List<String> arm64Digests = v22ManifestList.getDigestsForPlatform("arm64", "linux");
    assertThat(arm64Digests.size()).isEqualTo(1);
    String arm64Digest = arm64Digests.get(0);
    ManifestTemplate arm64Manifest = registryClient.pullManifest(arm64Digest).getManifest();
    assertThat(arm64Manifest).isInstanceOf(V22ManifestTemplate.class);
    V22ManifestTemplate arm64V22Manifest = (V22ManifestTemplate) arm64Manifest;
    DescriptorDigest arm64ConfigDigest = arm64V22Manifest.getContainerConfiguration().getDigest();
    Blob arm64ConfigBlob = registryClient.pullBlob(arm64ConfigDigest, ignored -> {
    }, ignored -> {
    });
    String arm64Config = Blobs.writeToString(arm64ConfigBlob);
    assertThat(arm64Config).contains("\"architecture\":\"arm64\"");
    assertThat(arm64Config).contains("\"os\":\"linux\"");
    // Check amd64/linux container config.
    List<String> amd64Digests = v22ManifestList.getDigestsForPlatform("amd64", "linux");
    assertThat(amd64Digests.size()).isEqualTo(1);
    String amd64Digest = amd64Digests.get(0);
    ManifestTemplate amd64Manifest = registryClient.pullManifest(amd64Digest).getManifest();
    assertThat(amd64Manifest).isInstanceOf(V22ManifestTemplate.class);
    V22ManifestTemplate amd64V22Manifest = (V22ManifestTemplate) amd64Manifest;
    DescriptorDigest amd64ConfigDigest = amd64V22Manifest.getContainerConfiguration().getDigest();
    Blob amd64ConfigBlob = registryClient.pullBlob(amd64ConfigDigest, ignored -> {
    }, ignored -> {
    });
    String amd64Config = Blobs.writeToString(amd64ConfigBlob);
    assertThat(amd64Config).contains("\"architecture\":\"amd64\"");
    assertThat(amd64Config).contains("\"os\":\"linux\"");
}
Also used : FailoverHttpClient(com.google.cloud.tools.jib.http.FailoverHttpClient) V22ManifestListTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) Blob(com.google.cloud.tools.jib.blob.Blob) ManifestDescriptorTemplate(com.google.cloud.tools.jib.image.json.V22ManifestListTemplate.ManifestDescriptorTemplate) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) Verifier(org.apache.maven.it.Verifier) V22ManifestTemplate(com.google.cloud.tools.jib.image.json.V22ManifestTemplate) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) Test(org.junit.Test)

Example 5 with FailoverHttpClient

use of com.google.cloud.tools.jib.http.FailoverHttpClient in project jib by google.

the class UpdateChecker method performUpdateCheck.

@VisibleForTesting
static Optional<String> performUpdateCheck(Path configDir, String currentVersion, String versionUrl, String toolName, Consumer<LogEvent> log) {
    Path lastUpdateCheck = configDir.resolve(LAST_UPDATE_CHECK_FILENAME);
    try {
        // Check time of last update check
        if (Files.exists(lastUpdateCheck)) {
            try {
                String fileContents = new String(Files.readAllBytes(lastUpdateCheck), StandardCharsets.UTF_8);
                Instant modifiedTime = Instant.parse(fileContents);
                if (modifiedTime.plus(Duration.ofDays(1)).isAfter(Instant.now())) {
                    return Optional.empty();
                }
            } catch (DateTimeParseException | IOException ex) {
                // If reading update time failed, file might be corrupt, so delete it
                log.accept(LogEvent.debug("Failed to read lastUpdateCheck; " + ex.getMessage()));
                Files.delete(lastUpdateCheck);
            }
        }
        // Check for update
        FailoverHttpClient httpClient = new FailoverHttpClient(true, false, ignored -> {
        });
        try {
            Response response = httpClient.get(new URL(versionUrl), Request.builder().setHttpTimeout(3000).setUserAgent("jib " + currentVersion + " " + toolName).build());
            VersionJsonTemplate version = JsonTemplateMapper.readJson(response.getBody(), VersionJsonTemplate.class);
            Path lastUpdateCheckTemp = Files.createTempFile(configDir, LAST_UPDATE_CHECK_FILENAME, null);
            lastUpdateCheckTemp.toFile().deleteOnExit();
            Files.write(lastUpdateCheckTemp, Instant.now().toString().getBytes(StandardCharsets.UTF_8));
            Files.move(lastUpdateCheckTemp, lastUpdateCheck, StandardCopyOption.REPLACE_EXISTING);
            if (currentVersion.equals(version.latest)) {
                return Optional.empty();
            }
            return Optional.of(version.latest);
        } finally {
            httpClient.shutDown();
        }
    } catch (IOException ex) {
        log.accept(LogEvent.debug("Update check failed; " + ex.getMessage()));
    }
    return Optional.empty();
}
Also used : Path(java.nio.file.Path) Response(com.google.cloud.tools.jib.http.Response) DateTimeParseException(java.time.format.DateTimeParseException) FailoverHttpClient(com.google.cloud.tools.jib.http.FailoverHttpClient) Instant(java.time.Instant) IOException(java.io.IOException) URL(java.net.URL) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

FailoverHttpClient (com.google.cloud.tools.jib.http.FailoverHttpClient)6 Test (org.junit.Test)4 DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)2 RegistryAuthenticationFailedException (com.google.cloud.tools.jib.api.RegistryAuthenticationFailedException)2 Blob (com.google.cloud.tools.jib.blob.Blob)2 Response (com.google.cloud.tools.jib.http.Response)2 TestWebServer (com.google.cloud.tools.jib.http.TestWebServer)2 ManifestTemplate (com.google.cloud.tools.jib.image.json.ManifestTemplate)2 V22ManifestListTemplate (com.google.cloud.tools.jib.image.json.V22ManifestListTemplate)2 ManifestDescriptorTemplate (com.google.cloud.tools.jib.image.json.V22ManifestListTemplate.ManifestDescriptorTemplate)2 V22ManifestTemplate (com.google.cloud.tools.jib.image.json.V22ManifestTemplate)2 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 IOException (java.io.IOException)2 URL (java.net.URL)2 Path (java.nio.file.Path)2 Instant (java.time.Instant)2 DateTimeParseException (java.time.format.DateTimeParseException)2 Verifier (org.apache.maven.it.Verifier)2