Skip to content

Commit 88771e8

Browse files
authored
Merge pull request #5 from Infisical/feat/updated-issuer
feat: completed pki v2 template name change
2 parents af1cecd + 7be6705 commit 88771e8

6 files changed

Lines changed: 53 additions & 49 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ build: manifests generate fmt vet ## Build manager binary
107107

108108
.PHONY: run
109109
run: manifests generate fmt vet ## Run a controller from your host.
110-
go run ./main.go
110+
go run cmd/main.go --cluster-resource-namespace default
111111

112112
# If you wish built the manager image targeting other platforms you can use the --platform flag.
113113
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.

api/v1alpha1/issuer_types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ type IssuerSpec struct {
5858

5959
// ID of the CA in Infisical to use for signing certificates.
6060

61-
// +kubebuilder:validation:Optional
62-
CaId string `json:"caId"`
61+
// +kubebuilder:validation:Required
62+
ProjectID string `json:"projectId"`
6363

6464
// ID of Certificate Template in Infisical to use for signing certificates.
6565

66-
// +kubebuilder:validation:Optional
67-
CertificateTemplateId string `json:"certificateTemplateId"`
66+
// +kubebuilder:validation:Required
67+
CertificateTemplateName string `json:"certificateTemplateName"`
6868

6969
// A reference to a Secret in the same namespace as the referent. If the
7070
// referent is a ClusterIssuer, the reference instead refers to the resource

config/crd/bases/infisical-issuer.infisical.com_clusterissuers.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ spec:
6767
required:
6868
- universalAuth
6969
type: object
70-
caId:
70+
certificateTemplateName:
7171
type: string
72-
certificateTemplateId:
72+
projectId:
7373
type: string
7474
url:
7575
description: |-
@@ -78,6 +78,8 @@ spec:
7878
type: string
7979
required:
8080
- authentication
81+
- certificateTemplateName
82+
- projectId
8183
- url
8284
type: object
8385
status:

config/crd/bases/infisical-issuer.infisical.com_issuers.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ spec:
6767
required:
6868
- universalAuth
6969
type: object
70-
caId:
70+
certificateTemplateName:
7171
type: string
72-
certificateTemplateId:
72+
projectId:
7373
type: string
7474
url:
7575
description: |-
@@ -78,6 +78,8 @@ spec:
7878
type: string
7979
required:
8080
- authentication
81+
- certificateTemplateName
82+
- projectId
8183
- url
8284
type: object
8385
status:

internal/controller/certificaterequest_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ type fakeSigner struct {
4141
errSign error
4242
}
4343

44-
func (o *fakeSigner) Sign(certmanager.CertificateRequest) ([]byte, error) {
45-
return []byte("fake signed certificate"), o.errSign
44+
func (o *fakeSigner) Sign(certmanager.CertificateRequest) ([]byte, []byte, error) {
45+
return []byte("fake signed certificate"), []byte("fake signed certificate"), o.errSign
4646
}
4747

4848
func TestCertificateRequestReconcile(t *testing.T) {

internal/issuer/signer/signer.go

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,30 @@ type SignerBuilder func(*v1alpha1.IssuerSpec, map[string][]byte) (Signer, error)
2525

2626
func HealthCheckerFromIssuerAndSecretData(spec *v1alpha1.IssuerSpec, secretData map[string][]byte) (HealthChecker, error) {
2727
return &signer{
28-
siteUrl: spec.URL,
29-
caId: spec.CaId,
30-
clientId: spec.Authentication.UniversalAuth.ClientId,
31-
certificateTemplateId: spec.CertificateTemplateId,
32-
clientSecret: string(secretData["clientSecret"]),
28+
siteUrl: spec.URL,
29+
projectId: spec.ProjectID,
30+
certificateTemplateName: spec.CertificateTemplateName,
31+
clientId: spec.Authentication.UniversalAuth.ClientId,
32+
clientSecret: string(secretData["clientSecret"]),
3333
}, nil
3434
}
3535

3636
func SignerFromIssuerAndSecretData(spec *v1alpha1.IssuerSpec, secretData map[string][]byte) (Signer, error) {
3737
return &signer{
38-
siteUrl: spec.URL,
39-
caId: spec.CaId,
40-
certificateTemplateId: spec.CertificateTemplateId,
41-
clientId: spec.Authentication.UniversalAuth.ClientId,
42-
clientSecret: string(secretData["clientSecret"]),
38+
siteUrl: spec.URL,
39+
projectId: spec.ProjectID,
40+
certificateTemplateName: spec.CertificateTemplateName,
41+
clientId: spec.Authentication.UniversalAuth.ClientId,
42+
clientSecret: string(secretData["clientSecret"]),
4343
}, nil
4444
}
4545

4646
type signer struct {
47-
siteUrl string
48-
caId string
49-
certificateTemplateId string
50-
clientId string
51-
clientSecret string
47+
siteUrl string
48+
projectId string
49+
certificateTemplateName string
50+
clientId string
51+
clientSecret string
5252
}
5353

5454
func (o *signer) Check() error {
@@ -84,10 +84,10 @@ type AuthResponse struct {
8484
}
8585

8686
type SignCertificateRequest struct {
87-
CaId string `json:"caId,omitempty"`
88-
CertificateTemplateId string `json:"certificateTemplateId,omitempty"`
89-
Csr string `json:"csr"`
90-
Ttl string `json:"ttl,omitempty"`
87+
ProjectId string `json:"projectId,omitempty"`
88+
CertificateTemplateName string `json:"certificateTemplateName,omitempty"`
89+
Csr string `json:"csr"`
90+
Ttl string `json:"ttl,omitempty"`
9191
}
9292

9393
type SignCertificateResponse struct {
@@ -98,12 +98,6 @@ type SignCertificateResponse struct {
9898
}
9999

100100
func (o *signer) Sign(cr certmanager.CertificateRequest) ([]byte, []byte, error) {
101-
102-
// Ensure either caId or certificateTemplateId is provided
103-
if o.caId == "" && o.certificateTemplateId == "" {
104-
return nil, nil, fmt.Errorf("Either caId or certificateTemplateId must be provided")
105-
}
106-
107101
csrBytes := cr.Spec.Request
108102
// csr, err := parseCSR(csrBytes)
109103
// if err != nil {
@@ -116,7 +110,7 @@ func (o *signer) Sign(cr certmanager.CertificateRequest) ([]byte, []byte, error)
116110
signCertificateResponse := SignCertificateResponse{}
117111

118112
// Login operation against Infisical
119-
_, err := client.R().
113+
res, err := client.R().
120114
SetHeader("Content-Type", "application/x-www-form-urlencoded").
121115
SetFormData(map[string]string{
122116
"clientId": o.clientId,
@@ -129,33 +123,39 @@ func (o *signer) Sign(cr certmanager.CertificateRequest) ([]byte, []byte, error)
129123
if err != nil {
130124
return nil, nil, err
131125
}
126+
if res.IsError() {
127+
return nil, nil, fmt.Errorf("%s", res.String())
128+
}
132129

133130
// Define the request body based on your CSR
134131
requestBody := SignCertificateRequest{
135-
Csr: string(csrBytes), // Required
136-
Ttl: "90d", // Default ttl
137-
}
138-
139-
if o.caId != "" {
140-
requestBody.CaId = o.caId
141-
}
142-
if o.certificateTemplateId != "" {
143-
requestBody.CertificateTemplateId = o.certificateTemplateId
132+
Csr: string(csrBytes), // Required
133+
Ttl: "90d", // Default ttl,
134+
ProjectId: o.projectId,
135+
CertificateTemplateName: o.certificateTemplateName,
144136
}
145137

146138
if cr.Spec.Duration != nil {
147139
requestBody.Ttl = fmt.Sprintf("%ds", int(cr.Spec.Duration.Duration.Seconds()))
148140
}
149141

150142
// Make the POST request with Bearer token authentication and JSON body
151-
_, err = client.R().
143+
res, err = client.R().
152144
SetHeader("Content-Type", "application/json").
153145
SetHeader("Authorization", "Bearer "+authResponse.AccessToken).
154146
SetBody(requestBody).
155147
SetResult(&signCertificateResponse).
156-
Post(o.siteUrl + "/api/v1/pki/certificates/sign-certificate")
148+
Post(o.siteUrl + "/api/v2/pki/certificate-templates/" + o.certificateTemplateName + "/sign-certificate")
149+
150+
// Check for errors
151+
if err != nil {
152+
return nil, nil, err
153+
}
154+
if res.IsError() {
155+
return nil, nil, fmt.Errorf("%s", res.String())
156+
}
157157

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

161161
caChainCerts, rootCACert, err := splitRootCACertificate([]byte(chainPem))

0 commit comments

Comments
 (0)