Skip to content
This repository was archived by the owner on Sep 19, 2024. It is now read-only.

Commit 199f682

Browse files
committed
init and save self signed CA pem
if not provided
1 parent ac9ef5f commit 199f682

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
agent:
2+
caPEMPath: /tmp/ca.pem
23
trustDomain: acme.corp
34
defaultCertTTL: 2h
45
metadataCollectors:

internal/cli/cmd/agent/agent.go

+41-13
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"context"
2424
"crypto/x509/pkix"
2525
"encoding/json"
26+
"os"
2627

2728
"emperror.dev/errors"
2829
"github.com/spf13/cobra"
@@ -37,6 +38,10 @@ import (
3738
"github.com/cisco-open/camblet/pkg/tls"
3839
)
3940

41+
const (
42+
defaultRootCACommonName = "Camblet root CA"
43+
)
44+
4045
type agentCommand struct {
4146
cli cli.CLI
4247
}
@@ -62,7 +67,7 @@ func NewCommand(c cli.CLI) *cobra.Command {
6267
cmd.Flags().StringSlice("services-path", config.DefaultServicesPaths, "Path to file or directory for service definitions")
6368
cmd.Flags().String("trust-domain", config.DefaultTrustDomain, "Trust domain")
6469
cmd.Flags().Duration("default-cert-ttl", config.DefaultCertTTLDuration, "Default certificate TTL")
65-
cmd.Flags().String("ca-pem-path", "", "Path for CA pem")
70+
cmd.Flags().String("ca-pem-path", config.DefaultCAPEMPath, "Path for CA pem")
6671

6772
cli.BindCMDFlags(c.Viper(), cmd)
6873

@@ -82,20 +87,12 @@ func (c *agentCommand) runCommander(ctx context.Context) error {
8287
h.AddHandler("connect", commands.Connect())
8388

8489
caOpts := []tls.CertificateAuthorityOption{}
85-
if c.cli.Configuration().Agent.CAPemPath == "" {
86-
if cert, pkey, err := tls.CreateSelfSignedCACertificate(tls.CertificateOptions{
87-
Subject: pkix.Name{
88-
CommonName: "Camblet root CA",
89-
},
90-
}); err != nil {
91-
return errors.WrapIf(err, "could not create self signed root CA certificate")
92-
} else {
93-
caOpts = append(caOpts, tls.CertificateAuthorityWithPEM(append(cert.GetPEM(), pkey.GetPEM()...)))
94-
}
95-
} else {
96-
caOpts = append(caOpts, tls.CertificateAuthorityWithPEMFile(c.cli.Configuration().Agent.CAPemPath))
90+
caPEMPath, err := c.ensureCACertificate()
91+
if err != nil {
92+
return errors.WithStackIf(err)
9793
}
9894

95+
caOpts = append(caOpts, tls.CertificateAuthorityWithPEMFile(caPEMPath))
9996
ca, err := tls.NewCertificateAuthority(caOpts...)
10097
if err != nil {
10198
return err
@@ -105,6 +102,8 @@ func (c *agentCommand) runCommander(ctx context.Context) error {
105102
if err != nil {
106103
return err
107104
}
105+
c.cli.Logger().Info("CA signer initialized", "caPEMPath", caPEMPath)
106+
108107
h.AddHandler("csr_sign", csrSign)
109108

110109
collector := collectors.GetMetadataCollector(c.cli.Configuration().Agent.MetadataCollectors, c.cli.Logger())
@@ -117,6 +116,35 @@ func (c *agentCommand) runCommander(ctx context.Context) error {
117116
return nil
118117
}
119118

119+
func (c *agentCommand) ensureCACertificate() (string, error) {
120+
path := c.cli.Configuration().Agent.CAPemPath
121+
122+
if _, err := os.Stat(path); path != "" && err == nil {
123+
return path, nil
124+
}
125+
126+
cert, pkey, err := tls.CreateSelfSignedCACertificate(tls.CertificateOptions{
127+
Subject: pkix.Name{
128+
CommonName: defaultRootCACommonName,
129+
},
130+
})
131+
if err != nil {
132+
return "", errors.WrapIf(err, "could not generate self signed root CA certificate")
133+
}
134+
135+
if file, err := os.Create(path); err != nil {
136+
return "", errors.WrapIf(err, "could not write generated self signed root CA certificate")
137+
} else {
138+
defer file.Close()
139+
if _, err := file.Write(append(cert.GetPEM(), pkey.GetPEM()...)); err != nil {
140+
return "", errors.WrapIf(err, "could not write generated self signed root CA certificate")
141+
}
142+
c.cli.Logger().Info("self signed root CA certificate is created and saved", "path", path)
143+
}
144+
145+
return path, nil
146+
}
147+
120148
func (c *agentCommand) run(cmd *cobra.Command) error {
121149
logger := c.cli.Logger()
122150
eventBus := c.cli.EventBus()

pkg/agent/commands/augment.go

-3
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@ func (c *augmentCommand) HandleCommand(cmd messenger.Command) (string, error) {
9999

100100
js := string(j)
101101
c.cache.Set(cmd.Context.UniqueString(), js, ttlcache.DefaultTTL)
102-
if cmd.Context.PID == 25085 {
103-
fmt.Printf("%d %s\n", cmd.Context.PID, js)
104-
}
105102
logger.V(2).Info("augmentation response cached")
106103

107104
return js, nil

pkg/config/agent.go

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
const (
3131
DefaultTrustDomain = "camblet"
3232
DefaultCertTTLDuration = time.Hour * 24
33+
DefaultCAPEMPath = "/etc/camblet/ca.pem"
3334
)
3435

3536
var (

0 commit comments

Comments
 (0)