Skip to content

Commit f8967a7

Browse files
committed
Fix cx panic when object and prefix present on bkt
1 parent 779b9d2 commit f8967a7

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

pkg/lib/gcp/gcs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func SplitGCSPath(gcsPath string) (string, string, error) {
3838
if !IsValidGCSPath(gcsPath) {
3939
return "", "", ErrorInvalidGCSPath(gcsPath)
4040
}
41-
fullPath := gcsPath[len("s3://"):]
41+
fullPath := gcsPath[len("gs://"):]
4242
slashIndex := strings.Index(fullPath, "/")
4343
if slashIndex == -1 {
4444
return fullPath, "", nil

pkg/lib/slices/string.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ func RemoveEmptiesAndUnique(strs []string) []string {
9898
return out
9999
}
100100

101+
func RemoveIfHasString(strs []string, str string) []string {
102+
var filteredStrs []string
103+
for _, elem := range strs {
104+
if elem != str {
105+
filteredStrs = append(filteredStrs, elem)
106+
}
107+
}
108+
return filteredStrs
109+
}
110+
101111
func HasDuplicateStr(in []string) bool {
102112
keys := strset.New()
103113
for _, elem := range in {

pkg/types/spec/errors.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ const (
5555

5656
ErrShmSizeCannotExceedMem = "spec.shm_size_cannot_exceed_mem"
5757

58-
ErrFileNotFound = "spec.file_not_found"
59-
ErrDirIsEmpty = "spec.dir_is_empty"
60-
ErrMustBeRelativeProjectPath = "spec.must_be_relative_project_path"
61-
ErrPythonPathNotFound = "spec.python_path_not_found"
58+
ErrFileNotFound = "spec.file_not_found"
59+
ErrDirIsEmpty = "spec.dir_is_empty"
60+
ErrMustBeRelativeProjectPath = "spec.must_be_relative_project_path"
61+
ErrPythonPathNotFound = "spec.python_path_not_found"
62+
ErrObjectConflictingWithPrefix = "spec.object_conflicting_with_prefix"
6263

6364
ErrS3FileNotFound = "spec.s3_file_not_found"
6465
ErrS3DirNotFound = "spec.s3_dir_not_found"
@@ -274,6 +275,13 @@ func ErrorPythonPathNotFound(pythonPath string) error {
274275
})
275276
}
276277

278+
func ErrorObjectConflictingWithPrefix(path string) error {
279+
return errors.WithStack(&errors.Error{
280+
Kind: ErrObjectConflictingWithPrefix,
281+
Message: fmt.Sprintf("%s: path prefix conflicts with object %s; remove object %s to be able to use %s as prefix for other objects", path, path, path, path),
282+
})
283+
}
284+
277285
func ErrorS3FileNotFound(path string) error {
278286
return errors.WithStack(&errors.Error{
279287
Kind: ErrS3FileNotFound,

pkg/types/spec/utils.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ func validateDirModels(
140140
if err != nil {
141141
return nil, err
142142
}
143+
dirPrefix = s.EnsureSuffix(dirPrefix, "/")
143144

144145
s3Objects, err := awsClientForBucket.ListS3PathDir(modelPath, false, nil)
145146
if err != nil {
@@ -152,6 +153,7 @@ func validateDirModels(
152153
if err != nil {
153154
return nil, err
154155
}
156+
dirPrefix = s.EnsureSuffix(dirPrefix, "/")
155157

156158
gcsObjects, err := gcpClient.ListGCSPathDir(modelPath, nil)
157159
if err != nil {
@@ -162,6 +164,12 @@ func validateDirModels(
162164
if len(modelDirPaths) == 0 {
163165
return nil, errorForPredictorType(dirPrefix, modelDirPaths)
164166
}
167+
if len(modelDirPaths) != len(slices.RemoveIfHasString(modelDirPaths, dirPrefix)) {
168+
return nil, ErrorObjectConflictingWithPrefix(dirPrefix)
169+
}
170+
if len(modelDirPaths) != len(slices.RemoveIfHasString(modelDirPaths, strings.TrimRight(dirPrefix, "/"))) {
171+
return nil, ErrorObjectConflictingWithPrefix(strings.TrimRight(dirPrefix, "/"))
172+
}
165173

166174
modelNames := []string{}
167175
modelDirPathLength := len(slices.RemoveEmpties(strings.Split(dirPrefix, "/")))
@@ -179,7 +187,13 @@ func validateDirModels(
179187
}
180188

181189
modelPrefix := filepath.Join(dirPrefix, modelName)
190+
if len(modelDirPaths) != len(slices.RemoveIfHasString(modelDirPaths, modelPrefix)) {
191+
return nil, ErrorObjectConflictingWithPrefix(modelPrefix)
192+
}
182193
modelPrefix = s.EnsureSuffix(modelPrefix, "/")
194+
if len(modelDirPaths) != len(slices.RemoveIfHasString(modelDirPaths, modelPrefix)) {
195+
return nil, ErrorObjectConflictingWithPrefix(modelPrefix)
196+
}
183197

184198
modelStructureType := determineBaseModelStructure(modelDirPaths, modelPrefix)
185199
if modelStructureType == userconfig.UnknownModelStructureType {
@@ -296,6 +310,12 @@ func validateModels(
296310
if len(modelPaths) == 0 {
297311
return nil, errors.Wrap(errorForPredictorType(modelPrefix, modelPaths), modelNameWrapStr)
298312
}
313+
if len(modelPaths) != len(slices.RemoveIfHasString(modelPaths, modelPrefix)) {
314+
return nil, ErrorObjectConflictingWithPrefix(modelPrefix)
315+
}
316+
if len(modelPaths) != len(slices.RemoveIfHasString(modelPaths, strings.TrimRight(modelPrefix, "/"))) {
317+
return nil, ErrorObjectConflictingWithPrefix(strings.TrimRight(modelPrefix, "/"))
318+
}
299319

300320
modelStructureType := determineBaseModelStructure(modelPaths, modelPrefix)
301321
if modelStructureType == userconfig.UnknownModelStructureType {

0 commit comments

Comments
 (0)