-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3.go
164 lines (135 loc) · 3.9 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"net/http"
"os"
"reflect"
"strings"
"sync"
"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"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
log "github.com/sirupsen/logrus"
)
type s3Backend struct {
Client *s3.S3
Uploader *s3manager.Uploader
Bucket string
}
// S3Config stores information about the S3 storage backend
type S3Config struct {
URL string
Port int
AccessKey string
SecretKey string
Bucket string
Region string
Chunksize int
Cacert string
}
func newS3Backend(config S3Config) (*s3Backend, error) {
log.Info("Start initializing the S3 backend")
s3Transport := transportConfigS3(config)
client := http.Client{Transport: s3Transport}
s3Session := session.Must(session.NewSession(
&aws.Config{
Endpoint: aws.String(fmt.Sprintf("%s:%d", config.URL, config.Port)),
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 {
log.Error("Unexpected issue while creating bucket", err)
}
}
}
sb := &s3Backend{
Bucket: config.Bucket,
Uploader: s3manager.NewUploader(s3Session, func(u *s3manager.Uploader) {
u.LeavePartsOnError = false
}),
Client: s3.New(s3Session)}
_, err = sb.Client.ListObjectsV2(&s3.ListObjectsV2Input{Bucket: &config.Bucket})
if err != nil {
return nil, err
}
return sb, nil
}
// NewFileReader returns an io.Reader instance
func (sb *s3Backend) NewFileReader(filePath string) (io.ReadCloser, error) {
if sb == nil {
return nil, fmt.Errorf("Invalid s3Backend")
}
r, err := sb.Client.GetObject(&s3.GetObjectInput{
Bucket: aws.String(sb.Bucket),
Key: aws.String(filePath),
})
if err != nil {
log.Error("Failed to retrieve s3 content")
return nil, err
}
log.Infof("Retrieved backup content len: %v", *r.ContentLength)
return r.Body, nil
}
// NewFileWriter uploads the contents of an io.Reader to a S3 bucket
func (sb *s3Backend) NewFileWriter(filePath string, wg *sync.WaitGroup) (io.WriteCloser, error) {
if sb == nil {
return nil, fmt.Errorf("Invalid s3Backend")
}
reader, writer := io.Pipe()
wg.Add(1)
go func() {
defer wg.Done()
_, err := sb.Uploader.Upload(&s3manager.UploadInput{
Body: reader,
Bucket: aws.String(sb.Bucket),
Key: aws.String(filePath),
ContentEncoding: aws.String("application/octet-stream"),
})
if err != nil {
_ = reader.CloseWithError(err)
}
}()
return writer, 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
}