Skip to content

Commit

Permalink
Added parameters notBefore and notAfter to the certificate requests s…
Browse files Browse the repository at this point in the history
…end to the NCM. They are calculated based on the Duration parameter defined in cert-manager.io/v1 Certificate object kind (notBefore is set to the current time when cert is being enrolled).
  • Loading branch information
Lukasz Swiacik committed Jan 7, 2025
1 parent 6b233c2 commit 3f49ece
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/ncmapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
type ExternalClient interface {
GetCAs() (*CAsResponse, error)
GetCA(path string) (*CAResponse, error)
SendCSR(pem []byte, CA *CAResponse, profileID string) (*CSRResponse, error)
SendCSR(pem []byte, CA *CAResponse,duration *metav1.Duration, profileID string) (*CSRResponse, error)
CheckCSRStatus(path string) (*CSRStatusResponse, error)
DownloadCertificate(path string) (*CertificateDownloadResponse, error)
DownloadCertificateInPEM(path string) ([]byte, error)
Expand Down
13 changes: 11 additions & 2 deletions pkg/ncmapi/ncmapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,15 +403,24 @@ func (c *Client) GetCA(path string) (*CAResponse, error) {
return &ca, nil
}

func (c *Client) SendCSR(pem []byte, CA *CAResponse, profileID string) (*CSRResponse, error) {
func (c *Client) SendCSR(pem []byte, CA *CAResponse, duration *metav1.Duration, profileID string) (*CSRResponse, error) {
filePath, err := ncmutil.WritePEMToTempFile(pem)
c.log.V(2).Info("Wrote certificate to temp PEM file", "path", filePath)
if err != nil {
return nil, &ClientError{Reason: "cannot write PEM to file", ErrorMessage: err}
}

certDuration := cmapi.DefaultCertificateDuration
if duration != nil {
certDuration = duration.Duration
}
notBefore := time.Now()
notAfter := notBefore.Add(certDuration)

params := map[string]string{
"ca": CA.Href,
"ca": CA.Href,
"notBefore": notBefore.Format(time.RFC3339),
"notAfter": notAfter.Format(time.RFC3339),
}

if profileID != "" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/provisioner/ncm.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (p *Provisioner) Sign(cr *cmapi.CertificateRequest) ([]byte, []byte, string
return p.handleAlreadySentCSR(cr.Namespace, cr.Annotations[cmapi.CertificateNameKey], certChain, wantedCA)
}

csrResp, err := p.NCMClient.SendCSR(cr.Spec.Request, signingCA, p.NCMConfig.ProfileID)
csrResp, err := p.NCMClient.SendCSR(cr.Spec.Request, signingCA, cr.Spec.Duration, p.NCMConfig.ProfileID)
if err != nil {
return nil, nil, "", fmt.Errorf("failed to send CSR, err: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/gen/ncmapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (fc *FakeClient) GetCA(path string) (*ncmapi.CAResponse, error) {
return fc.GetCAFn(path)
}

func (fc *FakeClient) SendCSR([]byte, *ncmapi.CAResponse, string) (*ncmapi.CSRResponse, error) {
func (fc *FakeClient) SendCSR([]byte, *ncmapi.CAResponse, *metav1.Duration, string) (*ncmapi.CSRResponse, error) {
return fc.SendCSRFn()
}

Expand Down

0 comments on commit 3f49ece

Please sign in to comment.