Add alias resolution to target service (#4277)
* Add aliases to target resource and alias resolution to target service * Wire up the target alias domain package to the target service handler * Adding alias resolution to target api tests
This commit is contained in:
1 parent
92181e272a
commit
3f3b624b54
12 files changed
+1359
-631
No files matched your search
@@ -262,6 +262,7 @@ func (c *Controller) registerGrpcServices(s *grpc.Server) error {
|
||||
c.StaticHostRepoFn,
|
||||
c.VaultCredentialRepoFn,
|
||||
c.StaticCredentialRepoFn,
|
||||
c.TargetAliasRepoFn,
|
||||
c.downstreamWorkers,
|
||||
c.workerStatusGracePeriod,
|
||||
c.conf.RawConfig.Controller.MaxPageSize,
|
||||
|
||||
@@ -136,7 +136,7 @@ func TestHandleImplementedPaths(t *testing.T) {
|
||||
"v1/storage-buckets",
|
||||
"v1/storage-buckets/someid",
|
||||
"v1/targets",
|
||||
"v1/targets/someid",
|
||||
"v1/targets/some_id",
|
||||
"v1/users",
|
||||
"v1/users/someid",
|
||||
"v1/billing:monthly-active-users",
|
||||
@@ -174,13 +174,13 @@ func TestHandleImplementedPaths(t *testing.T) {
|
||||
"v1/roles/someid:set-principals",
|
||||
"v1/roles/someid:remove-principals",
|
||||
"v1/sessions/someid:cancel",
|
||||
"v1/targets/someid:authorize-session",
|
||||
"v1/targets/someid:add-host-sources",
|
||||
"v1/targets/someid:set-host-sources",
|
||||
"v1/targets/someid:remove-host-sources",
|
||||
"v1/targets/someid:add-credential-sources",
|
||||
"v1/targets/someid:set-credential-sources",
|
||||
"v1/targets/someid:remove-credential-sources",
|
||||
"v1/targets/some_id:authorize-session",
|
||||
"v1/targets/some_id:add-host-sources",
|
||||
"v1/targets/some_id:set-host-sources",
|
||||
"v1/targets/some_id:remove-host-sources",
|
||||
"v1/targets/some_id:add-credential-sources",
|
||||
"v1/targets/some_id:set-credential-sources",
|
||||
"v1/targets/some_id:remove-credential-sources",
|
||||
"v1/users/someid:add-accounts",
|
||||
"v1/users/someid:set-accounts",
|
||||
"v1/users/someid:remove-accounts",
|
||||
@@ -198,7 +198,7 @@ func TestHandleImplementedPaths(t *testing.T) {
|
||||
"v1/roles/someid",
|
||||
"v1/scopes/someid",
|
||||
"v1/storage-buckets/someid",
|
||||
"v1/targets/someid",
|
||||
"v1/targets/some_id",
|
||||
"v1/users/someid",
|
||||
},
|
||||
"PATCH": {
|
||||
@@ -213,7 +213,7 @@ func TestHandleImplementedPaths(t *testing.T) {
|
||||
"v1/roles/someid",
|
||||
"v1/scopes/someid",
|
||||
"v1/storage-buckets/someid",
|
||||
"v1/targets/someid",
|
||||
"v1/targets/some_id",
|
||||
"v1/users/someid",
|
||||
},
|
||||
} {
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/globals"
|
||||
talias "github.com/hashicorp/boundary/internal/alias/target"
|
||||
"github.com/hashicorp/boundary/internal/credential"
|
||||
wl "github.com/hashicorp/boundary/internal/daemon/common"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller/auth"
|
||||
@@ -118,6 +119,7 @@ type Service struct {
|
||||
pbs.UnsafeTargetServiceServer
|
||||
|
||||
repoFn target.RepositoryFactory
|
||||
aliasRepoFn common.TargetAliasRepoFactory
|
||||
iamRepoFn common.IamRepoFactory
|
||||
serversRepoFn common.ServersRepoFactory
|
||||
sessionRepoFn session.RepositoryFactory
|
||||
@@ -146,38 +148,34 @@ func NewService(
|
||||
staticHostRepoFn common.StaticRepoFactory,
|
||||
vaultCredRepoFn common.VaultCredentialRepoFactory,
|
||||
staticCredRepoFn common.StaticCredentialRepoFactory,
|
||||
aliasRepoFn common.TargetAliasRepoFactory,
|
||||
downstreams common.Downstreamers,
|
||||
workerStatusGracePeriod *atomic.Int64,
|
||||
maxPageSize uint,
|
||||
controllerExt intglobals.ControllerExtension,
|
||||
) (Service, error) {
|
||||
const op = "targets.NewService"
|
||||
if kmsCache == nil {
|
||||
switch {
|
||||
case kmsCache == nil:
|
||||
return Service{}, errors.New(ctx, errors.InvalidParameter, op, "missing kms repo")
|
||||
}
|
||||
if repoFn == nil {
|
||||
case repoFn == nil:
|
||||
return Service{}, errors.New(ctx, errors.InvalidParameter, op, "missing target repository")
|
||||
}
|
||||
if iamRepoFn == nil {
|
||||
case iamRepoFn == nil:
|
||||
return Service{}, errors.New(ctx, errors.InvalidParameter, op, "missing iam repository")
|
||||
}
|
||||
if serversRepoFn == nil {
|
||||
case serversRepoFn == nil:
|
||||
return Service{}, errors.New(ctx, errors.InvalidParameter, op, "missing server repository")
|
||||
}
|
||||
if sessionRepoFn == nil {
|
||||
case sessionRepoFn == nil:
|
||||
return Service{}, errors.New(ctx, errors.InvalidParameter, op, "missing session repository")
|
||||
}
|
||||
if pluginHostRepoFn == nil {
|
||||
case pluginHostRepoFn == nil:
|
||||
return Service{}, errors.New(ctx, errors.InvalidParameter, op, "missing plugin host repository")
|
||||
}
|
||||
if staticHostRepoFn == nil {
|
||||
case staticHostRepoFn == nil:
|
||||
return Service{}, errors.New(ctx, errors.InvalidParameter, op, "missing static host repository")
|
||||
}
|
||||
if vaultCredRepoFn == nil {
|
||||
case vaultCredRepoFn == nil:
|
||||
return Service{}, errors.New(ctx, errors.InvalidParameter, op, "missing vault credential repository")
|
||||
}
|
||||
if staticCredRepoFn == nil {
|
||||
case staticCredRepoFn == nil:
|
||||
return Service{}, errors.New(ctx, errors.InvalidParameter, op, "missing static credential repository")
|
||||
case aliasRepoFn == nil:
|
||||
return Service{}, errors.New(ctx, errors.InvalidParameter, op, "missing target alias repository")
|
||||
}
|
||||
if maxPageSize == 0 {
|
||||
maxPageSize = uint(globals.DefaultMaxPageSize)
|
||||
@@ -191,6 +189,7 @@ func NewService(
|
||||
staticHostRepoFn: staticHostRepoFn,
|
||||
vaultCredRepoFn: vaultCredRepoFn,
|
||||
staticCredRepoFn: staticCredRepoFn,
|
||||
aliasRepoFn: aliasRepoFn,
|
||||
downstreams: downstreams,
|
||||
kmsCache: kmsCache,
|
||||
workerStatusGracePeriod: workerStatusGracePeriod,
|
||||
@@ -356,6 +355,14 @@ func (s Service) ListTargets(ctx context.Context, req *pbs.ListTargetsRequest) (
|
||||
func (s Service) GetTarget(ctx context.Context, req *pbs.GetTargetRequest) (*pbs.GetTargetResponse, error) {
|
||||
const op = "targets.(Service).GetTarget"
|
||||
|
||||
if maybeAlias(req.GetId()) {
|
||||
if a, err := s.resolveAlias(ctx, req.GetId()); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
} else {
|
||||
req.Id = a.DestinationId
|
||||
}
|
||||
}
|
||||
|
||||
if err := validateGetRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -436,6 +443,14 @@ func (s Service) CreateTarget(ctx context.Context, req *pbs.CreateTargetRequest)
|
||||
func (s Service) UpdateTarget(ctx context.Context, req *pbs.UpdateTargetRequest) (*pbs.UpdateTargetResponse, error) {
|
||||
const op = "targets.(Service).UpdateTarget"
|
||||
|
||||
if maybeAlias(req.GetId()) {
|
||||
if a, err := s.resolveAlias(ctx, req.GetId()); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
} else {
|
||||
req.Id = a.DestinationId
|
||||
}
|
||||
}
|
||||
|
||||
if err := validateUpdateRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -474,6 +489,16 @@ func (s Service) UpdateTarget(ctx context.Context, req *pbs.UpdateTargetRequest)
|
||||
|
||||
// DeleteTarget implements the interface pbs.TargetServiceServer.
|
||||
func (s Service) DeleteTarget(ctx context.Context, req *pbs.DeleteTargetRequest) (*pbs.DeleteTargetResponse, error) {
|
||||
const op = "targets.(Service).DeleteTarget"
|
||||
|
||||
if maybeAlias(req.GetId()) {
|
||||
if a, err := s.resolveAlias(ctx, req.GetId()); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
} else {
|
||||
req.Id = a.DestinationId
|
||||
}
|
||||
}
|
||||
|
||||
if err := validateDeleteRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -492,6 +517,14 @@ func (s Service) DeleteTarget(ctx context.Context, req *pbs.DeleteTargetRequest)
|
||||
func (s Service) AddTargetHostSources(ctx context.Context, req *pbs.AddTargetHostSourcesRequest) (*pbs.AddTargetHostSourcesResponse, error) {
|
||||
const op = "targets.(Service).AddTargetHostSources"
|
||||
|
||||
if maybeAlias(req.GetId()) {
|
||||
if a, err := s.resolveAlias(ctx, req.GetId()); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
} else {
|
||||
req.Id = a.DestinationId
|
||||
}
|
||||
}
|
||||
|
||||
if err := validateAddHostSourcesRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -534,6 +567,14 @@ func (s Service) AddTargetHostSources(ctx context.Context, req *pbs.AddTargetHos
|
||||
func (s Service) SetTargetHostSources(ctx context.Context, req *pbs.SetTargetHostSourcesRequest) (*pbs.SetTargetHostSourcesResponse, error) {
|
||||
const op = "targets.(Service).SetTargetHostSources"
|
||||
|
||||
if maybeAlias(req.GetId()) {
|
||||
if a, err := s.resolveAlias(ctx, req.GetId()); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
} else {
|
||||
req.Id = a.DestinationId
|
||||
}
|
||||
}
|
||||
|
||||
if err := validateSetHostSourcesRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -574,6 +615,14 @@ func (s Service) SetTargetHostSources(ctx context.Context, req *pbs.SetTargetHos
|
||||
func (s Service) RemoveTargetHostSources(ctx context.Context, req *pbs.RemoveTargetHostSourcesRequest) (*pbs.RemoveTargetHostSourcesResponse, error) {
|
||||
const op = "targets.(Service).RemoveTargetHostSources"
|
||||
|
||||
if maybeAlias(req.GetId()) {
|
||||
if a, err := s.resolveAlias(ctx, req.GetId()); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
} else {
|
||||
req.Id = a.DestinationId
|
||||
}
|
||||
}
|
||||
|
||||
if err := validateRemoveHostSourcesRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -614,6 +663,14 @@ func (s Service) RemoveTargetHostSources(ctx context.Context, req *pbs.RemoveTar
|
||||
func (s Service) AddTargetCredentialSources(ctx context.Context, req *pbs.AddTargetCredentialSourcesRequest) (*pbs.AddTargetCredentialSourcesResponse, error) {
|
||||
const op = "targets.(Service).AddTargetCredentialSources"
|
||||
|
||||
if maybeAlias(req.GetId()) {
|
||||
if a, err := s.resolveAlias(ctx, req.GetId()); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
} else {
|
||||
req.Id = a.DestinationId
|
||||
}
|
||||
}
|
||||
|
||||
if err := validateAddCredentialSourcesRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -655,6 +712,14 @@ func (s Service) AddTargetCredentialSources(ctx context.Context, req *pbs.AddTar
|
||||
func (s Service) SetTargetCredentialSources(ctx context.Context, req *pbs.SetTargetCredentialSourcesRequest) (*pbs.SetTargetCredentialSourcesResponse, error) {
|
||||
const op = "targets.(Service).SetTargetCredentialSources"
|
||||
|
||||
if maybeAlias(req.GetId()) {
|
||||
if a, err := s.resolveAlias(ctx, req.GetId()); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
} else {
|
||||
req.Id = a.DestinationId
|
||||
}
|
||||
}
|
||||
|
||||
if err := validateSetCredentialSourcesRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -696,6 +761,14 @@ func (s Service) SetTargetCredentialSources(ctx context.Context, req *pbs.SetTar
|
||||
func (s Service) RemoveTargetCredentialSources(ctx context.Context, req *pbs.RemoveTargetCredentialSourcesRequest) (*pbs.RemoveTargetCredentialSourcesResponse, error) {
|
||||
const op = "targets.(Service).RemoveTargetCredentialSources"
|
||||
|
||||
if maybeAlias(req.GetId()) {
|
||||
if a, err := s.resolveAlias(ctx, req.GetId()); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
} else {
|
||||
req.Id = a.DestinationId
|
||||
}
|
||||
}
|
||||
|
||||
if err := validateRemoveCredentialSourcesRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -781,6 +854,27 @@ func DefaultPostSessionAuthorizationCallback(context.Context, intglobals.Control
|
||||
|
||||
func (s Service) AuthorizeSession(ctx context.Context, req *pbs.AuthorizeSessionRequest) (_ *pbs.AuthorizeSessionResponse, retErr error) {
|
||||
const op = "targets.(Service).AuthorizeSession"
|
||||
|
||||
if maybeAlias(req.GetId()) && len(req.GetScopeId()) == 0 && len(req.GetScopeName()) == 0 {
|
||||
// AuthorizeSession allows passing the target name in the place of an id but
|
||||
// requires that the scope id or scope name is also provided in the request.
|
||||
// If the scope id or the scope name is provided, we will not attempt to
|
||||
// resolve the alias.
|
||||
if a, err := s.resolveAlias(ctx, req.GetId()); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
} else {
|
||||
req.Id = a.DestinationId
|
||||
if a.HostId != "" {
|
||||
if req.GetHostId() != "" && req.GetHostId() != a.HostId {
|
||||
return nil, handlers.InvalidArgumentErrorf("Errors in provided fields.", map[string]string{
|
||||
"host_id": "The host id specified in the request does not match the one provided by the alias. Consider omiting the host id in the request.",
|
||||
})
|
||||
}
|
||||
req.HostId = a.HostId
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := validateAuthorizeSessionRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -1230,6 +1324,38 @@ func (s Service) getFromRepo(ctx context.Context, id string) (target.Target, []t
|
||||
return u, hs, cl, nil
|
||||
}
|
||||
|
||||
// maybeAlias returns true if the input string is a candidate for being an alias.
|
||||
func maybeAlias(s string) bool {
|
||||
return !strings.Contains(s, "_") &&
|
||||
len(s) > 0
|
||||
}
|
||||
|
||||
// resolveAlias returns the alias resource with the specified value.
|
||||
// If the alias does not have a destination id or there is no alias with a
|
||||
// matching value, an error is returned.
|
||||
func (s Service) resolveAlias(ctx context.Context, value string) (*talias.Alias, error) {
|
||||
const op = "targets.(Service).resolveAlias"
|
||||
if value == "" {
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "input string is empty")
|
||||
}
|
||||
|
||||
r, err := s.aliasRepoFn()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
a, err := r.LookupAliasByValue(ctx, value)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if a == nil {
|
||||
return nil, handlers.ApiErrorWithCodeAndMessage(codes.NotFound, "alias with value %q not found", value)
|
||||
}
|
||||
if a.DestinationId == "" {
|
||||
return nil, handlers.ApiErrorWithCodeAndMessage(codes.NotFound, "target not found for alias value %q", value)
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
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())}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user