-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathutil.go
391 lines (352 loc) · 15 KB
/
util.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package server
import (
"archive/tar"
"archive/zip"
"bufio"
"bytes"
"compress/gzip"
"context"
"encoding/json"
"io"
"io/ioutil"
"net/url"
"strconv"
"strings"
"github.com/golang/glog"
api "github.com/kubeflow/pipelines/backend/api/go_client"
"github.com/kubeflow/pipelines/backend/src/apiserver/common"
"github.com/kubeflow/pipelines/backend/src/apiserver/resource"
"github.com/kubeflow/pipelines/backend/src/common/util"
"github.com/pkg/errors"
"google.golang.org/grpc/metadata"
)
// These are valid conditions of a ScheduledWorkflow.
const (
MaxFileNameLength = 100
MaxFileLength = 32 << 20 // 32Mb
)
// This method extract the common logic of naming the pipeline.
// API caller can either explicitly name the pipeline through query string ?name=foobar
// or API server can use the file name by default.
func GetPipelineName(queryString string, fileName string) (string, error) {
pipelineName, err := url.QueryUnescape(queryString)
if err != nil {
return "", util.NewInvalidInputErrorWithDetails(err, "Pipeline name in the query string has invalid format.")
}
if pipelineName == "" {
pipelineName = fileName
}
if len(pipelineName) > MaxFileNameLength {
return "", util.NewInvalidInputError("Pipeline name too long. Support maximum length of %v", MaxFileNameLength)
}
return pipelineName, nil
}
func loadFile(fileReader io.Reader, maxFileLength int) ([]byte, error) {
reader := bufio.NewReader(fileReader)
pipelineFile := make([]byte, maxFileLength+1)
size, err := reader.Read(pipelineFile)
if err != nil && err != io.EOF {
return nil, util.NewInvalidInputErrorWithDetails(err, "Error read pipeline file.")
}
if size == maxFileLength+1 {
return nil, util.NewInvalidInputError("File size too large. Maximum supported size: %v", maxFileLength)
}
return pipelineFile[:size], nil
}
func isSupportedPipelineFormat(fileName string, compressedFile []byte) bool {
return isYamlFile(fileName) || isCompressedTarballFile(compressedFile) || isZipFile(compressedFile)
}
func isYamlFile(fileName string) bool {
return strings.HasSuffix(fileName, ".yaml") || strings.HasSuffix(fileName, ".yml")
}
func isPipelineYamlFile(fileName string) bool {
return fileName == "pipeline.yaml"
}
func isZipFile(compressedFile []byte) bool {
return len(compressedFile) > 2 && compressedFile[0] == '\x50' && compressedFile[1] == '\x4B' //Signature of zip file is "PK"
}
func isCompressedTarballFile(compressedFile []byte) bool {
return len(compressedFile) > 2 && compressedFile[0] == '\x1F' && compressedFile[1] == '\x8B'
}
func DecompressPipelineTarball(compressedFile []byte) ([]byte, error) {
gzipReader, err := gzip.NewReader(bytes.NewReader(compressedFile))
if err != nil {
return nil, util.NewInvalidInputErrorWithDetails(err, "Error extracting pipeline from the tarball file. Not a valid tarball file.")
}
// New behavior: searching for the "pipeline.yaml" file.
tarReader := tar.NewReader(gzipReader)
for {
header, err := tarReader.Next()
if err == io.EOF {
tarReader = nil
break
}
if err != nil {
return nil, util.NewInvalidInputErrorWithDetails(err, "Error extracting pipeline from the tarball file. Not a valid tarball file.")
}
if isPipelineYamlFile(header.Name) {
//Found the pipeline file.
break
}
}
// Old behavior - taking the first file in the archive
if tarReader == nil {
// Resetting the reader
gzipReader, err = gzip.NewReader(bytes.NewReader(compressedFile))
if err != nil {
return nil, util.NewInvalidInputErrorWithDetails(err, "Error extracting pipeline from the tarball file. Not a valid tarball file.")
}
tarReader = tar.NewReader(gzipReader)
header, err := tarReader.Next()
if err != nil {
return nil, util.NewInvalidInputErrorWithDetails(err, "Error extracting pipeline from the tarball file. Not a valid tarball file.")
}
if !isYamlFile(header.Name) {
return nil, util.NewInvalidInputError("Error extracting pipeline from the tarball file. Expecting a pipeline.yaml file inside the tarball. Got: %v", header.Name)
}
}
decompressedFile, err := ioutil.ReadAll(tarReader)
if err != nil {
return nil, util.NewInvalidInputErrorWithDetails(err, "Error reading pipeline YAML from the tarball file.")
}
return decompressedFile, err
}
func DecompressPipelineZip(compressedFile []byte) ([]byte, error) {
reader, err := zip.NewReader(bytes.NewReader(compressedFile), int64(len(compressedFile)))
if err != nil {
return nil, util.NewInvalidInputErrorWithDetails(err, "Error extracting pipeline from the zip file. Not a valid zip file.")
}
if len(reader.File) < 1 {
return nil, util.NewInvalidInputErrorWithDetails(err, "Error extracting pipeline from the zip file. Empty zip file.")
}
// Old behavior - taking the first file in the archive
pipelineYamlFile := reader.File[0]
// New behavior: searching for the "pipeline.yaml" file.
for _, file := range reader.File {
if isPipelineYamlFile(file.Name) {
pipelineYamlFile = file
break
}
}
if !isYamlFile(pipelineYamlFile.Name) {
return nil, util.NewInvalidInputError("Error extracting pipeline from the zip file. Expecting a pipeline.yaml file inside the zip. Got: %v", pipelineYamlFile.Name)
}
rc, err := pipelineYamlFile.Open()
if err != nil {
return nil, util.NewInvalidInputErrorWithDetails(err, "Error extracting pipeline from the zip file. Failed to read the content.")
}
decompressedFile, err := ioutil.ReadAll(rc)
if err != nil {
return nil, util.NewInvalidInputErrorWithDetails(err, "Error reading pipeline YAML from the zip file.")
}
return decompressedFile, err
}
func ReadPipelineFile(fileName string, fileReader io.Reader, maxFileLength int) ([]byte, error) {
// Read file into size limited byte array.
pipelineFileBytes, err := loadFile(fileReader, maxFileLength)
if err != nil {
return nil, util.Wrap(err, "Error read pipeline file.")
}
var processedFile []byte
switch {
case isYamlFile(fileName):
processedFile = pipelineFileBytes
case isZipFile(pipelineFileBytes):
processedFile, err = DecompressPipelineZip(pipelineFileBytes)
case isCompressedTarballFile(pipelineFileBytes):
processedFile, err = DecompressPipelineTarball(pipelineFileBytes)
default:
return nil, util.NewInvalidInputError("Unexpected pipeline file format. Support .zip, .tar.gz or YAML.")
}
if err != nil {
return nil, util.Wrap(err, "Error decompress the pipeline file")
}
return processedFile, nil
}
func printParameters(params []*api.Parameter) string {
var s strings.Builder
for _, p := range params {
s.WriteString(p.String())
}
return s.String()
}
// Verify the input resource references has one and only reference which is owner experiment.
func ValidateExperimentResourceReference(resourceManager *resource.ResourceManager, references []*api.ResourceReference) error {
if references == nil || len(references) == 0 || references[0] == nil {
return util.NewInvalidInputError("The resource reference is empty. Please specify which experiment owns this resource.")
}
if len(references) > 1 {
return util.NewInvalidInputError("Got more resource references than expected. Please only specify which experiment owns this resource.")
}
if references[0].Key.Type != api.ResourceType_EXPERIMENT {
return util.NewInvalidInputError("Unexpected resource type. Expected:%v. Got: %v",
api.ResourceType_EXPERIMENT, references[0].Key.Type)
}
if references[0].Key.Id == "" {
return util.NewInvalidInputError("Resource ID is empty. Please specify a valid ID")
}
if references[0].Relationship != api.Relationship_OWNER {
return util.NewInvalidInputError("Unexpected relationship for the experiment. Expected: %v. Got: %v",
api.Relationship_OWNER, references[0].Relationship)
}
if _, err := resourceManager.GetExperiment(references[0].Key.Id); err != nil {
return util.Wrap(err, "Failed to get experiment.")
}
return nil
}
func ValidatePipelineSpec(resourceManager *resource.ResourceManager, spec *api.PipelineSpec) error {
if spec == nil || (spec.GetPipelineId() == "" && spec.GetWorkflowManifest() == "") {
return util.NewInvalidInputError("Please specify a pipeline by providing a pipeline ID or workflow manifest.")
}
if spec.GetPipelineId() != "" && spec.GetWorkflowManifest() != "" {
return util.NewInvalidInputError("Please either specify a pipeline ID or a workflow manifest, not both.")
}
if spec.GetPipelineId() != "" {
// Verify pipeline exist
if _, err := resourceManager.GetPipeline(spec.GetPipelineId()); err != nil {
return util.Wrap(err, "Get pipeline failed.")
}
}
if spec.GetWorkflowManifest() != "" {
// Verify valid workflow template
var workflow util.Workflow
if err := json.Unmarshal([]byte(spec.GetWorkflowManifest()), &workflow); err != nil {
return util.NewInvalidInputErrorWithDetails(err,
"Invalid argo workflow format. Workflow: "+spec.GetWorkflowManifest())
}
}
paramsBytes, err := json.Marshal(spec.Parameters)
if err != nil {
return util.NewInternalServerError(err,
"Failed to Marshall the pipeline parameters into bytes. Parameters: %s",
printParameters(spec.Parameters))
}
if len(paramsBytes) > util.MaxParameterBytes {
return util.NewInvalidInputError("The input parameter length exceed maximum size of %v.", util.MaxParameterBytes)
}
return nil
}
// Verify that
// (1) a pipeline version is specified in references as a creator.
// (2) the above pipeline version does exists in pipeline version store and is
// in ready status.
func CheckPipelineVersionReference(resourceManager *resource.ResourceManager, references []*api.ResourceReference) (*string, error) {
if references == nil {
return nil, util.NewInvalidInputError("Please specify a pipeline version in Run's resource references")
}
var pipelineVersionId = ""
for _, reference := range references {
if reference.Key.Type == api.ResourceType_PIPELINE_VERSION && reference.Relationship == api.Relationship_CREATOR {
pipelineVersionId = reference.Key.Id
}
}
if len(pipelineVersionId) == 0 {
return nil, util.NewInvalidInputError("Please specify a pipeline version in Run's resource references")
}
// Verify pipeline version exists
if _, err := resourceManager.GetPipelineVersion(pipelineVersionId); err != nil {
return nil, util.Wrap(err, "Please specify a valid pipeline version in Run's resource references.")
}
return &pipelineVersionId, nil
}
func getUserIdentity(ctx context.Context) (string, error) {
if ctx == nil {
return "", util.NewBadRequestError(errors.New("Request error: context is nil"), "Request error: context is nil.")
}
md, _ := metadata.FromIncomingContext(ctx)
// If the request header contains the user identity, requests are authorized
// based on the namespace field in the request.
if userIdentityHeader, ok := md[common.GoogleIAPUserIdentityHeader]; ok {
if len(userIdentityHeader) != 1 {
return "", util.NewBadRequestError(errors.New("Request header error: unexpected number of user identity header. Expect 1 got "+strconv.Itoa(len(userIdentityHeader))),
"Request header error: unexpected number of user identity header. Expect 1 got "+strconv.Itoa(len(userIdentityHeader)))
}
userIdentityHeaderFields := strings.Split(userIdentityHeader[0], ":")
if len(userIdentityHeaderFields) != 2 {
return "", util.NewBadRequestError(errors.New("Request header error: user identity value is incorrectly formatted"),
"Request header error: user identity value is incorrectly formatted")
}
return userIdentityHeaderFields[1], nil
}
return "", util.NewBadRequestError(errors.New("Request header error: there is no user identity header."), "Request header error: there is no user identity header.")
}
func CanAccessExperiment(resourceManager *resource.ResourceManager, ctx context.Context, experimentID string) error {
if common.IsMultiUserMode() == false {
// Skip authz if not multi-user mode.
return nil
}
experiment, err := resourceManager.GetExperiment(experimentID)
if err != nil {
return util.NewBadRequestError(errors.New("Invalid experiment ID"), "Failed to get experiment.")
}
if len(experiment.Namespace) == 0 {
return util.NewInternalServerError(errors.New("Missing namespace"), "Experiment %v doesn't have a namespace.", experiment.Name)
}
err = isAuthorized(resourceManager, ctx, experiment.Namespace)
if err != nil {
return util.Wrap(err, "Failed to authorize with API resource references")
}
return nil
}
func CanAccessExperimentInResourceReferences(resourceManager *resource.ResourceManager, ctx context.Context, resourceRefs []*api.ResourceReference) error {
if common.IsMultiUserMode() == false {
// Skip authz if not multi-user mode.
return nil
}
experimentID := common.GetExperimentIDFromAPIResourceReferences(resourceRefs)
if len(experimentID) == 0 {
return util.NewBadRequestError(errors.New("Missing experiment"), "Experiment is required for CreateRun/CreateJob.")
}
return CanAccessExperiment(resourceManager, ctx, experimentID)
}
func CanAccessNamespaceInResourceReferences(resourceManager *resource.ResourceManager, ctx context.Context, resourceRefs []*api.ResourceReference) error {
if common.IsMultiUserMode() == false {
// Skip authz if not multi-user mode.
return nil
}
namespace := common.GetNamespaceFromAPIResourceReferences(resourceRefs)
if len(namespace) == 0 {
return util.NewBadRequestError(errors.New("Namespace required in Kubeflow deployment for authorization."), "Namespace required in Kubeflow deployment for authorization.")
}
err := isAuthorized(resourceManager, ctx, namespace)
if err != nil {
return util.Wrap(err, "Failed to authorize with API resource references")
}
return nil
}
func CanAccessNamespace(resourceManager *resource.ResourceManager, ctx context.Context, namespace string) error {
if common.IsMultiUserMode() == false {
// Skip authz if not multi-user mode.
return nil
}
if len(namespace) == 0 {
return util.NewBadRequestError(errors.New("Namespace required for authorization."), "Namespace required for authorization.")
}
err := isAuthorized(resourceManager, ctx, namespace)
if err != nil {
return util.Wrap(err, "Failed to authorize namespace")
}
return nil
}
// isAuthorized verified whether the user identity, which is contains in the context object,
// can access the target namespace. If the returned error is nil, the authorization passes.
// Otherwise, Authorization fails with a non-nil error.
func isAuthorized(resourceManager *resource.ResourceManager, ctx context.Context, namespace string) error {
userIdentity, err := getUserIdentity(ctx)
if err != nil {
return util.Wrap(err, "Bad request.")
}
if len(userIdentity) == 0 {
return util.NewBadRequestError(errors.New("Request header error: user identity is empty."), "Request header error: user identity is empty.")
}
isAuthorized, err := resourceManager.IsRequestAuthorized(userIdentity, namespace)
if err != nil {
return util.Wrap(err, "Authorization failure.")
}
if isAuthorized == false {
glog.Infof("Unauthorized access for %s to namespace %s", userIdentity, namespace)
return util.NewBadRequestError(errors.New("Unauthorized access for "+userIdentity+" to namespace "+namespace), "Unauthorized access for "+userIdentity+" to namespace "+namespace)
}
glog.Infof("Authorized user %s in namespace %s", userIdentity, namespace)
return nil
}