* Bump generated proto code * Add argon2 conf sql and proto files * Add generated code for argon2 conf * Add Resources for Argon2 configuration * Add get and set Configuration to repository * Add current config reference to AuthMethod * Fix authmethod_test * Fix TestAccount_New * Add WithConfiguration to options * Add sql views for current configuration * Fix SetTableName * Remove duplicate testAccounts function * Make tests clearer * Fix comment * Rename test variable * Use NewPrivateId function for private Ids * Refactor: rename public_id to private_id for password_conf and password_credential * Remove bad comment * Check if embedded argon2 configuration is nil * Add checks around the WithConfiguration option * Use spaces not tabs in proto file * Add doc to explain choice of oplog ticket * Verify oplog in test
79 lines
2.6 KiB
Go
79 lines
2.6 KiB
Go
package password
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/watchtower/internal/db"
|
|
"github.com/hashicorp/watchtower/internal/oplog"
|
|
)
|
|
|
|
// CreateAuthMethod inserts m into the repository and returns a new
|
|
// AuthMethod containing the auth method's PublicId. m is not changed. m must
|
|
// contain a valid ScopeId. m must not contain a PublicId. The PublicId is
|
|
// generated and assigned by this method.
|
|
//
|
|
// WithConfiguration is the only valid option. All other options are
|
|
// ignored.
|
|
//
|
|
// Both m.Name and m.Description are optional. If m.Name is set, it must be
|
|
// unique within m.ScopeId.
|
|
func (r *Repository) CreateAuthMethod(ctx context.Context, m *AuthMethod, opt ...Option) (*AuthMethod, error) {
|
|
if m == nil {
|
|
return nil, fmt.Errorf("create: password auth method: %w", db.ErrNilParameter)
|
|
}
|
|
if m.AuthMethod == nil {
|
|
return nil, fmt.Errorf("create: password auth method: embedded AuthMethod: %w", db.ErrNilParameter)
|
|
}
|
|
if m.ScopeId == "" {
|
|
return nil, fmt.Errorf("create: password auth method: no scope id: %w", db.ErrInvalidParameter)
|
|
}
|
|
if m.PublicId != "" {
|
|
return nil, fmt.Errorf("create: password auth method: public id not empty: %w", db.ErrInvalidParameter)
|
|
}
|
|
m = m.clone()
|
|
|
|
id, err := newAuthMethodId()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create: password auth method: %w", err)
|
|
}
|
|
m.PublicId = id
|
|
|
|
opts := getOpts(opt...)
|
|
c, ok := opts.withConfig.(*Argon2Configuration)
|
|
if !ok {
|
|
return nil, fmt.Errorf("create: password auth method: unknown configuration: %w", ErrUnsupportedConfiguration)
|
|
}
|
|
if err := c.validate(); err != nil {
|
|
return nil, fmt.Errorf("create: password auth method: %w", err)
|
|
}
|
|
|
|
c.PrivateId, err = newArgon2ConfigurationId()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create: password auth method: %w", err)
|
|
}
|
|
m.PasswordConfId, c.PasswordMethodId = c.PrivateId, m.PublicId
|
|
|
|
var newAuthMethod *AuthMethod
|
|
var newArgon2Conf *Argon2Configuration
|
|
_, err = r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
|
|
func(_ db.Reader, w db.Writer) error {
|
|
newArgon2Conf = c.clone()
|
|
if err := w.Create(ctx, newArgon2Conf, db.WithOplog(r.wrapper, c.oplog(oplog.OpType_OP_TYPE_CREATE))); err != nil {
|
|
return err
|
|
}
|
|
newAuthMethod = m.clone()
|
|
return w.Create(ctx, newAuthMethod, db.WithOplog(r.wrapper, m.oplog(oplog.OpType_OP_TYPE_CREATE)))
|
|
},
|
|
)
|
|
|
|
if err != nil {
|
|
if db.IsUniqueError(err) {
|
|
return nil, fmt.Errorf("create: password auth method: in scope: %s: name %s already exists: %w",
|
|
m.ScopeId, m.Name, db.ErrNotUnique)
|
|
}
|
|
return nil, fmt.Errorf("create: password auth method: in scope: %s: %w", m.ScopeId, err)
|
|
}
|
|
return newAuthMethod, nil
|
|
}
|