File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed
main/java/com/google/cloud/sql/core
test/java/com/google/cloud/sql/core Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff 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 ()) {
Original file line number Diff line number Diff line change 2020import static org .mockito .Mockito .verify ;
2121import static org .mockito .Mockito .when ;
2222
23+ import com .google .api .services .sqladmin .model .ConnectSettings ;
2324import com .google .auth .oauth2 .GoogleCredentials ;
2425import com .google .auth .oauth2 .OAuth2Credentials ;
2526import 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}
You can’t perform that action at this time.
0 commit comments