Skip to content

Commit 8706b2a

Browse files
read aws default creds directly via sdk
1 parent 09b5f2d commit 8706b2a

File tree

9 files changed

+27
-68
lines changed

9 files changed

+27
-68
lines changed

client/client.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,13 @@ func (c *Client) GetSecretList(_ SecretListConfig) ([]SecretObject, error) {
107107
return objs, nil
108108
}
109109

110-
func getStore(ctx context.Context, cfg config.Client) (storeI, error) {
111-
var store storeI
110+
func getStore(ctx context.Context, cfg config.Client) (clouldStore, error) {
111+
var store clouldStore
112112
var err error
113113
switch cfg.Provider {
114114
case config.AWS:
115115
{
116-
acfg := aws.Config{ServiceAccountFile: cfg.Cloud.ApplicationCredentials}
117-
store, err = aws.NewStore(ctx, acfg)
116+
store, err = aws.NewStore(ctx)
118117
if err != nil {
119118
return nil, err
120119
}

client/cloud_service.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ import (
1010

1111
"github.com/rs/zerolog/log"
1212
"github.com/scalescape/dolores/config"
13-
cloud "github.com/scalescape/dolores/store/cld"
13+
"github.com/scalescape/dolores/store/cloud"
1414
)
1515

1616
var ErrInvalidPublicKeys = errors.New("invalid public keys")
1717

1818
const metadataFile = "dolores.md"
1919

2020
type Service struct {
21-
store storeI
21+
store clouldStore
2222
}
2323

24-
type storeI interface {
24+
type clouldStore interface {
2525
WriteToObject(ctx context.Context, bucketName, fileName string, data []byte) error
2626
ReadObject(ctx context.Context, bucketName, fileName string) ([]byte, error)
2727
ListObject(ctx context.Context, bucketName, path string) ([]cloud.Object, error)
@@ -146,6 +146,6 @@ func (s Service) ListObject(ctx context.Context, bucket, path string) ([]cloud.O
146146
return resp, nil
147147
}
148148

149-
func NewService(st storeI) Service {
149+
func NewService(st clouldStore) Service {
150150
return Service{store: st}
151151
}

client/service_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
"github.com/scalescape/dolores/client"
99
"github.com/scalescape/dolores/config"
10-
cloud "github.com/scalescape/dolores/store/cld"
10+
"github.com/scalescape/dolores/store/cloud"
1111
"github.com/stretchr/testify/mock"
1212
"github.com/stretchr/testify/require"
1313
"github.com/stretchr/testify/suite"
@@ -69,7 +69,8 @@ func (s *serviceSuite) TestShouldNotOverwriteMetadata() {
6969
cfg := client.Configuration{
7070
PublicKey: "public_key",
7171
Metadata: config.Metadata{Location: "secrets"},
72-
UserID: "test_user"}
72+
UserID: "test_user",
73+
}
7374
s.gcs.On("ExistsObject", mock.AnythingOfType("context.backgroundCtx"), s.bucket, name).Return(true, nil).Once()
7475
s.gcs.On("WriteToObject", mock.AnythingOfType("context.backgroundCtx"), s.bucket, "secrets/keys/test_user.key", []byte(cfg.PublicKey)).Return(nil).Once()
7576

cmd/dolores/init.go

+2-19
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,8 @@ func (c *InitCommand) getCred(res *Input) error {
8787
}
8888
case config.AWS:
8989
{
90-
credFile := os.Getenv("AWS_APPLICATION_CREDENTIALS")
91-
if credFile != "" {
92-
qs = append(qs, &survey.Question{
93-
Name: "creds",
94-
Validate: survey.Required,
95-
Prompt: &survey.Select{
96-
Message: "Use AWS_APPLICATION_CREDENTIALS env as credentials file",
97-
Options: []string{credFile},
98-
},
99-
})
100-
} else {
101-
qs = append(qs, &survey.Question{
102-
Name: "creds",
103-
Prompt: &survey.Input{
104-
Message: "Enter aws service account file path",
105-
},
106-
Validate: survey.Required,
107-
})
108-
}
90+
res.ApplicationCredentials = "aws_default"
91+
return nil
10992
}
11093
}
11194

go.mod

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ require (
77
filippo.io/age v1.1.1
88
github.com/AlecAivazis/survey/v2 v2.3.7
99
github.com/aws/aws-sdk-go-v2/config v1.23.0
10-
github.com/aws/aws-sdk-go-v2/credentials v1.15.2
1110
github.com/aws/aws-sdk-go-v2/service/s3 v1.42.1
1211
github.com/kelseyhightower/envconfig v1.4.0
1312
github.com/rs/zerolog v1.29.1
@@ -17,13 +16,14 @@ require (
1716
google.golang.org/api v0.129.0
1817
)
1918

19+
require github.com/aws/aws-sdk-go-v2/credentials v1.15.2 // indirect
20+
2021
require (
2122
cloud.google.com/go v0.110.0 // indirect
2223
cloud.google.com/go/compute v1.19.3 // indirect
2324
cloud.google.com/go/compute/metadata v0.2.3 // indirect
2425
cloud.google.com/go/iam v0.13.0 // indirect
25-
github.com/aws/aws-sdk-go v1.47.9
26-
github.com/aws/aws-sdk-go-v2 v1.22.2 // indirect
26+
github.com/aws/aws-sdk-go-v2 v1.22.2
2727
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.0 // indirect
2828
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.3 // indirect
2929
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.2 // indirect

go.sum

-3
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF
5555
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
5656
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
5757
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
58-
github.com/aws/aws-sdk-go v1.47.9 h1:rarTsos0mA16q+huicGx0e560aYRtOucV5z2Mw23JRY=
59-
github.com/aws/aws-sdk-go v1.47.9/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
6058
github.com/aws/aws-sdk-go-v2 v1.22.2 h1:lV0U8fnhAnPz8YcdmZVV60+tr6CakHzqA6P8T46ExJI=
6159
github.com/aws/aws-sdk-go-v2 v1.22.2/go.mod h1:Kd0OJtkW3Q0M0lUWGszapWjEvrXDzRW+D21JNsroB+c=
6260
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.0 h1:hHgLiIrTRtddC0AKcJr5s7i/hLgcpTt+q/FKxf1Zayk=
@@ -219,7 +217,6 @@ github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u
219217
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68=
220218
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
221219
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
222-
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
223220
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
224221
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
225222
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=

store/aws/aws.go

+11-32
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@ package aws
33
import (
44
"bytes"
55
"context"
6-
"encoding/json"
76
"errors"
87
"fmt"
98
"io"
10-
"os"
119

10+
"github.com/aws/aws-sdk-go-v2/aws"
1211
"github.com/aws/aws-sdk-go-v2/config"
13-
"github.com/aws/aws-sdk-go-v2/credentials"
1412
"github.com/aws/aws-sdk-go-v2/service/s3"
1513
"github.com/aws/aws-sdk-go-v2/service/s3/types"
16-
"github.com/aws/aws-sdk-go/aws"
1714
"github.com/rs/zerolog/log"
18-
cloud "github.com/scalescape/dolores/store/cld"
15+
"github.com/scalescape/dolores/store/cloud"
1916
)
2017

2118
var ErrInvalidServiceAccount = errors.New("invalid service account")
@@ -25,16 +22,6 @@ type StorageClient struct {
2522
region string
2623
}
2724

28-
type Config struct {
29-
ServiceAccountFile string
30-
}
31-
32-
type ServiceAccount struct {
33-
AccessKeyID string `json:"accessKey"`
34-
SecretAccessKey string `json:"secretKey"`
35-
Region string `json:"region"`
36-
}
37-
3825
func (s StorageClient) bucketExists(ctx context.Context, bucketName string) (bool, error) {
3926
_, err := s.client.HeadBucket(ctx, &s3.HeadBucketInput{
4027
Bucket: aws.String(bucketName),
@@ -51,8 +38,10 @@ func (s StorageClient) bucketExists(ctx context.Context, bucketName string) (boo
5138
func (s StorageClient) CreateBucket(ctx context.Context, bucketName string) error {
5239
lconst := types.BucketLocationConstraint(s.region)
5340
cbCfg := &types.CreateBucketConfiguration{LocationConstraint: lconst}
54-
bucket := &s3.CreateBucketInput{Bucket: aws.String(bucketName),
55-
CreateBucketConfiguration: cbCfg}
41+
bucket := &s3.CreateBucketInput{
42+
Bucket: aws.String(bucketName),
43+
CreateBucketConfiguration: cbCfg,
44+
}
5645
_, err := s.client.CreateBucket(ctx, bucket)
5746
var existsErr *types.BucketAlreadyOwnedByYou = new(types.BucketAlreadyOwnedByYou)
5847
if errors.As(err, &existsErr) {
@@ -114,7 +103,6 @@ func (s StorageClient) ReadObject(ctx context.Context, bucketName, fileName stri
114103
Bucket: aws.String(bucketName),
115104
Key: aws.String(fileName),
116105
})
117-
118106
if err != nil {
119107
return nil, fmt.Errorf("failed to read object : %w", err)
120108
}
@@ -135,28 +123,19 @@ func (s StorageClient) ExistsObject(ctx context.Context, bucketName, fileName st
135123
var notFoundType *types.NoSuchKey
136124
if errors.As(err, &notFoundType) {
137125
return false, nil
138-
} else {
139-
return false, err
140126
}
127+
return false, err
141128
}
142129

143130
return true, nil
144131
}
145132

146-
func NewStore(ctx context.Context, acfg Config) (StorageClient, error) {
147-
data, err := os.ReadFile(acfg.ServiceAccountFile)
148-
if err != nil {
149-
return StorageClient{}, fmt.Errorf("failed to read service account file with error %v %w", err, ErrInvalidServiceAccount)
150-
}
151-
sa := new(ServiceAccount)
152-
if err := json.Unmarshal(data, sa); err != nil {
153-
return StorageClient{}, fmt.Errorf("unable to parse service account file: %w", err)
154-
}
155-
cp := credentials.NewStaticCredentialsProvider(sa.AccessKeyID, sa.SecretAccessKey, "")
156-
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(sa.Region), config.WithCredentialsProvider(cp))
133+
func NewStore(ctx context.Context) (StorageClient, error) {
134+
cfg, err := config.LoadDefaultConfig(ctx)
157135
if err != nil {
158136
return StorageClient{}, err
159137
}
138+
160139
cli := s3.NewFromConfig(cfg)
161-
return StorageClient{client: cli, region: sa.Region}, nil
140+
return StorageClient{client: cli, region: cfg.Region}, nil
162141
}
File renamed without changes.

store/google/gcs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
"cloud.google.com/go/storage"
1212
"github.com/rs/zerolog/log"
13-
cloud "github.com/scalescape/dolores/store/cld"
13+
"github.com/scalescape/dolores/store/cloud"
1414
"google.golang.org/api/iterator"
1515
"google.golang.org/api/option"
1616
)

0 commit comments

Comments
 (0)