Skip to content

Commit 581a735

Browse files
committed
ignore when only * is passed
1 parent 2e438cc commit 581a735

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

internal/controlplane/controlplane.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,21 @@ func (d *dashboardClient) Update(ctx context.Context, tctx *translator.Translate
6969
}
7070
}
7171
for _, ssl := range result.SSL {
72+
// to avoid duplication
73+
ssl.Snis = arrayUniqueElements(ssl.Snis, []string{})
74+
if len(ssl.Snis) == 1 && ssl.Snis[0] == "*" {
75+
// skip creating ssl without throwing error
76+
return nil
77+
}
78+
ssl.Snis = removeWildcard(ssl.Snis)
7279
oldssl, err := d.c.Cluster(name).SSL().Get(ctx, ssl.Cert)
7380
if err != nil || oldssl == nil {
7481
if _, err := d.c.Cluster(name).SSL().Create(ctx, ssl); err != nil {
7582
return fmt.Errorf("failed to create ssl for sni %+v: %w", ssl.Snis, err)
7683
}
7784
} else {
7885
// array union is done to avoid host duplication
79-
ssl.Snis = arrayUnion(ssl.Snis, oldssl.Snis)
86+
ssl.Snis = arrayUniqueElements(ssl.Snis, oldssl.Snis)
8087
if _, err := d.c.Cluster(name).SSL().Update(ctx, ssl); err != nil {
8188
return fmt.Errorf("failed to update ssl for sni %+v: %w", ssl.Snis, err)
8289
}
@@ -85,7 +92,17 @@ func (d *dashboardClient) Update(ctx context.Context, tctx *translator.Translate
8592
return nil
8693
}
8794

88-
func arrayUnion(arr1 []string, arr2 []string) []string {
95+
func removeWildcard(snis []string) []string {
96+
newSni := make([]string, 0)
97+
for _, sni := range snis {
98+
if sni != "*" {
99+
newSni = append(newSni, sni)
100+
}
101+
}
102+
return newSni
103+
}
104+
105+
func arrayUniqueElements(arr1 []string, arr2 []string) []string {
89106
// return a union of elements from both array
90107
presentEle := make(map[string]bool)
91108
newArr := make([]string, 0)

internal/controlplane/translator/gateway.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package translator
22

33
import (
4+
"crypto/x509"
45
"fmt"
56

67
v1 "github.com/api7/api7-ingress-controller/api/dashboard/v1"
@@ -43,11 +44,8 @@ func (t *Translator) translateSecret(tctx *TranslateContext, listener gatewayv1.
4344
if ref.Namespace != nil {
4445
ns = string(*ref.Namespace)
4546
}
46-
sslObj := &v1.Ssl{}
47-
sslObj.Snis = []string{}
48-
// Dashboard doesn't allow wildcard hostname
49-
if listener.Hostname != nil && *listener.Hostname != "" && *listener.Hostname != "*" {
50-
sslObj.Snis = append(sslObj.Snis, string(*listener.Hostname))
47+
sslObj := &v1.Ssl{
48+
Snis: []string{},
5149
}
5250
name := listener.TLS.CertificateRefs[0].Name
5351
secret := tctx.Secrets[types.NamespacedName{Namespace: ns, Name: string(ref.Name)}]
@@ -61,6 +59,15 @@ func (t *Translator) translateSecret(tctx *TranslateContext, listener gatewayv1.
6159
}
6260
sslObj.Cert = string(cert)
6361
sslObj.Key = string(key)
62+
// Dashboard doesn't allow wildcard hostname
63+
if listener.Hostname != nil && *listener.Hostname != "" {
64+
sslObj.Snis = append(sslObj.Snis, string(*listener.Hostname))
65+
}
66+
hosts, err := extractHost(cert)
67+
if err != nil {
68+
return nil, err
69+
}
70+
sslObj.Snis = append(sslObj.Snis, hosts...)
6471
// Note: Dashboard doesn't allow duplicate certificate across ssl objects
6572
sslObj.ID = id.GenID(sslObj.Cert)
6673
sslObj.Labels = label.GenLabel(obj)
@@ -76,6 +83,14 @@ func (t *Translator) translateSecret(tctx *TranslateContext, listener gatewayv1.
7683
return sslObjs, nil
7784
}
7885

86+
func extractHost(cert []byte) ([]string, error) {
87+
parsedCert, err := x509.ParseCertificate(cert)
88+
if err != nil {
89+
return nil, err
90+
}
91+
return parsedCert.DNSNames, nil
92+
}
93+
7994
func extractKeyPair(s *corev1.Secret, hasPrivateKey bool) ([]byte, []byte, error) {
8095
if _, ok := s.Data["cert"]; ok {
8196
return extractApisixSecretKeyPair(s, hasPrivateKey)

test/conformance/conformance_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ import (
1818
"sigs.k8s.io/yaml"
1919
)
2020

21+
var skippedTestsForSSL = []string{
22+
// Reason: https://github.com/kubernetes-sigs/gateway-api/blob/5c5fc388829d24e8071071b01e8313ada8f15d9f/conformance/utils/suite/suite.go#L358. SAN includes '*'
23+
tests.HTTPRouteHTTPSListener.ShortName,
24+
tests.HTTPRouteRedirectPortAndScheme.ShortName,
25+
}
26+
2127
var skippedTestsForTraditionalRoutes = []string{
2228
// TODO: Support ReferenceGrant resource
2329
tests.HTTPRouteInvalidReferenceGrant.ShortName,
@@ -68,7 +74,8 @@ func TestGatewayAPIConformance(t *testing.T) {
6874
opts.CleanupBaseResources = true
6975
opts.GatewayClassName = gatewayClassName
7076
opts.SupportedFeatures = sets.New(gatewaySupportedFeatures...)
71-
opts.SkipTests = skippedTestsForTraditionalRoutes
77+
opts.SkipTests = skippedTestsForSSL
78+
opts.SkipTests = append(opts.SkipTests, skippedTestsForTraditionalRoutes...)
7279
opts.Implementation = conformancev1.Implementation{
7380
Organization: "API7",
7481
Project: "api7-ingress-controller",

0 commit comments

Comments
 (0)