|
| 1 | +package authutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/sha256" |
| 5 | + "encoding/hex" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | +) |
| 10 | + |
| 11 | +// machineAccountKeyDir returns the directory used to store machine account PEM key files. |
| 12 | +// It does not create the directory. |
| 13 | +func machineAccountKeyDir() (string, error) { |
| 14 | + configDir, err := os.UserConfigDir() |
| 15 | + if err != nil { |
| 16 | + return "", fmt.Errorf("failed to determine user config directory: %w", err) |
| 17 | + } |
| 18 | + return filepath.Join(configDir, "datumctl", "machine-accounts"), nil |
| 19 | +} |
| 20 | + |
| 21 | +// MachineAccountKeyFilePath returns the on-disk path where the PEM key for |
| 22 | +// the given userKey is stored. It does not create the directory. |
| 23 | +func MachineAccountKeyFilePath(userKey string) (string, error) { |
| 24 | + dir, err := machineAccountKeyDir() |
| 25 | + if err != nil { |
| 26 | + return "", err |
| 27 | + } |
| 28 | + sum := sha256.Sum256([]byte(userKey)) |
| 29 | + filename := hex.EncodeToString(sum[:]) + ".pem" |
| 30 | + return filepath.Join(dir, filename), nil |
| 31 | +} |
| 32 | + |
| 33 | +// WriteMachineAccountKeyFile atomically writes the PEM key to disk for the |
| 34 | +// given userKey and returns the absolute path. Creates the parent directory |
| 35 | +// with mode 0700 if needed. The file is written with mode 0600. |
| 36 | +func WriteMachineAccountKeyFile(userKey, pemKey string) (string, error) { |
| 37 | + dir, err := machineAccountKeyDir() |
| 38 | + if err != nil { |
| 39 | + return "", err |
| 40 | + } |
| 41 | + |
| 42 | + if err := os.MkdirAll(dir, 0700); err != nil { |
| 43 | + return "", fmt.Errorf("failed to create machine account key directory %s: %w", dir, err) |
| 44 | + } |
| 45 | + |
| 46 | + destPath, err := MachineAccountKeyFilePath(userKey) |
| 47 | + if err != nil { |
| 48 | + return "", err |
| 49 | + } |
| 50 | + |
| 51 | + tmpPath := destPath + ".tmp" |
| 52 | + if err := os.WriteFile(tmpPath, []byte(pemKey), 0600); err != nil { |
| 53 | + return "", fmt.Errorf("failed to write machine account key to %s: %w", tmpPath, err) |
| 54 | + } |
| 55 | + |
| 56 | + if err := os.Rename(tmpPath, destPath); err != nil { |
| 57 | + // Best-effort cleanup of the temp file on rename failure. |
| 58 | + _ = os.Remove(tmpPath) |
| 59 | + return "", fmt.Errorf("failed to move machine account key to %s: %w", destPath, err) |
| 60 | + } |
| 61 | + |
| 62 | + return destPath, nil |
| 63 | +} |
| 64 | + |
| 65 | +// ReadMachineAccountKeyFile reads a PEM key from the given path. |
| 66 | +func ReadMachineAccountKeyFile(path string) (string, error) { |
| 67 | + data, err := os.ReadFile(path) |
| 68 | + if err != nil { |
| 69 | + return "", fmt.Errorf("failed to read machine account key file %s: %w", path, err) |
| 70 | + } |
| 71 | + return string(data), nil |
| 72 | +} |
| 73 | + |
| 74 | +// RemoveMachineAccountKeyFile deletes the PEM key file for the given userKey. |
| 75 | +// Returns nil if the file does not exist. |
| 76 | +func RemoveMachineAccountKeyFile(userKey string) error { |
| 77 | + path, err := MachineAccountKeyFilePath(userKey) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + if err := os.Remove(path); err != nil && !os.IsNotExist(err) { |
| 82 | + return fmt.Errorf("failed to remove machine account key file %s: %w", path, err) |
| 83 | + } |
| 84 | + return nil |
| 85 | +} |
0 commit comments