2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2024-01-02 21:36:20 +03:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-15 19:00:52 +03:00
|
|
|
|
2022-02-17 22:43:07 +03:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
|
|
)
|
|
|
|
|
|
|
|
const sscGenCounterPath string = "core/sscGenCounter/"
|
|
|
|
|
|
|
|
type SSCTokenGenerationCounter struct {
|
|
|
|
Counter int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TokenStore) GetSSCTokensGenerationCounter() int {
|
|
|
|
return ts.sscTokensGenerationCounter.Counter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TokenStore) loadSSCTokensGenerationCounter(ctx context.Context) error {
|
|
|
|
sscTokensGenerationCounterStorageVal, err := ts.core.barrier.Get(ctx, sscGenCounterPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to retrieve SSCTokenGenerationCounter from storage: err %w", err)
|
|
|
|
}
|
|
|
|
if sscTokensGenerationCounterStorageVal == nil {
|
|
|
|
ts.logger.Trace("no token generation counter found in storage")
|
|
|
|
ts.sscTokensGenerationCounter = SSCTokenGenerationCounter{Counter: 0}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var sscTokensGenerationCounter SSCTokenGenerationCounter
|
|
|
|
err = json.Unmarshal(sscTokensGenerationCounterStorageVal.Value, &sscTokensGenerationCounter)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("malformed token generation counter found in storage: err %w", err)
|
|
|
|
}
|
2023-04-05 20:06:57 +03:00
|
|
|
|
|
|
|
ts.logger.Debug("loaded ssct generation counter", "generation", sscTokensGenerationCounter.Counter)
|
2022-02-17 22:43:07 +03:00
|
|
|
ts.sscTokensGenerationCounter = sscTokensGenerationCounter
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TokenStore) UpdateSSCTokensGenerationCounter(ctx context.Context) error {
|
2022-08-31 21:05:21 +03:00
|
|
|
if err := ts.loadSSCTokensGenerationCounter(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-17 22:43:07 +03:00
|
|
|
ts.sscTokensGenerationCounter.Counter += 1
|
|
|
|
if ts.sscTokensGenerationCounter.Counter <= 0 {
|
|
|
|
// Don't store the 0 value
|
|
|
|
ts.logger.Warn("attempt to store non-positive token generation counter was ignored",
|
|
|
|
"sscTokensGenerationCounter", ts.sscTokensGenerationCounter.Counter)
|
|
|
|
}
|
|
|
|
marshalledCtr, err := json.Marshal(ts.sscTokensGenerationCounter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = ts.core.barrier.Put(ctx, &logical.StorageEntry{
|
|
|
|
Key: sscGenCounterPath,
|
|
|
|
Value: marshalledCtr,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-05 20:06:57 +03:00
|
|
|
|
|
|
|
ts.logger.Debug("updated ssct generation counter", "generation", ts.sscTokensGenerationCounter.Counter)
|
2022-02-17 22:43:07 +03:00
|
|
|
return nil
|
|
|
|
}
|