forked from gopasspw/gopass
/
client.go
129 lines (111 loc) · 2.93 KB
/
client.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
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
package client
import (
"bytes"
"context"
"fmt"
"io"
"net"
"net/http"
"net/url"
"path/filepath"
"time"
"github.com/cenkalti/backoff"
"github.com/pkg/errors"
)
// Client is a agent client
type Client struct {
http *http.Client
}
// New creates a new client
func New(dir string) *Client {
socket := filepath.Join(dir, ".gopass-agent.sock")
return &Client{
http: &http.Client{
Transport: &http.Transport{
DialContext: func(context.Context, string, string) (net.Conn, error) {
return net.Dial("unix", socket)
},
},
Timeout: 10 * time.Minute,
},
}
}
// Ping checks connectivity to the agent
func (c *Client) Ping(ctx context.Context) error {
pc := &http.Client{
Transport: c.http.Transport,
Timeout: 5 * time.Second,
}
resp, err := pc.Get("http://unix/ping")
if err != nil {
return err
}
_ = resp.Body.Close()
return nil
}
func (c *Client) waitForAgent(ctx context.Context) error {
bo := backoff.NewExponentialBackOff()
bo.MaxElapsedTime = 60 * time.Second
return backoff.Retry(func() error { return c.Ping(ctx) }, bo)
}
func (c *Client) checkAgent(ctx context.Context) error {
if err := c.Ping(ctx); err == nil {
return nil
}
if err := c.startAgent(ctx); err != nil {
return errors.Wrapf(err, "failed to start agent")
}
if err := c.waitForAgent(ctx); err != nil {
return errors.Wrapf(err, "failed to start agent (expired)")
}
return nil
}
// Remove un-caches a single key
func (c *Client) Remove(ctx context.Context, key string) error {
if err := c.checkAgent(ctx); err != nil {
return errors.Wrapf(err, "agent not available: %s", err)
}
u, err := url.Parse("http://unix/cache/remove")
if err != nil {
return errors.Wrapf(err, "failed to build request url")
}
values := u.Query()
values.Set("key", key)
u.RawQuery = values.Encode()
resp, err := c.http.Get(u.String())
if err != nil {
return errors.Wrapf(err, "failed to talk to agent")
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("request failed: %d", resp.StatusCode)
}
return nil
}
// Passphrase asks for a passphrase from the agent
func (c *Client) Passphrase(ctx context.Context, key, reason string) (string, error) {
if err := c.checkAgent(ctx); err != nil {
return "", errors.Wrapf(err, "no agent available: %s", err)
}
u, err := url.Parse("http://unix/passphrase")
if err != nil {
return "", errors.Wrapf(err, "failed to build request url")
}
values := u.Query()
values.Set("key", key)
values.Set("reason", reason)
u.RawQuery = values.Encode()
resp, err := c.http.Get(u.String())
if err != nil {
return "", errors.Wrapf(err, "failed to talk to agent")
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("request failed: %d", resp.StatusCode)
}
buf := &bytes.Buffer{}
if _, err := io.Copy(buf, resp.Body); err != nil {
return "", errors.Wrapf(err, "failed to talk to agent")
}
return buf.String(), nil
}