Skip to content

Commit fdbddcf

Browse files
committed
Work around unwanted side-effect of getCredentials()
Previously, we called getCredentials() to determine whether or not a repository requires authentication. Unfortunately, the method has the unwanted side-effect of assigning empty username and password credentials to a repository that previously did not require authentication and did not, therefore, have any credentials. These empty credentials can then cause subsequent failures because "Username must not be null!". There's no side-effect-free public API for accessing a repository's credentials. Instead, we're using some internal API on AuthenticationSupportedInternal. If this causes problems when upgrading to a new version of Gradle a different approach will be required. For example, we could pass in the repositories in two separate collections: those that require authentication and those that don't. Closes gh-45950
1 parent 5bbc075 commit fdbddcf

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/MavenMetadataVersionResolver.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
import javax.xml.xpath.XPathFactory;
3232

3333
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
34+
import org.gradle.api.artifacts.repositories.PasswordCredentials;
35+
import org.gradle.api.credentials.Credentials;
36+
import org.gradle.internal.artifacts.repositories.AuthenticationSupportedInternal;
3437
import org.w3c.dom.Document;
3538
import org.w3c.dom.NodeList;
3639
import org.xml.sax.InputSource;
@@ -83,9 +86,10 @@ private Set<String> resolveVersions(String groupId, String artifactId, MavenArti
8386
.toUri();
8487
try {
8588
HttpHeaders headers = new HttpHeaders();
86-
String username = repository.getCredentials().getUsername();
89+
PasswordCredentials credentials = credentialsOf(repository);
90+
String username = (credentials != null) ? credentials.getUsername() : null;
8791
if (username != null) {
88-
headers.setBasicAuth(username, repository.getCredentials().getPassword());
92+
headers.setBasicAuth(username, credentials.getPassword());
8993
}
9094
HttpEntity<Void> request = new HttpEntity<>(headers);
9195
String metadata = this.rest.exchange(url, HttpMethod.GET, request, String.class).getBody();
@@ -112,4 +116,25 @@ private Set<String> resolveVersions(String groupId, String artifactId, MavenArti
112116
return versions;
113117
}
114118

119+
/**
120+
* Retrives the configured credentials of the given {@code repository}. We cannot use
121+
* {@link MavenArtifactRepository#getCredentials()} as, if the repository has no
122+
* credentials, it has the unwanted side-effect of assigning an empty set of username
123+
* and password credentials to the repository which may cause subsequent "Username
124+
* must not be null!" failures.
125+
* @param repository the repository that is the source of the credentials
126+
* @return the configured password credentials or {@code null}
127+
*/
128+
private PasswordCredentials credentialsOf(MavenArtifactRepository repository) {
129+
Credentials credentials = ((AuthenticationSupportedInternal) repository).getConfiguredCredentials().getOrNull();
130+
if (credentials != null) {
131+
if (credentials instanceof PasswordCredentials passwordCredentials) {
132+
return passwordCredentials;
133+
}
134+
throw new IllegalStateException("Repository '%s (%s)' has credentials '%s' that are not PasswordCredentials"
135+
.formatted(repository.getName(), repository.getUrl(), credentials));
136+
}
137+
return null;
138+
}
139+
115140
}

0 commit comments

Comments
 (0)