Add versioning to auth methods and accounts (#249)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-08-11 11:19:29 -04:00
1 parent a7c467bd68
commit 565059ed63
23 files changed
+640 -444

No files matched your search

@@ -211,6 +211,7 @@ func toProto(in *password.Account) (*pb.Account, error) {
CreatedTime: in.GetCreateTime().GetTimestamp(),
UpdatedTime: in.GetUpdateTime().GetTimestamp(),
AuthMethodId: in.GetAuthMethodId(),
Version: in.GetVersion(),
Type: "password",
}
if in.GetDescription() != "" {
@@ -48,6 +48,7 @@ func TestGet(t *testing.T) {
CreatedTime: aa.GetCreateTime().GetTimestamp(),
UpdatedTime: aa.GetUpdateTime().GetTimestamp(),
Scope: &scopes.ScopeInfo{Id: org.GetPublicId(), Type: scope.Org.String()},
Version: 1,
Type: "password",
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{"username": structpb.NewStringValue(aa.GetUserName())}},
}
@@ -113,6 +114,7 @@ func TestList(t *testing.T) {
CreatedTime: aa.GetCreateTime().GetTimestamp(),
UpdatedTime: aa.GetUpdateTime().GetTimestamp(),
Scope: &scopes.ScopeInfo{Id: o.GetPublicId(), Type: scope.Org.String()},
Version: 1,
Type: "password",
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{"username": structpb.NewStringValue(aa.GetUserName())}},
})
@@ -126,6 +128,7 @@ func TestList(t *testing.T) {
CreatedTime: aa.GetCreateTime().GetTimestamp(),
UpdatedTime: aa.GetUpdateTime().GetTimestamp(),
Scope: &scopes.ScopeInfo{Id: o.GetPublicId(), Type: scope.Org.String()},
Version: 1,
Type: "password",
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{"username": structpb.NewStringValue(aa.GetUserName())}},
})
@@ -326,6 +329,7 @@ func TestCreate(t *testing.T) {
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
Scope: &scopes.ScopeInfo{Id: o.GetPublicId(), Type: scope.Org.String()},
Version: 1,
Type: "password",
Attributes: defaultSt,
},
@@ -108,7 +108,7 @@ func (s Service) UpdateAuthMethod(ctx context.Context, req *pbs.UpdateAuthMethod
if err := validateUpdateRequest(req); err != nil {
return nil, err
}
u, err := s.updateInRepo(ctx, authResults.Scope.GetId(), req.GetId(), req.GetUpdateMask().GetPaths(), req.GetItem())
u, err := s.updateInRepo(ctx, authResults.Scope.GetId(), req.GetId(), req.GetVersion(), req.GetUpdateMask().GetPaths(), req.GetItem())
if err != nil {
return nil, err
}
@@ -195,7 +195,7 @@ func (s Service) createInRepo(ctx context.Context, scopeId string, item *pb.Auth
return toProto(out)
}
func (s Service) updateInRepo(ctx context.Context, scopeId, id string, mask []string, item *pb.AuthMethod) (*pb.AuthMethod, error) {
func (s Service) updateInRepo(ctx context.Context, scopeId, id string, version uint32, mask []string, item *pb.AuthMethod) (*pb.AuthMethod, error) {
var opts []password.Option
if desc := item.GetDescription(); desc != nil {
opts = append(opts, password.WithDescription(desc.GetValue()))
@@ -216,7 +216,7 @@ func (s Service) updateInRepo(ctx context.Context, scopeId, id string, mask []st
if err != nil {
return nil, err
}
out, rowsUpdated, err := repo.UpdateAuthMethod(ctx, u, dbMask)
out, rowsUpdated, err := repo.UpdateAuthMethod(ctx, u, version, dbMask)
if err != nil {
return nil, status.Errorf(codes.Internal, "Unable to update auth method: %v.", err)
}
@@ -246,6 +246,7 @@ func toProto(in *password.AuthMethod) (*pb.AuthMethod, error) {
Id: in.GetPublicId(),
CreatedTime: in.GetCreateTime().GetTimestamp(),
UpdatedTime: in.GetUpdateTime().GetTimestamp(),
Version: in.GetVersion(),
Type: "password",
}
if in.GetDescription() != "" {
@@ -324,6 +325,9 @@ func validateUpdateRequest(req *pbs.UpdateAuthMethodRequest) error {
if req.GetUpdateMask() == nil {
badFields["update_mask"] = "UpdateMask not provided but is required to update this resource."
}
if req.GetVersion() == 0 {
badFields["version"] = "Existing resource version is required for an update."
}
item := req.GetItem()
if item == nil {
@@ -47,6 +47,7 @@ func TestGet(t *testing.T) {
"min_password_length": structpb.NewNumberValue(8),
"min_user_name_length": structpb.NewNumberValue(3),
}},
Version: 1,
Scope: &scopes.ScopeInfo{
Id: o.GetPublicId(),
Type: o.GetType(),
@@ -122,6 +123,7 @@ func TestList(t *testing.T) {
CreatedTime: am.GetCreateTime().GetTimestamp(),
UpdatedTime: am.GetUpdateTime().GetTimestamp(),
Scope: &scopes.ScopeInfo{Id: oWithAuthMethods.GetPublicId(), Type: scope.Org.String()},
Version: 1,
Type: "password",
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"min_password_length": structpb.NewNumberValue(8),
@@ -137,6 +139,7 @@ func TestList(t *testing.T) {
CreatedTime: aa.GetCreateTime().GetTimestamp(),
UpdatedTime: aa.GetUpdateTime().GetTimestamp(),
Scope: &scopes.ScopeInfo{Id: oWithOtherAuthMethods.GetPublicId(), Type: scope.Org.String()},
Version: 1,
Type: "password",
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"min_password_length": structpb.NewNumberValue(8),
@@ -310,6 +313,7 @@ func TestCreate(t *testing.T) {
Name: &wrapperspb.StringValue{Value: "name"},
Description: &wrapperspb.StringValue{Value: "desc"},
Scope: &scopes.ScopeInfo{Id: o.GetPublicId(), Type: scope.Org.String()},
Version: 1,
Type: "password",
Attributes: &structpb.Struct{Fields: map[string]*structpb.Value{
"min_password_length": structpb.NewNumberValue(8),
@@ -673,6 +677,8 @@ func TestUpdate(t *testing.T) {
am, cleanup := freshAuthMethod()
defer cleanup()
tc.req.Version = 1
if tc.req.GetId() == "" {
tc.req.Id = am.GetId()
}
@@ -702,6 +708,9 @@ func TestUpdate(t *testing.T) {
// Clear all values which are hard to compare against.
got.Item.UpdatedTime, tc.res.Item.UpdatedTime = nil, nil
assert.EqualValues(2, got.Item.Version)
tc.res.Item.Version = 2
}
assert.Empty(cmp.Diff(got, tc.res, protocmp.Transform()), "UpdateAuthMethod(%q) got response %q, wanted %q", tc.req, got, tc.res)
})