Skip to content

Commit d8e9278

Browse files
authored
Implement issuance of temporary certificate when using VirtualServer cert-manager integration
1 parent 732d174 commit d8e9278

File tree

8 files changed

+55
-6
lines changed

8 files changed

+55
-6
lines changed

deployments/common/crds/k8s.nginx.org_virtualservers.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,8 @@ spec:
535535
type: string
536536
duration:
537537
type: string
538+
issue-temp-cert:
539+
type: boolean
538540
issuer:
539541
type: string
540542
issuer-group:

docs/content/configuration/virtualserver-and-virtualserverroute-resources.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ cert-manager:
127127
|``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 |
128128
|``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 |
129129
|``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 |
130+
|``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 |
130131
{{% /table %}}
131132

132133
### VirtualServer.Listener

internal/certmanager/helper.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var (
4141
issuerKindCmField = "tls.cert-manager.issuer-kind"
4242
renewBeforeCmField = "tls.cert-manager.renew-before"
4343
usagesCmField = "tls.cert-manager.usages"
44+
certMgrTempCertAnnotation = "cert-manager.io/issue-temporary-certificate"
4445
)
4546

4647
// translateVsSpec updates the Certificate spec using the VS TLS Cert-Manager
@@ -115,6 +116,14 @@ func translateVsSpec(crt *cmapi.Certificate, vsCmSpec *vsapi.CertManager) error
115116
}
116117
crt.Spec.Usages = newUsages
117118
}
119+
120+
if vsCmSpec.IssueTempCert {
121+
if crt.ObjectMeta.Annotations == nil {
122+
crt.ObjectMeta.Annotations = make(map[string]string)
123+
}
124+
crt.ObjectMeta.Annotations[certMgrTempCertAnnotation] = "true"
125+
}
126+
118127
if len(errs) > 0 {
119128
return errors.New(strings.Join(errs, ", "))
120129
}

internal/certmanager/helper_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ func Test_translateVsSpec(t *testing.T) {
4444
Usages: "server auth,signing",
4545
}
4646

47+
validSpecWithTempCert := vsapi.CertManager{
48+
CommonName: "www.example.com",
49+
Duration: "168h", // 1 week
50+
RenewBefore: "24h",
51+
Usages: "server auth,signing",
52+
IssueTempCert: true,
53+
}
54+
4755
invalidDuration := vsapi.CertManager{
4856
Duration: "un-parsable duration",
4957
}
@@ -71,6 +79,17 @@ func Test_translateVsSpec(t *testing.T) {
7179
a.Equal([]cmapi.KeyUsage{cmapi.UsageServerAuth, cmapi.UsageSigning}, crt.Spec.Usages)
7280
},
7381
},
82+
"success with temp cert": {
83+
crt: gen.Certificate("example-cert"),
84+
cmspec: &validSpecWithTempCert,
85+
check: func(a *assert.Assertions, crt *cmapi.Certificate) {
86+
a.Equal("www.example.com", crt.Spec.CommonName)
87+
a.Equal(&metav1.Duration{Duration: time.Hour * 24 * 7}, crt.Spec.Duration)
88+
a.Equal(&metav1.Duration{Duration: time.Hour * 24}, crt.Spec.RenewBefore)
89+
a.Equal([]cmapi.KeyUsage{cmapi.UsageServerAuth, cmapi.UsageSigning}, crt.Spec.Usages)
90+
a.Equal("true", crt.ObjectMeta.Annotations[certMgrTempCertAnnotation])
91+
},
92+
},
7493
"nil cm spec": {
7594
crt: gen.Certificate("example-cert"),
7695
cmspec: nil,

pkg/apis/configuration/v1/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ type CertManager struct {
300300
Duration string `json:"duration"`
301301
RenewBefore string `json:"renew-before"`
302302
Usages string `json:"usages"`
303+
IssueTempCert bool `json:"issue-temp-cert"`
303304
}
304305

305306
// VirtualServerStatus defines the status for the VirtualServer resource.

pkg/apis/configuration/v1/zz_generated.deepcopy.go

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/client/clientset/versioned/doc.go

Lines changed: 0 additions & 4 deletions
This file was deleted.

pkg/client/informers/externalversions/factory.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)