Skip to content

Commit 4bc5b4b

Browse files
Merge pull request #286 from adityajoshi12/channel-pki
2 parents 256a134 + 0ac5416 commit 4bc5b4b

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

controllers/certs_vault/provision_certs.go

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
hlfv1alpha1 "github.com/kfsoftware/hlf-operator/pkg/apis/hlf.kungfusoftware.es/v1alpha1"
2121
"github.com/pkg/errors"
2222
"github.com/sirupsen/logrus"
23+
log "github.com/sirupsen/logrus"
2324
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2425
"k8s.io/client-go/kubernetes"
2526
"k8s.io/client-go/rest"
@@ -376,27 +377,48 @@ func ReenrollUser(clientSet *kubernetes.Clientset, spec *hlfv1alpha1.VaultSpecCo
376377
return nil, nil, err
377378
}
378379

379-
// Create CSR data
380-
csrData := map[string]interface{}{
381-
"common_name": params.EnrollID,
382-
"key_type": "ec",
380+
commonName := params.EnrollID
381+
382+
template := &x509.CertificateRequest{
383+
Subject: pkix.Name{
384+
CommonName: commonName,
385+
},
383386
}
384-
if len(params.Hosts) > 0 {
385-
csrData["alt_names"] = strings.Join(params.Hosts, ",")
387+
388+
csrDER, err := x509.CreateCertificateRequest(rand.Reader, template, ecdsaKey)
389+
if err != nil {
390+
return nil, nil, errors.Wrap(err, "failed to create CSR")
391+
}
392+
393+
csrPEM := pem.EncodeToMemory(&pem.Block{
394+
Type: "CERTIFICATE REQUEST",
395+
Bytes: csrDER,
396+
})
397+
398+
csrData := map[string]interface{}{
399+
"csr": string(csrPEM),
400+
"common_name": commonName,
401+
"use_csr_common_name": true,
402+
"use_csr_sans": true,
403+
"key_type": "ec",
386404
}
387405
if params.CN != "" {
388406
csrData["common_name"] = params.CN
389407
}
390408

391-
// Add TTL if specified in the request
409+
if len(params.Hosts) > 0 {
410+
csrData["alt_names"] = strings.Join(params.Hosts, ",")
411+
}
412+
392413
if request.TTL != "" {
393414
csrData["ttl"] = request.TTL
394415
}
395416

396-
// Request certificate from Vault PKI using existing key
417+
log.Infof("reenrolling certs for %s", commonName)
418+
397419
secret, err := vaultClient.Write(
398420
context.Background(),
399-
fmt.Sprintf("%s/issue/%s", "pki", "fabric"), // TODO: Make these configurable
421+
fmt.Sprintf("%s/sign/%s", request.PKI, request.Role),
400422
csrData,
401423
)
402424
if err != nil {
@@ -478,8 +500,6 @@ func EnrollUser(clientSet *kubernetes.Clientset, vaultConf *hlfv1alpha1.VaultSpe
478500
"use_csr_common_name": true,
479501
"use_csr_sans": true,
480502
}
481-
482-
// Add TTL if specified in the request
483503
if request.TTL != "" {
484504
csrData["ttl"] = request.TTL
485505
}

0 commit comments

Comments
 (0)