|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "encoding/hex" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +// BootstrapTokenFileEnv names the file the bootstrap token is read from when |
| 13 | +// BootstrapTokenEnv is empty. |
| 14 | +const BootstrapTokenFileEnv = "MGT_BOOTSTRAP_TOKEN_FILE" |
| 15 | + |
| 16 | +// DefaultBootstrapTokenFile is next to the control-channel token on the |
| 17 | +// shared sock volume. |
| 18 | +const DefaultBootstrapTokenFile = "/app/sock/tfa_bootstrap_token" |
| 19 | + |
| 20 | +const bootstrapTokenBytes = 32 |
| 21 | +const bootstrapTokenFileMode os.FileMode = 0600 |
| 22 | + |
| 23 | +// bootstrapToken is the value GET /api/OTPUrl must present. It is never empty |
| 24 | +// after a successful EnsureBootstrapToken. |
| 25 | +var bootstrapToken string |
| 26 | + |
| 27 | +// BootstrapTokenFile returns the path of the persisted bootstrap token. |
| 28 | +func BootstrapTokenFile() string { |
| 29 | + if path := strings.TrimSpace(os.Getenv(BootstrapTokenFileEnv)); path != "" { |
| 30 | + return path |
| 31 | + } |
| 32 | + return DefaultBootstrapTokenFile |
| 33 | +} |
| 34 | + |
| 35 | +// EnsureBootstrapToken loads MGT_BOOTSTRAP_TOKEN or a persisted file, creating |
| 36 | +// the file once when neither is set. The console must send the value as |
| 37 | +// X-Bootstrap-Token; an empty token is never accepted. |
| 38 | +func EnsureBootstrapToken() (created bool, path string, err error) { |
| 39 | + if token := strings.TrimSpace(os.Getenv(BootstrapTokenEnv)); token != "" { |
| 40 | + bootstrapToken = token |
| 41 | + return false, "", nil |
| 42 | + } |
| 43 | + |
| 44 | + path = BootstrapTokenFile() |
| 45 | + token, created, err := loadOrCreateBootstrapToken(path) |
| 46 | + if err != nil { |
| 47 | + return false, path, err |
| 48 | + } |
| 49 | + if token == "" { |
| 50 | + return false, path, fmt.Errorf("bootstrap token is empty") |
| 51 | + } |
| 52 | + bootstrapToken = token |
| 53 | + return created, path, nil |
| 54 | +} |
| 55 | + |
| 56 | +func loadOrCreateBootstrapToken(path string) (string, bool, error) { |
| 57 | + if content, err := os.ReadFile(path); err == nil { |
| 58 | + if token := strings.TrimSpace(string(content)); token != "" { |
| 59 | + return token, false, nil |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + raw := make([]byte, bootstrapTokenBytes) |
| 64 | + if _, err := rand.Read(raw); err != nil { |
| 65 | + return "", false, err |
| 66 | + } |
| 67 | + value := hex.EncodeToString(raw) |
| 68 | + |
| 69 | + if err := os.MkdirAll(filepath.Dir(path), 0750); err != nil { |
| 70 | + return "", false, err |
| 71 | + } |
| 72 | + |
| 73 | + file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, bootstrapTokenFileMode) |
| 74 | + if err != nil { |
| 75 | + if !os.IsExist(err) { |
| 76 | + return "", false, err |
| 77 | + } |
| 78 | + content, readErr := os.ReadFile(path) |
| 79 | + if readErr != nil { |
| 80 | + return "", false, readErr |
| 81 | + } |
| 82 | + if token := strings.TrimSpace(string(content)); token != "" { |
| 83 | + return token, false, nil |
| 84 | + } |
| 85 | + // Empty leftover file: replace it. Do not truncate a non-empty secret. |
| 86 | + if err := os.WriteFile(path, []byte(value+"\n"), bootstrapTokenFileMode); err != nil { |
| 87 | + return "", false, err |
| 88 | + } |
| 89 | + return value, true, nil |
| 90 | + } |
| 91 | + if _, err = file.WriteString(value + "\n"); err != nil { |
| 92 | + _ = file.Close() |
| 93 | + return "", false, err |
| 94 | + } |
| 95 | + if err = file.Close(); err != nil { |
| 96 | + return "", false, err |
| 97 | + } |
| 98 | + return value, true, nil |
| 99 | +} |
0 commit comments