|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.spanner; |
| 18 | + |
| 19 | +//[START mutable_credentials] |
| 20 | + |
| 21 | +import com.google.auth.oauth2.ServiceAccountCredentials; |
| 22 | +import com.google.cloud.spanner.Spanner; |
| 23 | +import com.google.cloud.spanner.SpannerOptions; |
| 24 | +import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; |
| 25 | +import com.google.cloud.spanner.connection.MutableCredentials; |
| 26 | +import com.google.spanner.admin.database.v1.DatabaseName; |
| 27 | +import com.google.spanner.admin.database.v1.GetDatabaseDdlResponse; |
| 28 | +import java.io.FileInputStream; |
| 29 | +import java.io.IOException; |
| 30 | +import java.nio.file.Files; |
| 31 | +import java.nio.file.Path; |
| 32 | +import java.nio.file.Paths; |
| 33 | +import java.nio.file.attribute.FileTime; |
| 34 | +import java.util.concurrent.Executors; |
| 35 | +import java.util.concurrent.ScheduledExecutorService; |
| 36 | +import java.util.concurrent.TimeUnit; |
| 37 | + |
| 38 | +public class MutableCredentialsExample { |
| 39 | + |
| 40 | + |
| 41 | + static void createClientWithMutableCredentials() throws IOException { |
| 42 | + final String credentialsPath = "location_of_service_account_credential_json"; |
| 43 | + Path path = Paths.get(credentialsPath); |
| 44 | + // Use an array to hold the mutable lastModifiedTime so it can be accessed in the lambda |
| 45 | + FileTime[] lastModifiedTime = new FileTime[]{ Files.getLastModifiedTime(path) }; |
| 46 | + |
| 47 | + // 1 - create service account credentials |
| 48 | + ServiceAccountCredentials serviceAccountCredentials; |
| 49 | + try (FileInputStream is = new FileInputStream(credentialsPath)) { |
| 50 | + serviceAccountCredentials = ServiceAccountCredentials.fromStream(is); |
| 51 | + } |
| 52 | + |
| 53 | + // 2 - wrap credentials from step 1 in a MutableCredentials instance |
| 54 | + MutableCredentials mutableCredentials = new MutableCredentials(serviceAccountCredentials); |
| 55 | + |
| 56 | + // 3 - set credentials on your SpannerOptions builder to your mutableCredentials |
| 57 | + SpannerOptions options = SpannerOptions.newBuilder() |
| 58 | + .setCredentials(mutableCredentials) |
| 59 | + .build(); |
| 60 | + |
| 61 | + // 4 - include logic for when to how your mutableCredentials |
| 62 | + // In this example we'll use a SchedulerExecutorService to periodically check for updates |
| 63 | + ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); |
| 64 | + executorService.scheduleAtFixedRate(() -> { |
| 65 | + try { |
| 66 | + FileTime currentModifiedTime = Files.getLastModifiedTime(path); |
| 67 | + if (currentModifiedTime.compareTo(lastModifiedTime[0]) > 0) { |
| 68 | + lastModifiedTime[0] = currentModifiedTime; |
| 69 | + try (FileInputStream is = new FileInputStream(credentialsPath)) { |
| 70 | + ServiceAccountCredentials credentials = ServiceAccountCredentials.fromStream(is); |
| 71 | + mutableCredentials.updateCredentials(credentials); |
| 72 | + System.out.println("Credentials rotated."); |
| 73 | + } |
| 74 | + } |
| 75 | + } catch (IOException e) { |
| 76 | + System.err.println("Failed to check or update credentials: " + e.getMessage()); |
| 77 | + } |
| 78 | + }, 15, 15, TimeUnit.MINUTES); |
| 79 | + |
| 80 | + // 5. Use the client |
| 81 | + try (Spanner spanner = options.getService(); |
| 82 | + DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) { |
| 83 | + // Perform operations... |
| 84 | + // long running client operations will always use the latest credentials wrapped in |
| 85 | + // mutableCredentials |
| 86 | + } finally { |
| 87 | + // Ensure the executor is shut down when the application exits or the client is closed |
| 88 | + executorService.shutdown(); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | +//[END mutable_credentials] |
0 commit comments