Skip to content

Commit

Permalink
Avoid to emit duplicates for enum fileds (#1748)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Peruffo <[email protected]>
  • Loading branch information
andreaTP authored Jan 3, 2024
1 parent fc5de82 commit 212bda1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
7 changes: 6 additions & 1 deletion pkg/components/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -1036,9 +1036,14 @@ func GetCrd() *extv1.CustomResourceDefinition {

cipherSuites := func() []extv1.JSON {
suites := []extv1.JSON{}
m := make(map[string]bool)
for _, p := range tlsProfiles(ocpv1.TLSProfiles).sortedKeys() {
for _, c := range ocpv1.TLSProfiles[p].Ciphers {
suites = append(suites, extv1.JSON{Raw: []byte(fmt.Sprintf("\"%s\"", c))})
if m[c] {
continue
}
m[c] = true
suites = append(suites, extv1.JSON{Raw: []byte(fmt.Sprintf(`"%s"`, c))})
}
}
return suites
Expand Down
15 changes: 15 additions & 0 deletions pkg/components/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,19 @@ var _ = Describe("Components", func() {
Expect(ris[2].Ref).To(Equal("quay.io/kubevirt/ovs-cni-marker@sha256:0f08d6b1550a90c9f10221f2bb07709d1090e7c675ee1a711981bd429074d620"))
})
})

Context("When calculating ciphers", func() {
It("should not generate duplicates", func() {
var ciphers = GetCrd().Spec.Versions[0].Schema.OpenAPIV3Schema.Properties["spec"].Properties["tlsSecurityProfile"].Properties["custom"].Properties["ciphers"].Items.Schema.Enum
var stringCiphers = make([]string, len(ciphers))
for i, c := range ciphers {
stringCiphers[i] = string(c.Raw[:])
}
for i, vi := range stringCiphers {
for j := i + 1; j < len(stringCiphers); j++ {
Expect(vi).ToNot(Equal(stringCiphers[j]))
}
}
})
})
})
20 changes: 18 additions & 2 deletions pkg/network/tlsSecurityProfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,26 @@ func SelectCipherSuitesAndMinTLSVersion(profile *ocpv1.TLSSecurityProfile) ([]st
Intermediate: &ocpv1.IntermediateTLSProfile{},
}
}
var ciphers []string
var minTlsVersion ocpv1.TLSProtocolVersion
if profile.Custom != nil {
return profile.Custom.TLSProfileSpec.Ciphers, profile.Custom.TLSProfileSpec.MinTLSVersion
ciphers = profile.Custom.TLSProfileSpec.Ciphers
minTlsVersion = profile.Custom.TLSProfileSpec.MinTLSVersion
} else {
ciphers = ocpv1.TLSProfiles[profile.Type].Ciphers
minTlsVersion = ocpv1.TLSProfiles[profile.Type].MinTLSVersion
}
return ocpv1.TLSProfiles[profile.Type].Ciphers, ocpv1.TLSProfiles[profile.Type].MinTLSVersion
m := make(map[string]bool)
var result []string
for _, c := range ciphers {
if m[c] {
continue
}
m[c] = true
result = append(result, c)
}

return result, minTlsVersion
}

func TLSVersionToHumanReadable(version ocpv1.TLSProtocolVersion) string {
Expand Down
20 changes: 20 additions & 0 deletions pkg/network/tlsSecurityProfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,24 @@ var _ = Describe("Testing TLS Security Profile", func() {
expectedMinTLSVersion: testCustomTLSProfileSpec.MinTLSVersion,
}),
)

Context("When selecting ciphers", func() {
It("should not generate duplicates", func() {
var profile = &ocpv1.TLSSecurityProfile{
Type: ocpv1.TLSProfileCustomType,
Custom: &ocpv1.CustomTLSProfile{
TLSProfileSpec: ocpv1.TLSProfileSpec{
Ciphers: []string{"foo", "foo", "bar"},
},
},
Intermediate: &ocpv1.IntermediateTLSProfile{},
}
var ciphers, _ = SelectCipherSuitesAndMinTLSVersion(profile)
for i, vi := range ciphers {
for j := i + 1; j < len(ciphers); j++ {
Expect(vi).ToNot(Equal(ciphers[j]))
}
}
})
})
})

0 comments on commit 212bda1

Please sign in to comment.