Skip to content
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ target/
.vscode/

# MacOS
.DS_Store
.DS_Store

# Conductor and Gemini
conductor/
Gemini/
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
* <p>These credentials use the IAM API to sign data. See {@link #sign(byte[])} for more details.
*/
public class ComputeEngineCredentials extends GoogleCredentials
implements ServiceAccountSigner, IdTokenProvider, TrustBoundaryProvider {
implements ServiceAccountSigner, IdTokenProvider, RegionalAccessBoundaryProvider {

static final String METADATA_RESPONSE_EMPTY_CONTENT_ERROR_MESSAGE =
"Empty content from metadata token server request.";
Expand Down Expand Up @@ -386,11 +386,7 @@ public AccessToken refreshAccessToken() throws IOException {
int expiresInSeconds =
OAuth2Utils.validateInt32(responseData, "expires_in", PARSE_ERROR_PREFIX);
long expiresAtMilliseconds = clock.currentTimeMillis() + expiresInSeconds * 1000;
AccessToken newAccessToken = new AccessToken(accessToken, new Date(expiresAtMilliseconds));

refreshTrustBoundary(newAccessToken, transportFactory);

return newAccessToken;
return new AccessToken(accessToken, new Date(expiresAtMilliseconds));
}

/**
Expand Down Expand Up @@ -694,6 +690,11 @@ public static Builder newBuilder() {
*
* @throws RuntimeException if the default service account cannot be read
*/
@Override
HttpTransportFactory getTransportFactory() {
return transportFactory;
}

@Override
// todo(#314) getAccount should not throw a RuntimeException
public String getAccount() {
Expand All @@ -709,11 +710,9 @@ public String getAccount() {

@InternalApi
@Override
public String getTrustBoundaryUrl() throws IOException {
public String getRegionalAccessBoundaryUrl() throws IOException {
return String.format(
OAuth2Utils.IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_SERVICE_ACCOUNT,
getUniverseDomain(),
getAccount());
OAuth2Utils.IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_SERVICE_ACCOUNT, getAccount());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
* </pre>
*/
public class ExternalAccountAuthorizedUserCredentials extends GoogleCredentials
implements TrustBoundaryProvider {
implements RegionalAccessBoundaryProvider {

private static final String PARSE_ERROR_PREFIX = "Error parsing token refresh response. ";

Expand Down Expand Up @@ -214,28 +214,28 @@ public AccessToken refreshAccessToken() throws IOException {
this.refreshToken = refreshToken;
}

AccessToken newAccessToken =
AccessToken.newBuilder()
.setExpirationTime(expiresAtMilliseconds)
.setTokenValue(accessToken)
.build();

refreshTrustBoundary(newAccessToken, transportFactory);
return newAccessToken;
return AccessToken.newBuilder()
.setExpirationTime(expiresAtMilliseconds)
.setTokenValue(accessToken)
.build();
}

@InternalApi
@Override
public String getTrustBoundaryUrl() throws IOException {
public String getRegionalAccessBoundaryUrl() throws IOException {
Matcher matcher = WORKFORCE_AUDIENCE_PATTERN.matcher(getAudience());
if (!matcher.matches()) {
throw new IllegalStateException(
"The provided audience is not in the correct format for a workforce pool. "
+ "Refer: https://docs.cloud.google.com/iam/docs/principal-identifiers");
}
String poolId = matcher.group("pool");
return String.format(
IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_WORKFORCE_POOL, getUniverseDomain(), poolId);
return String.format(IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_WORKFORCE_POOL, poolId);
}

@Override
HttpTransportFactory getTransportFactory() {
return transportFactory;
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
* account impersonation.
*/
public abstract class ExternalAccountCredentials extends GoogleCredentials
implements TrustBoundaryProvider {
implements RegionalAccessBoundaryProvider {

private static final long serialVersionUID = 8049126194174465023L;

Expand Down Expand Up @@ -532,11 +532,7 @@ protected AccessToken exchangeExternalCredentialForAccessToken(
this.impersonatedCredentials = this.buildImpersonatedCredentials();
}
if (this.impersonatedCredentials != null) {
AccessToken accessToken = this.impersonatedCredentials.refreshAccessToken();
// After the impersonated credential refreshes, its trust boundary is
// also refreshed. That is the trust boundary we will use.
this.trustBoundary = this.impersonatedCredentials.getTrustBoundary();
return accessToken;
return this.impersonatedCredentials.refreshAccessToken();
}

StsRequestHandler.Builder requestHandler =
Expand Down Expand Up @@ -565,9 +561,7 @@ protected AccessToken exchangeExternalCredentialForAccessToken(
}

StsTokenExchangeResponse response = requestHandler.build().exchangeToken();
AccessToken accessToken = response.getAccessToken();
refreshTrustBoundary(accessToken, transportFactory);
return accessToken;
return response.getAccessToken();
}

/**
Expand All @@ -581,6 +575,11 @@ protected AccessToken exchangeExternalCredentialForAccessToken(
*/
public abstract String retrieveSubjectToken() throws IOException;

@Override
HttpTransportFactory getTransportFactory() {
return transportFactory;
}

public String getAudience() {
return audience;
}
Expand Down Expand Up @@ -626,14 +625,18 @@ public String getServiceAccountEmail() {

@InternalApi
@Override
public String getTrustBoundaryUrl() {
public String getRegionalAccessBoundaryUrl() throws IOException {
if (getServiceAccountEmail() != null) {
return String.format(
OAuth2Utils.IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_SERVICE_ACCOUNT,
getServiceAccountEmail());
}

Matcher workforceMatcher = WORKFORCE_AUDIENCE_PATTERN.matcher(getAudience());
if (workforceMatcher.matches()) {
String poolId = workforceMatcher.group("pool");
return String.format(
OAuth2Utils.IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_WORKFORCE_POOL,
getUniverseDomain(),
poolId);
OAuth2Utils.IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_WORKFORCE_POOL, poolId);
}

Matcher workloadMatcher = WORKLOAD_AUDIENCE_PATTERN.matcher(getAudience());
Expand All @@ -642,7 +645,6 @@ public String getTrustBoundaryUrl() {
String poolId = workloadMatcher.group("pool");
return String.format(
OAuth2Utils.IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_WORKLOAD_POOL,
getUniverseDomain(),
projectNumber,
poolId);
}
Expand Down
Loading