-
Notifications
You must be signed in to change notification settings - Fork 24
[VC-46370] CyberArk: Skip deleted resources when converting data readings to snapshot #741
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -218,6 +218,19 @@ func TestExtractResourceListFromReading(t *testing.T) { | |
| }, | ||
| }, | ||
| }, | ||
| // Deleted resource should be ignored | ||
| { | ||
| DeletedAt: api.Time{Time: time.Now()}, | ||
| Resource: &unstructured.Unstructured{ | ||
| Object: map[string]interface{}{ | ||
| "kind": "Namespace", | ||
| "metadata": map[string]interface{}{ | ||
| "name": "kube-system", | ||
| "uid": "uid-kube-system", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An example of how this test fails before the implementation change. |
||
| }, | ||
| }, | ||
| }, | ||
|
|
@@ -270,6 +283,16 @@ func TestConvertDataReadings(t *testing.T) { | |
| }, | ||
| }, | ||
| }, | ||
| // Deleted secret should be ignored | ||
| { | ||
| DeletedAt: api.Time{Time: time.Now()}, | ||
| Resource: &corev1.Secret{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "deleted-1", | ||
| Namespace: "team-1", | ||
| }, | ||
| }, | ||
| }, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An example of how this test fails before the implementation change: |
||
| }, | ||
| }, | ||
| }, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,37 @@ | ||
| package k8s | ||
|
|
||
| // The venafi-kubernetes-agent has a requirement that **all** resources should | ||
| // be uploaded, even short-lived secrets, which are created and deleted | ||
| // in-between data uploads. A cache was added to the datagatherer code, to | ||
| // satisfy this requirement. The cache stores all resources for 5 minutes. And | ||
| // the informer event handlers (onAdd, onUpdate, onDelete) update the cache | ||
| // accordingly. The onDelete handler does not remove the object from the cache, | ||
| // but instead marks the object as deleted by setting the DeletedAt field on the | ||
| // GatheredResource. This ensures that deleted resources are still present in | ||
| // the cache for the duration of the cache expiry time. | ||
| // | ||
| // The cache expiry is hard coded to 5 minutes, which is longer than the | ||
| // venafi-kubernetes-agent default upload interval of 1 minute. This means that | ||
| // even if a resource is created and deleted in-between data gatherer runs, it | ||
| // will still be present in the cache when the data gatherer runs. | ||
| // | ||
| // TODO(wallrj): When the agent is deployed as CyberArk disco-agent, the deleted | ||
| // items are currently discarded before upload. If this remains the case, then the cache is unnecessary | ||
| // and should be disabled to save memory. | ||
| // If, in the future, the CyberArk Discovery and Context service does want to | ||
| // see deleted items, the "deleted resource reporting mechanism" will need to be | ||
| // redesigned, so that deleted items are retained for the duration of the upload | ||
| // interval. | ||
| // | ||
| // TODO(wallrj): When the agent is deployed as CyberArk disco-agent, the upload | ||
| // interval is 12 hours by default, so the 5 minute cache expiry is not | ||
| // sufficient. | ||
| // | ||
| // TODO(wallrj): The shared informer is configured to refresh all relist all | ||
| // resources every 1 minute, which will cause unnecessary load on the apiserver. | ||
| // We need to look back at the Git history and understand whether this was done | ||
| // for good reason or due to some misunderstanding. | ||
|
|
||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added these comments for context and as a reminder that this all needs to be improved. If I had more time, I might have had a go at refactoring all this code allow the cache to be optional; or perhaps having a smaller cache containing only the deleted resources. |
||
| import ( | ||
| "context" | ||
| "errors" | ||
|
|
@@ -197,6 +229,8 @@ func (c *ConfigDynamic) newDataGathererWithClient(ctx context.Context, cl dynami | |
|
|
||
| if informerFunc, ok := kubernetesNativeResources[c.GroupVersionResource]; ok { | ||
| factory := informers.NewSharedInformerFactoryWithOptions(clientset, | ||
| // TODO(wallrj): This causes all resources to be relisted every 1 | ||
| // minute which will cause unnecessary load on the apiserver. | ||
| 60*time.Second, | ||
| informers.WithNamespace(metav1.NamespaceAll), | ||
| informers.WithTweakListOptions(func(options *metav1.ListOptions) { | ||
|
|
@@ -207,6 +241,8 @@ func (c *ConfigDynamic) newDataGathererWithClient(ctx context.Context, cl dynami | |
| } else { | ||
| factory := dynamicinformer.NewFilteredDynamicSharedInformerFactory( | ||
| cl, | ||
| // TODO(wallrj): This causes all resources to be relisted every 1 | ||
| // minute which will cause unnecessary load on the apiserver. | ||
| 60*time.Second, | ||
| metav1.NamespaceAll, | ||
| func(options *metav1.ListOptions) { | ||
|
|
||
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.
This file is used by the tests in
cmd/agent_test.go.jetstack-secure/cmd/agent_test.go
Lines 34 to 41 in ca9ae2e
By capturing the HTTP request using mitmproxy we can see the deleted items are included in the snapshot before the changes to the implementation and absent after the implementation change:
BEFORE:
AFTER: