-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize authentication for base image #2789
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,7 +137,10 @@ public ImagesAndRegistryClient call() | |
progressEventDispatcherFactory.create("pulling base image manifest", 2); | ||
TimerEventDispatcher ignored1 = new TimerEventDispatcher(eventHandlers, DESCRIPTION)) { | ||
|
||
// First, try with no credentials. | ||
// First, try with no credentials. This works with public GCR images (but not Docker Hub). | ||
// TODO: investigate if we should just pass credentials up front. However, this involves | ||
// some risk. https://github.com/GoogleContainerTools/jib/pull/2200#discussion_r359069026 | ||
// contains some related discussions. | ||
RegistryClient noAuthRegistryClient = | ||
buildContext.newBaseImageRegistryClientFactory().newRegistryClient(); | ||
try { | ||
|
@@ -149,40 +152,43 @@ public ImagesAndRegistryClient call() | |
LogEvent.lifecycle( | ||
"The base image requires auth. Trying again for " + imageReference + "...")); | ||
|
||
Credential registryCredential = | ||
Credential credential = | ||
RegistryCredentialRetriever.getBaseImageCredential(buildContext).orElse(null); | ||
|
||
RegistryClient registryClient = | ||
buildContext | ||
.newBaseImageRegistryClientFactory() | ||
.setCredential(registryCredential) | ||
.setCredential(credential) | ||
.newRegistryClient(); | ||
|
||
try { | ||
// TODO: refactor the code (https://github.com/GoogleContainerTools/jib/pull/2202) | ||
if (registryCredential == null || registryCredential.isOAuth2RefreshToken()) { | ||
throw ex; | ||
} | ||
|
||
eventHandlers.dispatch(LogEvent.debug("Trying basic auth for " + imageReference + "...")); | ||
registryClient.configureBasicAuth(); | ||
String wwwAuthenticate = ex.getHttpResponseException().getHeaders().getAuthenticate(); | ||
if (wwwAuthenticate != null) { | ||
eventHandlers.dispatch( | ||
LogEvent.debug("WWW-Authenticate for " + imageReference + ": " + wwwAuthenticate)); | ||
registryClient.authPullByWwwAuthenticate(wwwAuthenticate); | ||
return new ImagesAndRegistryClient( | ||
pullBaseImages(registryClient, progressEventDispatcher), registryClient); | ||
|
||
} catch (RegistryUnauthorizedException registryUnauthorizedException) { | ||
// The registry requires us to authenticate using the Docker Token Authentication. | ||
// See https://docs.docker.com/registry/spec/auth/token | ||
eventHandlers.dispatch( | ||
LogEvent.debug("Trying bearer auth for " + imageReference + "...")); | ||
if (registryClient.doPullBearerAuth()) { | ||
return new ImagesAndRegistryClient( | ||
pullBaseImages(registryClient, progressEventDispatcher), registryClient); | ||
} else { | ||
// Not getting WWW-Authenticate is unexpected in practice, and we may just blame the | ||
// server and fail. However, to keep some old behavior, try a few things as a last resort. | ||
// TODO: consider removing this fallback branch. | ||
if (credential != null && !credential.isOAuth2RefreshToken()) { | ||
eventHandlers.dispatch( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we actually do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think this is a good idea to identify registries that are acting strange. But up to you. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I missed this comment. I thought about that, and I agree logging a warning will be very valuable to us. But I think a warning should be something actionable and fixable on the user side, so that's my hesitation. I've seen some people regard a warning seriously (probably because of a 0-warning policy auto-enforced by some system). Gathering information is really useful to us, but I feel we should be careful for dual-purposing a warning. |
||
LogEvent.debug("Trying basic auth as fallback for " + imageReference + "...")); | ||
registryClient.configureBasicAuth(); | ||
try { | ||
return new ImagesAndRegistryClient( | ||
pullBaseImages(registryClient, progressEventDispatcher), registryClient); | ||
} catch (RegistryUnauthorizedException ignored) { | ||
// Fall back to try bearer auth. | ||
} | ||
} | ||
|
||
eventHandlers.dispatch( | ||
LogEvent.error( | ||
"The registry asked for basic authentication, but the registry had refused basic " | ||
+ "authentication previously")); | ||
throw registryUnauthorizedException; | ||
LogEvent.debug("Trying bearer auth as fallback for " + imageReference + "...")); | ||
registryClient.doPullBearerAuth(); | ||
return new ImagesAndRegistryClient( | ||
pullBaseImages(registryClient, progressEventDispatcher), registryClient); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -325,15 +325,26 @@ private boolean doBearerAuth(boolean readOnlyBearerAuth) throws IOException, Reg | |
return false; // server returned "WWW-Authenticate: Basic ..." | ||
} | ||
|
||
initialBearerAuthenticator.set(authenticator.get()); | ||
doBearerAuth(readOnlyBearerAuth, authenticator.get()); | ||
return true; | ||
} | ||
|
||
private void doBearerAuth(boolean readOnlyBearerAuth, RegistryAuthenticator authenticator) | ||
throws RegistryException { | ||
initialBearerAuthenticator.set(authenticator); | ||
if (readOnlyBearerAuth) { | ||
authorization.set(authenticator.get().authenticatePull(credential)); | ||
authorization.set(authenticator.authenticatePull(credential)); | ||
} else { | ||
authorization.set(authenticator.get().authenticatePush(credential)); | ||
authorization.set(authenticator.authenticatePush(credential)); | ||
} | ||
this.readOnlyBearerAuth = readOnlyBearerAuth; | ||
eventHandlers.dispatch(LogEvent.debug("bearer auth succeeded for " + image)); | ||
return true; | ||
|
||
eventHandlers.dispatch( | ||
LogEvent.debug( | ||
"bearer auth succeeded for " | ||
+ registryEndpointRequestProperties.getServerUrl() | ||
+ "/" | ||
+ registryEndpointRequestProperties.getImageName())); | ||
} | ||
|
||
private Authorization refreshBearerAuth(@Nullable String wwwAuthenticate) | ||
|
@@ -366,6 +377,27 @@ private Authorization refreshBearerAuth(@Nullable String wwwAuthenticate) | |
return Verify.verifyNotNull(initialBearerAuthenticator.get()).authenticatePush(credential); | ||
} | ||
|
||
/** | ||
* Configure basic authentication or attempts bearer authentication for pulling based on the | ||
* specified authentication method in a server response. | ||
* | ||
* @param wwwAuthenticate {@code WWW-Authenticate} HTTP header value from a server response | ||
* specifying a required authentication method | ||
* @throws RegistryException if communicating with the endpoint fails | ||
* @throws RegistryAuthenticationFailedException if authentication fails | ||
* @throws RegistryCredentialsNotSentException if authentication failed and credentials were not | ||
*/ | ||
public void authPullByWwwAuthenticate(String wwwAuthenticate) throws RegistryException { | ||
Optional<RegistryAuthenticator> authenticator = | ||
RegistryAuthenticator.fromAuthenticationMethod( | ||
wwwAuthenticate, registryEndpointRequestProperties, getUserAgent(), httpClient); | ||
if (authenticator.isPresent()) { | ||
doBearerAuth(true, authenticator.get()); | ||
} else if (credential != null && !credential.isOAuth2RefreshToken()) { | ||
configureBasicAuth(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find this flow a little confusing. Should we just have multiple types of credentials instead of checking the state of the Credential everywhere? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like defining subclasses of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I don't know, I've haven't done much in this section of the code base, so it's all a little strange. |
||
} | ||
} | ||
|
||
/** | ||
* Check if a manifest referred to by {@code imageQualifier} (tag or digest) exists on the | ||
* registry. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems a little strange that we have so much auth code in PullBaseImage step and not somewhere else like RegistryAuthenticator?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(First of all, I don't know the initial intention of
RegistryAuthenticator
, but in reality it has always been "BearerAuthenticator" that's only about bearer auth.)As discussed offline, we could have introduced an overarching
RegistryClient.doRightAuth()
that perhaps doesI expect this flow should work for both pull and push auth with any registries, and this is exactly the flow we do for target registry auth. However, we were taking a different flow for base image registries (as can be seen in the PR), and what is one of the reasons we don't have
doRightAuth()
.Given this and that
RegistryClient
is a low-level API, it also seemed reasonable to have separate nobs for configuring basic auth and completing bearer auth. Another factor to this is the difference between configuring basic auth and completing the whole bearer auth leg: configuring basic auth means that you'll just send the credentials going forward no matter what, while completing bearer auth means interacting with a registry server and an auth server with two round-trips to get another form of credentials to send going forward.