Commit 99f089b
committed
feat(loader): add viper-backed shared config loader for downstream tools
Introduces a new loader/ subpackage that resolves Keyfactor config
from layered sources (CLI flag > env > config file > defaults) and
returns a populated auth_providers.Server. Designed so multiple
Keyfactor Go tools (kfacme-cli, kfutil, terraform-provider-keyfactor,
future tools) can share the same env vars and the same
~/.keyfactor/command_config.{json,yml,yaml} layout, with each tool's
extension fields living in a named sub-block (e.g. acme:) under each
Server entry.
What this PR adds:
* New `loader/` subpackage. Viper is the only new transitive
dependency, and it stays scoped to this subpackage — consumers
that don't import loader keep their slim dep trees.
* `Loader` type with functional options: WithConfigFile, WithProfile,
WithFlagSet (for cobra/pflag integration), WithDefaults,
WithToolNamespace.
* File discovery preserves kfc-auth's existing convention: explicit
--config-file flag > WithConfigFile option > KEYFACTOR_AUTH_CONFIG_FILE
> ~/.keyfactor/command_config.{json,yaml,yml}.
* Profile selection: WithProfile > KEYFACTOR_AUTH_CONFIG_PROFILE >
DefaultConfigProfile ("default").
* Per-tool sub-blocks under each Server, decoded into caller-supplied
structs via Loader.DecodeExtras(namespace, target). Tool-namespaced
env vars (e.g. KEYFACTOR_ACME_BASE_URL → servers.<profile>.acme.base_url)
are bound by reflection over the schema struct's mapstructure tags.
* Override merge with strict mode: any auth-method-specific field set
in a sub-block forces the sub-block to declare the entire tuple;
partial overrides return a clear error naming the missing field.
This catches "I forgot client_secret" mistakes at load time rather
than at API call time. See ResolvedAuth() docs and the
partial_override.yaml fixture.
What this PR adds for the validation split (Phase 2, folded in):
* New `auth_providers.AuthCreds` type: the auth-only view of a
Server, decoupled from Command target fields (Host / Port /
APIPath). Lets callers validate credentials independently of
whether they're targeting Command, ACME, or anything else.
* `AuthCreds.Validate()` returns the resolved auth method
(basic / oauth2 / token / kerberos) or a structured error.
* `AuthCredsFromServer()` extracts the auth fields from a Server.
* Existing CommandAuthConfig.ValidateAuthConfig() is UNCHANGED in
signature and behavior — kfutil and terraform-provider keep
validating "auth + Command URL + reachability" exactly as before.
The new AuthCreds-only path is opt-in for callers that don't
target Command.
What this PR adds to the Server struct (additive, non-breaking):
* New `Extras map[string]any` field with `json:"-" yaml:"-"
mapstructure:",remain"`. The loader populates it with the unknown
keys at each Server entry. Existing JSON/YAML wire format is
unchanged because the tags exclude Extras from direct marshaling;
the loader handles sub-block decoding separately.
Tests:
* loader/loader_test.go covers shared-creds inheritance, separate-creds
override, ACME-only profile (no Host), mixed auth methods
per tool, partial-override rejection, profile selection,
env-var override, tool-namespace env binding, and the
AuthCreds.Validate matrix.
* Existing auth_providers tests continue to pass on the unchanged
code paths. Pre-existing Azure / config-format failures on
origin/release-v1.6 are unaffected by this PR.
Backward compatibility:
* Every existing exported symbol in auth_providers/ keeps its
signature.
* Server gains one field (Extras) with marshal-suppressing tags;
existing serialization paths produce identical output.
* No consumer is forced to import loader/ — opt-in adoption.
Follow-up work (separate PRs in other repos):
* kfacme-cli migrates internal/config to use loader/, with a one-time
~/.keyfactor/kfacme_config.json → ~/.keyfactor/command_config.yaml
migrator.
* kfutil wires its currently-defined-but-ignored flags through the
loader and retires its local ConfigurationFile mirror types.
* terraform-provider-keyfactor falls back to the loader when the
HCL provider block is incomplete.1 parent e114c03 commit 99f089b
18 files changed
Lines changed: 1694 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
50 | 63 | | |
51 | 64 | | |
52 | 65 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
23 | 25 | | |
24 | 26 | | |
25 | 27 | | |
| |||
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
| 36 | + | |
| 37 | + | |
34 | 38 | | |
35 | 39 | | |
36 | 40 | | |
| |||
40 | 44 | | |
41 | 45 | | |
42 | 46 | | |
| 47 | + | |
43 | 48 | | |
44 | 49 | | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
45 | 56 | | |
46 | 57 | | |
47 | 58 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | 1 | | |
4 | 2 | | |
5 | 3 | | |
| |||
19 | 17 | | |
20 | 18 | | |
21 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
22 | 26 | | |
23 | 27 | | |
| 28 | + | |
| 29 | + | |
24 | 30 | | |
25 | 31 | | |
26 | 32 | | |
| |||
50 | 56 | | |
51 | 57 | | |
52 | 58 | | |
| 59 | + | |
| 60 | + | |
53 | 61 | | |
54 | 62 | | |
55 | 63 | | |
56 | 64 | | |
57 | 65 | | |
58 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
59 | 79 | | |
60 | 80 | | |
61 | 81 | | |
| |||
65 | 85 | | |
66 | 86 | | |
67 | 87 | | |
| 88 | + | |
| 89 | + | |
68 | 90 | | |
| 91 | + | |
| 92 | + | |
69 | 93 | | |
70 | 94 | | |
71 | 95 | | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | 96 | | |
77 | 97 | | |
78 | 98 | | |
| |||
82 | 102 | | |
83 | 103 | | |
84 | 104 | | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | 105 | | |
89 | 106 | | |
90 | 107 | | |
| |||
98 | 115 | | |
99 | 116 | | |
100 | 117 | | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | 118 | | |
105 | 119 | | |
106 | 120 | | |
| |||
110 | 124 | | |
111 | 125 | | |
112 | 126 | | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | 127 | | |
117 | 128 | | |
118 | 129 | | |
| |||
0 commit comments