Compare commits

...

22 Commits

Author SHA1 Message Date
Disha Prakash
1b85cbfd41 minor fix 2025-12-02 10:41:54 +00:00
Disha Prakash
5f64b09f55 feat(source/cloudsqlmssql): add support for IAM authentication in Cloud SQL MSSQL source 2025-12-02 10:29:17 +00:00
Disha Prakash
3775bb7d81 Fix tool tests 2025-12-02 09:47:21 +00:00
Disha Prakash
ca3f77ac52 Fix tool tests 2025-12-02 09:28:50 +00:00
Disha Prakash
9bc16b434f fix dsn 2025-12-02 08:21:06 +00:00
Disha Prakash
be35382dec update dsn 2025-12-02 08:05:45 +00:00
Disha Prakash
1720ea14d8 update dsn 2025-12-02 07:57:58 +00:00
Disha Prakash
b3658af105 create 2 dsn's 2025-12-02 07:46:25 +00:00
dishaprakash
a8a67cd544 Merge branch 'main' into add-iam-mysql 2025-12-02 07:02:31 +00:00
Disha Prakash
0a39dbbac4 add a empty string validation for username 2025-12-02 07:00:59 +00:00
Disha Prakash
55a3f716e8 test changes 2025-11-29 16:22:10 +00:00
Disha Prakash
0827b5c715 test changes 2025-11-29 16:21:45 +00:00
dishaprakash
11d3299854 Merge branch 'main' into add-iam-mysql 2025-11-29 16:20:33 +00:00
dishaprakash
7126c776df Merge branch 'main' into add-iam-mysql 2025-11-27 14:46:57 +00:00
Disha Prakash
db7cd4621f minor comment change 2025-11-27 14:41:38 +00:00
Disha Prakash
25763e4c11 review changes 2025-11-27 14:37:33 +00:00
Disha Prakash
15a5d2a558 add documentation 2025-11-26 22:50:54 +00:00
Disha Prakash
40882e60ac add consistent error message 2025-11-26 22:29:29 +00:00
Disha Prakash
8992a05611 disable cloud healthcare tests 2025-11-26 22:07:04 +00:00
Disha Prakash
7eb1650cf3 disable dataplex tests 2025-11-26 21:59:34 +00:00
dishaprakash
62755fd4e1 Merge branch 'main' into add-iam-mysql 2025-11-26 21:47:44 +00:00
Disha Prakash
966f5f1be3 feat(source/cloudsqlmysql): add support for IAM authentication in Cloud SQL MySQL source 2025-11-26 21:46:34 +00:00
9 changed files with 294 additions and 18 deletions

View File

