-
Notifications
You must be signed in to change notification settings - Fork 250
/
s3-datasource.go
207 lines (179 loc) · 5.49 KB
/
s3-datasource.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package importer
import (
"fmt"
"io"
"net/url"
"path/filepath"
"regexp"
"strings"
"github.com/aws/aws-sdk-go/aws"
"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/pkg/errors"
"k8s.io/klog/v2"
"kubevirt.io/containerized-data-importer/pkg/util"
)
const (
s3FolderSep = "/"
httpScheme = "http"
)
// S3Client is the interface to the used S3 client.
type S3Client interface {
GetObject(input *s3.GetObjectInput) (*s3.GetObjectOutput, error)
}
// may be overridden in tests
var newClientFunc = getS3Client
// S3DataSource is the struct containing the information needed to import from an S3 data source.
// Sequence of phases:
// 1. Info -> Transfer
// 2. Transfer -> Convert
type S3DataSource struct {
// S3 end point
ep *url.URL
// User name
accessKey string
// Password
secKey string
// Reader
s3Reader io.ReadCloser
// stack of readers
readers *FormatReaders
// The image file in scratch space.
url *url.URL
}
// NewS3DataSource creates a new instance of the S3DataSource
func NewS3DataSource(endpoint, accessKey, secKey string, certDir string) (*S3DataSource, error) {
ep, err := ParseEndpoint(endpoint)
if err != nil {
return nil, errors.Wrapf(err, fmt.Sprintf("unable to parse endpoint %q", endpoint))
}
s3Reader, err := createS3Reader(ep, accessKey, secKey, certDir)
if err != nil {
return nil, err
}
return &S3DataSource{
ep: ep,
accessKey: accessKey,
secKey: secKey,
s3Reader: s3Reader,
}, nil
}
// Info is called to get initial information about the data.
func (sd *S3DataSource) Info() (ProcessingPhase, error) {
var err error
sd.readers, err = NewFormatReaders(sd.s3Reader, uint64(0))
if err != nil {
klog.Errorf("Error creating readers: %v", err)
return ProcessingPhaseError, err
}
if !sd.readers.Convert {
// Downloading a raw file, we can write that directly to the target.
return ProcessingPhaseTransferDataFile, nil
}
return ProcessingPhaseTransferScratch, nil
}
// Transfer is called to transfer the data from the source to a temporary location.
func (sd *S3DataSource) Transfer(path string) (ProcessingPhase, error) {
size, _ := util.GetAvailableSpace(path)
if size <= int64(0) {
//Path provided is invalid.
return ProcessingPhaseError, ErrInvalidPath
}
file := filepath.Join(path, tempFile)
err := util.StreamDataToFile(sd.readers.TopReader(), file)
if err != nil {
return ProcessingPhaseError, err
}
// If streaming succeeded, then parsing the file into URL will also succeed, no need to check error status
sd.url, _ = url.Parse(file)
return ProcessingPhaseConvert, nil
}
// TransferFile is called to transfer the data from the source to the passed in file.
func (sd *S3DataSource) TransferFile(fileName string) (ProcessingPhase, error) {
err := util.StreamDataToFile(sd.readers.TopReader(), fileName)
if err != nil {
return ProcessingPhaseError, err
}
return ProcessingPhaseResize, nil
}
// GetURL returns the url that the data processor can use when converting the data.
func (sd *S3DataSource) GetURL() *url.URL {
return sd.url
}
// Close closes any readers or other open resources.
func (sd *S3DataSource) Close() error {
var err error
if sd.readers != nil {
err = sd.readers.Close()
}
return err
}
func createS3Reader(ep *url.URL, accessKey, secKey string, certDir string) (io.ReadCloser, error) {
klog.V(3).Infoln("Using S3 client to get data")
endpoint := ep.Host
urlScheme := ep.Scheme
klog.Infof("Endpoint %s", endpoint)
path := strings.Trim(ep.Path, "/")
bucket, object := extractBucketAndObject(path)
klog.V(1).Infof("bucket %s", bucket)
klog.V(1).Infof("object %s", object)
svc, err := newClientFunc(endpoint, accessKey, secKey, certDir, urlScheme)
if err != nil {
return nil, errors.Wrapf(err, "could not build s3 client for %q", ep.Host)
}
objInput := &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(object),
}
objOutput, err := svc.GetObject(objInput)
if err != nil {
return nil, errors.Wrapf(err, "could not get s3 object: \"%s/%s\"", bucket, object)
}
objectReader := objOutput.Body
return objectReader, nil
}
func getS3Client(endpoint, accessKey, secKey string, certDir string, urlScheme string) (S3Client, error) {
// Adding certs using CustomCABundle will overwrite the SystemCerts, so we opt by creating a custom HTTPClient
httpClient, err := createHTTPClient(certDir)
if err != nil {
return nil, errors.Wrap(err, "Error creating http client for s3")
}
creds := credentials.NewStaticCredentials(accessKey, secKey, "")
region := extractRegion(endpoint)
disableSSL := false
// Disable SSL for http endpoint. This should cause the s3 client to create http requests.
if urlScheme == httpScheme {
disableSSL = true
}
sess, err := session.NewSession(&aws.Config{
Region: aws.String(region),
Endpoint: aws.String(endpoint),
Credentials: creds,
S3ForcePathStyle: aws.Bool(true),
HTTPClient: httpClient,
DisableSSL: &disableSSL,
},
)
if err != nil {
return nil, err
}
svc := s3.New(sess)
return svc, nil
}
func extractRegion(s string) string {
var region string
r, _ := regexp.Compile("s3\\.(.+)\\.amazonaws\\.com")
if matches := r.FindStringSubmatch(s); matches != nil {
region = matches[1]
} else {
region = strings.Split(s, ".")[0]
}
return region
}
func extractBucketAndObject(s string) (string, string) {
pathSplit := strings.Split(s, s3FolderSep)
bucket := pathSplit[0]
object := strings.Join(pathSplit[1:], s3FolderSep)
return bucket, object
}