-
Notifications
You must be signed in to change notification settings - Fork 92
Adding the basic revision recorder for k8s audit log #389
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
Merged
kyasbal
merged 15 commits into
GoogleCloudPlatform:epic/issue-373
from
kyasbal:epic/issue-373-basic-revision-recorder
Dec 2, 2025
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4c01cca
fix issues pointed by gemini-code-assist
kyasbal c6d7986
Added new fieldset related tasks and history modifiers for error audi…
kyasbal d487137
Adding k8s audit log parser tasks
kyasbal 1494c2d
fix issues pointed by gemini-code-assist
kyasbal 74ac2a9
fix issues pointed by gemini-code-assist
kyasbal 3ac406c
Adding grouping related tasks and tasks for gathering k8s audit logs …
kyasbal 6b76bbd
Adding k8s audit log parser tasks
kyasbal 054b537
fix issues pointed by gemini-code-assist
kyasbal acae8c5
Adding k8s audit log parser tasks
kyasbal 947b9d1
fix issues pointed by gemini-code-assist
kyasbal a57ff8c
Added several test asserter for changeset testing
kyasbal 092d6e1
fix issues pointed by gemini-code-assist
kyasbal 38b95f2
Adding manifest based history-modifier tasks and manifest utils
kyasbal 4704d3c
Add the basic revision recording tasks for k8s audit logs
kyasbal a96342b
fix issues pointed by gemini-code-assist
kyasbal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
326 changes: 326 additions & 0 deletions
326
pkg/task/inspection/commonlogk8sauditv2/contract/manifest_historymodifier.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,326 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // 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 commonlogk8sauditv2_contract | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log/slog" | ||
| "runtime" | ||
| "strings" | ||
| "sync/atomic" | ||
| "time" | ||
|
|
||
| "github.com/GoogleCloudPlatform/khi/pkg/common/khictx" | ||
| "github.com/GoogleCloudPlatform/khi/pkg/common/structured" | ||
| inspectionmetadata "github.com/GoogleCloudPlatform/khi/pkg/core/inspection/metadata" | ||
| "github.com/GoogleCloudPlatform/khi/pkg/core/inspection/progressutil" | ||
| inspectiontaskbase "github.com/GoogleCloudPlatform/khi/pkg/core/inspection/taskbase" | ||
| coretask "github.com/GoogleCloudPlatform/khi/pkg/core/task" | ||
| "github.com/GoogleCloudPlatform/khi/pkg/core/task/taskid" | ||
| "github.com/GoogleCloudPlatform/khi/pkg/model/history" | ||
| "github.com/GoogleCloudPlatform/khi/pkg/model/log" | ||
| inspectioncore_contract "github.com/GoogleCloudPlatform/khi/pkg/task/inspection/inspectioncore/contract" | ||
| "golang.org/x/sync/errgroup" | ||
| ) | ||
|
|
||
| // ChangeEventType is the type of the resource change event. | ||
| type ChangeEventType int | ||
|
|
||
| const ( | ||
| // ChangeEventTypeSourceCreation is the event type when the source resource is created. | ||
| // The "creation" is not necessary means the audit log with of a request with verb "CREATE", but it is the first event of the source resource. | ||
| // This event can happen within the same resource multiple time when the resource was completely deleted and then created again. | ||
| ChangeEventTypeSourceCreation ChangeEventType = iota | ||
| // ChangeEventTypeSourceDeletion is the event type when the source resource is deleted. | ||
| // Deletion event can happen multiple times when audit logs has no information on the content and the resource had multiple deletion requests in series. | ||
| ChangeEventTypeSourceDeletion | ||
| // ChangeEventTypeSourceModification is the event type when the source resource is modified. | ||
| ChangeEventTypeSourceModification | ||
| // ChangeEventTypeTargetCreation is the event type when the target resource is created. | ||
| // The "creation" is not necessary means the audit log with of a request with verb "CREATE", but it is the first event of the target resource. | ||
| ChangeEventTypeTargetCreation | ||
| // ChangeEventTypeTargetDeletion is the event type when the target resource is deleted. | ||
| // Deletion event can happen multiple times when audit logs has no information on the content and the resource had multiple deletion requests in series. | ||
| ChangeEventTypeTargetDeletion | ||
| // ChangeEventTypeTargetModification is the event type when the target resource is modified. | ||
| ChangeEventTypeTargetModification | ||
| ) | ||
|
|
||
| // K8sResource represents a k8s resource. | ||
| type K8sResource struct { | ||
| // APIVersion is the API version of the resource. | ||
| APIVersion string | ||
| // Kind is the kind of the resource. | ||
| Kind string | ||
| // Name is the name of the resource. | ||
| Name string | ||
| // Namespace is the namespace of the resource. | ||
| Namespace string | ||
| // Subresource is the subresource name. | ||
| Subresource string | ||
| } | ||
|
|
||
| // ResourcePath returns the resource path. | ||
| func (r *K8sResource) ResourcePath() string { | ||
| if r.Subresource == "" { | ||
| return fmt.Sprintf("%s#%s#%s#%s", r.APIVersion, r.Kind, r.Namespace, r.Name) | ||
| } | ||
| return fmt.Sprintf("%s#%s#%s#%s#%s", r.APIVersion, r.Kind, r.Namespace, r.Name, r.Subresource) | ||
| } | ||
|
|
||
| // ResourcePair is the pair of the target resource and the source resource. | ||
| type ResourcePair struct { | ||
| // TargetGroup is the resource path of the target resource. | ||
| TargetGroup string | ||
| // SourceGroup is the resource path of the source resource. | ||
| SourceGroup string | ||
| } | ||
|
|
||
| // ResourceChangeEvent is the event of the resource change. | ||
| type ResourceChangeEvent struct { | ||
| // EventType is the type of the event. | ||
| EventType ChangeEventType | ||
| // Log is the log associated with the event. | ||
| Log *log.Log | ||
| // EventSourceResource is the source resource of the event. | ||
| EventSourceResource *K8sResource | ||
| // EventTargetResource is the target resource of the event. | ||
| EventTargetResource *K8sResource | ||
| // EventSourceBodyYAML is the YAML representation of the source resource body. | ||
| EventSourceBodyYAML string | ||
| // EventSourceBodyReader is the reader for the source resource body. | ||
| EventSourceBodyReader *structured.NodeReader | ||
| // EventTargetBodyYAML is the YAML representation of the target resource body. | ||
| EventTargetBodyYAML string | ||
| // EventTargetBodyReader is the reader for the target resource body. | ||
| EventTargetBodyReader *structured.NodeReader | ||
| } | ||
|
|
||
| // ManifestHistoryModifierTaskSetting is the setting for the manifest history modifier task. | ||
| type ManifestHistoryModifierTaskSetting[T any] interface { | ||
| // TaskID returns the task ID. | ||
| TaskID() taskid.TaskImplementationID[struct{}] | ||
| // LogSerializerTask returns the task reference for the log serializer task. | ||
| LogSerializerTask() taskid.TaskReference[[]*log.Log] | ||
| // GroupedLogTask returns the task reference for the grouped log task. | ||
| GroupedLogTask() taskid.TaskReference[ResourceChangeLogGroupMap] | ||
| // Dependencies returns the dependencies of the task. | ||
| Dependencies() []taskid.UntypedTaskReference | ||
| // PassCount returns the number of passes. | ||
| PassCount() int | ||
| // ResourcePairs returns the resource pairs. | ||
| ResourcePairs(ctx context.Context, groupedLogs ResourceChangeLogGroupMap) ([]ResourcePair, error) | ||
| // Process processes the event. | ||
| Process(ctx context.Context, passIndex int, event ResourceChangeEvent, cs *history.ChangeSet, builder *history.Builder, prevGroupData T) (T, error) | ||
| } | ||
|
|
||
| // NewManifestHistoryModifier creates a new manifest history modifier task. | ||
| // ManifestHistoryModifier is a task that generates a timeline of resource changes based on the processed manifests. | ||
| // It is designed to handle the relationship between two resources (Source and Target) and generate revisions for the Target resource based on the changes in the Source resource. | ||
| // For example, it can be used to generate a timeline of Pod status changes based on the Pod resource itself (Source=None, Target=Pod), or to generate a timeline of binding subresource but deleted when its parent Pod is deleted (Source=Pod, Target=Source pod's binding). | ||
| // The setting has ResourcePairs method that returns the resource pairs to know these pairs of target and source. | ||
| // | ||
| // The task works by iterating over the logs of the Source and Target resources in chronological order. | ||
| // It calls the Process method of the setting for each event, allowing the implementation to maintain state and generate revisions. | ||
| // | ||
| // Type Parameter T: | ||
| // The type parameter T represents the state that is passed between Process calls for the same resource pair. | ||
| // This allows the implementation to track the history of the resource and detect changes. | ||
| func NewManifestHistoryModifier[T any](setting ManifestHistoryModifierTaskSetting[T]) coretask.Task[struct{}] { | ||
| dependencies := append([]taskid.UntypedTaskReference{setting.LogSerializerTask(), setting.GroupedLogTask()}, setting.Dependencies()...) | ||
| return inspectiontaskbase.NewProgressReportableInspectionTask(setting.TaskID(), dependencies, func(ctx context.Context, taskMode inspectioncore_contract.InspectionTaskModeType, tp *inspectionmetadata.TaskProgressMetadata) (struct{}, error) { | ||
| if taskMode == inspectioncore_contract.TaskModeDryRun { | ||
| slog.DebugContext(ctx, "Skipping task because this is dry run mode") | ||
| return struct{}{}, nil | ||
| } | ||
|
|
||
| builder := khictx.MustGetValue(ctx, inspectioncore_contract.CurrentHistoryBuilder) | ||
| groupedLogs := coretask.GetTaskResult(ctx, setting.GroupedLogTask()) | ||
|
|
||
| tp.MarkIndeterminate() | ||
| trackingGroups, err := setting.ResourcePairs(ctx, groupedLogs) | ||
| if err != nil { | ||
| return struct{}{}, err | ||
| } | ||
| doneGroupCount := atomic.Int32{} | ||
|
|
||
| updator := progressutil.NewProgressUpdator(tp, time.Second, func(tp *inspectionmetadata.TaskProgressMetadata) { | ||
| current := doneGroupCount.Load() | ||
| tp.Percentage = float32(current) / float32(len(trackingGroups)) | ||
| tp.Message = fmt.Sprintf("%d/%d", current, len(trackingGroups)) | ||
| }) | ||
| updator.Start(ctx) | ||
|
|
||
| errGrp, childCtx := errgroup.WithContext(ctx) | ||
| errGrp.SetLimit(runtime.GOMAXPROCS(0)) | ||
| passCount := setting.PassCount() | ||
| for _, trackingGroup := range trackingGroups { | ||
| trackingGroup := trackingGroup | ||
| errGrp.Go(func() error { | ||
| defer doneGroupCount.Add(1) | ||
| changedPaths := map[string]struct{}{} | ||
| sourceLogs := groupedLogs[trackingGroup.SourceGroup] | ||
| targetLogs := groupedLogs[trackingGroup.TargetGroup] | ||
| if sourceLogs == nil { | ||
| sourceLogs = &ResourceChangeLogGroup{} | ||
| } | ||
| if targetLogs == nil { | ||
| targetLogs = &ResourceChangeLogGroup{} | ||
| } | ||
| var prevData T | ||
| for pass := 0; pass < passCount; pass++ { | ||
| for event := range iterateLogGroupPair(sourceLogs, targetLogs) { | ||
| cs := history.NewChangeSet(event.Log) | ||
| prevData, err = setting.Process(childCtx, pass, event, cs, builder, prevData) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| cp, err := cs.FlushToHistory(builder) | ||
| if err != nil { | ||
| slog.WarnContext(ctx, "failed to flush the changeset to history", "error", err) | ||
| } | ||
| for _, path := range cp { | ||
| changedPaths[path] = struct{}{} | ||
| } | ||
| } | ||
| } | ||
| for path := range changedPaths { | ||
| tb := builder.GetTimelineBuilder(path) | ||
| tb.Sort() | ||
| } | ||
| return nil | ||
| }) | ||
| } | ||
| if err := errGrp.Wait(); err != nil { | ||
| return struct{}{}, err | ||
| } | ||
|
|
||
| return struct{}{}, nil | ||
| }) | ||
| } | ||
|
|
||
| func iterateLogGroupPair(sourceLogs *ResourceChangeLogGroup, targetLogs *ResourceChangeLogGroup) func(func(ResourceChangeEvent) bool) { | ||
| sResource := PathToK8sResource(sourceLogs.Group) | ||
| tResource := PathToK8sResource(targetLogs.Group) | ||
| return func(fn func(ResourceChangeEvent) bool) { | ||
| slogIndex := 0 | ||
| tlogIndex := 0 | ||
| var pickFromSource bool | ||
| for slogIndex < len(sourceLogs.Logs) || tlogIndex < len(targetLogs.Logs) { | ||
| switch { | ||
| case tlogIndex >= len(targetLogs.Logs): | ||
| pickFromSource = true | ||
| case slogIndex >= len(sourceLogs.Logs): | ||
| pickFromSource = false | ||
| default: | ||
| sTime := log.MustGetFieldSet(sourceLogs.Logs[slogIndex].Log, &log.CommonFieldSet{}) | ||
| tTime := log.MustGetFieldSet(targetLogs.Logs[tlogIndex].Log, &log.CommonFieldSet{}) | ||
| if sTime.Timestamp.Before(tTime.Timestamp) { | ||
| pickFromSource = true | ||
| } else { | ||
| pickFromSource = false | ||
| } | ||
| } | ||
| var next ResourceChangeEvent | ||
| if pickFromSource { | ||
| sLog := sourceLogs.Logs[slogIndex] | ||
| var tLogReader *structured.NodeReader | ||
| var tLogBodyYAML string | ||
| if tlogIndex-1 >= 0 { | ||
| tLog := targetLogs.Logs[tlogIndex-1] | ||
| tLogReader = tLog.ResourceBodyReader | ||
| tLogBodyYAML = tLog.ResourceBodyYAML | ||
| } | ||
| eType := ChangeEventTypeSourceModification | ||
| if sLog.ResourceCreated { | ||
| eType = ChangeEventTypeSourceCreation | ||
| } | ||
| if sLog.ResourceDeleted { | ||
| eType = ChangeEventTypeSourceDeletion | ||
| } | ||
| next = ResourceChangeEvent{ | ||
| EventType: eType, | ||
| Log: sLog.Log, | ||
| EventSourceResource: sResource, | ||
| EventTargetResource: tResource, | ||
| EventSourceBodyYAML: sLog.ResourceBodyYAML, | ||
| EventSourceBodyReader: sLog.ResourceBodyReader, | ||
| EventTargetBodyYAML: tLogBodyYAML, | ||
| EventTargetBodyReader: tLogReader, | ||
| } | ||
| slogIndex++ | ||
| } else { | ||
| tLog := targetLogs.Logs[tlogIndex] | ||
| var sLogReader *structured.NodeReader | ||
| var sLogBodyYAML string | ||
| if slogIndex-1 >= 0 { | ||
| sLog := sourceLogs.Logs[slogIndex-1] | ||
| sLogReader = sLog.ResourceBodyReader | ||
| sLogBodyYAML = sLog.ResourceBodyYAML | ||
| } | ||
| eType := ChangeEventTypeTargetModification | ||
| if tLog.ResourceCreated { | ||
| eType = ChangeEventTypeTargetCreation | ||
| } | ||
| if tLog.ResourceDeleted { | ||
| eType = ChangeEventTypeTargetDeletion | ||
| } | ||
| next = ResourceChangeEvent{ | ||
| EventType: eType, | ||
| Log: tLog.Log, | ||
| EventSourceResource: sResource, | ||
| EventTargetResource: tResource, | ||
| EventSourceBodyYAML: sLogBodyYAML, | ||
| EventSourceBodyReader: sLogReader, | ||
| EventTargetBodyYAML: tLog.ResourceBodyYAML, | ||
| EventTargetBodyReader: tLog.ResourceBodyReader, | ||
| } | ||
| tlogIndex++ | ||
| } | ||
| if !fn(next) { | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // PathToK8sResource converts the resource path to the k8s resource. | ||
| func PathToK8sResource(path string) *K8sResource { | ||
| pathFragments := strings.Split(path, "#") | ||
| var apiVersion, kind, namespace, name, subresource string | ||
| if len(pathFragments) >= 1 { | ||
| apiVersion = pathFragments[0] | ||
| } | ||
| if len(pathFragments) >= 2 { | ||
| kind = pathFragments[1] | ||
| } | ||
| if len(pathFragments) >= 3 { | ||
| namespace = pathFragments[2] | ||
| } | ||
| if len(pathFragments) >= 4 { | ||
| name = pathFragments[3] | ||
| } | ||
| if len(pathFragments) >= 5 { | ||
| subresource = pathFragments[4] | ||
| } | ||
| return &K8sResource{ | ||
| APIVersion: apiVersion, | ||
| Kind: kind, | ||
| Namespace: namespace, | ||
| Name: name, | ||
| Subresource: subresource, | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.