-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathastra_client.go
95 lines (78 loc) · 2.24 KB
/
astra_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
package datastax_astra
import (
"errors"
"io"
"net/http"
"strings"
dsAstraClient "github.com/datastax/astra-client-go/v2/astra"
)
const (
secretsPath = "/v2/clientIdSecrets"
pluginversion = "Vault-Plugin v2.0.0"
)
// astraClient creates an object storing
// the client.
type astraClient struct {
*dsAstraClient.Client
url string
token string
}
func makeHttpRequest(method, url, payload, astraToken string) (*http.Response, error) {
client := &http.Client{}
var body io.Reader
if payload != "" {
body = strings.NewReader(payload)
} else {
body = nil
}
httpReq, err := http.NewRequest(method, url, body)
if err != nil {
return nil, errors.New("error creating httpReq " + err.Error())
}
httpReq.Header.Add("Content-Type", "application/json")
httpReq.Header.Add("Authorization", "Bearer "+astraToken)
httpReq.Header.Add("User-Agent", pluginversion)
return client.Do(httpReq)
}
func (ac *astraClient) createToken(payload string) ([]byte, error) {
url := ac.url + secretsPath
res, err := makeHttpRequest(http.MethodPost, url, payload, ac.token)
if err != nil {
return nil, errors.New("error sending request " + err.Error())
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, errors.New("error reading ioutil " + err.Error())
}
return body, nil
}
func (ac *astraClient) deleteToken(clientId string) error {
url := ac.url + secretsPath + "/" + clientId
res, err := makeHttpRequest(http.MethodDelete, url, "", ac.token)
if err != nil {
return errors.New("error sending request " + err.Error())
}
defer res.Body.Close()
if res.StatusCode != http.StatusNoContent {
return errors.New("unable to delete token in astra; " + res.Status)
}
return nil
}
// newClient creates a new client to access Astra
// and exposes it for any secrets or roles to use.
func newClient(config *astraConfig) (*astraClient, error) {
if config == nil {
return nil, errors.New("client configuration was nil")
}
if config.AstraToken == "" {
return nil, errors.New("astra token was not defined")
}
if config.URL == "" {
return nil, errors.New("client URL was not defined")
}
newAstraClient := astraClient{}
newAstraClient.url = config.URL
newAstraClient.token = config.AstraToken
return &newAstraClient, nil
}