Add read:self and cancel:self to sessions; add dev unpriv user (#923)

* Sessions support for read:self and cancel:self permissions. This is automatic with fallback; no API change is needed.

* Strips out values from listing if only self actions are allowed on the resource and the user ID is different. This prevents a default ACL of id=*;type=session;actions=read:self,cancel:self from causing all values to be visible when listing.

* Adds an unprivileged user to development/testing instances. This user has only default permissions plus the ability to authorize sessions in the default target.
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2021-02-11 13:14:18 -05:00
1 parent d279a906ce
commit 22462cdd84
28 files changed
+626 -196

No files matched your search

@@ -717,7 +717,7 @@ func TestCreate(t *testing.T) {
require.NoError(err)
switch tc.scopeId {
case defaultOrg.PublicId:
require.Len(roles, 1)
require.Len(roles, 2)
case "global":
require.Len(roles, 2)
}
@@ -726,7 +726,9 @@ func TestCreate(t *testing.T) {
case "Administration":
assert.Equal(fmt.Sprintf("Role created for administration of scope %s by user %s at its creation time", got.GetItem().GetId(), userId), role.GetDescription())
case "Login and Default Grants":
assert.Equal(fmt.Sprintf("Role created for login capability and account self-management for users of scope %s at its creation time", got.GetItem().GetId()), role.GetDescription())
assert.Equal(fmt.Sprintf("Role created for login capability, account self-management, and other default grants for users of scope %s at its creation time", got.GetItem().GetId()), role.GetDescription())
case "Default Grants":
assert.Equal(fmt.Sprintf("Role created to provide default grants to users of scope %s at its creation time", got.GetItem().GetId()), role.GetDescription())
default:
t.Fatal("unexpected role name", role.GetName())
}
@@ -25,7 +25,9 @@ var (
// individual resources
IdActions = action.ActionSet{
action.Read,
action.ReadSelf,
action.Cancel,
action.CancelSelf,
}
// CollectionActions contains the set of actions that can be performed on
@@ -61,7 +63,7 @@ func (s Service) GetSession(ctx context.Context, req *pbs.GetSessionRequest) (*p
if err := validateGetRequest(req); err != nil {
return nil, err
}
authResults := s.authResult(ctx, req.GetId(), action.Read)
authResults := s.authResult(ctx, req.GetId(), action.ReadSelf)
if authResults.Error != nil {
return nil, authResults.Error
}
@@ -69,8 +71,24 @@ func (s Service) GetSession(ctx context.Context, req *pbs.GetSessionRequest) (*p
if err != nil {
return nil, err
}
authzdActions := authResults.FetchActionSetForId(ctx, ses.Id, IdActions)
// Check to see if we need to verify Read vs. just ReadSelf
if ses.GetUserId() != authResults.UserId {
var found bool
for _, v := range authzdActions {
if v == action.Read {
found = true
break
}
}
if !found {
return nil, handlers.ForbiddenError()
}
}
ses.Scope = authResults.Scope
ses.AuthorizedActions = authResults.FetchActionSetForId(ctx, ses.Id, IdActions).Strings()
ses.AuthorizedActions = authzdActions.Strings()
return &pbs.GetSessionResponse{Item: ses}, nil
}
@@ -101,9 +119,19 @@ func (s Service) ListSessions(ctx context.Context, req *pbs.ListSessionsRequest)
for _, item := range seslist {
item.Scope = scopeInfoMap[item.GetScopeId()]
res.ScopeId = item.Scope.Id
item.AuthorizedActions = authResults.FetchActionSetForId(ctx, item.Id, IdActions, auth.WithResource(res)).Strings()
if len(item.AuthorizedActions) > 0 {
finalItems = append(finalItems, item)
authorizedActions := authResults.FetchActionSetForId(ctx, item.Id, IdActions, auth.WithResource(res))
if len(authorizedActions) > 0 {
onlySelf := true
for _, v := range authorizedActions {
if v != action.ReadSelf && v != action.CancelSelf {
onlySelf = false
break
}
}
if !onlySelf || item.GetUserId() == authResults.UserId {
item.AuthorizedActions = authorizedActions.Strings()
finalItems = append(finalItems, item)
}
}
}
@@ -115,16 +143,36 @@ func (s Service) CancelSession(ctx context.Context, req *pbs.CancelSessionReques
if err := validateCancelRequest(req); err != nil {
return nil, err
}
authResults := s.authResult(ctx, req.GetId(), action.Cancel)
authResults := s.authResult(ctx, req.GetId(), action.CancelSelf)
if authResults.Error != nil {
return nil, authResults.Error
}
ses, err := s.cancelInRepo(ctx, req.GetId(), req.GetVersion())
ses, err := s.getFromRepo(ctx, req.GetId())
if err != nil {
return nil, err
}
authzdActions := authResults.FetchActionSetForId(ctx, ses.Id, IdActions)
// Check to see if we need to verify Read vs. just ReadSelf
if ses.GetUserId() != authResults.UserId {
var found bool
for _, v := range authzdActions {
if v == action.Cancel {
found = true
break
}
}
if !found {
return nil, handlers.ForbiddenError()
}
}
ses, err = s.cancelInRepo(ctx, req.GetId(), req.GetVersion())
if err != nil {
return nil, err
}
ses.Scope = authResults.Scope
ses.AuthorizedActions = authResults.FetchActionSetForId(ctx, ses.Id, IdActions).Strings()
ses.AuthorizedActions = authzdActions.Strings()
return &pbs.CancelSessionResponse{Item: ses}, nil
}
@@ -196,7 +244,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type) auth.
res.Error = handlers.NotFoundError()
return res
}
case action.Read, action.Cancel:
case action.Read, action.ReadSelf, action.Cancel, action.CancelSelf:
repo, err := s.repoFn()
if err != nil {
res.Error = err
@@ -81,7 +81,7 @@ func TestGetSession(t *testing.T) {
States: []*pb.SessionState{{Status: session.StatusPending.String(), StartTime: sess.CreateTime.GetTimestamp()}},
Certificate: sess.Certificate,
Type: target.TcpSubType.String(),
AuthorizedActions: []string{"read", "cancel"},
AuthorizedActions: []string{"read", "read:self", "cancel", "cancel:self"},
}
cases := []struct {
@@ -210,7 +210,7 @@ func TestList(t *testing.T) {
States: states,
Certificate: sess.Certificate,
Type: target.TcpSubType.String(),
AuthorizedActions: []string{"read", "cancel"},
AuthorizedActions: []string{"read", "read:self", "cancel", "cancel:self"},
})
totalSession = append(totalSession, wantSession[i])
@@ -245,7 +245,7 @@ func TestList(t *testing.T) {
States: states,
Certificate: sess.Certificate,
Type: target.TcpSubType.String(),
AuthorizedActions: []string{"read", "cancel"},
AuthorizedActions: []string{"read", "read:self", "cancel", "cancel:self"},
})
}
@@ -362,7 +362,7 @@ func TestCancel(t *testing.T) {
Status: session.StatusCanceling.String(),
Certificate: sess.Certificate,
Type: target.TcpSubType.String(),
AuthorizedActions: []string{"read", "cancel"},
AuthorizedActions: []string{"read", "read:self", "cancel", "cancel:self"},
}
version := wireSess.GetVersion()
+3 -9
View File
@@ -15,18 +15,12 @@ import (
func TestAuthenticationMulti(t *testing.T) {
assert, require := assert.New(t), require.New(t)
amId := "ampw_1234567890"
user := "user"
password := "passpass"
logger := hclog.New(&hclog.LoggerOptions{
Level: hclog.Trace,
})
c1 := controller.NewTestController(t, &controller.TestControllerOpts{
DefaultAuthMethodId: amId,
DefaultLoginName: user,
DefaultPassword: password,
Logger: logger.Named("c1"),
Logger: logger.Named("c1"),
})
defer c1.Shutdown()
@@ -36,14 +30,14 @@ func TestAuthenticationMulti(t *testing.T) {
defer c2.Shutdown()
auth := authmethods.NewClient(c1.Client())
token1Result, err := auth.Authenticate(c1.Context(), amId, map[string]interface{}{"login_name": user, "password": password})
token1Result, err := auth.Authenticate(c1.Context(), c1.Server().DevAuthMethodId, map[string]interface{}{"login_name": c1.Server().DevLoginName, "password": c1.Server().DevPassword})
require.Nil(err)
token1 := token1Result.Item
require.NotNil(token1)
time.Sleep(5 * time.Second)
auth = authmethods.NewClient(c2.Client())
token2Result, err := auth.Authenticate(c2.Context(), amId, map[string]interface{}{"login_name": user, "password": password})
token2Result, err := auth.Authenticate(c2.Context(), c2.Server().DevAuthMethodId, map[string]interface{}{"login_name": c2.Server().DevLoginName, "password": c2.Server().DevPassword})
require.Nil(err)
token2 := token2Result.Item
require.NotNil(token2)
+49 -5
View File
@@ -18,6 +18,7 @@ import (
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/servers"
"github.com/hashicorp/boundary/sdk/strutil"
"github.com/hashicorp/go-hclog"
wrapping "github.com/hashicorp/go-kms-wrapping"
"github.com/hashicorp/vault/sdk/helper/base62"
@@ -25,9 +26,11 @@ import (
)
const (
DefaultTestAuthMethodId = "ampw_1234567890"
DefaultTestLoginName = "user"
DefaultTestPassword = "passpass"
DefaultTestAuthMethodId = "ampw_1234567890"
DefaultTestLoginName = "admin"
DefaultTestUnprivilegedLoginName = "user"
DefaultTestPassword = "passpass"
DefaultTestUserId = "u_1234567890"
)
// TestController wraps a base.Server and Controller to provide a
@@ -46,6 +49,11 @@ type TestController struct {
opts *TestControllerOpts
}
// Server returns the underlying base server
func (tc *TestController) Server() *base.Server {
return tc.b
}
// Controller returns the underlying controller
func (tc *TestController) Controller() *Controller {
return tc.c
@@ -135,6 +143,26 @@ func (tc *TestController) Token() *authtokens.AuthToken {
return token.Item
}
func (tc *TestController) UnprivilegedToken() *authtokens.AuthToken {
if tc.opts.DisableAuthMethodCreation {
tc.t.Error("no default auth method ID configured")
return nil
}
token, err := authmethods.NewClient(tc.Client()).Authenticate(
tc.Context(),
tc.b.DevAuthMethodId,
map[string]interface{}{
"login_name": tc.b.DevUnprivilegedLoginName,
"password": tc.b.DevUnprivilegedPassword,
},
)
if err != nil {
tc.t.Error(fmt.Errorf("error logging in: %w", err))
return nil
}
return token.Item
}
func (tc *TestController) addrs(purpose string) []string {
var prefix string
switch purpose {
@@ -242,10 +270,13 @@ type TestControllerOpts struct {
// DefaultAuthMethodId is the default auth method ID to use, if set.
DefaultAuthMethodId string
// DefaultLoginName is the login name used when creating the default account.
// DefaultLoginName is the login name used when creating the default admin account.
DefaultLoginName string
// DefaultPassword is the password used when creating the default account.
// DefaultUnprivilegedLoginName is the login name used when creating the default unprivileged account.
DefaultUnprivilegedLoginName string
// DefaultPassword is the password used when creating the default accounts.
DefaultPassword string
// DisableInitialLoginRoleCreation can be set true to disable creating the
@@ -360,10 +391,17 @@ func NewTestController(t *testing.T, opts *TestControllerOpts) *TestController {
} else {
tc.b.DevLoginName = DefaultTestLoginName
}
if opts.DefaultUnprivilegedLoginName != "" {
tc.b.DevUnprivilegedLoginName = opts.DefaultUnprivilegedLoginName
} else {
tc.b.DevUnprivilegedLoginName = DefaultTestUnprivilegedLoginName
}
if opts.DefaultPassword != "" {
tc.b.DevPassword = opts.DefaultPassword
tc.b.DevUnprivilegedPassword = opts.DefaultPassword
} else {
tc.b.DevPassword = DefaultTestPassword
tc.b.DevUnprivilegedPassword = DefaultTestPassword
}
// Start a logger
@@ -396,7 +434,11 @@ func NewTestController(t *testing.T, opts *TestControllerOpts) *TestController {
tc.b.DevProjectId = "p_" + suffix
tc.b.DevTargetId = "ttcp_" + suffix
tc.b.DevUserId = "u_" + suffix
tc.b.DevUnprivilegedUserId = "u_" + strutil.Reverse(strings.TrimPrefix(tc.b.DevUserId, "u_"))
} else {
tc.b.DevUserId = DefaultTestUserId
}
tc.b.DevUnprivilegedUserId = "u_" + strutil.Reverse(strings.TrimPrefix(tc.b.DevUserId, "u_"))
// Set up KMSes
switch {
@@ -509,6 +551,8 @@ func (tc *TestController) AddClusterControllerMember(t *testing.T, opts *TestCon
RecoveryKms: tc.c.conf.RecoveryKms,
Name: opts.Name,
Logger: tc.c.conf.Logger,
DefaultLoginName: tc.b.DevLoginName,
DefaultPassword: tc.b.DevPassword,
DisableKmsKeyCreation: true,
DisableAuthMethodCreation: true,
}