Debian Codegraph @

providers/kiln/kiln.go

raw
1 // Package kiln implements a koanf.Provider that reads encrypted environment
2 // variables from kiln-encrypted files as conf maps.
3 package kiln
4
5 import (
6 "errors"
7 "strings"
8
9 "github.com/thunderbottom/kiln/pkg/kiln"
10 )
11
12 // Kiln implements a kiln encrypted environment variables provider.
13 type Kiln struct {
14 configPath string
15 keyPath string
16 file string
17 prefix string
18 transform func(key, value string) (string, any)
19 }
20
21 // Opt holds optional configuration for the kiln provider.
22 type Opt struct {
23 // If specified (case-sensitive), only env vars beginning with
24 // the prefix are captured. eg: "APP_"
25 Prefix string
26
27 // TransformFunc is an optional callback that takes an environment
28 // variable's string name and value, runs arbitrary transformations
29 // on them and returns a transformed string key and value of any type.
30 // Common usecase are stripping prefixes from keys, lowercasing variable names,
31 // replacing _ with . etc. Eg: APP_DB_HOST -> db.host
32 // If the returned variable name is an empty string (""), it is ignored altogether.
33 TransformFunc func(k, v string) (string, any)
34 }
35
36 // Provider returns a kiln provider that returns a nested map[string]any
37 // of decrypted environment variables from the specified kiln environment file.
38 //
39 // configPath is the path to the kiln.toml configuration file.
40 // keyPath is the path to the private key file (age or SSH format).
41 // If keyPath is empty, the provider will attempt to auto-discover a compatible
42 // private key from standard locations (~/.kiln/kiln.key, ~/.ssh/id_ed25519, etc.).
43 // file is the environment file name as defined in kiln.toml (e.g., "production", "staging").
44 // opt provides optional configuration for key filtering and transformation.
45 //
46 // The provider handles decryption, access control validation, and secure memory
47 // cleanup automatically. All operations respect the role-based access controls
48 // defined in the kiln.toml configuration file.
49 func Provider(configPath, keyPath, file string, opt Opt) *Kiln {
50 return &Kiln{
51 configPath: configPath,
52 keyPath: keyPath,
53 file: file,
54 prefix: opt.Prefix,
55 transform: opt.TransformFunc,
56 }
57 }
58
59 // ReadBytes is not supported by the kiln provider.
60 func (k *Kiln) ReadBytes() ([]byte, error) {
61 return nil, errors.New("kiln provider does not support this method")
62 }
63
64 // Read reads all available encrypted environment variables from the specified
65 // kiln environment file into a key:value map and returns it.
66 func (k *Kiln) Read() (map[string]any, error) {
67 cfg, err := kiln.LoadConfig(k.configPath)
68 if err != nil {
69 return nil, err
70 }
71
72 keyPath := k.keyPath
73 if keyPath == "" {
74 if discovered, discErr := kiln.DiscoverPrivateKey(); discErr == nil {
75 keyPath = discovered
76 } else {
77 return nil, discErr
78 }
79 }
80
81 identity, err := kiln.NewIdentityFromKey(keyPath)
82 if err != nil {
83 return nil, err
84 }
85 defer identity.Cleanup()
86
87 vars, cleanup, err := kiln.GetAllEnvironmentVars(identity, cfg, k.file)
88 if err != nil {
89 return nil, err
90 }
91 defer cleanup()
92
93 out := make(map[string]any)
94 for key, value := range vars {
95 if k.prefix != "" {
96 if !strings.HasPrefix(key, k.prefix) {
97 continue
98 }
99 }
100
101 // value is []bytes, so we need to convert it to
102 // string and accept any for the transformation
103 val := any(string(value))
104
105 // Apply key transformation, if there's a callback.
106 if k.transform != nil {
107 key, transformedValue := k.transform(key, string(value))
108
109 // If the callback blanked the key, omit it.
110 if key == "" {
111 continue
112 }
113
114 out[key] = transformedValue
115 }
116
117 out[key] = val
118 }
119
120 return out, nil
121 }