vvauth 2.7.2 → 2.8.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/API_and_tests.md CHANGED
@@ -91,6 +91,30 @@ err = client.Unset(key)
91
91
  err = client.Gitlab()
92
92
  ```
93
93
 
94
+ ### Go embedding with an in-memory vauthrc
95
+
96
+ ```go
97
+ client, err := vvauth.NewFromRC(rcBody, environ)
98
+ // Check err before proceeding.
99
+ exports, err := client.Env(false)
100
+ // exports.Values is map[string]string; exports.Keys preserves declaration order.
101
+ ```
102
+
103
+ `NewFromRC(body []byte, environ map[string]string) (*Vauth, error)` passes the
104
+ YAML/JSON RC to vauth-login's constructor of the same name, then compiles the
105
+ parent SDK's sections from its complete raw snapshot, just like `New()`.
106
+ It preserves the authentication constructor's contract: nil environment
107
+ inherits, non-nil environment is copied, configuration is not discovered or
108
+ merged from RC files, and the native home `~/.vauthrc` still authorizes Vault
109
+ addresses. The inline RC cannot authorize its own Vault. Home/cache/agent
110
+ behavior and all network destination checks stay owned by vauth-login.
111
+
112
+ `env.map`, local secrets, profile/KV, delegation and `env.gitlab` validation
113
+ use their existing SDK semantics. `Env` returns exports and errors without
114
+ publishing them to stdout or changing the process environment. Only explicitly
115
+ calling `PublishEnv` performs shell publication. No RC temporary file is used.
116
+ The Node constructor and both CLIs retain their existing entry points.
117
+
94
118
  `Connect`, `LookupSelf`, `Request`, immutable `RC` and address/token readers
95
119
  belong to vauth-login. vvauth owns `Config`/`config`, profiles, KV operations,
96
120
  macro expansion, shell publication and GitLab initialization. Constructors
package/go.mod CHANGED
@@ -1,12 +1,12 @@
1
1
  module github.com/131/vvauth/v2
2
2
 
3
- // version: 2.7.2
3
+ // version: 2.8.0
4
4
 
5
5
  go 1.22
6
6
 
7
7
  require (
8
8
  github.com/131/ssh-agent-crypt v1.4.4
9
- github.com/131/vauth-login v1.1.8
9
+ github.com/131/vauth-login v1.3.0
10
10
  github.com/Masterminds/semver/v3 v3.5.0
11
11
  gopkg.in/yaml.v3 v3.0.1
12
12
  )
package/go.sum CHANGED
@@ -2,10 +2,8 @@ github.com/131/nyks/v6 v6.18.0 h1:xpjRJk2YAyoqqgSvWrox0eIGZ1AY4IkEvqETavcwcQk=
2
2
  github.com/131/nyks/v6 v6.18.0/go.mod h1:ckNI5ql4USp+kQyrG1c35tl/PrPSgpoNXktcnQGJpdk=
3
3
  github.com/131/ssh-agent-crypt v1.4.4 h1:6MlDUao1Yq6iNY4Bgin/iZp2q3X/g6qD6H1X4ad8ukY=
4
4
  github.com/131/ssh-agent-crypt v1.4.4/go.mod h1:/THxpIdVrEAHxNoc6t6lVYBsSEd1zkCROPZepDe8QTI=
5
- github.com/131/vauth-login v1.1.7 h1:i256JyHP05WKqIrLv18ThfitGipH4oKnN7zpQESzV7U=
6
- github.com/131/vauth-login v1.1.7/go.mod h1:Dch6CZkaAxnYCD1V83h0g0xPQORjQoZarOOVt4mhNv8=
7
- github.com/131/vauth-login v1.1.8 h1:ElE+HTirBMmZNUPu788ndmAH844EnmWGFtCeHpi2WHs=
8
- github.com/131/vauth-login v1.1.8/go.mod h1:t7h0e1phy9sBHfsHlK2jXFstlS7VszU5gcTS4mDSnMQ=
5
+ github.com/131/vauth-login v1.3.0 h1:zRrsL4/TkFFCQCYxuDdnQFXmUNWi6CJjWKra1S0Nn/Q=
6
+ github.com/131/vauth-login v1.3.0/go.mod h1:bwMDCmFn3YVxT5Rc+Ip8zxl5XiDllp1ClV8JtqJhnMs=
9
7
  github.com/Masterminds/semver/v3 v3.5.0 h1:kQceYJfbupGfZOKZQg0kou0DgAKhzDg2NZPAwZ/2OOE=
10
8
  github.com/Masterminds/semver/v3 v3.5.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
11
9
  github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
@@ -0,0 +1,170 @@
1
+ package vvauth
2
+
3
+ import (
4
+ "bytes"
5
+ "io"
6
+ "net/http"
7
+ "os"
8
+ "path/filepath"
9
+ "strings"
10
+ "testing"
11
+ )
12
+
13
+ func memoryHome(t *testing.T) string {
14
+ t.Helper()
15
+ home := t.TempDir()
16
+ t.Setenv("HOME", home)
17
+ t.Setenv("USERPROFILE", home)
18
+ return home
19
+ }
20
+
21
+ func TestNewFromRCExportsToParent(t *testing.T) {
22
+ home := memoryHome(t)
23
+ f := newFixture(t, fixtureOptions{
24
+ metadata: map[string]any{"VAUTH_USER_LOGIN": "alice", "WHO": "profile"},
25
+ database: map[string]any{"KEY": "private"},
26
+ secrets: map[string]any{"APP_KEY": "fixture-secret"},
27
+ })
28
+ body := []byte(`vault_addr: $${env.SELECTED_VAULT}
29
+ jwt_auth: {jwt: fixture-jwt, role: fixture-role}
30
+ token_cache: false
31
+ env:
32
+ paths: [app]
33
+ map:
34
+ WHO: $${profile.WHO}
35
+ KEY: $${profile.KEY}
36
+ APP_KEY: $${secrets.APP_KEY}
37
+ INPUT: $${env.INPUT}
38
+ BOOL: true
39
+ `)
40
+ environ := map[string]string{"SELECTED_VAULT": f.server.URL, "VAULT_ADDR": f.server.URL, "INPUT": "caller-value", "SSH_AUTH_SOCK": agentSocket}
41
+ v, err := NewFromRC(body, environ)
42
+ if err != nil {
43
+ t.Fatal(err)
44
+ }
45
+ v.Stderr = io.Discard
46
+ var output bytes.Buffer
47
+ v.Stdout = &output
48
+ environ["INPUT"] = "changed"
49
+ exports, err := v.Env(false)
50
+ if err != nil {
51
+ t.Fatal(err)
52
+ }
53
+ for key, want := range map[string]string{
54
+ "VAULT_ADDR": f.server.URL, "VAULT_TOKEN": "fresh-token", "WHO": "profile",
55
+ "KEY": "private", "APP_KEY": "fixture-secret", "INPUT": "caller-value", "BOOL": "true",
56
+ } {
57
+ got, ok := exports.Get(key)
58
+ if !ok || got != want {
59
+ t.Fatalf("incorrect export %s", key)
60
+ }
61
+ }
62
+ equal(t, output.Len(), 0)
63
+ equal(t, v.HomeDir, home)
64
+ equal(t, v.RC()["vault_addr"], "$${env.SELECTED_VAULT}")
65
+ equal(t, v.RC()["env"].(map[string]any)["map"].(map[string]any)["INPUT"], "$${env.INPUT}")
66
+ equal(t, v.RC()["env"].(map[string]any)["map"].(map[string]any)["BOOL"], true)
67
+ entries, err := os.ReadDir(home)
68
+ if err != nil {
69
+ t.Fatal(err)
70
+ }
71
+ equal(t, len(entries), 0)
72
+ }
73
+
74
+ func TestNewFromRCUsesHomeVaultTrust(t *testing.T) {
75
+ for _, trusted := range []bool{false, true} {
76
+ t.Run(map[bool]string{false: "unknown", true: "known"}[trusted], func(t *testing.T) {
77
+ home := memoryHome(t)
78
+ f := newFixture(t, fixtureOptions{})
79
+ if trusted {
80
+ if err := os.WriteFile(filepath.Join(home, ".vauthrc"), []byte("known_vaults: ['"+f.server.URL+"']"), 0600); err != nil {
81
+ t.Fatal(err)
82
+ }
83
+ }
84
+ body := []byte("vault_addr: " + f.server.URL + "\nknown_vaults: ['" + f.server.URL + "']\nenv: {map: {VALUE: local}}")
85
+ v, err := NewFromRC(body, map[string]string{"VAULT_TOKEN": "inherited-fixture-token"})
86
+ if err != nil {
87
+ t.Fatal(err)
88
+ }
89
+ v.Stderr = io.Discard
90
+ exports, err := v.Env(false)
91
+ if trusted {
92
+ if err != nil {
93
+ t.Fatal(err)
94
+ }
95
+ value, _ := exports.Get("VALUE")
96
+ equal(t, value, "local")
97
+ } else {
98
+ if err == nil || !strings.Contains(err.Error(), "known_vaults") || exports != nil {
99
+ t.Fatalf("unknown inline Vault accepted: %v", err)
100
+ }
101
+ equal(t, len(f.requests), 0)
102
+ }
103
+ })
104
+ }
105
+ }
106
+
107
+ func TestNewFromRCValidatesParentSections(t *testing.T) {
108
+ memoryHome(t)
109
+ for _, body := range []string{"vault_addr: [", "delegate: [invalid/role]", "env: {map: {VALUE: [not-a-scalar]}}"} {
110
+ if v, err := NewFromRC([]byte(body), map[string]string{}); err == nil || v != nil {
111
+ t.Fatal("invalid inline RC accepted")
112
+ }
113
+ }
114
+ body := []byte("env: {gitlab: true, map: {PROJECT: '$${env.CI_PROJECT_PATH}'}}")
115
+ if v, err := NewFromRC(body, map[string]string{}); err == nil || v != nil || !strings.Contains(err.Error(), "CI_PROJECT_PATH") {
116
+ t.Fatalf("GitLab prerequisite not checked: %v", err)
117
+ }
118
+ environ := map[string]string{"CI_PROJECT_PATH": "team/project", "CI_PROJECT_NAME": "project", "CI_PROJECT_NAMESPACE": "team", "CI_PROJECT_PATH_SLUG": "team-project"}
119
+ v, err := NewFromRC(body, environ)
120
+ if err != nil {
121
+ t.Fatal(err)
122
+ }
123
+ v.Stderr = io.Discard
124
+ exports, err := v.Env(false)
125
+ if err != nil {
126
+ t.Fatal(err)
127
+ }
128
+ value, _ := exports.Get("PROJECT")
129
+ equal(t, value, "team/project")
130
+ }
131
+
132
+ func TestNewFromRCDelegatedExports(t *testing.T) {
133
+ memoryHome(t)
134
+ body := []byte(`vault_addr: https://synthetic.invalid
135
+ token_cache: false
136
+ delegate: [client-ro]
137
+ env:
138
+ map:
139
+ ROUTER_TOKEN: $${delegate.client-ro}
140
+ `)
141
+ v, err := NewFromRC(body, map[string]string{"VAULT_ADDR": "https://synthetic.invalid", "VAULT_TOKEN": "fixture-parent"})
142
+ if err != nil {
143
+ t.Fatal(err)
144
+ }
145
+ v.Stderr = io.Discard
146
+ creates := 0
147
+ v.HTTPClient = &http.Client{Transport: delegateTransport(func(r *http.Request) (*http.Response, error) {
148
+ response := `{"data":{"ttl":2000,"entity_id":"synthetic-entity"}}`
149
+ switch r.URL.Path {
150
+ case "/v1/identity/entity/id/synthetic-entity":
151
+ response = `{"data":{"metadata":{}}}`
152
+ case "/v1/auth/token/create/client-ro":
153
+ creates++
154
+ response = `{"auth":{"client_token":"fixture-child","lease_duration":1000}}`
155
+ case "/v1/auth/token/lookup-self":
156
+ default:
157
+ t.Fatalf("unexpected request %s", r.URL.Path)
158
+ }
159
+ return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(response))}, nil
160
+ })}
161
+ for i := 0; i < 2; i++ {
162
+ exports, err := v.Env(false)
163
+ if err != nil {
164
+ t.Fatal(err)
165
+ }
166
+ equal(t, exports.Values["VAULT_TOKEN"], "fixture-parent")
167
+ equal(t, exports.Values["ROUTER_TOKEN"], "fixture-child")
168
+ }
169
+ equal(t, creates, 1) // In-memory delegation reuse remains available without disk cache.
170
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vvauth",
3
- "version": "2.7.2",
3
+ "version": "2.8.0",
4
4
  "description": "Vault Auth helper",
5
5
  "main": "index.js",
6
6
  "bin": {
package/vauth.go CHANGED
@@ -80,17 +80,40 @@ func CheckProjectVersion(manifest, version string) error {
80
80
  // New loads authentication through vauth-login, then the environment/profile
81
81
  // configuration from the same selected .vauthrc.
82
82
  func New() (*Vauth, error) {
83
- cwd, err := os.Getwd()
83
+ if err := checkProjectVersion(); err != nil {
84
+ return nil, err
85
+ }
86
+ login, err := vauthlogin.New()
84
87
  if err != nil {
85
88
  return nil, err
86
89
  }
87
- if err := CheckProjectVersion(filepath.Join(cwd, "package.json"), Version); err != nil {
90
+ return newClient(login)
91
+ }
92
+
93
+ // NewFromRC constructs from a YAML/JSON RC supplied in memory. The environment
94
+ // is copied (nil inherits the process environment); native home Vault trust and
95
+ // cache handling remain owned by vauth-login. Env returns exports to the caller.
96
+ func NewFromRC(body []byte, environ map[string]string) (*Vauth, error) {
97
+ if err := checkProjectVersion(); err != nil {
88
98
  return nil, err
89
99
  }
90
- login, err := vauthlogin.New()
100
+ login, err := vauthlogin.NewFromRC(body, environ)
91
101
  if err != nil {
92
102
  return nil, err
93
103
  }
104
+ return newClient(login)
105
+ }
106
+
107
+ func checkProjectVersion() error {
108
+ cwd, err := os.Getwd()
109
+ if err != nil {
110
+ return err
111
+ }
112
+ return CheckProjectVersion(filepath.Join(cwd, "package.json"), Version)
113
+ }
114
+
115
+ // newClient compiles only the parent SDK's sections from the login RC snapshot.
116
+ func newClient(login *vauthlogin.Vauth) (*Vauth, error) {
94
117
  v := &Vauth{Vauth: login}
95
118
  config, err := configFromRC(login.RC(), login.Environ)
96
119
  if err != nil {