@@ -12,11 +12,17 @@ import (
12
12
13
13
doryMemory "github.com/abhishekkr/dory/doryMemory"
14
14
15
+ "github.com/abhishekkr/gol/golenv"
16
+ "github.com/abhishekkr/gol/golerror"
15
17
"github.com/abhishekkr/gol/gollog"
16
18
17
19
"github.com/gin-gonic/gin"
18
20
)
19
21
22
+ var (
23
+ DORY_ADMIN_TOKEN = golenv .OverrideIfEnv ("DORY_ADMIN_TOKEN" , "" )
24
+ )
25
+
20
26
/*
21
27
LocalAuth is a struct to maintain connection details for a Local-Auth and single item construct for actions.
22
28
*/
@@ -38,22 +44,48 @@ func NewLocalAuth(cacheName string) LocalAuth {
38
44
return localAuth
39
45
}
40
46
41
- func (localAuth LocalAuth ) ctxDatastore (ctx * gin.Context ) (datastore doryMemory.DataStore ) {
47
+ func (localAuth LocalAuth ) ctxPersist (ctx * gin.Context ) (datastore doryMemory.DataStore ) {
42
48
if ctx .DefaultQuery ("persist" , "false" ) == "false" {
43
- gollog .Debug (fmt .Sprintf ("SET - key '%s' is provided for memory store with expiry" , localAuth .Item .Name ))
49
+ gollog .Debug (fmt .Sprintf ("key '%s' is provided for memory store with expiry" , localAuth .Item .Name ))
44
50
datastore = localAuth .Cache
45
51
} else {
46
- gollog .Debug (fmt .Sprintf ("SET - key '%s' is provided for long-term disk store" , localAuth .Item .Name ))
52
+ gollog .Debug (fmt .Sprintf ("key '%s' is provided for long-term disk store" , localAuth .Item .Name ))
47
53
datastore = localAuth .Disk
48
54
}
49
55
return
50
56
}
51
57
58
+ func (localAuth LocalAuth ) ctxDatastore (ctx * gin.Context ) (datastore doryMemory.DataStore , err error ) {
59
+ datastoreType := ctx .Param ("datastore" )
60
+ if datastoreType == "cache" {
61
+ datastore = localAuth .Cache
62
+ } else if datastoreType == "disk" {
63
+ datastore = localAuth .Disk
64
+ } else {
65
+ err = golerror .Error (123 , fmt .Sprintf ("store %s is not allowed, only 'cache' and 'disk' are allowed" ))
66
+ }
67
+ return
68
+ }
69
+
70
+ func (localAuth LocalAuth ) ctxAdminToken (ctx * gin.Context ) (err error ) {
71
+ adminToken := ctx .Request .Header .Get ("X-DORY-ADMIN-TOKEN" )
72
+
73
+ if len (DORY_ADMIN_TOKEN ) < 256 {
74
+ err = golerror .Error (123 , "configured admin token length is less than 64 chars, not allowed" )
75
+ return
76
+ }
77
+ if DORY_ADMIN_TOKEN != adminToken {
78
+ err = golerror .Error (123 , "provided admin token doesn't match configured token" )
79
+ return
80
+ }
81
+ return
82
+ }
83
+
52
84
/*
53
85
Get fetchs required auth mapped secret from Local-Auth backend.
54
86
*/
55
87
func (localAuth LocalAuth ) Get (ctx * gin.Context ) {
56
- datastore := localAuth .ctxDatastore (ctx )
88
+ datastore := localAuth .ctxPersist (ctx )
57
89
58
90
localAuthItem := localAuth .Item
59
91
@@ -85,7 +117,7 @@ func (localAuth LocalAuth) Get(ctx *gin.Context) {
85
117
AuthMount stores a secret mapped with a new auth-path only at Local-Auth with unique auth-token.
86
118
*/
87
119
func (localAuth LocalAuth ) AuthMount (ctx * gin.Context ) {
88
- datastore := localAuth .ctxDatastore (ctx )
120
+ datastore := localAuth .ctxPersist (ctx )
89
121
90
122
localAuthItem := localAuth .Item
91
123
localAuthItem .Name = ctx .Param ("uuid" )
@@ -128,7 +160,7 @@ func (localAuth LocalAuth) AuthMount(ctx *gin.Context) {
128
160
AuthUnmount purges a previously local-auth stored mapped to a auth-path if not yet purged by TTL.
129
161
*/
130
162
func (localAuth LocalAuth ) AuthUnmount (ctx * gin.Context ) {
131
- datastore := localAuth .ctxDatastore (ctx )
163
+ datastore := localAuth .ctxPersist (ctx )
132
164
133
165
ctx .Writer .Header ().Add ("Content-Type" , "application/json" )
134
166
@@ -143,3 +175,58 @@ func (localAuth LocalAuth) AuthUnmount(ctx *gin.Context) {
143
175
144
176
ctx .JSON (200 , ExitResponse {Msg : "success" })
145
177
}
178
+
179
+ /*
180
+ List shows all keys registered with Dory for datatsore enquired.
181
+ */
182
+ func (localAuth LocalAuth ) List (ctx * gin.Context ) {
183
+ var err error
184
+ ctx .Writer .Header ().Add ("Content-Type" , "application/json" )
185
+
186
+ datastore , err := localAuth .ctxDatastore (ctx )
187
+ if err != nil {
188
+ ctx .JSON (500 , ExitResponse {Msg : err .Error ()})
189
+ return
190
+ }
191
+
192
+ err = localAuth .ctxAdminToken (ctx )
193
+ if err != nil {
194
+ ctx .JSON (500 , ExitResponse {Msg : err .Error ()})
195
+ return
196
+ }
197
+
198
+ ctx .JSON (200 , datastore .List ())
199
+ }
200
+
201
+ /*
202
+ Purge removes all keys from datastore enquired, without decryption required.
203
+ */
204
+ func (localAuth LocalAuth ) Purge (ctx * gin.Context ) {
205
+ ctx .Writer .Header ().Add ("Content-Type" , "application/json" )
206
+
207
+ datastore , err := localAuth .ctxDatastore (ctx )
208
+ if err != nil {
209
+ ctx .JSON (500 , ExitResponse {Msg : err .Error ()})
210
+ return
211
+ }
212
+
213
+ err = localAuth .ctxAdminToken (ctx )
214
+ if err != nil {
215
+ ctx .JSON (500 , ExitResponse {Msg : err .Error ()})
216
+ return
217
+ }
218
+
219
+ ctx .JSON (200 , datastore .Purge ())
220
+ }
221
+
222
+ /*
223
+ doryPing to return status for Dory
224
+ */
225
+ func (localAuth LocalAuth ) DoryPing (ctx * gin.Context ) {
226
+ ping := map [string ]string {
227
+ "keys-in-cache" : fmt .Sprintf ("%d" , localAuth .Cache .Count ()),
228
+ "keys-in-disk" : fmt .Sprintf ("%d" , localAuth .Disk .Count ()),
229
+ }
230
+
231
+ ctx .JSON (200 , ping )
232
+ }
0 commit comments