Update primary error functions to take a context, deprecate old functions (#1358)
* add new error funcs which take a ctx and deprecate existing funcs * use context where available when writing error events * remove circular internal/errors pkg dep * suppress some chatty errors using errors.WithoutEvent() * convert auth oidc funcs to take context where needed to write events Co-authored-by: Jim <jlambert@hashicorp.com>
This commit is contained in:
1 parent
02ff2db66e
commit
6b78108ecf
227 files changed
+3945
-3736
No files matched your search
@@ -27,7 +27,7 @@ type ClientCertificate struct {
|
||||
func NewClientCertificate(certificate []byte, key KeySecret) (*ClientCertificate, error) {
|
||||
const op = "vault.NewClientCertificate"
|
||||
if len(certificate) == 0 {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no certificate")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "no certificate")
|
||||
}
|
||||
|
||||
certificateCopy := make([]byte, len(certificate))
|
||||
@@ -77,14 +77,14 @@ func (c *ClientCertificate) SetTableName(n string) {
|
||||
func (c *ClientCertificate) encrypt(ctx context.Context, cipher wrapping.Wrapper) error {
|
||||
const op = "vault.(ClientCertificate).encrypt"
|
||||
if len(c.CertificateKey) == 0 {
|
||||
errors.New(errors.InvalidParameter, op, "no certificate key defined")
|
||||
errors.New(ctx, errors.InvalidParameter, op, "no certificate key defined")
|
||||
}
|
||||
if err := structwrapping.WrapStruct(ctx, cipher, c.ClientCertificate, nil); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithCode(errors.Encrypt))
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt))
|
||||
}
|
||||
c.KeyId = cipher.KeyID()
|
||||
if err := c.hmacCertificateKey(ctx, cipher); err != nil {
|
||||
errors.Wrap(err, op)
|
||||
errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -92,7 +92,7 @@ func (c *ClientCertificate) encrypt(ctx context.Context, cipher wrapping.Wrapper
|
||||
func (c *ClientCertificate) decrypt(ctx context.Context, cipher wrapping.Wrapper) error {
|
||||
const op = "vault.(ClientCertificate).decrypt"
|
||||
if err := structwrapping.UnwrapStruct(ctx, cipher, c.ClientCertificate, nil); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithCode(errors.Decrypt))
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decrypt))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -100,15 +100,15 @@ func (c *ClientCertificate) decrypt(ctx context.Context, cipher wrapping.Wrapper
|
||||
func (c *ClientCertificate) hmacCertificateKey(ctx context.Context, cipher wrapping.Wrapper) error {
|
||||
const op = "vault.(ClientCertificate).hmacCertificateKey"
|
||||
if cipher == nil {
|
||||
return errors.New(errors.InvalidParameter, op, "missing cipher")
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "missing cipher")
|
||||
}
|
||||
reader, err := kms.NewDerivedReader(cipher, 32, []byte(c.StoreId), nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
key, _, err := ed25519.GenerateKey(reader)
|
||||
if err != nil {
|
||||
return errors.New(errors.Encrypt, op, "unable to generate derived key")
|
||||
return errors.New(ctx, errors.Encrypt, op, "unable to generate derived key")
|
||||
}
|
||||
mac := hmac.New(sha256.New, key)
|
||||
_, _ = mac.Write(c.CertificateKey)
|
||||
|
||||
@@ -48,13 +48,13 @@ type Credential struct {
|
||||
func newCredential(libraryId, sessionId, externalId string, tokenHmac []byte, expiration time.Duration) (*Credential, error) {
|
||||
const op = "vault.newCredential"
|
||||
if libraryId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no library id")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "no library id")
|
||||
}
|
||||
if sessionId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no session id")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "no session id")
|
||||
}
|
||||
if len(tokenHmac) == 0 {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no tokenHmac")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "no tokenHmac")
|
||||
}
|
||||
|
||||
status := string(ActiveCredential)
|
||||
|
||||
@@ -182,7 +182,7 @@ func (cs *CredentialStore) client() (*client, error) {
|
||||
|
||||
c, err := newClient(clientConfig)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.WrapDeprecated(err, op)
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
@@ -32,45 +32,45 @@ func RegisterJobs(ctx context.Context, scheduler *scheduler.Scheduler, r db.Read
|
||||
const op = "vault.RegisterJobs"
|
||||
tokenRenewal, err := newTokenRenewalJob(r, w, kms, logger)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if err = scheduler.RegisterJob(ctx, tokenRenewal); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("token renewal job"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("token renewal job"))
|
||||
}
|
||||
tokenRevoke, err := newTokenRevocationJob(r, w, kms, logger)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if err = scheduler.RegisterJob(ctx, tokenRevoke); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("token revocation job"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("token revocation job"))
|
||||
}
|
||||
credRenewal, err := newCredentialRenewalJob(r, w, kms, logger)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if err = scheduler.RegisterJob(ctx, credRenewal); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("credential renewal job"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("credential renewal job"))
|
||||
}
|
||||
credRevoke, err := newCredentialRevocationJob(r, w, kms, logger)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if err = scheduler.RegisterJob(ctx, credRevoke); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("credential revocation job"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("credential revocation job"))
|
||||
}
|
||||
credStoreCleanup, err := newCredentialStoreCleanupJob(r, w, kms, logger)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if err = scheduler.RegisterJob(ctx, credStoreCleanup); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("credential store cleanup job"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("credential store cleanup job"))
|
||||
}
|
||||
credCleanup, err := newCredentialCleanupJob(w)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if err = scheduler.RegisterJob(ctx, credCleanup); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("credential cleanup job"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("credential cleanup job"))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -97,13 +97,13 @@ func newTokenRenewalJob(r db.Reader, w db.Writer, kms *kms.Kms, logger hclog.Log
|
||||
const op = "vault.newTokenRenewalJob"
|
||||
switch {
|
||||
case r == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing db.Reader")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing db.Reader")
|
||||
case w == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing db.Writer")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing db.Writer")
|
||||
case kms == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing kms")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing kms")
|
||||
case logger == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing logger")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing logger")
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
@@ -135,13 +135,13 @@ func (r *TokenRenewalJob) Status() scheduler.JobStatus {
|
||||
func (r *TokenRenewalJob) Run(ctx context.Context) error {
|
||||
const op = "vault.(TokenRenewalJob).Run"
|
||||
if !r.running.CAS(r.running.Load(), true) {
|
||||
return errors.New(errors.JobAlreadyRunning, op, "job already running")
|
||||
return errors.New(ctx, errors.JobAlreadyRunning, op, "job already running")
|
||||
}
|
||||
defer r.running.Store(false)
|
||||
|
||||
// Verify context is not done before running
|
||||
if err := ctx.Err(); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
var ps []*privateStore
|
||||
@@ -150,7 +150,7 @@ func (r *TokenRenewalJob) Run(ctx context.Context) error {
|
||||
// set to renew in sequence.
|
||||
err := r.reader.SearchWhere(ctx, &ps, `token_renewal_time < wt_add_seconds_to_now(?)`, []interface{}{renewalWindow.Seconds()}, db.WithLimit(r.limit))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
// Set numProcessed and numTokens for status report
|
||||
@@ -159,7 +159,7 @@ func (r *TokenRenewalJob) Run(ctx context.Context) error {
|
||||
for _, s := range ps {
|
||||
// Verify context is not done before renewing next token
|
||||
if err := ctx.Err(); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if err := r.renewToken(ctx, s); err != nil {
|
||||
event.WriteError(ctx, op, err, event.WithInfoMsg("error renewing token", "credential store id", s.StoreId, "token status", s.TokenStatus))
|
||||
@@ -174,10 +174,10 @@ func (r *TokenRenewalJob) renewToken(ctx context.Context, s *privateStore) error
|
||||
const op = "vault.(TokenRenewalJob).renewToken"
|
||||
databaseWrapper, err := r.kms.GetWrapper(ctx, s.ScopeId, kms.KeyPurposeDatabase)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
}
|
||||
if err = s.decrypt(ctx, databaseWrapper); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
token := s.token()
|
||||
@@ -188,7 +188,7 @@ func (r *TokenRenewalJob) renewToken(ctx context.Context, s *privateStore) error
|
||||
|
||||
vc, err := s.client()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
var respErr *vault.ResponseError
|
||||
@@ -200,10 +200,10 @@ func (r *TokenRenewalJob) renewToken(ctx context.Context, s *privateStore) error
|
||||
query, values := token.updateStatusQuery(ExpiredToken)
|
||||
numRows, err := r.writer.Exec(ctx, query, values)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if numRows != 1 {
|
||||
return errors.New(errors.Unknown, op, "token expired but failed to update repo")
|
||||
return errors.New(ctx, errors.Unknown, op, "token expired but failed to update repo")
|
||||
}
|
||||
if s.TokenStatus == string(CurrentToken) {
|
||||
event.WriteSysEvent(ctx, op, "Vault credential store current token has expired", "credential store id", s.StoreId)
|
||||
@@ -212,28 +212,28 @@ func (r *TokenRenewalJob) renewToken(ctx context.Context, s *privateStore) error
|
||||
// Set credentials associated with this token to expired as Vault will already cascade delete them
|
||||
_, err = r.writer.Exec(ctx, updateCredentialStatusByTokenQuery, []interface{}{ExpiredCredential, token.TokenHmac})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("error updating credentials to revoked after revoking token"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("error updating credentials to revoked after revoking token"))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to renew vault token"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to renew vault token"))
|
||||
}
|
||||
|
||||
tokenExpires, err := renewedToken.TokenTTL()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to get vault token expiration"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get vault token expiration"))
|
||||
}
|
||||
|
||||
token.expiration = tokenExpires
|
||||
query, values := token.updateExpirationQuery()
|
||||
numRows, err := r.writer.Exec(ctx, query, values)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if numRows != 1 {
|
||||
return errors.New(errors.Unknown, op, "token renewed but failed to update repo")
|
||||
return errors.New(ctx, errors.Unknown, op, "token renewed but failed to update repo")
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -244,7 +244,7 @@ func (r *TokenRenewalJob) NextRunIn() (time.Duration, error) {
|
||||
const op = "vault.(TokenRenewalJob).NextRunIn"
|
||||
next, err := nextRenewal(r)
|
||||
if err != nil {
|
||||
return defaultNextRunIn, errors.Wrap(err, op)
|
||||
return defaultNextRunIn, errors.WrapDeprecated(err, op)
|
||||
}
|
||||
|
||||
return next, nil
|
||||
@@ -262,12 +262,12 @@ func nextRenewal(j scheduler.Job) (time.Duration, error) {
|
||||
query = credentialRenewalNextRunInQuery
|
||||
r = job.reader
|
||||
default:
|
||||
return 0, errors.New(errors.Unknown, op, "unknown job")
|
||||
return 0, errors.NewDeprecated(errors.Unknown, op, "unknown job")
|
||||
}
|
||||
|
||||
rows, err := r.Query(context.Background(), query, nil)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, op)
|
||||
return 0, errors.WrapDeprecated(err, op)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
@@ -278,7 +278,7 @@ func nextRenewal(j scheduler.Job) (time.Duration, error) {
|
||||
var n NextRenewal
|
||||
err = r.ScanRows(rows, &n)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, op)
|
||||
return 0, errors.WrapDeprecated(err, op)
|
||||
}
|
||||
if n.RenewalIn < 0 {
|
||||
// If we are past the next renewal time, return 0 to schedule immediately
|
||||
@@ -323,13 +323,13 @@ func newTokenRevocationJob(r db.Reader, w db.Writer, kms *kms.Kms, logger hclog.
|
||||
const op = "vault.newTokenRevocationJob"
|
||||
switch {
|
||||
case r == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing db.Reader")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing db.Reader")
|
||||
case w == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing db.Writer")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing db.Writer")
|
||||
case kms == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing kms")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing kms")
|
||||
case logger == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing logger")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing logger")
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
@@ -361,13 +361,13 @@ func (r *TokenRevocationJob) Status() scheduler.JobStatus {
|
||||
func (r *TokenRevocationJob) Run(ctx context.Context) error {
|
||||
const op = "vault.(TokenRevocationJob).Run"
|
||||
if !r.running.CAS(r.running.Load(), true) {
|
||||
return errors.New(errors.JobAlreadyRunning, op, "job already running")
|
||||
return errors.New(ctx, errors.JobAlreadyRunning, op, "job already running")
|
||||
}
|
||||
defer r.running.Store(false)
|
||||
|
||||
// Verify context is not done before running
|
||||
if err := ctx.Err(); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
// Fetch all tokens in the revoke state as well as all tokens in the maintaining state
|
||||
@@ -385,7 +385,7 @@ or
|
||||
var ps []*privateStore
|
||||
err := r.reader.SearchWhere(ctx, &ps, where, nil, db.WithLimit(r.limit))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
// Set numProcessed and numTokens for s report
|
||||
@@ -393,7 +393,7 @@ or
|
||||
for _, s := range ps {
|
||||
// Verify context is not done before renewing next token
|
||||
if err := ctx.Err(); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if err := r.revokeToken(ctx, s); err != nil {
|
||||
event.WriteError(ctx, op, err, event.WithInfoMsg("error revoking token", "credential store id", s.StoreId))
|
||||
@@ -408,10 +408,10 @@ func (r *TokenRevocationJob) revokeToken(ctx context.Context, s *privateStore) e
|
||||
const op = "vault.(TokenRevocationJob).revokeToken"
|
||||
databaseWrapper, err := r.kms.GetWrapper(ctx, s.ScopeId, kms.KeyPurposeDatabase)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
}
|
||||
if err = s.decrypt(ctx, databaseWrapper); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
token := s.token()
|
||||
@@ -422,7 +422,7 @@ func (r *TokenRevocationJob) revokeToken(ctx context.Context, s *privateStore) e
|
||||
|
||||
vc, err := s.client()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
var respErr *vault.ResponseError
|
||||
@@ -433,22 +433,22 @@ func (r *TokenRevocationJob) revokeToken(ctx context.Context, s *privateStore) e
|
||||
err = nil
|
||||
}
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to revoke vault token"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to revoke vault token"))
|
||||
}
|
||||
|
||||
query, values := token.updateStatusQuery(RevokedToken)
|
||||
numRows, err := r.writer.Exec(ctx, query, values)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if numRows != 1 {
|
||||
return errors.New(errors.Unknown, op, "token revoked but failed to update repo")
|
||||
return errors.New(ctx, errors.Unknown, op, "token revoked but failed to update repo")
|
||||
}
|
||||
|
||||
// Set credentials associated with this token to revoked as Vault will already cascade revoke them
|
||||
_, err = r.writer.Exec(ctx, updateCredentialStatusByTokenQuery, []interface{}{RevokedCredential, token.TokenHmac})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("error updating credentials to revoked after revoking token"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("error updating credentials to revoked after revoking token"))
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -491,13 +491,13 @@ func newCredentialRenewalJob(r db.Reader, w db.Writer, kms *kms.Kms, logger hclo
|
||||
const op = "vault.newCredentialRenewalJob"
|
||||
switch {
|
||||
case r == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing db.Reader")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing db.Reader")
|
||||
case w == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing db.Writer")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing db.Writer")
|
||||
case kms == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing kms")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing kms")
|
||||
case logger == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing logger")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing logger")
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
@@ -529,13 +529,13 @@ func (r *CredentialRenewalJob) Status() scheduler.JobStatus {
|
||||
func (r *CredentialRenewalJob) Run(ctx context.Context) error {
|
||||
const op = "vault.(CredentialRenewalJob).Run"
|
||||
if !r.running.CAS(r.running.Load(), true) {
|
||||
return errors.New(errors.JobAlreadyRunning, op, "job already running")
|
||||
return errors.New(ctx, errors.JobAlreadyRunning, op, "job already running")
|
||||
}
|
||||
defer r.running.Store(false)
|
||||
|
||||
// Verify context is not done before running
|
||||
if err := ctx.Err(); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
var creds []*privateCredential
|
||||
@@ -544,7 +544,7 @@ func (r *CredentialRenewalJob) Run(ctx context.Context) error {
|
||||
// multiple credentials set to renew in sequence.
|
||||
err := r.reader.SearchWhere(ctx, &creds, `renewal_time < wt_add_seconds_to_now(?) and status = ?`, []interface{}{renewalWindow.Seconds(), ActiveCredential}, db.WithLimit(r.limit))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
// Set numProcessed and numTokens for status report
|
||||
@@ -552,7 +552,7 @@ func (r *CredentialRenewalJob) Run(ctx context.Context) error {
|
||||
for _, c := range creds {
|
||||
// Verify context is not done before renewing next token
|
||||
if err := ctx.Err(); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
if err := r.renewCred(ctx, c); err != nil {
|
||||
@@ -569,15 +569,15 @@ func (r *CredentialRenewalJob) renewCred(ctx context.Context, c *privateCredenti
|
||||
const op = "vault.(CredentialRenewalJob).renewCred"
|
||||
databaseWrapper, err := r.kms.GetWrapper(ctx, c.ScopeId, kms.KeyPurposeDatabase)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
}
|
||||
if err = c.decrypt(ctx, databaseWrapper); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
vc, err := c.client()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
cred := c.toCredential()
|
||||
|
||||
@@ -591,25 +591,25 @@ func (r *CredentialRenewalJob) renewCred(ctx context.Context, c *privateCredenti
|
||||
query, values := cred.updateStatusQuery(ExpiredCredential)
|
||||
numRows, err := r.writer.Exec(ctx, query, values)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if numRows != 1 {
|
||||
return errors.New(errors.Unknown, op, "credential expired but failed to update repo")
|
||||
return errors.New(ctx, errors.Unknown, op, "credential expired but failed to update repo")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to renew credential"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to renew credential"))
|
||||
}
|
||||
|
||||
cred.expiration = time.Duration(renewedCred.LeaseDuration) * time.Second
|
||||
query, values := cred.updateExpirationQuery()
|
||||
numRows, err := r.writer.Exec(ctx, query, values)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if numRows != 1 {
|
||||
return errors.New(errors.Unknown, op, "credential renewed but failed to update repo")
|
||||
return errors.New(ctx, errors.Unknown, op, "credential renewed but failed to update repo")
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -620,7 +620,7 @@ func (r *CredentialRenewalJob) NextRunIn() (time.Duration, error) {
|
||||
const op = "vault.(CredentialRenewalJob).NextRunIn"
|
||||
next, err := nextRenewal(r)
|
||||
if err != nil {
|
||||
return defaultNextRunIn, errors.Wrap(err, op)
|
||||
return defaultNextRunIn, errors.WrapDeprecated(err, op)
|
||||
}
|
||||
|
||||
return next, nil
|
||||
@@ -659,13 +659,13 @@ func newCredentialRevocationJob(r db.Reader, w db.Writer, kms *kms.Kms, logger h
|
||||
const op = "vault.newCredentialRevocationJob"
|
||||
switch {
|
||||
case r == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing db.Reader")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing db.Reader")
|
||||
case w == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing db.Writer")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing db.Writer")
|
||||
case kms == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing kms")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing kms")
|
||||
case logger == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing logger")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing logger")
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
@@ -697,19 +697,19 @@ func (r *CredentialRevocationJob) Status() scheduler.JobStatus {
|
||||
func (r *CredentialRevocationJob) Run(ctx context.Context) error {
|
||||
const op = "vault.(CredentialRevocationJob).Run"
|
||||
if !r.running.CAS(r.running.Load(), true) {
|
||||
return errors.New(errors.JobAlreadyRunning, op, "job already running")
|
||||
return errors.New(ctx, errors.JobAlreadyRunning, op, "job already running")
|
||||
}
|
||||
defer r.running.Store(false)
|
||||
|
||||
// Verify context is not done before running
|
||||
if err := ctx.Err(); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
var creds []*privateCredential
|
||||
err := r.reader.SearchWhere(ctx, &creds, "status = ?", []interface{}{RevokeCredential}, db.WithLimit(r.limit))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
// Set numProcessed and numTokens for status report
|
||||
@@ -717,7 +717,7 @@ func (r *CredentialRevocationJob) Run(ctx context.Context) error {
|
||||
for _, c := range creds {
|
||||
// Verify context is not done before renewing next token
|
||||
if err := ctx.Err(); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if err := r.revokeCred(ctx, c); err != nil {
|
||||
event.WriteError(ctx, op, err, event.WithInfoMsg("error revoking credential", "credential id", c.PublicId))
|
||||
@@ -732,15 +732,15 @@ func (r *CredentialRevocationJob) revokeCred(ctx context.Context, c *privateCred
|
||||
const op = "vault.(CredentialRenewalJob).revokeCred"
|
||||
databaseWrapper, err := r.kms.GetWrapper(ctx, c.ScopeId, kms.KeyPurposeDatabase)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
}
|
||||
if err = c.decrypt(ctx, databaseWrapper); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
vc, err := c.client()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
cred := c.toCredential()
|
||||
@@ -752,16 +752,16 @@ func (r *CredentialRevocationJob) revokeCred(ctx context.Context, c *privateCred
|
||||
err = nil
|
||||
}
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to revoke credential"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to revoke credential"))
|
||||
}
|
||||
|
||||
query, values := cred.updateStatusQuery(RevokedCredential)
|
||||
numRows, err := r.writer.Exec(ctx, query, values)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if numRows != 1 {
|
||||
return errors.New(errors.Unknown, op, "credential revoked but failed to update repo")
|
||||
return errors.New(ctx, errors.Unknown, op, "credential revoked but failed to update repo")
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -805,13 +805,13 @@ func newCredentialStoreCleanupJob(r db.Reader, w db.Writer, kms *kms.Kms, logger
|
||||
const op = "vault.newCredentialStoreCleanupJob"
|
||||
switch {
|
||||
case r == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing db.Reader")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing db.Reader")
|
||||
case w == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing db.Writer")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing db.Writer")
|
||||
case kms == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing kms")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing kms")
|
||||
case logger == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing logger")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing logger")
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
@@ -842,13 +842,13 @@ func (r *CredentialStoreCleanupJob) Status() scheduler.JobStatus {
|
||||
func (r *CredentialStoreCleanupJob) Run(ctx context.Context) error {
|
||||
const op = "vault.(CredentialStoreCleanupJob).Run"
|
||||
if !r.running.CAS(r.running.Load(), true) {
|
||||
return errors.New(errors.JobAlreadyRunning, op, "job already running")
|
||||
return errors.New(ctx, errors.JobAlreadyRunning, op, "job already running")
|
||||
}
|
||||
defer r.running.Store(false)
|
||||
|
||||
// Verify context is not done before running
|
||||
if err := ctx.Err(); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
// TODO (lcr 06/2021): Oplog does not currently support bulk
|
||||
@@ -857,7 +857,7 @@ func (r *CredentialStoreCleanupJob) Run(ctx context.Context) error {
|
||||
var stores []*CredentialStore
|
||||
err := r.reader.SearchWhere(ctx, &stores, credStoreCleanupWhereClause, []interface{}{RevokeToken}, db.WithLimit(r.limit))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
// Set numProcessed and numStores for status report
|
||||
@@ -865,7 +865,7 @@ func (r *CredentialStoreCleanupJob) Run(ctx context.Context) error {
|
||||
for _, store := range stores {
|
||||
// Verify context is not done before renewing next token
|
||||
if err := ctx.Err(); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, store.ScopeId, kms.KeyPurposeOplog)
|
||||
@@ -918,7 +918,7 @@ type CredentialCleanupJob struct {
|
||||
func newCredentialCleanupJob(w db.Writer) (*CredentialCleanupJob, error) {
|
||||
const op = "vault.newCredentialCleanupJob"
|
||||
if w == nil {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing db.Writer")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "missing db.Writer")
|
||||
}
|
||||
|
||||
return &CredentialCleanupJob{
|
||||
@@ -942,18 +942,18 @@ func (r *CredentialCleanupJob) Status() scheduler.JobStatus {
|
||||
func (r *CredentialCleanupJob) Run(ctx context.Context) error {
|
||||
const op = "vault.(CredentialCleanupJob).Run"
|
||||
if !r.running.CAS(r.running.Load(), true) {
|
||||
return errors.New(errors.JobAlreadyRunning, op, "job already running")
|
||||
return errors.New(ctx, errors.JobAlreadyRunning, op, "job already running")
|
||||
}
|
||||
defer r.running.Store(false)
|
||||
|
||||
// Verify context is not done before running
|
||||
if err := ctx.Err(); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
numRows, err := r.writer.Exec(ctx, credCleanupQuery, nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
r.numCreds = numRows
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ func (pc *privateCredential) decrypt(ctx context.Context, cipher wrapping.Wrappe
|
||||
CtToken: pc.CtToken,
|
||||
}
|
||||
if err := structwrapping.UnwrapStruct(ctx, cipher, ptkv, nil); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithCode(errors.Decrypt), errors.WithMsg("token"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decrypt), errors.WithMsg("token"))
|
||||
}
|
||||
pc.Token = ptkv.Token
|
||||
}
|
||||
@@ -71,7 +71,7 @@ func (pc *privateCredential) decrypt(ctx context.Context, cipher wrapping.Wrappe
|
||||
CtKey: pc.CtClientKey,
|
||||
}
|
||||
if err := structwrapping.UnwrapStruct(ctx, cipher, pckv, nil); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithCode(errors.Decrypt), errors.WithMsg("client certificate"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decrypt), errors.WithMsg("client certificate"))
|
||||
}
|
||||
pc.ClientKey = pckv.Key
|
||||
}
|
||||
@@ -96,7 +96,7 @@ func (pc *privateCredential) client() (*client, error) {
|
||||
|
||||
client, err := newClient(clientConfig)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to create vault client"))
|
||||
return nil, errors.WrapDeprecated(err, op, errors.WithMsg("unable to create vault client"))
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ func (pl *privateLibrary) decrypt(ctx context.Context, cipher wrapping.Wrapper)
|
||||
CtToken: pl.CtToken,
|
||||
}
|
||||
if err := structwrapping.UnwrapStruct(ctx, cipher, ptkv, nil); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithCode(errors.Decrypt), errors.WithMsg("token"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decrypt), errors.WithMsg("token"))
|
||||
}
|
||||
pl.Token = ptkv.Token
|
||||
}
|
||||
@@ -126,7 +126,7 @@ func (pl *privateLibrary) decrypt(ctx context.Context, cipher wrapping.Wrapper)
|
||||
CtKey: pl.CtClientKey,
|
||||
}
|
||||
if err := structwrapping.UnwrapStruct(ctx, cipher, pckv, nil); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithCode(errors.Decrypt), errors.WithMsg("client certificate"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decrypt), errors.WithMsg("client certificate"))
|
||||
}
|
||||
pl.ClientKey = pckv.Key
|
||||
}
|
||||
@@ -151,7 +151,7 @@ func (pl *privateLibrary) client() (*client, error) {
|
||||
|
||||
client, err := newClient(clientConfig)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to create vault client"))
|
||||
return nil, errors.WrapDeprecated(err, op, errors.WithMsg("unable to create vault client"))
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
@@ -166,7 +166,7 @@ func (r *Repository) getPrivateLibraries(ctx context.Context, requests []credent
|
||||
|
||||
mapper, err := newMapper(requests)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
libIds := mapper.libIds()
|
||||
@@ -181,7 +181,7 @@ func (r *Repository) getPrivateLibraries(ctx context.Context, requests []credent
|
||||
}
|
||||
rows, err := r.reader.Query(ctx, query, params)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("query failed"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("query failed"))
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
@@ -189,11 +189,11 @@ func (r *Repository) getPrivateLibraries(ctx context.Context, requests []credent
|
||||
for rows.Next() {
|
||||
var lib privateLibrary
|
||||
if err := r.reader.ScanRows(rows, &lib); err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("scan row failed"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("scan row failed"))
|
||||
}
|
||||
purps := mapper.get(lib.GetPublicId())
|
||||
if len(purps) == 0 {
|
||||
return nil, errors.E(errors.WithCode(errors.InvalidParameter), errors.WithMsg("unknown library"))
|
||||
return nil, errors.E(ctx, errors.WithCode(errors.InvalidParameter), errors.WithMsg("unknown library"))
|
||||
}
|
||||
for _, purp := range purps {
|
||||
cp := lib.clone()
|
||||
@@ -205,11 +205,11 @@ func (r *Repository) getPrivateLibraries(ctx context.Context, requests []credent
|
||||
for _, pl := range libs {
|
||||
databaseWrapper, err := r.kms.GetWrapper(ctx, pl.ScopeId, kms.KeyPurposeDatabase)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
}
|
||||
|
||||
if err := pl.decrypt(ctx, databaseWrapper); err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,7 +236,7 @@ func newMapper(requests []credential.Request) (*requestMap, error) {
|
||||
if purps, ok := ids[req.SourceId]; ok {
|
||||
for _, purp := range purps {
|
||||
if purp == req.Purpose {
|
||||
return nil, errors.E(errors.WithCode(errors.InvalidParameter), errors.WithMsg("duplicate library and purpose"))
|
||||
return nil, errors.EDeprecated(errors.WithCode(errors.InvalidParameter), errors.WithMsg("duplicate library and purpose"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,16 +24,16 @@ func (r *Repository) listRevokePrivateStores(ctx context.Context, opt ...Option)
|
||||
var stores []*privateStore
|
||||
where, values := "token_status = ?", []interface{}{"revoke"}
|
||||
if err := r.reader.SearchWhere(ctx, &stores, where, values, db.WithLimit(limit)); err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
for _, store := range stores {
|
||||
databaseWrapper, err := r.kms.GetWrapper(ctx, store.ScopeId, kms.KeyPurposeDatabase)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
}
|
||||
if err := store.decrypt(ctx, databaseWrapper); err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
}
|
||||
return stores, nil
|
||||
@@ -42,23 +42,23 @@ func (r *Repository) listRevokePrivateStores(ctx context.Context, opt ...Option)
|
||||
func (r *Repository) lookupPrivateStore(ctx context.Context, publicId string) (*privateStore, error) {
|
||||
const op = "vault.(Repository).lookupPrivateStore"
|
||||
if publicId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
ps := allocPrivateStore()
|
||||
if err := r.reader.LookupWhere(ctx, &ps, "public_id = ? and token_status = ?", publicId, CurrentToken); err != nil {
|
||||
if errors.IsNotFoundError(err) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("failed for: %s", publicId)))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for: %s", publicId)))
|
||||
}
|
||||
|
||||
databaseWrapper, err := r.kms.GetWrapper(ctx, ps.ScopeId, kms.KeyPurposeDatabase)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
}
|
||||
|
||||
if err := ps.decrypt(ctx, databaseWrapper); err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
return ps, nil
|
||||
@@ -159,7 +159,7 @@ func (ps *privateStore) decrypt(ctx context.Context, cipher wrapping.Wrapper) er
|
||||
CtToken: ps.CtToken,
|
||||
}
|
||||
if err := structwrapping.UnwrapStruct(ctx, cipher, ptkv, nil); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithCode(errors.Decrypt), errors.WithMsg("token"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decrypt), errors.WithMsg("token"))
|
||||
}
|
||||
ps.Token = ptkv.Token
|
||||
}
|
||||
@@ -173,7 +173,7 @@ func (ps *privateStore) decrypt(ctx context.Context, cipher wrapping.Wrapper) er
|
||||
CtKey: ps.CtClientKey,
|
||||
}
|
||||
if err := structwrapping.UnwrapStruct(ctx, cipher, pckv, nil); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithCode(errors.Decrypt), errors.WithMsg("client certificate"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decrypt), errors.WithMsg("client certificate"))
|
||||
}
|
||||
ps.ClientKey = pckv.Key
|
||||
}
|
||||
@@ -198,7 +198,7 @@ func (ps *privateStore) client() (*client, error) {
|
||||
|
||||
client, err := newClient(clientConfig)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to create vault client"))
|
||||
return nil, errors.WrapDeprecated(err, op, errors.WithMsg("unable to create vault client"))
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ const (
|
||||
func newCredentialStoreId() (string, error) {
|
||||
id, err := db.NewPublicId(CredentialStorePrefix)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "vault.newCredentialStoreId")
|
||||
return "", errors.WrapDeprecated(err, "vault.newCredentialStoreId")
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
@@ -33,7 +33,7 @@ func newCredentialStoreId() (string, error) {
|
||||
func newCredentialId() (string, error) {
|
||||
id, err := db.NewPublicId(DynamicCredentialPrefix)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "vault.newCredentialId")
|
||||
return "", errors.WrapDeprecated(err, "vault.newCredentialId")
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
@@ -41,7 +41,7 @@ func newCredentialId() (string, error) {
|
||||
func newCredentialLibraryId() (string, error) {
|
||||
id, err := db.NewPublicId(CredentialLibraryPrefix)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "vault.newCredentialLibraryId")
|
||||
return "", errors.WrapDeprecated(err, "vault.newCredentialLibraryId")
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
@@ -27,13 +27,13 @@ func NewRepository(r db.Reader, w db.Writer, kms *kms.Kms, scheduler *scheduler.
|
||||
const op = "vault.NewRepository"
|
||||
switch {
|
||||
case r == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "db.Reader")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "db.Reader")
|
||||
case w == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "db.Writer")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "db.Writer")
|
||||
case kms == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "kms")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "kms")
|
||||
case scheduler == nil:
|
||||
return nil, errors.New(errors.InvalidParameter, op, "scheduler")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "scheduler")
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
|
||||
@@ -25,22 +25,22 @@ import (
|
||||
func (r *Repository) CreateCredentialLibrary(ctx context.Context, scopeId string, l *CredentialLibrary, _ ...Option) (*CredentialLibrary, error) {
|
||||
const op = "vault.(Repository).CreateCredentialLibrary"
|
||||
if l == nil {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "nil CredentialLibrary")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil CredentialLibrary")
|
||||
}
|
||||
if l.CredentialLibrary == nil {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "nil embedded l")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil embedded l")
|
||||
}
|
||||
if l.StoreId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no store id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no store id")
|
||||
}
|
||||
if l.VaultPath == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no vault path")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no vault path")
|
||||
}
|
||||
if l.PublicId != "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "public id not empty")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "public id not empty")
|
||||
}
|
||||
if scopeId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
l = l.clone()
|
||||
|
||||
@@ -50,13 +50,13 @@ func (r *Repository) CreateCredentialLibrary(ctx context.Context, scopeId string
|
||||
|
||||
id, err := newCredentialLibraryId()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
l.PublicId = id
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
var newCredentialLibrary *CredentialLibrary
|
||||
@@ -65,7 +65,7 @@ func (r *Repository) CreateCredentialLibrary(ctx context.Context, scopeId string
|
||||
newCredentialLibrary = l.clone()
|
||||
err := w.Create(ctx, newCredentialLibrary, db.WithOplog(oplogWrapper, l.oplog(oplog.OpType_OP_TYPE_CREATE)))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
@@ -73,9 +73,9 @@ func (r *Repository) CreateCredentialLibrary(ctx context.Context, scopeId string
|
||||
|
||||
if err != nil {
|
||||
if errors.IsUniqueError(err) {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in credential store: %s: name %s already exists", l.StoreId, l.Name)))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in credential store: %s: name %s already exists", l.StoreId, l.Name)))
|
||||
}
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in credential store: %s", l.StoreId)))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in credential store: %s", l.StoreId)))
|
||||
}
|
||||
return newCredentialLibrary, nil
|
||||
}
|
||||
@@ -97,19 +97,19 @@ func (r *Repository) CreateCredentialLibrary(ctx context.Context, scopeId string
|
||||
func (r *Repository) UpdateCredentialLibrary(ctx context.Context, scopeId string, l *CredentialLibrary, version uint32, fieldMaskPaths []string, _ ...Option) (*CredentialLibrary, int, error) {
|
||||
const op = "vault.(Repository).UpdateCredentialLibrary"
|
||||
if l == nil {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "missing CredentialLibrary")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "missing CredentialLibrary")
|
||||
}
|
||||
if l.CredentialLibrary == nil {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "missing embedded CredentialLibrary")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "missing embedded CredentialLibrary")
|
||||
}
|
||||
if l.PublicId == "" {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidPublicId, op, "missing public id")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidPublicId, op, "missing public id")
|
||||
}
|
||||
if version == 0 {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "missing version")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "missing version")
|
||||
}
|
||||
if scopeId == "" {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "missing scope id")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "missing scope id")
|
||||
}
|
||||
l = l.clone()
|
||||
|
||||
@@ -121,7 +121,7 @@ func (r *Repository) UpdateCredentialLibrary(ctx context.Context, scopeId string
|
||||
case strings.EqualFold(httpMethodField, f):
|
||||
case strings.EqualFold(httpRequestBodyField, f):
|
||||
default:
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidFieldMask, op, f)
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidFieldMask, op, f)
|
||||
}
|
||||
}
|
||||
var dbMask, nullFields []string
|
||||
@@ -144,12 +144,12 @@ func (r *Repository) UpdateCredentialLibrary(ctx context.Context, scopeId string
|
||||
}
|
||||
|
||||
if len(dbMask) == 0 && len(nullFields) == 0 {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.EmptyFieldMask, op, "missing field mask")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.EmptyFieldMask, op, "missing field mask")
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithCode(errors.Encrypt),
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt),
|
||||
errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
@@ -163,7 +163,7 @@ func (r *Repository) UpdateCredentialLibrary(ctx context.Context, scopeId string
|
||||
db.WithOplog(oplogWrapper, l.oplog(oplog.OpType_OP_TYPE_UPDATE)),
|
||||
db.WithVersion(&version))
|
||||
if err == nil && rowsUpdated > 1 {
|
||||
return errors.New(errors.MultipleRecords, op, "more than 1 resource would have been updated")
|
||||
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 resource would have been updated")
|
||||
}
|
||||
return err
|
||||
},
|
||||
@@ -171,10 +171,10 @@ func (r *Repository) UpdateCredentialLibrary(ctx context.Context, scopeId string
|
||||
|
||||
if err != nil {
|
||||
if errors.IsUniqueError(err) {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.NotUnique, op,
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.NotUnique, op,
|
||||
fmt.Sprintf("name %s already exists: %s", l.Name, l.PublicId))
|
||||
}
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(l.PublicId))
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(l.PublicId))
|
||||
}
|
||||
|
||||
return returnedCredentialLibrary, rowsUpdated, nil
|
||||
@@ -185,7 +185,7 @@ func (r *Repository) UpdateCredentialLibrary(ctx context.Context, scopeId string
|
||||
func (r *Repository) LookupCredentialLibrary(ctx context.Context, publicId string, _ ...Option) (*CredentialLibrary, error) {
|
||||
const op = "vault.(Repository).LookupCredentialLibrary"
|
||||
if publicId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
l := allocCredentialLibrary()
|
||||
l.PublicId = publicId
|
||||
@@ -193,7 +193,7 @@ func (r *Repository) LookupCredentialLibrary(ctx context.Context, publicId strin
|
||||
if errors.IsNotFoundError(err) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("failed for: %s", publicId)))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for: %s", publicId)))
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
@@ -203,10 +203,10 @@ func (r *Repository) LookupCredentialLibrary(ctx context.Context, publicId strin
|
||||
func (r *Repository) DeleteCredentialLibrary(ctx context.Context, scopeId string, publicId string, _ ...Option) (int, error) {
|
||||
const op = "vault.(Repository).DeleteCredentialLibrary"
|
||||
if publicId == "" {
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
if scopeId == "" {
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
|
||||
l := allocCredentialLibrary()
|
||||
@@ -214,7 +214,7 @@ func (r *Repository) DeleteCredentialLibrary(ctx context.Context, scopeId string
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
var rowsDeleted int
|
||||
@@ -224,14 +224,14 @@ func (r *Repository) DeleteCredentialLibrary(ctx context.Context, scopeId string
|
||||
dl := l.clone()
|
||||
rowsDeleted, err = w.Delete(ctx, dl, db.WithOplog(oplogWrapper, l.oplog(oplog.OpType_OP_TYPE_DELETE)))
|
||||
if err == nil && rowsDeleted > 1 {
|
||||
return errors.New(errors.MultipleRecords, op, "more than 1 CredentialLibrary would have been deleted")
|
||||
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 CredentialLibrary would have been deleted")
|
||||
}
|
||||
return err
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("delete failed for %s", l.PublicId)))
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("delete failed for %s", l.PublicId)))
|
||||
}
|
||||
|
||||
return rowsDeleted, nil
|
||||
@@ -242,7 +242,7 @@ func (r *Repository) DeleteCredentialLibrary(ctx context.Context, scopeId string
|
||||
func (r *Repository) ListCredentialLibraries(ctx context.Context, storeId string, opt ...Option) ([]*CredentialLibrary, error) {
|
||||
const op = "vault.(Repository).ListCredentialLibraries"
|
||||
if storeId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no storeId")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no storeId")
|
||||
}
|
||||
opts := getOpts(opt...)
|
||||
limit := r.defaultLimit
|
||||
@@ -253,7 +253,7 @@ func (r *Repository) ListCredentialLibraries(ctx context.Context, storeId string
|
||||
var libs []*CredentialLibrary
|
||||
err := r.reader.SearchWhere(ctx, &libs, "store_id = ?", []interface{}{storeId}, db.WithLimit(limit))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return libs, nil
|
||||
}
|
||||
@@ -41,32 +41,32 @@ import (
|
||||
func (r *Repository) CreateCredentialStore(ctx context.Context, cs *CredentialStore, _ ...Option) (*CredentialStore, error) {
|
||||
const op = "vault.(Repository).CreateCredentialStore"
|
||||
if cs == nil {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "nil CredentialStore")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil CredentialStore")
|
||||
}
|
||||
if cs.CredentialStore == nil {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "nil embedded CredentialStore")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "nil embedded CredentialStore")
|
||||
}
|
||||
if cs.ScopeId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
if len(cs.inputToken) == 0 {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no vault token")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no vault token")
|
||||
}
|
||||
if cs.VaultAddress == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no vault address")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no vault address")
|
||||
}
|
||||
if cs.PublicId != "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "public id not empty")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "public id not empty")
|
||||
}
|
||||
if cs.clientCert != nil && len(cs.clientCert.CertificateKey) == 0 {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "client certificate without private key")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "client certificate without private key")
|
||||
}
|
||||
|
||||
cs = cs.clone()
|
||||
|
||||
id, err := newCredentialStoreId()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
cs.PublicId = id
|
||||
if cs.clientCert != nil {
|
||||
@@ -75,11 +75,11 @@ func (r *Repository) CreateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
|
||||
client, err := cs.client()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to create vault client"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to create vault client"))
|
||||
}
|
||||
tokenLookup, err := client.lookupToken()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to lookup vault token"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to lookup vault token"))
|
||||
}
|
||||
if err := validateTokenLookup(op, tokenLookup); err != nil {
|
||||
return nil, err
|
||||
@@ -87,27 +87,27 @@ func (r *Repository) CreateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
|
||||
available, err := client.capabilities(requiredCapabilities.paths())
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get vault capabilities"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get vault capabilities"))
|
||||
}
|
||||
missing := available.missing(requiredCapabilities)
|
||||
if len(missing) > 0 {
|
||||
return nil,
|
||||
errors.New(errors.VaultTokenMissingCapabilities, op, fmt.Sprintf("missing capabilites: %v", missing))
|
||||
errors.New(ctx, errors.VaultTokenMissingCapabilities, op, fmt.Sprintf("missing capabilites: %v", missing))
|
||||
}
|
||||
|
||||
renewedToken, err := client.renewToken()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to renew vault token"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to renew vault token"))
|
||||
}
|
||||
|
||||
tokenExpires, err := renewedToken.TokenTTL()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get vault token expiration"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get vault token expiration"))
|
||||
}
|
||||
|
||||
accessor, err := renewedToken.TokenAccessor()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get vault token accessor"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get vault token accessor"))
|
||||
}
|
||||
|
||||
token, err := newToken(id, cs.inputToken, []byte(accessor), tokenExpires)
|
||||
@@ -117,20 +117,20 @@ func (r *Repository) CreateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, cs.ScopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
databaseWrapper, err := r.kms.GetWrapper(ctx, cs.ScopeId, kms.KeyPurposeDatabase)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
}
|
||||
|
||||
// encrypt
|
||||
if err := token.encrypt(ctx, databaseWrapper); err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if cs.clientCert != nil {
|
||||
if err := cs.clientCert.encrypt(ctx, databaseWrapper); err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,14 +142,14 @@ func (r *Repository) CreateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
msgs := make([]*oplog.Message, 0, 3)
|
||||
ticket, err := w.GetTicket(cs)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to get ticket"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get ticket"))
|
||||
}
|
||||
|
||||
// insert credential store
|
||||
newCredentialStore = cs.clone()
|
||||
var csOplogMsg oplog.Message
|
||||
if err := w.Create(ctx, newCredentialStore, db.NewOplogMsg(&csOplogMsg)); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
msgs = append(msgs, &csOplogMsg)
|
||||
|
||||
@@ -158,10 +158,10 @@ func (r *Repository) CreateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
query, values := newToken.insertQuery()
|
||||
rows, err := w.Exec(ctx, query, values)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if rows > 1 {
|
||||
return errors.New(errors.MultipleRecords, op, "more than 1 token would have been created")
|
||||
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 token would have been created")
|
||||
}
|
||||
msgs = append(msgs, newToken.oplogMessage(db.CreateOp))
|
||||
|
||||
@@ -175,7 +175,7 @@ func (r *Repository) CreateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
newClientCertificate = cs.clientCert.clone()
|
||||
var clientCertOplogMsg oplog.Message
|
||||
if err := w.Create(ctx, newClientCertificate, db.NewOplogMsg(&clientCertOplogMsg)); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
msgs = append(msgs, &clientCertOplogMsg)
|
||||
|
||||
@@ -186,7 +186,7 @@ func (r *Repository) CreateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
}
|
||||
metadata := cs.oplog(oplog.OpType_OP_TYPE_CREATE)
|
||||
if err := w.WriteOplogEntryWith(ctx, oplogWrapper, ticket, metadata, msgs); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to write oplog"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to write oplog"))
|
||||
}
|
||||
return nil
|
||||
},
|
||||
@@ -194,9 +194,9 @@ func (r *Repository) CreateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
|
||||
if err != nil {
|
||||
if errors.IsUniqueError(err) {
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in scope: %s: name %s already exists", cs.ScopeId, cs.Name)))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in scope: %s: name %s already exists", cs.ScopeId, cs.Name)))
|
||||
}
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("in scope: %s", cs.ScopeId)))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("in scope: %s", cs.ScopeId)))
|
||||
}
|
||||
|
||||
// Best effort update next run time of token renewal job, but an error should not
|
||||
@@ -209,33 +209,33 @@ func (r *Repository) CreateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
|
||||
func validateTokenLookup(op errors.Op, s *vault.Secret) error {
|
||||
if s.Data == nil {
|
||||
return errors.New(errors.InvalidParameter, op, "vault secret is not a token lookup")
|
||||
return errors.NewDeprecated(errors.InvalidParameter, op, "vault secret is not a token lookup")
|
||||
}
|
||||
|
||||
if s.Data["renewable"] == nil {
|
||||
return errors.E(errors.WithCode(errors.VaultTokenNotRenewable), errors.WithOp(op))
|
||||
return errors.EDeprecated(errors.WithCode(errors.VaultTokenNotRenewable), errors.WithOp(op))
|
||||
}
|
||||
renewable, err := parseutil.ParseBool(s.Data["renewable"])
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.WrapDeprecated(err, op)
|
||||
}
|
||||
if !renewable {
|
||||
return errors.E(errors.WithCode(errors.VaultTokenNotRenewable), errors.WithOp(op))
|
||||
return errors.EDeprecated(errors.WithCode(errors.VaultTokenNotRenewable), errors.WithOp(op))
|
||||
}
|
||||
|
||||
if s.Data["orphan"] == nil {
|
||||
return errors.E(errors.WithCode(errors.VaultTokenNotOrphan), errors.WithOp(op))
|
||||
return errors.EDeprecated(errors.WithCode(errors.VaultTokenNotOrphan), errors.WithOp(op))
|
||||
}
|
||||
orphan, err := parseutil.ParseBool(s.Data["orphan"])
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.WrapDeprecated(err, op)
|
||||
}
|
||||
if !orphan {
|
||||
return errors.E(errors.WithCode(errors.VaultTokenNotOrphan), errors.WithOp(op))
|
||||
return errors.EDeprecated(errors.WithCode(errors.VaultTokenNotOrphan), errors.WithOp(op))
|
||||
}
|
||||
|
||||
if s.Data["period"] == nil {
|
||||
return errors.E(errors.WithCode(errors.VaultTokenNotPeriodic), errors.WithOp(op))
|
||||
return errors.EDeprecated(errors.WithCode(errors.VaultTokenNotPeriodic), errors.WithOp(op))
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -246,7 +246,7 @@ func validateTokenLookup(op errors.Op, s *vault.Secret) error {
|
||||
func (r *Repository) LookupCredentialStore(ctx context.Context, publicId string, _ ...Option) (*CredentialStore, error) {
|
||||
const op = "vault.(Repository).LookupCredentialStore"
|
||||
if publicId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
agg := allocPublicStore()
|
||||
agg.PublicId = publicId
|
||||
@@ -254,7 +254,7 @@ func (r *Repository) LookupCredentialStore(ctx context.Context, publicId string,
|
||||
if errors.IsNotFoundError(err) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("failed for: %s", publicId)))
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for: %s", publicId)))
|
||||
}
|
||||
return agg.toCredentialStore(), nil
|
||||
}
|
||||
@@ -343,19 +343,19 @@ func (ps *publicStore) GetPublicId() string { return ps.PublicId }
|
||||
func (r *Repository) UpdateCredentialStore(ctx context.Context, cs *CredentialStore, version uint32, fieldMaskPaths []string, _ ...Option) (*CredentialStore, int, error) {
|
||||
const op = "vault.(Repository).UpdateCredentialStore"
|
||||
if cs == nil {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "missing CredentialStore")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "missing CredentialStore")
|
||||
}
|
||||
if cs.CredentialStore == nil {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "missing embedded CredentialStore")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "missing embedded CredentialStore")
|
||||
}
|
||||
if cs.PublicId == "" {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidPublicId, op, "missing public id")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidPublicId, op, "missing public id")
|
||||
}
|
||||
if version == 0 {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "missing version")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "missing version")
|
||||
}
|
||||
if cs.ScopeId == "" {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "missing scope id")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "missing scope id")
|
||||
}
|
||||
cs = cs.clone()
|
||||
|
||||
@@ -378,7 +378,7 @@ func (r *Repository) UpdateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
validateToken = true
|
||||
}
|
||||
default:
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidFieldMask, op, f)
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidFieldMask, op, f)
|
||||
}
|
||||
}
|
||||
dbMask, nullFields := dbcommon.BuildUpdatePaths(
|
||||
@@ -410,10 +410,10 @@ func (r *Repository) UpdateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
fieldMaskPaths, nil,
|
||||
)
|
||||
if len(certNullFields) != 0 && len(certNullFields) != 2 {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "attempting to unset a required field on a client cert")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "attempting to unset a required field on a client cert")
|
||||
}
|
||||
if len(append(dbMask, certDbMask...)) == 0 && len(append(nullFields, certNullFields...)) == 0 {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.EmptyFieldMask, op, "missing field mask")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.EmptyFieldMask, op, "missing field mask")
|
||||
}
|
||||
|
||||
var filteredDbMask, filteredNullFields []string
|
||||
@@ -427,7 +427,7 @@ func (r *Repository) UpdateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
for _, f := range nullFields {
|
||||
switch {
|
||||
case strings.EqualFold(tokenField, f):
|
||||
return nil, db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "attempting to unset the value for token")
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "attempting to unset the value for token")
|
||||
default:
|
||||
filteredNullFields = append(filteredNullFields, f)
|
||||
}
|
||||
@@ -436,20 +436,20 @@ func (r *Repository) UpdateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, cs.ScopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected,
|
||||
errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
databaseWrapper, err := r.kms.GetWrapper(ctx, cs.ScopeId, kms.KeyPurposeDatabase)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected,
|
||||
errors.Wrap(err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
}
|
||||
|
||||
ps, err := r.lookupPrivateStore(ctx, cs.GetPublicId())
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to lookup private credential store"))
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to lookup private credential store"))
|
||||
}
|
||||
if ps == nil {
|
||||
return nil, db.NoRowsAffected, errors.New(errors.RecordNotFound, op, fmt.Sprintf("credential store %s", cs.PublicId))
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.RecordNotFound, op, fmt.Sprintf("credential store %s", cs.PublicId))
|
||||
}
|
||||
origStore := ps.toCredentialStore()
|
||||
origStore.inputToken = ps.Token
|
||||
@@ -457,60 +457,60 @@ func (r *Repository) UpdateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
origStore.clientCert, err = NewClientCertificate(ps.ClientCert, ps.ClientKey)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("can't recreate client certificate for vault client creation"))
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("can't recreate client certificate for vault client creation"))
|
||||
}
|
||||
updatedStore := origStore.applyUpdate(cs, fieldMaskPaths)
|
||||
|
||||
if len(certDbMask) > 0 && updatedStore.clientCert != nil {
|
||||
if err := updatedStore.clientCert.encrypt(ctx, databaseWrapper); err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op)
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
}
|
||||
|
||||
var token *Token
|
||||
client, err := updatedStore.client()
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to get client for updated store"))
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get client for updated store"))
|
||||
}
|
||||
if validateToken {
|
||||
tokenLookup, err := client.lookupToken()
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("cannot lookup token for updated store"))
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("cannot lookup token for updated store"))
|
||||
}
|
||||
if err := validateTokenLookup(op, tokenLookup); err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op)
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
available, err := client.capabilities(requiredCapabilities.paths())
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to get vault capabilities"))
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get vault capabilities"))
|
||||
}
|
||||
missing := available.missing(requiredCapabilities)
|
||||
if len(missing) > 0 {
|
||||
return nil,
|
||||
db.NoRowsAffected,
|
||||
errors.New(errors.VaultTokenMissingCapabilities, op, fmt.Sprintf("missing capabilites: %v", missing))
|
||||
errors.New(ctx, errors.VaultTokenMissingCapabilities, op, fmt.Sprintf("missing capabilites: %v", missing))
|
||||
}
|
||||
}
|
||||
if updateToken {
|
||||
renewedToken, err := client.renewToken()
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to renew vault token"))
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to renew vault token"))
|
||||
}
|
||||
tokenExpires, err := renewedToken.TokenTTL()
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to get vault token expiration"))
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get vault token expiration"))
|
||||
}
|
||||
accessor, err := renewedToken.TokenAccessor()
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to get vault token accessor"))
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get vault token accessor"))
|
||||
}
|
||||
if token, err = newToken(cs.GetPublicId(), cs.inputToken, []byte(accessor), tokenExpires); err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op)
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
// encrypt token
|
||||
if err := token.encrypt(ctx, databaseWrapper); err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(err, op)
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -521,7 +521,7 @@ func (r *Repository) UpdateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
msgs := make([]*oplog.Message, 0, 3)
|
||||
ticket, err := w.GetTicket(cs)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to get ticket"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get ticket"))
|
||||
}
|
||||
|
||||
cs := cs.clone()
|
||||
@@ -535,30 +535,30 @@ func (r *Repository) UpdateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
cs.Version = version + 1
|
||||
rowsUpdated, err = w.Update(ctx, cs, []string{"Version"}, nil, db.NewOplogMsg(&csOplogMsg), db.WithVersion(&version))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to update credential store version"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to update credential store version"))
|
||||
}
|
||||
switch rowsUpdated {
|
||||
case 1:
|
||||
case 0:
|
||||
return nil
|
||||
default:
|
||||
return errors.New(errors.MultipleRecords, op, fmt.Sprintf("updated credential store version and %d rows updated", rowsUpdated))
|
||||
return errors.New(ctx, errors.MultipleRecords, op, fmt.Sprintf("updated credential store version and %d rows updated", rowsUpdated))
|
||||
}
|
||||
default:
|
||||
rowsUpdated, err = w.Update(ctx, cs, filteredDbMask, filteredNullFields, db.NewOplogMsg(&csOplogMsg), db.WithVersion(&version))
|
||||
if err != nil {
|
||||
if errors.IsUniqueError(err) {
|
||||
return errors.New(errors.NotUnique, op,
|
||||
return errors.New(ctx, errors.NotUnique, op,
|
||||
fmt.Sprintf("name %s already exists: %s", cs.Name, cs.PublicId))
|
||||
}
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to update credential store"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to update credential store"))
|
||||
}
|
||||
switch rowsUpdated {
|
||||
case 1:
|
||||
case 0:
|
||||
return nil
|
||||
default:
|
||||
return errors.New(errors.MultipleRecords, op, fmt.Sprintf("updated credential store and %d rows updated", rowsUpdated))
|
||||
return errors.New(ctx, errors.MultipleRecords, op, fmt.Sprintf("updated credential store and %d rows updated", rowsUpdated))
|
||||
}
|
||||
}
|
||||
msgs = append(msgs, &csOplogMsg)
|
||||
@@ -571,23 +571,23 @@ func (r *Repository) UpdateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
query, values := deleteCert.deleteQuery()
|
||||
rows, err := w.Exec(ctx, query, values)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to delete client certificate"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to delete client certificate"))
|
||||
}
|
||||
if rows > 1 {
|
||||
return errors.New(errors.MultipleRecords, op, "more than 1 client certificate would have been deleted")
|
||||
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 client certificate would have been deleted")
|
||||
}
|
||||
msgs = append(msgs, deleteCert.oplogMessage(db.DeleteOp))
|
||||
case len(certDbMask) > 0:
|
||||
if updatedStore.clientCert == nil {
|
||||
return errors.New(errors.InvalidParameter, op, "updated cert")
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "updated cert")
|
||||
}
|
||||
query, values := updatedStore.clientCert.insertQuery()
|
||||
rows, err := w.Exec(ctx, query, values)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to upsert client certificate"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to upsert client certificate"))
|
||||
}
|
||||
if rows > 1 {
|
||||
return errors.New(errors.MultipleRecords, op, "more than 1 client certificate would have been upserted")
|
||||
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 client certificate would have been upserted")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -596,13 +596,13 @@ func (r *Repository) UpdateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
rows, err := w.Exec(ctx, query, values)
|
||||
if err != nil {
|
||||
if errors.IsUniqueError(err) {
|
||||
return errors.New(errors.NotUnique, op,
|
||||
return errors.New(ctx, errors.NotUnique, op,
|
||||
fmt.Sprintf("token already exists"))
|
||||
}
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if rows > 1 {
|
||||
return errors.New(errors.MultipleRecords, op, "more than 1 token would have been created")
|
||||
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 token would have been created")
|
||||
}
|
||||
msgs = append(msgs, token.oplogMessage(db.CreateOp))
|
||||
}
|
||||
@@ -611,13 +611,13 @@ func (r *Repository) UpdateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
agg := allocPublicStore()
|
||||
agg.PublicId = publicId
|
||||
if err := reader.LookupByPublicId(ctx, agg); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("unable to lookup credential store: %s", publicId)))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("unable to lookup credential store: %s", publicId)))
|
||||
}
|
||||
returnedCredentialStore = agg.toCredentialStore()
|
||||
|
||||
metadata := cs.oplog(oplog.OpType_OP_TYPE_UPDATE)
|
||||
if err := w.WriteOplogEntryWith(ctx, oplogWrapper, ticket, metadata, msgs); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to write oplog"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to write oplog"))
|
||||
}
|
||||
return nil
|
||||
},
|
||||
@@ -642,7 +642,7 @@ func (r *Repository) UpdateCredentialStore(ctx context.Context, cs *CredentialSt
|
||||
func (r *Repository) ListCredentialStores(ctx context.Context, scopeIds []string, opt ...Option) ([]*CredentialStore, error) {
|
||||
const op = "vault.(Repository).ListCredentialStores"
|
||||
if len(scopeIds) == 0 {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no scopeIds")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no scopeIds")
|
||||
}
|
||||
opts := getOpts(opt...)
|
||||
limit := r.defaultLimit
|
||||
@@ -653,7 +653,7 @@ func (r *Repository) ListCredentialStores(ctx context.Context, scopeIds []string
|
||||
var credentialStores []*publicStore
|
||||
err := r.reader.SearchWhere(ctx, &credentialStores, "scope_id in (?)", []interface{}{scopeIds}, db.WithLimit(limit))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
var out []*CredentialStore
|
||||
for _, ca := range credentialStores {
|
||||
@@ -667,7 +667,7 @@ func (r *Repository) ListCredentialStores(ctx context.Context, scopeIds []string
|
||||
func (r *Repository) DeleteCredentialStore(ctx context.Context, publicId string, _ ...Option) (int, error) {
|
||||
const op = "vault.(Repository).DeleteCredentialStore"
|
||||
if publicId == "" {
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no public id")
|
||||
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no public id")
|
||||
}
|
||||
|
||||
cs := allocCredentialStore()
|
||||
@@ -676,15 +676,15 @@ func (r *Repository) DeleteCredentialStore(ctx context.Context, publicId string,
|
||||
if errors.IsNotFoundError(err) {
|
||||
return db.NoRowsAffected, nil
|
||||
}
|
||||
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("failed for %s", publicId)))
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for %s", publicId)))
|
||||
}
|
||||
if cs.ScopeId == "" {
|
||||
return db.NoRowsAffected, errors.New(errors.InvalidParameter, op, "no scope id")
|
||||
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "no scope id")
|
||||
}
|
||||
|
||||
oplogWrapper, err := r.kms.GetWrapper(ctx, cs.ScopeId, kms.KeyPurposeOplog)
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get oplog wrapper"))
|
||||
}
|
||||
|
||||
var rows int
|
||||
@@ -695,15 +695,15 @@ func (r *Repository) DeleteCredentialStore(ctx context.Context, publicId string,
|
||||
var msgs []*oplog.Message
|
||||
ticket, err := w.GetTicket(cs)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to get ticket"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get ticket"))
|
||||
}
|
||||
|
||||
rows, err = w.Exec(ctx, query, values)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if rows > 1 {
|
||||
return errors.New(errors.MultipleRecords, op, "more than 1 credential store would have been deleted")
|
||||
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 credential store would have been deleted")
|
||||
}
|
||||
msg := cs.oplogMessage(db.UpdateOp)
|
||||
msg.FieldMaskPaths = []string{"DeleteTime"}
|
||||
@@ -711,7 +711,7 @@ func (r *Repository) DeleteCredentialStore(ctx context.Context, publicId string,
|
||||
|
||||
metadata := cs.oplog(oplog.OpType_OP_TYPE_UPDATE)
|
||||
if err := w.WriteOplogEntryWith(ctx, oplogWrapper, ticket, metadata, msgs); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithMsg("unable to write oplog"))
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to write oplog"))
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -719,7 +719,7 @@ func (r *Repository) DeleteCredentialStore(ctx context.Context, publicId string,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, errors.Wrap(err, op, errors.WithMsg(fmt.Sprintf("delete failed for %s", cs.PublicId)))
|
||||
return db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("delete failed for %s", cs.PublicId)))
|
||||
}
|
||||
|
||||
if rows > 0 {
|
||||
|
||||
@@ -18,15 +18,15 @@ var _ credential.Issuer = (*Repository)(nil)
|
||||
func (r *Repository) Issue(ctx context.Context, sessionId string, requests []credential.Request) ([]credential.Dynamic, error) {
|
||||
const op = "vault.(Repository).Issue"
|
||||
if sessionId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no session id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no session id")
|
||||
}
|
||||
if len(requests) == 0 {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no requests")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no requests")
|
||||
}
|
||||
|
||||
libs, err := r.getPrivateLibraries(ctx, requests)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
// TODO(mgaffney)(ICU-1329) 05/2021: if any error occurs, mark all credentials
|
||||
@@ -40,12 +40,12 @@ func (r *Repository) Issue(ctx context.Context, sessionId string, requests []cre
|
||||
// if there is no way to save it in the database.
|
||||
credId, err := newCredentialId()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
client, err := lib.client()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
var secret *vault.Secret
|
||||
@@ -55,13 +55,13 @@ func (r *Repository) Issue(ctx context.Context, sessionId string, requests []cre
|
||||
case MethodPost:
|
||||
secret, err = client.post(lib.VaultPath, lib.HttpRequestBody)
|
||||
default:
|
||||
return nil, errors.New(errors.Internal, op, fmt.Sprintf("unknown http method: library: %s", lib.PublicId))
|
||||
return nil, errors.New(ctx, errors.Internal, op, fmt.Sprintf("unknown http method: library: %s", lib.PublicId))
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
// TODO(mgaffney) 05/2021: detect if the error is because of an
|
||||
// expired or invalid token
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
leaseDuration := time.Duration(secret.LeaseDuration) * time.Second
|
||||
@@ -70,7 +70,7 @@ func (r *Repository) Issue(ctx context.Context, sessionId string, requests []cre
|
||||
}
|
||||
cred, err := newCredential(lib.GetPublicId(), sessionId, secret.LeaseID, lib.TokenHmac, leaseDuration)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
cred.PublicId = credId
|
||||
cred.IsRenewable = secret.Renewable
|
||||
@@ -82,24 +82,24 @@ func (r *Repository) Issue(ctx context.Context, sessionId string, requests []cre
|
||||
rowsInserted, err := w.Exec(ctx, insertQuery, insertQueryValues)
|
||||
switch {
|
||||
case err != nil:
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
case rowsInserted > 1:
|
||||
return errors.New(errors.MultipleRecords, op, "more than 1 credential would have been inserted")
|
||||
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 credential would have been inserted")
|
||||
}
|
||||
|
||||
rowsUpdated, err := w.Exec(ctx, updateQuery, updateQueryValues)
|
||||
switch {
|
||||
case err != nil:
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
case rowsUpdated == 0:
|
||||
return errors.New(errors.InvalidDynamicCredential, op, "no matching dynamic credential for session found")
|
||||
return errors.New(ctx, errors.InvalidDynamicCredential, op, "no matching dynamic credential for session found")
|
||||
case rowsUpdated > 1:
|
||||
return errors.New(errors.MultipleRecords, op, "more than 1 session credential would have been updated")
|
||||
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 session credential would have been updated")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
); err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
creds = append(creds, &actualCredential{
|
||||
@@ -125,13 +125,13 @@ var _ credential.Revoker = (*Repository)(nil)
|
||||
func (r *Repository) Revoke(ctx context.Context, sessionId string) error {
|
||||
const op = "vault.(Repository).Revoke"
|
||||
if sessionId == "" {
|
||||
return errors.New(errors.InvalidParameter, op, "no session id")
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "no session id")
|
||||
}
|
||||
|
||||
_, err := r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
|
||||
func(_ db.Reader, w db.Writer) error {
|
||||
if _, err := w.Exec(ctx, revokeCredentialsQuery, []interface{}{sessionId}); err != nil {
|
||||
return errors.Wrap(err, op)
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
|
||||
@@ -47,7 +47,7 @@ type client struct {
|
||||
func newClient(c *clientConfig) (*client, error) {
|
||||
const op = "vault.newClient"
|
||||
if !c.isValid() {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "invalid configuration")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "invalid configuration")
|
||||
}
|
||||
vc := vault.DefaultConfig()
|
||||
vc.Address = c.Addr
|
||||
@@ -58,14 +58,14 @@ func newClient(c *clientConfig) (*client, error) {
|
||||
tlsConfig := vc.HttpClient.Transport.(*http.Transport).TLSClientConfig
|
||||
tlsConfig.InsecureSkipVerify = c.TlsSkipVerify
|
||||
if err := rootcerts.ConfigureTLS(tlsConfig, rootConfig); err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.WrapDeprecated(err, op)
|
||||
}
|
||||
}
|
||||
|
||||
if c.isClientTLS() {
|
||||
clientCert, err := tls.X509KeyPair(c.ClientCert, c.ClientKey)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.WrapDeprecated(err, op)
|
||||
}
|
||||
tlsConfig := vc.HttpClient.Transport.(*http.Transport).TLSClientConfig
|
||||
tlsConfig.GetClientCertificate = func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
|
||||
@@ -75,7 +75,7 @@ func newClient(c *clientConfig) (*client, error) {
|
||||
|
||||
vClient, err := vault.NewClient(vc)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
return nil, errors.WrapDeprecated(err, op)
|
||||
}
|
||||
vClient.SetToken(string(c.Token))
|
||||
|
||||
@@ -94,11 +94,11 @@ func (c *client) ping() error {
|
||||
h, err := c.cl.Sys().Health()
|
||||
switch {
|
||||
case err != nil:
|
||||
return errors.Wrap(err, op, errors.WithCode(errors.Unknown), errors.WithMsg(fmt.Sprintf("vault: %s", c.cl.Address())))
|
||||
return errors.WrapDeprecated(err, op, errors.WithCode(errors.Unknown), errors.WithMsg(fmt.Sprintf("vault: %s", c.cl.Address())))
|
||||
case h == nil:
|
||||
return errors.New(errors.Unavailable, op, fmt.Sprintf("no repsonse: vault: %s", c.cl.Address()))
|
||||
return errors.NewDeprecated(errors.Unavailable, op, fmt.Sprintf("no repsonse: vault: %s", c.cl.Address()))
|
||||
case !h.Initialized || h.Sealed:
|
||||
return errors.New(errors.Unavailable, op, fmt.Sprintf("vault (%s): initialized: %t, sealed: %t ", c.cl.Address(), h.Initialized, h.Sealed))
|
||||
return errors.NewDeprecated(errors.Unavailable, op, fmt.Sprintf("vault (%s): initialized: %t, sealed: %t ", c.cl.Address(), h.Initialized, h.Sealed))
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -112,7 +112,7 @@ func (c *client) renewToken() (*vault.Secret, error) {
|
||||
const op = "vault.(client).renewToken"
|
||||
t, err := c.cl.Auth().Token().RenewSelf(0)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithCode(errors.Unknown), errors.WithMsg(fmt.Sprintf("vault: %s", c.cl.Address())))
|
||||
return nil, errors.WrapDeprecated(err, op, errors.WithCode(errors.Unknown), errors.WithMsg(fmt.Sprintf("vault: %s", c.cl.Address())))
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
@@ -124,7 +124,7 @@ func (c *client) revokeToken() error {
|
||||
const op = "vault.(client).revokeToken"
|
||||
// The `token` parameter is kept for backwards compatibility but is ignored, so use ""
|
||||
if err := c.cl.Auth().Token().RevokeSelf(""); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithCode(errors.Unknown), errors.WithMsg(fmt.Sprintf("vault: %s", c.cl.Address())))
|
||||
return errors.WrapDeprecated(err, op, errors.WithCode(errors.Unknown), errors.WithMsg(fmt.Sprintf("vault: %s", c.cl.Address())))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -137,7 +137,7 @@ func (c *client) renewLease(leaseId string, leaseDuration time.Duration) (*vault
|
||||
const op = "vault.(client).renewLease"
|
||||
t, err := c.cl.Sys().Renew(leaseId, int(leaseDuration.Round(time.Second).Seconds()))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithCode(errors.VaultCredentialRequest), errors.WithMsg(fmt.Sprintf("vault: %s", c.cl.Address())))
|
||||
return nil, errors.WrapDeprecated(err, op, errors.WithCode(errors.VaultCredentialRequest), errors.WithMsg(fmt.Sprintf("vault: %s", c.cl.Address())))
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
@@ -148,7 +148,7 @@ func (c *client) renewLease(leaseId string, leaseDuration time.Duration) (*vault
|
||||
func (c *client) revokeLease(leaseId string) error {
|
||||
const op = "vault.(client).revokeLease"
|
||||
if err := c.cl.Sys().Revoke(leaseId); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithCode(errors.Unknown), errors.WithMsg(fmt.Sprintf("vault: %s", c.cl.Address())))
|
||||
return errors.WrapDeprecated(err, op, errors.WithCode(errors.Unknown), errors.WithMsg(fmt.Sprintf("vault: %s", c.cl.Address())))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -161,7 +161,7 @@ func (c *client) lookupToken() (*vault.Secret, error) {
|
||||
const op = "vault.(client).lookupToken"
|
||||
t, err := c.cl.Auth().Token().LookupSelf()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithCode(errors.Unknown), errors.WithMsg(fmt.Sprintf("vault: %s", c.cl.Address())))
|
||||
return nil, errors.WrapDeprecated(err, op, errors.WithCode(errors.Unknown), errors.WithMsg(fmt.Sprintf("vault: %s", c.cl.Address())))
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
@@ -178,7 +178,7 @@ func (c *client) get(path string) (*vault.Secret, error) {
|
||||
const op = "vault.(client).get"
|
||||
s, err := c.cl.Logical().Read(path)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithCode(errors.VaultCredentialRequest), errors.WithMsg(fmt.Sprintf("vault: %s", c.cl.Address())))
|
||||
return nil, errors.WrapDeprecated(err, op, errors.WithCode(errors.VaultCredentialRequest), errors.WithMsg(fmt.Sprintf("vault: %s", c.cl.Address())))
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
@@ -193,7 +193,7 @@ func (c *client) post(path string, data []byte) (*vault.Secret, error) {
|
||||
}
|
||||
s, err := c.cl.Logical().WriteBytes(path, data)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op, errors.WithCode(errors.VaultCredentialRequest), errors.WithMsg(fmt.Sprintf("vault: %s", c.cl.Address())))
|
||||
return nil, errors.WrapDeprecated(err, op, errors.WithCode(errors.VaultCredentialRequest), errors.WithMsg(fmt.Sprintf("vault: %s", c.cl.Address())))
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
@@ -205,7 +205,7 @@ func (c *client) post(path string, data []byte) (*vault.Secret, error) {
|
||||
func (c *client) capabilities(paths []string) (pathCapabilities, error) {
|
||||
const op = "vault.(client).capabilities"
|
||||
if len(paths) == 0 {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "empty paths")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "empty paths")
|
||||
}
|
||||
body := map[string]string{
|
||||
"paths": strings.Join(paths, ","),
|
||||
@@ -230,7 +230,7 @@ func (c *client) capabilities(paths []string) (pathCapabilities, error) {
|
||||
return nil, err
|
||||
}
|
||||
if secret == nil || secret.Data == nil {
|
||||
return nil, errors.New(errors.Unknown, op, "data from Vault is empty")
|
||||
return nil, errors.NewDeprecated(errors.Unknown, op, "data from Vault is empty")
|
||||
}
|
||||
|
||||
var res map[string][]string
|
||||
|
||||
@@ -56,16 +56,16 @@ type Token struct {
|
||||
func newToken(storeId string, token TokenSecret, accessor []byte, expiration time.Duration) (*Token, error) {
|
||||
const op = "vault.newToken"
|
||||
if storeId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no store id")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "no store id")
|
||||
}
|
||||
if len(token) == 0 {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no vault token")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "no vault token")
|
||||
}
|
||||
if len(accessor) == 0 {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no vault token accessor")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "no vault token accessor")
|
||||
}
|
||||
if expiration == 0 {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "no expiration")
|
||||
return nil, errors.NewDeprecated(errors.InvalidParameter, op, "no expiration")
|
||||
}
|
||||
|
||||
tokenCopy := make(TokenSecret, len(token))
|
||||
@@ -120,7 +120,7 @@ func (t *Token) SetTableName(n string) {
|
||||
func (t *Token) encrypt(ctx context.Context, cipher wrapping.Wrapper) error {
|
||||
const op = "vault.(Token).encrypt"
|
||||
if err := structwrapping.WrapStruct(ctx, cipher, t.Token, nil); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithCode(errors.Encrypt))
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt))
|
||||
}
|
||||
t.KeyId = cipher.KeyID()
|
||||
return nil
|
||||
@@ -129,7 +129,7 @@ func (t *Token) encrypt(ctx context.Context, cipher wrapping.Wrapper) error {
|
||||
func (t *Token) decrypt(ctx context.Context, cipher wrapping.Wrapper) error {
|
||||
const op = "vault.(Token).decrypt"
|
||||
if err := structwrapping.UnwrapStruct(ctx, cipher, t.Token, nil); err != nil {
|
||||
return errors.Wrap(err, op, errors.WithCode(errors.Decrypt))
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decrypt))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user