Repository: GoogleContainerTools/jib
File: jib-cli/src/main/java/com/google/cloud/tools/jib/cli/cli2/Containerizers.java (Java)

Code before:
          TarImage.at(Paths.get(imageSpec.replaceFirst(TAR_IMAGE_PREFIX, "")))
              .named(buildOptions.getName()));
    }
    ImageReference imageReference =
        ImageReference.parse(imageSpec.replaceFirst(REGISTRY_IMAGE_PREFIX, ""));
    RegistryImage registryImage = RegistryImage.named(imageReference);
    applyCredentialConfig(
        registryImage,
        CredentialRetrieverFactory.forImage(
            imageReference, logEvent -> logger.log(logEvent.getLevel(), logEvent.getMessage())),
        buildOptions);
    return Containerizer.to(registryImage);
  }

  static void applyCredentialConfig(
      RegistryImage registryImage, CredentialRetrieverFactory factory, JibCli buildOptions) {
    if (buildOptions.getUsernamePassword().isPresent()) {
      registryImage.addCredentialRetriever(buildOptions::getUsernamePassword);
    } else if (buildOptions.getToUsernamePassword().isPresent()) {
      registryImage.addCredentialRetriever(buildOptions::getToUsernamePassword);
    }
    for (String credentialHelper : buildOptions.getCredentialHelpers()) {
      Path path = Paths.get(credentialHelper);
      if (Files.exists(path)) {
        registryImage.addCredentialRetriever(factory.dockerCredentialHelper(path));
      } else {
        registryImage.addCredentialRetriever(factory.dockerCredentialHelper(credentialHelper));
      }
    }
    // then add any other known helpers
    registryImage.addCredentialRetriever(factory.dockerConfig());
    registryImage.addCredentialRetriever(factory.wellKnownCredentialHelpers());
    registryImage.addCredentialRetriever(factory.googleApplicationDefaultCredentials());
  }

  static void applyConfiguration(Containerizer containerizer, JibCli buildOptions) {
    containerizer.setToolName(VersionInfo.TOOL_NAME);
    containerizer.setToolVersion(VersionInfo.getVersionSimple());

    // TODO: it's strange that we use system properties to set these
    // TODO: perhaps we should expose these as configuration options on the containerizer
    if (buildOptions.isSendCredentialsOverHttp()) {
      System.setProperty(JibSystemProperties.SEND_CREDENTIALS_OVER_HTTP, Boolean.TRUE.toString());
    }
    if (buildOptions.isSerialize()) {
      System.setProperty(JibSystemProperties.SERIALIZE, Boolean.TRUE.toString());
    }

    containerizer.setAllowInsecureRegistries(buildOptions.isAllowInsecureRegistries());
    buildOptions.getBaseImageCache().ifPresent(containerizer::setBaseImageLayersCache);
    buildOptions.getApplicationCache().ifPresent(containerizer::setApplicationLayersCache);

Review comment (security):
I think this needs a little more work for Windows (as in `DefaultCredentialRetrievers.asList()`). Add a TODO?

Code after:
    if (imageSpec.startsWith(TAR_IMAGE_PREFIX)) {
      return Containerizer.to(
          TarImage.at(Paths.get(imageSpec.replaceFirst(TAR_IMAGE_PREFIX, "")))
              .named(buildOptions.getName()));
    }
    ImageReference imageReference =
        ImageReference.parse(imageSpec.replaceFirst(REGISTRY_IMAGE_PREFIX, ""));
    RegistryImage registryImage = RegistryImage.named(imageReference);
    DefaultCredentialRetrievers defaultCredentialRetrievers =
        DefaultCredentialRetrievers.init(
            CredentialRetrieverFactory.forImage(
                imageReference,
                logEvent -> logger.log(logEvent.getLevel(), logEvent.getMessage())));
    Credentials.getToCredentialRetrievers(buildOptions, defaultCredentialRetrievers)
        .forEach(registryImage::addCredentialRetriever);
    return Containerizer.to(registryImage);
  }

  private static void applyConfiguration(Containerizer containerizer, JibCli buildOptions) {
    containerizer.setToolName(VersionInfo.TOOL_NAME);
    containerizer.setToolVersion(VersionInfo.getVersionSimple());

    // TODO: it's strange that we use system properties to set these
    // TODO: perhaps we should expose these as configuration options on the containerizer
    if (buildOptions.isSendCredentialsOverHttp()) {
      System.setProperty(JibSystemProperties.SEND_CREDENTIALS_OVER_HTTP, Boolean.TRUE.toString());
    }
    if (buildOptions.isSerialize()) {
      System.setProperty(JibSystemProperties.SERIALIZE, Boolean.TRUE.toString());
    }

    containerizer.setAllowInsecureRegistries(buildOptions.isAllowInsecureRegistries());
    buildOptions.getBaseImageCache().ifPresent(containerizer::setBaseImageLayersCache);
    buildOptions.getApplicationCache().ifPresent(containerizer::setApplicationLayersCache);

    buildOptions.getAdditionalTags().forEach(containerizer::withAdditionalTag);
  }

  private static void applyHandlers(Containerizer containerizer, ConsoleLogger consoleLogger) {
    containerizer
        .addEventHandler(
            LogEvent.class,
            logEvent -> consoleLogger.log(logEvent.getLevel(), logEvent.getMessage()))
        .addEventHandler(
            ProgressEvent.class,
            new ProgressEventHandler(
                update -> {
                  List<String> footer =
                      ProgressDisplayGenerator.generateProgressDisplay(
                          update.getProgress(), update.getUnfinishedLeafTasks());
                  footer.add("");