Add preferred endpoints logic to API/handler/DB and some CLI bits (#1529)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2021-09-20 12:48:38 -04:00
1 parent f9c620a5d5
commit 01d4383c1b
46 files changed
+1737 -231

No files matched your search

+3 -3
View File
@@ -12,7 +12,7 @@ import (
"github.com/hashicorp/boundary/internal/cmd/config"
"github.com/hashicorp/boundary/internal/credential/vault"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/host/plugin"
hostplugin "github.com/hashicorp/boundary/internal/host/plugin"
"github.com/hashicorp/boundary/internal/host/static"
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/kms"
@@ -135,8 +135,8 @@ func New(ctx context.Context, conf *Config) (*Controller, error) {
c.StaticHostRepoFn = func() (*static.Repository, error) {
return static.NewRepository(dbase, dbase, c.kms)
}
c.PluginHostRepoFn = func() (*plugin.Repository, error) {
return plugin.NewRepository(dbase, dbase, c.kms)
c.PluginHostRepoFn = func() (*hostplugin.Repository, error) {
return hostplugin.NewRepository(dbase, dbase, c.kms)
}
c.HostPluginRepoFn = func() (*host.Repository, error) {
return host.NewRepository(dbase, dbase, c.kms)
@@ -67,7 +67,7 @@ type Service struct {
staticRepoFn common.StaticRepoFactory
pluginHostRepoFn common.PluginHostRepoFactory
pluginRepoFn common.HostPluginRepoFactory
pluginRepoFn common.HostPluginRepoFactory
iamRepoFn common.IamRepoFactory
}
@@ -272,7 +272,7 @@ func TestList(t *testing.T) {
var testPluginCatalogs []*pb.HostCatalog
name := "test"
plg := host.TestPlugin(t, conn, name, name)
for i := 0 ; i < 3; i++ {
for i := 0; i < 3; i++ {
hc := plugin.TestCatalog(t, conn, pWithCatalogs.GetPublicId(), plg.GetPublicId())
cat := &pb.HostCatalog{
Id: hc.GetPublicId(),
@@ -306,7 +306,7 @@ func TestList(t *testing.T) {
name = "different"
plg = host.TestPlugin(t, conn, name, name)
for i := 0 ; i < 3; i++ {
for i := 0; i < 3; i++ {
hc := plugin.TestCatalog(t, conn, pWithOtherCatalogs.GetPublicId(), plg.GetPublicId())
wantOtherCatalogs = append(wantOtherCatalogs, &pb.HostCatalog{
Id: hc.GetPublicId(),
@@ -13,6 +13,7 @@ import (
"github.com/hashicorp/boundary/internal/host/plugin"
"github.com/hashicorp/boundary/internal/host/static"
"github.com/hashicorp/boundary/internal/host/static/store"
"github.com/hashicorp/boundary/internal/libs/endpoint"
"github.com/hashicorp/boundary/internal/perms"
"github.com/hashicorp/boundary/internal/requests"
"github.com/hashicorp/boundary/internal/servers/controller/auth"
@@ -80,6 +81,10 @@ func NewService(staticRepoFn common.StaticRepoFactory, pluginRepoFn common.Plugi
}
func (s Service) ListHostSets(ctx context.Context, req *pbs.ListHostSetsRequest) (*pbs.ListHostSetsResponse, error) {
return s.ListHostSetsWithOptions(ctx, req)
}
func (s Service) ListHostSetsWithOptions(ctx context.Context, req *pbs.ListHostSetsRequest, opt ...host.Option) (*pbs.ListHostSetsResponse, error) {
if err := validateListRequest(req); err != nil {
return nil, err
}
@@ -87,7 +92,7 @@ func (s Service) ListHostSets(ctx context.Context, req *pbs.ListHostSetsRequest)
if authResults.Error != nil {
return nil, authResults.Error
}
hl, err := s.listFromRepo(ctx, req.GetHostCatalogId())
hl, err := s.listFromRepo(ctx, req.GetHostCatalogId(), opt...)
if err != nil {
return nil, err
}
@@ -177,7 +182,7 @@ func (s Service) GetHostSet(ctx context.Context, req *pbs.GetHostSetRequest) (*p
func (s Service) CreateHostSet(ctx context.Context, req *pbs.CreateHostSetRequest) (*pbs.CreateHostSetResponse, error) {
const op = "host_sets.(Service).CreateHostSet"
if err := validateCreateRequest(req); err != nil {
if err := validateCreateRequest(ctx, req); err != nil {
return nil, err
}
_, authResults := s.parentAndAuthResult(ctx, req.GetItem().GetHostCatalogId(), action.Create)
@@ -218,7 +223,7 @@ func (s Service) CreateHostSet(ctx context.Context, req *pbs.CreateHostSetReques
func (s Service) UpdateHostSet(ctx context.Context, req *pbs.UpdateHostSetRequest) (*pbs.UpdateHostSetResponse, error) {
const op = "host_sets.(Service).UpdateHostSet"
if err := validateUpdateRequest(req); err != nil {
if err := validateUpdateRequest(ctx, req); err != nil {
return nil, err
}
cat, authResults := s.parentAndAuthResult(ctx, req.GetId(), action.Update)
@@ -444,6 +449,9 @@ func (s Service) createInRepo(ctx context.Context, scopeId, catalogId string, it
hSet = out
case plugin.Subtype:
h, err := toStoragePluginSet(ctx, catalogId, item)
if err != nil {
return nil, err
}
repo, err := s.pluginRepoFn()
if err != nil {
return nil, err
@@ -505,7 +513,7 @@ func (s Service) deleteFromRepo(ctx context.Context, scopeId, id string) (bool,
return rows > 0, nil
}
func (s Service) listFromRepo(ctx context.Context, catalogId string) ([]host.Set, error) {
func (s Service) listFromRepo(ctx context.Context, catalogId string, opt ...host.Option) ([]host.Set, error) {
const op = "host_sets.(Service).listFromRepo"
var sets []host.Set
switch host.SubtypeFromId(catalogId) {
@@ -526,7 +534,7 @@ func (s Service) listFromRepo(ctx context.Context, catalogId string) ([]host.Set
if err != nil {
return nil, err
}
sl, err := repo.ListSets(ctx, catalogId)
sl, err := repo.ListSets(ctx, catalogId, opt...)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
@@ -742,9 +750,12 @@ func toProto(ctx context.Context, in host.Set, hosts []host.Host, opt ...handler
}
}
if outputFields.Has(globals.AttributesField) {
switch h := in.(type) {
case *plugin.HostSet:
switch h := in.(type) {
case *plugin.HostSet:
if outputFields.Has(globals.PreferredEndpointsField) {
out.PreferredEndpoints = h.GetPreferredEndpoints()
}
if outputFields.Has(globals.AttributesField) {
attrs := map[string]interface{}{}
err := json.Unmarshal(h.Attributes, &attrs)
if err != nil {
@@ -790,6 +801,9 @@ func toStoragePluginSet(ctx context.Context, catalogId string, item *pb.HostSet)
if item.GetAttributes() != nil {
opts = append(opts, plugin.WithAttributes(item.GetAttributes().AsMap()))
}
if item.GetPreferredEndpoints() != nil {
opts = append(opts, plugin.WithPreferredEndpoints(item.GetPreferredEndpoints()))
}
hs, err := plugin.NewHostSet(ctx, catalogId, opts...)
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("Unable to build host set for creation"))
@@ -814,7 +828,7 @@ func validateGetRequest(req *pbs.GetHostSetRequest) error {
return handlers.ValidateGetRequest(handlers.NoopValidatorFn, req, static.HostSetPrefix, pluginPrefix)
}
func validateCreateRequest(req *pbs.CreateHostSetRequest) error {
func validateCreateRequest(ctx context.Context, req *pbs.CreateHostSetRequest) error {
return handlers.ValidateCreateRequest(req.GetItem(), func() map[string]string {
badFields := map[string]string{}
pluginPrefix := plugin.HostCatalogPrefix
@@ -826,6 +840,12 @@ func validateCreateRequest(req *pbs.CreateHostSetRequest) error {
if !handlers.ValidId(handlers.Id(req.GetItem().GetHostCatalogId()), static.HostCatalogPrefix, pluginPrefix) {
badFields["host_catalog_id"] = "The field is incorrectly formatted."
}
if len(req.GetItem().GetPreferredEndpoints()) > 0 {
_, err := endpoint.NewPreferencer(ctx, endpoint.WithPreferenceOrder(req.GetItem().GetPreferredEndpoints()))
if err != nil {
badFields[globals.PreferredEndpointsField] = fmt.Errorf("Error parsing preferred endpoints: %w.", err).Error()
}
}
switch host.SubtypeFromId(req.GetItem().GetHostCatalogId()) {
case static.Subtype:
if req.GetItem().GetType() != "" && req.GetItem().GetType() != static.Subtype.String() {
@@ -850,17 +870,23 @@ func validateCreateRequest(req *pbs.CreateHostSetRequest) error {
})
}
func validateUpdateRequest(req *pbs.UpdateHostSetRequest) error {
func validateUpdateRequest(ctx context.Context, req *pbs.UpdateHostSetRequest) error {
return handlers.ValidateUpdateRequest(req, req.GetItem(), func() map[string]string {
badFields := map[string]string{}
if len(req.GetItem().GetPreferredEndpoints()) > 0 {
_, err := endpoint.NewPreferencer(ctx, endpoint.WithPreferenceOrder(req.GetItem().GetPreferredEndpoints()))
if err != nil {
badFields[globals.PreferredEndpointsField] = fmt.Errorf("Error parsing preferred endpoints: %w.", err).Error()
}
}
switch host.SubtypeFromId(req.GetId()) {
case static.Subtype:
if req.GetItem().GetType() != "" && req.GetItem().GetType() != static.Subtype.String() {
badFields["type"] = "Cannot modify the resource type."
badFields[globals.TypeField] = "Cannot modify the resource type."
}
}
return badFields
}, static.HostSetPrefix)
}, static.HostSetPrefix, plugin.HostSetPrefix)
}
func validateDeleteRequest(req *pbs.DeleteHostSetRequest) error {
@@ -11,11 +11,12 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/boundary/internal/db"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/host"
"github.com/hashicorp/boundary/internal/host/plugin"
"github.com/hashicorp/boundary/internal/host/static"
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/plugin/host"
hostplugin "github.com/hashicorp/boundary/internal/plugin/host"
"github.com/hashicorp/boundary/internal/servers/controller/auth"
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
"github.com/hashicorp/boundary/internal/servers/controller/handlers/host_sets"
@@ -150,20 +151,22 @@ func TestGet_Plugin(t *testing.T) {
}
name := "test"
plg := host.TestPlugin(t, conn, name, name)
prefEndpoints := []string{"cidr:1.2.3.4", "cidr:2.3.4.5/24"}
plg := hostplugin.TestPlugin(t, conn, name, name)
hc := plugin.TestCatalog(t, conn, proj.GetPublicId(), plg.GetPublicId())
hs := plugin.TestSet(t, conn, hc.GetPublicId())
hs := plugin.TestSet(t, conn, kms, hc, plugin.WithPreferredEndpoints(prefEndpoints))
toMerge := &pbs.GetHostSetRequest{}
pHost := &pb.HostSet{
HostCatalogId: hc.GetPublicId(),
Id: hs.GetPublicId(),
CreatedTime: hs.CreateTime.GetTimestamp(),
UpdatedTime: hs.UpdateTime.GetTimestamp(),
Type: name,
Scope: &scopes.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String(), ParentScopeId: org.GetPublicId()},
AuthorizedActions: testAuthorizedActions,
HostCatalogId: hc.GetPublicId(),
Id: hs.GetPublicId(),
CreatedTime: hs.CreateTime.GetTimestamp(),
UpdatedTime: hs.UpdateTime.GetTimestamp(),
Type: name,
Scope: &scopes.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String(), ParentScopeId: org.GetPublicId()},
PreferredEndpoints: prefEndpoints,
AuthorizedActions: testAuthorizedActions,
}
cases := []struct {
@@ -179,7 +182,7 @@ func TestGet_Plugin(t *testing.T) {
},
{
name: "Get a non existing Host Set",
req: &pbs.GetHostSetRequest{Id: static.HostSetPrefix + "_DoesntExis"},
req: &pbs.GetHostSetRequest{Id: plugin.HostSetPrefix + "_DoesntExis"},
res: nil,
err: handlers.ApiErrorWithCode(codes.NotFound),
},
@@ -191,7 +194,7 @@ func TestGet_Plugin(t *testing.T) {
},
{
name: "space in id",
req: &pbs.GetHostSetRequest{Id: static.HostPrefix + "_1 23456789"},
req: &pbs.GetHostSetRequest{Id: plugin.HostSetPrefix + "_1 23456789"},
res: nil,
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
},
@@ -339,22 +342,24 @@ func TestList_Plugin(t *testing.T) {
return plugin.NewRepository(rw, rw, kms)
}
name := "test"
plg := host.TestPlugin(t, conn, name, name)
plg := hostplugin.TestPlugin(t, conn, name, name)
hc := plugin.TestCatalog(t, conn, proj.GetPublicId(), plg.GetPublicId())
hcNoHosts := plugin.TestCatalog(t, conn, proj.GetPublicId(), plg.GetPublicId())
preferredEndpoints := []string{"cidr:1.2.3.4", "dns:*.foobar.com"}
var wantHs []*pb.HostSet
for i := 0; i < 10; i++ {
h := plugin.TestSet(t, conn, hc.GetPublicId())
h := plugin.TestSet(t, conn, kms, hc, plugin.WithPreferredEndpoints(preferredEndpoints))
wantHs = append(wantHs, &pb.HostSet{
Id: h.GetPublicId(),
HostCatalogId: h.GetCatalogId(),
Scope: &scopes.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String(), ParentScopeId: org.GetPublicId()},
CreatedTime: h.GetCreateTime().GetTimestamp(),
UpdatedTime: h.GetUpdateTime().GetTimestamp(),
Version: h.GetVersion(),
Type: name,
AuthorizedActions: testAuthorizedActions,
Id: h.GetPublicId(),
HostCatalogId: h.GetCatalogId(),
Scope: &scopes.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String(), ParentScopeId: org.GetPublicId()},
CreatedTime: h.GetCreateTime().GetTimestamp(),
UpdatedTime: h.GetUpdateTime().GetTimestamp(),
Version: h.GetVersion(),
Type: name,
AuthorizedActions: testAuthorizedActions,
PreferredEndpoints: preferredEndpoints,
})
}
@@ -397,7 +402,7 @@ func TestList_Plugin(t *testing.T) {
require.NoError(err, "Couldn't create new host set service.")
// Test with non-anon user
got, gErr := s.ListHostSets(auth.DisabledAuthTestContext(iamRepoFn, proj.GetPublicId()), tc.req)
got, gErr := s.ListHostSetsWithOptions(auth.DisabledAuthTestContext(iamRepoFn, proj.GetPublicId()), tc.req, host.WithOrderByCreateTime(false))
if tc.err != nil {
require.Error(gErr)
assert.True(errors.Is(gErr, tc.err), "ListHostSets(%q) got error %v, wanted %v", tc.req, gErr, tc.err)
@@ -699,7 +704,7 @@ func TestCreate_Plugin(t *testing.T) {
return plugin.NewRepository(rw, rw, kms)
}
name := "test"
plg := host.TestPlugin(t, conn, name, name)
plg := hostplugin.TestPlugin(t, conn, name, name)
hc := plugin.TestCatalog(t, conn, proj.GetPublicId(), plg.GetPublicId())
testAttrs, err := structpb.NewStruct(map[string]interface{}{
@@ -724,6 +729,8 @@ func TestCreate_Plugin(t *testing.T) {
})
require.NoError(t, err)
prefEndpoints := []string{"cidr:1.2.3.4", "cidr:2.3.4.5/24"}
defaultHcCreated := hc.GetCreateTime().GetTimestamp().AsTime()
cases := []struct {
@@ -774,6 +781,39 @@ func TestCreate_Plugin(t *testing.T) {
},
},
},
{
name: "With Preferred Endpoints",
req: &pbs.CreateHostSetRequest{Item: &pb.HostSet{
HostCatalogId: hc.GetPublicId(),
Name: &wrappers.StringValue{Value: "name"},
Description: &wrappers.StringValue{Value: "desc"},
Type: name,
PreferredEndpoints: prefEndpoints,
}},
res: &pbs.CreateHostSetResponse{
Uri: fmt.Sprintf("host-sets/%s_%s_", plugin.HostSetPrefix, name),
Item: &pb.HostSet{
HostCatalogId: hc.GetPublicId(),
Scope: &scopes.ScopeInfo{Id: proj.GetPublicId(), Type: scope.Project.String(), ParentScopeId: org.GetPublicId()},
Name: &wrappers.StringValue{Value: "name"},
Description: &wrappers.StringValue{Value: "desc"},
Type: name,
PreferredEndpoints: prefEndpoints,
AuthorizedActions: testAuthorizedActions,
},
},
},
{
name: "Bad Preferred Endpoints",
req: &pbs.CreateHostSetRequest{Item: &pb.HostSet{
HostCatalogId: hc.GetPublicId(),
Name: &wrappers.StringValue{Value: "name"},
Description: &wrappers.StringValue{Value: "desc"},
Type: name,
PreferredEndpoints: append(prefEndpoints, "foobar:1.2.3.4"),
}},
err: handlers.ApiErrorWithCode(codes.InvalidArgument),
},
{
name: "Create with mismatched type/name",
req: &pbs.CreateHostSetRequest{Item: &pb.HostSet{
@@ -1731,7 +1731,7 @@ func validateSetSetsRequest(req *pbs.SetTargetHostSetsRequest) error {
badFields[globals.VersionField] = "Required field."
}
for _, id := range req.GetHostSetIds() {
if !handlers.ValidId(handlers.Id(id), static.HostSetPrefix) &&
if !handlers.ValidId(handlers.Id(id), static.HostSetPrefix) &&
!strings.HasPrefix(id, fmt.Sprintf("%s_", plugin.HostSetPrefix)) {
badFields[globals.HostSetIdsField] = fmt.Sprintf("Incorrectly formatted host set identifier %q.", id)
break
@@ -1755,7 +1755,7 @@ func validateRemoveSetsRequest(req *pbs.RemoveTargetHostSetsRequest) error {
badFields[globals.HostSetIdsField] = "Must be non-empty."
}
for _, id := range req.GetHostSetIds() {
if !handlers.ValidId(handlers.Id(id), static.HostSetPrefix) &&
if !handlers.ValidId(handlers.Id(id), static.HostSetPrefix) &&
!strings.HasPrefix(id, fmt.Sprintf("%s_", plugin.HostSetPrefix)) {
badFields[globals.HostSetIdsField] = fmt.Sprintf("Incorrectly formatted host set identifier %q.", id)
break
@@ -1779,7 +1779,7 @@ func validateAddHostSourcesRequest(req *pbs.AddTargetHostSourcesRequest) error {
badFields[globals.HostSourceIdsField] = "Must be non-empty."
}
for _, id := range req.GetHostSourceIds() {
if !handlers.ValidId(handlers.Id(id), static.HostSetPrefix) &&
if !handlers.ValidId(handlers.Id(id), static.HostSetPrefix) &&
!strings.HasPrefix(id, fmt.Sprintf("%s_", plugin.HostSetPrefix)) {
badFields[globals.HostSourceIdsField] = fmt.Sprintf("Incorrectly formatted host source identifier %q.", id)
break
@@ -1800,7 +1800,7 @@ func validateSetHostSourcesRequest(req *pbs.SetTargetHostSourcesRequest) error {
badFields[globals.VersionField] = "Required field."
}
for _, id := range req.GetHostSourceIds() {
if !handlers.ValidId(handlers.Id(id), static.HostSetPrefix) &&
if !handlers.ValidId(handlers.Id(id), static.HostSetPrefix) &&
!strings.HasPrefix(id, fmt.Sprintf("%s_", plugin.HostSetPrefix)) {
badFields[globals.HostSourceIdsField] = fmt.Sprintf("Incorrectly formatted host source identifier %q.", id)
break
@@ -1824,7 +1824,7 @@ func validateRemoveHostSourcesRequest(req *pbs.RemoveTargetHostSourcesRequest) e
badFields[globals.HostSourceIdsField] = "Must be non-empty."
}
for _, id := range req.GetHostSourceIds() {
if !handlers.ValidId(handlers.Id(id), static.HostSetPrefix) &&
if !handlers.ValidId(handlers.Id(id), static.HostSetPrefix) &&
!strings.HasPrefix(id, fmt.Sprintf("%s_", plugin.HostSetPrefix)) {
badFields[globals.HostSourceIdsField] = fmt.Sprintf("Incorrectly formatted host source identifier %q.", id)
break
@@ -983,7 +983,7 @@ func TestAddTargetHostSets(t *testing.T) {
plg := host.TestPlugin(t, conn, "test", "test")
pluginHc := plugin.TestCatalog(t, conn, proj.GetPublicId(), plg.GetPublicId())
pluginHs := plugin.TestSet(t, conn, pluginHc.GetPublicId())
pluginHs := plugin.TestSet(t, conn, kms, pluginHc)
addCases := []struct {
name string
@@ -1115,7 +1115,7 @@ func TestSetTargetHostSets(t *testing.T) {
plg := host.TestPlugin(t, conn, "test", "test")
pluginHc := plugin.TestCatalog(t, conn, proj.GetPublicId(), plg.GetPublicId())
pluginHs := plugin.TestSet(t, conn, pluginHc.GetPublicId())
pluginHs := plugin.TestSet(t, conn, kms, pluginHc)
setCases := []struct {
name string
@@ -1241,7 +1241,7 @@ func TestRemoveTargetHostSets(t *testing.T) {
plg := host.TestPlugin(t, conn, "test", "test")
pluginHc := plugin.TestCatalog(t, conn, proj.GetPublicId(), plg.GetPublicId())
pluginHs := plugin.TestSet(t, conn, pluginHc.GetPublicId())
pluginHs := plugin.TestSet(t, conn, kms, pluginHc)
removeCases := []struct {
name string
@@ -1379,7 +1379,7 @@ func TestAddTargetHostSources(t *testing.T) {
plg := host.TestPlugin(t, conn, "test", "test")
pluginHc := plugin.TestCatalog(t, conn, proj.GetPublicId(), plg.GetPublicId())
pluginHs := plugin.TestSet(t, conn, pluginHc.GetPublicId())
pluginHs := plugin.TestSet(t, conn, kms, pluginHc)
addCases := []struct {
name string
@@ -1511,7 +1511,7 @@ func TestSetTargetHostSources(t *testing.T) {
plg := host.TestPlugin(t, conn, "test", "test")
pluginHc := plugin.TestCatalog(t, conn, proj.GetPublicId(), plg.GetPublicId())
pluginHs := plugin.TestSet(t, conn, pluginHc.GetPublicId())
pluginHs := plugin.TestSet(t, conn, kms, pluginHc)
setCases := []struct {
name string
@@ -1631,7 +1631,7 @@ func TestRemoveTargetHostSources(t *testing.T) {
plg := host.TestPlugin(t, conn, "test", "test")
pluginHc := plugin.TestCatalog(t, conn, proj.GetPublicId(), plg.GetPublicId())
pluginHs := plugin.TestSet(t, conn, pluginHc.GetPublicId())
pluginHs := plugin.TestSet(t, conn, kms, pluginHc)
removeCases := []struct {
name string