Skip to content

Commit c712388

Browse files
authored
OCPBUGS-33082: Add new use cases for networking obfuscation (#947)
* Remove unneeded utils function * Add configuration hosts domain to the sensitive values
1 parent 65df87e commit c712388

File tree

4 files changed

+32
-112
lines changed

4 files changed

+32
-112
lines changed

pkg/anonymization/anonymizer.go

+27-10
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ const (
5656
Ipv4Regex = `((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)`
5757
Ipv4NetworkRegex = Ipv4Regex + "/([0-9]{1,2})"
5858
Ipv4AddressOrNetworkRegex = Ipv4Regex + "(/([0-9]{1,2}))?"
59-
ClusterAPIServerPlaceholder = "<CLUSTER_API_SERVER>"
6059
ClusterBaseDomainPlaceholder = "<CLUSTER_BASE_DOMAIN>"
60+
ClusterHostPlaceholder = "<CLUSTER_DOMAIN_HOST>"
6161
UnableToCreateAnonymizerErrorMessage = "Unable to create anonymizer, " +
6262
"some data won't be anonymized(ipv4 and cluster base domain). The error is %v"
6363
clusterNetworksRecordName = "config/network.json"
@@ -109,6 +109,7 @@ func NewAnonymizerFromConfigClient(
109109
networkClient networkv1client.NetworkV1Interface,
110110
configurator configobserver.Interface,
111111
dataPolicy v1alpha1.DataPolicy,
112+
sensitiveVals map[string]string,
112113
) (*Anonymizer, error) {
113114
anonBuilder := &AnonBuilder{}
114115
anonBuilder.
@@ -120,19 +121,15 @@ func NewAnonymizerFromConfigClient(
120121
WithRunningInCluster(true).
121122
WithSecretsClient(kubeClient.CoreV1().Secrets(secretNamespace))
122123

123-
baseDomain, err := utils.GetClusterBaseDomain(ctx, configClient)
124-
if err != nil {
125-
return nil, err
124+
for value, placeholder := range sensitiveVals {
125+
anonBuilder.WithSensitiveValue(value, placeholder)
126126
}
127-
anonBuilder.WithSensitiveValue(baseDomain, ClusterBaseDomainPlaceholder)
128127

129-
APIServerURLs, err := utils.GetClusterAPIServerInfo(ctx, configClient)
128+
baseDomain, err := utils.GetClusterBaseDomain(ctx, configClient)
130129
if err != nil {
131130
return nil, err
132131
}
133-
for _, v := range APIServerURLs {
134-
anonBuilder.WithSensitiveValue(v, ClusterAPIServerPlaceholder)
135-
}
132+
anonBuilder.WithSensitiveValue(baseDomain, ClusterBaseDomainPlaceholder)
136133

137134
return anonBuilder.Build()
138135
}
@@ -310,15 +307,18 @@ func NewAnonymizerFromConfig(
310307
configurator configobserver.Interface,
311308
dataPolicy v1alpha1.DataPolicy,
312309
) (*Anonymizer, error) {
310+
sensitiveVals := make(map[string]string)
313311
kubeClient, err := kubernetes.NewForConfig(protoKubeConfig)
314312
if err != nil {
315313
return nil, err
316314
}
315+
sensitiveVals[extractDomain(protoKubeConfig.Host)] = ClusterHostPlaceholder
317316

318317
gatherKubeClient, err := kubernetes.NewForConfig(gatherProtoKubeConfig)
319318
if err != nil {
320319
return nil, err
321320
}
321+
sensitiveVals[extractDomain(gatherProtoKubeConfig.Host)] = ClusterHostPlaceholder
322322

323323
configClient, err := configv1client.NewForConfig(gatherKubeConfig)
324324
if err != nil {
@@ -329,8 +329,12 @@ func NewAnonymizerFromConfig(
329329
if err != nil {
330330
return nil, err
331331
}
332+
sensitiveVals[extractDomain(gatherKubeConfig.Host)] = ClusterHostPlaceholder
332333

333-
return NewAnonymizerFromConfigClient(ctx, kubeClient, gatherKubeClient, configClient, networkClient, configurator, dataPolicy)
334+
return NewAnonymizerFromConfigClient(ctx,
335+
kubeClient, gatherKubeClient, configClient, networkClient,
336+
configurator, dataPolicy, sensitiveVals,
337+
)
334338
}
335339

336340
// AnonymizeMemoryRecord takes record.MemoryRecord, removes the sensitive data from it and returns the same object
@@ -542,3 +546,16 @@ func getNextIP(originalIP net.IP, mask net.IPMask) (net.IP, bool) {
542546

543547
return resultIP, false
544548
}
549+
550+
// extractDomain truncates protocol, host and port of the URL argument
551+
// and returns the base domain
552+
func extractDomain(url string) string {
553+
baseDomain := strings.Join(strings.Split(url, ".")[1:], ".") // removes protocol and host parts
554+
domain := strings.Split(baseDomain, ":")[0] // removes port (if any)
555+
556+
if domain == "" { // in case the URL is malformed
557+
return url
558+
}
559+
560+
return domain
561+
}

pkg/anonymization/anonymizer_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func Test_GetNextIP(t *testing.T) {
116116

117117
func getAnonymizer(t *testing.T) *Anonymizer {
118118
clusterBaseDomain := "example.com"
119-
clusterAPIServer := "example.apiserver.com" // in HyperShift, API Server does not share base domain
119+
clusterConfigHost := "apiserver.com" // in HyperShift, API Server does not share base domain
120120
networks := []string{
121121
"127.0.0.0/8",
122122
"192.168.0.0/16",
@@ -131,7 +131,7 @@ func getAnonymizer(t *testing.T) *Anonymizer {
131131
anonBuilder := &AnonBuilder{}
132132
anonBuilder.
133133
WithSensitiveValue(clusterBaseDomain, ClusterBaseDomainPlaceholder).
134-
WithSensitiveValue(clusterAPIServer, ClusterAPIServerPlaceholder).
134+
WithSensitiveValue(clusterConfigHost, ClusterHostPlaceholder).
135135
WithConfigurator(mockConfigMapConfigurator).
136136
WithDataPolicy(v1alpha1.ObfuscateNetworking).
137137
WithNetworks(networks).
@@ -153,7 +153,7 @@ func Test_Anonymizer(t *testing.T) {
153153
nameTestCases := []testCase{
154154
{"node1.example.com", "node1.<CLUSTER_BASE_DOMAIN>"},
155155
{"api.example.com/test", "api.<CLUSTER_BASE_DOMAIN>/test"},
156-
{"https://example.apiserver.com:6443", "https://<CLUSTER_API_SERVER>:6443"},
156+
{"https://example.apiserver.com:6443", "https://example.<CLUSTER_DOMAIN_HOST>:6443"},
157157
}
158158
dataTestCases := []testCase{
159159
{"api.example.com\n127.0.0.1 ", "api.<CLUSTER_BASE_DOMAIN>\n127.0.0.1 "},
@@ -165,7 +165,7 @@ func Test_Anonymizer(t *testing.T) {
165165
{"192.168.1.255 ", "192.168.0.3 "},
166166
{"192.169.1.255 ", "0.0.0.0 "},
167167
{`{"key1": "val1", "key2": "127.0.0.128"'}`, `{"key1": "val1", "key2": "127.0.0.2"'}`},
168-
{`{"APIServerURL": "https://example.apiserver.com:6443"}`, `{"APIServerURL": "https://<CLUSTER_API_SERVER>:6443"}`},
168+
{`{"APIServerURL": "https://example.apiserver.com:6443"}`, `{"APIServerURL": "https://example.<CLUSTER_DOMAIN_HOST>:6443"}`},
169169
}
170170

171171
for _, testCase := range nameTestCases {
@@ -451,6 +451,7 @@ func TestNewAnonymizerFromConfigClient(t *testing.T) {
451451
networkClient,
452452
mockConfigMapConfigurator,
453453
v1alpha1.ObfuscateNetworking,
454+
make(map[string]string),
454455
)
455456
assert.NoError(t, err)
456457
assert.NotNil(t, anonymizer)

pkg/utils/apiserver.go

-35
This file was deleted.

pkg/utils/apiserver_test.go

-63
This file was deleted.

0 commit comments

Comments
 (0)