2020-08-10 17:15:02 -04:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-02-02 17:15:17 -08:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
2020-07-24 10:23:35 -04:00
|
|
|
package password
|
|
|
|
|
|
|
|
|
|
import (
|
2023-06-06 17:09:08 -04:00
|
|
|
"context"
|
2020-07-31 14:35:56 -07:00
|
|
|
"strings"
|
2020-07-24 10:23:35 -04:00
|
|
|
|
2020-08-10 17:15:02 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/db"
|
2020-11-05 16:54:03 -05:00
|
|
|
"github.com/hashicorp/boundary/internal/errors"
|
2020-08-19 16:47:18 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/kms"
|
2020-07-24 10:23:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// A Repository stores and retrieves the persistent types in the password
|
|
|
|
|
// package. It is not safe to use a repository concurrently.
|
|
|
|
|
type Repository struct {
|
2020-08-19 16:47:18 -04:00
|
|
|
reader db.Reader
|
|
|
|
|
writer db.Writer
|
|
|
|
|
kms *kms.Kms
|
2020-07-24 11:38:27 -07:00
|
|
|
// defaultLimit provides a default for limiting the number of results returned from the repo
|
|
|
|
|
defaultLimit int
|
2020-07-24 10:23:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewRepository creates a new Repository. The returned repository should
|
|
|
|
|
// only be used for one transaction and it is not safe for concurrent go
|
2020-07-24 11:38:27 -07:00
|
|
|
// routines to access it. WithLimit option is used as a repo wide default
|
|
|
|
|
// limit applied to all ListX methods.
|
2023-06-06 17:09:08 -04:00
|
|
|
func NewRepository(ctx context.Context, r db.Reader, w db.Writer, kms *kms.Kms, opt ...Option) (*Repository, error) {
|
2021-02-11 13:32:45 -06:00
|
|
|
const op = "password.NewRepository"
|
2020-07-24 10:23:35 -04:00
|
|
|
switch {
|
|
|
|
|
case r == nil:
|
2023-06-06 17:09:08 -04:00
|
|
|
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing db.Reader")
|
2020-07-24 10:23:35 -04:00
|
|
|
case w == nil:
|
2023-06-06 17:09:08 -04:00
|
|
|
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing db.Writer")
|
2020-08-19 16:47:18 -04:00
|
|
|
case kms == nil:
|
2023-06-06 17:09:08 -04:00
|
|
|
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing kms")
|
2020-07-24 10:23:35 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-25 21:33:31 -04:00
|
|
|
opts := GetOpts(opt...)
|
2020-07-24 11:38:27 -07:00
|
|
|
if opts.withLimit == 0 {
|
2020-08-10 17:15:02 -04:00
|
|
|
// zero signals the boundary defaults should be used.
|
2020-07-24 11:38:27 -07:00
|
|
|
opts.withLimit = db.DefaultLimit
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-24 10:23:35 -04:00
|
|
|
return &Repository{
|
2020-07-24 11:38:27 -07:00
|
|
|
reader: r,
|
|
|
|
|
writer: w,
|
2020-08-19 16:47:18 -04:00
|
|
|
kms: kms,
|
2020-07-24 11:38:27 -07:00
|
|
|
defaultLimit: opts.withLimit,
|
2020-07-24 10:23:35 -04:00
|
|
|
}, nil
|
|
|
|
|
}
|
2020-07-31 14:35:56 -07:00
|
|
|
|
|
|
|
|
func contains(ss []string, t string) bool {
|
|
|
|
|
for _, s := range ss {
|
|
|
|
|
if strings.EqualFold(s, t) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|