Skip to content

Commit

Permalink
[enhancement] OCI registry login fails if the registry URL contains a…
Browse files Browse the repository at this point in the history
… context path
  • Loading branch information
mosidev committed Oct 21, 2024
1 parent da51c62 commit e0edf0a
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/main/java/com/tosan/plugin/oras/AbstractOrasMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
@Getter
@Setter
public abstract class AbstractOrasMojo extends AbstractMojo {
protected static final String LOGIN_TEMPLATE = "login -u %s %s --password-stdin";
protected static final String LOGIN_TEMPLATE = "-u %s %s --password-stdin";
protected static final String PUSH_TEMPLATE = "%s/%s:%s %s";

private final Clock clock = Clock.systemDefaultZone();
Expand Down Expand Up @@ -298,8 +298,30 @@ OCIRegistry getUploadRepo() {
void authenticate(OCIRegistry registry) throws MojoExecutionException {
PasswordAuthentication authentication = getAuthentication(registry);
if (authentication != null) {
String arguments = String.format(LOGIN_TEMPLATE, authentication.getUserName(), registry.getUrl());
oras(arguments, "", "can't login to registry", new String(authentication.getPassword()));
String loginUrl = removePathFromUrl(registry.getUrl());
String arguments = String.format(LOGIN_TEMPLATE, authentication.getUserName(), loginUrl);
oras("login", arguments, "Can't login to registry", new String(authentication.getPassword()));
}
}

private String removePathFromUrl(String urlString) {
try {
// Check if the URL contains a protocol (e.g., "https://")
int startIndex = 0;
if (urlString.contains("://")) {
startIndex = urlString.indexOf("://") + 3; // Skip past "://"
}
// Find the first "/" after the domain or host
int domainEndIndex = urlString.indexOf("/", startIndex);
// If no context path is found, return the original URL
if (domainEndIndex == -1) {
return urlString;
}
// Remove the context path by trimming everything after the domain/host
return urlString.substring(0, domainEndIndex);
} catch (Exception e) {
getLog().warn(e.getMessage(), e);
return urlString;
}
}

Expand Down

0 comments on commit e0edf0a

Please # to comment.