|
| 1 | +package database |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestNewRDSIAMConnector(t *testing.T) { |
| 13 | + ctx := context.Background() |
| 14 | + |
| 15 | + t.Run("parses endpoint, user and region", func(t *testing.T) { |
| 16 | + c, err := NewRDSIAMConnector(ctx, |
| 17 | + "postgres://svc_user@mydb.abc123.us-east-1.rds.amazonaws.com:5432/scroll?sslmode=verify-full", |
| 18 | + "us-east-1") |
| 19 | + require.NoError(t, err) |
| 20 | + |
| 21 | + conn, ok := c.(*rdsIAMConnector) |
| 22 | + require.True(t, ok) |
| 23 | + assert.Equal(t, "mydb.abc123.us-east-1.rds.amazonaws.com:5432", conn.endpoint) |
| 24 | + assert.Equal(t, "svc_user", conn.base.User) |
| 25 | + assert.Equal(t, "us-east-1", conn.region) |
| 26 | + // TLS requested in the DSN must be preserved. |
| 27 | + require.NotNil(t, conn.base.TLSConfig) |
| 28 | + }) |
| 29 | + |
| 30 | + t.Run("defaults port to 5432", func(t *testing.T) { |
| 31 | + c, err := NewRDSIAMConnector(ctx, |
| 32 | + "postgres://svc_user@mydb.example.rds.amazonaws.com/scroll?sslmode=require", |
| 33 | + "eu-west-1") |
| 34 | + require.NoError(t, err) |
| 35 | + assert.Equal(t, "mydb.example.rds.amazonaws.com:5432", c.(*rdsIAMConnector).endpoint) |
| 36 | + }) |
| 37 | + |
| 38 | + t.Run("rejects sslmode=disable", func(t *testing.T) { |
| 39 | + // IAM auth requires TLS; a plaintext DSN must fail closed rather than |
| 40 | + // silently sending the token in cleartext. |
| 41 | + _, err := NewRDSIAMConnector(ctx, |
| 42 | + "postgres://svc_user@mydb.example.rds.amazonaws.com:5432/scroll?sslmode=disable", |
| 43 | + "us-east-1") |
| 44 | + assert.ErrorContains(t, err, "TLS") |
| 45 | + }) |
| 46 | + |
| 47 | + t.Run("rejects sslmode=prefer (plaintext fallback)", func(t *testing.T) { |
| 48 | + // prefer keeps a non-TLS entry in Fallbacks that pgx would retry on, |
| 49 | + // which would leak the token in cleartext. |
| 50 | + _, err := NewRDSIAMConnector(ctx, |
| 51 | + "postgres://svc_user@mydb.example.rds.amazonaws.com:5432/scroll?sslmode=prefer", |
| 52 | + "us-east-1") |
| 53 | + assert.ErrorContains(t, err, "plaintext") |
| 54 | + }) |
| 55 | + |
| 56 | + t.Run("rejects sslmode=allow (plaintext primary)", func(t *testing.T) { |
| 57 | + _, err := NewRDSIAMConnector(ctx, |
| 58 | + "postgres://svc_user@mydb.example.rds.amazonaws.com:5432/scroll?sslmode=allow", |
| 59 | + "us-east-1") |
| 60 | + assert.Error(t, err) |
| 61 | + }) |
| 62 | + |
| 63 | + t.Run("rejects multi-host dsn", func(t *testing.T) { |
| 64 | + // The token is signed for a single host:port, so failover would fail auth. |
| 65 | + _, err := NewRDSIAMConnector(ctx, |
| 66 | + "host=host1.rds.amazonaws.com,host2.rds.amazonaws.com port=5432 user=svc_user dbname=scroll sslmode=require", |
| 67 | + "us-east-1") |
| 68 | + assert.ErrorContains(t, err, "multi-host") |
| 69 | + }) |
| 70 | + |
| 71 | + t.Run("region falls back to explicit empty error when unresolved", func(t *testing.T) { |
| 72 | + // With no region argument, no AWS_REGION in the environment, and no |
| 73 | + // shared config file, region resolution must fail rather than silently |
| 74 | + // signing with an empty region. |
| 75 | + t.Setenv("AWS_REGION", "") |
| 76 | + t.Setenv("AWS_DEFAULT_REGION", "") |
| 77 | + // Isolate from any ~/.aws/config on the dev/CI machine, which would |
| 78 | + // otherwise resolve a default region and make this test nondeterministic. |
| 79 | + t.Setenv("AWS_CONFIG_FILE", filepath.Join(t.TempDir(), "no-config")) |
| 80 | + t.Setenv("AWS_SHARED_CREDENTIALS_FILE", filepath.Join(t.TempDir(), "no-creds")) |
| 81 | + _, err := NewRDSIAMConnector(ctx, |
| 82 | + "postgres://svc_user@mydb.example.rds.amazonaws.com:5432/scroll?sslmode=require", |
| 83 | + "") |
| 84 | + assert.ErrorContains(t, err, "region") |
| 85 | + }) |
| 86 | + |
| 87 | + t.Run("rejects invalid dsn", func(t *testing.T) { |
| 88 | + _, err := NewRDSIAMConnector(ctx, "::not a dsn::", "us-east-1") |
| 89 | + assert.Error(t, err) |
| 90 | + }) |
| 91 | +} |
0 commit comments