-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Keep credentials cached across build commands. #16822
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2022 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.devtools.build.lib.authandtls.credentialhelper; | ||
|
||
import com.github.benmanes.caffeine.cache.Cache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.devtools.build.lib.authandtls.AuthAndTLSOptions; | ||
import com.google.devtools.build.lib.runtime.BlazeModule; | ||
import com.google.devtools.build.lib.runtime.CommandEnvironment; | ||
import java.net.URI; | ||
import java.time.Duration; | ||
|
||
/** A module whose sole purpose is to hold the credential cache which is shared by other modules. */ | ||
public class CredentialModule extends BlazeModule { | ||
private final Cache<URI, ImmutableMap<String, ImmutableList<String>>> credentialCache = | ||
Caffeine.newBuilder().expireAfterWrite(Duration.ZERO).build(); | ||
|
||
/** Returns the credential cache. */ | ||
public Cache<URI, ImmutableMap<String, ImmutableList<String>>> getCredentialCache() { | ||
return credentialCache; | ||
} | ||
|
||
@Override | ||
public void beforeCommand(CommandEnvironment env) { | ||
// Update the cache expiration policy according to the command options. | ||
AuthAndTLSOptions authAndTlsOptions = env.getOptions().getOptions(AuthAndTLSOptions.class); | ||
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. Probably clear the cache when the command is 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 think that's probably expected behavior for |
||
credentialCache.policy().expireAfterWrite().get() | ||
.setExpiresAfter(authAndTlsOptions.credentialHelperCacheTimeout); | ||
|
||
// Clear the cache on clean. | ||
if (env.getCommand().name().equals("clean")) { | ||
credentialCache.invalidateAll(); | ||
} | ||
} | ||
} |
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.
Not sure whether this is the best way to provide the cache. For now it seems fine as we have limited scope but if we ever want to share the cache for more modules (e.g. for bzlmod), please consider adding a provider to
ServerBuilder
.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.
Acknowledged; I'd prefer to wait until repository fetching is wired up to see how the whole thing looks.
I think in any case we'd still want this module to host the
beforeCommand
logic (otherwise we'd have to arbitrarily choose one of the many modules that will eventually use credentials, making the scope unclear).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.
FWIW, there's https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/runtime/ServerBuilder.java;l=202;bpv=1;bpt=1 which we may be able to re-use for the credential helper.