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

Fix issue that Audit Server could not correctly encode metav1.DeleteOption #110110

Merged
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
2 changes: 1 addition & 1 deletion staging/src/k8s.io/apiserver/pkg/audit/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func encodeObject(obj runtime.Object, gv schema.GroupVersion, serializer runtime

return &runtime.Unknown{
Raw: buf.Bytes(),
ContentType: runtime.ContentTypeJSON,
ContentType: mediaType,
}, nil
}

Expand Down
8 changes: 4 additions & 4 deletions staging/src/k8s.io/apiserver/pkg/endpoints/handlers/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func DeleteResource(r rest.GracefulDeleter, allowsOptions bool, scope *RequestSc
trace.Step("Decoded delete options")

objGV := gvk.GroupVersion()
audit.LogRequestObject(req.Context(), obj, objGV, scope.Resource, scope.Subresource, scope.Serializer)
audit.LogRequestObject(req.Context(), obj, objGV, scope.Resource, scope.Subresource, metainternalversionscheme.Codecs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add some unit test for these?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your suggestion, I have added Unit test to this modification. PTAL, thx~

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/ping @wojtek-t

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test you added doesn't verify the fix you're making in this PR.

I would like to see some test that fails before this change and is passing with this change...

trace.Step("Recorded the audit event")
} else {
if err := metainternalversionscheme.ParameterCodec.DecodeParameters(req.URL.Query(), scope.MetaGroupVersion, options); err != nil {
Expand Down Expand Up @@ -238,8 +238,8 @@ func DeleteCollection(r rest.CollectionDeleter, checkBody bool, scope *RequestSc
}
// For backwards compatibility, we need to allow existing clients to submit per group DeleteOptions
// It is also allowed to pass a body with meta.k8s.io/v1.DeleteOptions
defaultGVK := scope.Kind.GroupVersion().WithKind("DeleteOptions")
obj, gvk, err := scope.Serializer.DecoderToVersion(s.Serializer, defaultGVK.GroupVersion()).Decode(body, &defaultGVK, options)
defaultGVK := scope.MetaGroupVersion.WithKind("DeleteOptions")
obj, gvk, err := metainternalversionscheme.Codecs.DecoderToVersion(s.Serializer, defaultGVK.GroupVersion()).Decode(body, &defaultGVK, options)
if err != nil {
scope.err(err, w, req)
return
Expand All @@ -250,7 +250,7 @@ func DeleteCollection(r rest.CollectionDeleter, checkBody bool, scope *RequestSc
}

objGV := gvk.GroupVersion()
audit.LogRequestObject(req.Context(), obj, objGV, scope.Resource, scope.Subresource, scope.Serializer)
audit.LogRequestObject(req.Context(), obj, objGV, scope.Resource, scope.Subresource, metainternalversionscheme.Codecs)
} else {
if err := metainternalversionscheme.ParameterCodec.DecodeParameters(req.URL.Query(), scope.MetaGroupVersion, options); err != nil {
err = errors.NewBadRequest(err.Error())
Expand Down
134 changes: 134 additions & 0 deletions staging/src/k8s.io/apiserver/pkg/endpoints/handlers/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
Copyright 2019 The Kubernetes 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

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 handlers

import (
"context"
"io"
"testing"

metainternalversionscheme "k8s.io/apimachinery/pkg/apis/meta/internalversion/scheme"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
auditapis "k8s.io/apiserver/pkg/apis/audit"
"k8s.io/apiserver/pkg/audit"
"k8s.io/utils/pointer"
)

type mockCodecs struct {
serializer.CodecFactory
err error
}

type mockCodec struct {
runtime.Codec
codecs *mockCodecs
}

func (p mockCodec) Encode(obj runtime.Object, w io.Writer) error {
err := p.Codec.Encode(obj, w)
p.codecs.err = err
return err
}

func (s *mockCodecs) EncoderForVersion(encoder runtime.Encoder, gv runtime.GroupVersioner) runtime.Encoder {
out := s.CodecFactory.CodecForVersions(encoder, nil, gv, nil)
return &mockCodec{
Codec: out,
codecs: s,
}
}

func TestDeleteResourceAuditLogRequestObject(t *testing.T) {

ctx := audit.WithAuditContext(context.TODO(), &audit.AuditContext{
Event: &auditapis.Event{
Level: auditapis.LevelRequestResponse,
},
})

policy := metav1.DeletePropagationBackground
deleteOption := &metav1.DeleteOptions{
GracePeriodSeconds: pointer.Int64Ptr(30),
PropagationPolicy: &policy,
}

fakeCorev1GroupVersion := schema.GroupVersion{
Group: "",
Version: "v1",
}
testScheme := runtime.NewScheme()
metav1.AddToGroupVersion(testScheme, fakeCorev1GroupVersion)
testCodec := serializer.NewCodecFactory(testScheme)

tests := []struct {
name string
object runtime.Object
gv schema.GroupVersion
serializer serializer.CodecFactory
ok bool
}{
{
name: "meta built-in Codec encode v1.DeleteOptions",
object: &metav1.DeleteOptions{
GracePeriodSeconds: pointer.Int64Ptr(30),
PropagationPolicy: &policy,
},
gv: metav1.SchemeGroupVersion,
serializer: metainternalversionscheme.Codecs,
ok: true,
},
{
name: "fake corev1 registered codec encode v1 DeleteOptions",
object: &metav1.DeleteOptions{
GracePeriodSeconds: pointer.Int64Ptr(30),
PropagationPolicy: &policy,
},
gv: metav1.SchemeGroupVersion,
serializer: testCodec,
ok: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {

codecs := &mockCodecs{}
codecs.CodecFactory = test.serializer

audit.LogRequestObject(ctx, deleteOption, test.gv, schema.GroupVersionResource{
Group: "",
Version: "v1",
Resource: "pods",
}, "", codecs)

err := codecs.err
if err != nil {
if test.ok {
t.Errorf("expect nil but got %#v", err)
}
t.Logf("encode object: %#v", err)
} else {
if !test.ok {
t.Errorf("expect err but got nil")
}
}
})
}
}