Files
threshold/internal/session/repository_credential.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

95 lines
3.2 KiB
Go
Raw Normal View History

2022-07-20 16:24:46 -07:00
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
2023-02-02 17:15:17 -08:00
2021-12-17 11:25:30 -08:00
package session
import (
"context"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/kms"
)
// AddSessionCredentials encrypts the credData and adds the credentials to the repository. The credentials are linked
2022-07-20 16:24:46 -07:00
// to the sessionID provided, and encrypted using the sessProjectId. Session credentials are only valid for pending and
2021-12-17 11:25:30 -08:00
// active sessions, once a session ends, all session credentials are deleted.
// All options are ignored.
2022-07-20 16:24:46 -07:00
func (r *Repository) AddSessionCredentials(ctx context.Context, sessProjectId, sessionId string, credData []Credential, _ ...Option) error {
2021-12-17 11:25:30 -08:00
const op = "session.(Repository).AddSessionCredentials"
if sessionId == "" {
return errors.New(ctx, errors.InvalidParameter, op, "missing session id")
}
2022-07-20 16:24:46 -07:00
if sessProjectId == "" {
return errors.New(ctx, errors.InvalidParameter, op, "missing session project id")
2021-12-17 11:25:30 -08:00
}
if len(credData) == 0 {
return errors.New(ctx, errors.InvalidParameter, op, "missing credentials")
}
2022-07-20 16:24:46 -07:00
databaseWrapper, err := r.kms.GetWrapper(ctx, sessProjectId, kms.KeyPurposeDatabase)
2021-12-17 11:25:30 -08:00
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
}
addCreds := make([]credential, 0, len(credData))
2021-12-17 11:25:30 -08:00
for _, cred := range credData {
if len(cred) == 0 {
return errors.New(ctx, errors.InvalidParameter, op, "missing credential")
}
sessCred := credential{
SessionId: sessionId,
Credential: cred,
}
if err := sessCred.encrypt(ctx, databaseWrapper); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to encrypt credential"))
}
addCreds = append(addCreds, sessCred)
}
_, err = r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
func(_ db.Reader, w db.Writer) error {
if err := w.CreateItems(ctx, addCreds); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to add session credentials"))
}
return nil
},
)
return err
}
// ListSessionCredentials returns all Credential attached to the sessionId.
// All options are ignored.
2022-07-20 16:24:46 -07:00
func (r *Repository) ListSessionCredentials(ctx context.Context, sessProjectId, sessionId string, _ ...Option) ([]Credential, error) {
2021-12-17 11:25:30 -08:00
const op = "session.(Repository).ListSessionCredentials"
if sessionId == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing session id")
}
2022-07-20 16:24:46 -07:00
if sessProjectId == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing session project id")
2021-12-17 11:25:30 -08:00
}
2022-07-20 16:24:46 -07:00
databaseWrapper, err := r.kms.GetWrapper(ctx, sessProjectId, kms.KeyPurposeDatabase)
2021-12-17 11:25:30 -08:00
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
}
var creds []*credential
2022-10-25 18:29:38 -05:00
if err := r.reader.SearchWhere(ctx, &creds, "session_id = ?", []any{sessionId}); err != nil {
2021-12-17 11:25:30 -08:00
return nil, errors.Wrap(ctx, err, op)
}
if len(creds) == 0 {
return nil, nil
}
ret := make([]Credential, 0, len(creds))
for _, c := range creds {
if err := c.decrypt(ctx, databaseWrapper); err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to decrypt credential"))
}
ret = append(ret, c.Credential)
}
return ret, nil
}