GetAPIKeyFromHelper executes a shell command to dynamically generate an API key. Platform-specific implementations are in api_key_helper_unix.go and api_key_helper_windows.go. The command is executed with a timeout controlled by the provided context. It returns the trimmed output from stdout, or an
( ctx context.Context, helperCmd string, refreshInterval time.Duration, )
| 112 | // |
| 113 | // Security note: The returned API key is sensitive and should not be logged. |
| 114 | func GetAPIKeyFromHelperWithCache( |
| 115 | ctx context.Context, |
| 116 | helperCmd string, |
| 117 | refreshInterval time.Duration, |
| 118 | ) (string, error) { |
| 119 | if helperCmd == "" { |
| 120 | return "", errors.New("api_key_helper command is empty") |
| 121 | } |
| 122 | |
| 123 | // Try to read from cache |
| 124 | cache, err := readCache(helperCmd) |
| 125 | if err != nil { |
| 126 | // If cache read fails, log but continue to fetch fresh key |
| 127 | // Don't fail the entire operation just because cache is broken |
| 128 | cache = nil |
| 129 | } |
| 130 | |
| 131 | // Check if we need to refresh |
| 132 | if !needsRefresh(cache, refreshInterval) { |
| 133 | return cache.APIKey, nil |
| 134 | } |
| 135 | |
| 136 | // Fetch new API key |
| 137 | apiKey, err := GetAPIKeyFromHelper(ctx, helperCmd) |
| 138 | if err != nil { |
| 139 | return "", err |
| 140 | } |
| 141 | |
| 142 | // Write to cache (ignore errors to not block the operation) |
| 143 | _ = writeCache(helperCmd, apiKey) |
| 144 | |
| 145 | return apiKey, nil |
| 146 | } |