Skip to content

Commit

Permalink
test: add more tests for quotaProjectId
Browse files Browse the repository at this point in the history
  • Loading branch information
chingor13 committed Nov 30, 2020
1 parent 8f6b1fc commit eaa8ee5
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 6 deletions.
Expand Up @@ -76,7 +76,6 @@
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -601,8 +600,11 @@ public JwtCredentials jwtWithClaims(JwtClaims newClaims) {

@Override
protected Map<String, List<String>> getAdditionalHeaders() {
return Collections.singletonMap(
QUOTA_PROJECT_ID_HEADER_KEY, Collections.singletonList(quotaProjectId));
Map<String, List<String>> headers = super.getAdditionalHeaders();
if (quotaProjectId != null) {
return addQuotaProjectIdToRequestMetadata(quotaProjectId, headers);
}
return headers;
}

@Override
Expand Down
8 changes: 5 additions & 3 deletions oauth2_http/java/com/google/auth/oauth2/UserCredentials.java
Expand Up @@ -52,7 +52,6 @@
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.URI;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -279,8 +278,11 @@ public void save(String filePath) throws IOException {

@Override
protected Map<String, List<String>> getAdditionalHeaders() {
return Collections.singletonMap(
QUOTA_PROJECT_ID_HEADER_KEY, Collections.singletonList(quotaProjectId));
Map<String, List<String>> headers = super.getAdditionalHeaders();
if (quotaProjectId != null) {
return addQuotaProjectIdToRequestMetadata(quotaProjectId, headers);
}
return headers;
}

@Override
Expand Down
Expand Up @@ -49,6 +49,7 @@
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
import com.google.api.client.util.Clock;
import com.google.api.client.util.Joiner;
import com.google.auth.RequestMetadataCallback;
import com.google.auth.TestUtils;
import com.google.auth.http.HttpTransportFactory;
import com.google.auth.oauth2.GoogleCredentialsTest.MockHttpTransportFactory;
Expand All @@ -68,6 +69,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -1032,6 +1034,97 @@ public void fromStream_noPrivateKeyId_throws() throws IOException {
testFromStreamException(serviceAccountStream, "private_key_id");
}

@Test
public void getRequestMetadataSetsQuotaProjectId() throws IOException {
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
transportFactory.transport.addClient(CLIENT_ID, "unused-client-secret");
transportFactory.transport.addServiceAccount(CLIENT_EMAIL, ACCESS_TOKEN);

PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(PRIVATE_KEY_PKCS8);
GoogleCredentials credentials =
ServiceAccountCredentials.newBuilder()
.setClientId(CLIENT_ID)
.setClientEmail(CLIENT_EMAIL)
.setPrivateKey(privateKey)
.setPrivateKeyId(PRIVATE_KEY_ID)
.setScopes(SCOPES)
.setServiceAccountUser(USER)
.setProjectId(PROJECT_ID)
.setQuotaProjectId("my-quota-project-id")
.setHttpTransportFactory(transportFactory)
.build();

Map<String, List<String>> metadata = credentials.getRequestMetadata();
assertTrue(metadata.containsKey("x-goog-user-project"));
List<String> headerValues = metadata.get("x-goog-user-project");
assertEquals(1, headerValues.size());
assertEquals("my-quota-project-id", headerValues.get(0));
}

@Test
public void getRequestMetadataNoQuotaProjectId() throws IOException {
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
transportFactory.transport.addClient(CLIENT_ID, "unused-client-secret");
transportFactory.transport.addServiceAccount(CLIENT_EMAIL, ACCESS_TOKEN);

PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(PRIVATE_KEY_PKCS8);
GoogleCredentials credentials =
ServiceAccountCredentials.newBuilder()
.setClientId(CLIENT_ID)
.setClientEmail(CLIENT_EMAIL)
.setPrivateKey(privateKey)
.setPrivateKeyId(PRIVATE_KEY_ID)
.setScopes(SCOPES)
.setServiceAccountUser(USER)
.setProjectId(PROJECT_ID)
.setHttpTransportFactory(transportFactory)
.build();

Map<String, List<String>> metadata = credentials.getRequestMetadata();
assertFalse(metadata.containsKey("x-goog-user-project"));
}

@Test
public void getRequestMetadataWithCallback() throws IOException {
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
transportFactory.transport.addClient(CLIENT_ID, "unused-client-secret");
transportFactory.transport.addServiceAccount(CLIENT_EMAIL, ACCESS_TOKEN);

PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(PRIVATE_KEY_PKCS8);
GoogleCredentials credentials =
ServiceAccountCredentials.newBuilder()
.setClientId(CLIENT_ID)
.setClientEmail(CLIENT_EMAIL)
.setPrivateKey(privateKey)
.setPrivateKeyId(PRIVATE_KEY_ID)
.setScopes(SCOPES)
.setServiceAccountUser(USER)
.setProjectId(PROJECT_ID)
.setQuotaProjectId("my-quota-project-id")
.setHttpTransportFactory(transportFactory)
.build();

final Map<String, List<String>> plainMetadata = credentials.getRequestMetadata();
final AtomicBoolean success = new AtomicBoolean(false);
credentials.getRequestMetadata(
null,
null,
new RequestMetadataCallback() {
@Override
public void onSuccess(Map<String, List<String>> metadata) {
assertEquals(plainMetadata, metadata);
success.set(true);
}

@Override
public void onFailure(Throwable exception) {
fail("Should not throw a failure.");
}
});

assertTrue("Should have run onSuccess() callback", success.get());
}

static GenericJson writeServiceAccountJson(
String clientId,
String clientEmail,
Expand Down
Expand Up @@ -46,6 +46,7 @@
import com.google.api.client.json.webtoken.JsonWebSignature;
import com.google.api.client.util.Clock;
import com.google.auth.Credentials;
import com.google.auth.RequestMetadataCallback;
import com.google.auth.TestClock;
import com.google.auth.http.AuthHttpConstants;
import com.google.auth.oauth2.GoogleCredentialsTest.MockHttpTransportFactory;
Expand All @@ -61,6 +62,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -758,6 +760,76 @@ public void jwtWithClaims_defaultAudience() throws IOException {
verifyJwtAccess(metadata, SA_CLIENT_EMAIL, URI.create("default-audience"), SA_PRIVATE_KEY_ID);
}

@Test
public void getRequestMetadataSetsQuotaProjectId() throws IOException {
PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8);
ServiceAccountJwtAccessCredentials credentials =
ServiceAccountJwtAccessCredentials.newBuilder()
.setClientId(SA_CLIENT_ID)
.setClientEmail(SA_CLIENT_EMAIL)
.setPrivateKey(privateKey)
.setPrivateKeyId(SA_PRIVATE_KEY_ID)
.setQuotaProjectId("my-quota-project-id")
.setDefaultAudience(URI.create("default-audience"))
.build();

Map<String, List<String>> metadata = credentials.getRequestMetadata();
assertTrue(metadata.containsKey("x-goog-user-project"));
List<String> headerValues = metadata.get("x-goog-user-project");
assertEquals(1, headerValues.size());
assertEquals("my-quota-project-id", headerValues.get(0));
}

@Test
public void getRequestMetadataNoQuotaProjectId() throws IOException {
PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8);
ServiceAccountJwtAccessCredentials credentials =
ServiceAccountJwtAccessCredentials.newBuilder()
.setClientId(SA_CLIENT_ID)
.setClientEmail(SA_CLIENT_EMAIL)
.setPrivateKey(privateKey)
.setPrivateKeyId(SA_PRIVATE_KEY_ID)
.setDefaultAudience(URI.create("default-audience"))
.build();

Map<String, List<String>> metadata = credentials.getRequestMetadata();
assertFalse(metadata.containsKey("x-goog-user-project"));
}

@Test
public void getRequestMetadataWithCallback() throws IOException {
PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8);
ServiceAccountJwtAccessCredentials credentials =
ServiceAccountJwtAccessCredentials.newBuilder()
.setClientId(SA_CLIENT_ID)
.setClientEmail(SA_CLIENT_EMAIL)
.setPrivateKey(privateKey)
.setPrivateKeyId(SA_PRIVATE_KEY_ID)
.setQuotaProjectId("my-quota-project-id")
.setDefaultAudience(URI.create("default-audience"))
.build();

final Map<String, List<String>> plainMetadata = credentials.getRequestMetadata();
final AtomicBoolean success = new AtomicBoolean(false);
credentials.getRequestMetadata(
null,
null,
new RequestMetadataCallback() {
@Override
public void onSuccess(Map<String, List<String>> metadata) {
assertEquals(plainMetadata, metadata);
success.set(true);
}

@Override
public void onFailure(Throwable exception) {
fail("Should not throw a failure.");
}
});

assertTrue("Should have run onSuccess() callback", success.get());
}

