-
Notifications
You must be signed in to change notification settings - Fork 959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate fields mount name and mount path in Dataset #3687
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
Copyright 2024 The Fluid Author. | ||
|
||
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 validation | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/fluid-cloudnative/fluid/api/v1alpha1" | ||
"k8s.io/apimachinery/pkg/util/validation" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
) | ||
|
||
func IsValidDataset(dataset v1alpha1.Dataset, enableMountValidation bool) error { | ||
if errs := validation.IsDNS1035Label(dataset.ObjectMeta.Name); len(dataset.ObjectMeta.Name) > 0 && len(errs) > 0 { | ||
return field.Invalid(field.NewPath("metadata").Child("name"), dataset.ObjectMeta.Name, strings.Join(errs, ",")) | ||
} | ||
|
||
// 0.1 Validate the mount name and mount path | ||
// Users can set the environment variable to 'false' to disable this validation | ||
// Default is true | ||
if !enableMountValidation { | ||
return nil | ||
} | ||
for _, mount := range dataset.Spec.Mounts { | ||
// The field mount.Name and mount.Path is optional | ||
// Empty name or path is allowed | ||
if len(mount.Name) != 0 { | ||
// If users set the mount.Name, it should comply with the DNS1035 rule. | ||
if errs := validation.IsDNS1035Label(mount.Name); len(errs) > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest to use same validation method for both |
||
return field.Invalid(field.NewPath("spec").Child("mounts").Child("name"), mount.Name, strings.Join(errs, ",")) | ||
} | ||
} | ||
if len(mount.Path) != 0 { | ||
// If users set the mount.Path, check it. | ||
if err := IsValidMountPath(mount.Path); err != nil { | ||
return field.Invalid(field.NewPath("spec").Child("mounts").Child("path"), mount.Path, err.Error()) | ||
} | ||
} | ||
} | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/* | ||
Copyright 2024 The Fluid Author. | ||
|
||
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 validation | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/fluid-cloudnative/fluid/api/v1alpha1" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
const mountPoint1 string = "https://mirrors.bit.edu.cn/apache/spark/" | ||
const validMountName1 string = "spark" | ||
|
||
const mountPoint2 string = "https://mirrors.bit.edu.cn/apache/flink/" | ||
const validMountName2 string = "flink" | ||
|
||
const validMountPath1 string = "/test" | ||
const validMountPath2 string = "mnt/test" | ||
|
||
func TestIsValidDatasetWithValidDataset(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
input v1alpha1.Dataset | ||
enableMountValidation bool | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
name: "validDatasetWithSingleMount", | ||
enableMountValidation: true, | ||
input: v1alpha1.Dataset{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "demo", | ||
}, | ||
Spec: v1alpha1.DatasetSpec{ | ||
Mounts: []v1alpha1.Mount{ | ||
{ | ||
MountPoint: mountPoint1, | ||
Name: validMountName1, | ||
Path: validMountPath1, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "validDatasetWithMultiMount", | ||
enableMountValidation: true, | ||
input: v1alpha1.Dataset{ | ||
Spec: v1alpha1.DatasetSpec{ | ||
Mounts: []v1alpha1.Mount{ | ||
{ | ||
MountPoint: mountPoint1, | ||
Name: validMountName1, | ||
}, | ||
{ | ||
MountPoint: mountPoint2, | ||
Name: validMountName2, | ||
Path: validMountPath2, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "validDatasetWithDisableMountValidation", | ||
enableMountValidation: false, | ||
input: v1alpha1.Dataset{ | ||
Spec: v1alpha1.DatasetSpec{ | ||
Mounts: []v1alpha1.Mount{ | ||
{ | ||
MountPoint: mountPoint1, | ||
Path: "/${TEST}", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
got := IsValidDataset(test.input, test.enableMountValidation) | ||
if got != nil { | ||
t.Errorf("testcase %s failed, expect no error happened, but got an error: %s", test.name, got.Error()) | ||
} | ||
} | ||
} | ||
|
||
func TestIsValidDatasetWithInvalidDataset(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
input v1alpha1.Dataset | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
name: "invalidDatasetMountName", | ||
input: v1alpha1.Dataset{ | ||
Spec: v1alpha1.DatasetSpec{ | ||
Mounts: []v1alpha1.Mount{ | ||
{ | ||
MountPoint: mountPoint1, | ||
Name: "$(cat /etc/passwd > /test.txt)", | ||
Path: validMountPath1, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "invalidDatasetName", | ||
input: v1alpha1.Dataset{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "20-hbase", | ||
}, | ||
Spec: v1alpha1.DatasetSpec{ | ||
Mounts: []v1alpha1.Mount{ | ||
{ | ||
MountPoint: mountPoint1, | ||
Name: validMountName1, | ||
Path: validMountPath1, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "invalidDatasetMountPath", | ||
input: v1alpha1.Dataset{ | ||
Spec: v1alpha1.DatasetSpec{ | ||
Mounts: []v1alpha1.Mount{ | ||
{ | ||
MountPoint: mountPoint1, | ||
Name: validMountName1, | ||
Path: "/$(cat /etc/passwd > /test.txt)", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "invalidDatasetMountPathInSecondMount", | ||
input: v1alpha1.Dataset{ | ||
Spec: v1alpha1.DatasetSpec{ | ||
Mounts: []v1alpha1.Mount{ | ||
{ | ||
MountPoint: mountPoint1, | ||
Name: validMountName1, | ||
}, | ||
{ | ||
MountPoint: mountPoint2, | ||
Name: validMountName2, | ||
Path: "/test/$(cat /etc/passwd > /test.txt)", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
got := IsValidDataset(test.input, true) | ||
if got == nil { | ||
t.Errorf("testcase %s failed, expect an error happened, but got no error", test.name) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have to add a validation option here? WDYT @cheyang @zhang-x-z
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's not essential. Instead, how about putting the validation logic into function ReconcileInternal?
fluid/pkg/controllers/runtime_controller.go
Line 83 in 2723eed