Skip to content

Commit

Permalink
v1.1 added /local-cache and /local-disk for persist as false n true
Browse files Browse the repository at this point in the history
Signed-off-by: AbhishekKr <[email protected]>
  • Loading branch information
abhishekkr committed Dec 26, 2017
1 parent 02f43cf commit a91f45b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 30 deletions.
35 changes: 30 additions & 5 deletions dory.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ func ginHandleErrors(ctx *gin.Context) {
}
}

func ginUpLocalAuth(router *gin.Engine, localAuth *doryBackend.LocalAuth) {
localAuthAPI := router.Group("/local-auth")
{
localAuthAPI.GET("/:uuid", localAuth.Get)
localAuthAPI.POST("/:uuid", localAuth.AuthMount)
localAuthAPI.DELETE("/:uuid", localAuth.AuthUnmount)
}

localCacheAPI := router.Group("/local-cache")
{
localCacheAPI.GET("/:uuid", localAuth.Get)
localCacheAPI.POST("/:uuid", localAuth.AuthMount)
localCacheAPI.DELETE("/:uuid", localAuth.AuthUnmount)
}

localDiskAPI := router.Group("/local-disk")
{
localDiskAPI.GET("/:uuid", localAuth.Get)
localDiskAPI.POST("/:uuid", localAuth.AuthMount)
localDiskAPI.DELETE("/:uuid", localAuth.AuthUnmount)
}
}

/*
ginUp maps all routing logic and starts server.
*/
Expand All @@ -89,12 +112,14 @@ func ginUp(listenAt string) {

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)
ginUpLocalAuth(router, &localAuth)

router.GET("/admin/store/:datastore", localAuth.List)
router.DELETE("/admin/store/:datastore", localAuth.Purge)
adminStoreAPI := router.Group("/admin/store")
{
adminStoreAPI.GET("/:datastore", localAuth.List)
adminStoreAPI.DELETE("/:datastore", localAuth.Purge)
adminStoreAPI.DELETE("/:datastore/:uuid", localAuth.PurgeOne)
}

router.Run(listenAt)
}
Expand Down
58 changes: 33 additions & 25 deletions doryBackend/localAuth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"net/http"
"strconv"
"strings"

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

Expand Down Expand Up @@ -45,6 +46,18 @@ func NewLocalAuth(cacheName string) LocalAuth {
}

