Skip to content

Commit eb75087

Browse files
iancha1992illicitonionkeertk
authored
Use wall-time for credential helper invalidation (#18413)
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 Co-authored-by: Daniel Wagner-Hall <[email protected]> Co-authored-by: keertk <[email protected]>
1 parent bf82e7b commit eb75087

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
@@ -10,7 +10,10 @@ filegroup(
1010

1111
java_library(
1212
name = "credential_module",
13-
srcs = ["CredentialModule.java"],
13+
srcs = [
14+
"CredentialModule.java",
15+
"SystemMillisTicker.java",
16+
],
1417
deps = [
1518
"//src/main/java/com/google/devtools/build/lib:runtime",
1619
"//src/main/java/com/google/devtools/build/lib/authandtls",
@@ -23,7 +26,10 @@ java_library(
2326
name = "credentialhelper",
2427
srcs = glob(
2528
["*.java"],
26-
exclude = ["CredentialModule.java"],
29+
exclude = [
30+
"CredentialModule.java",
31+
"SystemMillisTicker.java",
32+
],
2733
),
2834
deps = [
2935
"//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)