* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUS-1.1 * Rerun make gen This will pick up the last of the license changes * Revert "Rerun make gen" This reverts commit dbe0968bffd92745d8663c6940debae098395a6b. * Remove copywrite from generation script The current version of copywrite is not sophisticated enough to only apply the copywrite changes to certain subdirectories, so to avoid accidentally relicensing our api and sdk modules, we will stop running the copywrite script until such time that it can do the right thing. * License protobuf API as MPL The protobuf API files are used to generate the SDK, since we want the SDK to be MPL, the protobuf files must also be MPL. This extends to all protobuf files whose sources end up in the SDK directory, including: - internal/proto/controller/api - internal/proto/controller/custom_options - internal/proto/plugin It also includes the generated service directory in internal/gen/controller/api/services, since they are generated from the protobuf sources used for the SDK. * Change website license file name This makes the naming scheme consistent with the rest of the repository. * Add note on licensing to README * Set copywrite license to BUSL * Revert "Add note on licensing to README" This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe. --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
164 lines
5.0 KiB
Go
164 lines
5.0 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package vault
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/hashicorp/boundary/internal/credential"
|
|
"github.com/hashicorp/boundary/internal/db"
|
|
"github.com/hashicorp/boundary/internal/errors"
|
|
"github.com/hashicorp/boundary/internal/observability/event"
|
|
)
|
|
|
|
var _ credential.Issuer = (*Repository)(nil)
|
|
|
|
func insertQuery(c *Credential, sessionId string) (query string, queryValues []any) {
|
|
queryValues = []any{
|
|
sql.Named("public_id", c.PublicId),
|
|
sql.Named("library_id", c.LibraryId),
|
|
sql.Named("session_id", sessionId),
|
|
sql.Named("token_hmac", c.TokenHmac),
|
|
sql.Named("external_id", c.ExternalId),
|
|
sql.Named("is_renewable", c.IsRenewable),
|
|
sql.Named("status", c.Status),
|
|
sql.Named("last_renewal_time", "now()"),
|
|
}
|
|
switch {
|
|
case c.expiration == 0:
|
|
query = insertCredentialWithInfiniteExpirationQuery
|
|
default:
|
|
query = insertCredentialWithExpirationQuery
|
|
queryValues = append(queryValues, sql.Named("expiration_time", int(c.expiration.Round(time.Second).Seconds())))
|
|
}
|
|
return
|
|
}
|
|
|
|
func updateSessionQuery(c *Credential, sessionId string, purpose credential.Purpose) (query string, queryValues []any) {
|
|
queryValues = []any{
|
|
sql.Named("public_id", c.PublicId),
|
|
sql.Named("library_id", c.LibraryId),
|
|
sql.Named("session_id", sessionId),
|
|
sql.Named("purpose", string(purpose)),
|
|
}
|
|
query = updateSessionCredentialQuery
|
|
return
|
|
}
|
|
|
|
// Issue issues and returns dynamic credentials from Vault for all of the
|
|
// requests and assigns them to sessionId.
|
|
func (r *Repository) Issue(ctx context.Context, sessionId string, requests []credential.Request, opt ...credential.Option) ([]credential.Dynamic, error) {
|
|
const op = "vault.(Repository).Issue"
|
|
if sessionId == "" {
|
|
return nil, errors.New(ctx, errors.InvalidParameter, op, "no session id")
|
|
}
|
|
if len(requests) == 0 {
|
|
return nil, errors.New(ctx, errors.InvalidParameter, op, "no requests")
|
|
}
|
|
|
|
libs, err := r.getIssueCredLibraries(ctx, requests)
|
|
if err != nil {
|
|
return nil, errors.Wrap(ctx, err, op)
|
|
}
|
|
|
|
// TODO(mgaffney)(ICU-1329) 05/2021: if any error occurs, mark all credentials
|
|
// retrieved for revocation which will be handled by the revocation
|
|
// job.
|
|
|
|
var creds []credential.Dynamic
|
|
var minLease time.Duration
|
|
runJobsInterval := r.scheduler.GetRunJobsInterval()
|
|
for _, lib := range libs {
|
|
cred, err := lib.retrieveCredential(ctx, op, opt...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
creds = append(creds, cred)
|
|
if !cred.isRevokable() {
|
|
// No need to persist since the credential cannot be revoked nor renewed
|
|
continue
|
|
}
|
|
|
|
if cred.getExpiration() < runJobsInterval {
|
|
event.WriteError(ctx, op,
|
|
fmt.Errorf("WARNING: credential will expire before job scheduler can run"),
|
|
event.WithInfo("credential_public_id", cred.GetPublicId()),
|
|
event.WithInfo("credential_library_public_id", lib.GetPublicId()),
|
|
event.WithInfo("runJobsInterval", runJobsInterval.String()),
|
|
)
|
|
}
|
|
|
|
if minLease > cred.getExpiration() {
|
|
minLease = cred.getExpiration()
|
|
}
|
|
|
|
underlyingCred := cred.getCredential()
|
|
|
|
insertQuery, insertQueryValues := insertQuery(underlyingCred, sessionId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
updateQuery, updateQueryValues := updateSessionQuery(underlyingCred, sessionId, cred.Purpose())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if _, err := r.writer.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{},
|
|
func(_ db.Reader, w db.Writer) error {
|
|
rowsInserted, err := w.Exec(ctx, insertQuery, insertQueryValues)
|
|
switch {
|
|
case err != nil:
|
|
return errors.Wrap(ctx, err, op)
|
|
case rowsInserted > 1:
|
|
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(ctx, err, op)
|
|
case rowsUpdated == 0:
|
|
return errors.New(ctx, errors.InvalidDynamicCredential, op, "no matching dynamic credential for session found")
|
|
case rowsUpdated > 1:
|
|
return errors.New(ctx, errors.MultipleRecords, op, "more than 1 session credential would have been updated")
|
|
}
|
|
return nil
|
|
},
|
|
); err != nil {
|
|
return nil, errors.Wrap(ctx, err, op)
|
|
}
|
|
}
|
|
|
|
// Best effort update next run time of credential renewal job, but an error should not
|
|
// cause Issue to fail.
|
|
// TODO (lcr 06/2021): log error once repo has logger
|
|
_ = r.scheduler.UpdateJobNextRunInAtLeast(ctx, credentialRenewalJobName, minLease)
|
|
|
|
return creds, nil
|
|
}
|
|
|
|
var _ credential.Revoker = (*Repository)(nil)
|
|
|
|
// Revoke revokes all dynamic credentials issued from Vault for sessionId.
|
|
func (r *Repository) Revoke(ctx context.Context, sessionId string) error {
|
|
const op = "vault.(Repository).Revoke"
|
|
if sessionId == "" {
|
|
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, []any{sessionId}); err != nil {
|
|
return errors.Wrap(ctx, err, op)
|
|
}
|
|
return nil
|
|
},
|
|
)
|
|
return err
|
|
}
|