Skip to content

Commit

Permalink
Generate certs and private keys for tests
Browse files Browse the repository at this point in the history
Signed-off-by: Tero Saarni <[email protected]>
  • Loading branch information
tsaarni committed Jan 19, 2024
1 parent 61b6fae commit 32bc763
Show file tree
Hide file tree
Showing 24 changed files with 184 additions and 655 deletions.
135 changes: 68 additions & 67 deletions internal/featuretests/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ package featuretests
// kubernetes helpers

import (
"github.com/projectcontour/contour/internal/dag"
"github.com/projectcontour/contour/internal/fixture"
"github.com/tsaarni/certyaml"
v1 "k8s.io/api/core/v1"
networking_v1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -32,77 +35,75 @@ func IngressBackend(svc *v1.Service) *networking_v1.IngressBackend {
}
}

// nolint:revive,gosec
const (
// CERTIFICATE generated by
// openssl genrsa -out example-key.pem 2048
// openssl req -new -x509 -days 18250 -key example-key.pem -sha256 -subj "/CN=www.example.com" -out example.pem
CERTIFICATE = `-----BEGIN CERTIFICATE-----
MIIDFzCCAf+gAwIBAgIUZULFakfIJl0qaJXAVPCz2nzvB38wDQYJKoZIhvcNAQEL
BQAwGjEYMBYGA1UEAwwPd3d3LmV4YW1wbGUuY29tMCAXDTIyMDgxOTExMDkxNVoY
DzIwNzIwODA2MTEwOTE1WjAaMRgwFgYDVQQDDA93d3cuZXhhbXBsZS5jb20wggEi
MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDS9S4d/ea6wqiib8UeyHMptoks
w+q2DNuF75NQHLh5Z2rUnE/N8/KVhpIx81QdId1maWS0b3392hCRRFY3sDlMpRk/
1uoQgzLdk8pjw1JiqoDpvTiKZsADmVuUcCdHLNEzYtcLWBv0VyyNyE5pdrnVnMbx
w1aiQ8w2lcBCJQ8Y4DAc5oKlvBu49aUsvfFZwjL6Cr1qafQiYylqQcz7zqGBYjXc
iMzN+4fE1XQlw1iy6XmVZiHQr8Sb7EBI+g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j8Pr/4zDVsqqNz2gA3
yVb6kAEpg2ON9NOWSfoUfC+THitOnRm8pKL1QL7oiq/2+s3IiK+jSevEU7TUfjio
1LwtUqv1MbKdv7TgkU0YQ99iLocWF4F4oWF6AX86/BL9y7gcbE0y
-----END RSA PRIVATE KEY-----`

CRL = `-----BEGIN X509 CRL-----
MIHlMIGMAgEBMAoGCCqGSM49BAMCMBsxGTAXBgNVBAMTEGNsaWVudC1yb290LWNh
LTEXDTIyMDYyMTA5NDQ0NVoXDTIyMDYyODA5NDQ0NVowGzAZAggW+pmWu/XnExcN
MjIwNjIxMDk0NDQ1WqAjMCEwHwYDVR0jBBgwFoAULRlmjBtfjbzwV2WeO9Vj5pWO
h5gwCgYIKoZIzj0EAwIDSAAwRQIhANVFCqByuASAcbz6ovyvi5KCtPfNjHjxVaNT
x69LFPN1AiA5pF5rqHy1FBctZBTW+3LTEEX35j3p1++zcNu8oHMO/w==
-----END X509 CRL-----`
)
var CACertificate = certyaml.Certificate{
Subject: "CN=ca",
}

var ServerCertificate = certyaml.Certificate{
Issuer: &CACertificate,
Subject: "CN=www.example.com",
SubjectAltNames: []string{"DNS:www.example.com"},
}

var ClientCertificate = certyaml.Certificate{
Issuer: &CACertificate,
Subject: "CN=client",
KeyType: certyaml.KeyTypeRSA,
}

