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

cli: change key file permissions to 0600 #709

Merged
merged 1 commit into from
Jul 11, 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
13 changes: 7 additions & 6 deletions cli/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"log/slog"
"os"
"os/exec"
Expand Down Expand Up @@ -238,10 +239,10 @@ func filterNonCoCoRuntime(runtimeClassNamePrefix string, paths []string, logger
}

func generatePolicies(ctx context.Context, regoRulesPath, policySettingsPath, genpolicyCachePath string, yamlPaths []string, logger *slog.Logger) error {
if err := createFileWithDefault(policySettingsPath, func() ([]byte, error) { return defaultGenpolicySettings, nil }); err != nil {
if err := createFileWithDefault(policySettingsPath, 0o644, func() ([]byte, error) { return defaultGenpolicySettings, nil }); err != nil {
return fmt.Errorf("creating default policy file: %w", err)
}
if err := createFileWithDefault(regoRulesPath, func() ([]byte, error) { return defaultRules, nil }); err != nil {
if err := createFileWithDefault(regoRulesPath, 0o644, func() ([]byte, error) { return defaultRules, nil }); err != nil {
return fmt.Errorf("creating default policy.rego file: %w", err)
}
binaryInstallDir, err := installDir()
Expand Down Expand Up @@ -509,7 +510,7 @@ func generateWorkloadOwnerKey(flags *generateFlags) error {
}
keyPath := flags.workloadOwnerKeys[0]

if err := createFileWithDefault(keyPath, manifest.NewWorkloadOwnerKey); err != nil {
if err := createFileWithDefault(keyPath, 0o600, manifest.NewWorkloadOwnerKey); err != nil {
return fmt.Errorf("creating default workload owner key file: %w", err)
}
return nil
Expand All @@ -523,7 +524,7 @@ func generateSeedshareOwnerKey(flags *generateFlags) error {
}
keyPath := flags.seedshareOwnerKeys[0]

if err := createFileWithDefault(keyPath, manifest.NewSeedShareOwnerPrivateKey); err != nil {
if err := createFileWithDefault(keyPath, 0o600, manifest.NewSeedShareOwnerPrivateKey); err != nil {
return fmt.Errorf("creating default seedshare owner key file: %w", err)
}
return nil
Expand Down Expand Up @@ -646,8 +647,8 @@ func readFileOrDefault(path string, deflt []byte) ([]byte, error) {

// createFileWithDefault creates the file at path with the default value,
// if it doesn't exist.
func createFileWithDefault(path string, dflt func() ([]byte, error)) error {
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
func createFileWithDefault(path string, perm fs.FileMode, dflt func() ([]byte, error)) error {
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
if os.IsExist(err) {
return nil
}
Expand Down