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:
Jeff Mitchell authored and GitHub committed 2020-07-22 15:10:44 -04:00
1 parent 2bbc1163c4
commit e8325a9f59
17 files changed
+372 -243

No files matched your search

+44 -27
View File
@@ -43,11 +43,12 @@ type RequestInfo struct {
Token string
TokenFormat TokenFormat
// This is used for operations on the scopes collection
scopeIdOverride string
// The following are useful for tests
DisableAuthzFailures bool
DisableAuthEntirely bool
scopeIdOverride string
parentScopeIdOverride string
DisableAuthzFailures bool
DisableAuthEntirely bool
}
type VerifyResults struct {
@@ -88,7 +89,7 @@ func NewVerifierContext(ctx context.Context,
// may come from the URL and may come from the token) and whether or not to
// proceed, e.g. whether the authn/authz check resulted in failure. If an error
// occurs it's logged to the system log.
func Verify(ctx context.Context) (ret VerifyResults) {
func Verify(ctx context.Context, opt ...Option) (ret VerifyResults) {
v, ok := ctx.Value(verifierKey).(*verifier)
if !ok {
// We don't have a logger yet and this should never happen in any
@@ -106,11 +107,12 @@ func Verify(ctx context.Context) (ret VerifyResults) {
case strings.HasPrefix(ret.Scope.Id, scope.Project.Prefix()):
ret.Scope.Type = scope.Project.String()
}
ret.Scope.ParentScopeId = v.requestInfo.parentScopeIdOverride
ret.Valid = true
return
}
v.ctx = ctx
opts := getOpts(opt...)
v.requestInfo.scopeIdOverride = 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
@@ -204,25 +206,50 @@ func (v *verifier) parseAuthParams() error {
}
// Get scope information and handle it in a special case; that is, for
// operating on scopes, we use the token scope, not the path scope
// 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
}
// We're operating on the scopes collection. Set the scope ID to
// "token", which will be a signal to read it from the token.
v.res.ScopeId = "token"
// TODO: check for an override
if v.requestInfo.scopeIdOverride == "" {
return errors.New("parse auth params: missing scope ID information for scopes collection operation")
}
v.res.ScopeId = v.requestInfo.scopeIdOverride
return nil
case 2:
// The next segment should be the scope ID, and we still need to look up
// the actual request scopefrom the token like in the case above.
v.res.ScopeId = "token"
// TODO: check for an override
v.res.Id = splitPath[1]
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:
@@ -353,16 +380,6 @@ func (v verifier) performAuthCheck() (aclResults *perms.ACLResults, userId strin
}
if at != nil {
userId = at.GetIamUserId()
if v.res.ScopeId == "token" {
v.res.ScopeId = at.ScopeId
}
}
// If no token was found, put at global scope. In the future we can allow an
// override as part of the request parameter. Then check that we actually
// have a valid scope.
if v.res.ScopeId == "token" {
v.res.ScopeId = "global"
}
// Fetch and parse grants for this user ID (which may include grants for
@@ -418,7 +435,7 @@ func (v verifier) performAuthCheck() (aclResults *perms.ACLResults, userId strin
return
}
// getTokenFromRequest pulls the token from either the Authorization header or
// GetTokenFromRequest pulls the token from either the Authorization header or
// split cookies and parses it. If it cannot be parsed successfully, the issue
// is logged and we return blank, so logic will continue as the anonymous user.
// The public ID and token are returned along with the token format.
+31 -3
View File
@@ -18,6 +18,15 @@ import (
)
func TestHandler_AuthDecoration(t *testing.T) {
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
wrapper := db.TestWrapper(t)
iamRepo, err := iam.NewRepository(rw, rw, wrapper)
require.NoError(t, err)
iamRepoFn := func() (*iam.Repository, error) {
return iamRepo, nil
}
org, proj := iam.TestScopes(t, conn)
cases := []struct {
name string
path string
@@ -48,14 +57,23 @@ func TestHandler_AuthDecoration(t *testing.T) {
id: "am_1234",
},
{
name: "global scope, delete, token scope",
name: "global scope, delete org",
path: "/v1/scopes/o_1234",
method: "DELETE",
action: action.Delete,
scope: "token",
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",
@@ -114,6 +132,15 @@ func TestHandler_AuthDecoration(t *testing.T) {
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",
@@ -144,7 +171,7 @@ func TestHandler_AuthDecoration(t *testing.T) {
path: "/v1/scopes/o_1234:set-principals",
method: "POST",
action: action.SetPrincipals,
scope: "token",
scope: "global",
resource: resource.Scope,
id: "o_1234",
},
@@ -222,6 +249,7 @@ func TestHandler_AuthDecoration(t *testing.T) {
Path: req.URL.Path,
Method: tc.method,
},
iamRepoFn: iamRepoFn,
}
err = v.parseAuthParams()
+29
View File
@@ -0,0 +1,29 @@
package auth
func getOpts(opt ...Option) options {
opts := getDefaultOptions()
for _, o := range opt {
o(&opts)
}
return opts
}
// Option - how Options are passed as arguments
type Option func(*options)
// options = how options are represented
type options struct {
withScopeId string
}
func getDefaultOptions() options {
return options{
withScopeId: "",
}
}
func WithScopeId(id string) Option {
return func(o *options) {
o.withScopeId = id
}
}
+2 -39
View File
@@ -3,46 +3,9 @@ package auth
import "context"
// DisabledAuthTestContext is meant for testing, and uses a context that has auth checking entirely disabled
func DisabledAuthTestContext(opt ...TestOption) context.Context {
func DisabledAuthTestContext(opt ...Option) context.Context {
reqInfo := RequestInfo{DisableAuthEntirely: true}
opts := getOpts(opt...)
reqInfo.scopeIdOverride = opts.withTestScopeId
reqInfo.parentScopeIdOverride = opts.withTestParentScopeId
reqInfo.scopeIdOverride = opts.withScopeId
return NewVerifierContext(context.Background(), nil, nil, nil, reqInfo)
}
func getOpts(opt ...TestOption) options {
opts := getDefaultOptions()
for _, o := range opt {
o(&opts)
}
return opts
}
// Option - how Options are passed as arguments
type TestOption func(*options)
// options = how options are represented
type options struct {
withTestScopeId string
withTestParentScopeId string
}
func getDefaultOptions() options {
return options{
withTestScopeId: "",
withTestParentScopeId: "",
}
}
func WithTestScopeId(id string) TestOption {
return func(o *options) {
o.withTestScopeId = id
}
}
func withTestParentScopeId(id string) TestOption {
return func(o *options) {
o.withTestParentScopeId = id
}
}