|
| 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 | +"log" |
| 20 | +"os" |
| 21 | + |
| 22 | +"github.com/prometheus/client_golang/prometheus" |
| 23 | +"github.com/prometheus/common/secrets" |
| 24 | +"go.yaml.in/yaml/v2" |
| 25 | +) |
| 26 | + |
| 27 | +funcExample() { |
| 28 | +// A Prometheus registry is needed to register the secret manager's metrics. |
| 29 | +promRegisterer:=prometheus.NewRegistry() |
| 30 | + |
| 31 | +// Create a temporary file for the password secret. |
| 32 | +passwordFile,err:=os.CreateTemp("","password") |
| 33 | +iferr!=nil { |
| 34 | +log.Fatal(err) |
| 35 | +} |
| 36 | +deferos.Remove(passwordFile.Name()) |
| 37 | + |
| 38 | +if_,err:=passwordFile.WriteString("my_super_secret_password");err!=nil { |
| 39 | +log.Fatal(err) |
| 40 | +} |
| 41 | +passwordFile.Close() |
| 42 | + |
| 43 | +// In your configuration struct, use the `secrets.Field` type for any fields |
| 44 | +// that should contain secrets. |
| 45 | +typeMyConfigstruct { |
| 46 | +APIKey secrets.Field`yaml:"api_key"` |
| 47 | +Password secrets.Field`yaml:"password"` |
| 48 | +} |
| 49 | + |
| 50 | +// Users can then provide secrets in their YAML configuration file. |
| 51 | +configData:= []byte(fmt.Sprintf(` |
| 52 | +api_key: "my_super_secret_api_key" |
| 53 | +password: |
| 54 | + file: |
| 55 | + path: %s |
| 56 | +`, |
| 57 | +passwordFile.Name())) |
| 58 | + |
| 59 | +varcfgMyConfig |
| 60 | +iferr:=yaml.Unmarshal(configData,&cfg);err!=nil { |
| 61 | +log.Fatalf("Error unmarshaling config: %v",err) |
| 62 | +} |
| 63 | + |
| 64 | +// Create a secret manager. This discovers and manages all Fields in cfg. |
| 65 | +// The manager will handle refreshing secrets in the background. |
| 66 | +manager,err:=secrets.NewManager(promRegisterer,secrets.Providers,&cfg) |
| 67 | +iferr!=nil { |
| 68 | +log.Fatalf("Error creating secret manager: %v",err) |
| 69 | +} |
| 70 | +// Start the manager's background refresh loop. |
| 71 | +manager.Start(context.Background()) |
| 72 | +defermanager.Stop() |
| 73 | + |
| 74 | +// Wait for the secrets in cfg to be ready. |
| 75 | +for { |
| 76 | +ifready,err:=manager.SecretsReady(&cfg);err!=nil { |
| 77 | +log.Fatalf("Error checking secret readiness: %v",err) |
| 78 | +}elseifready { |
| 79 | +break |
| 80 | +} |
| 81 | +} |
| 82 | + |
| 83 | +// Access the secret value when needed. |
| 84 | +apiKey:=cfg.APIKey.Get() |
| 85 | +password:=cfg.Password.Get() |
| 86 | + |
| 87 | +fmt.Printf("API Key: %s\n",apiKey) |
| 88 | +fmt.Printf("Password: %s\n",password) |
| 89 | + |
| 90 | +// Output: |
| 91 | +// API Key: my_super_secret_api_key |
| 92 | +// Password: my_super_secret_password |
| 93 | +} |