diff --git a/pkg/apiserver/auditing/backend.go b/pkg/apiserver/auditing/backend.go index f79a97c698ce4f6b6eeb34664d06c0147b982d35..4c847ee7461aaa7ceb71447fe3fdbdcf3fe24443 100644 --- a/pkg/apiserver/auditing/backend.go +++ b/pkg/apiserver/auditing/backend.go @@ -112,24 +112,18 @@ func (b *Backend) worker() { func (b *Backend) eventToBytes(event *v1alpha1.EventList) ([]byte, error) { - if bs, err := json.Marshal(event); err == nil { - return bs, nil - } - - // Normally, the serialization failure is caused by the failure of RequestObject or ResponseObject serialization. - // To ensure the integrity of the auditing event to the greatest extent, - // it is necessary to delete RequestObject or ResponseObject and and then try to serialize again. - if event.Items[0].RequestObject != nil { - if _, err := json.Marshal(event.Items[0].RequestObject); err != nil { - event.Items[0].RequestObject = nil - } - } - - if event.Items[0].ResponseObject != nil { - if _, err := json.Marshal(event.Items[0].ResponseObject); err != nil { + bs, err := json.Marshal(event) + if err != nil { + // Normally, the serialization failure is caused by the failure of ResponseObject serialization. + // To ensure the integrity of the auditing event to the greatest extent, + // it is necessary to delete ResponseObject and and then try to serialize again. + if event.Items[0].ResponseObject != nil { event.Items[0].ResponseObject = nil + return json.Marshal(event) } + + return nil, err } - return json.Marshal(event) + return bs, err } diff --git a/pkg/kapis/devops/v1alpha2/devops.go b/pkg/kapis/devops/v1alpha2/devops.go index d22478b8bf2655386507b9ab428d9605ef71f996..382cf972ce669de8da6b4e08e19e2606523a6d9f 100644 --- a/pkg/kapis/devops/v1alpha2/devops.go +++ b/pkg/kapis/devops/v1alpha2/devops.go @@ -20,7 +20,6 @@ import ( "github.com/emicklei/go-restful" log "k8s.io/klog" "kubesphere.io/kubesphere/pkg/api" - "kubesphere.io/kubesphere/pkg/apiserver/request" "kubesphere.io/kubesphere/pkg/models/devops" "net/http" "strings" @@ -93,12 +92,6 @@ func (h *ProjectPipelineHandler) StopPipeline(req *restful.Request, resp *restfu return } - if event := request.AuditEventFrom(req.Request.Context()); event != nil { - event.ObjectRef.Resource = "pipelines.runs" - event.ObjectRef.Name = pipelineName + "." + runId - event.Verb = "stop" - } - resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON) resp.WriteAsJson(res) } @@ -114,12 +107,6 @@ func (h *ProjectPipelineHandler) ReplayPipeline(req *restful.Request, resp *rest return } - if event := request.AuditEventFrom(req.Request.Context()); event != nil { - event.ObjectRef.Resource = "pipelines.runs" - event.ObjectRef.Name = pipelineName + "." + runId - event.Verb = "replay" - } - resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON) resp.WriteAsJson(res) } @@ -134,11 +121,6 @@ func (h *ProjectPipelineHandler) RunPipeline(req *restful.Request, resp *restful return } - if event := request.AuditEventFrom(req.Request.Context()); event != nil { - event.ObjectRef.Resource = "pipelines.runs" - event.ObjectRef.Name = pipelineName + "." + res.ID - } - resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON) resp.WriteAsJson(res) } @@ -233,11 +215,6 @@ func (h *ProjectPipelineHandler) SubmitInputStep(req *restful.Request, resp *res return } - if event := request.AuditEventFrom(req.Request.Context()); event != nil { - event.ObjectRef.Resource = "pipelines.runs.nodes.step" - event.ObjectRef.Name = pipelineName + "." + runId + "." + nodeId + "." + stepId - } - resp.Write(res) } @@ -297,12 +274,6 @@ func (h *ProjectPipelineHandler) StopBranchPipeline(req *restful.Request, resp * return } - if event := request.AuditEventFrom(req.Request.Context()); event != nil { - event.ObjectRef.Resource = "pipelines.branches.runs" - event.ObjectRef.Name = pipelineName + "." + branchName + "." + runId - event.Verb = "stop" - } - resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON) resp.WriteAsJson(res) } @@ -319,12 +290,6 @@ func (h *ProjectPipelineHandler) ReplayBranchPipeline(req *restful.Request, resp return } - if event := request.AuditEventFrom(req.Request.Context()); event != nil { - event.ObjectRef.Resource = "pipelines.branches.runs" - event.ObjectRef.Name = pipelineName + "." + branchName + "." + runId - event.Verb = "replay" - } - resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON) resp.WriteAsJson(res) } @@ -340,11 +305,6 @@ func (h *ProjectPipelineHandler) RunBranchPipeline(req *restful.Request, resp *r return } - if event := request.AuditEventFrom(req.Request.Context()); event != nil { - event.ObjectRef.Resource = "pipelines.branches.runs" - event.ObjectRef.Name = pipelineName + "." + branchName + "." + res.ID - } - resp.Header().Set(restful.HEADER_ContentType, restful.MIME_JSON) resp.WriteAsJson(res) } @@ -447,11 +407,6 @@ func (h *ProjectPipelineHandler) SubmitBranchInputStep(req *restful.Request, res return } - if event := request.AuditEventFrom(req.Request.Context()); event != nil { - event.ObjectRef.Resource = "pipelines.branches.runs.nodes.steps" - event.ObjectRef.Name = pipelineName + "." + branchName + "." + runId + "." + nodeId + "." + stepId - } - resp.Write(res) } diff --git a/pkg/kapis/iam/v1alpha2/handler.go b/pkg/kapis/iam/v1alpha2/handler.go index 1934fe3f92f4cccc19068f9a014d89d23267d151..c5353dcb221c70cf04b70fda52f598592c83ba8e 100644 --- a/pkg/kapis/iam/v1alpha2/handler.go +++ b/pkg/kapis/iam/v1alpha2/handler.go @@ -10,7 +10,6 @@ import ( iamv1alpha2 "kubesphere.io/kubesphere/pkg/apis/iam/v1alpha2" authoptions "kubesphere.io/kubesphere/pkg/apiserver/authentication/options" "kubesphere.io/kubesphere/pkg/apiserver/query" - requestinfo "kubesphere.io/kubesphere/pkg/apiserver/request" "kubesphere.io/kubesphere/pkg/models/iam/am" "kubesphere.io/kubesphere/pkg/models/iam/im" servererr "kubesphere.io/kubesphere/pkg/server/errors" @@ -822,14 +821,6 @@ func (h *iamHandler) CreateWorkspaceMembers(request *restful.Request, response * } } - if event := requestinfo.AuditEventFrom(request.Request.Context()); event != nil { - name := "" - for _, member := range members { - name += member.Username + ", " - } - name = strings.TrimSuffix(name, ", ") - event.ObjectRef.Name = name - } response.WriteEntity(members) } @@ -908,14 +899,6 @@ func (h *iamHandler) CreateNamespaceMembers(request *restful.Request, response * } } - if event := requestinfo.AuditEventFrom(request.Request.Context()); event != nil { - name := "" - for _, member := range members { - name += member.Username + ", " - } - name = strings.TrimSuffix(name, ", ") - event.ObjectRef.Name = name - } response.WriteEntity(members) } @@ -997,14 +980,6 @@ func (h *iamHandler) CreateClusterMembers(request *restful.Request, response *re } } - if event := requestinfo.AuditEventFrom(request.Request.Context()); event != nil { - name := "" - for _, member := range members { - name += member.Username + ", " - } - name = strings.TrimSuffix(name, ", ") - event.ObjectRef.Name = name - } response.WriteEntity(members) }