Migrate grants to more structured output (#194)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-07-16 22:18:27 -04:00
1 parent 1a1d3058c9
commit 99653727bb
19 files changed
+757 -642

No files matched your search

@@ -172,7 +172,7 @@ func (s Service) AddRoleGrants(ctx context.Context, req *pbs.AddRoleGrantsReques
if err := validateAddRoleGrantsRequest(req); err != nil {
return nil, err
}
r, err := s.addGrantsInRepo(ctx, req.GetRoleId(), req.GetGrants(), req.GetVersion().GetValue())
r, err := s.addGrantsInRepo(ctx, req.GetRoleId(), req.GetGrantStrings(), req.GetVersion().GetValue())
if err != nil {
return nil, err
}
@@ -186,7 +186,7 @@ func (s Service) SetRoleGrants(ctx context.Context, req *pbs.SetRoleGrantsReques
if err := validateSetRoleGrantsRequest(req); err != nil {
return nil, err
}
r, err := s.setGrantsInRepo(ctx, req.GetRoleId(), req.GetGrants(), req.GetVersion().GetValue())
r, err := s.setGrantsInRepo(ctx, req.GetRoleId(), req.GetGrantStrings(), req.GetVersion().GetValue())
if err != nil {
return nil, err
}
@@ -200,7 +200,7 @@ func (s Service) RemoveRoleGrants(ctx context.Context, req *pbs.RemoveRoleGrants
if err := validateRemoveRoleGrantsRequest(req); err != nil {
return nil, err
}
r, err := s.removeGrantsInRepo(ctx, req.GetRoleId(), req.GetGrants(), req.GetVersion().GetValue())
r, err := s.removeGrantsInRepo(ctx, req.GetRoleId(), req.GetGrantStrings(), req.GetVersion().GetValue())
if err != nil {
return nil, err
}
@@ -485,23 +485,27 @@ func toProto(in *iam.Role, principals []iam.PrincipalRole, grants []*iam.RoleGra
out.PrincipalIds = append(out.PrincipalIds, p.GetPrincipalId())
}
for _, g := range grants {
out.Grants = append(out.Grants, g.GetRawGrant())
out.GrantStrings = append(out.GrantStrings, 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>")
out.Grants = append(out.Grants, &pb.Grant{
Raw: g.GetRawGrant(),
Canonical: "<parse_error>",
Json: nil,
})
} 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))
}
_, actions := parsed.Actions()
out.Grants = append(out.Grants, &pb.Grant{
Raw: g.GetRawGrant(),
Canonical: g.GetCanonicalGrant(),
Json: &pb.GrantJson{
Id: parsed.Id(),
Type: parsed.Type().String(),
Actions: actions,
},
})
}
}
if in.GetGrantScopeId() != "" {
@@ -546,6 +550,9 @@ func validateCreateRequest(req *pbs.CreateRoleRequest) error {
if item.GetPrincipals() != nil {
badFields["principals"] = "This is a read only field."
}
if item.GetGrants() != nil {
badFields["grant_strings"] = "This is a read only field."
}
if len(badFields) > 0 {
return handlers.InvalidArgumentErrorf("Argument errors found in the request.", badFields)
}
@@ -579,6 +586,9 @@ func validateUpdateRequest(req *pbs.UpdateRoleRequest) error {
if item.GetPrincipals() != nil {
badFields["principals"] = "This is a read only field and cannot be specified in an update request."
}
if item.GetGrants() != nil {
badFields["grant_strings"] = "This is a read only field and cannot be specified in an update request."
}
if item.GetGrantScopeId() != nil && req.ProjectId != "" {
if item.GetGrantScopeId().Value != req.ProjectId {
badFields["grant_scope_id"] = "Must be empty or set to the project_id when the scope type is project."
@@ -666,8 +676,8 @@ func validateAddRoleGrantsRequest(req *pbs.AddRoleGrantsRequest) error {
if req.GetVersion() == nil {
badFields["version"] = "Required field."
}
if len(req.GetGrants()) == 0 {
badFields["grants"] = "Must be non-empty."
if len(req.GetGrantStrings()) == 0 {
badFields["grant_strings"] = "Must be non-empty."
}
if len(badFields) > 0 {
return handlers.InvalidArgumentErrorf("Errors in provided fields.", badFields)
@@ -697,8 +707,8 @@ func validateRemoveRoleGrantsRequest(req *pbs.RemoveRoleGrantsRequest) error {
if req.GetVersion() == nil {
badFields["version"] = "Required field."
}
if len(req.GetGrants()) == 0 {
badFields["grants"] = "Must be non-empty."
if len(req.GetGrantStrings()) == 0 {
badFields["grant_strings"] = "Must be non-empty."
}
if len(badFields) > 0 {
return handlers.InvalidArgumentErrorf("Errors in provided fields.", badFields)
@@ -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/roles"
"github.com/hashicorp/watchtower/internal/types/scope"
"google.golang.org/genproto/protobuf/field_mask"
@@ -1286,6 +1287,25 @@ func TestRemovePrincipal(t *testing.T) {
}
}
func checkEqualGrants(t *testing.T, expected []string, got *pb.Role) {
require, assert := require.New(t), assert.New(t)
require.Equal(len(expected), len(got.GrantStrings))
require.Equal(len(expected), len(got.Grants))
for i, v := range expected {
parsed, err := perms.Parse("o_abc123", "", v)
require.NoError(err)
assert.Equal(expected[i], got.GrantStrings[i])
assert.Equal(expected[i], got.Grants[i].GetRaw())
assert.Equal(parsed.CanonicalString(), got.Grants[i].GetCanonical())
j := got.Grants[i].GetJson()
require.NotNil(j)
assert.Equal(parsed.Id(), j.GetId())
assert.Equal(parsed.Type().String(), j.GetType())
_, acts := parsed.Actions()
assert.Equal(acts, j.GetActions())
}
}
func TestAddGrants(t *testing.T) {
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
@@ -1339,7 +1359,7 @@ func TestAddGrants(t *testing.T) {
if o != scope {
req.ProjectId = scope.GetPublicId()
}
req.Grants = append(req.Grants, tc.add...)
req.GrantStrings = append(req.GrantStrings, tc.add...)
got, err := s.AddRoleGrants(context.Background(), req)
if tc.wantErr {
assert.Error(err)
@@ -1347,7 +1367,7 @@ func TestAddGrants(t *testing.T) {
}
s, _ := status.FromError(err)
require.NoError(t, err, "Got error %v", s)
assert.Equal(tc.result, got.GetItem().GetGrants())
checkEqualGrants(t, tc.result, got.GetItem())
})
}
}
@@ -1362,33 +1382,33 @@ func TestAddGrants(t *testing.T) {
{
name: "Bad Org Id",
req: &pbs.AddRoleGrantsRequest{
OrgId: "",
ProjectId: role.GetScopeId(),
RoleId: role.GetPublicId(),
Grants: []string{"id=*;actions=create"},
Version: &wrapperspb.UInt32Value{Value: role.GetVersion()},
OrgId: "",
ProjectId: role.GetScopeId(),
RoleId: role.GetPublicId(),
GrantStrings: []string{"id=*;actions=create"},
Version: &wrapperspb.UInt32Value{Value: role.GetVersion()},
},
errCode: codes.InvalidArgument,
},
{
name: "Bad Project Id",
req: &pbs.AddRoleGrantsRequest{
OrgId: o.GetPublicId(),
ProjectId: "bad id",
RoleId: role.GetPublicId(),
Grants: []string{"id=*;actions=create"},
Version: &wrapperspb.UInt32Value{Value: role.GetVersion()},
OrgId: o.GetPublicId(),
ProjectId: "bad id",
RoleId: role.GetPublicId(),
GrantStrings: []string{"id=*;actions=create"},
Version: &wrapperspb.UInt32Value{Value: role.GetVersion()},
},
errCode: codes.InvalidArgument,
},
{
name: "Bad Role Id",
req: &pbs.AddRoleGrantsRequest{
OrgId: o.GetPublicId(),
ProjectId: role.GetScopeId(),
RoleId: "bad id",
Grants: []string{"id=*;actions=create"},
Version: &wrapperspb.UInt32Value{Value: role.GetVersion()},
OrgId: o.GetPublicId(),
ProjectId: role.GetScopeId(),
RoleId: "bad id",
GrantStrings: []string{"id=*;actions=create"},
Version: &wrapperspb.UInt32Value{Value: role.GetVersion()},
},
errCode: codes.InvalidArgument,
},
@@ -1462,7 +1482,7 @@ func TestSetGrants(t *testing.T) {
if o != scope {
req.ProjectId = scope.GetPublicId()
}
req.Grants = append(req.Grants, tc.set...)
req.GrantStrings = append(req.GrantStrings, tc.set...)
got, err := s.SetRoleGrants(context.Background(), req)
if tc.wantErr {
assert.Error(err)
@@ -1470,7 +1490,7 @@ func TestSetGrants(t *testing.T) {
}
s, _ := status.FromError(err)
require.NoError(t, err, "Got error %v", s)
assert.Equal(tc.result, got.GetItem().GetGrants())
checkEqualGrants(t, tc.result, got.GetItem())
})
}
}
@@ -1486,33 +1506,33 @@ func TestSetGrants(t *testing.T) {
{
name: "Bad Org Id",
req: &pbs.SetRoleGrantsRequest{
OrgId: "",
ProjectId: role.GetScopeId(),
RoleId: role.GetPublicId(),
Grants: []string{"id=*;actions=create"},
Version: &wrapperspb.UInt32Value{Value: role.GetVersion()},
OrgId: "",
ProjectId: role.GetScopeId(),
RoleId: role.GetPublicId(),
GrantStrings: []string{"id=*;actions=create"},
Version: &wrapperspb.UInt32Value{Value: role.GetVersion()},
},
errCode: codes.InvalidArgument,
},
{
name: "Bad Project Id",
req: &pbs.SetRoleGrantsRequest{
OrgId: o.GetPublicId(),
ProjectId: "bad id",
RoleId: role.GetPublicId(),
Grants: []string{"id=*;actions=create"},
Version: &wrapperspb.UInt32Value{Value: role.GetVersion()},
OrgId: o.GetPublicId(),
ProjectId: "bad id",
RoleId: role.GetPublicId(),
GrantStrings: []string{"id=*;actions=create"},
Version: &wrapperspb.UInt32Value{Value: role.GetVersion()},
},
errCode: codes.InvalidArgument,
},
{
name: "Bad Role Id",
req: &pbs.SetRoleGrantsRequest{
OrgId: o.GetPublicId(),
ProjectId: role.GetScopeId(),
RoleId: "bad id",
Grants: []string{"id=*;actions=create"},
Version: &wrapperspb.UInt32Value{Value: role.GetVersion()},
OrgId: o.GetPublicId(),
ProjectId: role.GetScopeId(),
RoleId: "bad id",
GrantStrings: []string{"id=*;actions=create"},
Version: &wrapperspb.UInt32Value{Value: role.GetVersion()},
},
errCode: codes.InvalidArgument,
},
@@ -1585,7 +1605,7 @@ func TestRemoveGrants(t *testing.T) {
if o != scope {
req.ProjectId = scope.GetPublicId()
}
req.Grants = append(req.Grants, tc.remove...)
req.GrantStrings = append(req.GrantStrings, tc.remove...)
got, err := s.RemoveRoleGrants(context.Background(), req)
if tc.wantErr {
assert.Error(err)
@@ -1594,7 +1614,7 @@ func TestRemoveGrants(t *testing.T) {
s, ok := status.FromError(err)
assert.True(ok)
require.NoError(t, err, "Got error %v", s)
assert.Equal(tc.result, got.GetItem().GetGrants())
checkEqualGrants(t, tc.result, got.GetItem())
})
}
}
@@ -1610,33 +1630,33 @@ func TestRemoveGrants(t *testing.T) {
{
name: "Bad Org Id",
req: &pbs.RemoveRoleGrantsRequest{
OrgId: "",
ProjectId: role.GetScopeId(),
RoleId: role.GetPublicId(),
Grants: []string{"id=*;actions=create"},
Version: &wrapperspb.UInt32Value{Value: role.GetVersion()},
OrgId: "",
ProjectId: role.GetScopeId(),
RoleId: role.GetPublicId(),
GrantStrings: []string{"id=*;actions=create"},
Version: &wrapperspb.UInt32Value{Value: role.GetVersion()},
},
errCode: codes.InvalidArgument,
},
{
name: "Bad Project Id",
req: &pbs.RemoveRoleGrantsRequest{
OrgId: o.GetPublicId(),
ProjectId: "bad id",
RoleId: role.GetPublicId(),
Grants: []string{"id=*;actions=create"},
Version: &wrapperspb.UInt32Value{Value: role.GetVersion()},
OrgId: o.GetPublicId(),
ProjectId: "bad id",
RoleId: role.GetPublicId(),
GrantStrings: []string{"id=*;actions=create"},
Version: &wrapperspb.UInt32Value{Value: role.GetVersion()},
},
errCode: codes.InvalidArgument,
},
{
name: "Bad Role Id",
req: &pbs.RemoveRoleGrantsRequest{
OrgId: o.GetPublicId(),
ProjectId: role.GetScopeId(),
RoleId: "bad id",
Grants: []string{"id=*;actions=create"},
Version: &wrapperspb.UInt32Value{Value: role.GetVersion()},
OrgId: o.GetPublicId(),
ProjectId: role.GetScopeId(),
RoleId: "bad id",
GrantStrings: []string{"id=*;actions=create"},
Version: &wrapperspb.UInt32Value{Value: role.GetVersion()},
},
errCode: codes.InvalidArgument,
},