|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: BUSL-1.1 |
| 3 | + |
| 4 | +package static |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/hashicorp/boundary/internal/credential" |
| 10 | + "github.com/hashicorp/boundary/internal/credential/static/store" |
| 11 | + "github.com/hashicorp/boundary/internal/db/timestamp" |
| 12 | + "github.com/hashicorp/boundary/internal/errors" |
| 13 | + "github.com/hashicorp/boundary/internal/libs/crypto" |
| 14 | + "github.com/hashicorp/boundary/internal/oplog" |
| 15 | + "github.com/hashicorp/boundary/internal/types/resource" |
| 16 | + wrapping "github.com/hashicorp/go-kms-wrapping/v2" |
| 17 | + "github.com/hashicorp/go-kms-wrapping/v2/extras/structwrapping" |
| 18 | + "google.golang.org/protobuf/proto" |
| 19 | +) |
| 20 | + |
| 21 | +var _ credential.Static = (*PasswordCredential)(nil) |
| 22 | + |
| 23 | +// PasswordCredential contains the credential with a password. |
| 24 | +// It is owned by a credential store. |
| 25 | +type PasswordCredential struct { |
| 26 | + *store.PasswordCredential |
| 27 | + tableName string `gorm:"-"` |
| 28 | +} |
| 29 | + |
| 30 | +// NewPasswordCredential creates a new in memory static Credential containing a |
| 31 | +// password that is assigned to storeId. Name and description are the only |
| 32 | +// valid options. All other options are ignored. |
| 33 | +func NewPasswordCredential( |
| 34 | + storeId string, |
| 35 | + password credential.Password, |
| 36 | + opt ...Option, |
| 37 | +) (*PasswordCredential, error) { |
| 38 | + opts := getOpts(opt...) |
| 39 | + l := &PasswordCredential{ |
| 40 | + PasswordCredential: &store.PasswordCredential{ |
| 41 | + StoreId: storeId, |
| 42 | + Name: opts.withName, |
| 43 | + Description: opts.withDescription, |
| 44 | + Password: []byte(password), |
| 45 | + }, |
| 46 | + } |
| 47 | + return l, nil |
| 48 | +} |
| 49 | + |
| 50 | +func allocPasswordCredential() *PasswordCredential { |
| 51 | + return &PasswordCredential{ |
| 52 | + PasswordCredential: &store.PasswordCredential{}, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func (c *PasswordCredential) clone() *PasswordCredential { |
| 57 | + cp := proto.Clone(c.PasswordCredential) |
| 58 | + return &PasswordCredential{ |
| 59 | + PasswordCredential: cp.(*store.PasswordCredential), |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// TableName returns the table name. |
| 64 | +func (c *PasswordCredential) TableName() string { |
| 65 | + if c.tableName != "" { |
| 66 | + return c.tableName |
| 67 | + } |
| 68 | + return "credential_static_password_credential" |
| 69 | +} |
| 70 | + |
| 71 | +// SetTableName sets the table name. |
| 72 | +func (c *PasswordCredential) SetTableName(n string) { |
| 73 | + c.tableName = n |
| 74 | +} |
| 75 | + |
| 76 | +// GetResourceType returns the resource type of the Credential |
| 77 | +func (c *PasswordCredential) GetResourceType() resource.Type { |
| 78 | + return resource.Credential |
| 79 | +} |
| 80 | + |
| 81 | +func (c *PasswordCredential) oplog(op oplog.OpType) oplog.Metadata { |
| 82 | + metadata := oplog.Metadata{ |
| 83 | + "resource-public-id": []string{c.PublicId}, |
| 84 | + "resource-type": []string{"credential-static-password"}, |
| 85 | + "op-type": []string{op.String()}, |
| 86 | + } |
| 87 | + if c.StoreId != "" { |
| 88 | + metadata["store-id"] = []string{c.StoreId} |
| 89 | + } |
| 90 | + return metadata |
| 91 | +} |
| 92 | + |
| 93 | +func (c *PasswordCredential) encrypt(ctx context.Context, cipher wrapping.Wrapper) error { |
| 94 | + const op = "static.(PasswordCredential).encrypt" |
| 95 | + if len(c.Password) == 0 { |
| 96 | + return errors.New(ctx, errors.InvalidParameter, op, "no password defined") |
| 97 | + } |
| 98 | + if err := structwrapping.WrapStruct(ctx, cipher, c.PasswordCredential, nil); err != nil { |
| 99 | + return errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt)) |
| 100 | + } |
| 101 | + keyId, err := cipher.KeyId(ctx) |
| 102 | + if err != nil { |
| 103 | + return errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("error reading cipher key id")) |
| 104 | + } |
| 105 | + c.KeyId = keyId |
| 106 | + if err := c.hmacPassword(ctx, cipher); err != nil { |
| 107 | + return errors.Wrap(ctx, err, op) |
| 108 | + } |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +func (c *PasswordCredential) decrypt(ctx context.Context, cipher wrapping.Wrapper) error { |
| 113 | + const op = "static.(PasswordCredential).decrypt" |
| 114 | + if err := structwrapping.UnwrapStruct(ctx, cipher, c.PasswordCredential, nil); err != nil { |
| 115 | + return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decrypt)) |
| 116 | + } |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +func (c *PasswordCredential) hmacPassword(ctx context.Context, cipher wrapping.Wrapper) error { |
| 121 | + const op = "static.(PasswordCredential).hmacPassword" |
| 122 | + if cipher == nil { |
| 123 | + return errors.New(ctx, errors.InvalidParameter, op, "missing cipher") |
| 124 | + } |
| 125 | + hm, err := crypto.HmacSha256(ctx, c.Password, cipher, []byte(c.StoreId), nil, crypto.WithEd25519()) |
| 126 | + if err != nil { |
| 127 | + return errors.Wrap(ctx, err, op) |
| 128 | + } |
| 129 | + c.PasswordHmac = []byte(hm) |
| 130 | + return nil |
| 131 | +} |
| 132 | + |
| 133 | +type deletedPasswordCredential struct { |
| 134 | + PublicId string `gorm:"primary_key"` |
| 135 | + DeleteTime *timestamp.Timestamp |
| 136 | +} |
| 137 | + |
| 138 | +// TableName returns the tablename to override the default gorm table name |
| 139 | +func (s *deletedPasswordCredential) TableName() string { |
| 140 | + return "credential_static_password_credential_deleted" |
| 141 | +} |
0 commit comments