* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUS-1.1 * Rerun make gen This will pick up the last of the license changes * Revert "Rerun make gen" This reverts commit dbe0968bffd92745d8663c6940debae098395a6b. * Remove copywrite from generation script The current version of copywrite is not sophisticated enough to only apply the copywrite changes to certain subdirectories, so to avoid accidentally relicensing our api and sdk modules, we will stop running the copywrite script until such time that it can do the right thing. * License protobuf API as MPL The protobuf API files are used to generate the SDK, since we want the SDK to be MPL, the protobuf files must also be MPL. This extends to all protobuf files whose sources end up in the SDK directory, including: - internal/proto/controller/api - internal/proto/controller/custom_options - internal/proto/plugin It also includes the generated service directory in internal/gen/controller/api/services, since they are generated from the protobuf sources used for the SDK. * Change website license file name This makes the naming scheme consistent with the rest of the repository. * Add note on licensing to README * Set copywrite license to BUSL * Revert "Add note on licensing to README" This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe. --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
255 lines
7.8 KiB
Go
255 lines
7.8 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package plugin
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hashicorp/boundary/internal/db/timestamp"
|
|
"github.com/hashicorp/boundary/internal/errors"
|
|
"github.com/hashicorp/boundary/internal/libs/crypto"
|
|
"github.com/hashicorp/boundary/internal/oplog"
|
|
"github.com/hashicorp/boundary/internal/storage/plugin/store"
|
|
"github.com/hashicorp/boundary/internal/util"
|
|
"github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/storagebuckets"
|
|
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
|
|
"github.com/hashicorp/go-kms-wrapping/v2/extras/structwrapping"
|
|
"google.golang.org/protobuf/proto"
|
|
"google.golang.org/protobuf/types/known/structpb"
|
|
)
|
|
|
|
// StorageBucket represents a bucket within an external object store. It contains secret
|
|
// data needed to create and read objects within the external object store.
|
|
type StorageBucket struct {
|
|
*store.StorageBucket
|
|
tableName string `gorm:"-"`
|
|
|
|
Secrets *structpb.Struct `gorm:"-"`
|
|
}
|
|
|
|
func allocStorageBucket() *StorageBucket {
|
|
return &StorageBucket{
|
|
StorageBucket: &store.StorageBucket{},
|
|
}
|
|
}
|
|
|
|
// clone provides a deep copy of the storage bucket with the exception of the
|
|
// secret. The secret shallow copied.
|
|
func (s *StorageBucket) clone() *StorageBucket {
|
|
cp := proto.Clone(s.StorageBucket)
|
|
newSecret := proto.Clone(s.Secrets)
|
|
|
|
sb := &StorageBucket{
|
|
StorageBucket: cp.(*store.StorageBucket),
|
|
Secrets: newSecret.(*structpb.Struct),
|
|
}
|
|
// proto.Clone will convert slices with length and capacity of 0 to nil.
|
|
// Fix this since gorm treats empty slices differently than nil.
|
|
if s.Attributes != nil && len(s.Attributes) == 0 && sb.Attributes == nil {
|
|
sb.Attributes = []byte{}
|
|
}
|
|
return sb
|
|
}
|
|
|
|
// TableName returns the table name for the storage bucket.
|
|
func (s *StorageBucket) TableName() string {
|
|
if s.tableName != "" {
|
|
return s.tableName
|
|
}
|
|
return "storage_plugin_storage_bucket"
|
|
}
|
|
|
|
// SetTableName sets the table name.
|
|
func (s *StorageBucket) SetTableName(n string) {
|
|
s.tableName = n
|
|
}
|
|
|
|
func (s *StorageBucket) oplog(op oplog.OpType) oplog.Metadata {
|
|
metadata := oplog.Metadata{
|
|
"resource-public-id": []string{s.PublicId},
|
|
"resource-type": []string{"plugin-storage-bucket"},
|
|
"op-type": []string{op.String()},
|
|
}
|
|
if s.ScopeId != "" {
|
|
metadata["scope-id"] = []string{s.ScopeId}
|
|
}
|
|
return metadata
|
|
}
|
|
|
|
type StorageBucketSecret struct {
|
|
*store.StorageBucketSecret
|
|
tableName string `gorm:"-"`
|
|
}
|
|
|
|
func allocStorageBucketSecret() *StorageBucketSecret {
|
|
return &StorageBucketSecret{
|
|
StorageBucketSecret: &store.StorageBucketSecret{},
|
|
}
|
|
}
|
|
|
|
// newStorageBucketSecret creates an in memory storage bucket secret from a json
|
|
// formatted generic struct.
|
|
func newStorageBucketSecret(ctx context.Context, storageBucketId string, secret *structpb.Struct) (*StorageBucketSecret, error) {
|
|
const op = "plugin.newStorageBucketSecret"
|
|
s := &StorageBucketSecret{
|
|
StorageBucketSecret: &store.StorageBucketSecret{
|
|
StorageBucketId: storageBucketId,
|
|
},
|
|
}
|
|
|
|
if secret != nil {
|
|
sec, err := proto.Marshal(secret)
|
|
if err != nil {
|
|
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.InvalidParameter))
|
|
}
|
|
s.Secrets = sec
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
func (sbs *StorageBucketSecret) clone() *StorageBucketSecret {
|
|
cp := proto.Clone(sbs.StorageBucketSecret)
|
|
return &StorageBucketSecret{
|
|
StorageBucketSecret: cp.(*store.StorageBucketSecret),
|
|
}
|
|
}
|
|
|
|
// TableName returns the table name for the storage bucket secrets.
|
|
func (sbs *StorageBucketSecret) TableName() string {
|
|
if sbs.tableName != "" {
|
|
return sbs.tableName
|
|
}
|
|
return "storage_plugin_storage_bucket_secret"
|
|
}
|
|
|
|
// SetTableName sets the table name.
|
|
func (sbs *StorageBucketSecret) SetTableName(n string) {
|
|
sbs.tableName = n
|
|
}
|
|
|
|
// hmacField simply hmac's a field in a consistent manner for this pkg
|
|
func hmacField(ctx context.Context, cipher wrapping.Wrapper, field []byte, publicId string) ([]byte, error) {
|
|
const op = "plugin.hmacField"
|
|
hm, err := crypto.HmacSha256(ctx, field, cipher, []byte(publicId), nil)
|
|
if err != nil {
|
|
return nil, errors.Wrap(ctx, err, op)
|
|
}
|
|
return []byte(hm), nil
|
|
}
|
|
|
|
// only hmac's the secret's value. does not modify the underlying secret
|
|
// returns nil on failure
|
|
func (sbs *StorageBucketSecret) hmacSecrets(ctx context.Context, cipher wrapping.Wrapper) ([]byte, error) {
|
|
const op = "plugin.(StorageBucketSecret).hmacSecrets"
|
|
if cipher == nil {
|
|
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing cipher")
|
|
}
|
|
var err error
|
|
if _, err = cipher.KeyId(ctx); err != nil {
|
|
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("failed to read cipher key id"))
|
|
}
|
|
var hmac []byte
|
|
if hmac, err = hmacField(ctx, cipher, sbs.Secrets, sbs.StorageBucketId); err != nil {
|
|
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("failed to hmac secrets"))
|
|
}
|
|
|
|
return hmac, nil
|
|
}
|
|
|
|
// encrypt the bind credential before writing it to the database
|
|
func (sbs *StorageBucketSecret) encrypt(ctx context.Context, cipher wrapping.Wrapper) error {
|
|
const op = "plugin.(StorageBucketSecret).encrypt"
|
|
if util.IsNil(cipher) {
|
|
return errors.New(ctx, errors.InvalidParameter, op, "missing cipher")
|
|
}
|
|
if err := structwrapping.WrapStruct(ctx, cipher, sbs.StorageBucketSecret); err != nil {
|
|
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt))
|
|
}
|
|
var err error
|
|
if sbs.KeyId, err = cipher.KeyId(ctx); err != nil {
|
|
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("failed to read cipher key id"))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (sbs *StorageBucketSecret) decrypt(ctx context.Context, cipher wrapping.Wrapper) error {
|
|
const op = "plugin.(StorageBucketSecret).decrypt"
|
|
if util.IsNil(cipher) {
|
|
return errors.New(ctx, errors.InvalidParameter, op, "missing cipher")
|
|
}
|
|
if err := structwrapping.UnwrapStruct(ctx, cipher, sbs.StorageBucketSecret, nil); err != nil {
|
|
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decrypt))
|
|
}
|
|
sbs.CtSecrets = nil
|
|
return nil
|
|
}
|
|
|
|
func (sbs *StorageBucketSecret) toPersisted(ctx context.Context) (*storagebuckets.StorageBucketPersisted, error) {
|
|
const op = "plugin.(StorageBucketSecret).toPersisted"
|
|
if sbs.Secrets == nil {
|
|
return nil, errors.New(ctx, errors.InvalidParameter, op, "secret data not populated")
|
|
}
|
|
sec := &storagebuckets.StorageBucketPersisted{
|
|
Data: &structpb.Struct{},
|
|
}
|
|
if err := proto.Unmarshal(sbs.Secrets, sec.Data); err != nil {
|
|
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.InvalidParameter))
|
|
}
|
|
return sec, nil
|
|
}
|
|
|
|
type storageBucketAgg struct {
|
|
PublicId string `gorm:"primary_key"`
|
|
ScopeId string
|
|
Name string
|
|
Description string
|
|
CreateTime *timestamp.Timestamp
|
|
UpdateTime *timestamp.Timestamp
|
|
Version uint32
|
|
PluginId string
|
|
BucketName string
|
|
BucketPrefix string
|
|
WorkerFilter string
|
|
Attributes []byte
|
|
SecretsEncrypted []byte
|
|
SecretsHmac []byte
|
|
KeyId string
|
|
}
|
|
|
|
// TableName returns the table name for gorm
|
|
func (sba *storageBucketAgg) TableName() string {
|
|
return "storage_plugin_storage_bucket_with_secret"
|
|
}
|
|
|
|
func (sba *storageBucketAgg) GetPublicId() string {
|
|
return sba.PublicId
|
|
}
|
|
|
|
func (sba *storageBucketAgg) toStorageBucketAndSecret() (*StorageBucket, *StorageBucketSecret) {
|
|
sb := allocStorageBucket()
|
|
sb.PublicId = sba.PublicId
|
|
sb.ScopeId = sba.ScopeId
|
|
sb.Name = sba.Name
|
|
sb.Description = sba.Description
|
|
sb.CreateTime = sba.CreateTime
|
|
sb.UpdateTime = sba.UpdateTime
|
|
sb.Version = sba.Version
|
|
sb.PluginId = sba.PluginId
|
|
sb.BucketName = sba.BucketName
|
|
sb.BucketPrefix = sba.BucketPrefix
|
|
sb.WorkerFilter = sba.WorkerFilter
|
|
sb.Attributes = sba.Attributes
|
|
sb.SecretsHmac = sba.SecretsHmac
|
|
|
|
var sbs *StorageBucketSecret
|
|
if len(sba.SecretsEncrypted) > 0 {
|
|
sbs = allocStorageBucketSecret()
|
|
sbs.StorageBucketId = sba.PublicId
|
|
sbs.CtSecrets = sba.SecretsEncrypted
|
|
sbs.KeyId = sba.KeyId
|
|
}
|
|
return sb, sbs
|
|
}
|