Skip to content

Commit af1cecd

Browse files
committed
Update pki issuer target secret output to include ca cert and chain
1 parent c2030ef commit af1cecd

4 files changed

Lines changed: 39 additions & 31 deletions

File tree

build/install.yaml

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@ spec:
4848
description: IssuerSpec defines the desired state of Issuer
4949
properties:
5050
authentication:
51-
description: |-
52-
A reference to a Secret in the same namespace as the referent. If the
53-
referent is a ClusterIssuer, the reference instead refers to the resource
54-
with the given name in the configured 'cluster resource namespace', which
55-
is set as a flag on the controller component (and defaults to the
56-
namespace that the controller runs in).
5751
properties:
5852
universalAuth:
5953
properties:
@@ -182,12 +176,6 @@ spec:
182176
description: IssuerSpec defines the desired state of Issuer
183177
properties:
184178
authentication:
185-
description: |-
186-
A reference to a Secret in the same namespace as the referent. If the
187-
referent is a ClusterIssuer, the reference instead refers to the resource
188-
with the given name in the configured 'cluster resource namespace', which
189-
is set as a flag on the controller component (and defaults to the
190-
namespace that the controller runs in).
191179
properties:
192180
universalAuth:
193181
properties:
@@ -600,7 +588,7 @@ spec:
600588
- --health-probe-bind-address=:8081
601589
command:
602590
- /manager
603-
image: docker.io/infisical/pki-issuer:v0.1.1-2-gb384f71
591+
image: docker.io/infisical/pki-issuer:v0.1.1-3-gc2030ef
604592
livenessProbe:
605593
httpGet:
606594
path: /healthz

build/kustomize/kustomization.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ resources:
55
images:
66
- name: controller
77
newName: docker.io/infisical/pki-issuer
8-
newTag: v0.1.1-2-gb384f71
8+
newTag: v0.1.1-3-gc2030ef

internal/controller/certificaterequest_controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,13 @@ func (r *CertificateRequestReconciler) Reconcile(ctx context.Context, req ctrl.R
246246
return ctrl.Result{}, fmt.Errorf("%w: %v", errSignerBuilder, err)
247247
}
248248

249-
signed, err := signer.Sign(certificateRequest)
249+
pem, ca, err := signer.Sign(certificateRequest)
250250
if err != nil {
251251
return ctrl.Result{}, fmt.Errorf("%w: %v", errSignerSign, err)
252252
}
253253

254-
certificateRequest.Status.Certificate = signed
254+
certificateRequest.Status.Certificate = pem
255+
certificateRequest.Status.CA = ca
255256

256257
report(cmapi.CertificateRequestReasonIssued, "Signed", nil)
257258
return ctrl.Result{}, nil

internal/issuer/signer/signer.go

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package signer
22

33
import (
4+
"bytes"
45
"encoding/pem"
56
"fmt"
67
"time"
@@ -17,7 +18,7 @@ type HealthChecker interface {
1718
type HealthCheckerBuilder func(*v1alpha1.IssuerSpec, map[string][]byte) (HealthChecker, error)
1819

1920
type Signer interface {
20-
Sign(certmanager.CertificateRequest) ([]byte, error)
21+
Sign(certmanager.CertificateRequest) ([]byte, []byte, error)
2122
}
2223

2324
type SignerBuilder func(*v1alpha1.IssuerSpec, map[string][]byte) (Signer, error)
@@ -82,7 +83,6 @@ type AuthResponse struct {
8283
TokenType string `json:"tokenType"`
8384
}
8485

85-
// NOTE (dangtony98): Add support for certificate template in the future
8686
type SignCertificateRequest struct {
8787
CaId string `json:"caId,omitempty"`
8888
CertificateTemplateId string `json:"certificateTemplateId,omitempty"`
@@ -97,11 +97,11 @@ type SignCertificateResponse struct {
9797
SerialNumber string `json:"serialNumber"`
9898
}
9999

100-
func (o *signer) Sign(cr certmanager.CertificateRequest) ([]byte, error) {
100+
func (o *signer) Sign(cr certmanager.CertificateRequest) ([]byte, []byte, error) {
101101

102102
// Ensure either caId or certificateTemplateId is provided
103103
if o.caId == "" && o.certificateTemplateId == "" {
104-
return nil, fmt.Errorf("Either caId or certificateTemplateId must be provided")
104+
return nil, nil, fmt.Errorf("Either caId or certificateTemplateId must be provided")
105105
}
106106

107107
csrBytes := cr.Spec.Request
@@ -127,7 +127,7 @@ func (o *signer) Sign(cr certmanager.CertificateRequest) ([]byte, error) {
127127

128128
// Check for errors
129129
if err != nil {
130-
return nil, err
130+
return nil, nil, err
131131
}
132132

133133
// Define the request body based on your CSR
@@ -155,16 +155,35 @@ func (o *signer) Sign(cr certmanager.CertificateRequest) ([]byte, error) {
155155
SetResult(&signCertificateResponse).
156156
Post(o.siteUrl + "/api/v1/pki/certificates/sign-certificate")
157157

158-
certificate := signCertificateResponse.Certificate
158+
certificate := signCertificateResponse.Certificate // Leaf certificate
159+
chainPem := signCertificateResponse.CertificateChain // Full chain (intermediate certs + root cert)
159160

160-
block, _ := pem.Decode([]byte(certificate))
161-
pem.EncodeToMemory(&pem.Block{
162-
Type: "CERTIFICATE",
163-
Bytes: block.Bytes,
164-
})
161+
caChainCerts, rootCACert, err := splitRootCACertificate([]byte(chainPem))
162+
certPem := []byte(certificate + "\n")
163+
certPem = append(certPem, caChainCerts...)
165164

166-
return pem.EncodeToMemory(&pem.Block{
167-
Type: "CERTIFICATE",
168-
Bytes: block.Bytes,
169-
}), nil
165+
return certPem, rootCACert, nil
166+
}
167+
168+
func splitRootCACertificate(caCertChainPem []byte) ([]byte, []byte, error) {
169+
var caChainCerts []byte
170+
var rootCACert []byte
171+
for {
172+
block, rest := pem.Decode(caCertChainPem)
173+
if block == nil || block.Type != "CERTIFICATE" {
174+
return nil, nil, fmt.Errorf("failed to read certificate")
175+
}
176+
var encBuf bytes.Buffer
177+
if err := pem.Encode(&encBuf, block); err != nil {
178+
return nil, nil, err
179+
}
180+
if len(rest) > 0 {
181+
caChainCerts = append(caChainCerts, encBuf.Bytes()...)
182+
caCertChainPem = rest
183+
} else {
184+
rootCACert = append(rootCACert, encBuf.Bytes()...)
185+
break
186+
}
187+
}
188+
return caChainCerts, rootCACert, nil
170189
}

0 commit comments

Comments
 (0)