-
Notifications
You must be signed in to change notification settings - Fork 17
/
auth.clj
160 lines (122 loc) · 4.04 KB
/
auth.clj
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
156
157
158
159
160
(ns vault.sys.auth
"The `/sys/auth` endpoint is used to list, create, update, and delete auth
methods. Auth methods convert user or machine-supplied information into a
token which can be used for all future requests.
Reference: https://www.vaultproject.io/api-docs/system/auth"
(:require
[vault.client.http :as http]
[vault.client.mock :as mock]
[vault.util :as u])
(:import
vault.client.http.HTTPClient
vault.client.mock.MockClient))
;; ## API Protocol
(defprotocol API
"The health endpoint is used to check the health status of Vault."
(list-methods
[client]
"List all enabled auth methods. Returns a map of endpoints to their
configurations.")
(enable-method!
[client path params]
"Enable a new auth method at the given path under the `auth/` prefix. After
enabling, the method can be accessed and configured via the specified path.
Returns nil.
Parameters:
- `:type` (string)
Name of the authentication method type, such as \"github\" or \"token\".
- `:description` (optional, string)
Human-friendly description of the auth method.
- `:config` (optional, map)
Configuration options for this auth method.
See the Vault API docs for details.")
(disable-method!
[client path]
"Disable the auth method at the given path. Returns nil.")
(read-method-tuning
[client path]
"Read the tuning configuration for the auth method at the path. Returns a
map of config.")
(tune-method!
[client path params]
"Tune the configuration parameters for the auth method at the path. Returns
`nil`.
See the Vault API docs for available parameters."))
;; ## Mock Client
(extend-type MockClient
API
(list-methods
[client]
(mock/success-response
client
{"token/" {:accessor "auth_token_96109b84"
:config {:default-lease-ttl 0
:force-no-cache false
:max-lease-ttl 0
:token-type "default-service"}
:description "token based credentials"
:external-entropy-access false
:local false
:options nil
:seal-wrap false
:type "token"
:uuid "fcd3aea9-d682-3143-72d3-938c3f666d62"}}))
(read-method-tuning
[client path]
(if (= "token" (u/trim-path path))
(mock/success-response
client
{:default-lease-ttl 2764800,
:description "token based credentials",
:force-no-cache false,
:max-lease-ttl 2764800,
:token-type "default-service"})
(mock/error-response
client
(let [error (str "cannot fetch sysview for path \"" path \")]
(ex-info (str "Vault API errors: " error)
{:vault.client/errors [error]
:vault.client/status 400}))))))
;; ## HTTP Client
(extend-type HTTPClient
API
(list-methods
[client]
(http/call-api
client ::list-methods
:get "sys/auth"
{:handle-response
(fn handle-response
[body]
(into {}
(map (juxt key (comp u/kebabify-keys val)))
(get body "data")))}))
(enable-method!
[client path params]
(http/call-api
client ::enable-method!
:post (u/join-path "sys/auth" path)
{:info {::path path, ::type (:type params)}
:content-type :json
:body (u/snakify-keys params)}))
(disable-method!
[client path]
(http/call-api
client ::disable-method!
:delete (u/join-path "sys/auth" path)
{:info {::path path}}))
(read-method-tuning
[client path]
(http/call-api
client ::read-method-tuning
:get (u/join-path "sys/auth" path "tune")
{:info {::path path}
:handle-response u/kebabify-body-data}))
(tune-method!
[client path params]
(http/call-api
client ::tune-method!
:post (u/join-path "sys/auth" path "tune")
{:info {::path path}
:content-type :json
:body (u/snakify-keys params)})))