Skip to content

Commit 2c5d58c

Browse files
authored
chore: format CloudSqlInstance.java (#1148)
1 parent 6dbe219 commit 2c5d58c

File tree

1 file changed

+58
-61
lines changed

1 file changed

+58
-61
lines changed

core/src/main/java/com/google/cloud/sql/core/CloudSqlInstance.java

Lines changed: 58 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,18 @@ class CloudSqlInstance {
112112
private final String regionalizedInstanceId;
113113
private final ListenableFuture<KeyPair> keyPair;
114114
private final Object instanceDataGuard = new Object();
115-
115+
// Limit forced refreshes to 1 every minute.
116+
private final RateLimiter forcedRenewRateLimiter = RateLimiter.create(1.0 / 60.0);
116117
@GuardedBy("instanceDataGuard")
117118
private ListenableFuture<InstanceData> currentInstanceData;
118-
119119
@GuardedBy("instanceDataGuard")
120120
private ListenableFuture<ListenableFuture<InstanceData>> nextInstanceData;
121121

122-
// Limit forced refreshes to 1 every minute.
123-
private final RateLimiter forcedRenewRateLimiter = RateLimiter.create(1.0 / 60.0);
124-
125122
/**
126123
* Initializes a new Cloud SQL instance based on the given connection name.
127124
*
128-
* @param connectionName instance connection name in the format "PROJECT_ID:REGION_ID:INSTANCE_ID"
125+
* @param connectionName instance connection name in the format
126+
* "PROJECT_ID:REGION_ID:INSTANCE_ID"
129127
* @param apiClient Cloud SQL Admin API client for interacting with the Cloud SQL instance
130128
* @param executor executor used to schedule asynchronous tasks
131129
* @param keyPair public/private key pair used to authenticate connections
@@ -172,37 +170,6 @@ class CloudSqlInstance {
172170
}
173171
}
174172

175-
private OAuth2Credentials parseCredentials(HttpRequestInitializer source) {
176-
if (source instanceof HttpCredentialsAdapter) {
177-
HttpCredentialsAdapter adapter = (HttpCredentialsAdapter) source;
178-
return (OAuth2Credentials) adapter.getCredentials();
179-
}
180-
181-
if (source instanceof Credential) {
182-
Credential credential = (Credential) source;
183-
AccessToken accessToken = new AccessToken(
184-
credential.getAccessToken(),
185-
getTokenExpirationTime(credential).orElse(null)
186-
);
187-
GoogleCredentials googleCredentials = new GoogleCredentials(accessToken) {
188-
189-
@Override
190-
public AccessToken refreshAccessToken() throws IOException {
191-
credential.refreshToken();
192-
193-
return new AccessToken(
194-
credential.getAccessToken(),
195-
getTokenExpirationTime(credential).orElse(null)
196-
);
197-
}
198-
};
199-
200-
return googleCredentials;
201-
}
202-
203-
throw new RuntimeException("Not supporting credentials of type " + source.getClass().getName());
204-
}
205-
206173
/**
207174
* Generates public key certificate for which the instance has the matching private key.
208175
*
@@ -282,6 +249,60 @@ private static Certificate createCertificate(String cert) throws CertificateExce
282249
return CertificateFactory.getInstance("X.509").generateCertificate(certStream);
283250
}
284251

252+
static void checkDatabaseCompatibility(ConnectSettings instanceMetadata, boolean iamAuth,
253+
String connectionName) {
254+
if (iamAuth && instanceMetadata.getDatabaseVersion().contains("SQLSERVER")) {
255+
throw new IllegalArgumentException(
256+
String.format(
257+
"[%s] IAM Authentication is not supported for SQL Server instances.",
258+
connectionName));
259+
}
260+
}
261+
262+
static GoogleCredentials getDownscopedCredentials(OAuth2Credentials credentials) {
263+
GoogleCredentials downscoped;
264+
try {
265+
GoogleCredentials oldCredentials = (GoogleCredentials) credentials;
266+
downscoped = oldCredentials.createScoped(SQL_LOGIN_SCOPE);
267+
} catch (ClassCastException ex) {
268+
throw new RuntimeException(
269+
"Failed to downscope credentials for IAM Authentication:",
270+
ex);
271+
}
272+
return downscoped;
273+
}
274+
275+
private OAuth2Credentials parseCredentials(HttpRequestInitializer source) {
276+
if (source instanceof HttpCredentialsAdapter) {
277+
HttpCredentialsAdapter adapter = (HttpCredentialsAdapter) source;
278+
return (OAuth2Credentials) adapter.getCredentials();
279+
}
280+
281+
if (source instanceof Credential) {
282+
Credential credential = (Credential) source;
283+
AccessToken accessToken = new AccessToken(
284+
credential.getAccessToken(),
285+
getTokenExpirationTime(credential).orElse(null)
286+
);
287+
GoogleCredentials googleCredentials = new GoogleCredentials(accessToken) {
288+
289+
@Override
290+
public AccessToken refreshAccessToken() throws IOException {
291+
credential.refreshToken();
292+
293+
return new AccessToken(
294+
credential.getAccessToken(),
295+
getTokenExpirationTime(credential).orElse(null)
296+
);
297+
}
298+
};
299+
300+
return googleCredentials;
301+
}
302+
303+
throw new RuntimeException("Not supporting credentials of type " + source.getClass().getName());
304+
}
305+
285306
/**
286307
* Returns the current data related to the instance from {@link #performRefresh()}. May block if
287308
* no valid data is currently available.
@@ -492,16 +513,6 @@ private SslData createSslData(
492513
}
493514
}
494515

495-
static void checkDatabaseCompatibility(ConnectSettings instanceMetadata, boolean iamAuth,
496-
String connectionName) {
497-
if (iamAuth && instanceMetadata.getDatabaseVersion().contains("SQLSERVER")) {
498-
throw new IllegalArgumentException(
499-
String.format(
500-
"[%s] IAM Authentication is not supported for SQL Server instances.",
501-
connectionName));
502-
}
503-
}
504-
505516
/**
506517
* Fetches the latest version of the instance's metadata using the Cloud SQL Admin API.
507518
*/
@@ -528,7 +539,6 @@ private Metadata fetchMetadata() {
528539

529540
checkDatabaseCompatibility(instanceMetadata, enableIamAuth, connectionName);
530541

531-
532542
// Verify the instance has at least one IP type assigned that can be used to connect.
533543
if (instanceMetadata.getIpAddresses().isEmpty()) {
534544
throw new IllegalStateException(
@@ -613,19 +623,6 @@ private Certificate fetchEphemeralCertificate(KeyPair keyPair) {
613623
return ephemeralCertificate;
614624
}
615625

616-
static GoogleCredentials getDownscopedCredentials(OAuth2Credentials credentials) {
617-
GoogleCredentials downscoped;
618-
try {
619-
GoogleCredentials oldCredentials = (GoogleCredentials) credentials;
620-
downscoped = oldCredentials.createScoped(SQL_LOGIN_SCOPE);
621-
} catch (ClassCastException ex) {
622-
throw new RuntimeException(
623-
"Failed to downscope credentials for IAM Authentication:",
624-
ex);
625-
}
626-
return downscoped;
627-
}
628-
629626
private Optional<Date> getTokenExpirationTime(OAuth2Credentials credentials) {
630627
return Optional.ofNullable(credentials.getAccessToken().getExpirationTime());
631628
}

0 commit comments

Comments
 (0)