-
Notifications
You must be signed in to change notification settings - Fork 311
/
client.go
155 lines (119 loc) · 4.02 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package controlplane
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"sync"
"github.com/rudderlabs/rudder-server/utils/httputil"
)
var ErrKeyNotFound = errors.New("request key not found")
type PublicPrivateKeyPair struct {
PublicKey string
PrivateKey string
}
type BasicAuth struct {
Username string
Password string
}
type InternalControlPlane interface {
GetDestinationSSHKeys(ctx context.Context, id string) (*PublicPrivateKeyPair, error)
GetSSHKeys(ctx context.Context, id string) (*PublicPrivateKeyPair, error)
}
type internalClientWithCache struct {
client InternalControlPlane
cache sync.Map
}
func NewInternalClient(baseURI string, auth BasicAuth) InternalControlPlane {
return &internalClient{
baseURI: baseURI,
auth: auth,
httpClient: &http.Client{},
}
}
type internalClient struct {
baseURI string
auth BasicAuth
httpClient *http.Client
}
func (api *internalClient) GetSSHKeys(ctx context.Context, id string) (*PublicPrivateKeyPair, error) {
url := fmt.Sprintf("%s/dataplane/admin/sshKeys/%s", api.baseURI, id)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
return nil, fmt.Errorf("creating new request with ctx: %w", err)
}
req.SetBasicAuth(api.auth.Username, api.auth.Password)
req.Header.Set("Content-Type", "application/json")
resp, err := api.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("fetching response from http client: %w", err)
}
defer func() { httputil.CloseResponse(resp) }()
if resp.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("%w: key requested: %s", ErrKeyNotFound, id)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid status code: %d", resp.StatusCode)
}
decoder := json.NewDecoder(resp.Body)
var keypair PublicPrivateKeyPair
if err := decoder.Decode(&keypair); err != nil {
return nil, fmt.Errorf("decoding upstream response body: %w", err)
}
return &keypair, nil
}
func (api *internalClient) GetDestinationSSHKeys(ctx context.Context, id string) (*PublicPrivateKeyPair, error) {
url := fmt.Sprintf("%s/dataplane/admin/destinations/%s/sshKeys", api.baseURI, id)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
return nil, fmt.Errorf("creating new request with ctx: %w", err)
}
req.SetBasicAuth(api.auth.Username, api.auth.Password)
req.Header.Set("Content-Type", "application/json")
resp, err := api.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("fetching response from http client: %w", err)
}
defer func() { httputil.CloseResponse(resp) }()
if resp.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("%w: key requested: %s", ErrKeyNotFound, id)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid status code: %d", resp.StatusCode)
}
decoder := json.NewDecoder(resp.Body)
var keypair PublicPrivateKeyPair
if err := decoder.Decode(&keypair); err != nil {
return nil, fmt.Errorf("decoding upstream response body: %w", err)
}
return &keypair, nil
}
func NewInternalClientWithCache(baseURI string, auth BasicAuth) InternalControlPlane {
return &internalClientWithCache{
client: NewInternalClient(baseURI, auth),
cache: sync.Map{},
}
}
func (cc *internalClientWithCache) GetSSHKeys(ctx context.Context, id string) (*PublicPrivateKeyPair, error) {
if val, ok := cc.cache.Load(id); ok {
return val.(*PublicPrivateKeyPair), nil
}
keypair, err := cc.client.GetSSHKeys(ctx, id)
if err != nil {
return nil, fmt.Errorf("fetching and caching destination ssh keys: %w", err)
}
cc.cache.Store(id, keypair)
return keypair, nil
}
func (cc *internalClientWithCache) GetDestinationSSHKeys(ctx context.Context, id string) (*PublicPrivateKeyPair, error) {
if val, ok := cc.cache.Load(id); ok {
return val.(*PublicPrivateKeyPair), nil
}
keypair, err := cc.client.GetDestinationSSHKeys(ctx, id)
if err != nil {
return nil, fmt.Errorf("fetching and caching destination ssh keys: %w", err)
}
cc.cache.Store(id, keypair)
return keypair, nil
}