Skip to content

Commit

Permalink
Remove extra call to docker credentials (#9545)
Browse files Browse the repository at this point in the history
In #9544, proper process execution handling was introduced in order
to manage error messages correctly.
  • Loading branch information
eddumelendez authored Nov 20, 2024
1 parent c68f791 commit 1facaf0
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,10 @@ private AuthConfig runCredentialProvider(String hostName, String helperOrStoreNa
final String responseErrorMsg = data.getStdout();

if (!StringUtils.isBlank(responseErrorMsg)) {
String credentialsNotFoundMsg = getGenericCredentialsNotFoundMsg(credentialProgramName);
String credentialsNotFoundMsg = getGenericCredentialsNotFoundMsg(
responseErrorMsg,
credentialProgramName
);
if (credentialsNotFoundMsg != null && credentialsNotFoundMsg.equals(responseErrorMsg)) {
log.info(
"Credential helper/store ({}) does not have credentials for {}",
Expand Down Expand Up @@ -346,48 +349,13 @@ private String effectiveRegistryName(DockerImageName dockerImageName) {
);
}

private String getGenericCredentialsNotFoundMsg(String credentialHelperName) {
private String getGenericCredentialsNotFoundMsg(String credentialsNotFoundMsg, String credentialHelperName) {
if (!CREDENTIALS_HELPERS_NOT_FOUND_MESSAGE_CACHE.containsKey(credentialHelperName)) {
String credentialsNotFoundMsg = discoverCredentialsHelperNotFoundMessage(credentialHelperName);
if (!StringUtils.isBlank(credentialsNotFoundMsg)) {
CREDENTIALS_HELPERS_NOT_FOUND_MESSAGE_CACHE.put(credentialHelperName, credentialsNotFoundMsg);
}
CREDENTIALS_HELPERS_NOT_FOUND_MESSAGE_CACHE.put(credentialHelperName, credentialsNotFoundMsg);
}

return CREDENTIALS_HELPERS_NOT_FOUND_MESSAGE_CACHE.get(credentialHelperName);
}

private String discoverCredentialsHelperNotFoundMessage(String credentialHelperName) {
// will do fake call to given credential helper to find out with which message
// it response when there are no credentials for given hostName

// hostName should be valid, but most probably not existing
// IF its not enough, then should probably run 'list' command first to be sure...
final String notExistentFakeHostName = "https://not.a.real.registry/url";

String credentialsNotFoundMsg = null;
try {
CredentialOutput data = runCredentialProgram(notExistentFakeHostName, credentialHelperName);

if (data.getStdout() != null && !data.getStdout().isEmpty()) {
credentialsNotFoundMsg = data.getStdout();

log.debug(
"Got credentials not found error message from docker credential helper - {}",
credentialsNotFoundMsg
);
}
} catch (Exception e) {
log.warn(
"Failure running docker credential helper ({}) with fake call, expected 'credentials not found' response. Exception message: {}",
credentialHelperName,
e.getMessage()
);
}

return credentialsNotFoundMsg;
}

private CredentialOutput runCredentialProgram(String hostName, String credentialHelperName)
throws InterruptedException, TimeoutException, IOException {
String[] command = SystemUtils.IS_OS_WINDOWS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public void lookupAuthConfigWithCredentialsNotFound() throws URISyntaxException,

assertThat(discoveredMessage)
.as("Not correct message discovered")
.isEqualTo("Fake credentials not found on credentials store 'https://not.a.real.registry/url'");
.isEqualTo("Fake credentials not found on credentials store 'registry2.example.com'");
}

@Test
Expand Down
4 changes: 0 additions & 4 deletions core/src/test/resources/auth-config/docker-credential-fake
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ if [ "$inputLine" = "registry2.example.com" ]; then
echo Fake credentials not found on credentials store \'$inputLine\' 0>&2
exit 1
fi
if [ "$inputLine" = "https://not.a.real.registry/url" ]; then
echo Fake credentials not found on credentials store \'$inputLine\' 0>&2
exit 1
fi

if [ "$inputLine" = "registry.example.com" ]; then
echo '{' \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ if "%inputLine%" == "registry2.example.com" (
echo Fake credentials not found on credentials store '%inputLine%' 0>&2
exit 1
)
if "%inputLine%" == "https://not.a.real.registry/url" (
echo Fake credentials not found on credentials store '%inputLine%' 0>&2
exit 1
)

if "%inputLine%" == "registry.example.com" (
echo {
Expand Down

0 comments on commit 1facaf0

Please sign in to comment.