Java Code Examples for com.spotify.docker.client.messages.RegistryConfigs#create()

The following examples show how to use com.spotify.docker.client.messages.RegistryConfigs#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContainerRegistryAuthSupplier.java    From docker-client with Apache License 2.0 6 votes vote down vote up
@Override
public RegistryConfigs authForBuild() throws DockerException {
  final AccessToken accessToken;
  try {
    accessToken = getAccessToken();
  } catch (IOException e) {
    // do not fail as the GCR access token may not be necessary for building the image currently
    // being built
    log.warn("unable to get access token for Google Container Registry, "
             + "configuration for building image will not contain RegistryAuth for GCR",
        e);
    return RegistryConfigs.empty();
  }

  final Map<String, RegistryAuth> configs = new HashMap<>(GCR_REGISTRIES.size());
  for (String serverName : GCR_REGISTRIES) {
    configs.put(serverName, authForAccessToken(accessToken));
  }
  return RegistryConfigs.create(configs);
}
 
Example 2
Source File: MultiRegistryAuthSupplierTest.java    From docker-client with Apache License 2.0 6 votes vote down vote up
/**
 * Test what happens if one of the Suppliers returns null for authForBuild().
 */
@Test
public void testAuthForBuild_ReturnsNull() throws Exception {

  when(supplier1.authForBuild()).thenReturn(null);

  final RegistryConfigs registryConfigs = RegistryConfigs.create(ImmutableMap.of(
      "a",
      RegistryAuth.builder()
          .username("1")
          .serverAddress("a")
          .build(),
      "b",
      RegistryAuth.builder()
          .username("2")
          .serverAddress("b")
          .build()
  ));
  when(supplier2.authForBuild()).thenReturn(registryConfigs);

  assertThat(multiSupplier.authForBuild(), is(registryConfigs));
}
 
Example 3
Source File: MavenRegistryAuthSupplier.java    From dockerfile-maven with Apache License 2.0 5 votes vote down vote up
@Override
public RegistryConfigs authForBuild() throws DockerException {
  final Map<String, RegistryAuth> allConfigs = new HashMap<>();
  for (Server server : settings.getServers()) {
    allConfigs.put(server.getId(), createRegistryAuth(server));
  }
  return RegistryConfigs.create(allConfigs);
}
 
Example 4
Source File: MavenPomAuthSupplier.java    From dockerfile-maven with Apache License 2.0 5 votes vote down vote up
@Override
public RegistryConfigs authForBuild() {
  final Map<String, RegistryAuth> allConfigs = new HashMap<>();
  if (hasUserName()) {
    allConfigs.put(
        "config",
        RegistryAuth.builder()
            .username(this.userName)
            .password(this.password)
            .build()
    );
  }
  return RegistryConfigs.create(allConfigs);
}
 
Example 5
Source File: MultiRegistryAuthSupplier.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Override
public RegistryConfigs authForBuild() throws DockerException {
  final Map<String, RegistryAuth> allConfigs = new HashMap<>();
  // iterate through suppliers in reverse so that the earlier suppliers in the list
  // have precedence
  for (RegistryAuthSupplier supplier : Lists.reverse(suppliers)) {
    final RegistryConfigs configs = supplier.authForBuild();
    if (configs != null && configs.configs() != null) {
      allConfigs.putAll(configs.configs());
    }
  }
  return RegistryConfigs.create(allConfigs);
}
 
Example 6
Source File: ConfigFileRegistryAuthSupplierTest.java    From docker-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testAuthForBuild_Success() throws Exception {
  final RegistryConfigs configs = RegistryConfigs.create(ImmutableMap.of(
      "server1",
      RegistryAuth.builder()
          .serverAddress("server1")
          .username("user1")
          .password("pass1")
          .build()
  ));

  when(reader.authForAllRegistries(configFile.toPath())).thenReturn(configs);

  assertThat(supplier.authForBuild(), is(equalTo(configs)));
}
 
Example 7
Source File: AbstractDockerMojo.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
private RegistryAuthSupplier authSupplier() throws MojoExecutionException {

    final List<RegistryAuthSupplier> suppliers = new ArrayList<>();

    // prioritize the docker config file
    suppliers.add(new ConfigFileRegistryAuthSupplier());

    // then Google Container Registry support
    final RegistryAuthSupplier googleRegistrySupplier = googleContainerRegistryAuthSupplier();
    if (googleRegistrySupplier != null) {
      suppliers.add(googleRegistrySupplier);
    }

    // lastly, use any explicitly configured RegistryAuth as a catch-all
    final RegistryAuth registryAuth = registryAuth();
    if (registryAuth != null) {
      final RegistryConfigs configsForBuild = RegistryConfigs.create(ImmutableMap.of(
          serverIdFor(registryAuth), registryAuth
      ));
      suppliers.add(new FixedRegistryAuthSupplier(registryAuth, configsForBuild));
    }

    getLog().info("Using authentication suppliers: " +
                  Lists.transform(suppliers, new SupplierToClassNameFunction()));

    return new MultiRegistryAuthSupplier(suppliers);
  }
 
Example 8
Source File: RegistryAuthSupplierChain.java    From docker-registry-artifact-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public RegistryConfigs authForBuild() {
    return RegistryConfigs.create(Collections.singletonMap(registryAuth.serverAddress(), registryAuth));
}
 
Example 9
Source File: DefaultDockerClientUnitTest.java    From docker-client with Apache License 2.0 4 votes vote down vote up
@Test
public void testBuildPassesMultipleRegistryConfigs() throws Exception {
  final RegistryConfigs registryConfigs = RegistryConfigs.create(ImmutableMap.of(
      "server1", RegistryAuth.builder()
          .serverAddress("server1")
          .username("u1")
          .password("p1")
          .email("e1")
          .build(),

      "server2", RegistryAuth.builder()
          .serverAddress("server2")
          .username("u2")
          .password("p2")
          .email("e2")
          .build()
  ));

  final RegistryAuthSupplier authSupplier = mock(RegistryAuthSupplier.class);
  when(authSupplier.authForBuild()).thenReturn(registryConfigs);

  final DefaultDockerClient client = builder.registryAuthSupplier(authSupplier)
      .build();

  // build() calls /version to check what format of header to send
  enqueueServerApiVersion("1.20");

  server.enqueue(new MockResponse()
          .setResponseCode(200)
          .addHeader("Content-Type", "application/json")
          .setBody(
              fixture("fixtures/1.22/build.json")
          )
  );

  final Path path = Paths.get(Resources.getResource("dockerDirectory").toURI());

  client.build(path);

  final RecordedRequest versionRequest = takeRequestImmediately();
  assertThat(versionRequest.getMethod(), is("GET"));
  assertThat(versionRequest.getPath(), is("/version"));

  final RecordedRequest buildRequest = takeRequestImmediately();
  assertThat(buildRequest.getMethod(), is("POST"));
  assertThat(buildRequest.getPath(), is("/build"));

  final String registryConfigHeader = buildRequest.getHeader("X-Registry-Config");
  assertThat(registryConfigHeader, is(not(nullValue())));

  // check that the JSON in the header is equivalent to what we mocked out above from
  // the registryAuthSupplier
  final JsonNode headerJsonNode = toJson(BaseEncoding.base64().decode(registryConfigHeader));
  assertThat(headerJsonNode, is(toJson(registryConfigs.configs())));
}