| 1 |
// Copyright 2025 The Archivista Contributors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package sqlstore |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"errors" |
| 20 |
"fmt" |
| 21 |
"net/url" |
| 22 |
"strings" |
| 23 |
"testing" |
| 24 |
|
| 25 |
"github.com/aws/aws-sdk-go-v2/aws" |
| 26 |
"github.com/aws/aws-sdk-go-v2/config" |
| 27 |
"github.com/aws/aws-sdk-go-v2/credentials" |
| 28 |
"github.com/go-sql-driver/mysql" |
| 29 |
"github.com/stretchr/testify/suite" |
| 30 |
) |
| 31 |
|
| 32 |
const ( |
| 33 |
mysqlConnStr = "user:password@tcp(host:3306)/dbname" |
| 34 |
postgresConnStr = "postgresql://user:password@host:5432/dbname" |
| 35 |
) |
| 36 |
|
| 37 |
type RewriteConnectionStringForIAMSuite struct { |
| 38 |
suite.Suite |
| 39 |
} |
| 40 |
|
| 41 |
func TestRewriteConnectionStringForIAM(t *testing.T) { |
| 42 |
suite.Run(t, new(RewriteConnectionStringForIAMSuite)) |
| 43 |
} |
| 44 |
|
| 45 |
type mockAWSConfigAPI struct { |
| 46 |
cfg aws.Config |
| 47 |
err error |
| 48 |
} |
| 49 |
|
| 50 |
func (m *mockAWSConfigAPI) LoadDefaultConfig(ctx context.Context, optFns ...func(*config.LoadOptions) error) (aws.Config, error) { |
| 51 |
return m.cfg, m.err |
| 52 |
} |
| 53 |
|
| 54 |
func (s *RewriteConnectionStringForIAMSuite) TestUnsupportedSqlBackend() { |
| 55 |
AwsConfigAPI = nil |
| 56 |
|
| 57 |
_, err := RewriteConnectionStringForIAM("unsupported", "connstr", false) |
| 58 |
s.Require().Error(err) |
| 59 |
} |
| 60 |
|
| 61 |
// An example token for PSQL that is returned will look like this: |
| 62 |
// user:host:3306?Action=connect&DBUser=user&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=foo%2F20251028%2Fus-east-1%2Frds-db%2Faws4_request&X-Amz-Date=20251028T171846Z&X-Amz-Expires=900&X-Amz-Security-Token=baz&X-Amz-SignedHeaders=host&X-Amz-Signature=e39c63e53919a3d1cdb96211af8d0a83b7afdd637b5d6788ad9014add900489e@tcp(host:3306)/dbname?allowCleartextPasswords=true&parseTime=true&tls=true |
| 63 |
// |
| 64 |
// The details of the token that we care about are: |
| 65 |
// The token starts with `host:3306` |
| 66 |
// |
| 67 |
// And has these predictable query parameters: |
| 68 |
// Action=connect |
| 69 |
// DBUser=user |
| 70 |
// X-Amz-Credential=foo/20251028/us-east-1/rds-db/aws4_request |
| 71 |
// X-Amz-Expires=900 |
| 72 |
// X-Amz-Security-Token=baz |
| 73 |
// |
| 74 |
// The other components are variable, based on the time or AWS SDK implementation details |
| 75 |
func validateMySqlAuthToken(s *RewriteConnectionStringForIAMSuite, connStr string, opts ...string) { |
| 76 |
parsedConnStr, err := mysql.ParseDSN(connStr) |
| 77 |
s.Require().NoError(err) |
| 78 |
|
| 79 |
s.Equal("user", parsedConnStr.User) |
| 80 |
s.Equal("tcp", parsedConnStr.Net) |
| 81 |
s.Equal("host:3306", parsedConnStr.Addr) |
| 82 |
s.Equal("dbname", parsedConnStr.DBName) |
| 83 |
|
| 84 |
// allowCleartextPasswords=true&parseTime=true&tls=true |
| 85 |
s.True(parsedConnStr.AllowCleartextPasswords) |
| 86 |
s.Equal("true", parsedConnStr.TLSConfig) |
| 87 |
s.True(parsedConnStr.ParseTime) |
| 88 |
|
| 89 |
for _, opt := range opts { |
| 90 |
kv := strings.Split(opt, "=") |
| 91 |
s.Require().Len(kv, 2, "expected opt %s to be in key=value format", opt) |
| 92 |
val, ok := parsedConnStr.Params[kv[0]] |
| 93 |
s.Require().True(ok, "expected param %s to be present", opt) |
| 94 |
s.Equal(kv[1], val, "expected param %s to be true", opt) |
| 95 |
} |
| 96 |
|
| 97 |
token := parsedConnStr.Passwd |
| 98 |
s.NotEmpty(token) |
| 99 |
s.True(strings.HasPrefix(token, "host:3306")) |
| 100 |
|
| 101 |
parsedToken, err := url.Parse(token) |
| 102 |
s.Require().NoError(err) |
| 103 |
|
| 104 |
params := parsedToken.Query() |
| 105 |
s.Equal("connect", params.Get("Action")) |
| 106 |
s.Equal("user", params.Get("DBUser")) |
| 107 |
s.True(strings.HasPrefix(params.Get("X-Amz-Credential"), "foo")) |
| 108 |
s.Contains(params.Get("X-Amz-Credential"), "us-east-1") |
| 109 |
s.Equal("baz", params.Get("X-Amz-Security-Token")) |
| 110 |
} |
| 111 |
|
| 112 |
func (s *RewriteConnectionStringForIAMSuite) TestMySqlSuccess() { |
| 113 |
AwsConfigAPI = &mockAWSConfigAPI{ |
| 114 |
cfg: aws.Config{ |
| 115 |
Region: "us-east-1", |
| 116 |
Credentials: credentials.NewStaticCredentialsProvider("foo", "bar", "baz"), |
| 117 |
}, |
| 118 |
} |
| 119 |
|
| 120 |
connStr, err := RewriteConnectionStringForIAM("mysql_rds_iam", mysqlConnStr, false) |
| 121 |
s.Require().NoError(err) |
| 122 |
|
| 123 |
validateMySqlAuthToken(s, connStr) |
| 124 |
} |
| 125 |
|
| 126 |
func (s *RewriteConnectionStringForIAMSuite) TestMySqlNoPasswordSuccess() { |
| 127 |
AwsConfigAPI = &mockAWSConfigAPI{ |
| 128 |
cfg: aws.Config{ |
| 129 |
Region: "us-east-1", |
| 130 |
Credentials: credentials.NewStaticCredentialsProvider("foo", "bar", "baz"), |
| 131 |
}, |
| 132 |
} |
| 133 |
|
| 134 |
connStr, err := RewriteConnectionStringForIAM("mysql_rds_iam", "user@tcp(host:3306)/dbname", false) |
| 135 |
s.Require().NoError(err) |
| 136 |
|
| 137 |
validateMySqlAuthToken(s, connStr) |
| 138 |
} |
| 139 |
|
| 140 |
func (s *RewriteConnectionStringForIAMSuite) TestMySqlWithParamsSuccess() { |
| 141 |
AwsConfigAPI = &mockAWSConfigAPI{ |
| 142 |
cfg: aws.Config{ |
| 143 |
Region: "us-east-1", |
| 144 |
Credentials: credentials.NewStaticCredentialsProvider("foo", "bar", "baz"), |
| 145 |
}, |
| 146 |
} |
| 147 |
|
| 148 |
connStr, err := RewriteConnectionStringForIAM("mysql_rds_iam", fmt.Sprintf("%s?foo=bar", mysqlConnStr), false) |
| 149 |
s.Require().NoError(err) |
| 150 |
|
| 151 |
validateMySqlAuthToken(s, connStr, "foo=bar") |
| 152 |
} |
| 153 |
|
| 154 |
func (s *RewriteConnectionStringForIAMSuite) TestMySqlOverrideParamsSuccess() { |
| 155 |
AwsConfigAPI = &mockAWSConfigAPI{ |
| 156 |
cfg: aws.Config{ |
| 157 |
Region: "us-east-1", |
| 158 |
Credentials: credentials.NewStaticCredentialsProvider("foo", "bar", "baz"), |
| 159 |
}, |
| 160 |
} |
| 161 |
|
| 162 |
connStr, err := RewriteConnectionStringForIAM("mysql_rds_iam", fmt.Sprintf("%s?allowCleartextPasswords=false&foo=baz&tls=false", mysqlConnStr), false) |
| 163 |
s.Require().NoError(err) |
| 164 |
|
| 165 |
validateMySqlAuthToken(s, connStr, "foo=baz") |
| 166 |
} |
| 167 |
|
| 168 |
// An example token for PSQL that is returned will look like this: |
| 169 |
// user:host:5432?Action=connect&DBUser=user&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=foo%2F20251028%2Fus-east-1%2Frds-db%2Faws4_request&X-Amz-Date=20251028T171846Z&X-Amz-Expires=900&X-Amz-Security-Token=baz&X-Amz-SignedHeaders=host&X-Amz-Signature=e39c63e53919a3d1cdb96211af8d0a83b7afdd637b5d6788ad9014add900489e@tcp(host:3306)/dbname?allowCleartextPasswords=true&parseTime=true&tls=true |
| 170 |
// |
| 171 |
// The details of the token that we care about are: |
| 172 |
// The token starts with `host:5432` |
| 173 |
// |
| 174 |
// And has these predictable query parameters: |
| 175 |
// Action=connect |
| 176 |
// DBUser=user |
| 177 |
// X-Amz-Credential=foo/20251028/us-east-1/rds-db/aws4_request |
| 178 |
// X-Amz-Expires=900 |
| 179 |
// X-Amz-Security-Token=baz |
| 180 |
// |
| 181 |
// The other components are variable, based on the time or AWS SDK implementation details |
| 182 |
func validatePostgresAuthToken(s *RewriteConnectionStringForIAMSuite, connStr string, opts ...string) { |
| 183 |
parsedConnStr, err := url.Parse(connStr) |
| 184 |
s.Require().NoError(err) |
| 185 |
s.Equal("postgres", parsedConnStr.Scheme) |
| 186 |
s.Equal("user", parsedConnStr.User.Username()) |
| 187 |
s.Equal("host", parsedConnStr.Hostname()) |
| 188 |
s.Equal("5432", parsedConnStr.Port()) |
| 189 |
s.Equal("/dbname", parsedConnStr.Path) |
| 190 |
|
| 191 |
queryParams := parsedConnStr.Query() |
| 192 |
for _, opt := range opts { |
| 193 |
kv := strings.Split(opt, "=") |
| 194 |
s.Require().Len(kv, 2, "expected opt %s to be in key=value format", opt) |
| 195 |
val := queryParams.Get(kv[0]) |
| 196 |
s.Equal(kv[1], val, "expected param %s to be true", opt) |
| 197 |
} |
| 198 |
|
| 199 |
token, set := parsedConnStr.User.Password() |
| 200 |
s.Require().True(set) |
| 201 |
s.NotEmpty(token) |
| 202 |
s.True(strings.HasPrefix(token, "host:5432")) |
| 203 |
|
| 204 |
parsedToken, err := url.Parse(token) |
| 205 |
s.Require().NoError(err) |
| 206 |
|
| 207 |
params := parsedToken.Query() |
| 208 |
s.Equal("connect", params.Get("Action")) |
| 209 |
s.Equal("user", params.Get("DBUser")) |
| 210 |
s.True(strings.HasPrefix(params.Get("X-Amz-Credential"), "foo")) |
| 211 |
s.Contains(params.Get("X-Amz-Credential"), "us-east-1") |
| 212 |
s.Equal("baz", params.Get("X-Amz-Security-Token")) |
| 213 |
} |
| 214 |
func (s *RewriteConnectionStringForIAMSuite) TestPostgresSuccess() { |
| 215 |
AwsConfigAPI = &mockAWSConfigAPI{ |
| 216 |
cfg: aws.Config{ |
| 217 |
Region: "us-east-1", |
| 218 |
Credentials: credentials.NewStaticCredentialsProvider("foo", "bar", "baz"), |
| 219 |
}, |
| 220 |
} |
| 221 |
|
| 222 |
connStr, err := RewriteConnectionStringForIAM("psql_rds_iam", postgresConnStr, false) |
| 223 |
s.Require().NoError(err) |
| 224 |
|
| 225 |
validatePostgresAuthToken(s, connStr) |
| 226 |
} |
| 227 |
|
| 228 |
func (s *RewriteConnectionStringForIAMSuite) TestPostgresNoPasswordSuccess() { |
| 229 |
AwsConfigAPI = &mockAWSConfigAPI{ |
| 230 |
cfg: aws.Config{ |
| 231 |
Region: "us-east-1", |
| 232 |
Credentials: credentials.NewStaticCredentialsProvider("foo", "bar", "baz"), |
| 233 |
}, |
| 234 |
} |
| 235 |
|
| 236 |
connStr, err := RewriteConnectionStringForIAM("psql_rds_iam", "postgresql://user@host:5432/dbname", false) |
| 237 |
s.Require().NoError(err) |
| 238 |
|
| 239 |
validatePostgresAuthToken(s, connStr) |
| 240 |
} |
| 241 |
|
| 242 |
func (s *RewriteConnectionStringForIAMSuite) TestPostgresWithParamsSuccess() { |
| 243 |
AwsConfigAPI = &mockAWSConfigAPI{ |
| 244 |
cfg: aws.Config{ |
| 245 |
Region: "us-east-1", |
| 246 |
Credentials: credentials.NewStaticCredentialsProvider("foo", "bar", "baz"), |
| 247 |
}, |
| 248 |
} |
| 249 |
|
| 250 |
connStr, err := RewriteConnectionStringForIAM("psql_rds_iam", fmt.Sprintf("%s?foo=bar", postgresConnStr), false) |
| 251 |
s.Require().NoError(err) |
| 252 |
|
| 253 |
validatePostgresAuthToken(s, connStr, "foo=bar") |
| 254 |
//s.Equal("postgresql://user:authtoken@host:5432/dbname?foo=bar", connStr) |
| 255 |
} |
| 256 |
|
| 257 |
func (s *RewriteConnectionStringForIAMSuite) TestLoadConfigError() { |
| 258 |
AwsConfigAPI = &mockAWSConfigAPI{ |
| 259 |
err: errors.New("some error"), |
| 260 |
} |
| 261 |
|
| 262 |
_, err := RewriteConnectionStringForIAM("mysql_rds_iam", mysqlConnStr, false) |
| 263 |
s.Require().Error(err) |
| 264 |
s.Contains(err.Error(), "loading AWS config") |
| 265 |
} |
| 266 |
|
| 267 |
func (s *RewriteConnectionStringForIAMSuite) TestBuildAuthTokenError() { |
| 268 |
AwsConfigAPI = &mockAWSConfigAPI{ |
| 269 |
cfg: aws.Config{Region: "us-east-1"}, |
| 270 |
} |
| 271 |
|
| 272 |
_, err := RewriteConnectionStringForIAM("mysql_rds_iam", mysqlConnStr, false) |
| 273 |
s.Require().Error(err) |
| 274 |
s.Contains(err.Error(), "could not create credentials refresh store") |
| 275 |
} |
| 276 |
|
| 277 |
func (s *RewriteConnectionStringForIAMSuite) TestMySqlNoUser() { |
| 278 |
AwsConfigAPI = &mockAWSConfigAPI{ |
| 279 |
cfg: aws.Config{Region: "us-east-1"}, |
| 280 |
} |
| 281 |
|
| 282 |
_, err := RewriteConnectionStringForIAM("mysql_rds_iam", ":password@tcp(host:3306)/dbname", false) |
| 283 |
s.Require().Error(err) |
| 284 |
s.Contains(err.Error(), "onnection string is missing a user") |
| 285 |
} |
| 286 |
|
| 287 |
func (s *RewriteConnectionStringForIAMSuite) TestPostgresNoUser() { |
| 288 |
AwsConfigAPI = &mockAWSConfigAPI{ |
| 289 |
cfg: aws.Config{Region: "us-east-1"}, |
| 290 |
} |
| 291 |
|
| 292 |
_, err := RewriteConnectionStringForIAM("psql_rds_iam", "postgresql://:password@host:5432/dbname", false) |
| 293 |
s.Require().Error(err) |
| 294 |
s.Contains(err.Error(), "onnection string is missing a user") |
| 295 |
} |
| 296 |
|
| 297 |
func (s *RewriteConnectionStringForIAMSuite) TestUrlParseError() { |
| 298 |
AwsConfigAPI = &mockAWSConfigAPI{ |
| 299 |
cfg: aws.Config{Region: "us-east-1"}, |
| 300 |
} |
| 301 |
|
| 302 |
_, err := RewriteConnectionStringForIAM("mysql_rds_iam", "://invalid-url", false) |
| 303 |
s.Require().Error(err) |
| 304 |
s.Contains(err.Error(), "parsing connection string") |
| 305 |
} |