Skip to content

Commit

Permalink
Add issuer option to be strict on certificate names
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Sep 6, 2024
1 parent e525cbd commit 62545b3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/pkg/rpaas/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ func (m *k8sRpaasManager) UpdateCertManagerRequest(ctx context.Context, instance
}
}

if issuerAnnotations[strictNamesAnnotation] == "true" && len(in.DNSNames) > 0 {
expectedName := strings.TrimPrefix(in.DNSNames[0], "*.")

if expectedName != in.Name {
return &ValidationError{Msg: fmt.Sprintf("the name of this certificate must be: %q", expectedName)}
}
}

if issuerAnnotations[allowWildcardAnnotation] == "false" {
for _, dnsName := range in.DNSNames {
if strings.HasPrefix(dnsName, "*") {
Expand Down
15 changes: 15 additions & 0 deletions internal/pkg/rpaas/certificates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func Test_k8sRpaasManager_UpdateCertManagerRequest(t *testing.T) {
maxDNSNamesAnnotation: "1",
maxIPsAnnotation: "0",
allowWildcardAnnotation: "false",
strictNamesAnnotation: "true",
},
},
},
Expand Down Expand Up @@ -274,6 +275,7 @@ func Test_k8sRpaasManager_UpdateCertManagerRequest(t *testing.T) {
"with forbidden use of wildcards": {
instanceName: "my-instance-1",
certManager: clientTypes.CertManager{
Name: "example.org",
DNSNames: []string{"*.example.org"},
},
cfg: config.RpaasConfig{
Expand All @@ -283,6 +285,19 @@ func Test_k8sRpaasManager_UpdateCertManagerRequest(t *testing.T) {
expectedError: "wildcard DNS names are not allowed on this issuer",
},

"with strict names": {
instanceName: "my-instance-1",
certManager: clientTypes.CertManager{
Name: "cert-1",
DNSNames: []string{"my-instance-1.example.com"},
},
cfg: config.RpaasConfig{
EnableCertManager: true,
DefaultCertManagerIssuer: "issuer-2",
},
expectedError: "the name of this certificate must be: \"my-instance-1.example.com\"",
},

"using wrong certificate issuer from configs": {
instanceName: "my-instance-1",
certManager: clientTypes.CertManager{
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/rpaas/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const (
externalDNSHostnameLabel = "external-dns.alpha.kubernetes.io/hostname"
allowedDNSZonesAnnotation = "rpaas.extensions.tsuru.io/allowed-dns-zones"
maxDNSNamesAnnotation = "rpaas.extensions.tsuru.io/cert-manager-max-dns-names"
strictNamesAnnotation = "rpaas.extensions.tsuru.io/cert-manager-strict-names"
maxIPsAnnotation = "rpaas.extensions.tsuru.io/cert-manager-max-ips"
allowWildcardAnnotation = "rpaas.extensions.tsuru.io/cert-manager-allow-wildcard"

Expand Down

0 comments on commit 62545b3

Please sign in to comment.