Reorder authz check for sessions (#2042)
* Reorder session authz checks This implements a mechanism for service handlers to pass a repo satisfying an interface into the scope IDs listing function. The purpose of this interface is to allow the same function to perform authorization checking against resources rather than requiring service handlers to fetch all resources in the determined set of scopes and then perform this checking. While this could be done in each service handler, this approach centralizes the logic, which has the nice benefit of removing some boilerplate from service handler list functions that have adapted to it. Tests haven't changed, which is expected -- the function should return exactly what was being returned before, simply faster.
This commit is contained in:
1 parent
0f961eb5f6
commit
b41e983503
16 files changed
+376
-116
No files matched your search
@@ -118,6 +118,13 @@ select expiration_time, connection_limit, current_connection_count
|
||||
from
|
||||
session_connection_limit, session_connection_count;
|
||||
`
|
||||
|
||||
sessionPublicIdList = `
|
||||
select public_id, scope_id, user_id from session
|
||||
%s
|
||||
;
|
||||
`
|
||||
|
||||
sessionList = `
|
||||
select *
|
||||
from
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/boundary"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/db/timestamp"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
@@ -205,6 +206,51 @@ func (r *Repository) LookupSession(ctx context.Context, sessionId string, _ ...O
|
||||
return &session, authzSummary, nil
|
||||
}
|
||||
|
||||
// FetchAuthzProtectedEntitiesByScope implements boundary.AuthzProtectedEntityProvider
|
||||
func (r *Repository) FetchAuthzProtectedEntitiesByScope(ctx context.Context, scopeIds []string) (map[string][]boundary.AuthzProtectedEntity, error) {
|
||||
const op = "session.(Repository).FetchAuthzProtectedEntityInfo"
|
||||
|
||||
var where string
|
||||
var args []interface{}
|
||||
|
||||
inClauseCnt := 0
|
||||
|
||||
switch len(scopeIds) {
|
||||
case 0:
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no scopes given")
|
||||
case 1:
|
||||
inClauseCnt += 1
|
||||
where, args = fmt.Sprintf("where scope_id = @%d", inClauseCnt), append(args, sql.Named("1", scopeIds[0]))
|
||||
default:
|
||||
idsInClause := make([]string, 0, len(scopeIds))
|
||||
for _, id := range scopeIds {
|
||||
inClauseCnt += 1
|
||||
idsInClause, args = append(idsInClause, fmt.Sprintf("@%d", inClauseCnt)), append(args, sql.Named(fmt.Sprintf("%d", inClauseCnt), id))
|
||||
}
|
||||
where = fmt.Sprintf("where scope_id in (%s)", strings.Join(idsInClause, ","))
|
||||
}
|
||||
|
||||
q := sessionPublicIdList
|
||||
query := fmt.Sprintf(q, where)
|
||||
|
||||
rows, err := r.reader.Query(ctx, query, args)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
sessionsMap := map[string][]boundary.AuthzProtectedEntity{}
|
||||
for rows.Next() {
|
||||
var ses Session
|
||||
if err := r.reader.ScanRows(ctx, rows, &ses); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("scan row failed"))
|
||||
}
|
||||
sessionsMap[ses.GetScopeId()] = append(sessionsMap[ses.GetScopeId()], ses)
|
||||
}
|
||||
|
||||
return sessionsMap, nil
|
||||
}
|
||||
|
||||
// ListSessions will sessions. Supports the WithLimit, WithScopeId, WithSessionIds, and WithServerId options.
|
||||
func (r *Repository) ListSessions(ctx context.Context, opt ...Option) ([]*Session, error) {
|
||||
const op = "session.(Repository).ListSessions"
|
||||
@@ -213,25 +259,31 @@ func (r *Repository) ListSessions(ctx context.Context, opt ...Option) ([]*Sessio
|
||||
var args []interface{}
|
||||
|
||||
inClauseCnt := 0
|
||||
if len(opts.withScopeIds) != 0 {
|
||||
switch len(opts.withScopeIds) {
|
||||
case 1:
|
||||
switch len(opts.withScopeIds) {
|
||||
case 0:
|
||||
case 1:
|
||||
inClauseCnt += 1
|
||||
where, args = append(where, fmt.Sprintf("scope_id = @%d", inClauseCnt)), append(args, sql.Named(fmt.Sprintf("%d", inClauseCnt), opts.withScopeIds[0]))
|
||||
default:
|
||||
idsInClause := make([]string, 0, len(opts.withScopeIds))
|
||||
for _, id := range opts.withScopeIds {
|
||||
inClauseCnt += 1
|
||||
where, args = append(where, fmt.Sprintf("scope_id = @%d", inClauseCnt)), append(args, sql.Named("1", opts.withScopeIds[0]))
|
||||
default:
|
||||
idsInClause := make([]string, 0, len(opts.withScopeIds))
|
||||
for _, id := range opts.withScopeIds {
|
||||
inClauseCnt += 1
|
||||
idsInClause, args = append(idsInClause, fmt.Sprintf("@%d", inClauseCnt)), append(args, sql.Named(fmt.Sprintf("%d", inClauseCnt), id))
|
||||
}
|
||||
where = append(where, fmt.Sprintf("scope_id in (%s)", strings.Join(idsInClause, ",")))
|
||||
idsInClause, args = append(idsInClause, fmt.Sprintf("@%d", inClauseCnt)), append(args, sql.Named(fmt.Sprintf("%d", inClauseCnt), id))
|
||||
}
|
||||
where = append(where, fmt.Sprintf("scope_id in (%s)", strings.Join(idsInClause, ",")))
|
||||
}
|
||||
|
||||
if opts.withUserId != "" {
|
||||
inClauseCnt += 1
|
||||
where, args = append(where, fmt.Sprintf("user_id = @%d", inClauseCnt)), append(args, sql.Named(fmt.Sprintf("%d", inClauseCnt), opts.withUserId))
|
||||
}
|
||||
if len(opts.withSessionIds) > 0 {
|
||||
|
||||
switch len(opts.withSessionIds) {
|
||||
case 0:
|
||||
case 1:
|
||||
inClauseCnt += 1
|
||||
where, args = append(where, fmt.Sprintf("s.public_id = @%d", inClauseCnt)), append(args, sql.Named(fmt.Sprintf("%d", inClauseCnt), opts.withSessionIds[0]))
|
||||
default:
|
||||
idsInClause := make([]string, 0, len(opts.withSessionIds))
|
||||
for _, id := range opts.withSessionIds {
|
||||
inClauseCnt += 1
|
||||
@@ -239,6 +291,7 @@ func (r *Repository) ListSessions(ctx context.Context, opt ...Option) ([]*Sessio
|
||||
}
|
||||
where = append(where, fmt.Sprintf("s.public_id in (%s)", strings.Join(idsInClause, ",")))
|
||||
}
|
||||
|
||||
if opts.withServerId != "" {
|
||||
inClauseCnt += 1
|
||||
where, args = append(where, fmt.Sprintf("server_id = @%d", inClauseCnt)), append(args, sql.Named(fmt.Sprintf("%d", inClauseCnt), opts.withServerId))
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/boundary"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/db/timestamp"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
@@ -119,13 +120,22 @@ type Session struct {
|
||||
tableName string `gorm:"-"`
|
||||
}
|
||||
|
||||
func (s *Session) GetPublicId() string {
|
||||
func (s Session) GetPublicId() string {
|
||||
return s.PublicId
|
||||
}
|
||||
|
||||
func (s Session) GetScopeId() string {
|
||||
return s.ScopeId
|
||||
}
|
||||
|
||||
func (s Session) GetUserId() string {
|
||||
return s.UserId
|
||||
}
|
||||
|
||||
var (
|
||||
_ Cloneable = (*Session)(nil)
|
||||
_ db.VetForWriter = (*Session)(nil)
|
||||
_ Cloneable = (*Session)(nil)
|
||||
_ db.VetForWriter = (*Session)(nil)
|
||||
_ boundary.AuthzProtectedEntity = (*Session)(nil)
|
||||
)
|
||||
|
||||
// New creates a new in memory session.
|
||||
|
||||
Reference in new issue
Block a user