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

set appropriate filemodes on unwrapped identity files #2706

Merged
merged 3 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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: 27 additions & 0 deletions ziti/cmd/file_mode_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build !windows
qrkourier marked this conversation as resolved.
Show resolved Hide resolved

package cmd

import (
"os"
"syscall"
)

func getFileMode(isPrivateKey bool) os.FileMode {
// Default modes before umask:
// - Private keys: 0600 (rw-------)
// - Public files: 0644 (rw-r--r--)
mode := os.FileMode(0644)
if isPrivateKey {
mode = os.FileMode(0600)
}

// Get current umask
oldMask := syscall.Umask(0)
syscall.Umask(oldMask) // Restore original umask

// Apply umask to our default mode
mode &= ^os.FileMode(oldMask)

return mode
}
15 changes: 15 additions & 0 deletions ziti/cmd/file_mode_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build windows
qrkourier marked this conversation as resolved.
Show resolved Hide resolved

package cmd

import "os"

func getFileMode(isPrivateKey bool) os.FileMode {
// Default modes for Windows:
// - Private keys: 0600 (rw-------)
// - Public files: 0644 (rw-r--r--)
if isPrivateKey {
return os.FileMode(0600)
}
return os.FileMode(0644)
}
10 changes: 5 additions & 5 deletions ziti/cmd/unwrap_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewUnwrapIdentityFileCommand(out io.Writer, errOut io.Writer) *cobra.Comman

if strings.HasPrefix(config.ID.Cert, "pem:") {
data := strings.TrimPrefix(config.ID.Cert, "pem:")
if err := os.WriteFile(outCertFile, []byte(data), 0); err != nil {
if err := os.WriteFile(outCertFile, []byte(data), getFileMode(false)); err != nil {
_, _ = fmt.Fprintf(errOut, "error writing certificate to file [%s]: %v\n", outCertFile, err)
return
}
Expand All @@ -67,22 +67,22 @@ func NewUnwrapIdentityFileCommand(out io.Writer, errOut io.Writer) *cobra.Comman

if strings.HasPrefix(config.ID.Key, "pem:") {
data := strings.TrimPrefix(config.ID.Key, "pem:")
if err := os.WriteFile(outKeyFile, []byte(data), 0); err != nil {
if err := os.WriteFile(outKeyFile, []byte(data), getFileMode(true)); err != nil {
_, _ = fmt.Fprintf(errOut, "error writing private key to file [%s]: %v\n", outKeyFile, err)
return
}
} else {
_, _ = fmt.Fprintf(errOut, "error writing private key to file [%s]: missing pem prefix, type is unsupported\n", outKeyFile)
}

if strings.HasPrefix(config.ID.Key, "pem:") {
if strings.HasPrefix(config.ID.CA, "pem:") {
data := strings.TrimPrefix(config.ID.CA, "pem:")
if err := os.WriteFile(outCaFile, []byte(data), 0); err != nil {
if err := os.WriteFile(outCaFile, []byte(data), getFileMode(false)); err != nil {
_, _ = fmt.Fprintf(errOut, "error writing CAs to file [%s]: %v\n", outCaFile, err)
return
}
} else {
_, _ = fmt.Fprintf(errOut, "error writing CAs to file [%s]: missing pem prefix, type is unsupported\n", outKeyFile)
_, _ = fmt.Fprintf(errOut, "error writing CAs to file [%s]: missing pem prefix, type is unsupported\n", outCaFile)
}
},
}
Expand Down
Loading