Migrate target host sets -> host sources (#1424)
This follows the same model as https://github.com/hashicorp/boundary/pull/1413 See there and CHANGELOG for more details.
This commit is contained in:
1 parent
6b78108ecf
commit
f8a51b987c
39 files changed
+3385
-964
No files matched your search
@@ -58,7 +58,7 @@ func TestGetSession(t *testing.T) {
|
||||
hs := static.TestSets(t, conn, hc.GetPublicId(), 1)[0]
|
||||
h := static.TestHosts(t, conn, hc.GetPublicId(), 1)[0]
|
||||
static.TestSetMembers(t, conn, hs.GetPublicId(), []*static.Host{h})
|
||||
tar := target.TestTcpTarget(t, conn, p.GetPublicId(), "test", target.WithHostSets([]string{hs.GetPublicId()}))
|
||||
tar := target.TestTcpTarget(t, conn, p.GetPublicId(), "test", target.WithHostSources([]string{hs.GetPublicId()}))
|
||||
|
||||
sess := session.TestSession(t, conn, wrap, session.ComposedOf{
|
||||
UserId: uId,
|
||||
@@ -176,7 +176,7 @@ func TestList_Self(t *testing.T) {
|
||||
hs := static.TestSets(t, conn, hc.GetPublicId(), 1)[0]
|
||||
h := static.TestHosts(t, conn, hc.GetPublicId(), 1)[0]
|
||||
static.TestSetMembers(t, conn, hs.GetPublicId(), []*static.Host{h})
|
||||
tar := target.TestTcpTarget(t, conn, pWithSessions.GetPublicId(), "test", target.WithHostSets([]string{hs.GetPublicId()}))
|
||||
tar := target.TestTcpTarget(t, conn, pWithSessions.GetPublicId(), "test", target.WithHostSources([]string{hs.GetPublicId()}))
|
||||
|
||||
// By default a user can read/cancel their own sessions.
|
||||
session.TestSession(t, conn, wrap, session.ComposedOf{
|
||||
@@ -260,13 +260,13 @@ func TestList(t *testing.T) {
|
||||
hs := static.TestSets(t, conn, hc.GetPublicId(), 1)[0]
|
||||
h := static.TestHosts(t, conn, hc.GetPublicId(), 1)[0]
|
||||
static.TestSetMembers(t, conn, hs.GetPublicId(), []*static.Host{h})
|
||||
tar := target.TestTcpTarget(t, conn, pWithSessions.GetPublicId(), "test", target.WithHostSets([]string{hs.GetPublicId()}))
|
||||
tar := target.TestTcpTarget(t, conn, pWithSessions.GetPublicId(), "test", target.WithHostSources([]string{hs.GetPublicId()}))
|
||||
|
||||
hcOther := static.TestCatalogs(t, conn, pWithOtherSessions.GetPublicId(), 1)[0]
|
||||
hsOther := static.TestSets(t, conn, hcOther.GetPublicId(), 1)[0]
|
||||
hOther := static.TestHosts(t, conn, hcOther.GetPublicId(), 1)[0]
|
||||
static.TestSetMembers(t, conn, hsOther.GetPublicId(), []*static.Host{hOther})
|
||||
tarOther := target.TestTcpTarget(t, conn, pWithOtherSessions.GetPublicId(), "test", target.WithHostSets([]string{hsOther.GetPublicId()}))
|
||||
tarOther := target.TestTcpTarget(t, conn, pWithOtherSessions.GetPublicId(), "test", target.WithHostSources([]string{hsOther.GetPublicId()}))
|
||||
|
||||
var wantSession []*pb.Session
|
||||
var totalSession []*pb.Session
|
||||
@@ -471,7 +471,7 @@ func TestCancel(t *testing.T) {
|
||||
hs := static.TestSets(t, conn, hc.GetPublicId(), 1)[0]
|
||||
h := static.TestHosts(t, conn, hc.GetPublicId(), 1)[0]
|
||||
static.TestSetMembers(t, conn, hs.GetPublicId(), []*static.Host{h})
|
||||
tar := target.TestTcpTarget(t, conn, p.GetPublicId(), "test", target.WithHostSets([]string{hs.GetPublicId()}))
|
||||
tar := target.TestTcpTarget(t, conn, p.GetPublicId(), "test", target.WithHostSources([]string{hs.GetPublicId()}))
|
||||
|
||||
sess := session.TestSession(t, conn, wrap, session.ComposedOf{
|
||||
UserId: uId,
|
||||
|
||||
@@ -351,7 +351,7 @@ func (s Service) AddTargetHostSets(ctx context.Context, req *pbs.AddTargetHostSe
|
||||
if authResults.Error != nil {
|
||||
return nil, authResults.Error
|
||||
}
|
||||
t, ts, cl, err := s.addSetsInRepo(ctx, req.GetId(), req.GetHostSetIds(), req.GetVersion())
|
||||
t, ts, cl, err := s.addHostSourcesInRepo(ctx, req.GetId(), req.GetHostSetIds(), req.GetVersion())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -389,7 +389,7 @@ func (s Service) SetTargetHostSets(ctx context.Context, req *pbs.SetTargetHostSe
|
||||
if authResults.Error != nil {
|
||||
return nil, authResults.Error
|
||||
}
|
||||
t, ts, cl, err := s.setSetsInRepo(ctx, req.GetId(), req.GetHostSetIds(), req.GetVersion())
|
||||
t, ts, cl, err := s.setHostSourcesInRepo(ctx, req.GetId(), req.GetHostSetIds(), req.GetVersion())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -427,7 +427,7 @@ func (s Service) RemoveTargetHostSets(ctx context.Context, req *pbs.RemoveTarget
|
||||
if authResults.Error != nil {
|
||||
return nil, authResults.Error
|
||||
}
|
||||
t, ts, cl, err := s.removeSetsInRepo(ctx, req.GetId(), req.GetHostSetIds(), req.GetVersion())
|
||||
t, ts, cl, err := s.removeHostSourcesInRepo(ctx, req.GetId(), req.GetHostSetIds(), req.GetVersion())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -454,6 +454,120 @@ func (s Service) RemoveTargetHostSets(ctx context.Context, req *pbs.RemoveTarget
|
||||
return &pbs.RemoveTargetHostSetsResponse{Item: item}, nil
|
||||
}
|
||||
|
||||
// AddTargetHostSources implements the interface pbs.TargetServiceServer.
|
||||
func (s Service) AddTargetHostSources(ctx context.Context, req *pbs.AddTargetHostSourcesRequest) (*pbs.AddTargetHostSourcesResponse, error) {
|
||||
const op = "targets.(Service).AddTargetHostSources"
|
||||
|
||||
if err := validateAddHostSourcesRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
authResults := s.authResult(ctx, req.GetId(), action.AddHostSets)
|
||||
if authResults.Error != nil {
|
||||
return nil, authResults.Error
|
||||
}
|
||||
t, ts, cl, err := s.addHostSourcesInRepo(ctx, req.GetId(), req.GetHostSourceIds(), req.GetVersion())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
outputFields, ok := requests.OutputFields(ctx)
|
||||
if !ok {
|
||||
return nil, errors.New(ctx, errors.Internal, op, "no request context found")
|
||||
}
|
||||
|
||||
outputOpts := make([]handlers.Option, 0, 3)
|
||||
outputOpts = append(outputOpts, handlers.WithOutputFields(&outputFields))
|
||||
if outputFields.Has(globals.ScopeField) {
|
||||
outputOpts = append(outputOpts, handlers.WithScope(authResults.Scope))
|
||||
}
|
||||
if outputFields.Has(globals.AuthorizedActionsField) {
|
||||
outputOpts = append(outputOpts, handlers.WithAuthorizedActions(authResults.FetchActionSetForId(ctx, t.GetPublicId(), IdActions).Strings()))
|
||||
}
|
||||
|
||||
item, err := toProto(ctx, t, ts, cl, outputOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pbs.AddTargetHostSourcesResponse{Item: item}, nil
|
||||
}
|
||||
|
||||
// SetTargetHostSources implements the interface pbs.TargetServiceServer.
|
||||
func (s Service) SetTargetHostSources(ctx context.Context, req *pbs.SetTargetHostSourcesRequest) (*pbs.SetTargetHostSourcesResponse, error) {
|
||||
const op = "targets.(Service).SetTargetHostSources"
|
||||
|
||||
if err := validateSetHostSourcesRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
authResults := s.authResult(ctx, req.GetId(), action.SetHostSets)
|
||||
if authResults.Error != nil {
|
||||
return nil, authResults.Error
|
||||
}
|
||||
t, ts, cl, err := s.setHostSourcesInRepo(ctx, req.GetId(), req.GetHostSourceIds(), req.GetVersion())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
outputFields, ok := requests.OutputFields(ctx)
|
||||
if !ok {
|
||||
return nil, errors.New(ctx, errors.Internal, op, "no request context found")
|
||||
}
|
||||
|
||||
outputOpts := make([]handlers.Option, 0, 3)
|
||||
outputOpts = append(outputOpts, handlers.WithOutputFields(&outputFields))
|
||||
if outputFields.Has(globals.ScopeField) {
|
||||
outputOpts = append(outputOpts, handlers.WithScope(authResults.Scope))
|
||||
}
|
||||
if outputFields.Has(globals.AuthorizedActionsField) {
|
||||
outputOpts = append(outputOpts, handlers.WithAuthorizedActions(authResults.FetchActionSetForId(ctx, t.GetPublicId(), IdActions).Strings()))
|
||||
}
|
||||
|
||||
item, err := toProto(ctx, t, ts, cl, outputOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pbs.SetTargetHostSourcesResponse{Item: item}, nil
|
||||
}
|
||||
|
||||
// RemoveTargetHostSources implements the interface pbs.TargetServiceServer.
|
||||
func (s Service) RemoveTargetHostSources(ctx context.Context, req *pbs.RemoveTargetHostSourcesRequest) (*pbs.RemoveTargetHostSourcesResponse, error) {
|
||||
const op = "targets.(Service).RemoveTargetHostSets"
|
||||
|
||||
if err := validateRemoveHostSourcesRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
authResults := s.authResult(ctx, req.GetId(), action.RemoveHostSets)
|
||||
if authResults.Error != nil {
|
||||
return nil, authResults.Error
|
||||
}
|
||||
t, ts, cl, err := s.removeHostSourcesInRepo(ctx, req.GetId(), req.GetHostSourceIds(), req.GetVersion())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
outputFields, ok := requests.OutputFields(ctx)
|
||||
if !ok {
|
||||
return nil, errors.New(ctx, errors.Internal, op, "no request context found")
|
||||
}
|
||||
|
||||
outputOpts := make([]handlers.Option, 0, 3)
|
||||
outputOpts = append(outputOpts, handlers.WithOutputFields(&outputFields))
|
||||
if outputFields.Has(globals.ScopeField) {
|
||||
outputOpts = append(outputOpts, handlers.WithScope(authResults.Scope))
|
||||
}
|
||||
if outputFields.Has(globals.AuthorizedActionsField) {
|
||||
outputOpts = append(outputOpts, handlers.WithAuthorizedActions(authResults.FetchActionSetForId(ctx, t.GetPublicId(), IdActions).Strings()))
|
||||
}
|
||||
|
||||
item, err := toProto(ctx, t, ts, cl, outputOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pbs.RemoveTargetHostSourcesResponse{Item: item}, nil
|
||||
}
|
||||
|
||||
// DEPRECATED: AddTargetCredentialLibraries implements the interface pbs.TargetServiceServer.
|
||||
func (s Service) AddTargetCredentialLibraries(ctx context.Context, req *pbs.AddTargetCredentialLibrariesRequest) (*pbs.AddTargetCredentialLibrariesResponse, error) {
|
||||
const op = "targets.(Service).AddTargetCredentialLibraries"
|
||||
@@ -730,7 +844,7 @@ func (s Service) AuthorizeSession(ctx context.Context, req *pbs.AuthorizeSession
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t, hostSets, libs, err := repo.LookupTarget(ctx, t.GetPublicId())
|
||||
t, hostSources, libs, err := repo.LookupTarget(ctx, t.GetPublicId())
|
||||
if err != nil {
|
||||
if errors.IsNotFoundError(err) {
|
||||
return nil, handlers.NotFoundErrorf("Target %q not found.", t.GetPublicId())
|
||||
@@ -838,11 +952,11 @@ func (s Service) AuthorizeSession(ctx context.Context, req *pbs.AuthorizeSession
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hostIds := make([]compoundHost, 0, len(hostSets)*10)
|
||||
hostIds := make([]compoundHost, 0, len(hostSources)*10)
|
||||
|
||||
HostSetIterationLoop:
|
||||
for _, tSet := range hostSets {
|
||||
hsId := tSet.PublicId
|
||||
HostSourceIterationLoop:
|
||||
for _, hSource := range hostSources {
|
||||
hsId := hSource.Id()
|
||||
switch host.SubtypeFromId(hsId) {
|
||||
case static.Subtype:
|
||||
_, hosts, err := staticHostRepo.LookupSet(ctx, hsId)
|
||||
@@ -854,7 +968,7 @@ HostSetIterationLoop:
|
||||
hostIds = append(hostIds, compoundId)
|
||||
if host.PublicId == requestedId {
|
||||
chosenId = &compoundId
|
||||
break HostSetIterationLoop
|
||||
break HostSourceIterationLoop
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -870,7 +984,7 @@ HostSetIterationLoop:
|
||||
if chosenId == nil {
|
||||
if len(hostIds) == 0 {
|
||||
// No hosts were found, error
|
||||
return nil, handlers.NotFoundErrorf("No hosts found from available target host sets.")
|
||||
return nil, handlers.NotFoundErrorf("No hosts found from available target host sources.")
|
||||
}
|
||||
chosenId = &hostIds[rand.Intn(len(hostIds))]
|
||||
}
|
||||
@@ -1026,7 +1140,7 @@ HostSetIterationLoop:
|
||||
return &pbs.AuthorizeSessionResponse{Item: ret}, nil
|
||||
}
|
||||
|
||||
func (s Service) getFromRepo(ctx context.Context, id string) (target.Target, []*target.TargetSet, []target.CredentialSource, error) {
|
||||
func (s Service) getFromRepo(ctx context.Context, id string) (target.Target, []target.HostSource, []target.CredentialSource, error) {
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
@@ -1044,7 +1158,7 @@ func (s Service) getFromRepo(ctx context.Context, id string) (target.Target, []*
|
||||
return u, hs, cl, nil
|
||||
}
|
||||
|
||||
func (s Service) createInRepo(ctx context.Context, item *pb.Target) (target.Target, []*target.TargetSet, []target.CredentialSource, error) {
|
||||
func (s Service) createInRepo(ctx context.Context, item *pb.Target) (target.Target, []target.HostSource, []target.CredentialSource, error) {
|
||||
const op = "targets.(Service).createInRepo"
|
||||
opts := []target.Option{target.WithName(item.GetName().GetValue())}
|
||||
if item.GetDescription() != nil {
|
||||
@@ -1084,7 +1198,7 @@ func (s Service) createInRepo(ctx context.Context, item *pb.Target) (target.Targ
|
||||
return out, hs, cl, nil
|
||||
}
|
||||
|
||||
func (s Service) updateInRepo(ctx context.Context, scopeId, id string, mask []string, item *pb.Target) (target.Target, []*target.TargetSet, []target.CredentialSource, error) {
|
||||
func (s Service) updateInRepo(ctx context.Context, scopeId, id string, mask []string, item *pb.Target) (target.Target, []target.HostSource, []target.CredentialSource, error) {
|
||||
const op = "targets.(Service).updateInRepo"
|
||||
var opts []target.Option
|
||||
if desc := item.GetDescription(); desc != nil {
|
||||
@@ -1161,66 +1275,66 @@ func (s Service) listFromRepo(ctx context.Context, scopeIds []string) ([]target.
|
||||
return ul, nil
|
||||
}
|
||||
|
||||
func (s Service) addSetsInRepo(ctx context.Context, targetId string, hostSetId []string, version uint32) (target.Target, []*target.TargetSet, []target.CredentialSource, error) {
|
||||
func (s Service) addHostSourcesInRepo(ctx context.Context, targetId string, hostSourceIds []string, version uint32) (target.Target, []target.HostSource, []target.CredentialSource, error) {
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
out, hs, cl, err := repo.AddTargetHostSets(ctx, targetId, version, strutil.RemoveDuplicates(hostSetId, false))
|
||||
out, hs, cl, err := repo.AddTargetHostSources(ctx, targetId, version, strutil.RemoveDuplicates(hostSourceIds, false))
|
||||
if err != nil {
|
||||
// TODO: Figure out a way to surface more helpful error info beyond the Internal error.
|
||||
return nil, nil, nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "Unable to add host sets to target: %v.", err)
|
||||
return nil, nil, nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "Unable to add host sources to target: %v.", err)
|
||||
}
|
||||
if out == nil {
|
||||
return nil, nil, nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "Unable to lookup target after adding host sets to it.")
|
||||
return nil, nil, nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "Unable to lookup target after adding host sources to it.")
|
||||
}
|
||||
return out, hs, cl, nil
|
||||
}
|
||||
|
||||
func (s Service) setSetsInRepo(ctx context.Context, targetId string, hostSetIds []string, version uint32) (target.Target, []*target.TargetSet, []target.CredentialSource, error) {
|
||||
const op = "targets.(Service).setSetsInRepo"
|
||||
func (s Service) setHostSourcesInRepo(ctx context.Context, targetId string, hostSourceIds []string, version uint32) (target.Target, []target.HostSource, []target.CredentialSource, error) {
|
||||
const op = "targets.(Service).setSourcesInRepo"
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
_, _, _, err = repo.SetTargetHostSets(ctx, targetId, version, strutil.RemoveDuplicates(hostSetIds, false))
|
||||
_, _, _, err = repo.SetTargetHostSources(ctx, targetId, version, strutil.RemoveDuplicates(hostSourceIds, false))
|
||||
if err != nil {
|
||||
// TODO: Figure out a way to surface more helpful error info beyond the Internal error.
|
||||
return nil, nil, nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "Unable to set host sets in target: %v.", err)
|
||||
return nil, nil, nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "Unable to set host sources in target: %v.", err)
|
||||
}
|
||||
|
||||
out, hs, cl, err := repo.LookupTarget(ctx, targetId)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to look up target after setting host sets"))
|
||||
return nil, nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to look up target after setting host sources"))
|
||||
}
|
||||
if out == nil {
|
||||
return nil, nil, nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "Unable to lookup target after setting host sets for it.")
|
||||
return nil, nil, nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "Unable to lookup target after setting host sources for it.")
|
||||
}
|
||||
return out, hs, cl, nil
|
||||
}
|
||||
|
||||
func (s Service) removeSetsInRepo(ctx context.Context, targetId string, hostSetIds []string, version uint32) (target.Target, []*target.TargetSet, []target.CredentialSource, error) {
|
||||
const op = "targets.(Service).removeSetsInRepo"
|
||||
func (s Service) removeHostSourcesInRepo(ctx context.Context, targetId string, hostSourceIds []string, version uint32) (target.Target, []target.HostSource, []target.CredentialSource, error) {
|
||||
const op = "targets.(Service).removeHostSourcesInRepo"
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
_, err = repo.DeleteTargeHostSets(ctx, targetId, version, strutil.RemoveDuplicates(hostSetIds, false))
|
||||
_, err = repo.DeleteTargetHostSources(ctx, targetId, version, strutil.RemoveDuplicates(hostSourceIds, false))
|
||||
if err != nil {
|
||||
// TODO: Figure out a way to surface more helpful error info beyond the Internal error.
|
||||
return nil, nil, nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "Unable to remove host sets from target: %v.", err)
|
||||
return nil, nil, nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "Unable to remove host sources from target: %v.", err)
|
||||
}
|
||||
out, hs, cl, err := repo.LookupTarget(ctx, targetId)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to look up target after removing host sets"))
|
||||
return nil, nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to look up target after removing host sources"))
|
||||
}
|
||||
if out == nil {
|
||||
return nil, nil, nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "Unable to lookup target after removing host sets from it.")
|
||||
return nil, nil, nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "Unable to lookup target after removing host sources from it.")
|
||||
}
|
||||
return out, hs, cl, nil
|
||||
}
|
||||
|
||||
func (s Service) addCredentialSourcesInRepo(ctx context.Context, targetId string, ids []string, version uint32) (target.Target, []*target.TargetSet, []target.CredentialSource, error) {
|
||||
func (s Service) addCredentialSourcesInRepo(ctx context.Context, targetId string, ids []string, version uint32) (target.Target, []target.HostSource, []target.CredentialSource, error) {
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
@@ -1236,7 +1350,7 @@ func (s Service) addCredentialSourcesInRepo(ctx context.Context, targetId string
|
||||
return out, hs, credSources, nil
|
||||
}
|
||||
|
||||
func (s Service) setCredentialSourcesInRepo(ctx context.Context, targetId string, ids []string, version uint32) (target.Target, []*target.TargetSet, []target.CredentialSource, error) {
|
||||
func (s Service) setCredentialSourcesInRepo(ctx context.Context, targetId string, ids []string, version uint32) (target.Target, []target.HostSource, []target.CredentialSource, error) {
|
||||
const op = "targets.(Service).setCredentialSourcesInRepo"
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
@@ -1258,7 +1372,7 @@ func (s Service) setCredentialSourcesInRepo(ctx context.Context, targetId string
|
||||
return out, hs, credSources, nil
|
||||
}
|
||||
|
||||
func (s Service) removeCredentialSourcesInRepo(ctx context.Context, targetId string, ids []string, version uint32) (target.Target, []*target.TargetSet, []target.CredentialSource, error) {
|
||||
func (s Service) removeCredentialSourcesInRepo(ctx context.Context, targetId string, ids []string, version uint32) (target.Target, []target.HostSource, []target.CredentialSource, error) {
|
||||
const op = "targets.(Service).removeCredentialSourcesInRepo"
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
@@ -1332,7 +1446,7 @@ func (s Service) authResult(ctx context.Context, id string, a action.Type, looku
|
||||
return ret
|
||||
}
|
||||
|
||||
func toProto(ctx context.Context, in target.Target, m []*target.TargetSet, credSources []target.CredentialSource, opt ...handlers.Option) (*pb.Target, error) {
|
||||
func toProto(ctx context.Context, in target.Target, hostSources []target.HostSource, credSources []target.CredentialSource, opt ...handlers.Option) (*pb.Target, error) {
|
||||
const op = "target_service.toProto"
|
||||
opts := handlers.GetOpts(opt...)
|
||||
if opts.WithOutputFields == nil {
|
||||
@@ -1381,15 +1495,28 @@ func toProto(ctx context.Context, in target.Target, m []*target.TargetSet, credS
|
||||
out.AuthorizedActions = opts.WithAuthorizedActions
|
||||
}
|
||||
if outputFields.Has(globals.HostSetIdsField) {
|
||||
for _, hs := range m {
|
||||
out.HostSetIds = append(out.HostSetIds, hs.GetPublicId())
|
||||
for _, hs := range hostSources {
|
||||
out.HostSetIds = append(out.HostSetIds, hs.Id())
|
||||
}
|
||||
}
|
||||
if outputFields.Has(globals.HostSourceIdsField) {
|
||||
for _, hs := range hostSources {
|
||||
out.HostSourceIds = append(out.HostSourceIds, hs.Id())
|
||||
}
|
||||
}
|
||||
if outputFields.Has(globals.HostSetsField) {
|
||||
for _, hs := range m {
|
||||
for _, hs := range hostSources {
|
||||
out.HostSets = append(out.HostSets, &pb.HostSet{
|
||||
Id: hs.GetPublicId(),
|
||||
HostCatalogId: hs.GetCatalogId(),
|
||||
Id: hs.Id(),
|
||||
HostCatalogId: hs.HostCatalogId(),
|
||||
})
|
||||
}
|
||||
}
|
||||
if outputFields.Has(globals.HostSourcesField) {
|
||||
for _, hs := range hostSources {
|
||||
out.HostSources = append(out.HostSources, &pb.HostSource{
|
||||
Id: hs.Id(),
|
||||
HostCatalogId: hs.HostCatalogId(),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1628,6 +1755,72 @@ func validateRemoveSetsRequest(req *pbs.RemoveTargetHostSetsRequest) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateAddHostSourcesRequest(req *pbs.AddTargetHostSourcesRequest) error {
|
||||
badFields := map[string]string{}
|
||||
if !handlers.ValidId(handlers.Id(req.GetId()), target.TcpTargetPrefix) {
|
||||
badFields[globals.IdField] = "Incorrectly formatted identifier."
|
||||
}
|
||||
if req.GetVersion() == 0 {
|
||||
badFields[globals.VersionField] = "Required field."
|
||||
}
|
||||
if len(req.GetHostSourceIds()) == 0 {
|
||||
badFields[globals.HostSourceIdsField] = "Must be non-empty."
|
||||
}
|
||||
for _, id := range req.GetHostSourceIds() {
|
||||
if !handlers.ValidId(handlers.Id(id), static.HostSetPrefix) {
|
||||
badFields[globals.HostSourceIdsField] = fmt.Sprintf("Incorrectly formatted host source identifier %q.", id)
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(badFields) > 0 {
|
||||
return handlers.InvalidArgumentErrorf("Errors in provided fields.", badFields)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateSetHostSourcesRequest(req *pbs.SetTargetHostSourcesRequest) error {
|
||||
badFields := map[string]string{}
|
||||
if !handlers.ValidId(handlers.Id(req.GetId()), target.TcpTargetPrefix) {
|
||||
badFields[globals.IdField] = "Incorrectly formatted identifier."
|
||||
}
|
||||
if req.GetVersion() == 0 {
|
||||
badFields[globals.VersionField] = "Required field."
|
||||
}
|
||||
for _, id := range req.GetHostSourceIds() {
|
||||
if !handlers.ValidId(handlers.Id(id), static.HostSetPrefix) {
|
||||
badFields[globals.HostSourceIdsField] = fmt.Sprintf("Incorrectly formatted host source identifier %q.", id)
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(badFields) > 0 {
|
||||
return handlers.InvalidArgumentErrorf("Errors in provided fields.", badFields)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateRemoveHostSourcesRequest(req *pbs.RemoveTargetHostSourcesRequest) error {
|
||||
badFields := map[string]string{}
|
||||
if !handlers.ValidId(handlers.Id(req.GetId()), target.TcpTargetPrefix) {
|
||||
badFields[globals.IdField] = "Incorrectly formatted identifier."
|
||||
}
|
||||
if req.GetVersion() == 0 {
|
||||
badFields[globals.VersionField] = "Required field."
|
||||
}
|
||||
if len(req.GetHostSourceIds()) == 0 {
|
||||
badFields[globals.HostSourceIdsField] = "Must be non-empty."
|
||||
}
|
||||
for _, id := range req.GetHostSourceIds() {
|
||||
if !handlers.ValidId(handlers.Id(id), static.HostSetPrefix) {
|
||||
badFields[globals.HostSourceIdsField] = fmt.Sprintf("Incorrectly formatted host source identifier %q.", id)
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(badFields) > 0 {
|
||||
return handlers.InvalidArgumentErrorf("Errors in provided fields.", badFields)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateAddLibrariesRequest(req *pbs.AddTargetCredentialLibrariesRequest) error {
|
||||
badFields := map[string]string{}
|
||||
if !handlers.ValidId(handlers.Id(req.GetId()), target.TcpTargetPrefix) {
|
||||
|
||||
@@ -108,7 +108,7 @@ func TestGet(t *testing.T) {
|
||||
hc := static.TestCatalogs(t, conn, proj.GetPublicId(), 1)[0]
|
||||
hs := static.TestSets(t, conn, hc.GetPublicId(), 2)
|
||||
|
||||
tar := target.TestTcpTarget(t, conn, proj.GetPublicId(), "test", target.WithHostSets([]string{hs[0].GetPublicId(), hs[1].GetPublicId()}))
|
||||
tar := target.TestTcpTarget(t, conn, proj.GetPublicId(), "test", target.WithHostSources([]string{hs[0].GetPublicId(), hs[1].GetPublicId()}))
|
||||
|
||||
pTar := &pb.Target{
|
||||
Id: tar.GetPublicId(),
|
||||
@@ -119,6 +119,7 @@ func TestGet(t *testing.T) {
|
||||
Scope: &scopes.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String(), ParentScopeId: o.GetPublicId()},
|
||||
Type: target.TcpTargetType.String(),
|
||||
HostSetIds: []string{hs[0].GetPublicId(), hs[1].GetPublicId()},
|
||||
HostSourceIds: []string{hs[0].GetPublicId(), hs[1].GetPublicId()},
|
||||
Attributes: new(structpb.Struct),
|
||||
SessionMaxSeconds: wrapperspb.UInt32(28800),
|
||||
SessionConnectionLimit: wrapperspb.Int32(1),
|
||||
@@ -126,6 +127,7 @@ func TestGet(t *testing.T) {
|
||||
}
|
||||
for _, ihs := range hs {
|
||||
pTar.HostSets = append(pTar.HostSets, &pb.HostSet{Id: ihs.GetPublicId(), HostCatalogId: ihs.GetCatalogId()})
|
||||
pTar.HostSources = append(pTar.HostSources, &pb.HostSource{Id: ihs.GetPublicId(), HostCatalogId: ihs.GetCatalogId()})
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
@@ -201,7 +203,7 @@ func TestList(t *testing.T) {
|
||||
var totalTars []*pb.Target
|
||||
for i := 0; i < 5; i++ {
|
||||
name := fmt.Sprintf("tar%d", i)
|
||||
tar := target.TestTcpTarget(t, conn, proj.GetPublicId(), name, target.WithHostSets([]string{hss[0].GetPublicId(), hss[1].GetPublicId()}))
|
||||
tar := target.TestTcpTarget(t, conn, proj.GetPublicId(), name, target.WithHostSources([]string{hss[0].GetPublicId(), hss[1].GetPublicId()}))
|
||||
wantTars = append(wantTars, &pb.Target{
|
||||
Id: tar.GetPublicId(),
|
||||
ScopeId: proj.GetPublicId(),
|
||||
@@ -217,7 +219,7 @@ func TestList(t *testing.T) {
|
||||
AuthorizedActions: testAuthorizedActions,
|
||||
})
|
||||
totalTars = append(totalTars, wantTars[i])
|
||||
tar = target.TestTcpTarget(t, conn, otherProj.GetPublicId(), name, target.WithHostSets([]string{otherHss[0].GetPublicId(), otherHss[1].GetPublicId()}))
|
||||
tar = target.TestTcpTarget(t, conn, otherProj.GetPublicId(), name, target.WithHostSources([]string{otherHss[0].GetPublicId(), otherHss[1].GetPublicId()}))
|
||||
totalTars = append(totalTars, &pb.Target{
|
||||
Id: tar.GetPublicId(),
|
||||
ScopeId: otherProj.GetPublicId(),
|
||||
@@ -564,11 +566,16 @@ func TestUpdate(t *testing.T) {
|
||||
{Id: hs[0].GetPublicId(), HostCatalogId: hs[0].GetCatalogId()},
|
||||
{Id: hs[1].GetPublicId(), HostCatalogId: hs[1].GetCatalogId()},
|
||||
}
|
||||
hostSourceIds := []string{hs[0].GetPublicId(), hs[1].GetPublicId()}
|
||||
hostSources := []*pb.HostSource{
|
||||
{Id: hs[0].GetPublicId(), HostCatalogId: hs[0].GetCatalogId()},
|
||||
{Id: hs[1].GetPublicId(), HostCatalogId: hs[1].GetCatalogId()},
|
||||
}
|
||||
|
||||
tar, err := target.NewTcpTarget(proj.GetPublicId(), target.WithName("default"), target.WithDescription("default"))
|
||||
tar.DefaultPort = 2
|
||||
require.NoError(t, err)
|
||||
gtar, _, _, err := repo.CreateTcpTarget(context.Background(), tar, target.WithHostSets([]string{hs[0].GetPublicId(), hs[1].GetPublicId()}))
|
||||
gtar, _, _, err := repo.CreateTcpTarget(context.Background(), tar, target.WithHostSources([]string{hs[0].GetPublicId(), hs[1].GetPublicId()}))
|
||||
require.NoError(t, err)
|
||||
tar = gtar.(*target.TcpTarget)
|
||||
|
||||
@@ -623,6 +630,8 @@ func TestUpdate(t *testing.T) {
|
||||
CreatedTime: tar.GetCreateTime().GetTimestamp(),
|
||||
HostSetIds: hsIds,
|
||||
HostSets: hostSets,
|
||||
HostSourceIds: hostSourceIds,
|
||||
HostSources: hostSources,
|
||||
SessionMaxSeconds: wrapperspb.UInt32(3600),
|
||||
SessionConnectionLimit: wrapperspb.Int32(5),
|
||||
AuthorizedActions: testAuthorizedActions,
|
||||
@@ -655,6 +664,8 @@ func TestUpdate(t *testing.T) {
|
||||
}},
|
||||
HostSetIds: hsIds,
|
||||
HostSets: hostSets,
|
||||
HostSourceIds: hostSourceIds,
|
||||
HostSources: hostSources,
|
||||
SessionMaxSeconds: wrapperspb.UInt32(3600),
|
||||
SessionConnectionLimit: wrapperspb.Int32(5),
|
||||
AuthorizedActions: testAuthorizedActions,
|
||||
@@ -740,6 +751,8 @@ func TestUpdate(t *testing.T) {
|
||||
}},
|
||||
HostSetIds: hsIds,
|
||||
HostSets: hostSets,
|
||||
HostSourceIds: hostSourceIds,
|
||||
HostSources: hostSources,
|
||||
SessionMaxSeconds: wrapperspb.UInt32(3600),
|
||||
SessionConnectionLimit: wrapperspb.Int32(5),
|
||||
AuthorizedActions: testAuthorizedActions,
|
||||
@@ -771,6 +784,8 @@ func TestUpdate(t *testing.T) {
|
||||
}},
|
||||
HostSetIds: hsIds,
|
||||
HostSets: hostSets,
|
||||
HostSourceIds: hostSourceIds,
|
||||
HostSources: hostSources,
|
||||
SessionMaxSeconds: wrapperspb.UInt32(3600),
|
||||
SessionConnectionLimit: wrapperspb.Int32(5),
|
||||
AuthorizedActions: testAuthorizedActions,
|
||||
@@ -802,6 +817,8 @@ func TestUpdate(t *testing.T) {
|
||||
Type: target.TcpTargetType.String(),
|
||||
HostSetIds: hsIds,
|
||||
HostSets: hostSets,
|
||||
HostSourceIds: hostSourceIds,
|
||||
HostSources: hostSources,
|
||||
SessionMaxSeconds: wrapperspb.UInt32(3600),
|
||||
SessionConnectionLimit: wrapperspb.Int32(5),
|
||||
AuthorizedActions: testAuthorizedActions,
|
||||
@@ -977,13 +994,13 @@ func TestAddTargetHostSets(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Add set on populated target",
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "populated", target.WithHostSets([]string{hs[0].GetPublicId()})),
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "populated", target.WithHostSources([]string{hs[0].GetPublicId()})),
|
||||
addHostSets: []string{hs[1].GetPublicId()},
|
||||
resultHostSets: []string{hs[0].GetPublicId(), hs[1].GetPublicId()},
|
||||
},
|
||||
{
|
||||
name: "Add duplicated sets on populated target",
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "duplicated", target.WithHostSets([]string{hs[0].GetPublicId()})),
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "duplicated", target.WithHostSources([]string{hs[0].GetPublicId()})),
|
||||
addHostSets: []string{hs[1].GetPublicId(), hs[1].GetPublicId()},
|
||||
resultHostSets: []string{hs[0].GetPublicId(), hs[1].GetPublicId()},
|
||||
},
|
||||
@@ -1093,19 +1110,19 @@ func TestSetTargetHostSets(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Set on populated target",
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "populated", target.WithHostSets([]string{hs[0].GetPublicId()})),
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "populated", target.WithHostSources([]string{hs[0].GetPublicId()})),
|
||||
setHostSets: []string{hs[1].GetPublicId()},
|
||||
resultHostSets: []string{hs[1].GetPublicId()},
|
||||
},
|
||||
{
|
||||
name: "Set duplicate host set on populated target",
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "duplicate", target.WithHostSets([]string{hs[0].GetPublicId()})),
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "duplicate", target.WithHostSources([]string{hs[0].GetPublicId()})),
|
||||
setHostSets: []string{hs[1].GetPublicId(), hs[1].GetPublicId()},
|
||||
resultHostSets: []string{hs[1].GetPublicId()},
|
||||
},
|
||||
{
|
||||
name: "Set empty on populated target",
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "another populated", target.WithHostSets([]string{hs[0].GetPublicId()})),
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "another populated", target.WithHostSources([]string{hs[0].GetPublicId()})),
|
||||
setHostSets: []string{},
|
||||
resultHostSets: nil,
|
||||
},
|
||||
@@ -1204,19 +1221,19 @@ func TestRemoveTargetHostSets(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "Remove 1 of 2 sets",
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "remove partial", target.WithHostSets([]string{hs[0].GetPublicId(), hs[1].GetPublicId()})),
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "remove partial", target.WithHostSources([]string{hs[0].GetPublicId(), hs[1].GetPublicId()})),
|
||||
removeHosts: []string{hs[1].GetPublicId()},
|
||||
resultHosts: []string{hs[0].GetPublicId()},
|
||||
},
|
||||
{
|
||||
name: "Remove 1 duplicate set of 2 sets",
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "remove duplicate", target.WithHostSets([]string{hs[0].GetPublicId(), hs[1].GetPublicId()})),
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "remove duplicate", target.WithHostSources([]string{hs[0].GetPublicId(), hs[1].GetPublicId()})),
|
||||
removeHosts: []string{hs[1].GetPublicId(), hs[1].GetPublicId()},
|
||||
resultHosts: []string{hs[0].GetPublicId()},
|
||||
},
|
||||
{
|
||||
name: "Remove all hosts from set",
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "remove all", target.WithHostSets([]string{hs[0].GetPublicId(), hs[1].GetPublicId()})),
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "remove all", target.WithHostSources([]string{hs[0].GetPublicId(), hs[1].GetPublicId()})),
|
||||
removeHosts: []string{hs[0].GetPublicId(), hs[1].GetPublicId()},
|
||||
resultHosts: []string{},
|
||||
},
|
||||
@@ -1299,6 +1316,360 @@ func TestRemoveTargetHostSets(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddTargetHostSources(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iamRepo, nil
|
||||
}
|
||||
|
||||
_, proj := iam.TestScopes(t, iamRepo)
|
||||
|
||||
s, err := testService(t, conn, kms, wrapper)
|
||||
require.NoError(t, err, "Error when getting new target service.")
|
||||
|
||||
hc := static.TestCatalogs(t, conn, proj.GetPublicId(), 1)[0]
|
||||
hs := static.TestSets(t, conn, hc.GetPublicId(), 2)
|
||||
|
||||
addCases := []struct {
|
||||
name string
|
||||
tar *target.TcpTarget
|
||||
addHostSources []string
|
||||
resultHostSources []string
|
||||
}{
|
||||
{
|
||||
name: "Add set on empty target",
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "empty"),
|
||||
addHostSources: []string{hs[1].GetPublicId()},
|
||||
resultHostSources: []string{hs[1].GetPublicId()},
|
||||
},
|
||||
{
|
||||
name: "Add set on populated target",
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "populated", target.WithHostSources([]string{hs[0].GetPublicId()})),
|
||||
addHostSources: []string{hs[1].GetPublicId()},
|
||||
resultHostSources: []string{hs[0].GetPublicId(), hs[1].GetPublicId()},
|
||||
},
|
||||
{
|
||||
name: "Add duplicated sets on populated target",
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "duplicated", target.WithHostSources([]string{hs[0].GetPublicId()})),
|
||||
addHostSources: []string{hs[1].GetPublicId(), hs[1].GetPublicId()},
|
||||
resultHostSources: []string{hs[0].GetPublicId(), hs[1].GetPublicId()},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range addCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
req := &pbs.AddTargetHostSourcesRequest{
|
||||
Id: tc.tar.GetPublicId(),
|
||||
Version: tc.tar.GetVersion(),
|
||||
HostSourceIds: tc.addHostSources,
|
||||
}
|
||||
|
||||
got, err := s.AddTargetHostSources(auth.DisabledAuthTestContext(iamRepoFn, proj.GetPublicId()), req)
|
||||
s, ok := status.FromError(err)
|
||||
require.True(t, ok)
|
||||
require.NoError(t, err, "Got error: %v", s)
|
||||
|
||||
assert.ElementsMatch(t, tc.resultHostSources, got.GetItem().GetHostSourceIds())
|
||||
})
|
||||
}
|
||||
|
||||
tar := target.TestTcpTarget(t, conn, proj.GetPublicId(), "test")
|
||||
|
||||
failCases := []struct {
|
||||
name string
|
||||
req *pbs.AddTargetHostSourcesRequest
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "Bad Set Id",
|
||||
req: &pbs.AddTargetHostSourcesRequest{
|
||||
Id: "bad id",
|
||||
Version: tar.GetVersion(),
|
||||
HostSourceIds: []string{hs[0].GetPublicId()},
|
||||
},
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Bad version",
|
||||
req: &pbs.AddTargetHostSourcesRequest{
|
||||
Id: tar.GetPublicId(),
|
||||
Version: tar.GetVersion() + 2,
|
||||
HostSourceIds: []string{hs[0].GetPublicId()},
|
||||
},
|
||||
err: handlers.ApiErrorWithCode(codes.Internal),
|
||||
},
|
||||
{
|
||||
name: "Empty host set list",
|
||||
req: &pbs.AddTargetHostSourcesRequest{
|
||||
Id: tar.GetPublicId(),
|
||||
Version: tar.GetVersion(),
|
||||
},
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Incorrect host set ids",
|
||||
req: &pbs.AddTargetHostSourcesRequest{
|
||||
Id: tar.GetPublicId(),
|
||||
Version: tar.GetVersion(),
|
||||
HostSourceIds: []string{"incorrect"},
|
||||
},
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
}
|
||||
for _, tc := range failCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
_, gErr := s.AddTargetHostSources(auth.DisabledAuthTestContext(iamRepoFn, proj.GetPublicId()), tc.req)
|
||||
if tc.err != nil {
|
||||
require.Error(gErr)
|
||||
assert.True(errors.Is(gErr, tc.err), "AddTargetHostSources(%+v) got error %v, wanted %v", tc.req, gErr, tc.err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetTargetHostSources(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iamRepo, nil
|
||||
}
|
||||
|
||||
_, proj := iam.TestScopes(t, iamRepo)
|
||||
|
||||
s, err := testService(t, conn, kms, wrapper)
|
||||
require.NoError(t, err, "Error when getting new host set service.")
|
||||
|
||||
hc := static.TestCatalogs(t, conn, proj.GetPublicId(), 1)[0]
|
||||
hs := static.TestSets(t, conn, hc.GetPublicId(), 2)
|
||||
|
||||
setCases := []struct {
|
||||
name string
|
||||
tar *target.TcpTarget
|
||||
setHostSources []string
|
||||
resultHostSources []string
|
||||
}{
|
||||
{
|
||||
name: "Set on empty target",
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "empty"),
|
||||
setHostSources: []string{hs[1].GetPublicId()},
|
||||
resultHostSources: []string{hs[1].GetPublicId()},
|
||||
},
|
||||
{
|
||||
name: "Set on populated target",
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "populated", target.WithHostSources([]string{hs[0].GetPublicId()})),
|
||||
setHostSources: []string{hs[1].GetPublicId()},
|
||||
resultHostSources: []string{hs[1].GetPublicId()},
|
||||
},
|
||||
{
|
||||
name: "Set duplicate host set on populated target",
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "duplicate", target.WithHostSources([]string{hs[0].GetPublicId()})),
|
||||
setHostSources: []string{hs[1].GetPublicId(), hs[1].GetPublicId()},
|
||||
resultHostSources: []string{hs[1].GetPublicId()},
|
||||
},
|
||||
{
|
||||
name: "Set empty on populated target",
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "another populated", target.WithHostSources([]string{hs[0].GetPublicId()})),
|
||||
setHostSources: []string{},
|
||||
resultHostSources: nil,
|
||||
},
|
||||
}
|
||||
for _, tc := range setCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
req := &pbs.SetTargetHostSourcesRequest{
|
||||
Id: tc.tar.GetPublicId(),
|
||||
Version: tc.tar.GetVersion(),
|
||||
HostSourceIds: tc.setHostSources,
|
||||
}
|
||||
|
||||
got, err := s.SetTargetHostSources(auth.DisabledAuthTestContext(iamRepoFn, proj.GetPublicId()), req)
|
||||
require.NoError(t, err, "Got error: %v", s)
|
||||
assert.ElementsMatch(t, tc.resultHostSources, got.GetItem().GetHostSourceIds())
|
||||
})
|
||||
}
|
||||
|
||||
tar := target.TestTcpTarget(t, conn, proj.GetPublicId(), "test name")
|
||||
|
||||
failCases := []struct {
|
||||
name string
|
||||
req *pbs.SetTargetHostSourcesRequest
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "Bad target Id",
|
||||
req: &pbs.SetTargetHostSourcesRequest{
|
||||
Id: "bad id",
|
||||
Version: tar.GetVersion(),
|
||||
HostSourceIds: []string{hs[0].GetPublicId()},
|
||||
},
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Bad version",
|
||||
req: &pbs.SetTargetHostSourcesRequest{
|
||||
Id: tar.GetPublicId(),
|
||||
Version: tar.GetVersion() + 3,
|
||||
HostSourceIds: []string{hs[0].GetPublicId()},
|
||||
},
|
||||
err: handlers.ApiErrorWithCode(codes.Internal),
|
||||
},
|
||||
{
|
||||
name: "Bad host set id",
|
||||
req: &pbs.SetTargetHostSourcesRequest{
|
||||
Id: tar.GetPublicId(),
|
||||
Version: tar.GetVersion(),
|
||||
HostSourceIds: []string{"invalid"},
|
||||
},
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
}
|
||||
for _, tc := range failCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
_, gErr := s.SetTargetHostSources(auth.DisabledAuthTestContext(iamRepoFn, proj.GetPublicId()), tc.req)
|
||||
if tc.err != nil {
|
||||
require.Error(gErr)
|
||||
assert.True(errors.Is(gErr, tc.err), "SetTargetHostSources(%+v) got error %v, wanted %v", tc.req, gErr, tc.err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveTargetHostSources(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
kms := kms.TestKms(t, conn, wrapper)
|
||||
|
||||
iamRepo := iam.TestRepo(t, conn, wrapper)
|
||||
iamRepoFn := func() (*iam.Repository, error) {
|
||||
return iamRepo, nil
|
||||
}
|
||||
|
||||
_, proj := iam.TestScopes(t, iamRepo)
|
||||
|
||||
s, err := testService(t, conn, kms, wrapper)
|
||||
require.NoError(t, err, "Error when getting new host set service.")
|
||||
|
||||
hc := static.TestCatalogs(t, conn, proj.GetPublicId(), 1)[0]
|
||||
hs := static.TestSets(t, conn, hc.GetPublicId(), 2)
|
||||
|
||||
removeCases := []struct {
|
||||
name string
|
||||
tar *target.TcpTarget
|
||||
removeHostSources []string
|
||||
resultHostSources []string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Remove from empty",
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "empty"),
|
||||
removeHostSources: []string{hs[1].GetPublicId()},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Remove 1 of 2 sets",
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "remove partial", target.WithHostSources([]string{hs[0].GetPublicId(), hs[1].GetPublicId()})),
|
||||
removeHostSources: []string{hs[1].GetPublicId()},
|
||||
resultHostSources: []string{hs[0].GetPublicId()},
|
||||
},
|
||||
{
|
||||
name: "Remove 1 duplicate set of 2 sets",
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "remove duplicate", target.WithHostSources([]string{hs[0].GetPublicId(), hs[1].GetPublicId()})),
|
||||
removeHostSources: []string{hs[1].GetPublicId(), hs[1].GetPublicId()},
|
||||
resultHostSources: []string{hs[0].GetPublicId()},
|
||||
},
|
||||
{
|
||||
name: "Remove all hosts from set",
|
||||
tar: target.TestTcpTarget(t, conn, proj.GetPublicId(), "remove all", target.WithHostSources([]string{hs[0].GetPublicId(), hs[1].GetPublicId()})),
|
||||
removeHostSources: []string{hs[0].GetPublicId(), hs[1].GetPublicId()},
|
||||
resultHostSources: []string{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range removeCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
req := &pbs.RemoveTargetHostSourcesRequest{
|
||||
Id: tc.tar.GetPublicId(),
|
||||
Version: tc.tar.GetVersion(),
|
||||
HostSourceIds: tc.removeHostSources,
|
||||
}
|
||||
|
||||
got, err := s.RemoveTargetHostSources(auth.DisabledAuthTestContext(iamRepoFn, proj.GetPublicId()), req)
|
||||
if tc.wantErr {
|
||||
assert.Error(t, err)
|
||||
return
|
||||
}
|
||||
s, ok := status.FromError(err)
|
||||
require.True(t, ok)
|
||||
require.NoError(t, err, "Got error: %v", s)
|
||||
|
||||
assert.ElementsMatch(t, tc.resultHostSources, got.GetItem().GetHostSourceIds())
|
||||
})
|
||||
}
|
||||
|
||||
tar := target.TestTcpTarget(t, conn, proj.GetPublicId(), "testing")
|
||||
|
||||
failCases := []struct {
|
||||
name string
|
||||
req *pbs.RemoveTargetHostSourcesRequest
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "Bad version",
|
||||
req: &pbs.RemoveTargetHostSourcesRequest{
|
||||
Id: tar.GetPublicId(),
|
||||
Version: tar.GetVersion() + 3,
|
||||
HostSourceIds: []string{hs[0].GetPublicId()},
|
||||
},
|
||||
err: handlers.ApiErrorWithCode(codes.Internal),
|
||||
},
|
||||
{
|
||||
name: "Bad target Id",
|
||||
req: &pbs.RemoveTargetHostSourcesRequest{
|
||||
Id: "bad id",
|
||||
Version: tar.GetVersion(),
|
||||
HostSourceIds: []string{hs[0].GetPublicId()},
|
||||
},
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "empty sets",
|
||||
req: &pbs.RemoveTargetHostSourcesRequest{
|
||||
Id: tar.GetPublicId(),
|
||||
Version: tar.GetVersion(),
|
||||
HostSourceIds: []string{},
|
||||
},
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
{
|
||||
name: "Invalid set ids",
|
||||
req: &pbs.RemoveTargetHostSourcesRequest{
|
||||
Id: tar.GetPublicId(),
|
||||
Version: tar.GetVersion(),
|
||||
HostSourceIds: []string{"invalid"},
|
||||
},
|
||||
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
|
||||
},
|
||||
}
|
||||
for _, tc := range failCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
_, gErr := s.RemoveTargetHostSources(auth.DisabledAuthTestContext(iamRepoFn, proj.GetPublicId()), tc.req)
|
||||
if tc.err != nil {
|
||||
require.Error(gErr)
|
||||
assert.True(errors.Is(gErr, tc.err), "RemoveTargetHostSets(%+v) got error %v, wanted %v", tc.req, gErr, tc.err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddTargetLibraries(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
Reference in new issue
Block a user