Don't rely on token for scopes collection actions (#210)
Don't rely on token for scopes collection actions This requires `scope_id` to be passed (via query param) for actions on the scopes collection; that is, listing or creating. However, it removes the dependency on using the token for the scope of the command. For actions on a specific scope, we use the given ID to determine the scope we should be operating at, doing a lookup if necessary. Existing tests have been updated. Of particular note, the CORS test was already operating on the scopes collection so served as a good test to ensure that from an API level the scope_id parameter was being parsed and passed through (the tests fail without adding that query parameter in).
This commit is contained in:
1 parent
2bbc1163c4
commit
e8325a9f59
17 files changed
+372
-243
No files matched your search
@@ -3,11 +3,14 @@ package controller
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/hashicorp/go-retryablehttp"
|
||||
"github.com/hashicorp/watchtower/api/scopes"
|
||||
"github.com/hashicorp/watchtower/internal/cmd/config"
|
||||
)
|
||||
|
||||
@@ -73,13 +76,14 @@ func TestHandler_CORS(t *testing.T) {
|
||||
defer tc.Shutdown()
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
method string
|
||||
origin string
|
||||
code int
|
||||
acrmHeader string
|
||||
allowedHeader string
|
||||
listenerNum int
|
||||
name string
|
||||
method string
|
||||
origin string
|
||||
code int
|
||||
acrmHeader string
|
||||
allowedHeader string
|
||||
listenerNum int
|
||||
provideScopeId bool
|
||||
}{
|
||||
{
|
||||
name: "disabled no origin",
|
||||
@@ -140,6 +144,21 @@ func TestHandler_CORS(t *testing.T) {
|
||||
code: http.StatusOK,
|
||||
listenerNum: 4,
|
||||
},
|
||||
{
|
||||
name: "enabled with wildcard origins and no origin defined, scope id checking",
|
||||
method: http.MethodGet,
|
||||
code: http.StatusOK,
|
||||
listenerNum: 4,
|
||||
provideScopeId: true,
|
||||
},
|
||||
{
|
||||
name: "enabled with wildcard origins and origin defined, scope id checking",
|
||||
method: http.MethodPost,
|
||||
origin: "flubber.com",
|
||||
code: http.StatusOK,
|
||||
listenerNum: 4,
|
||||
provideScopeId: true,
|
||||
},
|
||||
{
|
||||
name: "wildcard origins with method list and good method",
|
||||
method: http.MethodOptions,
|
||||
@@ -169,8 +188,22 @@ func TestHandler_CORS(t *testing.T) {
|
||||
client.SetToken("fo_o_bar")
|
||||
|
||||
// Create the request
|
||||
req, err := client.NewRequest(tc.Context(), c.method, "scopes", nil)
|
||||
var req *retryablehttp.Request
|
||||
|
||||
// This tests out scope_id handling from body or query
|
||||
var body interface{}
|
||||
scopeId := "global"
|
||||
if c.provideScopeId {
|
||||
scopeId = "o_1234567890"
|
||||
if c.method == http.MethodPost {
|
||||
body = &scopes.Scope{}
|
||||
}
|
||||
}
|
||||
req, err = client.NewRequest(tc.Context(), c.method, "scopes", body)
|
||||
require.NoError(t, err)
|
||||
q := url.Values{}
|
||||
q.Add("scope_id", scopeId)
|
||||
req.URL.RawQuery = q.Encode()
|
||||
|
||||
// Append headers
|
||||
if c.origin != "" {
|
||||
|
||||
@@ -125,7 +125,7 @@ func wrapHandlerWithCommonFuncs(h http.Handler, c *Controller, props HandlerProp
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if logUrls {
|
||||
c.logger.Trace("request received", "url", r.URL.String())
|
||||
c.logger.Trace("request received", "url", r.URL.RequestURI())
|
||||
}
|
||||
|
||||
// Set the Cache-Control header for all responses returned
|
||||
|
||||
@@ -65,7 +65,6 @@ func TestAuthenticate(t *testing.T) {
|
||||
{
|
||||
name: "basic",
|
||||
request: &pbs.AuthenticateRequest{
|
||||
ScopeId: o.GetPublicId(),
|
||||
AuthMethodId: amId,
|
||||
TokenType: "token",
|
||||
Credentials: func() *structpb.Struct {
|
||||
@@ -83,7 +82,6 @@ func TestAuthenticate(t *testing.T) {
|
||||
{
|
||||
name: "no-token-type",
|
||||
request: &pbs.AuthenticateRequest{
|
||||
ScopeId: o.GetPublicId(),
|
||||
AuthMethodId: amId,
|
||||
Credentials: func() *structpb.Struct {
|
||||
creds := map[string]*structpb.Value{
|
||||
@@ -100,7 +98,6 @@ func TestAuthenticate(t *testing.T) {
|
||||
{
|
||||
name: "bad-token-type",
|
||||
request: &pbs.AuthenticateRequest{
|
||||
ScopeId: o.GetPublicId(),
|
||||
AuthMethodId: amId,
|
||||
TokenType: "email",
|
||||
Credentials: func() *structpb.Struct {
|
||||
@@ -116,7 +113,6 @@ func TestAuthenticate(t *testing.T) {
|
||||
{
|
||||
name: "no-authmethod",
|
||||
request: &pbs.AuthenticateRequest{
|
||||
ScopeId: o.GetPublicId(),
|
||||
Credentials: func() *structpb.Struct {
|
||||
creds := map[string]*structpb.Value{
|
||||
"name": {Kind: &structpb.Value_StringValue{StringValue: "admin"}},
|
||||
@@ -130,7 +126,6 @@ func TestAuthenticate(t *testing.T) {
|
||||
{
|
||||
name: "wrong-username-password",
|
||||
request: &pbs.AuthenticateRequest{
|
||||
ScopeId: o.GetPublicId(),
|
||||
AuthMethodId: amId,
|
||||
TokenType: "token",
|
||||
Credentials: func() *structpb.Struct {
|
||||
@@ -214,7 +209,6 @@ func TestAuthenticate_AuthAccountConnectedToIamUser(t *testing.T) {
|
||||
s, err := NewService(iamRepoFn, authTokenRepoFn)
|
||||
require.NoError(err)
|
||||
resp, err := s.Authenticate(context.Background(), &pbs.AuthenticateRequest{
|
||||
ScopeId: o.GetPublicId(),
|
||||
AuthMethodId: amId,
|
||||
Credentials: func() *structpb.Struct {
|
||||
creds := map[string]*structpb.Value{
|
||||
|
||||
@@ -136,7 +136,7 @@ func TestGet(t *testing.T) {
|
||||
s, err := groups.NewService(repo)
|
||||
require.NoError(err, "Couldn't create new group service.")
|
||||
|
||||
got, gErr := s.GetGroup(auth.DisabledAuthTestContext(auth.WithTestScopeId(tc.scopeId)), req)
|
||||
got, gErr := s.GetGroup(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "GetGroup(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
|
||||
assert.True(proto.Equal(got, tc.res), "GetGroup(%q) got response\n%q, wanted\n%q", req, got, tc.res)
|
||||
})
|
||||
@@ -208,7 +208,7 @@ func TestList(t *testing.T) {
|
||||
s, err := groups.NewService(repoFn)
|
||||
require.NoError(err, "Couldn't create new group service.")
|
||||
|
||||
got, gErr := s.ListGroups(auth.DisabledAuthTestContext(auth.WithTestScopeId(tc.scopeId)), &pbs.ListGroupsRequest{})
|
||||
got, gErr := s.ListGroups(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), &pbs.ListGroupsRequest{})
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "ListGroups(%q) got error %v, wanted %v", tc.scopeId, gErr, tc.errCode)
|
||||
assert.True(proto.Equal(got, tc.res), "ListGroups(%q) got response %q, wanted %q", tc.scopeId, got, tc.res)
|
||||
})
|
||||
@@ -308,7 +308,7 @@ func TestDelete(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
got, gErr := s.DeleteGroup(auth.DisabledAuthTestContext(auth.WithTestScopeId(tc.scopeId)), tc.req)
|
||||
got, gErr := s.DeleteGroup(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), tc.req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "DeleteGroup(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
|
||||
assert.EqualValuesf(tc.res, got, "DeleteGroup(%+v) got response %q, wanted %q", tc.req, got, tc.res)
|
||||
})
|
||||
@@ -326,7 +326,7 @@ func TestDelete_twice(t *testing.T) {
|
||||
req := &pbs.DeleteGroupRequest{
|
||||
Id: og.GetPublicId(),
|
||||
}
|
||||
ctx := auth.DisabledAuthTestContext(auth.WithTestScopeId(scopeId))
|
||||
ctx := auth.DisabledAuthTestContext(auth.WithScopeId(scopeId))
|
||||
got, gErr := s.DeleteGroup(ctx, req)
|
||||
assert.NoError(gErr, "First attempt")
|
||||
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
|
||||
@@ -338,7 +338,7 @@ func TestDelete_twice(t *testing.T) {
|
||||
projReq := &pbs.DeleteGroupRequest{
|
||||
Id: pg.GetPublicId(),
|
||||
}
|
||||
ctx = auth.DisabledAuthTestContext(auth.WithTestScopeId(scopeId))
|
||||
ctx = auth.DisabledAuthTestContext(auth.WithScopeId(scopeId))
|
||||
got, gErr = s.DeleteGroup(ctx, projReq)
|
||||
assert.NoError(gErr, "First attempt")
|
||||
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
|
||||
@@ -431,7 +431,7 @@ func TestCreate(t *testing.T) {
|
||||
s, err := groups.NewService(repo)
|
||||
require.NoError(err, "Error when getting new group service.")
|
||||
|
||||
got, gErr := s.CreateGroup(auth.DisabledAuthTestContext(auth.WithTestScopeId(tc.scopeId)), req)
|
||||
got, gErr := s.CreateGroup(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "CreateGroup(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
|
||||
if got != nil {
|
||||
assert.True(strings.HasPrefix(got.GetUri(), tc.res.Uri), got.GetUri())
|
||||
@@ -742,7 +742,7 @@ func TestUpdate(t *testing.T) {
|
||||
req := proto.Clone(toMerge).(*pbs.UpdateGroupRequest)
|
||||
proto.Merge(req, tc.req)
|
||||
|
||||
got, gErr := tested.UpdateGroup(auth.DisabledAuthTestContext(auth.WithTestScopeId(tc.scopeId)), req)
|
||||
got, gErr := tested.UpdateGroup(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "UpdateGroup(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
|
||||
|
||||
if got != nil {
|
||||
|
||||
@@ -105,7 +105,7 @@ func TestGet(t *testing.T) {
|
||||
s, err := host_catalogs.NewService(repo)
|
||||
require.NoError(t, err, "Couldn't create a new host catalog service.")
|
||||
|
||||
got, gErr := s.GetHostCatalog(auth.DisabledAuthTestContext(auth.WithTestScopeId(proj.GetPublicId())), req)
|
||||
got, gErr := s.GetHostCatalog(auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId())), req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "GetHostCatalog(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
|
||||
assert.True(proto.Equal(got, tc.res), "GetHostCatalog(%q) got response %q, wanted %q", req, got, tc.res)
|
||||
})
|
||||
@@ -184,7 +184,7 @@ func TestDelete(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
got, gErr := s.DeleteHostCatalog(auth.DisabledAuthTestContext(auth.WithTestScopeId(tc.scopeId)), tc.req)
|
||||
got, gErr := s.DeleteHostCatalog(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), tc.req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "DeleteHostCatalog(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
|
||||
assert.EqualValuesf(tc.res, got, "DeleteHostCatalog(%q) got response %q, wanted %q", tc.req, got, tc.res)
|
||||
})
|
||||
@@ -201,7 +201,7 @@ func TestDelete_twice(t *testing.T) {
|
||||
req := &pbs.DeleteHostCatalogRequest{
|
||||
Id: hc.GetPublicId(),
|
||||
}
|
||||
ctx := auth.DisabledAuthTestContext(auth.WithTestScopeId(proj.GetPublicId()))
|
||||
ctx := auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId()))
|
||||
got, gErr := s.DeleteHostCatalog(ctx, req)
|
||||
assert.NoError(gErr, "First attempt")
|
||||
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
|
||||
@@ -293,7 +293,7 @@ func TestCreate(t *testing.T) {
|
||||
s, err := host_catalogs.NewService(repo)
|
||||
require.NoError(err, "Failed to create a new host catalog service.")
|
||||
|
||||
got, gErr := s.CreateHostCatalog(auth.DisabledAuthTestContext(auth.WithTestScopeId(proj.GetPublicId())), req)
|
||||
got, gErr := s.CreateHostCatalog(auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId())), req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "CreateHostCatalog(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
|
||||
if got != nil {
|
||||
assert.True(strings.HasPrefix(got.GetUri(), tc.res.GetUri()))
|
||||
@@ -588,7 +588,7 @@ func TestUpdate(t *testing.T) {
|
||||
req := proto.Clone(toMerge).(*pbs.UpdateHostCatalogRequest)
|
||||
proto.Merge(req, tc.req)
|
||||
|
||||
got, gErr := tested.UpdateHostCatalog(auth.DisabledAuthTestContext(auth.WithTestScopeId(proj.GetPublicId())), req)
|
||||
got, gErr := tested.UpdateHostCatalog(auth.DisabledAuthTestContext(auth.WithScopeId(proj.GetPublicId())), req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "UpdateHostCatalog(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
|
||||
|
||||
if got != nil {
|
||||
|
||||
@@ -166,7 +166,7 @@ func TestGet(t *testing.T) {
|
||||
s, err := roles.NewService(repo)
|
||||
require.NoError(err, "Couldn't create new role service.")
|
||||
|
||||
got, gErr := s.GetRole(auth.DisabledAuthTestContext(auth.WithTestScopeId(tc.scopeId)), req)
|
||||
got, gErr := s.GetRole(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "GetRole(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
|
||||
assert.True(proto.Equal(got, tc.res), "GetRole(%q) got response\n%q, wanted\n%q", req, got, tc.res)
|
||||
})
|
||||
@@ -247,7 +247,7 @@ func TestList(t *testing.T) {
|
||||
s, err := roles.NewService(repoFn)
|
||||
require.NoError(err, "Couldn't create new role service.")
|
||||
|
||||
got, gErr := s.ListRoles(auth.DisabledAuthTestContext(auth.WithTestScopeId(tc.scopeId)), tc.req)
|
||||
got, gErr := s.ListRoles(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), tc.req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "ListRoles(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
|
||||
assert.True(proto.Equal(got, tc.res), "ListRoles(%q) got response %q, wanted %q", tc.req, got, tc.res)
|
||||
})
|
||||
@@ -326,7 +326,7 @@ func TestDelete(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
got, gErr := s.DeleteRole(auth.DisabledAuthTestContext(auth.WithTestScopeId(tc.scopeId)), tc.req)
|
||||
got, gErr := s.DeleteRole(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), tc.req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "DeleteRole(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
|
||||
assert.EqualValuesf(tc.res, got, "DeleteRole(%q) got response %q, wanted %q", tc.req, got, tc.res)
|
||||
})
|
||||
@@ -343,7 +343,7 @@ func TestDelete_twice(t *testing.T) {
|
||||
req := &pbs.DeleteRoleRequest{
|
||||
Id: or.GetPublicId(),
|
||||
}
|
||||
ctx := auth.DisabledAuthTestContext(auth.WithTestScopeId(or.GetPublicId()))
|
||||
ctx := auth.DisabledAuthTestContext(auth.WithScopeId(or.GetPublicId()))
|
||||
got, gErr := s.DeleteRole(ctx, req)
|
||||
assert.NoError(gErr, "First attempt")
|
||||
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
|
||||
@@ -354,7 +354,7 @@ func TestDelete_twice(t *testing.T) {
|
||||
projReq := &pbs.DeleteRoleRequest{
|
||||
Id: pr.GetPublicId(),
|
||||
}
|
||||
ctx = auth.DisabledAuthTestContext(auth.WithTestScopeId(pr.GetPublicId()))
|
||||
ctx = auth.DisabledAuthTestContext(auth.WithScopeId(pr.GetPublicId()))
|
||||
got, gErr = s.DeleteRole(ctx, projReq)
|
||||
assert.NoError(gErr, "First attempt")
|
||||
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
|
||||
@@ -469,7 +469,7 @@ func TestCreate(t *testing.T) {
|
||||
s, err := roles.NewService(repo)
|
||||
require.NoError(err, "Error when getting new role service.")
|
||||
|
||||
got, gErr := s.CreateRole(auth.DisabledAuthTestContext(auth.WithTestScopeId(tc.scopeId)), req)
|
||||
got, gErr := s.CreateRole(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "CreateRole(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
|
||||
if got != nil {
|
||||
assert.True(strings.HasPrefix(got.GetUri(), tc.res.Uri), "Expected %q to have the prefix %q", got.GetUri(), tc.res.GetUri())
|
||||
@@ -877,7 +877,7 @@ func TestUpdate(t *testing.T) {
|
||||
req := proto.Clone(toMerge).(*pbs.UpdateRoleRequest)
|
||||
proto.Merge(req, tc.req)
|
||||
|
||||
got, gErr := tested.UpdateRole(auth.DisabledAuthTestContext(auth.WithTestScopeId(tc.scopeId)), req)
|
||||
got, gErr := tested.UpdateRole(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "UpdateRole(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
|
||||
|
||||
if got != nil {
|
||||
@@ -978,7 +978,7 @@ func TestAddPrincipal(t *testing.T) {
|
||||
PrincipalIds: append(tc.addUsers, tc.addGroups...),
|
||||
}
|
||||
|
||||
got, err := s.AddRolePrincipals(auth.DisabledAuthTestContext(auth.WithTestScopeId(o.GetPublicId())), req)
|
||||
got, err := s.AddRolePrincipals(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), req)
|
||||
if tc.wantErr {
|
||||
assert.Error(t, err)
|
||||
return
|
||||
@@ -1011,7 +1011,7 @@ func TestAddPrincipal(t *testing.T) {
|
||||
for _, tc := range failCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
_, gErr := s.AddRolePrincipals(auth.DisabledAuthTestContext(auth.WithTestScopeId(p.GetPublicId())), tc.req)
|
||||
_, gErr := s.AddRolePrincipals(auth.DisabledAuthTestContext(auth.WithScopeId(p.GetPublicId())), tc.req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "AddRolePrincipals(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
|
||||
})
|
||||
}
|
||||
@@ -1098,7 +1098,7 @@ func TestSetPrincipal(t *testing.T) {
|
||||
PrincipalIds: append(tc.setUsers, tc.setGroups...),
|
||||
}
|
||||
|
||||
got, err := s.SetRolePrincipals(auth.DisabledAuthTestContext(auth.WithTestScopeId(o.GetPublicId())), req)
|
||||
got, err := s.SetRolePrincipals(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), req)
|
||||
if tc.wantErr {
|
||||
assert.Error(t, err)
|
||||
return
|
||||
@@ -1131,7 +1131,7 @@ func TestSetPrincipal(t *testing.T) {
|
||||
for _, tc := range failCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
_, gErr := s.SetRolePrincipals(auth.DisabledAuthTestContext(auth.WithTestScopeId(p.GetPublicId())), tc.req)
|
||||
_, gErr := s.SetRolePrincipals(auth.DisabledAuthTestContext(auth.WithScopeId(p.GetPublicId())), tc.req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "SetRolePrincipals(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
|
||||
})
|
||||
}
|
||||
@@ -1236,7 +1236,7 @@ func TestRemovePrincipal(t *testing.T) {
|
||||
PrincipalIds: append(tc.removeUsers, tc.removeGroups...),
|
||||
}
|
||||
|
||||
got, err := s.RemoveRolePrincipals(auth.DisabledAuthTestContext(auth.WithTestScopeId(o.GetPublicId())), req)
|
||||
got, err := s.RemoveRolePrincipals(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), req)
|
||||
if tc.wantErr {
|
||||
assert.Error(t, err)
|
||||
return
|
||||
@@ -1269,7 +1269,7 @@ func TestRemovePrincipal(t *testing.T) {
|
||||
for _, tc := range failCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
_, gErr := s.AddRolePrincipals(auth.DisabledAuthTestContext(auth.WithTestScopeId(p.GetPublicId())), tc.req)
|
||||
_, gErr := s.AddRolePrincipals(auth.DisabledAuthTestContext(auth.WithScopeId(p.GetPublicId())), tc.req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "AddRolePrincipals(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
|
||||
})
|
||||
}
|
||||
@@ -1348,7 +1348,7 @@ func TestAddGrants(t *testing.T) {
|
||||
scopeId = p.GetPublicId()
|
||||
}
|
||||
req.GrantStrings = append(req.GrantStrings, tc.add...)
|
||||
got, err := s.AddRoleGrants(auth.DisabledAuthTestContext(auth.WithTestScopeId(scopeId)), req)
|
||||
got, err := s.AddRoleGrants(auth.DisabledAuthTestContext(auth.WithScopeId(scopeId)), req)
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
@@ -1379,7 +1379,7 @@ func TestAddGrants(t *testing.T) {
|
||||
for _, tc := range failCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
_, gErr := s.AddRoleGrants(auth.DisabledAuthTestContext(auth.WithTestScopeId(p.GetPublicId())), tc.req)
|
||||
_, gErr := s.AddRoleGrants(auth.DisabledAuthTestContext(auth.WithScopeId(p.GetPublicId())), tc.req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "AddRoleGrants(%+v) got error %#v, wanted %#v", tc.req, gErr, tc.errCode)
|
||||
})
|
||||
}
|
||||
@@ -1446,7 +1446,7 @@ func TestSetGrants(t *testing.T) {
|
||||
scopeId = p.GetPublicId()
|
||||
}
|
||||
req.GrantStrings = append(req.GrantStrings, tc.set...)
|
||||
got, err := s.SetRoleGrants(auth.DisabledAuthTestContext(auth.WithTestScopeId(scopeId)), req)
|
||||
got, err := s.SetRoleGrants(auth.DisabledAuthTestContext(auth.WithScopeId(scopeId)), req)
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
@@ -1479,7 +1479,7 @@ func TestSetGrants(t *testing.T) {
|
||||
for _, tc := range failCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
_, gErr := s.SetRoleGrants(auth.DisabledAuthTestContext(auth.WithTestScopeId(p.GetPublicId())), tc.req)
|
||||
_, gErr := s.SetRoleGrants(auth.DisabledAuthTestContext(auth.WithScopeId(p.GetPublicId())), tc.req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "SetRoleGrants(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
|
||||
})
|
||||
}
|
||||
@@ -1545,7 +1545,7 @@ func TestRemoveGrants(t *testing.T) {
|
||||
scopeId = p.GetPublicId()
|
||||
}
|
||||
req.GrantStrings = append(req.GrantStrings, tc.remove...)
|
||||
got, err := s.RemoveRoleGrants(auth.DisabledAuthTestContext(auth.WithTestScopeId(scopeId)), req)
|
||||
got, err := s.RemoveRoleGrants(auth.DisabledAuthTestContext(auth.WithScopeId(scopeId)), req)
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
@@ -1579,7 +1579,7 @@ func TestRemoveGrants(t *testing.T) {
|
||||
for _, tc := range failCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
_, gErr := s.RemoveRoleGrants(auth.DisabledAuthTestContext(auth.WithTestScopeId(p.GetPublicId())), tc.req)
|
||||
_, gErr := s.RemoveRoleGrants(auth.DisabledAuthTestContext(auth.WithScopeId(p.GetPublicId())), tc.req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "RemoveRoleGrants(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -50,10 +50,17 @@ var _ pbs.ScopeServiceServer = Service{}
|
||||
|
||||
// ListScopes implements the interface pbs.ScopeServiceServer.
|
||||
func (s Service) ListScopes(ctx context.Context, req *pbs.ListScopesRequest) (*pbs.ListScopesResponse, error) {
|
||||
authResults := auth.Verify(ctx)
|
||||
if req.GetScopeId() == "" {
|
||||
return nil, handlers.InvalidArgumentErrorf(
|
||||
"Argument errors found in the request.",
|
||||
map[string]string{"scope_id": "Missing value for scope_id"},
|
||||
)
|
||||
}
|
||||
authResults := auth.Verify(ctx, auth.WithScopeId(req.GetScopeId()))
|
||||
if !authResults.Valid {
|
||||
return nil, handlers.ForbiddenError()
|
||||
}
|
||||
|
||||
if err := validateListRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -88,10 +95,17 @@ func (s Service) GetScope(ctx context.Context, req *pbs.GetScopeRequest) (*pbs.G
|
||||
|
||||
// CreateScope implements the interface pbs.ScopeServiceServer.
|
||||
func (s Service) CreateScope(ctx context.Context, req *pbs.CreateScopeRequest) (*pbs.CreateScopeResponse, error) {
|
||||
authResults := auth.Verify(ctx)
|
||||
if req.GetScopeId() == "" {
|
||||
return nil, handlers.InvalidArgumentErrorf(
|
||||
"Argument errors found in the request.",
|
||||
map[string]string{"scope_id": "Missing value for scope_id"},
|
||||
)
|
||||
}
|
||||
authResults := auth.Verify(ctx, auth.WithScopeId(req.GetScopeId()))
|
||||
if !authResults.Valid {
|
||||
return nil, handlers.ForbiddenError()
|
||||
}
|
||||
|
||||
if err := validateCreateRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -136,7 +136,7 @@ func TestGet(t *testing.T) {
|
||||
s, err := scopes.NewService(repo)
|
||||
require.NoError(err, "Couldn't create new project service.")
|
||||
|
||||
got, gErr := s.GetScope(auth.DisabledAuthTestContext(auth.WithTestScopeId(tc.scopeId)), req)
|
||||
got, gErr := s.GetScope(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "GetProject(%+v) got error\n%v, wanted\n%v", req, gErr, tc.errCode)
|
||||
assert.True(proto.Equal(got, tc.res), "GetProject(%q) got response\n%q, wanted\n%q", req, got, tc.res)
|
||||
})
|
||||
@@ -177,16 +177,16 @@ func TestList(t *testing.T) {
|
||||
errCode codes.Code
|
||||
}{
|
||||
{
|
||||
name: "List Only initial Orgs",
|
||||
name: "List initial orgs",
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.ListScopesRequest{},
|
||||
req: &pbs.ListScopesRequest{ScopeId: "global"},
|
||||
res: &pbs.ListScopesResponse{Items: initialOrgs},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "List No Projects",
|
||||
scopeId: oNoProjects.GetPublicId(),
|
||||
req: &pbs.ListScopesRequest{},
|
||||
req: &pbs.ListScopesRequest{ScopeId: oNoProjects.GetPublicId()},
|
||||
res: &pbs.ListScopesResponse{},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
@@ -197,9 +197,9 @@ func TestList(t *testing.T) {
|
||||
s, err := scopes.NewService(repoFn)
|
||||
require.NoError(err, "Couldn't create new role service.")
|
||||
|
||||
got, gErr := s.ListScopes(auth.DisabledAuthTestContext(auth.WithTestScopeId(tc.scopeId)), tc.req)
|
||||
got, gErr := s.ListScopes(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), tc.req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "ListScopes(%+v) got error\n%v, wanted\n%v", tc.req, gErr, tc.errCode)
|
||||
assert.True(proto.Equal(got, tc.res), "ListScopes(%q) got response\n%q, wanted\n%q", tc.req, got, tc.res)
|
||||
assert.True(proto.Equal(got, tc.res), "ListScopes(%q) got response\n%q\nwanted\n%q", tc.req, got, tc.res)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -244,14 +244,14 @@ func TestList(t *testing.T) {
|
||||
{
|
||||
name: "List Many Orgs",
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.ListScopesRequest{},
|
||||
req: &pbs.ListScopesRequest{ScopeId: "global"},
|
||||
res: &pbs.ListScopesResponse{Items: wantOrgs},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
{
|
||||
name: "List Many Projects",
|
||||
scopeId: oWithProjects.GetPublicId(),
|
||||
req: &pbs.ListScopesRequest{},
|
||||
req: &pbs.ListScopesRequest{ScopeId: oWithProjects.GetPublicId()},
|
||||
res: &pbs.ListScopesResponse{Items: wantProjects},
|
||||
errCode: codes.OK,
|
||||
},
|
||||
@@ -262,7 +262,7 @@ func TestList(t *testing.T) {
|
||||
s, err := scopes.NewService(repoFn)
|
||||
require.NoError(err, "Couldn't create new role service.")
|
||||
|
||||
got, gErr := s.ListScopes(auth.DisabledAuthTestContext(auth.WithTestScopeId(tc.scopeId)), tc.req)
|
||||
got, gErr := s.ListScopes(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), tc.req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "ListScopes(%+v) got error\n%v, wanted\n%v", tc.req, gErr, tc.errCode)
|
||||
assert.True(proto.Equal(got, tc.res), "ListScopes(%q) got response\n%q, wanted\n%q", tc.req, got, tc.res)
|
||||
})
|
||||
@@ -349,7 +349,7 @@ func TestDelete(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
got, gErr := s.DeleteScope(auth.DisabledAuthTestContext(auth.WithTestScopeId(tc.scopeId)), tc.req)
|
||||
got, gErr := s.DeleteScope(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), tc.req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "DeleteProject(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
|
||||
assert.EqualValuesf(tc.res, got, "DeleteProject(%q) got response %q, wanted %q", tc.req, got, tc.res)
|
||||
})
|
||||
@@ -363,7 +363,7 @@ func TestDelete_twice(t *testing.T) {
|
||||
|
||||
s, err := scopes.NewService(repo)
|
||||
require.NoError(err, "Error when getting new scopes service")
|
||||
ctx := auth.DisabledAuthTestContext(auth.WithTestScopeId(org.GetPublicId()))
|
||||
ctx := auth.DisabledAuthTestContext(auth.WithScopeId(org.GetPublicId()))
|
||||
req := &pbs.DeleteScopeRequest{
|
||||
Id: proj.GetPublicId(),
|
||||
}
|
||||
@@ -374,7 +374,7 @@ func TestDelete_twice(t *testing.T) {
|
||||
assert.NoError(gErr, "Second attempt")
|
||||
assert.False(got.GetExisted(), "Expected existed to be false for the second delete.")
|
||||
|
||||
ctx = auth.DisabledAuthTestContext(auth.WithTestScopeId(scope.Global.String()))
|
||||
ctx = auth.DisabledAuthTestContext(auth.WithScopeId(scope.Global.String()))
|
||||
req = &pbs.DeleteScopeRequest{
|
||||
Id: org.GetPublicId(),
|
||||
}
|
||||
@@ -403,10 +403,13 @@ func TestCreate(t *testing.T) {
|
||||
{
|
||||
name: "Create a valid Project",
|
||||
scopeId: defaultOrg.GetPublicId(),
|
||||
req: &pbs.CreateScopeRequest{Item: &pb.Scope{
|
||||
Name: &wrapperspb.StringValue{Value: "name"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
}},
|
||||
req: &pbs.CreateScopeRequest{
|
||||
ScopeId: defaultOrg.GetPublicId(),
|
||||
Item: &pb.Scope{
|
||||
Name: &wrapperspb.StringValue{Value: "name"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
},
|
||||
},
|
||||
res: &pbs.CreateScopeResponse{
|
||||
Uri: "scopes/p_",
|
||||
Item: &pb.Scope{
|
||||
@@ -420,10 +423,13 @@ func TestCreate(t *testing.T) {
|
||||
{
|
||||
name: "Create a valid Org",
|
||||
scopeId: scope.Global.String(),
|
||||
req: &pbs.CreateScopeRequest{Item: &pb.Scope{
|
||||
Name: &wrapperspb.StringValue{Value: "name"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
}},
|
||||
req: &pbs.CreateScopeRequest{
|
||||
ScopeId: scope.Global.String(),
|
||||
Item: &pb.Scope{
|
||||
Name: &wrapperspb.StringValue{Value: "name"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
},
|
||||
},
|
||||
res: &pbs.CreateScopeResponse{
|
||||
Uri: "scopes/o_",
|
||||
Item: &pb.Scope{
|
||||
@@ -471,7 +477,7 @@ func TestCreate(t *testing.T) {
|
||||
s, err := scopes.NewService(repo)
|
||||
require.NoError(err, "Error when getting new project service.")
|
||||
|
||||
got, gErr := s.CreateScope(auth.DisabledAuthTestContext(auth.WithTestScopeId(tc.scopeId)), req)
|
||||
got, gErr := s.CreateScope(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "CreateProject(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
|
||||
if got != nil {
|
||||
assert.True(strings.HasPrefix(got.GetUri(), tc.res.Uri))
|
||||
@@ -805,7 +811,7 @@ func TestUpdate(t *testing.T) {
|
||||
}
|
||||
proto.Merge(req, tc.req)
|
||||
|
||||
got, gErr := tested.UpdateScope(auth.DisabledAuthTestContext(auth.WithTestScopeId(tc.scopeId)), req)
|
||||
got, gErr := tested.UpdateScope(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "UpdateScope(%+v) got error\n%v, wanted\n%v", req, gErr, tc.errCode)
|
||||
|
||||
if got != nil {
|
||||
|
||||
@@ -95,7 +95,7 @@ func TestGet(t *testing.T) {
|
||||
s, err := users.NewService(repo)
|
||||
require.NoError(err, "Couldn't create new user service.")
|
||||
|
||||
got, gErr := s.GetUser(auth.DisabledAuthTestContext(auth.WithTestScopeId(u.GetScopeId())), req)
|
||||
got, gErr := s.GetUser(auth.DisabledAuthTestContext(auth.WithScopeId(u.GetScopeId())), req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "GetUser(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
|
||||
assert.True(proto.Equal(got, tc.res), "GetUser(%q) got response %q, wanted %q", req, got, tc.res)
|
||||
})
|
||||
@@ -157,7 +157,7 @@ func TestList(t *testing.T) {
|
||||
s, err := users.NewService(repoFn)
|
||||
require.NoError(err, "Couldn't create new user service.")
|
||||
|
||||
got, gErr := s.ListUsers(auth.DisabledAuthTestContext(auth.WithTestScopeId(tc.scopeId)), tc.req)
|
||||
got, gErr := s.ListUsers(auth.DisabledAuthTestContext(auth.WithScopeId(tc.scopeId)), tc.req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "ListUsers(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
|
||||
assert.True(proto.Equal(got, tc.res), "ListUsers(%q) got response %q, wanted %q", tc.req, got, tc.res)
|
||||
})
|
||||
@@ -209,7 +209,7 @@ func TestDelete(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
got, gErr := s.DeleteUser(auth.DisabledAuthTestContext(auth.WithTestScopeId(u.GetScopeId())), tc.req)
|
||||
got, gErr := s.DeleteUser(auth.DisabledAuthTestContext(auth.WithScopeId(u.GetScopeId())), tc.req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "DeleteUser(%+v) got error %v, wanted %v", tc.req, gErr, tc.errCode)
|
||||
assert.EqualValuesf(tc.res, got, "DeleteUser(%q) got response %q, wanted %q", tc.req, got, tc.res)
|
||||
})
|
||||
@@ -226,7 +226,7 @@ func TestDelete_twice(t *testing.T) {
|
||||
req := &pbs.DeleteUserRequest{
|
||||
Id: u.GetPublicId(),
|
||||
}
|
||||
ctx := auth.DisabledAuthTestContext(auth.WithTestScopeId(u.GetScopeId()))
|
||||
ctx := auth.DisabledAuthTestContext(auth.WithScopeId(u.GetScopeId()))
|
||||
got, gErr := s.DeleteUser(ctx, req)
|
||||
assert.NoError(gErr, "First attempt")
|
||||
assert.True(got.GetExisted(), "Expected existed to be true for the first delete.")
|
||||
@@ -298,7 +298,7 @@ func TestCreate(t *testing.T) {
|
||||
s, err := users.NewService(repo)
|
||||
require.NoError(err, "Error when getting new user service.")
|
||||
|
||||
got, gErr := s.CreateUser(auth.DisabledAuthTestContext(auth.WithTestScopeId(defaultUser.GetScopeId())), req)
|
||||
got, gErr := s.CreateUser(auth.DisabledAuthTestContext(auth.WithScopeId(defaultUser.GetScopeId())), req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "CreateUser(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
|
||||
if got != nil {
|
||||
assert.True(strings.HasPrefix(got.GetUri(), tc.res.Uri))
|
||||
@@ -551,7 +551,7 @@ func TestUpdate(t *testing.T) {
|
||||
req := proto.Clone(toMerge).(*pbs.UpdateUserRequest)
|
||||
proto.Merge(req, tc.req)
|
||||
|
||||
got, gErr := tested.UpdateUser(auth.DisabledAuthTestContext(auth.WithTestScopeId(u.GetScopeId())), req)
|
||||
got, gErr := tested.UpdateUser(auth.DisabledAuthTestContext(auth.WithScopeId(u.GetScopeId())), req)
|
||||
assert.Equal(tc.errCode, status.Code(gErr), "UpdateUser(%+v) got error %v, wanted %v", req, gErr, tc.errCode)
|
||||
|
||||
if got != nil {
|
||||
|
||||
Reference in new issue
Block a user