Generate new version of SDK resources and Add Tests (#331)
This commit is contained in:
1 parent
96e4b1cdba
commit
c3ecea172d
93 files changed
+3414
-2826
No files matched your search
+6
-244
@@ -16,7 +16,6 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/common"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
|
||||
"github.com/hashicorp/boundary/internal/types/action"
|
||||
"github.com/hashicorp/boundary/internal/types/resource"
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
"github.com/hashicorp/boundary/recovery"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
@@ -149,26 +148,12 @@ func Verify(ctx context.Context, opt ...Option) (ret VerifyResults) {
|
||||
return
|
||||
}
|
||||
|
||||
// check if it's new style
|
||||
if opts.withAction != action.Unknown {
|
||||
v.act = opts.withAction
|
||||
v.res = &perms.Resource{
|
||||
ScopeId: opts.withScopeId,
|
||||
Id: opts.withId,
|
||||
Pin: opts.withPin,
|
||||
Type: opts.withType,
|
||||
}
|
||||
} else {
|
||||
// This remains for legacy
|
||||
v.requestInfo.scopeId = opts.withScopeId
|
||||
if err := v.parseAuthParams(); err != nil {
|
||||
v.logger.Trace("error reading auth parameters from URL", "url", v.requestInfo.Path, "method", v.requestInfo.Method, "error", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if v.res == nil {
|
||||
v.logger.Trace("got nil resource information after decorating auth parameters")
|
||||
return
|
||||
v.act = opts.withAction
|
||||
v.res = &perms.Resource{
|
||||
ScopeId: opts.withScopeId,
|
||||
Id: opts.withId,
|
||||
Pin: opts.withPin,
|
||||
Type: opts.withType,
|
||||
}
|
||||
|
||||
var authResults *perms.ACLResults
|
||||
@@ -192,229 +177,6 @@ func Verify(ctx context.Context, opt ...Option) (ret VerifyResults) {
|
||||
return
|
||||
}
|
||||
|
||||
func (v *verifier) parseAuthParams() error {
|
||||
// Remove trailing and leading slashes
|
||||
trimmedPath := strings.Trim(v.requestInfo.Path, "/")
|
||||
// Remove `v1/`
|
||||
splitPath := strings.Split(strings.TrimPrefix(trimmedPath, "v1/"), "/")
|
||||
splitLen := len(splitPath)
|
||||
|
||||
// It must be at least length 1. If so, figure out new vs. legacy parsing
|
||||
if len(splitPath) == 0 {
|
||||
return fmt.Errorf("parse auth params: invalid path")
|
||||
}
|
||||
|
||||
for i := 1; i < splitLen; i++ {
|
||||
if splitPath[i] == "" {
|
||||
return fmt.Errorf("parse auth params: empty segment found")
|
||||
}
|
||||
}
|
||||
|
||||
// The first segment must be "scopes"
|
||||
if splitPath[0] != "scopes" {
|
||||
return fmt.Errorf("parse auth params: invalid first segment %q", splitPath[0])
|
||||
}
|
||||
|
||||
v.act = action.Unknown
|
||||
v.res = &perms.Resource{
|
||||
// Start out with scope, and replace when we walk backwards if it's
|
||||
// actually something else
|
||||
Type: resource.Scope,
|
||||
}
|
||||
|
||||
// Handle non-custom types. We'll deal with custom types, including list,
|
||||
// after parsing the path.
|
||||
switch v.requestInfo.Method {
|
||||
case "GET":
|
||||
v.act = action.Read
|
||||
case "POST":
|
||||
v.act = action.Create
|
||||
case "PATCH":
|
||||
v.act = action.Update
|
||||
case "DELETE":
|
||||
v.act = action.Delete
|
||||
default:
|
||||
return fmt.Errorf("parse auth params: unknown method %q", v.requestInfo.Method)
|
||||
}
|
||||
|
||||
// Look for a custom action
|
||||
colonSplit := strings.Split(splitPath[splitLen-1], ":")
|
||||
switch len(colonSplit) {
|
||||
case 1:
|
||||
// No custom action specified
|
||||
|
||||
case 2:
|
||||
// Parse and validate the action, then elide it
|
||||
actStr := colonSplit[len(colonSplit)-1]
|
||||
v.act = action.Map[actStr]
|
||||
if v.act == action.Unknown || v.act == action.All {
|
||||
return fmt.Errorf("parse auth params: unknown action %q", actStr)
|
||||
}
|
||||
// Keep going with the logic without the custom action
|
||||
splitPath[splitLen-1] = colonSplit[0]
|
||||
|
||||
default:
|
||||
return fmt.Errorf("parse auth params: unexpected number of colons in last segment %q", colonSplit[len(colonSplit)-1])
|
||||
}
|
||||
|
||||
// Get scope information and handle it in a special case; that is, for
|
||||
// operating on scopes, scope from the request ID, not the path scope
|
||||
switch splitLen {
|
||||
case 1:
|
||||
// We've already validated that this is "scopes"
|
||||
if v.act == action.Read {
|
||||
v.act = action.List
|
||||
}
|
||||
if v.requestInfo.scopeId == "" {
|
||||
return errors.New("parse auth params: missing scope ID information for scopes collection operation")
|
||||
}
|
||||
v.res.ScopeId = v.requestInfo.scopeId
|
||||
return nil
|
||||
|
||||
case 2:
|
||||
id := splitPath[1]
|
||||
// The next segment should be the scope ID, but it takes place not in
|
||||
// its own scope but in the parent scope. Rather than require the user
|
||||
// to provide it, look up the parent.
|
||||
switch {
|
||||
case id == "global", strings.HasPrefix(id, scope.Org.Prefix()):
|
||||
// Org scope parent is always global. Set scope for global
|
||||
// operations to global as well (it's basically acting as its own
|
||||
// parent scope). We want this so that users can e.g. modify the
|
||||
// name or description of the global scope if they have permissions
|
||||
// in the scope.
|
||||
v.res.ScopeId = "global"
|
||||
|
||||
default:
|
||||
// Project case
|
||||
iamRepo, err := v.iamRepoFn()
|
||||
if err != nil {
|
||||
return fmt.Errorf("perform auth check: failed to get iam repo: %w", err)
|
||||
}
|
||||
|
||||
scp, err := iamRepo.LookupScope(v.ctx, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("perform auth check: failed to lookup scope: %w", err)
|
||||
}
|
||||
if scp == nil {
|
||||
return fmt.Errorf("perform auth check: non-existent scope %q", id)
|
||||
}
|
||||
v.res.ScopeId = scp.GetParentId()
|
||||
}
|
||||
v.res.Id = id
|
||||
return nil
|
||||
|
||||
case 3:
|
||||
// If a custom action was being performed within a scope, it will have
|
||||
// been elided above. If the last path segment is now empty, address
|
||||
// this scenario. In this case the action took place _in_ the scope so
|
||||
// it should be bound accordingly. (This is for actions like
|
||||
// /scopes/o_abc/:deauthenticate where the action is _in_ the scope, not
|
||||
// on the scope.)
|
||||
if splitPath[2] == "" {
|
||||
v.res.ScopeId = splitPath[1]
|
||||
v.res.Id = splitPath[1]
|
||||
return nil
|
||||
}
|
||||
|
||||
fallthrough
|
||||
|
||||
default:
|
||||
// In all other cases the scope ID is the next segment
|
||||
v.res.ScopeId = splitPath[1]
|
||||
}
|
||||
|
||||
// Walk backwards. As we walk backwards we look for a pin and figure out if
|
||||
// we're operating on a resource or a collection. The rules for the pin are
|
||||
// as follows:
|
||||
//
|
||||
// * If the last segment is a collection, the pin is the immediately
|
||||
// preceding ID. This does not include scopes since those are permission
|
||||
// boundaries.
|
||||
//
|
||||
// * If the last segment is an ID, the pin is the immediately preceding ID
|
||||
// not including the last segment
|
||||
//
|
||||
// * If at the end of the logic the pin is the id of a scope then there is
|
||||
// no pin. The scopes are already enclosing so a pin is redundant.
|
||||
nextIdIsPin := true
|
||||
// Use an empty string so we can detect if we found anything in this loop.
|
||||
var foundId string
|
||||
var typStr string
|
||||
// We stop at [2] because we've already dealt with the first two segments
|
||||
// (scopes/<scope_id> above.
|
||||
for i := splitLen - 1; i >= 2; i-- {
|
||||
segment := splitPath[i]
|
||||
|
||||
if segment == "" {
|
||||
return fmt.Errorf("parse auth parameters: unexpected empty segment")
|
||||
}
|
||||
|
||||
// If the segment contains whitespace, it's not valid
|
||||
if fields := strings.Fields(segment); len(fields) != 1 || fields[0] != segment {
|
||||
return fmt.Errorf("parse auth params: segment %q contains whitespace", segment)
|
||||
}
|
||||
|
||||
// Collections don't contain underscores; every resource ID does.
|
||||
segmentIsCollection := !strings.Contains(segment, "_")
|
||||
|
||||
// If we see an ID, ensure that it's not the right-most ID; if not, it's
|
||||
// the pin
|
||||
if !segmentIsCollection && i != splitLen-1 && nextIdIsPin {
|
||||
v.res.Pin = segment
|
||||
// By definition this is the last thing we'd be looking for as
|
||||
// scopes were found above, so we can now break out
|
||||
break
|
||||
}
|
||||
|
||||
if typStr == "" {
|
||||
// The resource check takes place inside the type check because if
|
||||
// we've identified the type we have either already identified the
|
||||
// right-most resource ID or we're operating on a collection, so
|
||||
// this prevents us from finding a different ID earlier in the path.
|
||||
// We still work backwards to identify a pin.
|
||||
if foundId == "" && !segmentIsCollection {
|
||||
foundId = segment
|
||||
} else {
|
||||
// Every collection is the plural of the resource type so drop
|
||||
// the last 's'
|
||||
if !strings.HasSuffix(segment, "s") {
|
||||
return fmt.Errorf("parse auth params: invalid collection syntax for %q", segment)
|
||||
}
|
||||
typStr = strings.TrimSuffix(segment, "s")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if foundId != "" {
|
||||
v.res.Id = foundId
|
||||
}
|
||||
|
||||
if typStr != "" {
|
||||
v.res.Type = resource.Map[typStr]
|
||||
if v.res.Type == resource.Unknown {
|
||||
return fmt.Errorf("parse auth params: unknown resource type %q", typStr)
|
||||
}
|
||||
} else {
|
||||
// If we found no other type information, we walked backwards all the
|
||||
// way to the scope boundary, so the type is scope
|
||||
v.res.Type = resource.Scope
|
||||
}
|
||||
|
||||
// If we're operating on a collection (that is, the ID is blank) and it's a
|
||||
// GET, it's actually a list
|
||||
if v.res.Id == "" && v.act == action.Read {
|
||||
v.act = action.List
|
||||
}
|
||||
|
||||
// If the pin ended up being a scope, nil it out
|
||||
if v.res.Pin != "" && v.res.Pin == v.res.ScopeId {
|
||||
v.res.Pin = ""
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v verifier) performAuthCheck() (aclResults *perms.ACLResults, userId string, scopeInfo *scopes.ScopeInfo, retErr error) {
|
||||
// Ensure we return an error by default if we forget to set this somewhere
|
||||
retErr = errors.New("unknown")
|
||||
|
||||
@@ -13,263 +13,11 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/servers"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
|
||||
"github.com/hashicorp/boundary/internal/types/action"
|
||||
"github.com/hashicorp/boundary/internal/types/resource"
|
||||
"github.com/hashicorp/go-hclog"
|
||||
"github.com/kr/pretty"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestHandler_AuthDecoration(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iamRepo, nil
|
||||
}
|
||||
org, proj := iam.TestScopes(t, iamRepo)
|
||||
cases := []struct {
|
||||
name string
|
||||
path string
|
||||
method string
|
||||
action action.Type
|
||||
scope string
|
||||
resource resource.Type
|
||||
id string
|
||||
pin string
|
||||
wantErrContains string
|
||||
}{
|
||||
{
|
||||
name: "global scope, read, with id",
|
||||
path: "/v1/scopes/global/users/u_anon",
|
||||
method: "GET",
|
||||
action: action.Read,
|
||||
scope: "global",
|
||||
resource: resource.User,
|
||||
id: "u_anon",
|
||||
},
|
||||
{
|
||||
name: "global scope, update, with id",
|
||||
path: "/v1/scopes/global/auth-methods/am_1234",
|
||||
method: "PATCH",
|
||||
action: action.Update,
|
||||
scope: "global",
|
||||
resource: resource.AuthMethod,
|
||||
id: "am_1234",
|
||||
},
|
||||
{
|
||||
name: "global scope, delete org",
|
||||
path: "/v1/scopes/o_1234",
|
||||
method: "DELETE",
|
||||
action: action.Delete,
|
||||
scope: "global",
|
||||
resource: resource.Scope,
|
||||
id: "o_1234",
|
||||
},
|
||||
{
|
||||
name: "org scope, delete project",
|
||||
path: fmt.Sprintf("/v1/scopes/%s", proj.GetPublicId()),
|
||||
method: "DELETE",
|
||||
action: action.Delete,
|
||||
scope: org.GetPublicId(),
|
||||
resource: resource.Scope,
|
||||
id: proj.GetPublicId(),
|
||||
},
|
||||
{
|
||||
name: "empty segments",
|
||||
path: "/v1/scopes/o_abc1234/auth-methods//am_1234",
|
||||
wantErrContains: "empty segment found",
|
||||
},
|
||||
{
|
||||
name: "global scope, create",
|
||||
path: "/v1/scopes/global/roles",
|
||||
method: "POST",
|
||||
action: action.Create,
|
||||
scope: "global",
|
||||
resource: resource.Role,
|
||||
},
|
||||
{
|
||||
name: "global, invalid collection syntax",
|
||||
path: "/v1/scope",
|
||||
wantErrContains: `parse auth params: invalid first segment "scope"`,
|
||||
},
|
||||
{
|
||||
name: "invalid scopes segment with id",
|
||||
path: "/v1/scope/o_abc",
|
||||
wantErrContains: `parse auth params: invalid first segment "scope"`,
|
||||
},
|
||||
{
|
||||
name: "org, custom action",
|
||||
path: "/v1/scopes/o_123/auth-methods/am_1234:authenticate",
|
||||
method: "POST",
|
||||
action: action.Authenticate,
|
||||
scope: "o_123",
|
||||
resource: resource.AuthMethod,
|
||||
id: "am_1234",
|
||||
},
|
||||
{
|
||||
name: "unknown action in first segment",
|
||||
path: "/v1/:authentifake",
|
||||
wantErrContains: `parse auth params: invalid first segment ":authentifake"`,
|
||||
},
|
||||
{
|
||||
name: "root, unknown action",
|
||||
path: "/v1/scopes/o_abc/:authentifake",
|
||||
wantErrContains: `parse auth params: unknown action "authentifake"`,
|
||||
},
|
||||
{
|
||||
name: "root, unknown empty action",
|
||||
path: "/v1/:",
|
||||
wantErrContains: `parse auth params: invalid first segment ":"`,
|
||||
},
|
||||
{
|
||||
name: "root, invalid method",
|
||||
path: "/v1/scopes/o_abc/:authenticate",
|
||||
method: "FOOBAR",
|
||||
wantErrContains: `unknown method "FOOBAR"`,
|
||||
},
|
||||
{
|
||||
name: "root, wrong number of colons",
|
||||
path: "/v1/scopes/o_abc/:auth:enticate",
|
||||
wantErrContains: "unexpected number of colons",
|
||||
},
|
||||
{
|
||||
name: "root, action on global",
|
||||
path: "/v1/scopes/global",
|
||||
method: "GET",
|
||||
action: action.Read,
|
||||
scope: "global",
|
||||
resource: resource.Scope,
|
||||
id: "global",
|
||||
},
|
||||
{
|
||||
name: "org scope, valid",
|
||||
path: "/v1/scopes/o_abc123/auth-methods",
|
||||
method: "POST",
|
||||
action: action.Create,
|
||||
scope: "o_abc123",
|
||||
resource: resource.AuthMethod,
|
||||
},
|
||||
{
|
||||
name: "project scope, valid",
|
||||
path: "/v1/scopes/p_1234/host-catalogs",
|
||||
method: "POST",
|
||||
action: action.Create,
|
||||
scope: "p_1234",
|
||||
resource: resource.HostCatalog,
|
||||
},
|
||||
{
|
||||
name: "project scope, action on project",
|
||||
path: "/v1/scopes/p_1234/:deauthenticate",
|
||||
method: "POST",
|
||||
action: action.Deauthenticate,
|
||||
scope: "p_1234",
|
||||
resource: resource.Scope,
|
||||
id: "p_1234",
|
||||
},
|
||||
{
|
||||
name: "global scope, action on org, token scope",
|
||||
path: "/v1/scopes/o_1234:set-principals",
|
||||
method: "POST",
|
||||
action: action.SetPrincipals,
|
||||
scope: "global",
|
||||
resource: resource.Scope,
|
||||
id: "o_1234",
|
||||
},
|
||||
{
|
||||
name: "global action, valid",
|
||||
path: "/v1/scopes/global/:read",
|
||||
action: action.Read,
|
||||
scope: "global",
|
||||
resource: resource.Scope,
|
||||
id: "global",
|
||||
},
|
||||
{
|
||||
name: "top level, invalid",
|
||||
path: "/v1/",
|
||||
wantErrContains: `invalid first segment "v1"`,
|
||||
},
|
||||
{
|
||||
name: "non-api path",
|
||||
path: "/",
|
||||
wantErrContains: `parse auth params: invalid first segment ""`,
|
||||
},
|
||||
{
|
||||
name: "project scope, pinning collection",
|
||||
path: "/v1/scopes/p_1234/host-catalogs/hc_1234/host-sets",
|
||||
action: action.List,
|
||||
scope: "p_1234",
|
||||
pin: "hc_1234",
|
||||
resource: resource.HostSet,
|
||||
},
|
||||
{
|
||||
name: "project scope, pinning collection, custom action",
|
||||
path: "/v1/scopes/p_1234/host-catalogs/hc_1234/host-sets:create",
|
||||
action: action.Create,
|
||||
scope: "p_1234",
|
||||
pin: "hc_1234",
|
||||
resource: resource.HostSet,
|
||||
},
|
||||
{
|
||||
name: "project scope, pinning id",
|
||||
path: "/v1/scopes/p_1234/host-catalogs/hc_1234/host-sets/hs_abc",
|
||||
action: action.Read,
|
||||
id: "hs_abc",
|
||||
scope: "p_1234",
|
||||
pin: "hc_1234",
|
||||
resource: resource.HostSet,
|
||||
},
|
||||
{
|
||||
name: "project scope, pinning id, custom action",
|
||||
path: "/v1/scopes/p_1234/host-catalogs/hc_1234/host-sets/hs_abc:update",
|
||||
action: action.Update,
|
||||
id: "hs_abc",
|
||||
scope: "p_1234",
|
||||
pin: "hc_1234",
|
||||
resource: resource.HostSet,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
require, assert := require.New(t), assert.New(t)
|
||||
if tc.method == "" {
|
||||
tc.method = "GET"
|
||||
}
|
||||
req, err := http.NewRequest(tc.method, fmt.Sprintf("http://127.0.0.1:9200:%s", tc.path), nil)
|
||||
require.NoError(err)
|
||||
|
||||
v := &verifier{
|
||||
requestInfo: RequestInfo{
|
||||
Path: req.URL.Path,
|
||||
Method: tc.method,
|
||||
},
|
||||
iamRepoFn: iamRepoFn,
|
||||
}
|
||||
|
||||
err = v.parseAuthParams()
|
||||
if tc.wantErrContains != "" {
|
||||
require.Error(err)
|
||||
assert.Contains(err.Error(), tc.wantErrContains, err.Error())
|
||||
return
|
||||
}
|
||||
require.NoError(err, pretty.Sprint(v))
|
||||
|
||||
if tc.path == "/" {
|
||||
return
|
||||
}
|
||||
|
||||
require.NotNil(v.res)
|
||||
require.NotEqual(action.Unknown, v.act)
|
||||
assert.Equal(tc.scope, v.res.ScopeId, "scope "+pretty.Sprint(v))
|
||||
assert.Equal(tc.action, v.act, "action "+pretty.Sprint(v))
|
||||
assert.Equal(tc.resource, v.res.Type, "type "+pretty.Sprint(v))
|
||||
assert.Equal(tc.id, v.res.Id, "id "+pretty.Sprint(v))
|
||||
assert.Equal(tc.pin, v.res.Pin, "pin "+pretty.Sprint(v))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthTokenAuthenticator(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
|
||||
@@ -288,7 +288,7 @@ func TestRepository_ChangePassword(t *testing.T) {
|
||||
wantIsErr: db.ErrInvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "invalid-no-old-password",
|
||||
name: "invalid-no-current-password",
|
||||
args: args{
|
||||
acctId: acct.PublicId,
|
||||
old: "",
|
||||
@@ -325,7 +325,7 @@ func TestRepository_ChangePassword(t *testing.T) {
|
||||
wantIsErr: db.ErrRecordNotFound,
|
||||
},
|
||||
{
|
||||
name: "auth-failure-wrong-old-password",
|
||||
name: "auth-failure-wrong-current-password",
|
||||
args: args{
|
||||
acctId: acct.PublicId,
|
||||
old: "wrong-password",
|
||||
@@ -455,13 +455,13 @@ func TestRepository_SetPassword(t *testing.T) {
|
||||
createAcct func(string) *Account
|
||||
}{
|
||||
{
|
||||
name: "valid-new-password-no-old-password",
|
||||
name: "valid-new-password-no-current-password",
|
||||
createAcct: createAccount("validnewpwnoold"),
|
||||
oldPw: "",
|
||||
newPw: "abcdefghijk",
|
||||
},
|
||||
{
|
||||
name: "valid-new-password-delete-old-password",
|
||||
name: "valid-new-password-delete-current-password",
|
||||
createAcct: createAccount("validnewpwyesold"),
|
||||
oldPw: origPasswd,
|
||||
newPw: "abcdefghijk",
|
||||
|
||||
Reference in new issue
Block a user