func (localAuth LocalAuth) ctxPersist(ctx *gin.Context) (datastore doryMemory.DataStore) {
requestURI := ctx.Request.RequestURI
requestAt := strings.Split(requestURI, "/")[1]
if requestAt == "local-cache" {
gollog.Debug(fmt.Sprintf("key '%s' is provided for memory store with expiry", localAuth.Item.Name))
datastore = localAuth.Cache
return
} else if requestAt == "local-disk" {
gollog.Debug(fmt.Sprintf("key '%s' is provided for long-term disk store", localAuth.Item.Name))
datastore = localAuth.Disk
return
}

if ctx.DefaultQuery("persist", "false") == "false" {
gollog.Debug(fmt.Sprintf("key '%s' is provided for memory store with expiry", localAuth.Item.Name))
datastore = localAuth.Cache
Expand Down Expand Up @@ -87,30 +100,28 @@ Get fetchs required auth mapped secret from Local-Auth backend.
func (localAuth LocalAuth) Get(ctx *gin.Context) {
datastore := localAuth.ctxPersist(ctx)

localAuthItem := localAuth.Item

localAuthItem.Name = ctx.Param("uuid")
localAuthItem.Value.Key = []byte(ctx.Request.Header.Get("X-DORY-TOKEN"))
localAuth.Item.Name = ctx.Param("uuid")
localAuth.Item.Value.Key = []byte(ctx.Request.Header.Get("X-DORY-TOKEN"))

if localAuth.Item.Name == "" {
ctx.JSON(500, ExitResponse{Msg: "passed uuid is empty"})
return
}
if !localAuthItem.Get(datastore) {
if !localAuth.Item.Get(datastore) {
ctx.Writer.Header().Add("Content-Type", "application/json")
ctx.JSON(500, ExitResponse{Msg: "get for required auth identifier failed"})
return
}

response := localAuthItem.Value.DataBlob
response := localAuth.Item.Value.DataBlob

if ctx.DefaultQuery("keep", "false") == "false" {
if !localAuthItem.Delete(datastore) {
if !localAuth.Item.Delete(datastore) {
ctx.JSON(500, ExitResponse{Msg: "auth identifier purge failed", Data: response})
return
}
} else {
gollog.Debug(fmt.Sprintf("GET - key '%s' is queried to be not purged", localAuthItem.Name))
gollog.Debug(fmt.Sprintf("GET - key '%s' is queried to be not purged", localAuth.Item.Name))
}

ctx.Writer.WriteHeader(http.StatusOK)
Expand All @@ -123,14 +134,13 @@ AuthMount stores a secret mapped with a new auth-path only at Local-Auth with un
func (localAuth LocalAuth) AuthMount(ctx *gin.Context) {
datastore := localAuth.ctxPersist(ctx)

localAuthItem := localAuth.Item
localAuthItem.Name = ctx.Param("uuid")
localAuth.Item.Name = ctx.Param("uuid")

if localAuth.Item.Name == "" {
ctx.JSON(500, ExitResponse{Msg: "passed uuid is empty"})
return
}
if localAuthItem.Exists(datastore) {
if localAuth.Item.Exists(datastore) {
ctx.JSON(409, ExitResponse{Msg: "auth identifier conflict"})
return
}
Expand All @@ -141,27 +151,27 @@ func (localAuth LocalAuth) AuthMount(ctx *gin.Context) {
ctx.JSON(400, ExitResponse{Msg: err.Error()})
return
}
localAuthItem.TTLSecond = uint64(ttlsecond)
localAuth.Item.TTLSecond = uint64(ttlsecond)

localAuthItem.Value.DataBlob, err = ioutil.ReadAll(ctx.Request.Body)
localAuth.Item.Value.DataBlob, err = ioutil.ReadAll(ctx.Request.Body)
if err != nil {
gollog.Err(fmt.Sprintf("SET - key '%s' had failure to read it's data", localAuthItem.Name))
gollog.Err(fmt.Sprintf("SET - key '%s' had failure to read it's data", localAuth.Item.Name))
ctx.Writer.Header().Add("Content-Type", "application/json")
ctx.JSON(400, ExitResponse{Msg: err.Error()})
return
}
if len(localAuthItem.Value.DataBlob) == 0 {
gollog.Err(fmt.Sprintf("SET - key '%s' is provided with empty data", localAuthItem.Name))
if len(localAuth.Item.Value.DataBlob) == 0 {
gollog.Err(fmt.Sprintf("SET - key '%s' is provided with empty data", localAuth.Item.Name))
ctx.JSON(400, ExitResponse{Msg: "empty data blob recieved"})
return
}

if !localAuthItem.Set(datastore) {
if !localAuth.Item.Set(datastore) {
ctx.JSON(500, ExitResponse{Msg: "auth identifier creation failed"})
return
}

ctx.String(http.StatusOK, string(localAuthItem.Value.Key))
ctx.String(http.StatusOK, string(localAuth.Item.Value.Key))
}

/*
Expand All @@ -172,15 +182,14 @@ func (localAuth LocalAuth) AuthUnmount(ctx *gin.Context) {

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

localAuthItem := localAuth.Item
localAuthItem.Name = ctx.Param("uuid")
localAuthItem.Value.Key = []byte(ctx.Request.Header.Get("X-DORY-TOKEN"))
localAuth.Item.Name = ctx.Param("uuid")
localAuth.Item.Value.Key = []byte(ctx.Request.Header.Get("X-DORY-TOKEN"))

if localAuth.Item.Name == "" {
ctx.JSON(500, ExitResponse{Msg: "passed uuid is empty"})
return
}
if !localAuthItem.Delete(datastore) {
if !localAuth.Item.Delete(datastore) {
ctx.JSON(500, ExitResponse{Msg: "auth identifier purge failed"})
return
}
Expand Down Expand Up @@ -249,14 +258,13 @@ func (localAuth LocalAuth) PurgeOne(ctx *gin.Context) {
return
}

localAuthItem := localAuth.Item
localAuth.Item.Name = ctx.Param("uuid")

if localAuthItem.Name == "" {
if localAuth.Item.Name == "" {
ctx.JSON(500, ExitResponse{Msg: "passed uuid is empty"})
return
}
if datastore.PurgeOne(localAuthItem.Name) != nil {
if datastore.PurgeOne(localAuth.Item.Name) != nil {
ctx.JSON(500, ExitResponse{Msg: "purge-one failed"})
return
}
Expand Down

0 comments on commit a91f45b

Please sign in to comment.