|
| 1 | +package authutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/sha256" |
| 5 | + "encoding/hex" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | +) |
| 11 | + |
| 12 | +// machineAccountKeyDir returns the directory used to store machine account PEM key files. |
| 13 | +// It does not create the directory. |
| 14 | +func machineAccountKeyDir() (string, error) { |
| 15 | + configDir, err := os.UserConfigDir() |
| 16 | + if err != nil { |
| 17 | + return "", fmt.Errorf("failed to determine user config directory: %w", err) |
| 18 | + } |
| 19 | + return filepath.Join(configDir, "datumctl", "machine-accounts"), nil |
| 20 | +} |
| 21 | + |
| 22 | +// MachineAccountKeyFilePath returns the on-disk path where the PEM key for |
| 23 | +// the given userKey is stored. It does not create the directory. |
| 24 | +func MachineAccountKeyFilePath(userKey string) (string, error) { |
| 25 | + dir, err := machineAccountKeyDir() |
| 26 | + if err != nil { |
| 27 | + return "", err |
| 28 | + } |
| 29 | + sum := sha256.Sum256([]byte(userKey)) |
| 30 | + filename := hex.EncodeToString(sum[:]) + ".pem" |
| 31 | + return filepath.Join(dir, filename), nil |
| 32 | +} |
| 33 | + |
| 34 | +// WriteMachineAccountKeyFile atomically writes the PEM key to disk for the |
| 35 | +// given userKey and returns the absolute path. Creates the parent directory |
| 36 | +// with mode 0700 if needed. The file is written with mode 0600. |
| 37 | +func WriteMachineAccountKeyFile(userKey, pemKey string) (string, error) { |
| 38 | + dir, err := machineAccountKeyDir() |
| 39 | + if err != nil { |
| 40 | + return "", err |
| 41 | + } |
| 42 | + |
| 43 | + if err := os.MkdirAll(dir, 0700); err != nil { |
| 44 | + return "", fmt.Errorf("failed to create machine account key directory %s: %w", dir, err) |
| 45 | + } |
| 46 | + |
| 47 | + // Tighten perms on the directory even when it already existed with looser ones. |
| 48 | + if err := os.Chmod(dir, 0700); err != nil && !errors.Is(err, os.ErrNotExist) { |
| 49 | + return "", fmt.Errorf("failed to set permissions on machine account key directory %s: %w", dir, err) |
| 50 | + } |
| 51 | + |
| 52 | + destPath, err := MachineAccountKeyFilePath(userKey) |
| 53 | + if err != nil { |
| 54 | + return "", err |
| 55 | + } |
| 56 | + |
| 57 | + // Use a unique temp filename so concurrent logins for the same account |
| 58 | + // do not race on the same .tmp path and corrupt each other's writes. |
| 59 | + tmpFile, err := os.CreateTemp(dir, filepath.Base(destPath)+".tmp.*") |
| 60 | + if err != nil { |
| 61 | + return "", fmt.Errorf("failed to create temp file for machine account key in %s: %w", dir, err) |
| 62 | + } |
| 63 | + tmpName := tmpFile.Name() |
| 64 | + |
| 65 | + // Ensure the temp file is removed on any error path. After a successful |
| 66 | + // Rename the file no longer exists at tmpName, so Remove is a no-op. |
| 67 | + var writeErr error |
| 68 | + defer func() { |
| 69 | + if writeErr != nil { |
| 70 | + _ = os.Remove(tmpName) |
| 71 | + } |
| 72 | + }() |
| 73 | + |
| 74 | + // Be explicit about mode even though CreateTemp already uses 0600. |
| 75 | + if writeErr = tmpFile.Chmod(0600); writeErr != nil { |
| 76 | + _ = tmpFile.Close() |
| 77 | + writeErr = fmt.Errorf("failed to set permissions on temp key file %s: %w", tmpName, writeErr) |
| 78 | + return "", writeErr |
| 79 | + } |
| 80 | + |
| 81 | + if _, writeErr = tmpFile.Write([]byte(pemKey)); writeErr != nil { |
| 82 | + _ = tmpFile.Close() |
| 83 | + writeErr = fmt.Errorf("failed to write machine account key to %s: %w", tmpName, writeErr) |
| 84 | + return "", writeErr |
| 85 | + } |
| 86 | + |
| 87 | + if writeErr = tmpFile.Close(); writeErr != nil { |
| 88 | + writeErr = fmt.Errorf("failed to close temp key file %s: %w", tmpName, writeErr) |
| 89 | + return "", writeErr |
| 90 | + } |
| 91 | + |
| 92 | + if writeErr = os.Rename(tmpName, destPath); writeErr != nil { |
| 93 | + writeErr = fmt.Errorf("failed to move machine account key to %s: %w", destPath, writeErr) |
| 94 | + return "", writeErr |
| 95 | + } |
| 96 | + |
| 97 | + return destPath, nil |
| 98 | +} |
| 99 | + |
| 100 | +// ReadMachineAccountKeyFile reads a PEM key from the given path. |
| 101 | +func ReadMachineAccountKeyFile(path string) (string, error) { |
| 102 | + data, err := os.ReadFile(path) |
| 103 | + if err != nil { |
| 104 | + return "", fmt.Errorf("failed to read machine account key file %s: %w", path, err) |
| 105 | + } |
| 106 | + return string(data), nil |
| 107 | +} |
| 108 | + |
| 109 | +// RemoveMachineAccountKeyFile deletes the PEM key file for the given userKey. |
| 110 | +// Returns nil if the file does not exist. |
| 111 | +func RemoveMachineAccountKeyFile(userKey string) error { |
| 112 | + path, err := MachineAccountKeyFilePath(userKey) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + if err := os.Remove(path); err != nil && !os.IsNotExist(err) { |
| 117 | + return fmt.Errorf("failed to remove machine account key file %s: %w", path, err) |
| 118 | + } |
| 119 | + return nil |
| 120 | +} |
0 commit comments