/
config.go
100 lines (82 loc) · 2.59 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
91
92
93
94
95
96
97
98
99
100
package s3configurer
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"github.com/anthonywittig/jit-ssh/internal/config"
"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
type S3Configurer struct {
s3 *s3.Client
}
func New(ctx context.Context) (*S3Configurer, error) {
lc, err := getLocalConfig()
if err != nil {
return nil, errors.New(fmt.Sprintf("error parsing local config: %s", err.Error()))
}
s3, err := s3Client(ctx, lc)
if err != nil {
return nil, errors.New(fmt.Sprintf("error getting s3 client: %s", err.Error()))
}
return &S3Configurer{
s3: s3,
}, nil
}
func (c *S3Configurer) GetConfig(ctx context.Context) (config.Config, error) {
local, err := getLocalConfig()
if err != nil {
return config.Config{}, errors.New(fmt.Sprintf("error getting local config: %s", err.Error()))
}
remote, err := c.getRemoteConfig(ctx, local)
if err != nil {
return config.Config{}, errors.New(fmt.Sprintf("error getting remote config: %s", err.Error()))
}
conf := config.Config{
Local: local,
Remote: remote,
}
log.Printf("config is: %+v", conf)
return conf, nil
}
func (c *S3Configurer) getRemoteConfig(ctx context.Context, local config.Local) (config.Remote, error) {
out, err := c.s3.GetObject(ctx, &s3.GetObjectInput{
Bucket: aws.String(local.AWS.S3.Bucket),
Key: aws.String(local.AWS.S3.Key),
})
if err != nil {
return config.Remote{}, errors.New(fmt.Sprintf("error getting s3 object: %s", err.Error()))
}
rc := config.Remote{}
if err := json.NewDecoder(out.Body).Decode(&rc); err != nil {
return config.Remote{}, errors.New(fmt.Sprintf("error decoding s3 object: %s", err.Error()))
}
return rc, nil
}
func getLocalConfig() (config.Local, error) {
localConfig, err := ioutil.ReadFile(".env.json")
if err != nil {
return config.Local{}, errors.New(fmt.Sprintf("error reading file: %s", err.Error()))
}
lc := config.Local{}
if err := json.Unmarshal(localConfig, &lc); err != nil {
return config.Local{}, errors.New(fmt.Sprintf("error unmarshalling local config: %s", err.Error()))
}
return lc, nil
}
func s3Client(ctx context.Context, lc config.Local) (*s3.Client, error) {
region := awsconfig.WithRegion(lc.AWS.Region)
cfg, err := awsconfig.LoadDefaultConfig(ctx, region)
if lc.AWS.Profile != "" {
cfg, err = awsconfig.LoadDefaultConfig(ctx, region, awsconfig.WithSharedConfigProfile(lc.AWS.Profile))
}
if err != nil {
return nil, errors.New(fmt.Sprintf("error getting aws config: %s", err.Error()))
}
client := s3.NewFromConfig(cfg)
return client, nil
}