Placeholder Plugin Providers for Repos and added testPlugin struct (#1532)

* Create minimalistic plugin map and test plugin.

* Pass secrets and attributes to the plugin when creating catalogs or sets.  Add tests for CreateSet and CreateCatalog repo methods.

* make fmt and extract out getPrivateCatalog and getCatalog helper methods.

* Exporting TestPluginServer so it can be used in the handlers.

* Keep separate persisted data from host catalogs lookups which facilitate passing it onto the plugins.

* Add TODO to merge get Catalog and get Persisted data.

* Adding tests for unimplemented being returned by the plugin.

* Use GetFields() instead of AsMap() to confirm attributes should be populated. Make clone consistent between catalog and sets.

Co-authored-by: Jeff Mitchell <jeffrey.mitchell@gmail.com>
This commit is contained in:
ToddandJeff Mitchell authored and GitHub committed 2021-09-21 09:18:49 -07:00
1 parent ea939a4445
commit bbfef2802f
30 files changed
+669 -227

No files matched your search

+102 -12
View File
@@ -10,6 +10,12 @@ import (
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/oplog"
hostplg "github.com/hashicorp/boundary/internal/plugin/host"
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/hostcatalogs"
plgpb "github.com/hashicorp/boundary/sdk/pbs/plugin"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/structpb"
)
// CreateCatalog inserts c into the repository and returns a new
@@ -44,7 +50,6 @@ func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, _ ...Opt
}
c = c.clone()
// TODO: Capture this in a plugin manager and call the plugin's OnCreateCatalog function
plg := hostplg.NewPlugin("", "")
plg.PublicId = c.PluginId
if err := r.reader.LookupByPublicId(ctx, plg); err != nil {
@@ -60,27 +65,41 @@ func (r *Repository) CreateCatalog(ctx context.Context, c *HostCatalog, _ ...Opt
}
c.PublicId = id
oplogWrapper, err := r.kms.GetWrapper(ctx, c.ScopeId, kms.KeyPurposeOplog)
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
plgClient, ok := r.plugins[plg.GetPublicId()]
if !ok || plgClient == nil {
return nil, errors.New(ctx, errors.Internal, op, fmt.Sprintf("expected plugin %q not available", plg.GetPluginName()))
}
dbWrapper, err := r.kms.GetWrapper(ctx, c.ScopeId, kms.KeyPurposeDatabase)
plgHc, err := toPluginCatalog(ctx, c)
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get db wrapper"))
return nil, errors.Wrap(ctx, err, op)
}
plgResp, err := plgClient.OnCreateCatalog(ctx, &plgpb.OnCreateCatalogRequest{Catalog: plgHc})
if err != nil {
if status.Code(err) != codes.Unimplemented {
return nil, errors.Wrap(ctx, err, op)
}
}
var hcSecret *HostCatalogSecret
if c.secrets != nil {
// TODO: Create the secret using the returned value from the call to the plugin.
hcSecret, err = newHostCatalogSecret(ctx, id, c.secrets)
if plgResp != nil && plgResp.GetPersisted().GetData() != nil {
hcSecret, err = newHostCatalogSecret(ctx, id, plgResp.GetPersisted().GetData())
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
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 := hcSecret.encrypt(ctx, dbWrapper); err != nil {
return nil, errors.Wrap(ctx, err, op)
}
}
oplogWrapper, err := r.kms.GetWrapper(ctx, c.ScopeId, kms.KeyPurposeOplog)
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
}
var newHostCatalog *HostCatalog
_, err = r.writer.DoTx(
ctx,
@@ -137,9 +156,8 @@ func (r *Repository) LookupCatalog(ctx context.Context, id string, _ ...Option)
if id == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "no public id")
}
c := allocHostCatalog()
c.PublicId = id
if err := r.reader.LookupByPublicId(ctx, c); err != nil {
c, err := r.getCatalog(ctx, id)
if err != nil {
if errors.IsNotFoundError(err) {
return nil, nil
}
@@ -169,3 +187,75 @@ func (r *Repository) ListCatalogs(ctx context.Context, scopeIds []string, opt ..
}
return hostCatalogs, nil
}
// 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) {
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)))
}
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
}
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)
}
data := &structpb.Struct{}
if err := proto.Unmarshal(cSecret.GetSecret(), data); err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unmarshaling secret"))
}
return &plgpb.HostCatalogPersisted{Data: data}, nil
}
// toPluginCatalog returns a host catalog, with it's secret if available, in the format expected
// by the host plugin system.
func toPluginCatalog(ctx context.Context, in *HostCatalog) (*pb.HostCatalog, error) {
const op = "plugin.toPluginCatalog"
if in == nil {
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil storage plugin")
}
hc := &pb.HostCatalog{
Id: in.GetPublicId(),
ScopeId: in.GetScopeId(),
}
if in.GetAttributes() != nil {
attrs := &structpb.Struct{}
if err := proto.Unmarshal(in.GetAttributes(), attrs); err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to unmarshal attributes"))
}
hc.Attributes = attrs
}
if in.secrets != nil {
hc.Secrets = in.secrets
}
return hc, nil
}