Skip to content

[image-builder-bob] Force refresh of oauth token #18620

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

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion components/image-builder-bob/pkg/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,28 @@ func (proxy *Proxy) reverse(alias string) *httputil.ReverseProxy {
return false, nil
}
if resp.StatusCode == http.StatusUnauthorized {
err := auth.AddResponses(context.Background(), []*http.Response{resp})
// the docker authorizer only refreshes OAuth tokens after two
// successive 401 errors for the same URL. Rather than issue the same
// request multiple times to tickle the token-refreshing logic, just
// provide the same response twice to trick it into refreshing the
// cached OAuth token. Call AddResponses() twice, first to invalidate
// the existing token (with two responses), second to fetch a new one
// (with one response).
// TODO: fix after one of these two PRs are merged and available:
// https://github.com/containerd/containerd/pull/8735
// https://github.com/containerd/containerd/pull/8388
err := auth.AddResponses(ctx, []*http.Response{resp, resp})
if err != nil {
log.WithError(err).WithField("URL", resp.Request.URL.String()).Warn("cannot add responses although response was Unauthorized")
return false, nil
}

err = auth.AddResponses(ctx, []*http.Response{resp})
if err != nil {
log.WithError(err).WithField("URL", resp.Request.URL.String()).Warn("cannot add responses although response was Unauthorized")
return false, nil
}

return true, nil
}

Expand Down