Skip to content

Commit

Permalink
dory api for List and Purge with DORY_ADMIN_TOKEN (>255 char), /ping
Browse files Browse the repository at this point in the history
Signed-off-by: AbhishekKr <abhikumar163@gmail.com>
  • Loading branch information
abhishekkr committed Dec 22, 2017
1 parent 840aa6a commit 28a2f36
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 21 deletions.
19 changes: 6 additions & 13 deletions dory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"net/http"

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

Expand All @@ -24,17 +23,6 @@ func main() {
fmt.Println("bye .")
}

/*
doryHelp to serve help file for Dory.
*/
func doryHelp(ctx *gin.Context) {
ctx.HTML(
http.StatusOK,
"help.html",
gin.H{"title": "Help"},
)
}

/*
ginCors to set required HTTP configs.
*/
Expand Down Expand Up @@ -73,11 +61,16 @@ func GinUp(listenAt string) {
router.Static("/images", "w3assets/images")
router.StaticFile("/favicon.ico", "w3assets/favicon.ico")

router.GET("/help", doryHelp)
router.GET("/help", doryBackend.DoryHelp)

router.GET("/ping", localAuth.DoryPing)

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

router.GET("/admin/store/:datastore", localAuth.List)
router.DELETE("/admin/store/:datastore", localAuth.Purge)

router.Run(listenAt)
}
14 changes: 13 additions & 1 deletion doryBackend/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package doryBackend

import (
"encoding/json"
"net/http"

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

response := ExitResponse{Msg: "WIP"}
ctx.JSON(200, response)
}

/*
doryHelp to serve help file for Dory.
*/
func DoryHelp(ctx *gin.Context) {
ctx.HTML(
http.StatusOK,
"help.html",
gin.H{"title": "Help"},
)
}
99 changes: 93 additions & 6 deletions doryBackend/localAuth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ import (

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

"github.com/abhishekkr/gol/golenv"
"github.com/abhishekkr/gol/golerror"
"github.com/abhishekkr/gol/gollog"

"github.com/gin-gonic/gin"
)

var (
DORY_ADMIN_TOKEN = golenv.OverrideIfEnv("DORY_ADMIN_TOKEN", "")
)

/*
LocalAuth is a struct to maintain connection details for a Local-Auth and single item construct for actions.
*/
Expand All @@ -38,22 +44,48 @@ func NewLocalAuth(cacheName string) LocalAuth {
return localAuth
}

func (localAuth LocalAuth) ctxDatastore(ctx *gin.Context) (datastore doryMemory.DataStore) {
func (localAuth LocalAuth) ctxPersist(ctx *gin.Context) (datastore doryMemory.DataStore) {
if ctx.DefaultQuery("persist", "false") == "false" {
gollog.Debug(fmt.Sprintf("SET - key '%s' is provided for memory store with expiry", localAuth.Item.Name))
gollog.Debug(fmt.Sprintf("key '%s' is provided for memory store with expiry", localAuth.Item.Name))
datastore = localAuth.Cache
} else {
gollog.Debug(fmt.Sprintf("SET - key '%s' is provided for long-term disk store", localAuth.Item.Name))
gollog.Debug(fmt.Sprintf("key '%s' is provided for long-term disk store", localAuth.Item.Name))
datastore = localAuth.Disk
}
return
}

func (localAuth LocalAuth) ctxDatastore(ctx *gin.Context) (datastore doryMemory.DataStore, err error) {
datastoreType := ctx.Param("datastore")
if datastoreType == "cache" {
datastore = localAuth.Cache
} else if datastoreType == "disk" {
datastore = localAuth.Disk
} else {
err = golerror.Error(123, fmt.Sprintf("store %s is not allowed, only 'cache' and 'disk' are allowed"))
}
return
}

func (localAuth LocalAuth) ctxAdminToken(ctx *gin.Context) (err error) {
adminToken := ctx.Request.Header.Get("X-DORY-ADMIN-TOKEN")

if len(DORY_ADMIN_TOKEN) < 256 {
err = golerror.Error(123, "configured admin token length is less than 64 chars, not allowed")
return
}
if DORY_ADMIN_TOKEN != adminToken {
err = golerror.Error(123, "provided admin token doesn't match configured token")
return
}
return
}

/*
Get fetchs required auth mapped secret from Local-Auth backend.
*/
func (localAuth LocalAuth) Get(ctx *gin.Context) {
datastore := localAuth.ctxDatastore(ctx)
datastore := localAuth.ctxPersist(ctx)

localAuthItem := localAuth.Item

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

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

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

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

ctx.JSON(200, ExitResponse{Msg: "success"})
}

/*
List shows all keys registered with Dory for datatsore enquired.
*/
func (localAuth LocalAuth) List(ctx *gin.Context) {
var err error
ctx.Writer.Header().Add("Content-Type", "application/json")

datastore, err := localAuth.ctxDatastore(ctx)
if err != nil {
ctx.JSON(500, ExitResponse{Msg: err.Error()})
return
}

err = localAuth.ctxAdminToken(ctx)
if err != nil {
ctx.JSON(500, ExitResponse{Msg: err.Error()})
return
}

ctx.JSON(200, datastore.List())
}

/*
Purge removes all keys from datastore enquired, without decryption required.
*/
func (localAuth LocalAuth) Purge(ctx *gin.Context) {
ctx.Writer.Header().Add("Content-Type", "application/json")

datastore, err := localAuth.ctxDatastore(ctx)
if err != nil {
ctx.JSON(500, ExitResponse{Msg: err.Error()})
return
}

err = localAuth.ctxAdminToken(ctx)
if err != nil {
ctx.JSON(500, ExitResponse{Msg: err.Error()})
return
}

ctx.JSON(200, datastore.Purge())
}

/*
doryPing to return status for Dory
*/
func (localAuth LocalAuth) DoryPing(ctx *gin.Context) {
ping := map[string]string{
"keys-in-cache": fmt.Sprintf("%d", localAuth.Cache.Count()),
"keys-in-disk": fmt.Sprintf("%d", localAuth.Disk.Count()),
}

ctx.JSON(200, ping)
}
2 changes: 1 addition & 1 deletion doryMemory/cachetable.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (cache *Cache2Go) List() []string {
keyIndex := 0
keyList := make([]string, cache.CacheTable.Count())
cache.CacheTable.Foreach(func(key interface{}, item *cache2go.CacheItem) {
keyList[keyIndex] = fmt.Sprintf("%q", key)
keyList[keyIndex] = fmt.Sprintf("%s", key)
keyIndex += 1
})
return keyList
Expand Down

0 comments on commit 28a2f36

Please sign in to comment.