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

Kong cert order v2 #859

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1914,4 +1914,4 @@ sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
sourcegraph.com/sqs/pbtypes v0.0.0-20180604144634-d3ebe8f20ae4/go.mod h1:ketZ/q3QxT9HOBeFhu6RdvsftgpsbFHBF5Cas6cDKZ0=
sourcegraph.com/sqs/pbtypes v1.0.0/go.mod h1:3AciMUv4qUuRHRHhOG4TZOB+72GdPVz5k+c648qsFS4=
vbom.ml/util v0.0.0-20160121211510-db5cfe13f5cc/go.mod h1:so/NYdZXCz+E3ZpW0uAoCj6uzU2+8OWDFv/HxUSs7kI=
vbom.ml/util v0.0.0-20180919145318-efcd4e0f9787/go.mod h1:so/NYdZXCz+E3ZpW0uAoCj6uzU2+8OWDFv/HxUSs7kI=
vbom.ml/util v0.0.0-20180919145318-efcd4e0f9787/go.mod h1:so/NYdZXCz+E3ZpW0uAoCj6uzU2+8OWDFv/HxUSs7kI=
8 changes: 7 additions & 1 deletion internal/ingress/controller/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/pem"
"fmt"
"reflect"
"sort"
"strings"

"github.com/kong/go-kong/kong"
Expand Down Expand Up @@ -268,7 +269,9 @@ func getCerts(log logrus.FieldLogger, s store.Storer, secretsToSNIs map[string][
CreationTimestamp: secret.CreationTimestamp,
}
} else {
if kongCert.CreationTimestamp.After(secret.CreationTimestamp.Time) {
if kongCert.CreationTimestamp.After(secret.CreationTimestamp.Time) ||
(kongCert.CreationTimestamp.Equal(&secret.CreationTimestamp) &&
*kongCert.cert.ID > string(secret.UID)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question:
What is the logic behind this comparison?
Is there a reason why you have not opted for !=?

(blocker for merge)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
image
As shown in the figure above, when the certificate name and create_time of different namespace When time is consistent, there are problems
image
Because secretsToSNIs is a map, it is unordered

kongCert.cert.ID = kong.String(string(secret.UID))
kongCert.CreationTimestamp = secret.CreationTimestamp
}
Expand All @@ -280,6 +283,9 @@ func getCerts(log logrus.FieldLogger, s store.Storer, secretsToSNIs map[string][
kongCert.cert.SNIs = append(kongCert.cert.SNIs, kong.String(sni))
}
}
sort.Slice(kongCert.cert.SNIs, func(i, j int) bool {
return *kongCert.cert.SNIs[i] > *kongCert.cert.SNIs[j]
})
certs[cert+key] = kongCert
}
var res []kongstate.Certificate
Expand Down
93 changes: 93 additions & 0 deletions internal/ingress/controller/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2445,6 +2445,99 @@ func TestParserSecret(t *testing.T) {
assert.Equal(1, len(state.Certificates),
"SNIs are de-duplicated")
})
t.Run("Same secret from different namespace", func(t *testing.T) {
ingresses := []*networkingv1beta1.Ingress{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "default",
Annotations: map[string]string{
annotations.IngressClassKey: annotations.DefaultIngressClass,
},
},
Spec: networkingv1beta1.IngressSpec{
TLS: []networkingv1beta1.IngressTLS{
{
SecretName: "secret1",
Hosts: []string{"foo.com"},
},
},
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: "bar",
Namespace: "ns1",
Annotations: map[string]string{
annotations.IngressClassKey: annotations.DefaultIngressClass,
},
},
Spec: networkingv1beta1.IngressSpec{
TLS: []networkingv1beta1.IngressTLS{
{
SecretName: "secret1",
Hosts: []string{"bar.com"},
},
},
},
},
}

t1, _ := time.Parse(time.RFC3339, "2006-01-02T15:05:05Z")
secrets := []*corev1.Secret{
{
ObjectMeta: metav1.ObjectMeta{
UID: "3e8edeca-7d23-4e02-84c9-437d11b746a6",
Name: "secret1",
Namespace: "default",
CreationTimestamp: metav1.Time{
Time: t1,
},
},
Data: map[string][]byte{
"tls.crt": []byte(tlsPairs[0].Cert),
"tls.key": []byte(tlsPairs[0].Key),
},
},
{
ObjectMeta: metav1.ObjectMeta{
UID: "fc28a22c-41e1-4cd6-9099-fd7756ffe58e",
Name: "secret1",
Namespace: "ns1",
CreationTimestamp: metav1.Time{
Time: t1,
},
},
Data: map[string][]byte{
"tls.crt": []byte(tlsPairs[0].Cert),
"tls.key": []byte(tlsPairs[0].Key),
},
},
}
store, err := store.NewFakeStore(store.FakeObjects{
IngressesV1beta1: ingresses,
Secrets: secrets,
})
assert.Nil(err)
state, err := Build(logrus.New(), store)
assert.Nil(err)
assert.NotNil(state)
assert.Equal(1, len(state.Certificates),
"certificates are de-duplicated")
//
sort.SliceStable(state.Certificates[0].SNIs, func(i, j int) bool {
return strings.Compare(*state.Certificates[0].SNIs[i],
*state.Certificates[0].SNIs[j]) > 0
})
assert.Equal(kongstate.Certificate{
Certificate: kong.Certificate{
ID: kong.String("3e8edeca-7d23-4e02-84c9-437d11b746a6"),
Cert: kong.String(tlsPairs[0].Cert),
Key: kong.String(tlsPairs[0].Key),
SNIs: kong.StringSlice("foo.com", "bar.com"),
},
}, state.Certificates[0])
})
}

func TestPluginAnnotations(t *testing.T) {
Expand Down