-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 2005816: make projectid and region anonymization consistent (#534)
* make projectid and region anonymization consistent * anonymized the rest * cluster-config-v1 * test * anonymize service accounts * lint
- Loading branch information
Serhii Zakharov
authored
Dec 15, 2021
1 parent
f23f521
commit 0fefe84
Showing
11 changed files
with
195 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
50 changes: 50 additions & 0 deletions
50
pkg/gatherers/clusterconfig/cluster_config_v1_config_map_test.go
This file contains 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,50 @@ | ||
package clusterconfig | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
kubefake "k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
func Test_gatherClusterConfigV1(t *testing.T) { | ||
coreClient := kubefake.NewSimpleClientset() | ||
|
||
_, err := coreClient.CoreV1().ConfigMaps("kube-system").Create(context.Background(), &corev1.ConfigMap{ | ||
TypeMeta: metav1.TypeMeta{}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "cluster-config-v1", | ||
}, | ||
Immutable: nil, | ||
Data: map[string]string{ | ||
"install-config": "{}", | ||
}, | ||
BinaryData: nil, | ||
}, metav1.CreateOptions{}) | ||
assert.NoError(t, err) | ||
|
||
records, errs := gatherClusterConfigV1(context.Background(), coreClient.CoreV1()) | ||
assert.Empty(t, errs) | ||
|
||
assert.Len(t, records, 1) | ||
assert.Equal(t, "config/configmaps/kube-system/cluster-config-v1", records[0].Name) | ||
|
||
data, err := records[0].Item.Marshal(context.Background()) | ||
assert.NoError(t, err) | ||
|
||
installConfig := `baseDomain: \"\"\nmetadata:\n creationTimestamp: null\nplatform: {}\npullSecret: \"\"\n` | ||
|
||
assert.JSONEq(t, `{ | ||
"metadata": { | ||
"name": "cluster-config-v1", | ||
"namespace": "kube-system", | ||
"creationTimestamp": null | ||
}, | ||
"data": { | ||
"install-config": "`+installConfig+`" | ||
} | ||
}`, string(data)) | ||
} |
This file contains 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 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 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 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 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,22 @@ | ||
package anonymize | ||
|
||
import ( | ||
"fmt" | ||
|
||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
// UnstructuredNestedStringField anonymizes the nested field in the unstructured object | ||
// or returns error if it is not possible | ||
func UnstructuredNestedStringField(data map[string]interface{}, fields ...string) error { | ||
value, found, err := unstructured.NestedFieldCopy(data, fields...) | ||
if err != nil { | ||
return err | ||
} | ||
if !found { | ||
return fmt.Errorf("unable to find field '%v'", fields) | ||
} | ||
|
||
valueStr, _ := value.(string) | ||
return unstructured.SetNestedField(data, String(valueStr), fields...) | ||
} |