private void verifyJwtAccess(
Map<String, List<String>> metadata,
String expectedEmail,
Expand Down
Expand Up @@ -624,6 +624,46 @@ public void saveAndRestoreUserCredential_saveAndRestored_throws() throws IOExcep
assertEquals(userCredentials.getRefreshToken(), restoredCredentials.getRefreshToken());
}

@Test
public void getRequestMetadataSetsQuotaProjectId() throws IOException {
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET);
transportFactory.transport.addRefreshToken(REFRESH_TOKEN, ACCESS_TOKEN);

UserCredentials userCredentials =
UserCredentials.newBuilder()
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
.setRefreshToken(REFRESH_TOKEN)
.setQuotaProjectId("my-quota-project-id")
.setHttpTransportFactory(transportFactory)
.build();

Map<String, List<String>> metadata = userCredentials.getRequestMetadata();
assertTrue(metadata.containsKey("x-goog-user-project"));
List<String> headerValues = metadata.get("x-goog-user-project");
assertEquals(1, headerValues.size());
assertEquals("my-quota-project-id", headerValues.get(0));
}

@Test
public void getRequestMetadataNoQuotaProjectId() throws IOException {
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET);
transportFactory.transport.addRefreshToken(REFRESH_TOKEN, ACCESS_TOKEN);

UserCredentials userCredentials =
UserCredentials.newBuilder()
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
.setRefreshToken(REFRESH_TOKEN)
.setHttpTransportFactory(transportFactory)
.build();

Map<String, List<String>> metadata = userCredentials.getRequestMetadata();
assertFalse(metadata.containsKey("x-goog-user-project"));
}

@Test
public void getRequestMetadataWithCallback() throws IOException {
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
Expand Down

0 comments on commit eaa8ee5

Please sign in to comment.