/
source_s3.go
66 lines (56 loc) · 1.55 KB
/
source_s3.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
package credentials
import (
"fmt"
"io"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
)
// AWSS3Source represents s3 objects containing credentials
type AWSS3Source struct {
Bucket string
Key string
client s3iface.S3API
}
func (source *AWSS3Source) getClient() s3iface.S3API {
if source.client == nil {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
AssumeRoleTokenProvider: stscreds.StdinTokenProvider,
}))
source.client = s3.New(sess)
}
return source.client
}
// Credentials extracts credentials from the source
func (source *AWSS3Source) Credentials() ([]Credentials, error) {
client := source.getClient()
response, err := client.GetObject(&s3.GetObjectInput{
Bucket: aws.String(source.Bucket),
Key: aws.String(source.Key),
})
if err != nil {
return nil, err
}
body, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
return getCredentialsFromBytes(body)
}
// Type returns the type of the source
func (source *AWSS3Source) Type() string {
return "Amazon S3"
}
// ValidateConfiguration verifies that the source's attributes are valid
func (source *AWSS3Source) ValidateConfiguration() error {
if source.Bucket == "" {
return fmt.Errorf("S3 sources must define a bucket")
}
if source.Key == "" {
return fmt.Errorf("S3 sources must define a key")
}
return nil
}