This repository has been archived by the owner on Mar 26, 2022. It is now read-only.
/
config.go
90 lines (65 loc) · 1.54 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package config
import (
"context"
"errors"
"github.com/aws/aws-sdk-go-v2/aws"
aws_config "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sts/types"
"github.com/go-ini/ini"
_ "log"
"os"
)
type Config struct {
Path string
ini *ini.File
}
func NewConfig() (*Config, error) {
var ini_config *ini.File
var ini_path string
for _, path := range aws_config.DefaultSharedCredentialsFiles {
_, err := os.Stat(path)
if err != nil {
continue
}
i, err := ini.Load(path)
if err != nil {
return nil, err
}
ini_path = path
ini_config = i
break
}
if ini_config == nil {
return nil, errors.New("Unable to load config file")
}
c := Config{
Path: ini_path,
ini: ini_config,
}
return &c, nil
}
func (c *Config) AWSConfigWithProfile(profile string) (aws.Config, error) {
ctx := context.Background()
sect := c.ini.Section("default")
region := sect.Key("region")
return aws_config.LoadDefaultConfig(ctx,
aws_config.WithSharedConfigProfile(profile),
aws_config.WithRegion(region.String()),
)
}
func (c *Config) SetSessionCredentialsWithProfile(profile string, creds *types.Credentials) error {
ini_path := c.Path
ini_config, err := ini.Load(ini_path)
if err != nil {
return err
}
sect := ini_config.Section(profile)
sect.Key("aws_access_key_id").SetValue(*creds.AccessKeyId)
sect.Key("aws_secret_access_key").SetValue(*creds.SecretAccessKey)
sect.Key("aws_session_token").SetValue(*creds.SessionToken)
err = ini_config.SaveTo(ini_path)
if err != nil {
return err
}
return nil
}