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

Commit

Permalink
fix permission of CA bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
bonifaido committed May 13, 2024
1 parent 179ed8e commit d185fd9
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion internal/cli/cmd/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ func (c *agentCommand) runCommander(ctx context.Context) error {

func (c *agentCommand) ensureCACertificate() (string, error) {
path := c.cli.Configuration().Agent.CAPemPath

if _, err := os.Stat(path); path != "" && err == nil {
err = changePermissionsToUserReadonly(path)
if err != nil {
return "", errors.WrapIf(err, "could not set permissions for existing self signed root CA certificate")
}
return path, nil
}

Expand All @@ -148,6 +151,10 @@ func (c *agentCommand) ensureCACertificate() (string, error) {
if file, err := os.Create(path); err != nil {
return "", errors.WrapIf(err, "could not write generated self signed root CA certificate")
} else {
err = changePermissionsToUserReadonly(path)
if err != nil {
return "", errors.WrapIf(err, "could not set permissions for new self signed root CA certificate")
}
defer file.Close()
if _, err := file.Write(append(cert.GetPEM(), pkey.GetPEM()...)); err != nil {
return "", errors.WrapIf(err, "could not write generated self signed root CA certificate")
Expand All @@ -158,6 +165,13 @@ func (c *agentCommand) ensureCACertificate() (string, error) {
return path, nil
}

func changePermissionsToUserReadonly(path string) error {
if err := os.Chmod(path, 0600); err != nil {
return errors.WrapIf(err, "could not change file to user readonly")
}
return nil
}

func (c *agentCommand) run(cmd *cobra.Command) error {
logger := c.cli.Logger()
eventBus := c.cli.EventBus()
Expand Down

0 comments on commit d185fd9

Please sign in to comment.