Skip to content

Commit

Permalink
Merge pull request #503 from alexandrevilain/feat/add-support-for-ren…
Browse files Browse the repository at this point in the history
…ewBefore

feat(mtls): add support for certificate renewBefore field
  • Loading branch information
alexandrevilain authored Oct 1, 2023
2 parents 2c38a49 + e66d69a commit d6f6c4d
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 19 deletions.
6 changes: 6 additions & 0 deletions api/v1beta1/temporalcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,12 @@ type MTLSSpec struct {
// Useless if mTLS provider is not cert-manager.
// +optional
RefreshInterval *metav1.Duration `json:"refreshInterval"`
// RenewBefore is defines how long before the currently issued certificate's expiry
// cert-manager should renew the certificate. The default is 2/3 of the
// issued certificate's duration. Minimum accepted value is 5 minutes.
// Useless if mTLS provider is not cert-manager.
// +optional
RenewBefore *metav1.Duration `json:"renewBefore,omitempty"`
}

func (m *MTLSSpec) InternodeEnabled() bool {
Expand Down
25 changes: 25 additions & 0 deletions api/v1beta1/temporalcluster_validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package v1beta1

import (
"time"

"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

func (m *MTLSSpec) Validate() (admission.Warnings, field.ErrorList) {
var warns admission.Warnings
var errs field.ErrorList

if m == nil || m.Provider != CertManagerMTLSProvider {
return nil, nil
}

if m.RenewBefore != nil {
if m.RenewBefore.Duration < 5*time.Minute {
errs = append(errs, field.Invalid(field.NewPath("spec.mTLS.renewBefore"), m.RenewBefore, "must be at least 5 minutes"))
}
}

return warns, errs
}
5 changes: 5 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

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

3 changes: 3 additions & 0 deletions config/crd/bases/temporal.io_temporalclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ spec:
refreshInterval:
description: RefreshInterval defines interval between refreshes of certificates in the cluster components. Defaults to 1 hour. Useless if mTLS provider is not cert-manager.
type: string
renewBefore:
description: RenewBefore is defines how long before the currently issued certificate's expiry cert-manager should renew the certificate. The default is 2/3 of the issued certificate's duration. Minimum accepted value is 5 minutes. Useless if mTLS provider is not cert-manager.
type: string
type: object
metrics:
description: Metrics allows configuration of scraping endpoints for stats. prometheus or m3.
Expand Down
17 changes: 17 additions & 0 deletions docs/api/v1beta1.md
Original file line number Diff line number Diff line change
Expand Up @@ -2199,6 +2199,23 @@ Defaults to 1 hour.
Useless if mTLS provider is not cert-manager.</p>
</td>
</tr>
<tr>
<td>
<code>renewBefore</code><br>
<em>
<a href="https://pkg.go.dev/k8s.io/apimachinery/pkg/apis/meta/v1#Duration">
Kubernetes meta/v1.Duration
</a>
</em>
</td>
<td>
<em>(Optional)</em>
<p>RenewBefore is defines how long before the currently issued certificate&rsquo;s expiry
cert-manager should renew the certificate. The default is <sup>2</sup>&frasl;<sub>3</sub> of the
issued certificate&rsquo;s duration. Minimum accepted value is 5 minutes.
Useless if mTLS provider is not cert-manager.</p>
</td>
</tr>
</tbody>
</table>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ func (b *GenericFrontendClientCertificateBuilder) Update(object client.Object) e
certificate.Labels = object.GetLabels()
certificate.Annotations = object.GetAnnotations()
certificate.Spec = certmanagerv1.CertificateSpec{
SecretName: b.instance.ChildResourceName(GetCertificateSecretName(b.name)),
CommonName: fmt.Sprintf("%s client certificate", b.name),
Duration: b.instance.Spec.MTLS.CertificatesDuration.ClientCertificates,
SecretName: b.instance.ChildResourceName(GetCertificateSecretName(b.name)),
CommonName: fmt.Sprintf("%s client certificate", b.name),
Duration: b.instance.Spec.MTLS.CertificatesDuration.ClientCertificates,
RenewBefore: b.instance.Spec.MTLS.RenewBefore,
PrivateKey: &certmanagerv1.CertificatePrivateKey{
RotationPolicy: certmanagerv1.RotationPolicyAlways,
Encoding: certmanagerv1.PKCS8,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ func (b *GenericItermediateCACertificateBuilder) Update(object client.Object) er
certificate.Labels = object.GetLabels()
certificate.Annotations = object.GetAnnotations()
certificate.Spec = certmanagerv1.CertificateSpec{
IsCA: true,
SecretName: b.instance.ChildResourceName(b.secretName),
CommonName: b.commonName,
Duration: b.instance.Spec.MTLS.CertificatesDuration.IntermediateCAsCertificates,
PrivateKey: caCertificatePrivateKey,
IsCA: true,
SecretName: b.instance.ChildResourceName(b.secretName),
CommonName: b.commonName,
Duration: b.instance.Spec.MTLS.CertificatesDuration.IntermediateCAsCertificates,
RenewBefore: b.instance.Spec.MTLS.RenewBefore,
PrivateKey: caCertificatePrivateKey,
DNSNames: []string{
b.instance.ServerName(),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ func (b *MTLSFrontendCertificateBuilder) Update(object client.Object) error {
certificate.Labels = object.GetLabels()
certificate.Annotations = object.GetAnnotations()
certificate.Spec = certmanagerv1.CertificateSpec{
SecretName: b.instance.ChildResourceName(FrontendCertificate),
CommonName: "Frontend Certificate",
Duration: b.instance.Spec.MTLS.CertificatesDuration.FrontendCertificate,
SecretName: b.instance.ChildResourceName(FrontendCertificate),
CommonName: "Frontend Certificate",
Duration: b.instance.Spec.MTLS.CertificatesDuration.FrontendCertificate,
RenewBefore: b.instance.Spec.MTLS.RenewBefore,
PrivateKey: &certmanagerv1.CertificatePrivateKey{
RotationPolicy: certmanagerv1.RotationPolicyAlways,
Encoding: certmanagerv1.PKCS8,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ func (b *MTLSInternodeCertificateBuilder) Update(object client.Object) error {
certificate.Labels = object.GetLabels()
certificate.Annotations = object.GetAnnotations()
certificate.Spec = certmanagerv1.CertificateSpec{
SecretName: b.instance.ChildResourceName(InternodeCertificate),
CommonName: "Internode Certificate",
Duration: b.instance.Spec.MTLS.CertificatesDuration.InternodeCertificate,
SecretName: b.instance.ChildResourceName(InternodeCertificate),
CommonName: "Internode Certificate",
Duration: b.instance.Spec.MTLS.CertificatesDuration.InternodeCertificate,
RenewBefore: b.instance.Spec.MTLS.RenewBefore,
PrivateKey: &certmanagerv1.CertificatePrivateKey{
RotationPolicy: certmanagerv1.RotationPolicyAlways,
Encoding: certmanagerv1.PKCS8,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ func (b *MTLSRootCACertificateBuilder) Update(object client.Object) error {
certificate.Labels = object.GetLabels()
certificate.Annotations = object.GetAnnotations()
certificate.Spec = certmanagerv1.CertificateSpec{
IsCA: true,
Duration: b.instance.Spec.MTLS.CertificatesDuration.RootCACertificate,
SecretName: b.instance.ChildResourceName(rootCaCertificate),
CommonName: "Root CA certificate",
PrivateKey: caCertificatePrivateKey,
IsCA: true,
Duration: b.instance.Spec.MTLS.CertificatesDuration.RootCACertificate,
RenewBefore: b.instance.Spec.MTLS.RenewBefore,
SecretName: b.instance.ChildResourceName(rootCaCertificate),
CommonName: "Root CA certificate",
PrivateKey: caCertificatePrivateKey,
DNSNames: []string{
b.instance.ServerName(),
},
Expand Down
4 changes: 4 additions & 0 deletions webhooks/temporalcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func (w *TemporalClusterWebhook) validateCluster(cluster *v1beta1.TemporalCluste
)
}

mTLSWarnings, mTLSErrors := cluster.Spec.MTLS.Validate()
warns = append(warns, mTLSWarnings...)
errs = append(errs, mTLSErrors...)

// Validate that the cluster version is a supported one.
err := cluster.Spec.Version.Validate()
if err != nil {
Expand Down

0 comments on commit d6f6c4d

Please sign in to comment.