| 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 |
"strconv" |
| 21 |
"strings" |
| 22 |
|
| 23 |
"github.com/go-sql-driver/mysql" |
| 24 |
"github.com/jackc/pgx/v5" |
| 25 |
"github.com/jkjell/go-db-credential-refresh/driver" |
| 26 |
"github.com/jkjell/go-db-credential-refresh/store/awsrds" |
| 27 |
) |
| 28 |
|
| 29 |
func ConfigFromPostgres(connectionString string) (c *driver.Config, user, password string, err error) { |
| 30 |
dc := driver.Config{ |
| 31 |
Retries: 3, |
| 32 |
} |
| 33 |
dc.Formatter = driver.PgFormatter |
| 34 |
|
| 35 |
dbConfig, err := pgx.ParseConfig(connectionString) |
| 36 |
if err != nil { |
| 37 |
return nil, "", "", fmt.Errorf("could not parse postgresql connection string: %w", err) |
| 38 |
} |
| 39 |
|
| 40 |
dc.Host = dbConfig.Host |
| 41 |
dc.Port = int(dbConfig.Port) |
| 42 |
dc.DB = dbConfig.Database |
| 43 |
dc.Opts = dbConfig.RuntimeParams |
| 44 |
if dc.Opts == nil { |
| 45 |
dc.Opts = make(map[string]string) |
| 46 |
} |
| 47 |
// ParseConfig truncates sslmode param |
| 48 |
if dbConfig.TLSConfig == nil { |
| 49 |
dc.Opts["sslmode"] = "disable" |
| 50 |
} |
| 51 |
|
| 52 |
if dbConfig.User == "" { |
| 53 |
return nil, "", "", fmt.Errorf("connection string is missing a user") |
| 54 |
} |
| 55 |
|
| 56 |
return &dc, dbConfig.User, dbConfig.Password, nil |
| 57 |
} |
| 58 |
|
| 59 |
func ConfigFromMySQL(connectionString string) (c *driver.Config, user, password string, err error) { |
| 60 |
dc := driver.Config{ |
| 61 |
Retries: 3, |
| 62 |
} |
| 63 |
dc.Formatter = driver.MysqlFormatter |
| 64 |
|
| 65 |
dbConfig, err := mysql.ParseDSN(connectionString) |
| 66 |
if err != nil { |
| 67 |
return nil, "", "", fmt.Errorf("parsing connection string: %w", err) |
| 68 |
} |
| 69 |
|
| 70 |
addr := strings.Split(dbConfig.Addr, ":") |
| 71 |
dc.Host = addr[0] |
| 72 |
port, err := strconv.Atoi(addr[1]) |
| 73 |
if err != nil { |
| 74 |
return nil, "", "", fmt.Errorf("could not parse mysql port: %w", err) |
| 75 |
} |
| 76 |
dc.Port = port |
| 77 |
dc.DB = dbConfig.DBName |
| 78 |
|
| 79 |
dc.Opts = dbConfig.Params |
| 80 |
// this tells the go-sql-driver to parse times from mysql to go's time.Time |
| 81 |
// see https://github.com/go-sql-driver/mysql#timetime-support for details |
| 82 |
if dc.Opts == nil { |
| 83 |
dc.Opts = make(map[string]string) |
| 84 |
} |
| 85 |
dc.Opts["parseTime"] = "true" |
| 86 |
|
| 87 |
if dbConfig.User == "" { |
| 88 |
return nil, "", "", fmt.Errorf("connection string is missing a user") |
| 89 |
} |
| 90 |
|
| 91 |
return &dc, dbConfig.User, dbConfig.Passwd, nil |
| 92 |
} |
| 93 |
|
| 94 |
func AWSRDSStoreFromDriverConfig(dc *driver.Config, user string) (driver.Store, error) { |
| 95 |
awsConfig, err := AwsConfigAPI.LoadDefaultConfig(context.Background()) |
| 96 |
if err != nil { |
| 97 |
return nil, fmt.Errorf("problem loading AWS config: %w", err) |
| 98 |
} |
| 99 |
|
| 100 |
rdsEndpoint := fmt.Sprintf("%s:%d", dc.Host, dc.Port) |
| 101 |
config := awsrds.Config{ |
| 102 |
Credentials: awsConfig.Credentials, |
| 103 |
Endpoint: rdsEndpoint, |
| 104 |
User: user, |
| 105 |
Region: awsConfig.Region, |
| 106 |
} |
| 107 |
|
| 108 |
return awsrds.NewStore(&config) |
| 109 |
} |