Skip to content

Commit

Permalink
implement issuance of temporary certificate when using VirtualServer …
Browse files Browse the repository at this point in the history
…cert-manager integration (#4408)
  • Loading branch information
svvac committed Oct 12, 2023
1 parent 732d174 commit 6a8633b
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 6 deletions.
2 changes: 2 additions & 0 deletions deployments/common/crds/k8s.nginx.org_virtualservers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,8 @@ spec:
type: string
duration:
type: string
issue-temp-cert:
type: boolean
issuer:
type: string
issuer-group:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ cert-manager:
|``duration`` | This field allows you to configure spec.duration field for the Certificate to be generated. Must be specified using a [Go time.Duration](https://pkg.go.dev/time#ParseDuration) string format, which does not allow the d (days) suffix. You must specify these values using s, m, and h suffixes instead. | ``string`` | No |
|``renew-before`` | this annotation allows you to configure spec.renewBefore field for the Certificate to be generated. Must be specified using a [Go time.Duration](https://pkg.go.dev/time#ParseDuration) string format, which does not allow the d (days) suffix. You must specify these values using s, m, and h suffixes instead. | ``string`` | No |
|``usages`` | This field allows you to configure spec.usages field for the Certificate to be generated. Pass a string with comma-separated values i.e. ``key agreement,digital signature, server auth``. An exhaustive list of supported key usages can be found in the [the cert-manager api documentation](https://cert-manager.io/docs/reference/api-docs/#cert-manager.io/v1.KeyUsage). | ``string`` | No |
|``issue-temp-cert`` | When ``true``, ask cert-manager for a [temporary self-signed certificate](https://cert-manager.io/docs/usage/certificate/#temporary-certificates-while-issuing) pending the issuance of the Certificate. This allows HTTPS-only servers to use ACME HTTP01 challenges when the TLS secret does not exist yet. | ``boolean`` | No |
{{% /table %}}

### VirtualServer.Listener
Expand Down
9 changes: 9 additions & 0 deletions internal/certmanager/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
issuerKindCmField = "tls.cert-manager.issuer-kind"
renewBeforeCmField = "tls.cert-manager.renew-before"
usagesCmField = "tls.cert-manager.usages"
certMgrTempCertAnnotation = "cert-manager.io/issue-temporary-certificate"
)

// translateVsSpec updates the Certificate spec using the VS TLS Cert-Manager
Expand Down Expand Up @@ -115,6 +116,14 @@ func translateVsSpec(crt *cmapi.Certificate, vsCmSpec *vsapi.CertManager) error
}
crt.Spec.Usages = newUsages
}

if vsCmSpec.IssueTempCert {
if crt.ObjectMeta.Annotations == nil {
crt.ObjectMeta.Annotations = make(map[string]string)
}
crt.ObjectMeta.Annotations[certMgrTempCertAnnotation] = "true"
}

if len(errs) > 0 {
return errors.New(strings.Join(errs, ", "))
}
Expand Down
19 changes: 19 additions & 0 deletions internal/certmanager/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ func Test_translateVsSpec(t *testing.T) {
Usages: "server auth,signing",
}

validSpecWithTempCert := vsapi.CertManager{
CommonName: "www.example.com",
Duration: "168h", // 1 week
RenewBefore: "24h",
Usages: "server auth,signing",
IssueTempCert: true,
}

invalidDuration := vsapi.CertManager{
Duration: "un-parsable duration",
}
Expand Down Expand Up @@ -71,6 +79,17 @@ func Test_translateVsSpec(t *testing.T) {
a.Equal([]cmapi.KeyUsage{cmapi.UsageServerAuth, cmapi.UsageSigning}, crt.Spec.Usages)
},
},
"success with temp cert": {
crt: gen.Certificate("example-cert"),
cmspec: &validSpecWithTempCert,
check: func(a *assert.Assertions, crt *cmapi.Certificate) {
a.Equal("www.example.com", crt.Spec.CommonName)
a.Equal(&metav1.Duration{Duration: time.Hour * 24 * 7}, crt.Spec.Duration)
a.Equal(&metav1.Duration{Duration: time.Hour * 24}, crt.Spec.RenewBefore)
a.Equal([]cmapi.KeyUsage{cmapi.UsageServerAuth, cmapi.UsageSigning}, crt.Spec.Usages)
a.Equal("true", crt.ObjectMeta.Annotations[certMgrTempCertAnnotation])
},
},
"nil cm spec": {
crt: gen.Certificate("example-cert"),
cmspec: nil,
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/configuration/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ type CertManager struct {
Duration string `json:"duration"`
RenewBefore string `json:"renew-before"`
Usages string `json:"usages"`
IssueTempCert bool `json:"issue-temp-cert"`
}

// VirtualServerStatus defines the status for the VirtualServer resource.
Expand Down
21 changes: 21 additions & 0 deletions pkg/apis/configuration/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions pkg/client/clientset/versioned/doc.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/client/informers/externalversions/factory.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6a8633b

Please sign in to comment.