| 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 |
"fmt" |
| 20 |
"strings" |
| 21 |
|
| 22 |
"github.com/aws/aws-sdk-go-v2/aws" |
| 23 |
"github.com/aws/aws-sdk-go-v2/config" |
| 24 |
"github.com/jkjell/go-db-credential-refresh/driver" |
| 25 |
|
| 26 |
_ "github.com/lib/pq" |
| 27 |
) |
| 28 |
|
| 29 |
type AWSConfigAPI interface { |
| 30 |
// LoadDefaultConfig loads the default AWS configuration. |
| 31 |
LoadDefaultConfig(ctx context.Context, opts ...func(*config.LoadOptions) error) (aws.Config, error) |
| 32 |
} |
| 33 |
|
| 34 |
type AWSConfig struct{} |
| 35 |
|
| 36 |
func (c *AWSConfig) LoadDefaultConfig(ctx context.Context, opts ...func(*config.LoadOptions) error) (aws.Config, error) { |
| 37 |
return config.LoadDefaultConfig(ctx, opts...) |
| 38 |
} |
| 39 |
|
| 40 |
var AwsConfigAPI AWSConfigAPI = &AWSConfig{} |
| 41 |
|
| 42 |
// RewriteConnectionStringForIAM rewrites the connection string to use AWS RDS IAM authentication |
| 43 |
// It supports both MYSQL_RDS_IAM and PSQL_RDS_IAM backends |
| 44 |
func RewriteConnectionStringForIAM(sqlBackend string, connectionString string, dryRun bool) (string, error) { |
| 45 |
var dc *driver.Config |
| 46 |
var user string |
| 47 |
upperSqlBackend := strings.ToUpper(sqlBackend) |
| 48 |
|
| 49 |
if strings.HasPrefix(upperSqlBackend, "MYSQL") { |
| 50 |
var err error |
| 51 |
dc, user, _, err = ConfigFromMySQL(connectionString) |
| 52 |
if err != nil { |
| 53 |
return "", fmt.Errorf("could not get driver config from mysql connection string: %w", err) |
| 54 |
} |
| 55 |
// for mysql, we need to add some query parameters |
| 56 |
dc.Opts["tls"] = "true" |
| 57 |
dc.Opts["allowCleartextPasswords"] = "true" |
| 58 |
} else if strings.HasPrefix(upperSqlBackend, "PSQL") { |
| 59 |
var err error |
| 60 |
dc, user, _, err = ConfigFromPostgres(connectionString) |
| 61 |
if err != nil { |
| 62 |
return "", fmt.Errorf("could not get driver config from mysql connection string: %w", err) |
| 63 |
} |
| 64 |
} else { |
| 65 |
return "", fmt.Errorf("unsupported sql backend: %s", sqlBackend) |
| 66 |
} |
| 67 |
|
| 68 |
s, err := AWSRDSStoreFromDriverConfig(dc, user) |
| 69 |
if err != nil { |
| 70 |
return "", fmt.Errorf("could not create credentials refresh store: %w", err) |
| 71 |
} |
| 72 |
|
| 73 |
creds, err := s.Get(context.Background()) |
| 74 |
if err != nil { |
| 75 |
return "", fmt.Errorf("could not get refreshed credentials: %w", err) |
| 76 |
} |
| 77 |
|
| 78 |
if creds == nil { |
| 79 |
return "", fmt.Errorf("refreshed credentials are nil") |
| 80 |
} |
| 81 |
|
| 82 |
var password string |
| 83 |
if dryRun { |
| 84 |
password = "authtoken" |
| 85 |
} else { |
| 86 |
password = creds.GetPassword() |
| 87 |
} |
| 88 |
return dc.Formatter(creds.GetUsername(), password, dc.Host, dc.Port, dc.DB, dc.Opts), nil |
| 89 |
} |