@@ -88,13 +88,40 @@ mTLS.
[public-ip]: https://cloud.google.com/sql/docs/mysql/configure-ip
[conn-overview]: https://cloud.google.com/sql/docs/mysql/connect-overview
### Database User
### Authentication
Currently, this source only uses standard authentication. You will need to [create
a MySQL user][cloud-sql-users] to login to the database with.
This source supports both password-based authentication and IAM
authentication (using your [Application Default Credentials][adc]).
#### Standard Authentication
To connect using user/password, [create
a MySQL user][cloud-sql-users] and input your credentials in the `user` and
`password` fields.
```yaml
user: ${USER_NAME}
password: ${PASSWORD}
```
[cloud-sql-users]: https://cloud.google.com/sql/docs/mysql/create-manage-users
#### IAM Authentication
To connect using IAM authentication:
1. Prepare your database instance and user following this [guide][iam-guide].
2. You could choose one of the two ways to log in:
- Specify your IAM email as the `user`.
- Leave your `user` field blank. Toolbox will fetch the [ADC][adc]
automatically and log in using the email associated with it.
3. Leave the `password` field blank.
[iam-guide]: https://cloud.google.com/sql/docs/mysql/iam-logins
[cloudsql-users]: https://cloud.google.com/sql/docs/mysql/create-manage-users
## Example
```yaml
@@ -124,6 +151,6 @@ instead of hardcoding your secrets into the configuration file.
| region | string | true | Name of the GCP region that the cluster was created in (e.g. "us-central1"). |
| instance | string | true | Name of the Cloud SQL instance within the cluster (e.g. "my-instance"). |
| database | string | true | Name of the MySQL database to connect to (e.g. "my_db"). |
| user | string | true | Name of the MySQL user to connect as (e.g. "my-pg-user"). |
| password | string | true | Password of the MySQL user (e.g. "my-password"). |
| user | string | false | Name of the MySQL user to connect as (e.g "my-mysql-user"). Defaults to IAM auth using [ADC][adc] email if unspecified. |
| password | string | false | Password of the MySQL user (e.g. "my-password"). Defaults to attempting IAM authentication if unspecified. |
| ipType | string | false | IP Type of the Cloud SQL instance, must be either `public`, `private`, or `psc`. Default: `public`. |

View File

@@ -141,7 +141,7 @@ func getConnectionConfig(ctx context.Context, user, pass, dbname string) (string
// If password is provided without an username, raise an error
return "", useIAM, fmt.Errorf("password is provided without a username. Please provide both a username and password, or leave both fields empty")
}
email, err := sources.GetIAMPrincipalEmailFromADC(ctx)
email, err := sources.GetIAMPrincipalEmailFromADC(ctx, "postgres")
if err != nil {
return "", useIAM, fmt.Errorf("error getting email from ADC: %v", err)
}

View File

@@ -56,8 +56,8 @@ type Config struct {
Instance string `yaml:"instance" validate:"required"`
IPAddress string `yaml:"ipAddress"` // Deprecated: kept for backwards compatibility
IPType sources.IPType `yaml:"ipType" validate:"required"`
User string `yaml:"user" validate:"required"`
Password string `yaml:"password" validate:"required"`
User string `yaml:"user"`
Password string `yaml:"password"`
Database string `yaml:"database" validate:"required"`
}
@@ -107,6 +107,32 @@ func (s *Source) MSSQLDB() *sql.DB {
return s.Db
}
func getConnectionConfig(ctx context.Context, user, pass string) (string, string, bool, error) {
useIAM := true
// If username and password both provided, use password authentication
if user != "" && pass != "" {
useIAM = false
return user, pass, useIAM, nil
}
// If username is empty, fetch email from ADC
// otherwise, use username as IAM email
if user == "" {
if pass != "" {
return "", "", useIAM, fmt.Errorf("password is provided without a username. Please provide both a username and password, or leave both fields empty")
}
email, err := sources.GetIAMPrincipalEmailFromADC(ctx, "mssql")
if err != nil {
return "", "", useIAM, fmt.Errorf("error getting email from ADC: %v", err)
}
user = email
}
// Pass the user, empty password and useIAM set to true
return user, pass, useIAM, nil
}
func initCloudSQLMssqlConnection(ctx context.Context, tracer trace.Tracer, name, project, region, instance, ipType, user, pass, dbname string) (*sql.DB, error) {
//nolint:all // Reassigned ctx
ctx, span := sources.InitConnectionSpan(ctx, tracer, SourceKind, name)
@@ -117,6 +143,10 @@ func initCloudSQLMssqlConnection(ctx context.Context, tracer trace.Tracer, name,
return nil, err
}
user, pass, useIAM, err := getConnectionConfig(ctx, user, pass)
if err != nil {
return nil, fmt.Errorf("unable to get Cloud SQL connection config: %w", err)
}
// Create dsn
query := url.Values{}
query.Add("app name", userAgent)
@@ -130,7 +160,7 @@ func initCloudSQLMssqlConnection(ctx context.Context, tracer trace.Tracer, name,
}
// Get dial options
opts, err := sources.GetCloudSQLOpts(ipType, userAgent, false)
opts, err := sources.GetCloudSQLOpts(ipType, userAgent, useIAM)
if err != nil {
return nil, err
}

View File

@@ -54,8 +54,8 @@ type Config struct {
Region string `yaml:"region" validate:"required"`
Instance string `yaml:"instance" validate:"required"`
IPType sources.IPType `yaml:"ipType"`
User string `yaml:"user" validate:"required"`
Password string `yaml:"password" validate:"required"`
User string `yaml:"user"`
Password string `yaml:"password"`
Database string `yaml:"database" validate:"required"`
}
@@ -100,17 +100,49 @@ func (s *Source) MySQLPool() *sql.DB {
return s.Pool
}
func getConnectionConfig(ctx context.Context, user, pass string) (string, string, bool, error) {
useIAM := true
// If username and password both provided, use password authentication
if user != "" && pass != "" {
useIAM = false
return user, pass, useIAM, nil
}
// If username is empty, fetch email from ADC
// otherwise, use username as IAM email
if user == "" {
if pass != "" {
return "", "", useIAM, fmt.Errorf("password is provided without a username. Please provide both a username and password, or leave both fields empty")
}
email, err := sources.GetIAMPrincipalEmailFromADC(ctx, "mysql")
if err != nil {
return "", "", useIAM, fmt.Errorf("error getting email from ADC: %v", err)
}
user = email
}
// Pass the user, empty password and useIAM set to true
return user, pass, useIAM, nil
}
func initCloudSQLMySQLConnectionPool(ctx context.Context, tracer trace.Tracer, name, project, region, instance, ipType, user, pass, dbname string) (*sql.DB, error) {
//nolint:all // Reassigned ctx
ctx, span := sources.InitConnectionSpan(ctx, tracer, SourceKind, name)
defer span.End()
// Configure the driver to connect to the database
user, pass, useIAM, err := getConnectionConfig(ctx, user, pass)
if err != nil {
return nil, fmt.Errorf("unable to get Cloud SQL connection config: %w", err)
}
// Create a new dialer with options
userAgent, err := util.UserAgentFromContext(ctx)
if err != nil {
return nil, err
}
opts, err := sources.GetCloudSQLOpts(ipType, userAgent, false)
opts, err := sources.GetCloudSQLOpts(ipType, userAgent, useIAM)
if err != nil {
return nil, err
}
@@ -121,8 +153,36 @@ func initCloudSQLMySQLConnectionPool(ctx context.Context, tracer trace.Tracer, n
return nil, fmt.Errorf("unable to register driver: %w", err)
}
}
var dsn string
// Tell the driver to use the Cloud SQL Go Connector to create connections
dsn := fmt.Sprintf("%s:%s@cloudsql-mysql(%s:%s:%s)/%s?connectionAttributes=program_name:%s", user, pass, project, region, instance, dbname, url.QueryEscape(userAgent))
if useIAM {
token, err := sources.GetIAMAccessToken(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get IAM access token: %w", err)
}
// Set allowCleartextPasswords to true for IAM
dsn = fmt.Sprintf("%s:%s@cloudsql-mysql(%s:%s:%s)/%s?connectionAttributes=program_name:%s&allowCleartextPasswords=true",
user,
token,
project,
region,
instance,
dbname,
url.QueryEscape(userAgent),
)
} else {
dsn = fmt.Sprintf("%s:%s@cloudsql-mysql(%s:%s:%s)/%s?connectionAttributes=program_name:%s",
user,
pass,
project,
region,
instance,
dbname,
url.QueryEscape(userAgent),
)
}
db, err := sql.Open(
"cloudsql-mysql",
dsn,

View File

@@ -120,7 +120,7 @@ func getConnectionConfig(ctx context.Context, user, pass, dbname string) (string
// If password is provided without an username, raise an error
return "", useIAM, fmt.Errorf("password is provided without a username. Please provide both a username and password, or leave both fields empty")
}
email, err := sources.GetIAMPrincipalEmailFromADC(ctx)
email, err := sources.GetIAMPrincipalEmailFromADC(ctx, "postgres")
if err != nil {
return "", useIAM, fmt.Errorf("error getting email from ADC: %v", err)
}

View File

@@ -48,7 +48,7 @@ func GetCloudSQLOpts(ipType, userAgent string, useIAM bool) ([]cloudsqlconn.Opti
}
// GetIAMPrincipalEmailFromADC finds the email associated with ADC
func GetIAMPrincipalEmailFromADC(ctx context.Context) (string, error) {
func GetIAMPrincipalEmailFromADC(ctx context.Context, dbType string) (string, error) {
// Finds ADC and returns an HTTP client associated with it
client, err := google.DefaultClient(ctx,
"https://www.googleapis.com/auth/userinfo.email")
@@ -83,9 +83,31 @@ func GetIAMPrincipalEmailFromADC(ctx context.Context) (string, error) {
if !ok {
return "", fmt.Errorf("email not found in response: %v", err)
}
// service account email used for IAM should trim the suffix
email := strings.TrimSuffix(emailValue.(string), ".gserviceaccount.com")
return email, nil
fullEmail, ok := emailValue.(string)
if !ok {
return "", fmt.Errorf("email field is not a string")
}
var username string
// Format the username based on Database Type
switch strings.ToLower(dbType) {
case "mysql":
username, _, _ = strings.Cut(fullEmail, "@")
case "postgres", "mssql":
// service account email used for IAM should trim the suffix
username = strings.TrimSuffix(fullEmail, ".gserviceaccount.com")
default:
return "", fmt.Errorf("unsupported dbType: %s. Use 'mysql' or 'postgres'", dbType)
}
if username == "" {
return "", fmt.Errorf("username from ADC cannot be an empty string")
}
return username, nil
}
func GetIAMAccessToken(ctx context.Context) (string, error) {

View File

@@ -198,3 +198,70 @@ func TestCloudSQLMSSQLIpConnection(t *testing.T) {
})
}
}
func TestCloudSQLMSSQLIAMConnection(t *testing.T) {
getCloudSQLMSSQLVars(t)
// service account email used for IAM should trim the suffix
serviceAccountEmail := strings.TrimSuffix(tests.ServiceAccountEmail, ".gserviceaccount.com")
noPassSourceConfig := map[string]any{
"kind": CloudSQLMSSQLSourceKind,
"project": CloudSQLMSSQLProject,
"instance": CloudSQLMSSQLInstance,
"region": CloudSQLMSSQLRegion,
"database": CloudSQLMSSQLDatabase,
"user": serviceAccountEmail,
}
noUserSourceConfig := map[string]any{
"kind": CloudSQLMSSQLSourceKind,
"project": CloudSQLMSSQLProject,
"instance": CloudSQLMSSQLInstance,
"region": CloudSQLMSSQLRegion,
"database": CloudSQLMSSQLDatabase,
"password": "random",
}
noUserNoPassSourceConfig := map[string]any{
"kind": CloudSQLMSSQLSourceKind,
"project": CloudSQLMSSQLProject,
"instance": CloudSQLMSSQLInstance,
"region": CloudSQLMSSQLRegion,
"database": CloudSQLMSSQLDatabase,
}
tcs := []struct {
name string
sourceConfig map[string]any
isErr bool
}{
{
name: "no user no pass",
sourceConfig: noUserNoPassSourceConfig,
isErr: false,
},
{
name: "no password",
sourceConfig: noPassSourceConfig,
isErr: false,
},
{
name: "no user",
sourceConfig: noUserSourceConfig,
isErr: true,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
err := tests.RunSourceConnectionTest(t, tc.sourceConfig, CloudSQLMSSQLToolKind)
if err != nil {
if tc.isErr {
return
}
t.Fatalf("Connection test failure: %s", err)
}
if tc.isErr {
t.Fatalf("Expected error but test passed.")
}
})
}
}

View File

@@ -191,3 +191,70 @@ func TestCloudSQLMySQLIpConnection(t *testing.T) {
})
}
}
func TestCloudSQLMySQLIAMConnection(t *testing.T) {
getCloudSQLMySQLVars(t)
// service account email used for IAM should trim the suffix
serviceAccountEmail, _, _ := strings.Cut(tests.ServiceAccountEmail, "@")
noPassSourceConfig := map[string]any{
"kind": CloudSQLMySQLSourceKind,
"project": CloudSQLMySQLProject,
"instance": CloudSQLMySQLInstance,
"region": CloudSQLMySQLRegion,
"database": CloudSQLMySQLDatabase,
"user": serviceAccountEmail,
}
noUserSourceConfig := map[string]any{
"kind": CloudSQLMySQLSourceKind,
"project": CloudSQLMySQLProject,
"instance": CloudSQLMySQLInstance,
"region": CloudSQLMySQLRegion,
"database": CloudSQLMySQLDatabase,
"password": "random",
}
noUserNoPassSourceConfig := map[string]any{
"kind": CloudSQLMySQLSourceKind,
"project": CloudSQLMySQLProject,
"instance": CloudSQLMySQLInstance,
"region": CloudSQLMySQLRegion,
"database": CloudSQLMySQLDatabase,
}
tcs := []struct {
name string
sourceConfig map[string]any
isErr bool
}{
{
name: "no user no pass",
sourceConfig: noUserNoPassSourceConfig,
isErr: false,
},
{
name: "no password",
sourceConfig: noPassSourceConfig,
isErr: false,
},
{
name: "no user",
sourceConfig: noUserSourceConfig,
isErr: true,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
err := tests.RunSourceConnectionTest(t, tc.sourceConfig, CloudSQLMySQLToolKind)
if err != nil {
if tc.isErr {
return
}
t.Fatalf("Connection test failure: %s", err)
}
if tc.isErr {
t.Fatalf("Expected error but test passed.")
}
})
}
}

View File

@@ -2232,6 +2232,9 @@ func RunMySQLListTablesTest(t *testing.T, databaseName, tableNameParam, tableNam
if err := json.Unmarshal([]byte(table.ObjectDetails), &d); err != nil {
t.Fatalf("failed to unmarshal nested ObjectDetails string: %v", err)
}
// Set Owner to nil for verification purposes because
// standard MySQL returns nil while Cloud SQL returns the user.
d.Owner = nil
details = append(details, d)
}
got = details