Skip to content

Commit e36392c

Browse files
committed
Define new default values for refreshMargin and minimumTokenLifetime.
1 parent aa1967b commit e36392c

File tree

2 files changed

+86
-13
lines changed

2 files changed

+86
-13
lines changed

cab-token-generator/java/com/google/auth/credentialaccessboundary/ClientSideCredentialAccessBoundaryFactory.java

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@
8080
import javax.annotation.Nullable;
8181

8282
public class ClientSideCredentialAccessBoundaryFactory {
83-
static final Duration DEFAULT_REFRESH_MARGIN = Duration.ofMinutes(30);
84-
static final Duration DEFAULT_MINIMUM_TOKEN_LIFETIME = Duration.ofMinutes(3);
83+
static final Duration DEFAULT_REFRESH_MARGIN = Duration.ofMinutes(45);
84+
static final Duration DEFAULT_MINIMUM_TOKEN_LIFETIME = Duration.ofMinutes(30);
8585
private final GoogleCredentials sourceCredential;
8686
private final transient HttpTransportFactory transportFactory;
8787
private final String tokenExchangeEndpoint;
@@ -103,6 +103,9 @@ private ClientSideCredentialAccessBoundaryFactory(Builder builder) {
103103
this.transportFactory = builder.transportFactory;
104104
this.sourceCredential = builder.sourceCredential;
105105
this.tokenExchangeEndpoint = builder.tokenExchangeEndpoint;
106+
this.refreshMargin = builder.refreshMargin;
107+
this.minimumTokenLifetime = builder.minimumTokenLifetime;
108+
this.clock = builder.clock;
106109

107110
// Initializes the Tink AEAD registry for encrypting the client-side
108111
// restrictions.
@@ -114,14 +117,6 @@ private ClientSideCredentialAccessBoundaryFactory(Builder builder) {
114117

115118
CelOptions options = CelOptions.current().build();
116119
this.celCompiler = CelCompilerFactory.standardCelCompilerBuilder().setOptions(options).build();
117-
118-
this.refreshMargin =
119-
builder.refreshMargin != null ? builder.refreshMargin : DEFAULT_REFRESH_MARGIN;
120-
this.minimumTokenLifetime =
121-
builder.minimumTokenLifetime != null
122-
? builder.minimumTokenLifetime
123-
: DEFAULT_MINIMUM_TOKEN_LIFETIME;
124-
this.clock = builder.clock;
125120
}
126121

127122
/**
@@ -379,6 +374,16 @@ HttpTransportFactory getTransportFactory() {
379374
return transportFactory;
380375
}
381376

377+
@VisibleForTesting
378+
Duration getRefreshMargin() {
379+
return refreshMargin;
380+
}
381+
382+
@VisibleForTesting
383+
Duration getMinimumTokenLifetime() {
384+
return minimumTokenLifetime;
385+
}
386+
382387
/**
383388
* Holds intermediate credentials obtained from the STS token exchange endpoint.
384389
*
@@ -645,6 +650,23 @@ public ClientSideCredentialAccessBoundaryFactory build() {
645650
"Error occurred when attempting to retrieve source credential universe domain.", e);
646651
}
647652

653+
// Use default values for refreshMargin if not provided.
654+
if (refreshMargin == null) {
655+
this.refreshMargin = DEFAULT_REFRESH_MARGIN;
656+
}
657+
658+
// Use default values for minimumTokenLifetime if not provided.
659+
if (minimumTokenLifetime == null) {
660+
this.minimumTokenLifetime = DEFAULT_MINIMUM_TOKEN_LIFETIME;
661+
}
662+
663+
// Check if refreshMargin is at least one minute longer than minimumTokenLifetime.
664+
Duration minRefreshMargin = minimumTokenLifetime.plusMinutes(1);
665+
if (refreshMargin.compareTo(minRefreshMargin) < 0) {
666+
throw new IllegalArgumentException(
667+
"Refresh margin must be at least one minute longer than the minimum token lifetime.");
668+
}
669+
648670
this.tokenExchangeEndpoint = String.format(TOKEN_EXCHANGE_URL_FORMAT, universeDomain);
649671
return new ClientSideCredentialAccessBoundaryFactory(this);
650672
}

cab-token-generator/javatests/com/google/auth/credentialaccessboundary/ClientSideCredentialAccessBoundaryFactoryTest.java

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ public void fetchIntermediateCredentials() throws Exception {
149149
public void fetchIntermediateCredentials_withCustomUniverseDomain() throws IOException {
150150
String universeDomain = "foobar";
151151
GoogleCredentials sourceCredentials =
152-
getServiceAccountSourceCredentials(mockTokenServerTransportFactory)
153-
.toBuilder()
152+
getServiceAccountSourceCredentials(mockTokenServerTransportFactory).toBuilder()
154153
.setUniverseDomain(universeDomain)
155154
.build();
156155

@@ -483,6 +482,58 @@ public void builder_universeDomainMismatch_throws() throws IOException {
483482
exception.getMessage());
484483
}
485484

485+
@Test
486+
public void builder_invalidRefreshMarginAndMinimumTokenLifetime_throws() throws IOException {
487+
GoogleCredentials sourceCredentials =
488+
getServiceAccountSourceCredentials(mockTokenServerTransportFactory);
489+
490+
IllegalArgumentException exception =
491+
assertThrows(
492+
IllegalArgumentException.class,
493+
() -> {
494+
ClientSideCredentialAccessBoundaryFactory.newBuilder()
495+
.setSourceCredential(sourceCredentials)
496+
.setRefreshMargin(Duration.ofMinutes(50))
497+
.setMinimumTokenLifetime(Duration.ofMinutes(50))
498+
.build();
499+
});
500+
assertEquals(
501+
"Refresh margin must be at least one minute longer than the minimum token lifetime.",
502+
exception.getMessage());
503+
}
504+
505+
@Test
506+
public void builder_minimumTokenLifetimeNotSet_usesDefault() throws IOException {
507+
GoogleCredentials sourceCredentials =
508+
getServiceAccountSourceCredentials(mockTokenServerTransportFactory);
509+
510+
ClientSideCredentialAccessBoundaryFactory factory =
511+
ClientSideCredentialAccessBoundaryFactory.newBuilder()
512+
.setSourceCredential(sourceCredentials)
513+
.setRefreshMargin(Duration.ofMinutes(50))
514+
.build();
515+
516+
assertEquals(
517+
ClientSideCredentialAccessBoundaryFactory.DEFAULT_MINIMUM_TOKEN_LIFETIME,
518+
factory.getMinimumTokenLifetime());
519+
}
520+
521+
@Test
522+
public void builder_refreshMarginNotSet_usesDefault() throws IOException {
523+
GoogleCredentials sourceCredentials =
524+
getServiceAccountSourceCredentials(mockTokenServerTransportFactory);
525+
526+
ClientSideCredentialAccessBoundaryFactory factory =
527+
ClientSideCredentialAccessBoundaryFactory.newBuilder()
528+
.setSourceCredential(sourceCredentials)
529+
.setMinimumTokenLifetime(Duration.ofMinutes(20))
530+
.build();
531+
532+
assertEquals(
533+
ClientSideCredentialAccessBoundaryFactory.DEFAULT_REFRESH_MARGIN,
534+
factory.getRefreshMargin());
535+
}
536+
486537
private static GoogleCredentials getServiceAccountSourceCredentials(
487538
MockTokenServerTransportFactory transportFactory) throws IOException {
488539
String email = "[email protected]";
@@ -613,7 +664,7 @@ private static ClientSideAccessBoundary decryptRestriction(String restriction, S
613664

614665
Aead aead = keysetHandle.getPrimitive(RegistryConfiguration.get(), Aead.class);
615666
byte[] rawRestrictions =
616-
aead.decrypt(Base64.getUrlDecoder().decode(restriction), /*associatedData=*/ new byte[0]);
667+
aead.decrypt(Base64.getUrlDecoder().decode(restriction), /* associatedData= */ new byte[0]);
617668

618669
return ClientSideAccessBoundary.parseFrom(rawRestrictions);
619670
}

0 commit comments

Comments
 (0)