func Secretdata(cert, key string) map[string][]byte {
return map[string][]byte{
v1.TLSCertKey: []byte(cert),
v1.TLSPrivateKeyKey: []byte(key),
var CRL = certyaml.CRL{
Issuer: &CACertificate,
}

func TLSSecret(name string, credential *certyaml.Certificate) *v1.Secret {
cert, key, err := credential.PEM()
if err != nil {
panic(err)

Check warning on line 61 in internal/featuretests/kubernetes.go

View check run for this annotation

Codecov / codecov/patch

internal/featuretests/kubernetes.go#L61

Added line #L61 was not covered by tests
}
return &v1.Secret{
ObjectMeta: fixture.ObjectMeta(name),
Type: v1.SecretTypeTLS,
Data: map[string][]byte{
v1.TLSCertKey: cert,
v1.TLSPrivateKeyKey: key,
},
}
}

func CASecret(name string, credential *certyaml.Certificate) *v1.Secret {
cert, _, err := credential.PEM()
if err != nil {
panic(err)

Check warning on line 76 in internal/featuretests/kubernetes.go

View check run for this annotation

Codecov / codecov/patch

internal/featuretests/kubernetes.go#L76

Added line #L76 was not covered by tests
}
return &v1.Secret{
ObjectMeta: fixture.ObjectMeta(name),
Type: v1.SecretTypeOpaque,
Data: map[string][]byte{
dag.CACertificateKey: cert,
},
}
}

func CRLSecret(name string, credential *certyaml.CRL) *v1.Secret {
crl, err := credential.PEM()
if err != nil {
panic(err)

Check warning on line 90 in internal/featuretests/kubernetes.go

View check run for this annotation

Codecov / codecov/patch

internal/featuretests/kubernetes.go#L90

Added line #L90 was not covered by tests
}

return &v1.Secret{
ObjectMeta: fixture.ObjectMeta(name),
Type: v1.SecretTypeOpaque,
Data: map[string][]byte{
dag.CRLKey: crl,
},
}
}

func PEMBytes(cert *certyaml.Certificate) []byte {
c, _, _ := cert.PEM()
return c
}

func Endpoints(ns, name string, subsets ...v1.EndpointSubset) *v1.Endpoints {
return &v1.Endpoints{
ObjectMeta: metav1.ObjectMeta{
Expand Down
35 changes: 5 additions & 30 deletions internal/featuretests/v3/authorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,7 @@ func authzResponseTimeout(t *testing.T, rh ResourceEventHandlerWrapper, c *Conto
envoy_v3.TLSInspector(),
),
FilterChains: []*envoy_listener_v3.FilterChain{
filterchaintls(fqdn,
&corev1.Secret{
ObjectMeta: fixture.ObjectMeta("certificate"),
Type: "kubernetes.io/tls",
Data: featuretests.Secretdata(featuretests.CERTIFICATE, featuretests.RSA_PRIVATE_KEY),
},
filterchaintls(fqdn, featuretests.TLSSecret("certificate", &featuretests.ServerCertificate),
authzFilterFor(
fqdn,
&envoy_config_filter_http_ext_authz_v3.ExtAuthz{
Expand Down Expand Up @@ -172,12 +167,7 @@ func authzFailOpen(t *testing.T, rh ResourceEventHandlerWrapper, c *Contour) {
envoy_v3.TLSInspector(),
),
FilterChains: []*envoy_listener_v3.FilterChain{
filterchaintls(fqdn,
&corev1.Secret{
ObjectMeta: fixture.ObjectMeta("certificate"),
Type: "kubernetes.io/tls",
Data: featuretests.Secretdata(featuretests.CERTIFICATE, featuretests.RSA_PRIVATE_KEY),
},
filterchaintls(fqdn, featuretests.TLSSecret("certificate", &featuretests.ServerCertificate),
authzFilterFor(
fqdn,
&envoy_config_filter_http_ext_authz_v3.ExtAuthz{
Expand Down Expand Up @@ -487,12 +477,7 @@ func authzInvalidReference(t *testing.T, rh ResourceEventHandlerWrapper, c *Cont
envoy_v3.TLSInspector(),
),
FilterChains: []*envoy_listener_v3.FilterChain{
filterchaintls(fqdn,
&corev1.Secret{
ObjectMeta: fixture.ObjectMeta("certificate"),
Type: "kubernetes.io/tls",
Data: featuretests.Secretdata(featuretests.CERTIFICATE, featuretests.RSA_PRIVATE_KEY),
},
filterchaintls(fqdn, featuretests.TLSSecret("certificate", &featuretests.ServerCertificate),
authzFilterFor(
fqdn,
&envoy_config_filter_http_ext_authz_v3.ExtAuthz{
Expand Down Expand Up @@ -551,12 +536,7 @@ func authzWithRequestBodyBufferSettings(t *testing.T, rh ResourceEventHandlerWra
envoy_v3.TLSInspector(),
),
FilterChains: []*envoy_listener_v3.FilterChain{
filterchaintls(fqdn,
&corev1.Secret{
ObjectMeta: fixture.ObjectMeta("certificate"),
Type: "kubernetes.io/tls",
Data: featuretests.Secretdata(featuretests.CERTIFICATE, featuretests.RSA_PRIVATE_KEY),
},
filterchaintls(fqdn, featuretests.TLSSecret("certificate", &featuretests.ServerCertificate),
authzFilterFor(
fqdn,
&envoy_config_filter_http_ext_authz_v3.ExtAuthz{
Expand Down Expand Up @@ -631,12 +611,7 @@ func TestAuthorization(t *testing.T) {
Ports: featuretests.Ports(featuretests.Port("", 80)),
}))

rh.OnAdd(&corev1.Secret{
ObjectMeta: fixture.ObjectMeta("certificate"),
Type: "kubernetes.io/tls",
Data: featuretests.Secretdata(featuretests.CERTIFICATE, featuretests.RSA_PRIVATE_KEY),
})

rh.OnAdd(featuretests.TLSSecret("certificate", &featuretests.ServerCertificate))
f(t, rh, c)
})
}
Expand Down
23 changes: 6 additions & 17 deletions internal/featuretests/v3/backendcavalidation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,17 @@ import (

envoy_discovery_v3 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
contour_api_v1 "github.com/projectcontour/contour/apis/projectcontour/v1"
"github.com/projectcontour/contour/internal/dag"
"github.com/projectcontour/contour/internal/featuretests"
"github.com/projectcontour/contour/internal/fixture"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)

func TestClusterServiceTLSBackendCAValidation(t *testing.T) {
rh, c, done := setup(t)
defer done()

secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "default",
},
Type: v1.SecretTypeOpaque,
Data: map[string][]byte{
dag.CACertificateKey: []byte(featuretests.CERTIFICATE),
},
}
caSecret := featuretests.CASecret("foo", &featuretests.CACertificate)

svc := fixture.NewService("default/kuard").
Annotate("projectcontour.io/upstream-protocol.tls", "securebackend,443").
Expand All @@ -60,7 +49,7 @@ func TestClusterServiceTLSBackendCAValidation(t *testing.T) {
}},
},
}
rh.OnAdd(secret)
rh.OnAdd(caSecret)
rh.OnAdd(svc)
rh.OnAdd(p1)

Expand Down Expand Up @@ -93,7 +82,7 @@ func TestClusterServiceTLSBackendCAValidation(t *testing.T) {
Name: svc.Name,
Port: 443,
UpstreamValidation: &contour_api_v1.UpstreamValidation{
CACertificate: secret.Name,
CACertificate: caSecret.Name,
SubjectName: "subjname",
},
}},
Expand All @@ -114,7 +103,7 @@ func TestClusterServiceTLSBackendCAValidation(t *testing.T) {
// assert that the cluster now has a certificate and subject name.
c.Request(clusterType).Equals(&envoy_discovery_v3.DiscoveryResponse{
Resources: resources(t,
tlsCluster(cluster("default/kuard/443/c6ccd34de5", "default/kuard/securebackend", "default_kuard_443"), []byte(featuretests.CERTIFICATE), "subjname", "", nil, nil),
tlsCluster(cluster("default/kuard/443/c6ccd34de5", "default/kuard/securebackend", "default_kuard_443"), &featuretests.CACertificate, "subjname", "", nil, nil),
),
TypeUrl: clusterType,
})
Expand All @@ -140,7 +129,7 @@ func TestClusterServiceTLSBackendCAValidation(t *testing.T) {
Name: svc.Name,
Port: 443,
UpstreamValidation: &contour_api_v1.UpstreamValidation{
CACertificate: secret.Name,
CACertificate: caSecret.Name,
SubjectName: "subjname",
},
}},
Expand All @@ -161,7 +150,7 @@ func TestClusterServiceTLSBackendCAValidation(t *testing.T) {
// assert that the cluster now has a certificate and subject name.
c.Request(clusterType).Equals(&envoy_discovery_v3.DiscoveryResponse{
Resources: resources(t,
tlsCluster(cluster("default/kuard/443/c6ccd34de5", "default/kuard/securebackend", "default_kuard_443"), []byte(featuretests.CERTIFICATE), "subjname", "", nil, nil),
tlsCluster(cluster("default/kuard/443/c6ccd34de5", "default/kuard/securebackend", "default_kuard_443"), &featuretests.CACertificate, "subjname", "", nil, nil),
),
TypeUrl: clusterType,
})
Expand Down
Loading

0 comments on commit 32bc763

Please sign in to comment.