|
| 1 | +// Copyright 2025 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package secrets_test |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + |
| 21 | + "github.com/prometheus/client_golang/prometheus" |
| 22 | + "go.yaml.in/yaml/v2" |
| 23 | + |
| 24 | + "github.com/prometheus/common/secrets" |
| 25 | +) |
| 26 | + |
| 27 | +func Example() { |
| 28 | + // A Prometheus registry is needed to register the secret manager's metrics. |
| 29 | + promRegisterer := prometheus.NewRegistry() |
| 30 | + |
| 31 | + // Create a temporary file to simulate a file-based secret (e.g., Kubernetes mount). |
| 32 | + passwordFile, err := os.CreateTemp("", "password_secret") |
| 33 | + if err != nil { |
| 34 | + panic(err) |
| 35 | + } |
| 36 | + defer os.Remove(passwordFile.Name()) |
| 37 | + |
| 38 | + if _, err := passwordFile.WriteString("my_super_secret_password"); err != nil { |
| 39 | + passwordFile.Close() |
| 40 | + panic(err) |
| 41 | + } |
| 42 | + passwordFile.Close() |
| 43 | + |
| 44 | + // In your configuration struct, use the `secrets.Field` type for any fields |
| 45 | + // that should contain secrets. |
| 46 | + type MyConfig struct { |
| 47 | + APIKey secrets.Field `yaml:"api_key"` |
| 48 | + Password secrets.Field `yaml:"password"` |
| 49 | + } |
| 50 | + |
| 51 | + // Users can then provide secrets in their YAML configuration file. |
| 52 | + // We inject the temporary file path created above. |
| 53 | + configData := []byte(fmt.Sprintf(` |
| 54 | +api_key: "my_super_secret_api_key" |
| 55 | +password: |
| 56 | + file: |
| 57 | + path: %s |
| 58 | +`, passwordFile.Name())) |
| 59 | + |
| 60 | + var cfg MyConfig |
| 61 | + if err := yaml.Unmarshal(configData, &cfg); err != nil { |
| 62 | + panic(fmt.Errorf("error unmarshaling config: %w", err)) |
| 63 | + } |
| 64 | + |
| 65 | + // Create a secret manager. This discovers and manages all Fields in cfg. |
| 66 | + // The manager will handle refreshing secrets in the background. |
| 67 | + manager, err := secrets.NewManager(context.Background(), promRegisterer, secrets.Providers, &cfg) |
| 68 | + if err != nil { |
| 69 | + panic(fmt.Errorf("error creating secret manager: %w", err)) |
| 70 | + } |
| 71 | + |
| 72 | + // Start the manager's background refresh loop. |
| 73 | + manager.Start(context.Background()) |
| 74 | + defer manager.Stop() |
| 75 | + |
| 76 | + // Access the secret values. |
| 77 | + apiKey := cfg.APIKey.Value() |
| 78 | + password := cfg.Password.Value() |
| 79 | + |
| 80 | + fmt.Printf("API Key: %s\n", apiKey) |
| 81 | + fmt.Printf("Password: %s\n", password) |
| 82 | + |
| 83 | + // Output: |
| 84 | + // API Key: my_super_secret_api_key |
| 85 | + // Password: my_super_secret_password |
| 86 | +} |
0 commit comments