2023-05-17 16:12:04 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2023-05-19 18:15:43 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-kms-wrapping/entropy/v2"
|
2023-05-17 16:12:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// See comment in command/format.go
|
|
|
|
const hopeDelim = "♨"
|
|
|
|
|
2023-05-24 16:29:47 +00:00
|
|
|
// Modifying this constant will result in previous activities not being
|
|
|
|
// associated with future activities of this same type. A storage
|
|
|
|
// migration would need to occur to fix this.
|
|
|
|
//
|
|
|
|
// Suggested naming precedence: <plugin name>-<client type>. Since this
|
|
|
|
// comes from the builtin PKI plugin, and we're counting ACME certificates,
|
|
|
|
// this becomes pki-acme.
|
|
|
|
const ACMEActivityType = "pki-acme"
|
|
|
|
|
2023-05-19 18:15:43 +00:00
|
|
|
// acmeBillingImpl is the (single) implementation of the actual client
|
|
|
|
// counting interface. It needs a reference to core (as per discussions
|
|
|
|
// with Mike, in the future the activityLog reference will no longer be
|
|
|
|
// static but may be replaced throughout the lifecycle of a core instance)
|
|
|
|
// and a reference to the mount that is being counted.
|
|
|
|
type acmeBillingImpl struct {
|
|
|
|
core *Core
|
|
|
|
entry *MountEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ logical.ACMEBillingSystemView = (*acmeBillingImpl)(nil)
|
|
|
|
|
|
|
|
// Due to unfortunate layering of system view interfaces, there are three
|
|
|
|
// possible sets of interfaces we need to layer with this ACME interface:
|
|
|
|
//
|
|
|
|
// 1. Everything: a managed key system view, an entropy sourcer, and an
|
|
|
|
// extended system view.
|
|
|
|
// 2. Managed keys without an entropy sourcer.
|
|
|
|
// 3. Just extended system view.
|
|
|
|
//
|
|
|
|
// Unfortunately, just using acmeBillingSystemViewImpl is not sufficient:
|
|
|
|
// because of the embedded interfaces, even when these are nil, the
|
|
|
|
// implementation will claim to support these interfaces, (thus, their cast
|
|
|
|
// being accepted by the toolchain), but when the caller goes to use the new
|
|
|
|
// value, they are hit with a nil panic.
|
|
|
|
//
|
|
|
|
// This is unfortunate.
|
|
|
|
//
|
|
|
|
// To avoid this, we create three possible implementations and use the lowest
|
|
|
|
// common denominator of the caller of NewAcmeBillingSystemView(...) to assume
|
|
|
|
// it is _just_ an extendedSystemView implementation.
|
|
|
|
|
|
|
|
// Scenario 1 above.
|
2023-05-17 16:12:04 +00:00
|
|
|
type acmeBillingSystemViewImpl struct {
|
|
|
|
extendedSystemView
|
|
|
|
logical.ManagedKeySystemView
|
2023-05-19 18:15:43 +00:00
|
|
|
entropy.Sourcer
|
|
|
|
acmeBillingImpl
|
|
|
|
}
|
2023-05-17 16:12:04 +00:00
|
|
|
|
2023-05-19 18:15:43 +00:00
|
|
|
var (
|
|
|
|
_ logical.ACMEBillingSystemView = (*acmeBillingSystemViewImpl)(nil)
|
|
|
|
_ extendedSystemView = (*acmeBillingSystemViewImpl)(nil)
|
|
|
|
_ logical.ManagedKeySystemView = (*acmeBillingSystemViewImpl)(nil)
|
|
|
|
_ entropy.Sourcer = (*acmeBillingSystemViewImpl)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
// Scenario 2 above.
|
|
|
|
type acmeBillingSystemViewImplNoSourcer struct {
|
|
|
|
extendedSystemView
|
|
|
|
logical.ManagedKeySystemView
|
|
|
|
acmeBillingImpl
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ logical.ACMEBillingSystemView = (*acmeBillingSystemViewImplNoSourcer)(nil)
|
|
|
|
_ extendedSystemView = (*acmeBillingSystemViewImplNoSourcer)(nil)
|
|
|
|
_ logical.ManagedKeySystemView = (*acmeBillingSystemViewImplNoSourcer)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
// Scenario 3 above.
|
|
|
|
type acmeBillingSystemViewImplNoManagedKeys struct {
|
|
|
|
extendedSystemView
|
|
|
|
acmeBillingImpl
|
2023-05-17 16:12:04 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 18:15:43 +00:00
|
|
|
var (
|
|
|
|
_ logical.ACMEBillingSystemView = (*acmeBillingSystemViewImplNoManagedKeys)(nil)
|
|
|
|
_ extendedSystemView = (*acmeBillingSystemViewImplNoManagedKeys)(nil)
|
|
|
|
)
|
2023-05-17 16:12:04 +00:00
|
|
|
|
2023-05-19 18:15:43 +00:00
|
|
|
// NewAcmeBillingSystemView creates the appropriate implementation based on
|
|
|
|
// the passed arguments, mapping them to the above scenarios. We further
|
|
|
|
// restrict sysView to have a dynamicSystemView implementation, to get the
|
|
|
|
// mount entry out of.
|
|
|
|
func (c *Core) NewAcmeBillingSystemView(sysView interface{}) extendedSystemView {
|
2023-05-17 16:12:04 +00:00
|
|
|
es := sysView.(extendedSystemViewImpl)
|
|
|
|
des := es.dynamicSystemView
|
|
|
|
|
2023-05-19 18:15:43 +00:00
|
|
|
// Scenario 3.
|
|
|
|
return &acmeBillingSystemViewImplNoManagedKeys{
|
|
|
|
extendedSystemView: es,
|
|
|
|
acmeBillingImpl: acmeBillingImpl{
|
|
|
|
core: c,
|
|
|
|
entry: des.mountEntry,
|
|
|
|
},
|
2023-05-17 16:12:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 18:15:43 +00:00
|
|
|
func (a *acmeBillingImpl) CreateActivityCountEventForIdentifiers(ctx context.Context, identifiers []string) error {
|
2023-05-17 16:12:04 +00:00
|
|
|
// Fake our clientID from the identifiers, but ensure it is
|
|
|
|
// independent of ordering.
|
|
|
|
//
|
|
|
|
// TODO: Because of prefixing currently handled by AddActivityToFragment,
|
|
|
|
// we do not need to ensure it is globally unique.
|
|
|
|
sort.Strings(identifiers)
|
|
|
|
joinedIdentifiers := "[" + strings.Join(identifiers, "]"+hopeDelim+"[") + "]"
|
|
|
|
identifiersHash := sha256.Sum256([]byte(joinedIdentifiers))
|
|
|
|
clientID := base64.RawURLEncoding.EncodeToString(identifiersHash[:])
|
|
|
|
|
|
|
|
// Log so users can correlate ACME requests to client count tokens.
|
2023-05-19 14:42:50 +00:00
|
|
|
a.core.activityLogLock.RLock()
|
|
|
|
activityLog := a.core.activityLog
|
|
|
|
a.core.activityLogLock.RUnlock()
|
|
|
|
if activityLog == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
activityLog.logger.Debug(fmt.Sprintf("Handling ACME client count event for [%v] -> %v", identifiers, clientID))
|
2023-05-24 16:29:47 +00:00
|
|
|
activityLog.AddActivityToFragment(clientID, a.entry.NamespaceID, time.Now().Unix(), ACMEActivityType, a.entry.Accessor)
|
2023-05-17 16:12:04 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|