Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shared/cliconfig: Ensure client certificate key is 0600 #637

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions shared/cliconfig/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,24 @@ func (c *Config) GenerateClientCertificate() error {

// CopyGlobalCert will copy global (system-wide) certificates to the user config path.
func (c *Config) CopyGlobalCert(src string, dst string) error {
copyFile := func(oldPath string, newPath string) error {
copyFile := func(oldPath string, newPath string, mode os.FileMode) error {
sourceFile, err := os.Open(oldPath)
if err != nil {
return err
}

defer sourceFile.Close()

// Get the mode from the source file if not specified.
if mode == 0 {
fInfo, err := sourceFile.Stat()
if err != nil {
return err
}

mode = fInfo.Mode()
}

// Create new file.
newFile, err := os.Create(newPath)
if err != nil {
Expand All @@ -61,6 +71,13 @@ func (c *Config) CopyGlobalCert(src string, dst string) error {

defer newFile.Close()

// Apply the file mode.
err = newFile.Chmod(mode)
if err != nil {
return err
}

// Copy the content.
_, err = io.Copy(newFile, sourceFile)
if err != nil {
return err
Expand All @@ -74,7 +91,7 @@ func (c *Config) CopyGlobalCert(src string, dst string) error {
if util.PathExists(oldPath) {
newPath := c.ConfigPath("servercerts", fmt.Sprintf("%s.crt", dst))

err := copyFile(oldPath, newPath)
err := copyFile(oldPath, newPath, 0)
if err != nil {
return err
}
Expand All @@ -85,7 +102,7 @@ func (c *Config) CopyGlobalCert(src string, dst string) error {
if util.PathExists(oldPath) {
newPath := c.ConfigPath("clientcerts", fmt.Sprintf("%s.crt", dst))

err := copyFile(oldPath, newPath)
err := copyFile(oldPath, newPath, 0)
if err != nil {
return err
}
Expand All @@ -96,7 +113,7 @@ func (c *Config) CopyGlobalCert(src string, dst string) error {
if util.PathExists(oldPath) {
newPath := c.ConfigPath("clientcerts", fmt.Sprintf("%s.key", dst))

err := copyFile(oldPath, newPath)
err := copyFile(oldPath, newPath, 0600)
if err != nil {
return err
}
Expand All @@ -107,7 +124,7 @@ func (c *Config) CopyGlobalCert(src string, dst string) error {
if util.PathExists(oldPath) {
newPath := c.ConfigPath("clientcerts", fmt.Sprintf("%s.ca", dst))

err := copyFile(oldPath, newPath)
err := copyFile(oldPath, newPath, 0)
if err != nil {
return err
}
Expand Down
Loading