This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket.go
84 lines (70 loc) · 2.16 KB
/
bucket.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
package main
import (
"crypto/tls"
"crypto/x509"
"net/http"
"os"
"reflect"
"strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
func checkS3Bucket(config S3Config) error {
s3Transport := transportConfigS3(config)
client := http.Client{Transport: s3Transport}
s3Session := session.Must(session.NewSession(
&aws.Config{
Endpoint: aws.String(config.url),
Region: aws.String(config.region),
HTTPClient: &client,
S3ForcePathStyle: aws.Bool(true),
DisableSSL: aws.Bool(strings.HasPrefix(config.url, "http:")),
Credentials: credentials.NewStaticCredentials(config.accessKey, config.secretKey, ""),
},
))
_, err := s3.New(s3Session).CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String(config.bucket),
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
if aerr.Code() != s3.ErrCodeBucketAlreadyOwnedByYou &&
aerr.Code() != s3.ErrCodeBucketAlreadyExists {
return errors.Errorf("Unexpected issue while creating bucket: %v", err)
}
return nil
}
return errors.New("Verifying bucket failed, check S3 configuration")
}
return nil
}
// transportConfigS3 is a helper method to setup TLS for the S3 client.
func transportConfigS3(config S3Config) http.RoundTripper {
cfg := new(tls.Config)
// Enforce TLS1.2 or higher
cfg.MinVersion = 2
// Read system CAs
var systemCAs, _ = x509.SystemCertPool()
if reflect.DeepEqual(systemCAs, x509.NewCertPool()) {
log.Debug("creating new CApool")
systemCAs = x509.NewCertPool()
}
cfg.RootCAs = systemCAs
if config.cacert != "" {
cacert, e := os.ReadFile(config.cacert) // #nosec this file comes from our config
if e != nil {
log.Fatalf("failed to append %q to RootCAs: %v", cacert, e)
}
if ok := cfg.RootCAs.AppendCertsFromPEM(cacert); !ok {
log.Debug("no certs appended, using system certs only")
}
}
var trConfig http.RoundTripper = &http.Transport{
TLSClientConfig: cfg,
ForceAttemptHTTP2: true}
return trConfig
}