Skip to content

Commit 2100d24

Browse files
authored
fix: throw exception on invalid IAM Authn config (#1082)
1 parent 59ddc04 commit 2100d24

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,16 @@ private SslData createSslData(
459459
}
460460
}
461461

462+
static void checkDatabaseCompatibility(ConnectSettings instanceMetadata, boolean iamAuth,
463+
String connectionName) {
464+
if (iamAuth && instanceMetadata.getDatabaseVersion().contains("SQLSERVER")) {
465+
throw new IllegalArgumentException(
466+
String.format(
467+
"[%s] IAM Authentication is not supported for SQL Server instances.",
468+
connectionName));
469+
}
470+
}
471+
462472
/**
463473
* Fetches the latest version of the instance's metadata using the Cloud SQL Admin API.
464474
*/
@@ -482,6 +492,9 @@ private Metadata fetchMetadata() {
482492
+ "instance.",
483493
connectionName));
484494
}
495+
496+
checkDatabaseCompatibility(instanceMetadata, enableIamAuth, connectionName);
497+
485498

486499
// Verify the instance has at least one IP type assigned that can be used to connect.
487500
if (instanceMetadata.getIpAddresses().isEmpty()) {

core/src/test/java/com/google/cloud/sql/core/CloudSqlInstanceTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.mockito.Mockito.verify;
2121
import static org.mockito.Mockito.when;
2222

23+
import com.google.api.services.sqladmin.model.ConnectSettings;
2324
import com.google.auth.oauth2.GoogleCredentials;
2425
import com.google.auth.oauth2.OAuth2Credentials;
2526
import java.io.IOException;
@@ -42,11 +43,15 @@ public class CloudSqlInstanceTest {
4243
@Mock
4344
private OAuth2Credentials oAuth2Credentials;
4445

46+
@Mock
47+
private ConnectSettings instanceData;
48+
4549
@Before
4650
public void setup() throws IOException {
4751
MockitoAnnotations.openMocks(this);
4852
when(googleCredentials.createScoped(
4953
"https://www.googleapis.com/auth/sqlservice.login")).thenReturn(scopedCredentials);
54+
when(instanceData.getDatabaseVersion()).thenReturn("SQLSERVER_2019_STANDARD");
5055
}
5156

5257
@Test
@@ -69,5 +74,19 @@ public void throwsErrorForWrongCredentialType() {
6974
}
7075
}
7176

77+
@Test
78+
public void throwsErrorIamAuthNotSupported() {
79+
Boolean enableIamAuth = true;
80+
String connName = "my-project:region:my-instance";
81+
82+
try {
83+
CloudSqlInstance.checkDatabaseCompatibility(instanceData, enableIamAuth, connName);
84+
} catch (IllegalArgumentException ex) {
85+
assertThat(ex)
86+
.hasMessageThat()
87+
.contains("[my-project:region:my-instance] " +
88+
"IAM Authentication is not supported for SQL Server instances");
89+
}
90+
}
7291

7392
}

0 commit comments

Comments
 (0)