vvauth 2.5.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/rc.go CHANGED
@@ -5,7 +5,6 @@ import (
5
5
  "encoding/json"
6
6
  "fmt"
7
7
  "net/url"
8
- "os"
9
8
  "os/exec"
10
9
  "path/filepath"
11
10
  "regexp"
@@ -15,65 +14,11 @@ import (
15
14
  "gopkg.in/yaml.v3"
16
15
  )
17
16
 
18
- // RC is the parsed .vauthrc, after $${env.X} expansion.
19
- type RC struct {
20
- VaultAddr string `yaml:"vault_addr"`
21
- SSHAuth *SSHAuth `yaml:"ssh_auth"`
22
- JWTAuth *JWTAuth `yaml:"jwt_auth"`
23
- TokenCache *bool `yaml:"token_cache"`
17
+ // Config contains only environment/profile settings; authentication is
18
+ // parsed and owned by vauth-login.
19
+ type Config struct {
24
20
  AgentCrypt *AgentCrypt `yaml:"ssh-agent-crypt"`
25
21
  Env *EnvConfig `yaml:"env"`
26
-
27
- // authKey is the JSON of {ssh_auth, jwt_auth} in document order, as
28
- // JSON.stringify would render them: it isolates the token cache.
29
- authKey string
30
- }
31
-
32
- // AuthKey renders ssh_auth and jwt_auth as JSON members, in the order of the
33
- // parsed document. Programmatic configurations are rendered from the structs.
34
- func (rc *RC) AuthKey() string {
35
- if rc.authKey != "" {
36
- return rc.authKey
37
- }
38
- var members []string
39
- if rc.SSHAuth != nil {
40
- members = append(members, `"ssh_auth":`+jsonMembers([][2]string{{"path", rc.SSHAuth.Path}, {"role", rc.SSHAuth.Role}}))
41
- }
42
- if rc.JWTAuth != nil {
43
- members = append(members, `"jwt_auth":`+jsonMembers([][2]string{{"path", rc.JWTAuth.Path}, {"jwt", rc.JWTAuth.JWT}, {"role", rc.JWTAuth.Role}}))
44
- }
45
- return strings.Join(members, ",")
46
- }
47
-
48
- func jsonMembers(pairs [][2]string) string {
49
- var out bytes.Buffer
50
- out.WriteByte('{')
51
- for _, pair := range pairs {
52
- if pair[1] == "" {
53
- continue
54
- }
55
- if out.Len() > 1 {
56
- out.WriteByte(',')
57
- }
58
- out.Write(jsonString(pair[0]))
59
- out.WriteByte(':')
60
- out.Write(jsonString(pair[1]))
61
- }
62
- out.WriteByte('}')
63
- return out.String()
64
- }
65
-
66
- // SSHAuth configures the Vault ssh auth plugin (nonce challenge).
67
- type SSHAuth struct {
68
- Path string `yaml:"path"`
69
- Role string `yaml:"role"`
70
- }
71
-
72
- // JWTAuth configures a Vault jwt login.
73
- type JWTAuth struct {
74
- Path string `yaml:"path"`
75
- JWT string `yaml:"jwt"`
76
- Role string `yaml:"role"`
77
22
  }
78
23
 
79
24
  // AgentCrypt points to a local ssh-agent-crypt armored JSON file whose keys
@@ -244,54 +189,6 @@ func walk(node *yaml.Node, process func(string) string) {
244
189
  }
245
190
  }
246
191
 
247
- // jsonNode renders a YAML subtree as JSON.stringify would after yaml.parse.
248
- func jsonNode(node *yaml.Node, out *bytes.Buffer) {
249
- switch node.Kind {
250
- case yaml.DocumentNode, yaml.AliasNode:
251
- if node.Kind == yaml.AliasNode {
252
- jsonNode(node.Alias, out)
253
- } else if len(node.Content) > 0 {
254
- jsonNode(node.Content[0], out)
255
- }
256
- case yaml.MappingNode:
257
- out.WriteByte('{')
258
- for i := 0; i+1 < len(node.Content); i += 2 {
259
- if i > 0 {
260
- out.WriteByte(',')
261
- }
262
- out.Write(jsonString(node.Content[i].Value))
263
- out.WriteByte(':')
264
- jsonNode(node.Content[i+1], out)
265
- }
266
- out.WriteByte('}')
267
- case yaml.SequenceNode:
268
- out.WriteByte('[')
269
- for i, child := range node.Content {
270
- if i > 0 {
271
- out.WriteByte(',')
272
- }
273
- jsonNode(child, out)
274
- }
275
- out.WriteByte(']')
276
- case yaml.ScalarNode:
277
- switch node.Tag {
278
- case "!!int", "!!float", "!!bool":
279
- var value any
280
- if node.Decode(&value) == nil {
281
- if text, ok := stringify(value); ok {
282
- out.WriteString(text)
283
- return
284
- }
285
- }
286
- out.Write(jsonString(node.Value))
287
- case "!!null":
288
- out.WriteString("null")
289
- default:
290
- out.Write(jsonString(node.Value))
291
- }
292
- }
293
- }
294
-
295
192
  // jsonString encodes like JSON.stringify (no HTML escaping).
296
193
  func jsonString(value string) []byte {
297
194
  var out bytes.Buffer
@@ -319,57 +216,53 @@ func mappingValue(node *yaml.Node, key string) *yaml.Node {
319
216
  return nil
320
217
  }
321
218
 
322
- // LocateRC returns the first existing configuration file among VAUTHRC,
323
- // ./.vauthrc and ~/.vauthrc, or an empty string.
324
- func LocateRC(environ map[string]string, cwd, home string) string {
325
- candidates := []string{environ["VAUTHRC"], filepath.Join(cwd, ".vauthrc"), filepath.Join(home, ".vauthrc")}
326
- for _, candidate := range candidates {
327
- if candidate == "" {
328
- continue
329
- }
330
- if _, err := os.Stat(candidate); err == nil {
331
- return candidate
332
- }
333
- }
334
- return ""
335
- }
336
-
337
- // ParseRC parses a .vauthrc body and expands its $${env.X} macros against
338
- // environ. With env.gitlab outside CI, CI_PROJECT_* variables are derived
339
- // from the git remote of the directory holding the file.
340
- func ParseRC(body []byte, file string, environ map[string]string) (*RC, error) {
219
+ // ParseConfig expands environment/profile sections from a standalone YAML body.
220
+ // With env.gitlab outside CI, CI_PROJECT_* comes from the file's Git repository.
221
+ func ParseConfig(body []byte, file string, environ map[string]string) (*Config, error) {
341
222
  var document yaml.Node
342
223
  if err := yaml.Unmarshal(body, &document); err != nil {
343
224
  return nil, fmt.Errorf("Could not parse %s: %w", file, err)
344
225
  }
345
- if len(document.Content) == 0 {
346
- return &RC{authKey: "{}"}, nil
226
+ return compileConfig(&document, file, filepath.Dir(file), environ)
227
+ }
228
+
229
+ // configFromRC consumes the independent JSON snapshot without reopening its file.
230
+ func configFromRC(rc map[string]any, dir string, environ map[string]string) (*Config, error) {
231
+ var document yaml.Node
232
+ if err := document.Encode(rc); err != nil {
233
+ return nil, fmt.Errorf("Could not read RC: %w", err)
347
234
  }
348
- if gitlab := mappingValue(mappingValue(&document, "env"), "gitlab"); gitlab != nil && truthy(gitlab) && environ["CI"] == "" {
349
- environ = withGitLabProject(environ, filepath.Dir(file))
235
+ return compileConfig(&document, "RC", dir, environ)
236
+ }
237
+
238
+ func compileConfig(document *yaml.Node, file, dir string, environ map[string]string) (*Config, error) {
239
+ root := document
240
+ if root.Kind == yaml.DocumentNode {
241
+ if len(root.Content) == 0 {
242
+ return &Config{}, nil
243
+ }
244
+ root = root.Content[0]
350
245
  }
351
- walk(&document, func(value string) string {
352
- return ReplaceEnv(value, Lookup{"env": environ})
353
- })
354
- rc := RC{}
355
- if err := document.Decode(&rc); err != nil {
356
- return nil, fmt.Errorf("Could not parse %s: %w", file, err)
246
+ if root.Kind != yaml.MappingNode {
247
+ return nil, fmt.Errorf("Could not parse %s: expected a mapping", file)
357
248
  }
358
- var key bytes.Buffer
359
- for i, name := range []string{"ssh_auth", "jwt_auth"} {
360
- node := mappingValue(&document, name)
361
- if node == nil || node.Tag == "!!null" {
362
- continue
363
- }
364
- if i > 0 && key.Len() > 0 {
365
- key.WriteByte(',')
249
+ var content []*yaml.Node
250
+ for i := 0; i+1 < len(root.Content); i += 2 {
251
+ switch root.Content[i].Value {
252
+ case "env", "ssh-agent-crypt":
253
+ content = append(content, root.Content[i], root.Content[i+1])
366
254
  }
367
- key.Write(jsonString(name))
368
- key.WriteByte(':')
369
- jsonNode(node, &key)
370
255
  }
371
- rc.authKey = key.String()
372
- return &rc, nil
256
+ root.Content = content
257
+ if gitlab := mappingValue(mappingValue(document, "env"), "gitlab"); gitlab != nil && truthy(gitlab) && environ["CI"] == "" {
258
+ environ = withGitLabProject(environ, dir)
259
+ }
260
+ walk(document, func(value string) string { return ReplaceEnv(value, Lookup{"env": environ}) })
261
+ var config Config
262
+ if err := document.Decode(&config); err != nil {
263
+ return nil, fmt.Errorf("Could not parse %s: %w", file, err)
264
+ }
265
+ return &config, nil
373
266
  }
374
267
 
375
268
  func truthy(node *yaml.Node) bool {