Skip to content

Commit 5f77aba

Browse files
illicitonioncopybara-github
authored andcommitted
Use wall-time for credential helper invalidation
Previously we were using time intervals which excluded time spent with the system sleeping, which is not appropriate for expiring tokens which expire based on wall-time duration. Closes #18301. PiperOrigin-RevId: 529340767 Change-Id: I15e74e7bc87284f8ba53aedace955b29bd52df8e
1 parent 8087520 commit 5f77aba

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper/BUILD

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ filegroup(
1313

1414
java_library(
1515
name = "credential_module",
16-
srcs = ["CredentialModule.java"],
16+
srcs = [
17+
"CredentialModule.java",
18+
"SystemMillisTicker.java",
19+
],
1720
deps = [
1821
"//src/main/java/com/google/devtools/build/lib:runtime",
1922
"//src/main/java/com/google/devtools/build/lib/authandtls",
@@ -26,7 +29,10 @@ java_library(
2629
name = "credentialhelper",
2730
srcs = glob(
2831
["*.java"],
29-
exclude = ["CredentialModule.java"],
32+
exclude = [
33+
"CredentialModule.java",
34+
"SystemMillisTicker.java",
35+
],
3036
),
3137
deps = [
3238
"//src/main/java/com/google/devtools/build/lib/events",

src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper/CredentialModule.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
/** A module whose sole purpose is to hold the credential cache which is shared by other modules. */
2828
public class CredentialModule extends BlazeModule {
2929
private final Cache<URI, ImmutableMap<String, ImmutableList<String>>> credentialCache =
30-
Caffeine.newBuilder().expireAfterWrite(Duration.ZERO).build();
30+
Caffeine.newBuilder()
31+
.expireAfterWrite(Duration.ZERO)
32+
.ticker(SystemMillisTicker.INSTANCE)
33+
.build();
3134

3235
/** Returns the credential cache. */
3336
public Cache<URI, ImmutableMap<String, ImmutableList<String>>> getCredentialCache() {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2023 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.devtools.build.lib.authandtls.credentialhelper;
16+
17+
import com.github.benmanes.caffeine.cache.Ticker;
18+
19+
/**
20+
* SystemMillisTicker is a Ticker which uses the unix epoch as its fixed reference point.
21+
*
22+
* <p>It is preferable to com.github.benmanes.caffeine.cache.Ticker.SystemTicker because that class
23+
* doesn't increment its time-source while the system is asleep, which isn't appropriate when
24+
* expiring tokens which have wall-time-based expiry policies.
25+
*/
26+
public class SystemMillisTicker implements Ticker {
27+
public static final SystemMillisTicker INSTANCE = new SystemMillisTicker();
28+
29+
private SystemMillisTicker() {}
30+
31+
@Override
32+
public long read() {
33+
return System.currentTimeMillis() * 1_000_000;
34+
}
35+
}

0 commit comments

Comments
 (0)