| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"log" |
| 6 |
"strings" |
| 7 |
|
| 8 |
"github.com/knadh/koanf/parsers/json" |
| 9 |
"github.com/knadh/koanf/providers/file" |
| 10 |
"github.com/knadh/koanf/providers/kiln" |
| 11 |
"github.com/knadh/koanf/v2" |
| 12 |
) |
| 13 |
|
| 14 |
var k = koanf.New(".") |
| 15 |
|
| 16 |
// Run this example: |
| 17 |
// |
| 18 |
// kiln init key |
| 19 |
// kiln init config --path="./examples/read-kiln/kiln.toml" --recipients $(whoami)=$(cat ~/.kiln/kiln.key.pub) |
| 20 |
// export KILN_CONFIG_FILE=./examples/read-kiln/kiln.toml |
| 21 |
// kiln set API_KEY super-secret-dev-key |
| 22 |
// kiln set API_HOST example.com |
| 23 |
// kiln set DB_HOST postgres://user@localhost:5432 |
| 24 |
// go run ./examples/read-kiln-environment |
| 25 |
// |
| 26 |
// This example demonstrates loading configuration from multiple sources: |
| 27 |
// 1. A JSON config file for defaults |
| 28 |
// 2. Encrypted kiln environment variables that override defaults |
| 29 |
// |
| 30 |
// The kiln provider decrypts environment variables from encrypted files |
| 31 |
// and applies transformations similar to the env provider. |
| 32 |
// |
| 33 |
// Example encrypted environment file (.kiln.env): |
| 34 |
// |
| 35 |
// API_KEY=super-secret-dev-key |
| 36 |
// API_HOST=example.com |
| 37 |
func main() { |
| 38 |
// Load JSON config for default values. |
| 39 |
if err := k.Load(file.Provider("mock/mock.json"), json.Parser()); err != nil { |
| 40 |
log.Fatalf("error loading config: %v", err) |
| 41 |
} |
| 42 |
|
| 43 |
// Load encrypted environment variables from kiln and merge into config. |
| 44 |
// The kiln provider decrypts variables from the specified environment file |
| 45 |
// and applies optional transformations for seamless integration. |
| 46 |
// |
| 47 |
// In this example: |
| 48 |
// - Only variables with "API_" prefix are loaded |
| 49 |
// - Keys are transformed to lowercase with dots for nesting |
| 50 |
// - The prefix "api." is stripped |
| 51 |
// - API_KEY becomes "key", API_HOST becomes "host" |
| 52 |
// |
| 53 |
// The provider automatically handles: |
| 54 |
// - Key discovery from standard locations (~/.kiln/kiln.key, ~/.ssh/id_*) |
| 55 |
// - Decryption using age or SSH keys |
| 56 |
// - Access control validation per kiln.toml |
| 57 |
// - Secure memory cleanup after operations |
| 58 |
if err := k.Load(kiln.Provider("kiln.toml", "", "default", kiln.Opt{ |
| 59 |
Prefix: "API_", |
| 60 |
TransformFunc: func(key, value string) (string, any) { |
| 61 |
// Strip common prefixes and normalize to lowercase with dots |
| 62 |
key = strings.ToLower(key) |
| 63 |
key = strings.ReplaceAll(key, "_", ".") |
| 64 |
|
| 65 |
// Remove prefixes for cleaner config keys |
| 66 |
if strings.HasPrefix(key, "api.") { |
| 67 |
key = strings.TrimPrefix(key, "api.") |
| 68 |
} |
| 69 |
|
| 70 |
return key, value |
| 71 |
}, |
| 72 |
}), nil); err != nil { |
| 73 |
log.Fatalf("error loading kiln config: %v", err) |
| 74 |
} |
| 75 |
|
| 76 |
// Display the merged configuration |
| 77 |
fmt.Println("api key is =", k.String("key")) |
| 78 |
fmt.Println("api host is =", k.String("host")) |
| 79 |
fmt.Println("Parent name from JSON =", k.String("parent1.name")) |
| 80 |
} |