-
Notifications
You must be signed in to change notification settings - Fork 43
Backup SCC of Pod #376
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
Open
msfrucht
wants to merge
7
commits into
openshift:oadp-dev
Choose a base branch
from
msfrucht:backup_scc_of_pod
base: oadp-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+271
−31
Open
Backup SCC of Pod #376
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
029a5ff
backup SCC of pod
msfrucht 6ace8d8
resolve remaining uncovered lines
msfrucht 99dde3c
empty group from gvk, check expected identifiers
msfrucht 0fc4d62
skip lookup if scc annotation is empty
msfrucht 7d5fe9a
clarify test message
msfrucht c8e73ec
fix nit error =nil, remove unused security client
msfrucht d01f396
fix security client naming in clients doesn't match others
msfrucht 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
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
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
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
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
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,188 @@ | ||
| package pod | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/konveyor/openshift-velero-plugin/velero-plugins/clients" | ||
| "github.com/konveyor/openshift-velero-plugin/velero-plugins/common" | ||
| securityv1 "github.com/openshift/api/security/v1" | ||
| fakeSecurityClient "github.com/openshift/client-go/security/clientset/versioned/fake" | ||
| security "github.com/openshift/client-go/security/clientset/versioned/typed/security/v1" | ||
| "github.com/sirupsen/logrus" | ||
| "github.com/stretchr/testify/assert" | ||
| velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" | ||
| "github.com/vmware-tanzu/velero/pkg/plugin/velero" | ||
| corev1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| ktesting "k8s.io/client-go/testing" | ||
| ) | ||
|
|
||
| type ErrorType string | ||
|
|
||
| const ( | ||
| NotFoundError ErrorType = "NotFound" | ||
| AccessDenied ErrorType = "AccessDenied" | ||
| NoError ErrorType = "NoError" | ||
| ) | ||
|
|
||
| func getNewSecurityClient(errorType ErrorType, withSecurityClientError error, objects ...*securityv1.SecurityContextConstraints) func() (security.SecurityV1Interface, error) { | ||
| // the fake client has a weird bug where if the object is added to NewSimpleClientSet the resource value is set wrong | ||
| // as a result the object will not be found | ||
| // [key 0]: schema.GroupVersionResource {Group: "security.openshift.io", Version: "v1", Resource: "securitycontextconstraintses"} <--- should be securitycontextconstraints | ||
| cs := fakeSecurityClient.NewSimpleClientset() | ||
|
|
||
| // add error if enabled | ||
| switch errorType { | ||
| case NotFoundError: | ||
| cs.Fake.PrependReactor("get", "securitycontextconstraints", func(action ktesting.Action) (handled bool, ret runtime.Object, err error) { | ||
| return true, nil, errors.NewNotFound(schema.GroupResource{Group: "security.openshift.io", Resource: "securitycontextconstraints"}, "test-scc") | ||
| }) | ||
| case AccessDenied: | ||
| cs.Fake.PrependReactor("get", "securitycontextconstraints", func(action ktesting.Action) (handled bool, ret runtime.Object, err error) { | ||
| return true, nil, errors.NewForbidden(schema.GroupResource{Group: "security.openshift.io", Resource: "securitycontextconstraints"}, "test-scc", nil) | ||
| }) | ||
| case NoError: | ||
| // do nothing, client will return objects as normal | ||
| } | ||
|
|
||
| return func() (security.SecurityV1Interface, error) { | ||
| client := cs.SecurityV1() | ||
| for _, object := range objects { | ||
| _, localError := client.SecurityContextConstraints().Create(context.Background(), object, metav1.CreateOptions{}) | ||
| if localError != nil { | ||
| return nil, localError | ||
| } | ||
| } | ||
| return client, withSecurityClientError | ||
| } | ||
| } | ||
|
|
||
| func TestExecute_BackupPod(t *testing.T) { | ||
| originalSecurityClient := clients.SecurityClient | ||
| t.Cleanup(func() { | ||
| clients.SecurityClient = originalSecurityClient | ||
| }) | ||
|
|
||
| scc := &securityv1.SecurityContextConstraints{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-scc", | ||
| }, | ||
| } | ||
|
|
||
| pod := corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-pod", | ||
| Namespace: "test-namespace", | ||
| Annotations: map[string]string{ | ||
| common.SCCPodAnnotation: "test-scc", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| annotations map[string]string | ||
| inducedErrorType ErrorType | ||
| expectedSccCount int | ||
| shouldErr bool | ||
| expectedIdentifiers []velero.ResourceIdentifier | ||
| securityClientError error | ||
| }{ | ||
| { | ||
| name: "SCC exists and is added to additional items", | ||
| inducedErrorType: NoError, | ||
| expectedSccCount: 1, | ||
| shouldErr: false, | ||
| expectedIdentifiers: []velero.ResourceIdentifier{ | ||
| { | ||
| Name: "test-scc", | ||
| GroupResource: schema.GroupResource{ | ||
| Group: "security.openshift.io", | ||
| Resource: "securitycontextconstraints", | ||
| }, | ||
| }, | ||
| }, | ||
| annotations: map[string]string{ | ||
| common.SCCPodAnnotation: "test-scc", | ||
| }, | ||
| }, | ||
| { | ||
| name: "SCC does not exist and error is handled", | ||
| inducedErrorType: NotFoundError, | ||
| expectedSccCount: 0, | ||
| shouldErr: false, | ||
| expectedIdentifiers: []velero.ResourceIdentifier{}, | ||
| annotations: map[string]string{ | ||
| common.SCCPodAnnotation: "test-scc", | ||
| }, | ||
| }, | ||
| { | ||
| name: "error getting SCC for any other reason", | ||
| inducedErrorType: AccessDenied, | ||
| expectedSccCount: 0, | ||
| shouldErr: true, | ||
| expectedIdentifiers: []velero.ResourceIdentifier{}, | ||
| annotations: map[string]string{ | ||
| common.SCCPodAnnotation: "test-scc", | ||
| }, | ||
| }, | ||
| { | ||
| name: "pod has no annotations", | ||
| inducedErrorType: NoError, | ||
| expectedSccCount: 0, | ||
| shouldErr: false, | ||
| expectedIdentifiers: []velero.ResourceIdentifier{}, | ||
| annotations: nil, | ||
| }, | ||
| { | ||
| name: "security client initialize error", | ||
| inducedErrorType: NoError, | ||
| expectedSccCount: 0, | ||
| shouldErr: true, | ||
| expectedIdentifiers: []velero.ResourceIdentifier{}, | ||
| securityClientError: assert.AnError, | ||
| annotations: map[string]string{ | ||
| common.SCCPodAnnotation: "test-scc", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| pod.Annotations = test.annotations | ||
|
|
||
| objs := []*securityv1.SecurityContextConstraints{scc} | ||
|
|
||
| podData, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&pod) | ||
| assert.NoError(t, err) | ||
| unstructuredPod := unstructured.Unstructured{Object: podData} | ||
| clients.SecurityClient = getNewSecurityClient(test.inducedErrorType, test.securityClientError, objs...) | ||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| backupPlugin := &BackupPlugin{ | ||
| Log: logrus.WithField("plugin", "pod-backup-test"), | ||
| } | ||
|
|
||
| _, items, err := backupPlugin.Execute(&unstructuredPod, &velerov1.Backup{}) | ||
| if test.shouldErr { | ||
| assert.Error(t, err, "Test %s NOT errored when should %v", test.name, err) | ||
| } else { | ||
| assert.NoError(t, err, "Test %s errored when should succeed %v", test.name, err) | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert.Len(t, items, test.expectedSccCount, "Test %s Expected %d additional items for the SCC", test.name, test.expectedSccCount) | ||
| assert.ElementsMatch(t, items, test.expectedIdentifiers, "Test %s expected additional items to match expected identifiers", test.name) | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func TestAppliesTo(t *testing.T) { | ||
| backupPlugin := &BackupPlugin{ | ||
| Log: logrus.WithField("plugin", "pod-backup-test"), | ||
| } | ||
|
|
||
| resourceSelector, err := backupPlugin.AppliesTo() | ||
| assert.NoError(t, err) | ||
| assert.Contains(t, resourceSelector.IncludedResources, "pods", "Expected resource selector to include pods") | ||
| } | ||
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
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.
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.
🧩 Analysis chain
🏁 Script executed:
Repository: openshift/openshift-velero-plugin
Length of output: 110
🏁 Script executed:
Repository: openshift/openshift-velero-plugin
Length of output: 949
🏁 Script executed:
Repository: openshift/openshift-velero-plugin
Length of output: 2448
🏁 Script executed:
Repository: openshift/openshift-velero-plugin
Length of output: 59
🏁 Script executed:
Repository: openshift/openshift-velero-plugin
Length of output: 147
Fix race condition in lazy-initialized client getters across the file.
Lines 258-259 show an unsynchronized check-then-initialize pattern that is vulnerable to concurrent access races. The same issue exists in at least 9 other client getter functions (CoreClient, ImageClient, DiscoveryClient, RouteClient, BuildClient, AppsClient, OCPAppsClient, OCPConfigClient, OCPImageRegistryConfigClient). When multiple goroutines concurrently call these functions, they can race between the nil check and the initialization assignment, resulting in nondeterministic state.
Use
sync.Onceto synchronize initialization across all client getter functions. The sync package is not currently imported and must be added.🔧 Proposed fix (use
sync.Oncefor thread-safe one-time init)Apply the same pattern systematically to all 9+ remaining client getters.
🤖 Prompt for AI Agents