Lookups Catalogs and Persisted data together (#1643)
* Fixed a number of TODOs: 1. Create a catalog/persisted data view. 2. Pass all sets to the plugin when deleting a catalog.
This commit is contained in:
11 files changed
+344
-261
No files matched your search
@@ -6,6 +6,7 @@ package plugin
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db/timestamp"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/host/plugin/store"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
@@ -96,3 +97,55 @@ func (s *HostCatalog) oplog(op oplog.OpType) oplog.Metadata {
|
||||
}
|
||||
return metadata
|
||||
}
|
||||
|
||||
type catalogAgg struct {
|
||||
PublicId string `gorm:"primary_key"`
|
||||
ScopeId string
|
||||
PluginId string
|
||||
Name string
|
||||
Description string
|
||||
CreateTime *timestamp.Timestamp
|
||||
UpdateTime *timestamp.Timestamp
|
||||
Version uint32
|
||||
Attributes []byte
|
||||
Secret []byte
|
||||
KeyId string
|
||||
PersistedCreateTime *timestamp.Timestamp
|
||||
PersistedUpdateTime *timestamp.Timestamp
|
||||
}
|
||||
|
||||
func (agg *catalogAgg) toCatalogAndPersisted() (*HostCatalog, *HostCatalogSecret) {
|
||||
if agg == nil {
|
||||
return nil, nil
|
||||
}
|
||||
c := allocHostCatalog()
|
||||
c.PublicId = agg.PublicId
|
||||
c.ScopeId = agg.ScopeId
|
||||
c.PluginId = agg.PluginId
|
||||
c.Name = agg.Name
|
||||
c.Description = agg.Description
|
||||
c.CreateTime = agg.CreateTime
|
||||
c.UpdateTime = agg.UpdateTime
|
||||
c.Version = agg.Version
|
||||
c.Attributes = agg.Attributes
|
||||
|
||||
var s *HostCatalogSecret
|
||||
if len(agg.Secret) > 0 {
|
||||
s = allocHostCatalogSecret()
|
||||
s.CatalogId = agg.PublicId
|
||||
s.Secret = agg.Secret
|
||||
s.KeyId = agg.KeyId
|
||||
s.CreateTime = agg.PersistedCreateTime
|
||||
s.UpdateTime = agg.PersistedUpdateTime
|
||||
}
|
||||
return c, s
|
||||
}
|
||||
|
||||
// TableName returns the table name for gorm
|
||||
func (agg *catalogAgg) TableName() string {
|
||||
return "host_plugin_catalog_with_secret"
|
||||
}
|
||||
|
||||
func (agg *catalogAgg) GetPublicId() string {
|
||||
return agg.PublicId
|
||||
}
|
||||
@@ -19,9 +19,6 @@ type Repository struct {
|
||||
kms *kms.Kms
|
||||
|
||||
// plugins is a map from plugin resource id to host plugin client.
|
||||
//
|
||||
// TODO: When we are using go-plugin change from using a
|
||||
// plgpb.HostPluginServiceServer to the client.
|
||||
plugins map[string]plgpb.HostPluginServiceClient
|
||||
// defaultLimit provides a default for limiting the number of results
|
||||
// returned from the repo
|
||||
|
||||
@@ -8,9 +8,11 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/host"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/observability/event"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
hostplugin "github.com/hashicorp/boundary/internal/plugin/host"
|
||||
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/hostcatalogs"
|
||||
pbset "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/hostsets"
|
||||
plgpb "github.com/hashicorp/boundary/sdk/pbs/plugin"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
@@ -55,14 +57,14 @@ func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, _ ...Opt
|
||||
}
|
||||
c.PublicId = id
|
||||
|
||||
plgClient, ok := r.plugins[c.GetPluginId()]
|
||||
if !ok || plgClient == nil {
|
||||
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("plugin %q not available", c.GetPluginId()))
|
||||
}
|
||||
plgHc, err := toPluginCatalog(ctx, c)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
plgClient, ok := r.plugins[c.GetPluginId()]
|
||||
if !ok || plgClient == nil {
|
||||
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("plugin %q not available", c.GetPluginId()))
|
||||
}
|
||||
plgResp, err := plgClient.OnCreateCatalog(ctx, &plgpb.OnCreateCatalogRequest{Catalog: plgHc})
|
||||
if err != nil {
|
||||
if status.Code(err) != codes.Unimplemented {
|
||||
@@ -150,11 +152,10 @@ func (r *Repository) LookupCatalog(ctx context.Context, id string, _ ...Option)
|
||||
if id == "" {
|
||||
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
c, err := r.getCatalog(ctx, id)
|
||||
c, _, err := r.getCatalog(ctx, id)
|
||||
if errors.IsNotFoundError(err) {
|
||||
return nil, nil, nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for: %s", id)))
|
||||
}
|
||||
@@ -203,33 +204,40 @@ func (r *Repository) DeleteCatalog(ctx context.Context, id string, _ ...Option)
|
||||
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
|
||||
c, err := r.getCatalog(ctx, id)
|
||||
c, p, err := r.getCatalog(ctx, id)
|
||||
if err != nil && !errors.IsNotFoundError(err) {
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if c == nil {
|
||||
return db.NoRowsAffected, nil
|
||||
}
|
||||
p, err := r.getPersistedDataForCatalog(ctx, c)
|
||||
plgHc, err := toPluginCatalog(ctx, c)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
sets, _, err := r.getSets(ctx, "", c.GetPublicId())
|
||||
var plgSets []*pbset.HostSet
|
||||
for _, s := range sets {
|
||||
ps, err := toPluginSet(ctx, s)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
plgSets = append(plgSets, ps)
|
||||
}
|
||||
|
||||
plgClient, ok := r.plugins[c.GetPluginId()]
|
||||
if !ok || plgClient == nil {
|
||||
return 0, errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("plugin %q not available", c.GetPluginId()))
|
||||
}
|
||||
plgHc, err := toPluginCatalog(ctx, c)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
// TODO: Include all the sets for this catalog in the delete catalog request. We don't need it for now
|
||||
// since our currently provided plugins only read data for set configuration, but in the future that might
|
||||
// change.
|
||||
_, err = plgClient.OnDeleteCatalog(ctx, &plgpb.OnDeleteCatalogRequest{Catalog: plgHc, Persisted: p})
|
||||
_, err = plgClient.OnDeleteCatalog(ctx, &plgpb.OnDeleteCatalogRequest{
|
||||
Catalog: plgHc,
|
||||
Sets: plgSets,
|
||||
Persisted: p,
|
||||
})
|
||||
if err != nil {
|
||||
// Even if the plugin returns an error, we ignore it and proceed with
|
||||
// deleting the catalog.
|
||||
event.WriteError(ctx, op, err, event.WithInfoMsg("plugin deleting catalog", "host plugin id", c.GetPluginId()))
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, c.ScopeId, kms.KeyPurposeOplog)
|
||||
@@ -271,50 +279,23 @@ func (r *Repository) DeleteCatalog(ctx context.Context, id string, _ ...Option)
|
||||
|
||||
// getCatalog retrieves the *HostCatalog with the provided id. If it is not found or there
|
||||
// is an problem getting it from the database an error is returned instead.
|
||||
func (r *Repository) getCatalog(ctx context.Context, id string) (*HostCatalog, error) {
|
||||
func (r *Repository) getCatalog(ctx context.Context, id string) (*HostCatalog, *plgpb.HostCatalogPersisted, error) {
|
||||
const op = "plugin.(Repository).getCatalog"
|
||||
c := allocHostCatalog()
|
||||
c.PublicId = id
|
||||
if err := r.reader.LookupByPublicId(ctx, c); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for: %s", id)))
|
||||
ca := &catalogAgg{}
|
||||
ca.PublicId = id
|
||||
if err := r.reader.LookupByPublicId(ctx, ca); err != nil {
|
||||
return nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for: %s", id)))
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// getPersistedDataForCatalog returns the persisted data for a catalog if
|
||||
// present. c must have a valid Public Id and Scope Id set.
|
||||
// TODO: consider merging the functions for getting catalog and persisted data into a view.
|
||||
func (r *Repository) getPersistedDataForCatalog(ctx context.Context, c *HostCatalog) (*plgpb.HostCatalogPersisted, error) {
|
||||
const op = "plugin.(Repository).getPersistedDataForCatalog"
|
||||
if c.PublicId == "" {
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "empty public id")
|
||||
}
|
||||
if c.ScopeId == "" {
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "empty scope id")
|
||||
}
|
||||
cSecret := allocHostCatalogSecret()
|
||||
if err := r.reader.LookupWhere(ctx, cSecret, "catalog_id=?", c.GetPublicId()); err != nil {
|
||||
if errors.IsNotFoundError(err) {
|
||||
return nil, nil
|
||||
c, s := ca.toCatalogAndPersisted()
|
||||
var p *plgpb.HostCatalogPersisted
|
||||
if s != nil {
|
||||
var err error
|
||||
p, err = r.toPluginPersistedData(ctx, c.GetScopeId(), s)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if cSecret == nil {
|
||||
return nil, nil
|
||||
}
|
||||
dbWrapper, err := r.kms.GetWrapper(ctx, c.ScopeId, kms.KeyPurposeDatabase)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get db wrapper"))
|
||||
}
|
||||
if err := cSecret.decrypt(ctx, dbWrapper); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
secrets := &structpb.Struct{}
|
||||
if err := proto.Unmarshal(cSecret.GetSecret(), secrets); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unmarshaling secret"))
|
||||
}
|
||||
return &plgpb.HostCatalogPersisted{Secrets: secrets}, nil
|
||||
return c, p, nil
|
||||
}
|
||||
|
||||
func (r *Repository) getPlugin(ctx context.Context, plgId string) (*hostplugin.Plugin, error) {
|
||||
@@ -353,3 +334,28 @@ func toPluginCatalog(ctx context.Context, in *HostCatalog) (*pb.HostCatalog, err
|
||||
}
|
||||
return hc, nil
|
||||
}
|
||||
|
||||
// toPluginCatalog converts a *HostCatalogSecret from storage to a
|
||||
// *plgpb.HostCatalogPersisted expected by a plugin. Scope Id must be set.
|
||||
func (r *Repository) toPluginPersistedData(ctx context.Context, scopeId string, cSecret *HostCatalogSecret) (*plgpb.HostCatalogPersisted, error) {
|
||||
const op = "plugin.(Repository).getPersistedDataForCatalog"
|
||||
if scopeId == "" {
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "empty scope id")
|
||||
}
|
||||
if cSecret == nil {
|
||||
return nil, nil
|
||||
}
|
||||
dbWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeDatabase)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get db wrapper"))
|
||||
}
|
||||
if err := cSecret.decrypt(ctx, dbWrapper); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
secrets := &structpb.Struct{}
|
||||
if err := proto.Unmarshal(cSecret.GetSecret(), secrets); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unmarshaling secret"))
|
||||
}
|
||||
return &plgpb.HostCatalogPersisted{Secrets: secrets}, nil
|
||||
}
|
||||
@@ -495,11 +495,11 @@ func TestRepository_DeleteCatalog(t *testing.T) {
|
||||
assert.NotNil(t, repo)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
name string
|
||||
id string
|
||||
pluginChecker func(*testing.T, *plgpb.OnDeleteCatalogRequest) error
|
||||
want int
|
||||
wantErr errors.Code
|
||||
wantErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "found",
|
||||
@@ -529,8 +529,8 @@ func TestRepository_DeleteCatalog(t *testing.T) {
|
||||
want: 0,
|
||||
},
|
||||
{
|
||||
name: "bad-public-id",
|
||||
id: "",
|
||||
name: "bad-public-id",
|
||||
id: "",
|
||||
pluginChecker: func(t *testing.T, req *plgpb.OnDeleteCatalogRequest) error {
|
||||
assert.Fail(t, "Should not call the plugin for a bad id")
|
||||
return nil
|
||||
@@ -621,4 +621,4 @@ func TestRepository_DeleteCatalogX(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -54,14 +54,10 @@ func (r *Repository) CreateSet(ctx context.Context, scopeId string, s *HostSet,
|
||||
}
|
||||
s = s.clone()
|
||||
|
||||
c, err := r.getCatalog(ctx, s.CatalogId)
|
||||
c, per, err := r.getCatalog(ctx, s.CatalogId)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg("looking up catalog"))
|
||||
}
|
||||
per, err := r.getPersistedDataForCatalog(ctx, c)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg("looking up persisted data"))
|
||||
}
|
||||
id, err := newHostSetId(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrap(ctx, err, op)
|
||||
@@ -187,7 +183,7 @@ func (r *Repository) LookupSet(ctx context.Context, publicId string, opt ...host
|
||||
|
||||
// FIXME: change to use the database
|
||||
if plg != nil && opts.WithSetMembers {
|
||||
cat, err := r.getCatalog(ctx, setToReturn.GetCatalogId())
|
||||
cat, persisted, err := r.getCatalog(ctx, setToReturn.GetCatalogId())
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
@@ -195,10 +191,6 @@ func (r *Repository) LookupSet(ctx context.Context, publicId string, opt ...host
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
persisted, err := r.getPersistedDataForCatalog(ctx, cat)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
plgSet, err := toPluginSet(ctx, setToReturn)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrap(ctx, err, op)
|
||||
@@ -270,17 +262,13 @@ func (r *Repository) DeleteSet(ctx context.Context, scopeId string, publicId str
|
||||
}
|
||||
s := sets[0]
|
||||
|
||||
c, err := r.getCatalog(ctx, s.GetCatalogId())
|
||||
c, p, err := r.getCatalog(ctx, s.GetCatalogId())
|
||||
if err != nil && errors.IsNotFoundError(err) {
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if c == nil {
|
||||
return db.NoRowsAffected, nil
|
||||
}
|
||||
p, err := r.getPersistedDataForCatalog(ctx, c)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
plgClient, ok := r.plugins[plg.GetPublicId()]
|
||||
if !ok || plgClient == nil {
|
||||
@@ -561,14 +549,15 @@ func (r *Repository) Endpoints(ctx context.Context, setIds []string) ([]*host.En
|
||||
for k := range catalogInfos {
|
||||
catIds = append(catIds, k)
|
||||
}
|
||||
var cats []*HostCatalog
|
||||
if err := r.reader.SearchWhere(ctx, &cats, "public_id in (?)", []interface{}{catIds}); err != nil {
|
||||
var catAggs []*catalogAgg
|
||||
if err := r.reader.SearchWhere(ctx, &catAggs, "public_id in (?)", []interface{}{catIds}); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("can't retrieve catalogs %v", catIds)))
|
||||
}
|
||||
if len(cats) == 0 {
|
||||
if len(catAggs) == 0 {
|
||||
return nil, errors.New(ctx, errors.NotSpecificIntegrity, op, "no catalogs returned for retrieved sets")
|
||||
}
|
||||
for _, c := range cats {
|
||||
for _, ca := range catAggs {
|
||||
c, s := ca.toCatalogAndPersisted()
|
||||
ci, ok := catalogInfos[c.GetPublicId()]
|
||||
if !ok {
|
||||
return nil, errors.New(ctx, errors.NotSpecificIntegrity, op, "catalog returned when no set requested it")
|
||||
@@ -579,10 +568,9 @@ func (r *Repository) Endpoints(ctx context.Context, setIds []string) ([]*host.En
|
||||
}
|
||||
ci.plgCat = plgCat
|
||||
|
||||
// TODO: Do these lookups from the DB in bulk instead of individually.
|
||||
per, err := r.getPersistedDataForCatalog(ctx, c)
|
||||
per, err := r.toPluginPersistedData(ctx, c.GetScopeId(), s)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("persisted catalog lookup failed"))
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
ci.persisted = per
|
||||
catalogInfos[c.GetPublicId()] = ci
|
||||
|
||||
@@ -677,14 +677,14 @@ func TestRepository_DeleteSet(t *testing.T) {
|
||||
newHostSetId, err := newHostSetId(ctx)
|
||||
require.NoError(t, err)
|
||||
tests := []struct {
|
||||
name string
|
||||
in string
|
||||
name string
|
||||
in string
|
||||
pluginChecker func(*plgpb.OnDeleteSetRequest) error
|
||||
want int
|
||||
wantIsErr errors.Code
|
||||
want int
|
||||
wantIsErr errors.Code
|
||||
}{
|
||||
{
|
||||
name: "With no public id",
|
||||
name: "With no public id",
|
||||
pluginChecker: func(req *plgpb.OnDeleteSetRequest) error {
|
||||
assert.Fail(t, "The plugin shouldn't be called")
|
||||
return nil
|
||||
|
||||
Reference in new issue
Block a user