Skip to content

Commit

Permalink
Ignore registry port when inferring auth from maven settings (#2212)
Browse files Browse the repository at this point in the history
* Ignore port as fallback when inferring registry auth from maven settings

Fixes #2135

* Update comment

Co-authored-by: Chanseok Oh <chanseok@google.com>
  • Loading branch information
padyx and chanseokoh committed Jan 15, 2020
1 parent 9bec3e6 commit 72a26cb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import com.google.cloud.tools.jib.plugins.common.AuthProperty;
import com.google.cloud.tools.jib.plugins.common.InferredAuthException;
import com.google.cloud.tools.jib.plugins.common.InferredAuthProvider;
import com.google.common.annotations.VisibleForTesting;
import java.util.Optional;
import javax.annotation.Nullable;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.building.SettingsProblem;
Expand Down Expand Up @@ -58,7 +60,7 @@ class MavenSettingsServerCredentials implements InferredAuthProvider {
@Override
public Optional<AuthProperty> inferAuth(String registry) throws InferredAuthException {

Server server = settings.getServer(registry);
Server server = getServerFromMavenSettings(registry);
if (server == null) {
return Optional.empty();
}
Expand Down Expand Up @@ -108,4 +110,20 @@ public String getPasswordDescriptor() {
}
});
}

@Nullable
@VisibleForTesting
Server getServerFromMavenSettings(String registry) {
Server server = settings.getServer(registry);
if (server != null) {
return server;
}

// try without port
int index = registry.lastIndexOf(':');
if (index != -1) {
return settings.getServer(registry.substring(0, index));
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,34 @@ public void testInferredAuth_successNoPasswordDoesNotBlowUp() throws InferredAut
Assert.assertEquals("password2", auth.get().getPassword());
}

@Test
public void testInferredAuth_registryWithHostAndPort() throws InferredAuthException {
Optional<AuthProperty> auth =
mavenSettingsServerCredentialsNoMasterPassword.inferAuth("docker.example.com:8080");
Assert.assertTrue(auth.isPresent());
Assert.assertEquals("registryUser", auth.get().getUsername());
Assert.assertEquals("registryPassword", auth.get().getPassword());
}

@Test
public void testInferredAuth_registryWithHostWithoutPort() throws InferredAuthException {
Optional<AuthProperty> auth =
mavenSettingsServerCredentialsNoMasterPassword.inferAuth("docker.example.com");
Assert.assertTrue(auth.isPresent());
Assert.assertEquals("registryUser", auth.get().getUsername());
Assert.assertEquals("registryPassword", auth.get().getPassword());
}

@Test
public void testInferredAuth_registrySettingsWithPort() throws InferredAuthException {
// Attempt to resolve WITHOUT the port. Should work as well.
Optional<AuthProperty> auth =
mavenSettingsServerCredentialsNoMasterPassword.inferAuth("docker.example.com:5432");
Assert.assertTrue(auth.isPresent());
Assert.assertEquals("registryUser", auth.get().getUsername());
Assert.assertEquals("registryPassword", auth.get().getPassword());
}

@Test
public void testInferredAuth_notFound() throws InferredAuthException {
Assert.assertFalse(mavenSettingsServerCredentials.inferAuth("serverUnknown").isPresent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@
<username>badUser</username>
<password>{i-made-this-up=}</password>
</server>
<server>
<id>docker.example.com</id>
<username>registryUser</username>
<password>registryPassword</password>
</server>
</servers>
</settings>

0 comments on commit 72a26cb

Please sign in to comment.