2023-08-11 13:12:13 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
2023-07-17 22:15:22 +00:00
|
|
|
package secrets
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/testing/deployer/topology"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Store struct {
|
|
|
|
m map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
GossipKey = "gossip"
|
|
|
|
BootstrapToken = "bootstrap-token"
|
|
|
|
AgentRecovery = "agent-recovery"
|
2023-11-02 19:25:48 +00:00
|
|
|
CAPEM = "ca-pem"
|
2023-07-17 22:15:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Store) SaveGeneric(cluster, name, value string) {
|
|
|
|
s.save(encode(cluster, "generic", name), value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) ReadGeneric(cluster, name string) string {
|
|
|
|
return s.read(encode(cluster, "generic", name))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) SaveAgentToken(cluster string, nid topology.NodeID, value string) {
|
|
|
|
s.save(encode(cluster, "agent", nid.String()), value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) ReadAgentToken(cluster string, nid topology.NodeID) string {
|
|
|
|
return s.read(encode(cluster, "agent", nid.String()))
|
|
|
|
}
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
// Deprecated: SaveWorkloadToken
|
|
|
|
func (s *Store) SaveServiceToken(cluster string, wid topology.ID, value string) {
|
|
|
|
s.SaveWorkloadToken(cluster, wid, value)
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 19:22:06 +00:00
|
|
|
func (s *Store) SaveWorkloadToken(cluster string, wid topology.ID, value string) {
|
|
|
|
s.save(encode(cluster, "workload", wid.String()), value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deprecated: ReadWorkloadToken
|
|
|
|
func (s *Store) ReadServiceToken(cluster string, wid topology.ID) string {
|
|
|
|
return s.ReadWorkloadToken(cluster, wid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) ReadWorkloadToken(cluster string, wid topology.ID) string {
|
|
|
|
return s.read(encode(cluster, "workload", wid.String()))
|
2023-07-17 22:15:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) save(key, value string) {
|
|
|
|
if s.m == nil {
|
|
|
|
s.m = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.m[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) read(key string) string {
|
|
|
|
if s.m == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok := s.m[key]
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func encode(parts ...string) string {
|
|
|
|
var out []string
|
|
|
|
for _, p := range parts {
|
|
|
|
out = append(out, url.QueryEscape(p))
|
|
|
|
}
|
|
|
|
return strings.Join(out, "/")
|
|
|
|
}
|