Skip to content

Commit 28a2f36

Browse files
committed
dory api for List and Purge with DORY_ADMIN_TOKEN (>255 char), /ping
Signed-off-by: AbhishekKr <[email protected]>
1 parent 840aa6a commit 28a2f36

File tree

4 files changed

+113
-21
lines changed

4 files changed

+113
-21
lines changed

dory.go

+6-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"fmt"
5-
"net/http"
65

76
doryBackend "github.com/abhishekkr/dory/doryBackend"
87

@@ -24,17 +23,6 @@ func main() {
2423
fmt.Println("bye .")
2524
}
2625

27-
/*
28-
doryHelp to serve help file for Dory.
29-
*/
30-
func doryHelp(ctx *gin.Context) {
31-
ctx.HTML(
32-
http.StatusOK,
33-
"help.html",
34-
gin.H{"title": "Help"},
35-
)
36-
}
37-
3826
/*
3927
ginCors to set required HTTP configs.
4028
*/
@@ -73,11 +61,16 @@ func GinUp(listenAt string) {
7361
router.Static("/images", "w3assets/images")
7462
router.StaticFile("/favicon.ico", "w3assets/favicon.ico")
7563

76-
router.GET("/help", doryHelp)
64+
router.GET("/help", doryBackend.DoryHelp)
65+
66+
router.GET("/ping", localAuth.DoryPing)
7767

7868
router.GET("/local-auth/:uuid", localAuth.Get)
7969
router.POST("/local-auth/:uuid", localAuth.AuthMount)
8070
router.DELETE("/local-auth/:uuid", localAuth.AuthUnmount)
8171

72+
router.GET("/admin/store/:datastore", localAuth.List)
73+
router.DELETE("/admin/store/:datastore", localAuth.Purge)
74+
8275
router.Run(listenAt)
8376
}

doryBackend/api.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package doryBackend
22

33
import (
44
"encoding/json"
5+
"net/http"
56

67
"github.com/gin-gonic/gin"
78
)
@@ -28,9 +29,20 @@ func (response ExitResponse) JSON() (jsonResponse []byte) {
2829
/*
2930
wip sets response handling at API Paths yet WIP.
3031
*/
31-
func wip(ctx *gin.Context) {
32+
func Wip(ctx *gin.Context) {
3233
ctx.Writer.Header().Add("Content-Type", "application/json")
3334

3435
response := ExitResponse{Msg: "WIP"}
3536
ctx.JSON(200, response)
3637
}
38+
39+
/*
40+
doryHelp to serve help file for Dory.
41+
*/
42+
func DoryHelp(ctx *gin.Context) {
43+
ctx.HTML(
44+
http.StatusOK,
45+
"help.html",
46+
gin.H{"title": "Help"},
47+
)
48+
}

doryBackend/localAuth.go

+93-6
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@ import (
1212

1313
doryMemory "github.com/abhishekkr/dory/doryMemory"
1414

15+
"github.com/abhishekkr/gol/golenv"
16+
"github.com/abhishekkr/gol/golerror"
1517
"github.com/abhishekkr/gol/gollog"
1618

1719
"github.com/gin-gonic/gin"
1820
)
1921

22+
var (
23+
DORY_ADMIN_TOKEN = golenv.OverrideIfEnv("DORY_ADMIN_TOKEN", "")
24+
)
25+
2026
/*
2127
LocalAuth is a struct to maintain connection details for a Local-Auth and single item construct for actions.
2228
*/
@@ -38,22 +44,48 @@ func NewLocalAuth(cacheName string) LocalAuth {
3844
return localAuth
3945
}
4046

41-
func (localAuth LocalAuth) ctxDatastore(ctx *gin.Context) (datastore doryMemory.DataStore) {
47+
func (localAuth LocalAuth) ctxPersist(ctx *gin.Context) (datastore doryMemory.DataStore) {
4248
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))
4450
datastore = localAuth.Cache
4551
} 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))
4753
datastore = localAuth.Disk
4854
}
4955
return
5056
}
5157

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+
5284
/*
5385
Get fetchs required auth mapped secret from Local-Auth backend.
5486
*/
5587
func (localAuth LocalAuth) Get(ctx *gin.Context) {
56-
datastore := localAuth.ctxDatastore(ctx)
88+
datastore := localAuth.ctxPersist(ctx)
5789

5890
localAuthItem := localAuth.Item
5991

@@ -85,7 +117,7 @@ func (localAuth LocalAuth) Get(ctx *gin.Context) {
85117
AuthMount stores a secret mapped with a new auth-path only at Local-Auth with unique auth-token.
86118
*/
87119
func (localAuth LocalAuth) AuthMount(ctx *gin.Context) {
88-
datastore := localAuth.ctxDatastore(ctx)
120+
datastore := localAuth.ctxPersist(ctx)
89121

90122
localAuthItem := localAuth.Item
91123
localAuthItem.Name = ctx.Param("uuid")
@@ -128,7 +160,7 @@ func (localAuth LocalAuth) AuthMount(ctx *gin.Context) {
128160
AuthUnmount purges a previously local-auth stored mapped to a auth-path if not yet purged by TTL.
129161
*/
130162
func (localAuth LocalAuth) AuthUnmount(ctx *gin.Context) {
131-
datastore := localAuth.ctxDatastore(ctx)
163+
datastore := localAuth.ctxPersist(ctx)
132164

133165
ctx.Writer.Header().Add("Content-Type", "application/json")
134166

@@ -143,3 +175,58 @@ func (localAuth LocalAuth) AuthUnmount(ctx *gin.Context) {
143175

144176
ctx.JSON(200, ExitResponse{Msg: "success"})
145177
}
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+
}

doryMemory/cachetable.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (cache *Cache2Go) List() []string {
4242
keyIndex := 0
4343
keyList := make([]string, cache.CacheTable.Count())
4444
cache.CacheTable.Foreach(func(key interface{}, item *cache2go.CacheItem) {
45-
keyList[keyIndex] = fmt.Sprintf("%q", key)
45+
keyList[keyIndex] = fmt.Sprintf("%s", key)
4646
keyIndex += 1
4747
})
4848
return keyList

0 commit comments

Comments
 (0)