public static Optional<Credential> getImageCredential(
      Consumer<LogEvent> logger,
      String usernameProperty,
      String passwordProperty,
      AuthProperty auth,
      RawConfiguration rawConfiguration) {
    // System property takes priority over build configuration
    String commandlineUsername = rawConfiguration.getProperty(usernameProperty).orElse("");
    String commandlinePassword = rawConfiguration.getProperty(passwordProperty).orElse("");
    if (!commandlineUsername.isEmpty() && !commandlinePassword.isEmpty()) {
      return Optional.of(Credential.from(commandlineUsername, commandlinePassword));
    }

    // Warn if a system property is missing
    String missingProperty =
        "%s system property is set, but %s is not; attempting other authentication methods.";
    if (!commandlinePassword.isEmpty()) {
      logger.accept(
          LogEvent.warn(String.format(missingProperty, passwordProperty, usernameProperty)));
    }
    if (!commandlineUsername.isEmpty()) {
      logger.accept(
          LogEvent.warn(String.format(missingProperty, usernameProperty, passwordProperty)));
    }

    // Check auth configuration next; warn if they aren't both set
    if (!Strings.isNullOrEmpty(auth.getUsername()) && !Strings.isNullOrEmpty(auth.getPassword())) {
      return Optional.of(Credential.from(auth.getUsername(), auth.getPassword()));
    }

    String missingConfig = "%s is missing from build configuration; ignoring auth section.";
    if (!Strings.isNullOrEmpty(auth.getPassword())) {
      logger.accept(LogEvent.warn(String.format(missingConfig, auth.getUsernameDescriptor())));
    }
    if (!Strings.isNullOrEmpty(auth.getUsername())) {
      logger.accept(LogEvent.warn(String.format(missingConfig, auth.getPasswordDescriptor())));
    }

    return Optional.empty();
  }
@Test
  public void testGetImageAuth() {
    when(mockAuth.getUsernameDescriptor()).thenReturn("user");
    when(mockAuth.getPasswordDescriptor()).thenReturn("pass");
    when(mockAuth.getUsername()).thenReturn("vwxyz");
    when(mockAuth.getPassword()).thenReturn("98765");

    // System properties set
    when(mockConfiguration.getProperty("jib.test.auth.user")).thenReturn(Optional.of("abcde"));
    when(mockConfiguration.getProperty("jib.test.auth.pass")).thenReturn(Optional.of("12345"));
    Credential expected = Credential.from("abcde", "12345");
    Optional<Credential> actual =
        ConfigurationPropertyValidator.getImageCredential(
            mockLogger, "jib.test.auth.user", "jib.test.auth.pass", mockAuth, mockConfiguration);
    assertThat(actual).hasValue(expected);

    // Auth set in configuration
    when(mockConfiguration.getProperty("jib.test.auth.user")).thenReturn(Optional.empty());
    when(mockConfiguration.getProperty("jib.test.auth.pass")).thenReturn(Optional.empty());
    expected = Credential.from("vwxyz", "98765");
    actual =
        ConfigurationPropertyValidator.getImageCredential(
            mockLogger, "jib.test.auth.user", "jib.test.auth.pass", mockAuth, mockConfiguration);
    assertThat(actual).hasValue(expected);
    verify(mockLogger, never()).accept(LogEvent.warn(any()));

    // Auth completely missing
    when(mockAuth.getUsername()).thenReturn(null);
    when(mockAuth.getPassword()).thenReturn(null);
    actual =
        ConfigurationPropertyValidator.getImageCredential(
            mockLogger, "jib.test.auth.user", "jib.test.auth.pass", mockAuth, mockConfiguration);
    assertThat(actual).isEmpty();

    // Password missing
    when(mockAuth.getUsername()).thenReturn("vwxyz");
    when(mockAuth.getPassword()).thenReturn(null);
    actual =
        ConfigurationPropertyValidator.getImageCredential(
            mockLogger, "jib.test.auth.user", "jib.test.auth.pass", mockAuth, mockConfiguration);
    assertThat(actual).isEmpty();
    verify(mockLogger)
        .accept(LogEvent.warn("pass is missing from build configuration; ignoring auth section."));

    // Username missing
    when(mockAuth.getUsername()).thenReturn(null);
    when(mockAuth.getPassword()).thenReturn("98765");
    actual =
        ConfigurationPropertyValidator.getImageCredential(
            mockLogger, "jib.test.auth.user", "jib.test.auth.pass", mockAuth, mockConfiguration);
    assertThat(actual).isEmpty();
    verify(mockLogger)
        .accept(LogEvent.warn("user is missing from build configuration; ignoring auth section."));
  }