| 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 cmd |
| 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/aws/aws-sdk-go-v2/credentials" |
| 25 |
"github.com/in-toto/archivista/pkg/metadatastorage/sqlstore" |
| 26 |
"github.com/spf13/cobra" |
| 27 |
) |
| 28 |
|
| 29 |
var iamCmd = &cobra.Command{ |
| 30 |
Use: "iam [sql backend] [connection string]", |
| 31 |
Short: "Converts a connection string to use AWS RDS IAM authentication", |
| 32 |
Long: `If the sql backend ends with _RDS_IAM, an IAM authentication token is added to the connection string.`, |
| 33 |
Example: `archivistactl iam PSQL_RDS_IAM "postgres://user@host:3306/dbname"`, |
| 34 |
Args: cobra.ExactArgs(2), |
| 35 |
ValidArgs: []string{"MYSQL", "MYSQL_RDS_IAM", "PSQL", "PSQL_RDS_IAM"}, |
| 36 |
Annotations: map[string]string{"help:args": "sql backend and connection string"}, |
| 37 |
SilenceUsage: true, |
| 38 |
DisableFlagsInUseLine: true, |
| 39 |
RunE: func(cmd *cobra.Command, args []string) error { |
| 40 |
sqlBackend := strings.ToUpper(args[0]) |
| 41 |
connectionString := args[1] |
| 42 |
dryrun, _ := cmd.Flags().GetBool("dryrun") |
| 43 |
|
| 44 |
if strings.HasSuffix(sqlBackend, "_RDS_IAM") { |
| 45 |
if dryrun { |
| 46 |
sqlstore.AwsConfigAPI = &dryrunConfig{ |
| 47 |
cfg: aws.Config{ |
| 48 |
Region: "us-east-1", |
| 49 |
Credentials: credentials.NewStaticCredentialsProvider("foo", "bar", "baz"), |
| 50 |
}, |
| 51 |
} |
| 52 |
} |
| 53 |
revisedConnectionString, err := sqlstore.RewriteConnectionStringForIAM(sqlBackend, connectionString, dryrun) |
| 54 |
if err != nil { |
| 55 |
return err |
| 56 |
} |
| 57 |
fmt.Println(revisedConnectionString) |
| 58 |
} else { |
| 59 |
fmt.Println(connectionString) |
| 60 |
} |
| 61 |
|
| 62 |
return nil |
| 63 |
}, |
| 64 |
} |
| 65 |
|
| 66 |
func init() { |
| 67 |
rootCmd.AddCommand(iamCmd) |
| 68 |
iamCmd.Flags().Bool("dryrun", false, "Shows the result using a fake authentication token 'authtoken'") |
| 69 |
} |
| 70 |
|
| 71 |
type dryrunConfig struct { |
| 72 |
cfg aws.Config |
| 73 |
} |
| 74 |
|
| 75 |
func (m *dryrunConfig) LoadDefaultConfig(ctx context.Context, optFns ...func(*config.LoadOptions) error) (aws.Config, error) { |
| 76 |
return m.cfg, nil |
| 77 |
} |