Adding principal custom methods to roles handler. (#180)
* Adding Add/Set/Remove principals API calls. * Add SDK custom methods. Co-authored-by: Jeff Mitchell <jeffrey.mitchell@gmail.com>
This commit is contained in:
1 parent
136e923583
commit
1ddc59f81e
17 files changed
+3540
-143
No files matched your search
@@ -0,0 +1,107 @@
|
||||
package roles
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
)
|
||||
|
||||
func (s Role) AddPrincipals(ctx context.Context, groups, users []string) (*Role, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in AddPrincipals request")
|
||||
}
|
||||
// We assume that the client provided has the org and optionally the project id of the request.
|
||||
|
||||
body := map[string]interface{}{
|
||||
"group_ids": groups,
|
||||
"user_ids": users,
|
||||
"version": s.Version,
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "POST", fmt.Sprintf("roles/%s:add-principals", s.Id), body)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating AddPrincipals request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during AddPrincipals call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Role)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding AddPrincipals repsonse: %w", err)
|
||||
}
|
||||
|
||||
target.Client = s.Client
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (s Role) SetPrincipals(ctx context.Context, groups, users []string) (*Role, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in SetPrincipals request")
|
||||
}
|
||||
// We assume that the client provided has the org and optionally the project id of the request.
|
||||
|
||||
body := map[string]interface{}{
|
||||
"group_ids": groups,
|
||||
"user_ids": users,
|
||||
"version": s.Version,
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "POST", fmt.Sprintf("roles/%s:set-principals", s.Id), body)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating SetPrincipals request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during SetPrincipals call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Role)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding SetPrincipals repsonse: %w", err)
|
||||
}
|
||||
|
||||
target.Client = s.Client
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
|
||||
func (s Role) RemovePrincipals(ctx context.Context, groups, users []string) (*Role, *api.Error, error) {
|
||||
if s.Client == nil {
|
||||
return nil, nil, fmt.Errorf("nil client in RemovePrincipals request")
|
||||
}
|
||||
// We assume that the client provided has the org and optionally the project id of the request.
|
||||
|
||||
body := map[string]interface{}{
|
||||
"group_ids": groups,
|
||||
"user_ids": users,
|
||||
"version": s.Version,
|
||||
}
|
||||
|
||||
req, err := s.Client.NewRequest(ctx, "POST", fmt.Sprintf("roles/%s:remove-principals", s.Id), body)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error creating RemovePrincipals request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := s.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error performing client request during RemovePrincipals call: %w", err)
|
||||
}
|
||||
|
||||
target := new(Role)
|
||||
apiErr, err := resp.Decode(target)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("error decoding RemovePrincipals repsonse: %w", err)
|
||||
}
|
||||
|
||||
target.Client = s.Client
|
||||
|
||||
return target, apiErr, nil
|
||||
}
|
||||
@@ -31,6 +31,18 @@ type Role struct {
|
||||
UpdatedTime time.Time `json:"updated_time,omitempty"`
|
||||
// Whether the resource is disabled
|
||||
Disabled *bool `json:"disabled,omitempty"`
|
||||
// The version can be used in subsiquent write requests to ensure this resource
|
||||
// has not changed and to fail the write if it has.
|
||||
// Output only.
|
||||
Version *uint32 `json:"version,omitempty"`
|
||||
// The principals that are assigned this role.
|
||||
// Output only.
|
||||
UserIds []string `json:"user_ids,omitempty"`
|
||||
// Output only.
|
||||
GroupIds []string `json:"group_ids,omitempty"`
|
||||
// The grants that this role provides for it's principals.
|
||||
// Output only.
|
||||
Grants []string `json:"grants,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Role) SetDefault(key string) {
|
||||
|
||||
@@ -7,8 +7,10 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/groups"
|
||||
"github.com/hashicorp/watchtower/api/roles"
|
||||
"github.com/hashicorp/watchtower/api/scopes"
|
||||
"github.com/hashicorp/watchtower/api/users"
|
||||
"github.com/hashicorp/watchtower/internal/iam"
|
||||
"github.com/hashicorp/watchtower/internal/servers/controller"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -16,6 +18,7 @@ import (
|
||||
)
|
||||
|
||||
type roleCrud interface {
|
||||
CreateGroup(context.Context, *groups.Group) (*groups.Group, *api.Error, error)
|
||||
CreateRole(context.Context, *roles.Role) (*roles.Role, *api.Error, error)
|
||||
ReadRole(context.Context, *roles.Role) (*roles.Role, *api.Error, error)
|
||||
UpdateRole(context.Context, *roles.Role) (*roles.Role, *api.Error, error)
|
||||
@@ -23,6 +26,83 @@ type roleCrud interface {
|
||||
ListRoles(ctx context.Context) ([]*roles.Role, *api.Error, error)
|
||||
}
|
||||
|
||||
func TestCustom(t *testing.T) {
|
||||
tc := controller.NewTestController(t, nil)
|
||||
defer tc.Shutdown()
|
||||
|
||||
client := tc.Client()
|
||||
org := &scopes.Organization{
|
||||
Client: client,
|
||||
}
|
||||
proj, apiErr, err := org.CreateProject(context.Background(), &scopes.Project{})
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, apiErr)
|
||||
|
||||
user, apiErr, err := org.CreateUser(context.Background(), &users.User{})
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, apiErr)
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
scope roleCrud
|
||||
}{
|
||||
{
|
||||
name: "org",
|
||||
scope: org,
|
||||
},
|
||||
{
|
||||
name: "proj",
|
||||
scope: proj,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
require := require.New(t)
|
||||
ctx := context.Background()
|
||||
g, apiErr, err := tc.scope.CreateGroup(ctx, &groups.Group{})
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
require.NotNil(g)
|
||||
|
||||
r, apiErr, err := tc.scope.CreateRole(ctx, &roles.Role{Name: api.String("foo")})
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr)
|
||||
require.NotNil(r)
|
||||
|
||||
updatedRole, apiErr, err := r.AddPrincipals(ctx, []string{g.Id}, nil)
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr, "Got error ", errorMessage(apiErr))
|
||||
assert.Equal(t, *updatedRole.Version, (*r.Version)+1)
|
||||
assert.Contains(t, updatedRole.GroupIds, g.Id)
|
||||
assert.Empty(t, updatedRole.UserIds)
|
||||
|
||||
r = updatedRole
|
||||
updatedRole, apiErr, err = r.SetPrincipals(ctx, nil, []string{user.Id})
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr, "Got error ", errorMessage(apiErr))
|
||||
assert.Equal(t, *updatedRole.Version, (*r.Version)+1)
|
||||
assert.Empty(t, updatedRole.GroupIds)
|
||||
assert.Contains(t, updatedRole.UserIds, user.Id)
|
||||
|
||||
r = updatedRole
|
||||
updatedRole, apiErr, err = r.RemovePrincipals(ctx, nil, []string{user.Id})
|
||||
require.NoError(err)
|
||||
require.Nil(apiErr, "Got error ", errorMessage(apiErr))
|
||||
assert.Equal(t, *updatedRole.Version, (*r.Version)+1)
|
||||
assert.Empty(t, updatedRole.GroupIds)
|
||||
assert.Empty(t, updatedRole.UserIds)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func errorMessage(in *api.Error) string {
|
||||
if in == nil {
|
||||
return ""
|
||||
}
|
||||
return *in.Message
|
||||
}
|
||||
|
||||
func TestRole_List(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
tc := controller.NewTestController(t, nil)
|
||||
|
||||
Reference in new issue
Block a user