|
| 1 | +package s3_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "instant.dev/common/storageprovider" |
| 13 | + "instant.dev/common/storageprovider/s3" |
| 14 | +) |
| 15 | + |
| 16 | +// TestS3_New_RequiresMasterCreds — sts:AssumeRole requires platform creds. |
| 17 | +func TestS3_New_RequiresMasterCreds(t *testing.T) { |
| 18 | + _, err := s3.New(storageprovider.Config{ |
| 19 | + AWSRoleARN: "arn:aws:iam::123:role/x", |
| 20 | + }) |
| 21 | + require.Error(t, err) |
| 22 | + assert.Contains(t, err.Error(), "OBJECT_STORE_ACCESS_KEY") |
| 23 | +} |
| 24 | + |
| 25 | +// TestS3_Getters_AndCustomerEndpointFallbacks covers Master*/Endpoint/Bucket/ |
| 26 | +// Region/PublicURL/Name + the two customerEndpointURL branches (already-schemed |
| 27 | +// and bare-host). |
| 28 | +func TestS3_Getters_AndCustomerEndpointFallbacks(t *testing.T) { |
| 29 | + t.Run("bare_host_gets_https_prefix", func(t *testing.T) { |
| 30 | + raw, err := s3.New(storageprovider.Config{ |
| 31 | + AWSRoleARN: "arn:aws:iam::123:role/x", |
| 32 | + MasterKey: "MK", |
| 33 | + MasterSecret: "MS", |
| 34 | + }) |
| 35 | + require.NoError(t, err) |
| 36 | + p := raw.(*s3.Provider) |
| 37 | + |
| 38 | + assert.Equal(t, "MK", p.MasterAccessKey()) |
| 39 | + assert.Equal(t, "MS", p.MasterSecretKey()) |
| 40 | + assert.Equal(t, "s3.us-east-1.amazonaws.com", p.Endpoint()) |
| 41 | + assert.Equal(t, "instant-shared", p.Bucket()) |
| 42 | + assert.Equal(t, "us-east-1", p.Region()) |
| 43 | + assert.Equal(t, "s3", p.Name()) |
| 44 | + assert.Equal(t, "https://s3.us-east-1.amazonaws.com", p.PublicURL()) |
| 45 | + }) |
| 46 | + |
| 47 | + t.Run("endpoint_already_schemed_passes_through", func(t *testing.T) { |
| 48 | + raw, err := s3.New(storageprovider.Config{ |
| 49 | + Endpoint: "https://custom.example", |
| 50 | + AWSRoleARN: "arn:aws:iam::123:role/x", |
| 51 | + MasterKey: "MK", |
| 52 | + MasterSecret: "MS", |
| 53 | + }) |
| 54 | + require.NoError(t, err) |
| 55 | + p := raw.(*s3.Provider) |
| 56 | + assert.Equal(t, "https://custom.example", p.PublicURL()) |
| 57 | + }) |
| 58 | + |
| 59 | + t.Run("public_url_set_overrides_endpoint", func(t *testing.T) { |
| 60 | + raw, err := s3.New(storageprovider.Config{ |
| 61 | + Endpoint: "s3.amazonaws.com", |
| 62 | + PublicURL: "https://cdn.example.dev", |
| 63 | + AWSRoleARN: "arn:aws:iam::123:role/x", |
| 64 | + MasterKey: "MK", |
| 65 | + MasterSecret: "MS", |
| 66 | + }) |
| 67 | + require.NoError(t, err) |
| 68 | + p := raw.(*s3.Provider) |
| 69 | + assert.Equal(t, "https://cdn.example.dev", p.PublicURL()) |
| 70 | + }) |
| 71 | + |
| 72 | + t.Run("region_defaults_and_bucket_defaults_apply", func(t *testing.T) { |
| 73 | + raw, err := s3.New(storageprovider.Config{ |
| 74 | + AWSRoleARN: "arn:aws:iam::123:role/x", |
| 75 | + MasterKey: "MK", |
| 76 | + MasterSecret: "MS", |
| 77 | + }) |
| 78 | + require.NoError(t, err) |
| 79 | + p := raw.(*s3.Provider) |
| 80 | + assert.Equal(t, "us-east-1", p.Region()) |
| 81 | + assert.Equal(t, "instant-shared", p.Bucket()) |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +// TestS3_RevokeTenantCredentials_IsNoOp — STS sessions can't be revoked, the |
| 86 | +// method must return nil regardless of input. |
| 87 | +func TestS3_RevokeTenantCredentials_IsNoOp(t *testing.T) { |
| 88 | + raw, err := s3.New(storageprovider.Config{ |
| 89 | + AWSRoleARN: "arn:aws:iam::123:role/x", |
| 90 | + MasterKey: "MK", |
| 91 | + MasterSecret: "MS", |
| 92 | + }) |
| 93 | + require.NoError(t, err) |
| 94 | + p := raw.(*s3.Provider) |
| 95 | + |
| 96 | + require.NoError(t, p.RevokeTenantCredentials(context.Background(), "")) |
| 97 | + require.NoError(t, p.RevokeTenantCredentials(context.Background(), "any-key")) |
| 98 | +} |
| 99 | + |
| 100 | +// TestS3_IssueTenantCredentials_DefaultsTTLAndPrefix covers: |
| 101 | +// - TTL==0 falls through to the 1-hour default |
| 102 | +// - Empty Prefix falls back to ResourceToken |
| 103 | +// - Empty Bucket falls back to provider default |
| 104 | +func TestS3_IssueTenantCredentials_DefaultsTTLAndPrefix(t *testing.T) { |
| 105 | + raw, err := s3.New(storageprovider.Config{ |
| 106 | + Region: "us-west-2", |
| 107 | + Bucket: "my-default", |
| 108 | + AWSRoleARN: "arn:aws:iam::123:role/x", |
| 109 | + MasterKey: "MK", |
| 110 | + MasterSecret: "MS", |
| 111 | + }) |
| 112 | + require.NoError(t, err) |
| 113 | + p := raw.(*s3.Provider) |
| 114 | + |
| 115 | + var captured s3.AssumeRoleInput |
| 116 | + p.SetAssumeRoleFunc(func(ctx context.Context, in s3.AssumeRoleInput) (*s3.AssumeRoleOutput, error) { |
| 117 | + captured = in |
| 118 | + return &s3.AssumeRoleOutput{ |
| 119 | + AccessKeyID: "AK", |
| 120 | + SecretAccessKey: "SK", |
| 121 | + SessionToken: "TOK", |
| 122 | + Expiration: time.Now().Add(time.Hour), |
| 123 | + }, nil |
| 124 | + }) |
| 125 | + |
| 126 | + creds, err := p.IssueTenantCredentials(context.Background(), storageprovider.IssueRequest{ |
| 127 | + ResourceToken: "tok-ABC", |
| 128 | + // No Prefix, no Bucket, no TTL — exercises every fallback line. |
| 129 | + }) |
| 130 | + require.NoError(t, err) |
| 131 | + assert.Equal(t, "tok-ABC", creds.Prefix, "prefix must default to resource token") |
| 132 | + assert.Equal(t, "my-default", creds.Bucket, "bucket must default to provider default") |
| 133 | + assert.Equal(t, int32(3600), captured.DurationSeconds, "TTL==0 must default to 1h") |
| 134 | +} |
| 135 | + |
| 136 | +// TestS3_SafeSessionName covers the trimming + min-length fallback. RoleSessionName |
| 137 | +// is constrained by AWS to [\w+=,.@-] and length 2..64; safeSessionName must drop |
| 138 | +// disallowed characters AND backfill "instanode-x" when the result is too short |
| 139 | +// to be a valid session name. |
| 140 | +func TestS3_SafeSessionName(t *testing.T) { |
| 141 | + raw, err := s3.New(storageprovider.Config{ |
| 142 | + AWSRoleARN: "arn:aws:iam::123:role/x", |
| 143 | + MasterKey: "MK", |
| 144 | + MasterSecret: "MS", |
| 145 | + }) |
| 146 | + require.NoError(t, err) |
| 147 | + p := raw.(*s3.Provider) |
| 148 | + |
| 149 | + cases := []struct { |
| 150 | + name string |
| 151 | + token string |
| 152 | + expected string |
| 153 | + }{ |
| 154 | + { |
| 155 | + name: "all_disallowed_chars_falls_back_to_instanode_x", |
| 156 | + token: "!!!@@@###", |
| 157 | + expected: "instanode-instanode-x", |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "long_token_truncated_to_56", |
| 161 | + token: strings.Repeat("a", 200), |
| 162 | + expected: "instanode-" + strings.Repeat("a", 56), |
| 163 | + }, |
| 164 | + { |
| 165 | + name: "digits_underscores_dashes_kept", |
| 166 | + token: "tenant-01_abc", |
| 167 | + expected: "instanode-tenant-01_abc", |
| 168 | + }, |
| 169 | + } |
| 170 | + for _, c := range cases { |
| 171 | + t.Run(c.name, func(t *testing.T) { |
| 172 | + var captured s3.AssumeRoleInput |
| 173 | + p.SetAssumeRoleFunc(func(ctx context.Context, in s3.AssumeRoleInput) (*s3.AssumeRoleOutput, error) { |
| 174 | + captured = in |
| 175 | + return &s3.AssumeRoleOutput{ |
| 176 | + AccessKeyID: "AK", |
| 177 | + SecretAccessKey: "SK", |
| 178 | + SessionToken: "TOK", |
| 179 | + Expiration: time.Now().Add(15 * time.Minute), |
| 180 | + }, nil |
| 181 | + }) |
| 182 | + _, err := p.IssueTenantCredentials(context.Background(), storageprovider.IssueRequest{ |
| 183 | + ResourceToken: c.token, |
| 184 | + TTL: 15 * time.Minute, |
| 185 | + }) |
| 186 | + require.NoError(t, err) |
| 187 | + assert.Equal(t, c.expected, captured.RoleSessionName) |
| 188 | + }) |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +// TestS3_AssumeRoleError_Propagates verifies the error from a failing |
| 193 | +// AssumeRole call surfaces to the caller rather than being swallowed into |
| 194 | +// a "best-effort" silent return. |
| 195 | +func TestS3_AssumeRoleError_Propagates(t *testing.T) { |
| 196 | + raw, err := s3.New(storageprovider.Config{ |
| 197 | + AWSRoleARN: "arn:aws:iam::123:role/x", |
| 198 | + MasterKey: "MK", |
| 199 | + MasterSecret: "MS", |
| 200 | + }) |
| 201 | + require.NoError(t, err) |
| 202 | + p := raw.(*s3.Provider) |
| 203 | + p.SetAssumeRoleFunc(func(ctx context.Context, in s3.AssumeRoleInput) (*s3.AssumeRoleOutput, error) { |
| 204 | + return nil, assertError("aws-sdk: throttled") |
| 205 | + }) |
| 206 | + _, err = p.IssueTenantCredentials(context.Background(), storageprovider.IssueRequest{ |
| 207 | + ResourceToken: "x", |
| 208 | + TTL: 15 * time.Minute, |
| 209 | + }) |
| 210 | + require.Error(t, err) |
| 211 | + assert.Contains(t, err.Error(), "throttled") |
| 212 | +} |
| 213 | + |
| 214 | +type assertError string |
| 215 | + |
| 216 | +func (a assertError) Error() string { return string(a) } |
0 commit comments