Skip to content

Commit ef67d17

Browse files
BBBmaumoreinhardt
andauthored
Certificate signing request expiration seconds (#2559)
* Add expirationSeconds to CertificateSigningRequest * add changelog-entry * changelog --------- Co-authored-by: Moritz Reinhardt <[email protected]>
1 parent 3f7c172 commit ef67d17

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

.changelog/2559.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
`resource/kubernetes_certificate_signing_request_v1`: Add argument `spec.expiration_seconds`
3+
```

docs/resources/certificate_signing_request_v1.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ Custom signerNames can also be specified. The signer defines:
9797

9898
Optional:
9999

100+
- `expiration_seconds` (Integer) expirationSeconds is the requested duration of validity of the issued certificate.
101+
102+
The certificate signer may issue a certificate with a different validity duration so a client must check the delta between the notBefore and and notAfter fields in the issued certificate to determine the actual duration. The v1.22+ in-tree implementations of the well-known Kubernetes signers will honor this field as long as the requested duration is not greater than the maximum duration they will honor per the --cluster-signing-duration CLI flag to the Kubernetes controller manager.
103+
104+
Certificate signers may not honor this field for various reasons:
105+
106+
1. Old signer that is unaware of the field (such as the in-tree implementations prior to v1.22)
107+
2. Signer whose configured maximum is shorter than the requested duration
108+
3. Signer whose configured minimum is longer than the requested duration
109+
110+
The minimum valid value for expirationSeconds is 600, i.e. 10 minutes.
111+
100112
- `usages` (Set of String) usages specifies a set of key usages requested in the issued certificate.
101113

102114
Requests for TLS client certificates typically request: "digital signature", "key encipherment", "client auth".

kubernetes/resource_kubernetes_certificate_signing_request_v1.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ func resourceKubernetesCertificateSigningRequestV1() *schema.Resource {
5757
MaxItems: 1,
5858
Elem: &schema.Resource{
5959
Schema: map[string]*schema.Schema{
60+
"expiration_seconds": {
61+
Type: schema.TypeInt,
62+
Description: apiDocSpec["expirationSeconds"],
63+
Optional: true,
64+
ForceNew: true,
65+
},
6066
"request": {
6167
Type: schema.TypeString,
6268
Description: apiDocSpec["request"],

kubernetes/resource_kubernetes_certificate_signing_request_v1_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func TestAccKubernetesCertificateSigningRequestV1_basic(t *testing.T) {
3939
resource.TestCheckResourceAttrSet(resourceName, "certificate"),
4040
resource.TestCheckResourceAttr(resourceName, "spec.0.signer_name", signerName),
4141
resource.TestCheckResourceAttr(resourceName, "spec.0.usages.0", usages[0]),
42+
resource.TestCheckResourceAttr(resourceName, "spec.0.expiration_seconds", "604800"),
4243
),
4344
},
4445
},
@@ -150,7 +151,8 @@ func testAccKubernetesCertificateSigningRequestV1Config_basic(name, signerName s
150151
}
151152
auto_approve = %t
152153
spec {
153-
request = <<EOT
154+
expiration_seconds = 604800 # 1 week
155+
request = <<EOT
154156
-----BEGIN CERTIFICATE REQUEST-----
155157
MIHSMIGBAgEAMCoxGDAWBgNVBAoTD2V4YW1wbGUgY2x1c3RlcjEOMAwGA1UEAxMF
156158
YWRtaW4wTjAQBgcqhkjOPQIBBgUrgQQAIQM6AASSG8S2+hQvfMq5ucngPCzK0m0C
@@ -159,8 +161,8 @@ BAMCA0AAMD0CHQDErNLjX86BVfOsYh/A4zmjmGknZpc2u6/coTHqAhxcR41hEU1I
159161
DpNPvh30e0Js8/DYn2YUfu/pQU19
160162
-----END CERTIFICATE REQUEST-----
161163
EOT
162-
signer_name = %q
163-
usages = %q
164+
signer_name = %q
165+
usages = %q
164166
}
165167
}
166168
`, name, autoApprove, signerName, usages)

kubernetes/structures_certificate_signing_request_v1.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
88

99
certificates "k8s.io/api/certificates/v1"
10+
"k8s.io/utils/ptr"
1011
)
1112

1213
func expandCertificateSigningRequestV1Spec(csr []interface{}) *certificates.CertificateSigningRequestSpec {
@@ -15,6 +16,9 @@ func expandCertificateSigningRequestV1Spec(csr []interface{}) *certificates.Cert
1516
return obj
1617
}
1718
in := csr[0].(map[string]interface{})
19+
if v, ok := in["expiration_seconds"].(int); ok && v >= 600 {
20+
obj.ExpirationSeconds = ptr.To(int32(v))
21+
}
1822
obj.Request = []byte(in["request"].(string))
1923
if v, ok := in["usages"].(*schema.Set); ok && v.Len() > 0 {
2024
obj.Usages = expandCertificateSigningRequestV1Usages(v.List())

0 commit comments

Comments
 (0)