|
17 | 17 | package com.google.cloud.spanner.connection.it; |
18 | 18 |
|
19 | 19 | import static org.junit.Assert.*; |
20 | | -import static org.junit.Assume.assumeTrue; |
21 | 20 |
|
22 | 21 | import com.google.auth.oauth2.GoogleCredentials; |
23 | 22 | import com.google.auth.oauth2.ServiceAccountCredentials; |
24 | 23 | import com.google.cloud.spanner.*; |
25 | 24 | import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; |
26 | | -import com.google.cloud.spanner.connection.ITAbstractSpannerTest; |
27 | 25 | import com.google.cloud.spanner.connection.MutableCredentials; |
28 | 26 | import com.google.spanner.admin.database.v1.Database; |
29 | 27 | import com.google.spanner.admin.database.v1.InstanceName; |
30 | 28 | import java.io.IOException; |
31 | 29 | import java.io.InputStream; |
32 | | -import java.util.ArrayList; |
| 30 | +import java.util.Collections; |
33 | 31 | import java.util.List; |
34 | 32 | import org.junit.Test; |
35 | 33 | import org.junit.experimental.categories.Category; |
|
38 | 36 |
|
39 | 37 | @Category(SerialIntegrationTest.class) |
40 | 38 | @RunWith(JUnit4.class) |
41 | | -public class ITMutableCredentialsTest extends ITAbstractSpannerTest { |
42 | | - private static final String VALID_KEY_RESOURCE = |
43 | | - "/com/google/cloud/spanner/connection/test-key.json"; |
| 39 | +public class ITMutableCredentialsTest { |
| 40 | + private static final String MISSING_PERM_KEY = |
| 41 | + "/com/google/cloud/spanner/connection/test-key-missing-permissions.json"; |
44 | 42 |
|
45 | | - private static final String INVALID_KEY_RESOURCE = |
46 | | - "/com/google/cloud/spanner/connection/invalid-test-key.json"; |
| 43 | + private static final String INVALID_KEY = "/com/google/cloud/spanner/connection/test-key.json"; |
47 | 44 |
|
48 | 45 | @Test |
49 | 46 | public void testMutableCredentialsUpdateAuthorizationForRunningClient() throws IOException { |
50 | 47 |
|
51 | | - GoogleCredentials credentialsFromFile; |
| 48 | + GoogleCredentials missingPermissionCredentials; |
52 | 49 | try (InputStream stream = |
53 | | - ITMutableCredentialsTest.class.getResourceAsStream(VALID_KEY_RESOURCE)) { |
54 | | - assertNotNull("Missing test resource: " + VALID_KEY_RESOURCE, stream); |
55 | | - credentialsFromFile = GoogleCredentials.fromStream(stream); |
| 50 | + ITMutableCredentialsTest.class.getResourceAsStream(MISSING_PERM_KEY)) { |
| 51 | + missingPermissionCredentials = GoogleCredentials.fromStream(stream); |
56 | 52 | } |
57 | | - assumeTrue( |
58 | | - "This test requires service account credentials", |
59 | | - credentialsFromFile instanceof ServiceAccountCredentials); |
60 | | - |
61 | | - ServiceAccountCredentials validCredentials = (ServiceAccountCredentials) credentialsFromFile; |
62 | 53 | ServiceAccountCredentials invalidCredentials; |
63 | | - try (InputStream stream = |
64 | | - ITMutableCredentialsTest.class.getResourceAsStream(INVALID_KEY_RESOURCE)) { |
65 | | - assertNotNull("Missing test resource: " + INVALID_KEY_RESOURCE, stream); |
| 54 | + try (InputStream stream = ITMutableCredentialsTest.class.getResourceAsStream(INVALID_KEY)) { |
66 | 55 | invalidCredentials = ServiceAccountCredentials.fromStream(stream); |
67 | 56 | } |
68 | | - |
69 | | - List<String> scopes = new ArrayList<>(getTestEnv().getTestHelper().getOptions().getScopes()); |
70 | | - MutableCredentials mutableCredentials = new MutableCredentials(validCredentials, scopes); |
| 57 | + List<String> scopes = |
| 58 | + Collections.singletonList("https://www.googleapis.com/auth/cloud-platform"); |
| 59 | + // create MutableCredentials first with missing permissions |
| 60 | + MutableCredentials mutableCredentials = |
| 61 | + new MutableCredentials((ServiceAccountCredentials) missingPermissionCredentials, scopes); |
71 | 62 |
|
72 | 63 | SpannerOptions options = SpannerOptions.newBuilder().setCredentials(mutableCredentials).build(); |
73 | | - |
74 | 64 | try (Spanner spanner = options.getService(); |
75 | 65 | DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { |
76 | | - /* String dbName = |
77 | | - DatabaseName.of( |
78 | | - getTestEnv().getTestHelper().getInstanceId().getProject(), |
79 | | - getTestEnv().getTestHelper().getInstanceId().getInstance(), |
80 | | - "TEST") |
81 | | - .toString(); |
82 | | - Database database = databaseAdminClient.getDatabase(dbName);*/ |
83 | | - InstanceName instanceName = |
84 | | - InstanceName.of( |
85 | | - getTestEnv().getTestHelper().getInstanceId().getProject(), |
86 | | - getTestEnv().getTestHelper().getInstanceId().getInstance()); |
87 | | - DatabaseAdminClient.ListDatabasesPagedResponse response = |
88 | | - databaseAdminClient.listDatabases(instanceName); |
89 | | - |
90 | | - boolean databaseFound = false; |
91 | | - for (DatabaseAdminClient.ListDatabasesPage page : response.iteratePages()) { |
92 | | - for (Database database : page.iterateAll()) { |
93 | | - System.out.println("\t" + database.getName()); |
94 | | - databaseFound = true; |
95 | | - } |
| 66 | + String project = "gcloud-devel"; |
| 67 | + String instance = "java-client-integration-tests"; |
| 68 | + try { |
| 69 | + listDatabases(databaseAdminClient, project, instance); |
| 70 | + } catch (Exception e) { |
| 71 | + // specifically validate the permission denied error message |
| 72 | + assertTrue(e.getMessage().contains("PERMISSION_DENIED")); |
| 73 | + assertFalse(e.getMessage().contains("UNAUTHENTICATED")); |
96 | 74 | } |
97 | | - assertTrue(databaseFound); |
| 75 | + |
| 76 | + // update mutableCredentials now to use an invalid credential |
| 77 | + mutableCredentials.updateCredentials(invalidCredentials); |
98 | 78 | try { |
99 | | - mutableCredentials.updateCredentials(invalidCredentials); |
100 | | - DatabaseAdminClient.ListDatabasesPagedResponse responseFailure = |
101 | | - databaseAdminClient.listDatabases(instanceName); |
102 | | - for (DatabaseAdminClient.ListDatabasesPage page : responseFailure.iteratePages()) { |
103 | | - for (Database database : page.iterateAll()) { |
104 | | - System.out.println("\t" + database.getName()); |
105 | | - } |
106 | | - } |
| 79 | + listDatabases(databaseAdminClient, project, instance); |
107 | 80 | fail("Expected UNAUTHENTICATED after switching to invalid credentials"); |
108 | | - } catch (SpannerException e) { |
109 | | - assertEquals(ErrorCode.UNAUTHENTICATED, e.getErrorCode()); |
| 81 | + } catch (Exception e) { |
| 82 | + assertTrue(e.getMessage().contains("UNAUTHENTICATED")); |
| 83 | + assertFalse(e.getMessage().contains("PERMISSION_DENIED")); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private static void listDatabases( |
| 89 | + DatabaseAdminClient databaseAdminClient, String projectId, String instanceId) { |
| 90 | + DatabaseAdminClient.ListDatabasesPagedResponse response = |
| 91 | + databaseAdminClient.listDatabases(InstanceName.of(projectId, instanceId)); |
| 92 | + |
| 93 | + for (DatabaseAdminClient.ListDatabasesPage page : response.iteratePages()) { |
| 94 | + for (Database database : page.iterateAll()) { |
| 95 | + // no-op |
110 | 96 | } |
111 | | - } finally { |
112 | | - closeSpanner(); |
113 | 97 | } |
114 | 98 | } |
115 | 99 | } |
0 commit comments