Grant management added to Roles API and SDK (#185)
* Adding grants related methods to Roles. * Adding grants back after the merge of Principals. * Adding tests for Grant related custom methods. * Improving api handler methods. * Fixing tests and sdk field. * Adding todos for adding principals and grants to List and Update Role responses. * Fix up Set* tests and fix SetPrincipals bug. * Making AddPrincipals and RemovePrincipals similar to the tests for AddGrants and RemoveGrants. * Add JSON grants to output (#187) I also changed canonical_grants to grants_canonical and used grants_json. I believe that when members are simply different formats of other members this is the preferred way to have it in the API. Let me know if you disagree and we can dig in more. * Regenerate the API role resource. Co-authored-by: Jeff Mitchell <jeffrey.mitchell@gmail.com>
This commit is contained in:
1 parent
260703febe
commit
847ca1cc78
19 files changed
+3321
-732
No files matched your search
@@ -141,6 +141,12 @@ func TestGrpcGatewayRouting_CustomActions(t *testing.T) {
|
||||
"v1/orgs/someid/projects/p_something/roles/r_anotherid:add-principals",
|
||||
"v1/orgs/someid/projects/p_something/roles/r_anotherid:set-principals",
|
||||
"v1/orgs/someid/projects/p_something/roles/r_anotherid:remove-principals",
|
||||
"v1/orgs/someid/roles/r_anotherid:add-grants",
|
||||
"v1/orgs/someid/roles/r_anotherid:set-grants",
|
||||
"v1/orgs/someid/roles/r_anotherid:remove-grants",
|
||||
"v1/orgs/someid/projects/p_something/roles/r_anotherid:add-grants",
|
||||
"v1/orgs/someid/projects/p_something/roles/r_anotherid:set-grants",
|
||||
"v1/orgs/someid/projects/p_something/roles/r_anotherid:remove-grants",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
pb "github.com/hashicorp/watchtower/internal/gen/controller/api/resources/roles"
|
||||
pbs "github.com/hashicorp/watchtower/internal/gen/controller/api/services"
|
||||
"github.com/hashicorp/watchtower/internal/iam"
|
||||
"github.com/hashicorp/watchtower/internal/perms"
|
||||
"github.com/hashicorp/watchtower/internal/servers/controller/handlers"
|
||||
"github.com/hashicorp/watchtower/internal/types/scope"
|
||||
"google.golang.org/grpc/codes"
|
||||
@@ -164,12 +165,54 @@ func (s Service) RemoveRolePrincipals(ctx context.Context, req *pbs.RemoveRolePr
|
||||
return &pbs.RemoveRolePrincipalsResponse{Item: r}, nil
|
||||
}
|
||||
|
||||
// AddRoleGrants implements the interface pbs.RoleServiceServer.
|
||||
func (s Service) AddRoleGrants(ctx context.Context, req *pbs.AddRoleGrantsRequest) (*pbs.AddRoleGrantsResponse, error) {
|
||||
auth := handlers.ToTokenMetadata(ctx)
|
||||
_ = auth
|
||||
if err := validateAddRoleGrantsRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r, err := s.addGrantsInRepo(ctx, req.GetRoleId(), req.GetGrants(), req.GetVersion().GetValue())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pbs.AddRoleGrantsResponse{Item: r}, nil
|
||||
}
|
||||
|
||||
// SetRoleGrants implements the interface pbs.RoleServiceServer.
|
||||
func (s Service) SetRoleGrants(ctx context.Context, req *pbs.SetRoleGrantsRequest) (*pbs.SetRoleGrantsResponse, error) {
|
||||
auth := handlers.ToTokenMetadata(ctx)
|
||||
_ = auth
|
||||
if err := validateSetRoleGrantsRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r, err := s.setGrantsInRepo(ctx, req.GetRoleId(), req.GetGrants(), req.GetVersion().GetValue())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pbs.SetRoleGrantsResponse{Item: r}, nil
|
||||
}
|
||||
|
||||
// RemoveRoleGrants implements the interface pbs.RoleServiceServer.
|
||||
func (s Service) RemoveRoleGrants(ctx context.Context, req *pbs.RemoveRoleGrantsRequest) (*pbs.RemoveRoleGrantsResponse, error) {
|
||||
auth := handlers.ToTokenMetadata(ctx)
|
||||
_ = auth
|
||||
if err := validateRemoveRoleGrantsRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r, err := s.removeGrantsInRepo(ctx, req.GetRoleId(), req.GetGrants(), req.GetVersion().GetValue())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pbs.RemoveRoleGrantsResponse{Item: r}, nil
|
||||
}
|
||||
|
||||
func (s Service) getFromRepo(ctx context.Context, id string) (*pb.Role, error) {
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, pr, err := repo.LookupRole(ctx, id)
|
||||
out, pr, roleGrants, err := repo.LookupRole(ctx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrRecordNotFound) {
|
||||
return nil, handlers.NotFoundErrorf("Role %q doesn't exist.", id)
|
||||
@@ -179,7 +222,7 @@ func (s Service) getFromRepo(ctx context.Context, id string) (*pb.Role, error) {
|
||||
if out == nil {
|
||||
return nil, handlers.NotFoundErrorf("Role %q doesn't exist.", id)
|
||||
}
|
||||
return toProto(out, pr), nil
|
||||
return toProto(out, pr, roleGrants), nil
|
||||
}
|
||||
|
||||
func (s Service) createInRepo(ctx context.Context, scopeId string, item *pb.Role) (*pb.Role, error) {
|
||||
@@ -208,7 +251,7 @@ func (s Service) createInRepo(ctx context.Context, scopeId string, item *pb.Role
|
||||
if out == nil {
|
||||
return nil, status.Error(codes.Internal, "Unable to create role but no error returned from repository.")
|
||||
}
|
||||
return toProto(out, nil), nil
|
||||
return toProto(out, nil, nil), nil
|
||||
}
|
||||
|
||||
func (s Service) updateInRepo(ctx context.Context, scopeId, id string, mask []string, item *pb.Role) (*pb.Role, error) {
|
||||
@@ -246,7 +289,8 @@ func (s Service) updateInRepo(ctx context.Context, scopeId, id string, mask []st
|
||||
if rowsUpdated == 0 {
|
||||
return nil, handlers.NotFoundErrorf("Role %q doesn't exist.", id)
|
||||
}
|
||||
return toProto(out, nil), nil
|
||||
// TODO: Attach principals and grants to UpdateRole response
|
||||
return toProto(out, nil, nil), nil
|
||||
}
|
||||
|
||||
func (s Service) deleteFromRepo(ctx context.Context, id string) (bool, error) {
|
||||
@@ -275,7 +319,8 @@ func (s Service) listFromRepo(ctx context.Context, scopeId string) ([]*pb.Role,
|
||||
}
|
||||
var outRl []*pb.Role
|
||||
for _, g := range rl {
|
||||
outRl = append(outRl, toProto(g, nil))
|
||||
// TODO: Attach principals and grants to ListRoles response.
|
||||
outRl = append(outRl, toProto(g, nil, nil))
|
||||
}
|
||||
return outRl, nil
|
||||
}
|
||||
@@ -289,14 +334,14 @@ func (s Service) addPrinciplesInRepo(ctx context.Context, roleId string, userIds
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Unable to add principles to role: %v.", err)
|
||||
}
|
||||
out, pr, err := repo.LookupRole(ctx, roleId)
|
||||
out, pr, roleGrants, err := repo.LookupRole(ctx, roleId)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Unable to look up role: %v.", err)
|
||||
}
|
||||
if out == nil {
|
||||
return nil, status.Error(codes.Internal, "Unable to lookup role after adding principles to it.")
|
||||
}
|
||||
return toProto(out, pr), nil
|
||||
return toProto(out, pr, roleGrants), nil
|
||||
}
|
||||
|
||||
func (s Service) setPrinciplesInRepo(ctx context.Context, roleId string, userIds []string, groupIds []string, version uint32) (*pb.Role, error) {
|
||||
@@ -308,14 +353,14 @@ func (s Service) setPrinciplesInRepo(ctx context.Context, roleId string, userIds
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Unable to set principles on role: %v.", err)
|
||||
}
|
||||
out, pr, err := repo.LookupRole(ctx, roleId)
|
||||
out, pr, roleGrants, err := repo.LookupRole(ctx, roleId)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Unable to look up role: %v.", err)
|
||||
}
|
||||
if out == nil {
|
||||
return nil, status.Error(codes.Internal, "Unable to lookup role after setting principles for it.")
|
||||
}
|
||||
return toProto(out, pr), nil
|
||||
return toProto(out, pr, roleGrants), nil
|
||||
}
|
||||
|
||||
func (s Service) removePrinciplesInRepo(ctx context.Context, roleId string, userIds []string, groupIds []string, version uint32) (*pb.Role, error) {
|
||||
@@ -327,14 +372,75 @@ func (s Service) removePrinciplesInRepo(ctx context.Context, roleId string, user
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Unable to remove principles from role: %v.", err)
|
||||
}
|
||||
out, pr, err := repo.LookupRole(ctx, roleId)
|
||||
out, pr, roleGrants, err := repo.LookupRole(ctx, roleId)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Unable to look up role: %v.", err)
|
||||
}
|
||||
if out == nil {
|
||||
return nil, status.Error(codes.Internal, "Unable to lookup role after removing principles from it.")
|
||||
}
|
||||
return toProto(out, pr), nil
|
||||
return toProto(out, pr, roleGrants), nil
|
||||
}
|
||||
|
||||
func (s Service) addGrantsInRepo(ctx context.Context, roleId string, grants []string, version uint32) (*pb.Role, error) {
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = repo.AddRoleGrants(ctx, roleId, version, grants)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Unable to add grants to role: %v.", err)
|
||||
}
|
||||
out, pr, roleGrants, err := repo.LookupRole(ctx, roleId)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, "Error looking up role after adding grants to it.")
|
||||
}
|
||||
if out == nil {
|
||||
return nil, status.Error(codes.Internal, "Unable to lookup role after adding grants to it.")
|
||||
}
|
||||
return toProto(out, pr, roleGrants), nil
|
||||
}
|
||||
|
||||
func (s Service) setGrantsInRepo(ctx context.Context, roleId string, grants []string, version uint32) (*pb.Role, error) {
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// If no grant was provided, we clear the grants.
|
||||
if grants == nil {
|
||||
grants = []string{}
|
||||
}
|
||||
_, _, err = repo.SetRoleGrants(ctx, roleId, version, grants)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Unable to set grants on role: %v.", err)
|
||||
}
|
||||
out, pr, roleGrants, err := repo.LookupRole(ctx, roleId)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, "Error looking up role after setting grants on it.")
|
||||
}
|
||||
if out == nil {
|
||||
return nil, status.Error(codes.Internal, "Unable to lookup role after setting grants on it.")
|
||||
}
|
||||
return toProto(out, pr, roleGrants), nil
|
||||
}
|
||||
|
||||
func (s Service) removeGrantsInRepo(ctx context.Context, roleId string, grants []string, version uint32) (*pb.Role, error) {
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = repo.DeleteRoleGrants(ctx, roleId, version, grants)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Unable to remove grants from role: %v.", err)
|
||||
}
|
||||
out, pr, roleGrants, err := repo.LookupRole(ctx, roleId)
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, "Error looking up role after removing grants from it.")
|
||||
}
|
||||
if out == nil {
|
||||
return nil, status.Error(codes.Internal, "Unable to lookup role after removing grants from it.")
|
||||
}
|
||||
return toProto(out, pr, roleGrants), nil
|
||||
}
|
||||
|
||||
// toDbUpdateMask converts the wire format's FieldMask into a list of strings containing FieldMask paths used
|
||||
@@ -356,7 +462,7 @@ func toDbUpdateMask(paths []string) ([]string, error) {
|
||||
return dbPaths, nil
|
||||
}
|
||||
|
||||
func toProto(in *iam.Role, principals []iam.PrincipalRole) *pb.Role {
|
||||
func toProto(in *iam.Role, principals []iam.PrincipalRole, grants []*iam.RoleGrant) *pb.Role {
|
||||
out := pb.Role{
|
||||
Id: in.GetPublicId(),
|
||||
CreatedTime: in.GetCreateTime().GetTimestamp(),
|
||||
@@ -377,6 +483,26 @@ func toProto(in *iam.Role, principals []iam.PrincipalRole) *pb.Role {
|
||||
out.GroupIds = append(out.GroupIds, p.GetPrincipalId())
|
||||
}
|
||||
}
|
||||
for _, g := range grants {
|
||||
out.Grants = append(out.Grants, g.GetRawGrant())
|
||||
parsed, err := perms.Parse(in.GetGrantScopeId(), "", g.GetRawGrant())
|
||||
if err != nil {
|
||||
// This should never happen as we validate on the way in, but let's
|
||||
// return what we can since we are still returning the raw grant
|
||||
out.GrantsCanonical = append(out.GrantsCanonical, "<parse_error>")
|
||||
out.GrantsJson = append(out.GrantsJson, "<parse_error>")
|
||||
} else {
|
||||
out.GrantsCanonical = append(out.GrantsCanonical, parsed.CanonicalString())
|
||||
jsonGrant, err := parsed.MarshalJSON()
|
||||
if err != nil {
|
||||
// Again, this should never happen, but let's return what we
|
||||
// have anyways
|
||||
out.GrantsJson = append(out.GrantsJson, "<parse_error>")
|
||||
} else {
|
||||
out.GrantsJson = append(out.GrantsJson, string(jsonGrant))
|
||||
}
|
||||
}
|
||||
}
|
||||
if in.GetGrantScopeId() != "" {
|
||||
out.GrantScopeId = &wrapperspb.StringValue{Value: in.GetGrantScopeId()}
|
||||
}
|
||||
@@ -503,10 +629,6 @@ func validateSetRolePrincipalsRequest(req *pbs.SetRolePrincipalsRequest) error {
|
||||
if req.GetVersion() == nil {
|
||||
badFields["version"] = "Required field."
|
||||
}
|
||||
if len(req.GetGroupIds()) == 0 && len(req.GetUserIds()) == 0 {
|
||||
badFields["user_ids"] = "Either user_ids or group_ids must be non empty."
|
||||
badFields["group_ids"] = "Either user_ids or group_ids must be non empty."
|
||||
}
|
||||
if len(badFields) > 0 {
|
||||
return handlers.InvalidArgumentErrorf("Errors in provided fields.", badFields)
|
||||
}
|
||||
@@ -531,6 +653,54 @@ func validateRemoveRolePrincipalsRequest(req *pbs.RemoveRolePrincipalsRequest) e
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateAddRoleGrantsRequest(req *pbs.AddRoleGrantsRequest) error {
|
||||
badFields := validateAncestors(req)
|
||||
if !validId(req.GetRoleId(), iam.RolePrefix+"_") {
|
||||
badFields["id"] = "Incorrectly formatted identifier."
|
||||
}
|
||||
if req.GetVersion() == nil {
|
||||
badFields["version"] = "Required field."
|
||||
}
|
||||
if len(req.GetGrants()) == 0 {
|
||||
badFields["grants"] = "This must be non empty."
|
||||
}
|
||||
if len(badFields) > 0 {
|
||||
return handlers.InvalidArgumentErrorf("Errors in provided fields.", badFields)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateSetRoleGrantsRequest(req *pbs.SetRoleGrantsRequest) error {
|
||||
badFields := validateAncestors(req)
|
||||
if !validId(req.GetRoleId(), iam.RolePrefix+"_") {
|
||||
badFields["id"] = "Incorrectly formatted identifier."
|
||||
}
|
||||
if req.GetVersion() == nil {
|
||||
badFields["version"] = "Required field."
|
||||
}
|
||||
if len(badFields) > 0 {
|
||||
return handlers.InvalidArgumentErrorf("Errors in provided fields.", badFields)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateRemoveRoleGrantsRequest(req *pbs.RemoveRoleGrantsRequest) error {
|
||||
badFields := validateAncestors(req)
|
||||
if !validId(req.GetRoleId(), iam.RolePrefix+"_") {
|
||||
badFields["id"] = "Incorrectly formatted identifier."
|
||||
}
|
||||
if req.GetVersion() == nil {
|
||||
badFields["version"] = "Required field."
|
||||
}
|
||||
if len(req.GetGrants()) == 0 {
|
||||
badFields["grants"] = "This must be non empty."
|
||||
}
|
||||
if len(badFields) > 0 {
|
||||
return handlers.InvalidArgumentErrorf("Errors in provided fields.", badFields)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validId(id, prefix string) bool {
|
||||
if !strings.HasPrefix(id, prefix) {
|
||||
return false
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user