Skip to content

Commit

Permalink
refactor allowedservices
Browse files Browse the repository at this point in the history
  • Loading branch information
schallert committed Mar 6, 2019
1 parent 8b1fee8 commit c31e3da
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 22 deletions.
56 changes: 56 additions & 0 deletions src/query/api/v1/handler/allowed_services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package handler

type allowedServicesSet map[string]bool

func (a allowedServicesSet) String() []string {
s := make([]string, 0, len(a))
for key := range a {
s = append(s, key)
}
return s
}

var (
allowedServices = allowedServicesSet{
M3DBServiceName: true,
M3AggregatorServiceName: true,
M3CoordinatorServiceName: true,
}
)

// IsAllowedService returns whether a service name is a valid M3 service.
func IsAllowedService(svc string) bool {
_, ok := allowedServices[svc]
return ok
}

// AllowedServices returns the list of valid M3 services.
func AllowedServices() []string {
svcs := make([]string, 0, len(allowedServices))
for svc, allowed := range allowedServices {
if allowed {
svcs = append(svcs, svc)
}
}
return svcs
}
45 changes: 45 additions & 0 deletions src/query/api/v1/handler/allowed_services_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package handler

import (
"sort"
"testing"

"github.com/stretchr/testify/assert"
)

func TestIsAllowedService(t *testing.T) {
assert.True(t, IsAllowedService("m3db"))
assert.False(t, IsAllowedService("foo"))
}

func TestAllowedServices(t *testing.T) {
exp := []string{
"m3aggregator",
"m3coordinator",
"m3db",
}

svcs := AllowedServices()
sort.Strings(svcs)
assert.Equal(t, exp, svcs)
}
2 changes: 1 addition & 1 deletion src/query/api/v1/handler/database/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (h *createHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

opts := handler.NewServiceOptions("kv", r.Header, nil)
opts := handler.NewServiceOptions(handler.M3DBServiceName, r.Header, nil)
nsRegistry, err = h.namespaceAddHandler.Add(namespaceRequest, opts)
if err != nil {
logger.Error("unable to add namespace", zap.Error(err))
Expand Down
24 changes: 4 additions & 20 deletions src/query/api/v1/handler/placement/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ import (
"github.com/gorilla/mux"
)

type allowedServicesSet map[string]bool

func (a allowedServicesSet) String() []string {
s := make([]string, 0, len(a))
for key := range a {
s = append(s, key)
}
return s
}

const (
// ServicesPathName is the services part of the API path.
ServicesPathName = "services"
Expand All @@ -70,12 +60,6 @@ var (
M3CoordinatorServicePlacementPathName = path.Join(ServicesPathName, handler.M3CoordinatorServiceName, PlacementPathName)

errUnableToParseService = errors.New("unable to parse service")

allowedServices = allowedServicesSet{
handler.M3DBServiceName: true,
handler.M3AggregatorServiceName: true,
handler.M3CoordinatorServiceName: true,
}
)

// HandlerOptions is the options struct for the handler.
Expand Down Expand Up @@ -148,10 +132,10 @@ func ServiceWithAlgo(
return nil, nil, err
}

if _, ok := allowedServices[opts.ServiceName]; !ok {
if !handler.IsAllowedService(opts.ServiceName) {
return nil, nil, fmt.Errorf(
"invalid service name: %s, must be one of: %s",
opts.ServiceName, allowedServices.String())
"invalid service name: %s, must be one of: %v",
opts.ServiceName, handler.AllowedServices())
}

sid := opts.ServiceID()
Expand Down Expand Up @@ -438,7 +422,7 @@ func parseServiceFromRequest(r *http.Request) (string, error) {
for i, c := range components {
if c == "services" && i+1 < len(components) {
service := components[i+1]
if _, ok := allowedServices[service]; ok {
if handler.IsAllowedService(service) {
return service, nil
}
return "", fmt.Errorf("unknown service: %s", service)
Expand Down
2 changes: 1 addition & 1 deletion src/query/api/v1/handler/placement/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func TestValidateAllAvailable(t *testing.T) {
}

func runForAllAllowedServices(f func(service string)) {
for service := range allowedServices {
for _, service := range handler.AllowedServices() {
f(service)
}
}
Expand Down

0 comments on commit c31e3da

Please sign in to comment.