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/.github/workflows/test.yml +14 -0
- package/API_and_tests.md +144 -0
- package/README.md +87 -33
- package/SECURITY_AUDIT_fable-5.1.md +88 -0
- package/SECURITY_AUDIT_gpt-6.1-sol.md +196 -0
- package/cmd/vauth/main.go +19 -3
- package/go.mod +5 -3
- package/go.sum +12 -2
- package/index.js +146 -345
- package/package.json +7 -7
- package/rc.go +41 -148
- package/test/token-cache.js +271 -135
- package/vault.go +5 -197
- package/vauth.go +109 -228
- package/vauth_test.go +315 -111
- package/cache.go +0 -101
- package/humandiff.go +0 -50
package/vault.go
CHANGED
|
@@ -1,209 +1,17 @@
|
|
|
1
1
|
package vvauth
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
|
-
"bytes"
|
|
5
|
-
"encoding/base64"
|
|
6
4
|
"encoding/json"
|
|
7
|
-
"errors"
|
|
8
5
|
"fmt"
|
|
9
|
-
"io"
|
|
10
|
-
"net/http"
|
|
11
6
|
"strings"
|
|
12
7
|
|
|
13
|
-
|
|
14
|
-
"golang.org/x/crypto/ssh/agent"
|
|
8
|
+
vauthlogin "github.com/131/vauth-login"
|
|
15
9
|
)
|
|
16
10
|
|
|
17
|
-
// HTTPError reports an unexpected Vault status code.
|
|
18
|
-
type HTTPError struct {
|
|
19
|
-
Status int
|
|
20
|
-
Body string
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
func (e *HTTPError) Error() string {
|
|
24
|
-
return fmt.Sprintf("Unexpected status %d: %s", e.Status, e.Body)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// auth is the Vault auth block of a login or renewal response.
|
|
28
|
-
type auth struct {
|
|
29
|
-
ClientToken string `json:"client_token"`
|
|
30
|
-
LeaseDuration int64 `json:"lease_duration"`
|
|
31
|
-
Renewable bool `json:"renewable"`
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// lookup is the relevant part of auth/token/lookup-self.
|
|
35
|
-
type lookup struct {
|
|
36
|
-
TTL float64 `json:"ttl"`
|
|
37
|
-
Renewable bool `json:"renewable"`
|
|
38
|
-
CreationTTL int64 `json:"creation_ttl"`
|
|
39
|
-
EntityID string `json:"entity_id"`
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
func (v *Vauth) vaultURL(path string) string {
|
|
43
|
-
return strings.TrimRight(v.VaultAddr, "/") + "/v1/" + strings.TrimLeft(path, "/")
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// request performs a Vault API call. A JSON payload is sent when non-nil.
|
|
47
|
-
// With expect > 0, another status is returned as *HTTPError.
|
|
48
|
-
func (v *Vauth) request(method, path, token string, payload any, expect int) (int, []byte, error) {
|
|
49
|
-
var body io.Reader
|
|
50
|
-
if payload != nil {
|
|
51
|
-
encoded, err := json.Marshal(payload)
|
|
52
|
-
if err != nil {
|
|
53
|
-
return 0, nil, err
|
|
54
|
-
}
|
|
55
|
-
body = bytes.NewReader(encoded)
|
|
56
|
-
}
|
|
57
|
-
req, err := http.NewRequest(method, v.vaultURL(path), body)
|
|
58
|
-
if err != nil {
|
|
59
|
-
return 0, nil, err
|
|
60
|
-
}
|
|
61
|
-
if payload != nil {
|
|
62
|
-
req.Header.Set("Content-Type", "application/json")
|
|
63
|
-
}
|
|
64
|
-
if token != "" {
|
|
65
|
-
req.Header.Set("X-Vault-Token", token)
|
|
66
|
-
}
|
|
67
|
-
res, err := v.httpClient().Do(req)
|
|
68
|
-
if err != nil {
|
|
69
|
-
return 0, nil, err
|
|
70
|
-
}
|
|
71
|
-
defer res.Body.Close()
|
|
72
|
-
response, err := io.ReadAll(res.Body)
|
|
73
|
-
if err != nil {
|
|
74
|
-
return res.StatusCode, nil, err
|
|
75
|
-
}
|
|
76
|
-
if expect > 0 && res.StatusCode != expect {
|
|
77
|
-
return res.StatusCode, response, &HTTPError{res.StatusCode, string(response)}
|
|
78
|
-
}
|
|
79
|
-
return res.StatusCode, response, nil
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
func (v *Vauth) httpClient() *http.Client {
|
|
83
|
-
if v.HTTPClient != nil {
|
|
84
|
-
return v.HTTPClient
|
|
85
|
-
}
|
|
86
|
-
return http.DefaultClient
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
func parseAuth(response []byte, invalid string) (*auth, error) {
|
|
90
|
-
var parsed struct {
|
|
91
|
-
Auth *auth `json:"auth"`
|
|
92
|
-
}
|
|
93
|
-
if err := json.Unmarshal(response, &parsed); err != nil || parsed.Auth == nil || parsed.Auth.ClientToken == "" || parsed.Auth.LeaseDuration < 0 {
|
|
94
|
-
return nil, errors.New(invalid)
|
|
95
|
-
}
|
|
96
|
-
return parsed.Auth, nil
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
func (v *Vauth) loginVault(path string, payload any) (*auth, error) {
|
|
100
|
-
status, response, err := v.request("POST", "auth/"+path+"/login", "", payload, 0)
|
|
101
|
-
if err != nil {
|
|
102
|
-
return nil, err
|
|
103
|
-
}
|
|
104
|
-
if status != 200 {
|
|
105
|
-
return nil, fmt.Errorf("Could not login to vault : %s", response)
|
|
106
|
-
}
|
|
107
|
-
return parseAuth(response, "Invalid Vault login response")
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
func (v *Vauth) loginVaultSSH(config *SSHAuth) (*auth, error) {
|
|
111
|
-
path := config.Path
|
|
112
|
-
if path == "" {
|
|
113
|
-
path = "ssh"
|
|
114
|
-
}
|
|
115
|
-
v.debug("Trying to auth as '%s'", config.Role)
|
|
116
|
-
conn, err := sshagentcrypt.DialAgent(v.Agent)
|
|
117
|
-
if err != nil {
|
|
118
|
-
return nil, fmt.Errorf("Could not reach ssh-agent at %s: %w", v.Agent, err)
|
|
119
|
-
}
|
|
120
|
-
defer conn.Close()
|
|
121
|
-
client := agent.NewClient(conn)
|
|
122
|
-
keys, err := client.List()
|
|
123
|
-
if err != nil {
|
|
124
|
-
return nil, fmt.Errorf("Unable to list ssh-agent keys: %w", err)
|
|
125
|
-
}
|
|
126
|
-
for _, key := range keys {
|
|
127
|
-
_, response, err := v.request("GET", "auth/"+path+"/nonce", "", nil, 200)
|
|
128
|
-
if err != nil {
|
|
129
|
-
return nil, err
|
|
130
|
-
}
|
|
131
|
-
var challenge struct {
|
|
132
|
-
Data struct {
|
|
133
|
-
Nonce string `json:"nonce"`
|
|
134
|
-
} `json:"data"`
|
|
135
|
-
}
|
|
136
|
-
if err := json.Unmarshal(response, &challenge); err != nil {
|
|
137
|
-
return nil, fmt.Errorf("Invalid nonce response: %w", err)
|
|
138
|
-
}
|
|
139
|
-
nonce := []byte(challenge.Data.Nonce)
|
|
140
|
-
signature, err := client.Sign(key, nonce)
|
|
141
|
-
if err != nil {
|
|
142
|
-
v.debug("ssh : agent refused to sign with public key %s", key.Comment)
|
|
143
|
-
continue
|
|
144
|
-
}
|
|
145
|
-
payload := map[string]string{
|
|
146
|
-
"public_key": key.Type() + " " + base64.StdEncoding.EncodeToString(key.Marshal()),
|
|
147
|
-
"role": config.Role,
|
|
148
|
-
"nonce": base64.StdEncoding.EncodeToString(nonce),
|
|
149
|
-
"signature": base64.StdEncoding.EncodeToString(signature.Blob),
|
|
150
|
-
}
|
|
151
|
-
auth, err := v.loginVault(path, payload)
|
|
152
|
-
if err != nil {
|
|
153
|
-
v.debug("ssh : invalid challenge for public key %s", key.Comment)
|
|
154
|
-
continue
|
|
155
|
-
}
|
|
156
|
-
return auth, nil
|
|
157
|
-
}
|
|
158
|
-
return nil, errors.New("Could not login to vault")
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
func (v *Vauth) lookupToken(token string) (*lookup, int64, error) {
|
|
162
|
-
_, response, err := v.request("GET", "auth/token/lookup-self", token, nil, 200)
|
|
163
|
-
if err != nil {
|
|
164
|
-
return nil, 0, err
|
|
165
|
-
}
|
|
166
|
-
var parsed struct {
|
|
167
|
-
Data *lookup `json:"data"`
|
|
168
|
-
}
|
|
169
|
-
if err := json.Unmarshal(response, &parsed); err != nil || parsed.Data == nil || parsed.Data.TTL < 0 {
|
|
170
|
-
return nil, 0, errors.New("Invalid Vault token TTL")
|
|
171
|
-
}
|
|
172
|
-
lease := parsed.Data.CreationTTL
|
|
173
|
-
if cached := v.readTokenCache(); cached != nil && cached.Token == token {
|
|
174
|
-
lease = cached.LeaseDuration
|
|
175
|
-
}
|
|
176
|
-
return parsed.Data, lease, nil
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
func (v *Vauth) renewToken(token string) (*auth, error) {
|
|
180
|
-
_, response, err := v.request("POST", "auth/token/renew-self", token, map[string]any{}, 200)
|
|
181
|
-
if err != nil {
|
|
182
|
-
return nil, err
|
|
183
|
-
}
|
|
184
|
-
return parseAuth(response, "Invalid Vault token renewal response")
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
func (v *Vauth) lookupIdentity(token, id string) (map[string]any, error) {
|
|
188
|
-
_, response, err := v.request("GET", "identity/entity/id/"+id, token, nil, 200)
|
|
189
|
-
if err != nil {
|
|
190
|
-
return nil, err
|
|
191
|
-
}
|
|
192
|
-
var parsed struct {
|
|
193
|
-
Data struct {
|
|
194
|
-
Metadata map[string]any `json:"metadata"`
|
|
195
|
-
} `json:"data"`
|
|
196
|
-
}
|
|
197
|
-
if err := json.Unmarshal(response, &parsed); err != nil {
|
|
198
|
-
return nil, fmt.Errorf("Invalid identity response: %w", err)
|
|
199
|
-
}
|
|
200
|
-
return parsed.Data.Metadata, nil
|
|
201
|
-
}
|
|
202
|
-
|
|
203
11
|
// vaultRead returns the data of a kv2 secret.
|
|
204
12
|
func (v *Vauth) vaultRead(mount, secretPath string, optional bool) (map[string]any, error) {
|
|
205
13
|
secretPath = strings.Trim(secretPath, "/")
|
|
206
|
-
status, response, err := v.
|
|
14
|
+
status, response, err := v.Request("GET", mount+"/data/"+secretPath, v.VAULT_TOKEN(), nil, 0)
|
|
207
15
|
if err != nil {
|
|
208
16
|
return nil, err
|
|
209
17
|
}
|
|
@@ -211,7 +19,7 @@ func (v *Vauth) vaultRead(mount, secretPath string, optional bool) (map[string]a
|
|
|
211
19
|
return map[string]any{}, nil
|
|
212
20
|
}
|
|
213
21
|
if status != 200 {
|
|
214
|
-
return nil,
|
|
22
|
+
return nil, &vauthlogin.HTTPError{Status: status, Body: string(response)}
|
|
215
23
|
}
|
|
216
24
|
var parsed struct {
|
|
217
25
|
Data struct {
|
|
@@ -227,12 +35,12 @@ func (v *Vauth) vaultRead(mount, secretPath string, optional bool) (map[string]a
|
|
|
227
35
|
// vaultWrite replaces the data of a kv2 secret.
|
|
228
36
|
func (v *Vauth) vaultWrite(mount, secretPath string, data map[string]any) error {
|
|
229
37
|
secretPath = strings.Trim(secretPath, "/")
|
|
230
|
-
status, response, err := v.
|
|
38
|
+
status, response, err := v.Request("POST", mount+"/data/"+secretPath, v.VAULT_TOKEN(), map[string]any{"data": data}, 0)
|
|
231
39
|
if err != nil {
|
|
232
40
|
return err
|
|
233
41
|
}
|
|
234
42
|
if status != 200 {
|
|
235
|
-
return
|
|
43
|
+
return &vauthlogin.HTTPError{Status: status, Body: string(response)}
|
|
236
44
|
}
|
|
237
45
|
return nil
|
|
238
46
|
}
|
package/vauth.go
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
// Package vvauth
|
|
2
|
-
//
|
|
3
|
-
// declared by .vauthrc. It is the Go counterpart of the vvauth npm package.
|
|
1
|
+
// Package vvauth resolves the environment and profile declared by .vauthrc.
|
|
2
|
+
// Authentication, token caching and HTTP transport belong to vauth-login.
|
|
4
3
|
package vvauth
|
|
5
4
|
|
|
6
5
|
import (
|
|
@@ -9,17 +8,26 @@ import (
|
|
|
9
8
|
"errors"
|
|
10
9
|
"fmt"
|
|
11
10
|
"io"
|
|
12
|
-
"net/http"
|
|
13
11
|
"os"
|
|
14
12
|
"os/exec"
|
|
15
13
|
"path/filepath"
|
|
16
14
|
"regexp"
|
|
17
15
|
"strings"
|
|
18
|
-
"time"
|
|
19
16
|
|
|
20
17
|
sshagentcrypt "github.com/131/ssh-agent-crypt"
|
|
18
|
+
vauthlogin "github.com/131/vauth-login"
|
|
19
|
+
"github.com/Masterminds/semver/v3"
|
|
21
20
|
)
|
|
22
21
|
|
|
22
|
+
// PackageName is the npm name a project uses to pin vauth:
|
|
23
|
+
// `"dependencies": {"vvauth": "^2.5.0"}` in its package.json.
|
|
24
|
+
const PackageName = "vvauth"
|
|
25
|
+
|
|
26
|
+
// Version is the running version (ldflags -X or debug.ReadBuildInfo), checked
|
|
27
|
+
// by New against the current directory's package.json; empty or non-semver
|
|
28
|
+
// values ("(devel)") skip the check.
|
|
29
|
+
var Version string
|
|
30
|
+
|
|
23
31
|
// FunctionName is the shell function installed by Install.
|
|
24
32
|
const FunctionName = "venv"
|
|
25
33
|
|
|
@@ -29,83 +37,65 @@ const FunctionDecl = `function ` + FunctionName + `() { source <(/usr/bin/env va
|
|
|
29
37
|
// Vauth is a configured client. New builds one from the process environment;
|
|
30
38
|
// fields may be set directly for programmatic use.
|
|
31
39
|
type Vauth struct {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
Agent string
|
|
37
|
-
// TokenCache enables the on-disk token cache in CacheDir.
|
|
38
|
-
TokenCache bool
|
|
39
|
-
CacheDir string
|
|
40
|
-
HomeDir string
|
|
41
|
-
// Environ is the process environment snapshot used for macros.
|
|
42
|
-
Environ map[string]string
|
|
43
|
-
// Stdout/Stderr/Stdin default to the process streams.
|
|
44
|
-
Stdout io.Writer
|
|
45
|
-
Stderr io.Writer
|
|
46
|
-
Stdin io.Reader
|
|
47
|
-
HTTPClient *http.Client
|
|
48
|
-
// Now overrides the clock (tests).
|
|
49
|
-
Now func() time.Time
|
|
50
|
-
|
|
51
|
-
connected bool
|
|
40
|
+
*vauthlogin.Vauth
|
|
41
|
+
Config Config
|
|
42
|
+
Stdout io.Writer
|
|
43
|
+
Stdin io.Reader
|
|
52
44
|
}
|
|
53
45
|
|
|
54
|
-
//
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
environ[key] = value
|
|
68
|
-
}
|
|
46
|
+
// CheckProjectVersion fails when manifest (a package.json) declares a
|
|
47
|
+
// dependency on PackageName that version does not satisfy. A missing manifest,
|
|
48
|
+
// dependency or unparseable version is not an error.
|
|
49
|
+
func CheckProjectVersion(manifest, version string) error {
|
|
50
|
+
body, err := os.ReadFile(manifest)
|
|
51
|
+
if err != nil {
|
|
52
|
+
return nil
|
|
53
|
+
}
|
|
54
|
+
var pkg struct {
|
|
55
|
+
Dependencies map[string]string `json:"dependencies"`
|
|
56
|
+
}
|
|
57
|
+
if json.Unmarshal(body, &pkg) != nil {
|
|
58
|
+
return nil
|
|
69
59
|
}
|
|
70
|
-
|
|
60
|
+
required, ok := pkg.Dependencies[PackageName]
|
|
61
|
+
if !ok || required == "" {
|
|
62
|
+
return nil
|
|
63
|
+
}
|
|
64
|
+
current, err := semver.NewVersion(version)
|
|
65
|
+
if err != nil {
|
|
66
|
+
return nil
|
|
67
|
+
}
|
|
68
|
+
constraint, err := semver.NewConstraint(required)
|
|
69
|
+
if err != nil {
|
|
70
|
+
return fmt.Errorf("Invalid %s version requirement '%s' in %s", PackageName, required, manifest)
|
|
71
|
+
}
|
|
72
|
+
if !constraint.Check(current) {
|
|
73
|
+
return fmt.Errorf("Unsupported %s version %s (project requires %s)", PackageName, version, required)
|
|
74
|
+
}
|
|
75
|
+
return nil
|
|
71
76
|
}
|
|
72
77
|
|
|
73
|
-
// New loads
|
|
74
|
-
//
|
|
78
|
+
// New loads authentication through vauth-login, then the environment/profile
|
|
79
|
+
// configuration from the same selected .vauthrc.
|
|
75
80
|
func New() (*Vauth, error) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
v := &Vauth{
|
|
80
|
-
Agent: sshagentcrypt.DefaultAgentAddress(),
|
|
81
|
-
TokenCache: true,
|
|
82
|
-
HomeDir: home,
|
|
83
|
-
CacheDir: filepath.Join(home, ".vauth", "tokens"),
|
|
84
|
-
Environ: environ,
|
|
85
|
-
}
|
|
86
|
-
if file := LocateRC(environ, cwd, home); file != "" {
|
|
87
|
-
body, err := os.ReadFile(file)
|
|
88
|
-
if err != nil {
|
|
89
|
-
return nil, err
|
|
90
|
-
}
|
|
91
|
-
rc, err := ParseRC(body, file, environ)
|
|
92
|
-
if err != nil {
|
|
93
|
-
return nil, err
|
|
94
|
-
}
|
|
95
|
-
v.RC = *rc
|
|
81
|
+
cwd, err := os.Getwd()
|
|
82
|
+
if err != nil {
|
|
83
|
+
return nil, err
|
|
96
84
|
}
|
|
97
|
-
if
|
|
98
|
-
|
|
85
|
+
if err := CheckProjectVersion(filepath.Join(cwd, "package.json"), Version); err != nil {
|
|
86
|
+
return nil, err
|
|
99
87
|
}
|
|
100
|
-
|
|
101
|
-
|
|
88
|
+
login, err := vauthlogin.New()
|
|
89
|
+
if err != nil {
|
|
90
|
+
return nil, err
|
|
102
91
|
}
|
|
103
|
-
v
|
|
104
|
-
|
|
105
|
-
|
|
92
|
+
v := &Vauth{Vauth: login}
|
|
93
|
+
config, err := configFromRC(login.RC(), cwd, login.Environ)
|
|
94
|
+
if err != nil {
|
|
95
|
+
return nil, err
|
|
106
96
|
}
|
|
107
|
-
v.
|
|
108
|
-
if v.
|
|
97
|
+
v.Config = *config
|
|
98
|
+
if v.VAULT_ADDR() == "" {
|
|
109
99
|
fmt.Fprintln(v.stderr(), "Not bound to any vault")
|
|
110
100
|
}
|
|
111
101
|
return v, nil
|
|
@@ -132,140 +122,6 @@ func (v *Vauth) stdin() io.Reader {
|
|
|
132
122
|
return os.Stdin
|
|
133
123
|
}
|
|
134
124
|
|
|
135
|
-
func (v *Vauth) now() time.Time {
|
|
136
|
-
if v.Now != nil {
|
|
137
|
-
return v.Now()
|
|
138
|
-
}
|
|
139
|
-
return time.Now()
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
func (v *Vauth) debug(format string, args ...any) {
|
|
143
|
-
if strings.Contains(v.Environ["DEBUG"], "vvauth") {
|
|
144
|
-
fmt.Fprintf(v.stderr(), "vvauth "+format+"\n", args...)
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
func (v *Vauth) tokenStatus(operation string, token *token) {
|
|
149
|
-
validity := "valid indefinitely"
|
|
150
|
-
if token.TTL != 0 {
|
|
151
|
-
validity = HumanDiff(token.TTL, 2)
|
|
152
|
-
}
|
|
153
|
-
fmt.Fprintf(v.stderr(), "Vauth token: %s · %s · %s\n", operation, v.VaultAddr, validity)
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// Connect establishes VaultToken: the environment token, then the cached one
|
|
157
|
-
// (renewed in the last third of its lease), then a new login. With renew, a
|
|
158
|
-
// new login is forced. Without an authentication method, no token is used.
|
|
159
|
-
func (v *Vauth) Connect(renew bool) (string, error) {
|
|
160
|
-
if v.RC.SSHAuth == nil && v.RC.JWTAuth == nil {
|
|
161
|
-
v.VaultToken = ""
|
|
162
|
-
return "", nil
|
|
163
|
-
}
|
|
164
|
-
if v.connected && !renew {
|
|
165
|
-
return v.VaultToken, nil
|
|
166
|
-
}
|
|
167
|
-
v.connected = false
|
|
168
|
-
|
|
169
|
-
cached := v.readTokenCache()
|
|
170
|
-
current := v.VaultToken
|
|
171
|
-
if current == "" && cached != nil {
|
|
172
|
-
current = cached.Token
|
|
173
|
-
}
|
|
174
|
-
hadToken := current != ""
|
|
175
|
-
operation := "reused"
|
|
176
|
-
now := v.now().UnixMilli()
|
|
177
|
-
if renew || (cached != nil && current == cached.Token && cached.ExpiresAt != nil && *cached.ExpiresAt <= now) {
|
|
178
|
-
current = ""
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
var established *token
|
|
182
|
-
if current != "" {
|
|
183
|
-
established = &token{Value: current}
|
|
184
|
-
if cached != nil && cached.Token == current {
|
|
185
|
-
established.LeaseDuration, established.Renewable = cached.LeaseDuration, cached.Renewable
|
|
186
|
-
if cached.ExpiresAt != nil {
|
|
187
|
-
established.TTL = float64(*cached.ExpiresAt-now) / 1000
|
|
188
|
-
}
|
|
189
|
-
} else {
|
|
190
|
-
info, lease, err := v.lookupToken(current)
|
|
191
|
-
if err := reauthenticable(err); err != nil {
|
|
192
|
-
return "", err
|
|
193
|
-
}
|
|
194
|
-
if err != nil {
|
|
195
|
-
established = nil
|
|
196
|
-
} else {
|
|
197
|
-
established.TTL, established.Renewable, established.LeaseDuration = info.TTL, info.Renewable, lease
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
if established != nil && established.TTL > 0 && established.TTL <= float64(established.LeaseDuration)/3 {
|
|
201
|
-
if established.Renewable {
|
|
202
|
-
renewed, err := v.renewToken(current)
|
|
203
|
-
if err := reauthenticable(err); err != nil {
|
|
204
|
-
return "", err
|
|
205
|
-
}
|
|
206
|
-
if err != nil {
|
|
207
|
-
established = nil
|
|
208
|
-
} else {
|
|
209
|
-
established = fromAuth(renewed)
|
|
210
|
-
operation = "renewed"
|
|
211
|
-
}
|
|
212
|
-
} else {
|
|
213
|
-
established = nil
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
if established == nil {
|
|
219
|
-
var granted *auth
|
|
220
|
-
var err error
|
|
221
|
-
if v.RC.SSHAuth != nil && v.Agent != "" {
|
|
222
|
-
granted, err = v.loginVaultSSH(v.RC.SSHAuth)
|
|
223
|
-
} else if v.RC.JWTAuth != nil && v.RC.JWTAuth.JWT != "" {
|
|
224
|
-
path := v.RC.JWTAuth.Path
|
|
225
|
-
if path == "" {
|
|
226
|
-
path = "jwt"
|
|
227
|
-
}
|
|
228
|
-
granted, err = v.loginVault(path, map[string]string{"jwt": v.RC.JWTAuth.JWT, "role": v.RC.JWTAuth.Role})
|
|
229
|
-
}
|
|
230
|
-
if err != nil {
|
|
231
|
-
return "", err
|
|
232
|
-
}
|
|
233
|
-
if granted == nil {
|
|
234
|
-
if renew || hadToken {
|
|
235
|
-
return "", errors.New("Could not login to vault: no authentication method available")
|
|
236
|
-
}
|
|
237
|
-
} else {
|
|
238
|
-
established = fromAuth(granted)
|
|
239
|
-
}
|
|
240
|
-
operation = "generated"
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
v.VaultToken = ""
|
|
244
|
-
if established != nil {
|
|
245
|
-
if operation != "reused" || cached == nil || cached.Token != established.Value {
|
|
246
|
-
v.saveTokenCache(established)
|
|
247
|
-
}
|
|
248
|
-
v.tokenStatus(operation, established)
|
|
249
|
-
v.VaultToken = established.Value
|
|
250
|
-
}
|
|
251
|
-
v.connected = true
|
|
252
|
-
return v.VaultToken, nil
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// reauthenticable returns err unless it is an invalid token (400/403), in
|
|
256
|
-
// which case a new login is appropriate.
|
|
257
|
-
func reauthenticable(err error) error {
|
|
258
|
-
var failure *HTTPError
|
|
259
|
-
if err == nil || (errors.As(err, &failure) && (failure.Status == 400 || failure.Status == 403)) {
|
|
260
|
-
return nil
|
|
261
|
-
}
|
|
262
|
-
return err
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
func fromAuth(granted *auth) *token {
|
|
266
|
-
return &token{Value: granted.ClientToken, TTL: float64(granted.LeaseDuration), LeaseDuration: granted.LeaseDuration, Renewable: granted.Renewable}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
125
|
// Profile is the Vault identity of the token: entity metadata and the
|
|
270
126
|
// private .vauth_database of VAUTH_USER_LOGIN.
|
|
271
127
|
type Profile struct {
|
|
@@ -293,22 +149,30 @@ func (v *Vauth) GetProfile(renew bool) (*Profile, error) {
|
|
|
293
149
|
return nil, err
|
|
294
150
|
}
|
|
295
151
|
profile := &Profile{Metadata: map[string]any{}, Database: map[string]any{}}
|
|
296
|
-
if v.
|
|
152
|
+
if v.VAULT_TOKEN() == "" || v.VAULT_ADDR() == "" {
|
|
297
153
|
return profile, nil
|
|
298
154
|
}
|
|
299
|
-
info,
|
|
155
|
+
info, err := v.LookupSelf(v.VAULT_TOKEN())
|
|
300
156
|
if err != nil {
|
|
301
157
|
return nil, err
|
|
302
158
|
}
|
|
303
|
-
|
|
159
|
+
_, response, err := v.Request("GET", "identity/entity/id/"+info.EntityID, v.VAULT_TOKEN(), nil, 200)
|
|
304
160
|
if err != nil {
|
|
305
161
|
return nil, err
|
|
306
162
|
}
|
|
163
|
+
var identity struct {
|
|
164
|
+
Data struct {
|
|
165
|
+
Metadata map[string]any `json:"metadata"`
|
|
166
|
+
} `json:"data"`
|
|
167
|
+
}
|
|
168
|
+
if err := json.Unmarshal(response, &identity); err != nil {
|
|
169
|
+
return nil, fmt.Errorf("Invalid identity response: %w", err)
|
|
170
|
+
}
|
|
307
171
|
profile.EntityID = info.EntityID
|
|
308
|
-
if
|
|
309
|
-
profile.Metadata =
|
|
172
|
+
if identity.Data.Metadata != nil {
|
|
173
|
+
profile.Metadata = identity.Data.Metadata
|
|
310
174
|
}
|
|
311
|
-
if login, _ := stringify(
|
|
175
|
+
if login, _ := stringify(profile.Metadata["VAUTH_USER_LOGIN"]); login != "" {
|
|
312
176
|
database, err := v.vaultRead("private/"+login, ".vauth_database", true)
|
|
313
177
|
if err != nil {
|
|
314
178
|
return nil, err
|
|
@@ -359,34 +223,44 @@ func (v *Vauth) Show() (map[string]any, error) {
|
|
|
359
223
|
// Env resolves the environment declared by .vauthrc: VAULT_TOKEN and
|
|
360
224
|
// VAULT_ADDR, then env.map with $${env|profile|secrets.X} expanded.
|
|
361
225
|
func (v *Vauth) Env(renew bool) (*OrderedMap, error) {
|
|
362
|
-
|
|
363
|
-
if
|
|
364
|
-
|
|
226
|
+
config := v.Config.Env
|
|
227
|
+
if config == nil {
|
|
228
|
+
config = &EnvConfig{}
|
|
229
|
+
}
|
|
230
|
+
profile := &Profile{Metadata: map[string]any{}, Database: map[string]any{}}
|
|
231
|
+
rc := v.RC()
|
|
232
|
+
localCredentials := v.Config.AgentCrypt != nil && rc["ssh_auth"] == nil && rc["jwt_auth"] == nil
|
|
233
|
+
if localCredentials {
|
|
234
|
+
if _, err := v.Connect(renew); err != nil {
|
|
235
|
+
return nil, err
|
|
236
|
+
}
|
|
237
|
+
} else {
|
|
238
|
+
var err error
|
|
239
|
+
profile, err = v.GetProfile(renew)
|
|
240
|
+
if err != nil {
|
|
241
|
+
return nil, err
|
|
242
|
+
}
|
|
365
243
|
}
|
|
366
244
|
env := &OrderedMap{}
|
|
367
|
-
if v.
|
|
368
|
-
env.Set("VAULT_TOKEN", v.
|
|
245
|
+
if v.VAULT_TOKEN() != "" {
|
|
246
|
+
env.Set("VAULT_TOKEN", v.VAULT_TOKEN())
|
|
369
247
|
}
|
|
370
|
-
if v.
|
|
371
|
-
env.Set("VAULT_ADDR", v.
|
|
248
|
+
if v.VAULT_ADDR() != "" {
|
|
249
|
+
env.Set("VAULT_ADDR", v.VAULT_ADDR())
|
|
372
250
|
}
|
|
373
251
|
|
|
374
|
-
config := v.RC.Env
|
|
375
|
-
if config == nil {
|
|
376
|
-
config = &EnvConfig{}
|
|
377
|
-
}
|
|
378
252
|
secrets := map[string]any{}
|
|
379
|
-
if v.
|
|
380
|
-
armor, err := os.ReadFile(v.
|
|
253
|
+
if v.Config.AgentCrypt != nil {
|
|
254
|
+
armor, err := os.ReadFile(v.Config.AgentCrypt.Path)
|
|
381
255
|
if err != nil {
|
|
382
256
|
return nil, err
|
|
383
257
|
}
|
|
384
|
-
body, err := sshagentcrypt.Decrypt(armor, v.
|
|
258
|
+
body, err := sshagentcrypt.Decrypt(armor, v.Config.AgentCrypt.Identity, &sshagentcrypt.Options{AgentSocket: v.Agent})
|
|
385
259
|
if err != nil {
|
|
386
260
|
return nil, err
|
|
387
261
|
}
|
|
388
262
|
if err := json.Unmarshal(body, &secrets); err != nil {
|
|
389
|
-
return nil, fmt.Errorf("Invalid ssh-agent-crypt payload %s: %w", v.
|
|
263
|
+
return nil, fmt.Errorf("Invalid ssh-agent-crypt payload %s: %w", v.Config.AgentCrypt.Path, err)
|
|
390
264
|
}
|
|
391
265
|
}
|
|
392
266
|
|
|
@@ -418,11 +292,18 @@ func (v *Vauth) Env(renew bool) (*OrderedMap, error) {
|
|
|
418
292
|
}
|
|
419
293
|
dict := Lookup{"env": v.Environ, "profile": merged, "secrets": secrets}
|
|
420
294
|
for _, key := range mapping.Keys {
|
|
295
|
+
if !envName.MatchString(key) {
|
|
296
|
+
return nil, fmt.Errorf("Invalid environment variable name '%s' in env.map", key)
|
|
297
|
+
}
|
|
421
298
|
env.Set(key, ReplaceEnv(mapping.Values[key], dict))
|
|
422
299
|
}
|
|
423
300
|
return env, nil
|
|
424
301
|
}
|
|
425
302
|
|
|
303
|
+
// envName is the only shape of key that reaches `export KEY=...` unescaped;
|
|
304
|
+
// .vauthrc may come from an untrusted working directory.
|
|
305
|
+
var envName = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`)
|
|
306
|
+
|
|
426
307
|
// Login connects, and with source publishes VAULT_TOKEN as shell exports.
|
|
427
308
|
func (v *Vauth) Login(source, renew bool) error {
|
|
428
309
|
if _, err := v.Connect(renew); err != nil {
|
|
@@ -430,7 +311,7 @@ func (v *Vauth) Login(source, renew bool) error {
|
|
|
430
311
|
}
|
|
431
312
|
if source {
|
|
432
313
|
env := &OrderedMap{}
|
|
433
|
-
env.Set("VAULT_TOKEN", v.
|
|
314
|
+
env.Set("VAULT_TOKEN", v.VAULT_TOKEN())
|
|
434
315
|
v.PublishEnv(env)
|
|
435
316
|
}
|
|
436
317
|
return nil
|