|
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; |
20 | 21 |
|
21 | | -import com.google.api.gax.core.FixedCredentialsProvider; |
22 | 22 | import com.google.auth.oauth2.GoogleCredentials; |
23 | 23 | import com.google.auth.oauth2.ServiceAccountCredentials; |
24 | 24 | import com.google.cloud.spanner.*; |
|
37 | 37 | @Category(SerialIntegrationTest.class) |
38 | 38 | @RunWith(JUnit4.class) |
39 | 39 | public class ITMutableCredentialsTest { |
40 | | - private static final String MISSING_PERM_KEY = |
41 | | - "/com/google/cloud/spanner/connection/test-key-missing-permissions.json"; |
42 | 40 |
|
43 | | - private static final String INVALID_KEY = "/com/google/cloud/spanner/connection/test-key.json"; |
| 41 | + private static final String INVALID_CERT_PATH = |
| 42 | + "/com/google/cloud/spanner/connection/test-key.json"; |
44 | 43 |
|
45 | 44 | @Test |
46 | 45 | public void testMutableCredentialsUpdateAuthorizationForRunningClient() throws IOException { |
47 | | - System.out.println("property" + System.getenv("GOOGLE_ACCOUNT_CREDENTIALS")); |
48 | | - GoogleCredentials missingPermissionCredentials; |
49 | | - try (InputStream stream = |
50 | | - Files.newInputStream( |
51 | | - Paths.get("/tmpfs/src/gfile/secret_manager/java-it-service-account"))) { |
52 | | - missingPermissionCredentials = GoogleCredentials.fromStream(stream); |
| 46 | + GoogleCredentials validCredentials; |
| 47 | + |
| 48 | + // accept cert path overridden by environment variable for local testing |
| 49 | + if (System.getenv("GOOGLE_ACCOUNT_CREDENTIALS") != null) { |
| 50 | + try (InputStream stream = |
| 51 | + Files.newInputStream(Paths.get(System.getenv("GOOGLE_ACCOUNT_CREDENTIALS")))) { |
| 52 | + validCredentials = GoogleCredentials.fromStream(stream); |
| 53 | + } |
| 54 | + } else { |
| 55 | + validCredentials = GoogleCredentials.getApplicationDefault(); |
53 | 56 | } |
| 57 | + |
| 58 | + // credentials must be ServiceAccountCredentials |
| 59 | + assumeTrue(validCredentials instanceof ServiceAccountCredentials); |
| 60 | + |
54 | 61 | ServiceAccountCredentials invalidCredentials; |
55 | | - try (InputStream stream = ITMutableCredentialsTest.class.getResourceAsStream(INVALID_KEY)) { |
| 62 | + try (InputStream stream = |
| 63 | + ITMutableCredentialsTest.class.getResourceAsStream(INVALID_CERT_PATH)) { |
56 | 64 | invalidCredentials = ServiceAccountCredentials.fromStream(stream); |
57 | 65 | } |
58 | 66 |
|
59 | | - // create MutableCredentials first default account credentials |
| 67 | + // create MutableCredentials first with valid credentials |
60 | 68 | MutableCredentials mutableCredentials = |
61 | | - new MutableCredentials((ServiceAccountCredentials) missingPermissionCredentials); |
| 69 | + new MutableCredentials((ServiceAccountCredentials) validCredentials); |
62 | 70 |
|
63 | | - System.out.println("missingPermissionCredentials " + missingPermissionCredentials); |
| 71 | + System.out.println("validCredentials " + validCredentials); |
64 | 72 |
|
65 | | - System.out.println("application default " + GoogleCredentials.getApplicationDefault()); |
66 | 73 | SpannerOptions options = |
67 | 74 | SpannerOptions.newBuilder() |
68 | | - .setEmulatorHost(null) |
69 | | - .setCredentials(FixedCredentialsProvider.create(mutableCredentials).getCredentials()) |
| 75 | + .setEmulatorHost( |
| 76 | + null) // this setting is required otherwise SpannerOptions overrides credentials to |
| 77 | + // NoCredentials |
| 78 | + .setCredentials(mutableCredentials) |
70 | 79 | .build(); |
71 | 80 | System.out.println("initial credentials " + options.getCredentials()); |
72 | | - System.out.println("default projecct" + options.getProjectId()); |
| 81 | + ProjectName projectName = ProjectName.of(options.getProjectId()); |
73 | 82 | try (Spanner spanner = options.getService(); |
74 | 83 | InstanceAdminClient instanceAdminClient = spanner.createInstanceAdminClient()) { |
75 | | - String project = "gcloud-devel"; |
76 | | - String instance = "java-client-integration-tests"; |
77 | | - try { |
78 | | - listInstances(instanceAdminClient, options.getProjectId(), instance); |
79 | | - // fail("Expected PERMISSION_DENIED"); |
80 | | - } catch (Exception e) { |
81 | | - // specifically validate the permission denied error message |
82 | | - System.out.println("exception " + e.getMessage()); |
83 | | - assertTrue(e.getMessage().contains("PERMISSION_DENIED")); |
84 | | - assertFalse(e.getMessage().contains("UNAUTHENTICATED")); |
85 | | - } |
86 | | - |
87 | | - // update mutableCredentials now to use an invalid credential |
| 84 | + instanceAdminClient.listInstances(projectName); |
| 85 | + // update mutableCredentials now to use an invalid credentials |
88 | 86 | mutableCredentials.updateCredentials(invalidCredentials); |
89 | 87 | try { |
90 | | - listInstances(instanceAdminClient, options.getProjectId(), instance); |
| 88 | + instanceAdminClient.listInstances(projectName); |
91 | 89 | fail("Expected UNAUTHENTICATED after switching to invalid credentials"); |
92 | 90 | } catch (Exception e) { |
93 | 91 | assertTrue(e.getMessage().contains("UNAUTHENTICATED")); |
94 | | - assertFalse(e.getMessage().contains("PERMISSION_DENIED")); |
95 | 92 | } |
96 | 93 | } |
97 | 94 | } |
98 | | - |
99 | | - private static void listInstances( |
100 | | - InstanceAdminClient instanceAdminClient, String projectId, String instanceId) { |
101 | | - InstanceAdminClient.ListInstancesPagedResponse response = |
102 | | - instanceAdminClient.listInstances(ProjectName.of(projectId)); |
103 | | - |
104 | | - for (InstanceAdminClient.ListInstancesPage page : response.iteratePages()) { |
105 | | - // no-op |
106 | | - } |
107 | | - } |
108 | 95 | } |
0 commit comments