-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathutil.go
174 lines (158 loc) · 5.97 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
// Copyright 2018 The Kubeflow 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
//
// https://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 server
import (
"archive/tar"
"archive/zip"
"bufio"
"bytes"
"compress/gzip"
"errors"
"io"
"strings"
"github.com/kubeflow/pipelines/backend/src/common/util"
)
func loadFile(fileReader io.Reader, MaxFileLength int) ([]byte, error) {
// TODO(lingqinggan): investigate ways to increase the buffer size, so we don't have to use a loop.
reader := bufio.NewReaderSize(fileReader, MaxFileLength)
var pipelineFile []byte
for {
currentRead := make([]byte, bufio.MaxScanTokenSize)
size, err := reader.Read(currentRead)
pipelineFile = append(pipelineFile, currentRead[:size]...)
if err == io.EOF {
// there is no more data to read
break
}
if err != nil {
return nil, util.NewInvalidInputErrorWithDetails(err, "Error read pipeline file")
}
}
if len(pipelineFile) > MaxFileLength {
return nil, util.NewInvalidInputError("File size too large. Maximum supported size: %v", MaxFileLength)
}
return pipelineFile, nil
}
func isYamlFile(fileName string) bool {
return strings.HasSuffix(fileName, ".yaml") || strings.HasSuffix(fileName, ".yml")
}
func isJSONFile(fileName string) bool {
return strings.HasSuffix(fileName, ".json")
}
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 errors.Is(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 := io.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 := io.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 isJSONFile(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, .json or YAML")
}
if err != nil {
return nil, util.Wrap(err, "Error decompress the pipeline file")
}
return processedFile, nil
}