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

chore: enhance data sanitizer to remove last-applied-configuration #357

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions src/services/data_sanitizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ def _sanitize_secret(self, obj: dict) -> dict:
def _sanitize_workload(self, obj: dict) -> dict:
"""Sanitize a workload object (Deployment, Pod, StatefulSet, DaemonSet)."""
try:
# First remove last-applied-configuration
obj = self._remove_last_applied_configuration(obj)

# Handle template-based resources (Deployment, StatefulSet, DaemonSet)
if "spec" in obj and "template" in obj["spec"]:
containers = obj["spec"]["template"]["spec"]["containers"]
Expand Down Expand Up @@ -172,6 +175,9 @@ def _sanitize_dict(self, data: dict) -> dict:
"""Recursively sanitize a dictionary by looking for sensitive data patterns."""
result = data.copy()

# First remove last-applied-configuration if exists
result = self._remove_last_applied_configuration(result)

for key, value in data.items():
# Check if the key should be excluded from sanitization
if (
Expand Down Expand Up @@ -204,3 +210,19 @@ def _clean_personal_information(self, data: dict) -> dict:
sanitized_data_str = self.scrubber.clean(data_str)

return dict(json.loads(sanitized_data_str))

@staticmethod
def _remove_last_applied_configuration(data: dict) -> dict:
"""Remove kubectl.kubernetes.io/last-applied-configuration annotation if it exists."""
if "metadata" in data and "annotations" in data["metadata"]:
if (
"kubectl.kubernetes.io/last-applied-configuration"
in data["metadata"]["annotations"]
):
del data["metadata"]["annotations"][
"kubectl.kubernetes.io/last-applied-configuration"
]
# Remove empty annotations dict if it's the last annotation
if not data["metadata"]["annotations"]:
del data["metadata"]["annotations"]
return data
110 changes: 110 additions & 0 deletions tests/unit/services/test_data_sanitizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,116 @@ def test_data_structures_and_pii(self, test_data, expected_results, error):
"defaultRuntimePodPreset": "M",
},
),
# delete "kubectl.kubernetes.io/last-applied-configuration" field completely
(
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"annotations": {
"deployment.kubernetes.io/revision": "1",
"kubectl.kubernetes.io/last-applied-configuration": '{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"nginx"},"name":"nginx","namespace":"test-sanit"},"spec":{"replicas":3,"selector":{"matchLabels":{"app":"nginx"}},"template":{"metadata":{"labels":{"app":"nginx"}},"spec":{"containers":[{"env":[{"name":"TOKEN","value":"test token"},{"name":"PASSWORD","value":"test password"},{"name":"CLIENT_ID","value":"test client ID"},{"name":"USER_NAME","value":"test user name"}],"image":"nginx:1.14.2","name":"nginx","ports":[{"containerPort":80}]}]}}}}\n',
},
},
"name": "nginx",
"namespace": "test-sanit",
"spec": {
"progressDeadlineSeconds": 600,
"replicas": 3,
"revisionHistoryLimit": 10,
"selector": {"matchLabels": {"app": "nginx"}},
"strategy": {
"rollingUpdate": {
"maxSurge": "25%",
"maxUnavailable": "25%",
},
"type": "RollingUpdate",
},
"template": {
"metadata": {
"labels": {"app": "nginx"},
},
"spec": {
"containers": [
{
"env": [
{"name": "TOKEN", "value": "test token"},
{
"name": "PASSWORD",
"value": "test password",
},
{
"name": "CLIENT_ID",
"value": "test client ID",
},
{
"name": "USER_NAME",
"value": "test user name",
},
],
"image": "nginx:1.14.2",
"name": "nginx",
"ports": [{"containerPort": 80}],
}
],
},
},
},
},
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"annotations": {
"deployment.kubernetes.io/revision": "1",
}
},
"name": "nginx",
"namespace": "test-sanit",
"spec": {
"progressDeadlineSeconds": 600,
"replicas": 3,
"revisionHistoryLimit": 10,
"selector": {"matchLabels": {"app": "nginx"}},
"strategy": {
"rollingUpdate": {
"maxSurge": "25%",
"maxUnavailable": "25%",
},
"type": "RollingUpdate",
},
"template": {
"metadata": {
"labels": {"app": "nginx"},
},
"spec": {
"containers": [
{
"env": [
{"name": "TOKEN", "value": REDACTED_VALUE},
{
"name": "PASSWORD",
"value": REDACTED_VALUE,
},
{
"name": "CLIENT_ID",
"value": REDACTED_VALUE,
},
{
"name": "USER_NAME",
"value": REDACTED_VALUE,
},
],
"image": "nginx:1.14.2",
"name": "nginx",
"ports": [{"containerPort": 80}],
}
],
},
},
},
},
),
],
)
def test_kubernetes_resources(self, test_data, expected_results):
Expand Down
Loading