Skip to content
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

Require CloudInit .spec.contents to be valid YAML (backport #34) #35

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/admitter/cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"

"github.com/harvester/webhook/pkg/server/admission"
"gopkg.in/yaml.v3"
admissionregv1 "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -21,6 +22,7 @@ var (
errFilenameTaken = errors.New("filename already in use")
errProtectedFilename = errors.New("filename conflicts with a critical system-owned file")
errMissingExt = errors.New("filename does not end in .yaml or .yml")
errNotYAML = errors.New("could not parse document as yaml")
)

var builtinFilenameDenyList = []string{
Expand Down Expand Up @@ -85,6 +87,16 @@ func (v *CloudInit) validate(cloudinit *v1beta1.CloudInit) error {
return errFilenameTaken
}

var obj map[string]any
var typeErr *yaml.TypeError
err = yaml.Unmarshal([]byte(cloudinit.Spec.Contents), &obj)
if errors.As(err, &typeErr) {
return errNotYAML
}
if err != nil {
return err
}

return nil
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/admitter/cloudinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func TestCreate(t *testing.T) {
{"filename collision", v1beta1.CloudInitSpec{Filename: "99_ssh.yaml"}, errFilenameTaken},
{"conflicts with protected file", v1beta1.CloudInitSpec{Filename: "helloworld.yaml"}, errProtectedFilename},
{"not yaml or yml file ext", v1beta1.CloudInitSpec{Filename: "a"}, errMissingExt},
{"not yaml contents", v1beta1.CloudInitSpec{Filename: "not.yaml", Contents: "hello, there"}, errNotYAML},
}

for _, tt := range tests {
Expand Down Expand Up @@ -96,6 +97,7 @@ func TestUpdate(t *testing.T) {
{"filename collision", v1beta1.CloudInitSpec{Filename: "99_ssh.yaml"}, errFilenameTaken},
{"conflicts with protected file", v1beta1.CloudInitSpec{Filename: "helloworld.yaml"}, errProtectedFilename},
{"not yaml or yml file ext", v1beta1.CloudInitSpec{Filename: "a"}, errMissingExt},
{"not yaml contents", v1beta1.CloudInitSpec{Filename: "not.yaml", Contents: "hello, there"}, errNotYAML},
}

for _, tt := range tests {
Expand Down
Loading