forked from pachyderm/pachyderm
/
renew.go
92 lines (79 loc) · 2.99 KB
/
renew.go
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
package pachyderm
import (
"context"
"errors"
"fmt"
"time"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
pclient "github.com/pachyderm/pachyderm/src/client"
"github.com/pachyderm/pachyderm/src/client/auth"
)
// Renew renews the caller's credentials (and extends the TTL of their Pachyderm
// token by sending a request to Pachyderm). Unlike other handlers, it doesn't
// get assigned to a path; instead it's called by the vault lease API when a
// token's lease is renewed. It's set in Backend.Secrets[0].Revoke in backend.go
func (b *backend) Renew(ctx context.Context, req *logical.Request, d *framework.FieldData) (resp *logical.Response, retErr error) {
// renew seems to be handled specially, and req.ID doesn't seem to be set
b.Logger().Debug(fmt.Sprintf("%s received at %s", req.Operation, req.Path))
defer func() {
b.Logger().Debug(fmt.Sprintf("%s finished at %s (success=%t)", req.Operation, req.Path, retErr == nil && !resp.IsError()))
}()
// Extract pachyderm token from vault secret
tokenIface, ok := req.Secret.InternalData["user_token"]
if !ok {
return nil, fmt.Errorf("secret is missing user_token")
}
userToken, ok := tokenIface.(string)
if !ok {
return nil, fmt.Errorf("secret.user_token has wrong type (expected string but was %T)", tokenIface)
}
// Get pach address and admin token from config
config, err := getConfig(ctx, req.Storage)
if err != nil {
return nil, err
}
if len(config.AdminToken) == 0 {
return nil, errors.New("plugin is missing admin token")
}
if len(config.PachdAddress) == 0 {
return nil, errors.New("plugin is missing pachd_address")
}
// Extract TTL from request first, and then config (if unset)
ttl, maxTTL := req.Secret.LeaseOptions.Increment, b.System().MaxLeaseTTL()
if ttl == 0 {
ttl, maxTTL, err = b.SanitizeTTLStr(config.TTL, maxTTL.String())
if err != nil {
return nil, fmt.Errorf("%v: could not sanitize config TTL", err)
}
}
// Renew creds in Pachyderm
err = renewUserCredentials(ctx, config.PachdAddress, config.AdminToken, userToken, ttl)
if err != nil {
return nil, err
}
// Renew vault lease
return framework.LeaseExtend(ttl, maxTTL, b.System())(ctx, req, d)
}
// renewUserCredentials extends the TTL of the Pachyderm authentication token
// 'userToken', using the vault plugin's Admin credentials. 'userToken' belongs
// to the user who is calling vault, and would like to extend their Pachyderm
// session.
func renewUserCredentials(ctx context.Context, pachdAddress string, adminToken string, userToken string, ttl time.Duration) error {
// Setup a single use client w the given admin token / address
client, err := pclient.NewFromAddress(pachdAddress)
if err != nil {
return err
}
defer client.Close() // avoid leaking connections
client = client.WithCtx(ctx)
client.SetAuthToken(adminToken)
_, err = client.AuthAPIClient.ExtendAuthToken(client.Ctx(), &auth.ExtendAuthTokenRequest{
Token: userToken,
TTL: int64(ttl.Seconds()),
})
if err != nil {
return err
}
return nil
}