-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathcopyfile.go
216 lines (177 loc) · 6.2 KB
/
copyfile.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
208
209
210
211
212
213
214
215
216
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package assets
import (
"bytes"
"fmt"
"net/url"
"os"
"strings"
"k8s.io/klog/v2"
"k8s.io/kops/pkg/acls"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/util/pkg/hashing"
"k8s.io/kops/util/pkg/vfs"
)
// CopyFile copies an from a source file repository, to a target repository,
// typically used for highly secure clusters.
type CopyFile struct {
Name string
SourceFile string
TargetFile string
SHA string
Cluster *kops.Cluster
}
// fileExtensionForSHA returns the expected extension for the given hash
// If the hash length is not recognized, it returns an error.
func fileExtensionForSHA(sha string) (string, error) {
switch len(sha) {
case 40:
return ".sha1", nil
case 64:
return ".sha256", nil
default:
return "", fmt.Errorf("unhandled sha length for %q", sha)
}
}
func (e *CopyFile) Run() error {
expectedSHA := strings.TrimSpace(e.SHA)
shaExtension, err := fileExtensionForSHA(expectedSHA)
if err != nil {
return err
}
targetSHAFile := e.TargetFile + shaExtension
targetSHABytes, err := vfs.Context.ReadFile(targetSHAFile)
if err != nil {
if os.IsNotExist(err) {
klog.V(4).Infof("unable to download: %q, assuming target file is not present, and if not present may not be an error: %v",
targetSHAFile, err)
} else {
klog.V(4).Infof("unable to download: %q, %v", targetSHAFile, err)
}
} else {
targetSHA := string(targetSHABytes)
if strings.TrimSpace(targetSHA) == expectedSHA {
klog.V(8).Infof("found matching target sha for file: %q", e.TargetFile)
return nil
}
klog.V(8).Infof("did not find same file, found mismatching target sha1 for file: %q", e.TargetFile)
}
source := e.SourceFile
target := e.TargetFile
sourceSha := e.SHA
klog.V(2).Infof("copying bits from %q to %q", source, target)
if err := transferFile(e.Cluster, source, target, sourceSha); err != nil {
return fmt.Errorf("unable to transfer %q to %q: %v", source, target, err)
}
return nil
}
// transferFile downloads a file from the source location, validates the file matches the SHA,
// and uploads the file to the target location.
func transferFile(cluster *kops.Cluster, source string, target string, sha string) error {
// TODO drop file to disk, as vfs reads file into memory. We load kubelet into memory for instance.
// TODO in s3 can we do a copy file ... would need to test
data, err := vfs.Context.ReadFile(source)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("file not found %q: %v", source, err)
}
return fmt.Errorf("error downloading file %q: %v", source, err)
}
objectStore, err := buildVFSPath(target)
if err != nil {
return err
}
uploadVFS, err := vfs.Context.BuildVfsPath(objectStore)
if err != nil {
return fmt.Errorf("error building path %q: %v", objectStore, err)
}
shaExtension, err := fileExtensionForSHA(sha)
if err != nil {
return err
}
shaTarget := objectStore + shaExtension
shaVFS, err := vfs.Context.BuildVfsPath(shaTarget)
if err != nil {
return fmt.Errorf("error building path %q: %v", shaTarget, err)
}
shaHash, err := hashing.FromString(strings.TrimSpace(sha))
if err != nil {
return fmt.Errorf("unable to parse sha: %q, %v", sha, err)
}
in := bytes.NewReader(data)
dataHash, err := shaHash.Algorithm.Hash(in)
if err != nil {
return fmt.Errorf("unable to hash file %q downloaded: %v", source, err)
}
if !shaHash.Equal(dataHash) {
return fmt.Errorf("the sha value in %q does not match %q calculated value %q", shaTarget, source, dataHash.String())
}
klog.Infof("uploading %q to %q", source, objectStore)
if err := writeFile(cluster, uploadVFS, data); err != nil {
return err
}
b := []byte(shaHash.Hex())
if err := writeFile(cluster, shaVFS, b); err != nil {
return err
}
return nil
}
func writeFile(cluster *kops.Cluster, p vfs.Path, data []byte) error {
acl, err := acls.GetACL(p, cluster)
if err != nil {
return err
}
if err = p.WriteFile(bytes.NewReader(data), acl); err != nil {
return fmt.Errorf("error writing path %v: %v", p, err)
}
return nil
}
// buildVFSPath task a recognizable https url and transforms that URL into the equivalent url with the object
// store prefix.
func buildVFSPath(target string) (string, error) {
if !strings.Contains(target, "://") || strings.HasPrefix(target, "memfs://") {
return target, nil
}
var vfsPath string
// Matches all S3 regional naming conventions:
// https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
// and converts to a s3://<bucket>/<path> vfsPath
s3VfsPath, err := vfs.VFSPath(target)
if err == nil {
vfsPath = s3VfsPath
} else {
// These matches only cover a subset of the URLs that you can use, but I am uncertain how to cover more of the possible
// options.
// This code parses the HOST and determines gs URLs.
// For instance you can have the bucket name in the gs url hostname.
u, err := url.Parse(target)
if err != nil {
return "", fmt.Errorf("Unable to parse Google Cloud Storage URL: %q", target)
}
if u.Host == "storage.googleapis.com" {
vfsPath = "gs:/" + u.Path
}
}
if vfsPath == "" {
klog.Errorf("Unable to determine VFS path from supplied URL: %s", target)
klog.Errorf("S3, Google Cloud Storage, and File Paths are supported.")
klog.Errorf("For S3, please make sure that the supplied file repository URL adhere to S3 naming conventions, https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region.")
klog.Errorf("For GCS, please make sure that the supplied file repository URL adheres to https://storage.googleapis.com/")
if err != nil { // print the S3 error for more details
return "", fmt.Errorf("Error Details: %v", err)
}
return "", fmt.Errorf("unable to determine vfs type for %q", target)
}
return vfsPath, nil
}