Skip to content

Commit

Permalink
feat: Add support to extra dns names (#52)
Browse files Browse the repository at this point in the history
* feat: Add support to extra dns names

Signed-off-by: Jorge Turrado <jorge_turrado@hotmail.es>

* reorder the code to have the common name the 1st

Signed-off-by: Jorge Turrado <jorge_turrado@hotmail.es>

Signed-off-by: Jorge Turrado <jorge_turrado@hotmail.es>
  • Loading branch information
JorTurFer committed Jan 11, 2023
1 parent fef091a commit 71c4f4e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ The following code snippet is taken from the Gatekeeper project:
CAName: caName,
CAOrganization: caOrganization,
DNSName: dnsName,
ExtraDNSNames: extraDnsNames,
IsReady: setupFinished,
VWHName: vwhName,
}); err != nil {
Expand Down
7 changes: 4 additions & 3 deletions pkg/rotator/rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ type CertRotator struct {
CAName string
CAOrganization string
DNSName string
ExtraDNSNames []string
IsReady chan struct{}
Webhooks []WebhookInfo
RestartOnSecretRefresh bool
Expand Down Expand Up @@ -475,14 +476,14 @@ func (cr *CertRotator) CreateCACert(begin, end time.Time) (*KeyPairArtifacts, er
// CreateCertPEM takes the results of CreateCACert and uses it to create the
// PEM-encoded public certificate and private key, respectively
func (cr *CertRotator) CreateCertPEM(ca *KeyPairArtifacts, begin, end time.Time) ([]byte, []byte, error) {
dnsNames := []string{cr.DNSName}
dnsNames = append(dnsNames, cr.ExtraDNSNames...)
templ := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{
CommonName: cr.DNSName,
},
DNSNames: []string{
cr.DNSName,
},
DNSNames: dnsNames,
NotBefore: begin,
NotAfter: end,
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
Expand Down
10 changes: 9 additions & 1 deletion pkg/rotator/rotator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ var (
CAName: "ca",
CAOrganization: "org",
DNSName: "service.namespace",
ExtraDNSNames: []string{
"other-service.namespace",
},
ExtKeyUsages: &[]x509.ExtKeyUsage{
x509.ExtKeyUsageClientAuth,
x509.ExtKeyUsageServerAuth,
Expand All @@ -48,7 +51,12 @@ func TestCertSigning(t *testing.T) {
}

if !cr.validServerCert(caArtifacts.CertPEM, cert, key) {
t.Error("Generated cert is not valid")
t.Error("Generated cert is not valid for common name")
}

valid, err := ValidCert(caArtifacts.CertPEM, cert, key, cr.ExtraDNSNames[0], cr.ExtKeyUsages, lookaheadTime())
if err != nil || !valid {
t.Error("Generated cert is not valid for ExtraDnsName")
}
}

Expand Down

0 comments on commit 71c4f4e

Please sign in to comment.