forked from buildkite/agent
/
s3.go
76 lines (62 loc) · 2.01 KB
/
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
67
68
69
70
71
72
73
74
75
76
package agent
import (
"errors"
"os"
"time"
"github.com/AdRoll/goamz/aws"
)
func awsS3Auth() (aws.Auth, error) {
// First try to authenticate using the BUILDKITE_ ENV variables
buildkiteAuth, buildkiteErr := buildkiteS3EnvAuth()
if buildkiteErr == nil {
return buildkiteAuth, nil
}
// Passing blank values here instructs the AWS library to look at the
// current instances meta data for the security credentials.
awsAuth, awsErr := aws.GetAuth("", "", "", time.Time{})
if awsErr == nil {
return awsAuth, nil
}
var err error
// If they attempted to use the BUILDKITE_ ENV variables, return them
// that error, otherwise default to the error from AWS
if buildkiteErr != nil && buildkiteAuth.AccessKey != "" || buildkiteAuth.SecretKey != "" {
err = buildkiteErr
} else {
err = awsErr
}
return aws.Auth{}, err
}
func buildkiteS3EnvAuth() (auth aws.Auth, err error) {
auth.AccessKey = os.Getenv("BUILDKITE_S3_ACCESS_KEY_ID")
if auth.AccessKey == "" {
auth.AccessKey = os.Getenv("BUILDKITE_S3_ACCESS_KEY")
}
auth.SecretKey = os.Getenv("BUILDKITE_S3_SECRET_ACCESS_KEY")
if auth.SecretKey == "" {
auth.SecretKey = os.Getenv("BUILDKITE_S3_SECRET_KEY")
}
if auth.AccessKey == "" {
err = errors.New("BUILDKITE_S3_ACCESS_KEY_ID or BUILDKITE_S3_ACCESS_KEY not found in environment")
}
if auth.SecretKey == "" {
err = errors.New("BUILDKITE_S3_SECRET_ACCESS_KEY or BUILDKITE_S3_SECRET_KEY not found in environment")
}
return
}
func awsS3Region() (region aws.Region, err error) {
regionName := "us-east-1"
if os.Getenv("BUILDKITE_S3_DEFAULT_REGION") != "" {
regionName = os.Getenv("BUILDKITE_S3_DEFAULT_REGION")
} else if os.Getenv("AWS_DEFAULT_REGION") != "" {
regionName = os.Getenv("AWS_DEFAULT_REGION")
}
// Check to make sure the region exists. There is a GetRegion API, but
// there doesn't seem to be a way to make it error out if the region
// doesn't exist.
region, ok := aws.Regions[regionName]
if ok == false {
err = errors.New("Unknown AWS S3 Region `" + regionName + "`")
}
return
}