Skip to content

Commit

Permalink
Switch to using newly created Handlers (#56)
Browse files Browse the repository at this point in the history
* Switch to new handlers

* clean up old handlers and move errors around

* update changelog

* Switch to router.User() for auth middleware
  • Loading branch information
joe94 authored Sep 29, 2020
1 parent 530c07c commit 22b3c6d
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 310 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Add helper functions for DeleteItem Handler. [#52](https://github.com/xmidt-org/argus/pull/52)
- Add helper functions for GetAllItems Handler. [#53](https://github.com/xmidt-org/argus/pull/53)
- Add helper functions for PushItem Handler. [#55](https://github.com/xmidt-org/argus/pull/55)
- Switch store provider to use new handlers. [#56](https://github.com/xmidt-org/argus/pull/56)


### Fixed
- Add back listener to chrysom config. [#54](https://github.com/xmidt-org/argus/pull/54)
Expand Down
34 changes: 17 additions & 17 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main

import (
"fmt"
"net/http"

"github.com/gorilla/mux"
"github.com/justinas/alice"
Expand Down Expand Up @@ -89,28 +90,27 @@ type PrimaryRouter struct {
AuthChain *alice.Chain `name:"auth_chain"`
}

type SetRoutesIn struct {
type PrimaryRoutes struct {
fx.In
Handler store.Handler `name:"setHandler"`
}
type GetRoutesIn struct {
fx.In
Handler store.Handler `name:"getHandler"`
}
type GetAllRoutesIn struct {
fx.In
Handler store.Handler `name:"getAllHandler"`
Set store.Handler `name:"setHandler"`
Delete store.Handler `name:"deleteHandler"`
Get store.Handler `name:"getHandler"`
GetAll store.Handler `name:"getAllHandler"`
}

func BuildPrimaryRoutes(router PrimaryRouter, sin SetRoutesIn, gin GetRoutesIn, gain GetAllRoutesIn) {
if sin.Handler != nil {
router.Router.Handle(fmt.Sprintf("/%s/store/{bucket}", apiBase), router.AuthChain.Then(sin.Handler)).Methods("PUT")
func BuildPrimaryRoutes(router PrimaryRouter, routes PrimaryRoutes) {
router.Router.Use(router.AuthChain.Then)
if routes.Set != nil {
router.Router.Handle(fmt.Sprintf("/%s/store/{bucket}", apiBase), routes.Set).Methods(http.MethodPut)
}
if routes.Get != nil {
router.Router.Handle(fmt.Sprintf("/%s/store/{bucket}/{id}", apiBase), routes.Get).Methods(http.MethodGet)
}
if gin.Handler != nil {
router.Router.Handle(fmt.Sprintf("/%s/store/{bucket}/{key}", apiBase), router.AuthChain.Then(gin.Handler)).Methods("GET", "DELETE")
if routes.GetAll != nil {
router.Router.Handle(fmt.Sprintf("/%s/store/{bucket}", apiBase), routes.GetAll).Methods(http.MethodGet)
}
if gain.Handler != nil {
router.Router.Handle(fmt.Sprintf("/%s/store/{bucket}", apiBase), router.AuthChain.Then(gain.Handler)).Methods("GET")
if routes.Delete != nil {
router.Router.Handle(fmt.Sprintf("/%s/store/{bucket}/{id}", apiBase), routes.Delete).Methods(http.MethodDelete)
}
}

Expand Down
132 changes: 2 additions & 130 deletions store/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,94 +19,11 @@ package store

import (
"context"
"crypto/sha256"
"encoding/base64"
"fmt"
"net/http"

"github.com/go-kit/kit/endpoint"
"github.com/xmidt-org/argus/model"
)

type KeyNotFoundError struct {
Key model.Key
}

func (knfe KeyNotFoundError) Error() string {
if knfe.Key.ID == "" && knfe.Key.Bucket == "" {
return fmt.Sprint("parameters for key not set")
} else if knfe.Key.ID == "" && knfe.Key.Bucket != "" {
return fmt.Sprintf("no value exists for bucket %s", knfe.Key.Bucket)

}
return fmt.Sprintf("no value exists with bucket: %s, id: %s", knfe.Key.Bucket, knfe.Key.ID)
}

func (knfe KeyNotFoundError) StatusCode() int {
return http.StatusNotFound
}

type BadRequestError struct {
Request interface{}
}

func (bre BadRequestError) Error() string {
return fmt.Sprintf("No value exists with request: %#v", bre)
}

func (bre BadRequestError) StatusCode() int {
return http.StatusBadRequest
}

type InternalError struct {
Reason interface{}
Retryable bool
}

func (ie InternalError) Error() string {
return fmt.Sprintf("Request Failed: \n%#v", ie.Reason)
}

func (ie InternalError) StatusCode() int {
return http.StatusInternalServerError
}

type InvalidRequestError struct {
Reason string
}

func (ire InvalidRequestError) Error() string {
return ire.Reason
}

func (ire InvalidRequestError) StatusCode() int {
return http.StatusBadRequest
}

func NewSetEndpoint(s S) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
var (
kv KeyItemPairRequest
ok bool
)
if kv, ok = request.(KeyItemPairRequest); !ok {
return nil, BadRequestError{Request: request}
}
if kv.Identifier == "" {
return nil, BadRequestError{Request: request}
}

// Generate ID from Item identifier
sum := sha256.Sum256([]byte(kv.Identifier))
kv.ID = base64.RawURLEncoding.EncodeToString(sum[:])
if len([]byte(kv.ID)) >= 1024 {
return nil, InvalidRequestError{Reason: "identifier is too big"}
}
err := s.Push(kv.Key, kv.OwnableItem)
return kv.Key, err
}
}

func newGetItemEndpoint(s S) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
itemRequest := request.(*getOrDeleteItemRequest)
Expand All @@ -122,10 +39,6 @@ func newGetItemEndpoint(s S) endpoint.Endpoint {
}
}

func userOwnsItem(requestItemOwner, actualItemOwner string) bool {
return requestItemOwner == "" || requestItemOwner == actualItemOwner
}

func newDeleteItemEndpoint(s S) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
itemRequest := request.(*getOrDeleteItemRequest)
Expand All @@ -145,35 +58,6 @@ func newDeleteItemEndpoint(s S) endpoint.Endpoint {
}
}

func NewGetEndpoint(s S) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
var (
kv KeyItemPairRequest
ok bool
)
if kv, ok = request.(KeyItemPairRequest); !ok {
return nil, BadRequestError{Request: request}
}
if kv.Key.Bucket == "" || kv.Key.ID == "" {
return nil, BadRequestError{Request: request}
}
value, err := s.Get(kv.Key)
if err != nil {
return nil, err
}
if kv.Owner == value.Owner || kv.Owner == "" {
if kv.Method == "DELETE" {
value, err = s.Delete(kv.Key)
return value, err
}
return value, nil
}
return nil, KeyNotFoundError{
Key: kv.Key,
}
}
}

func newGetAllItemsEndpoint(s S) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
itemsRequest := request.(*getAllItemsRequest)
Expand Down Expand Up @@ -201,18 +85,6 @@ func newPushItemEndpoint(s S) endpoint.Endpoint {
}
}

func NewGetAllEndpoint(s S) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
var (
kv KeyItemPairRequest
ok bool
)
if kv, ok = request.(KeyItemPairRequest); !ok {
return nil, BadRequestError{Request: request}
}

value, err := s.GetAll(kv.Key.Bucket)

return FilterOwner(value, kv.Owner), err
}
func userOwnsItem(requestItemOwner, actualItemOwner string) bool {
return requestItemOwner == "" || requestItemOwner == actualItemOwner
}
38 changes: 37 additions & 1 deletion store/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package store

import "net/http"
import (
"fmt"
"net/http"

"github.com/xmidt-org/argus/model"
)

type BadRequestErr struct {
Message string
Expand All @@ -13,3 +18,34 @@ func (bre BadRequestErr) Error() string {
func (bre BadRequestErr) StatusCode() int {
return http.StatusBadRequest
}

type KeyNotFoundError struct {
Key model.Key
}

func (knfe KeyNotFoundError) Error() string {
if knfe.Key.ID == "" && knfe.Key.Bucket == "" {
return fmt.Sprint("parameters for key not set")
} else if knfe.Key.ID == "" && knfe.Key.Bucket != "" {
return fmt.Sprintf("no value exists for bucket %s", knfe.Key.Bucket)

}
return fmt.Sprintf("no value exists with bucket: %s, id: %s", knfe.Key.Bucket, knfe.Key.ID)
}

func (knfe KeyNotFoundError) StatusCode() int {
return http.StatusNotFound
}

type InternalError struct {
Reason interface{}
Retryable bool
}

func (ie InternalError) Error() string {
return fmt.Sprintf("Request Failed: \n%#v", ie.Reason)
}

func (ie InternalError) StatusCode() int {
return http.StatusInternalServerError
}
Loading

0 comments on commit 22b3c6d

Please sign in to comment.