2019-01-08 00:37:16 +00:00
|
|
|
package influxdb
|
2018-11-13 20:20:37 +00:00
|
|
|
|
2019-07-26 19:38:30 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
2021-03-30 18:10:02 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/influxdb/v2/kit/platform"
|
2019-07-26 19:38:30 +00:00
|
|
|
)
|
2018-11-13 20:20:37 +00:00
|
|
|
|
2019-01-21 16:57:47 +00:00
|
|
|
// ErrSecretNotFound is the error msg for a missing secret.
|
|
|
|
const ErrSecretNotFound = "secret not found"
|
|
|
|
|
2018-11-13 20:20:37 +00:00
|
|
|
// SecretService a service for storing and retrieving secrets.
|
|
|
|
type SecretService interface {
|
|
|
|
// LoadSecret retrieves the secret value v found at key k for organization orgID.
|
2021-03-30 18:10:02 +00:00
|
|
|
LoadSecret(ctx context.Context, orgID platform.ID, k string) (string, error)
|
2018-11-13 20:20:37 +00:00
|
|
|
|
|
|
|
// GetSecretKeys retrieves all secret keys that are stored for the organization orgID.
|
2021-03-30 18:10:02 +00:00
|
|
|
GetSecretKeys(ctx context.Context, orgID platform.ID) ([]string, error)
|
2018-11-13 20:20:37 +00:00
|
|
|
|
|
|
|
// PutSecret stores the secret pair (k,v) for the organization orgID.
|
2021-03-30 18:10:02 +00:00
|
|
|
PutSecret(ctx context.Context, orgID platform.ID, k string, v string) error
|
2018-11-16 16:45:00 +00:00
|
|
|
|
|
|
|
// PutSecrets puts all provided secrets and overwrites any previous values.
|
2021-03-30 18:10:02 +00:00
|
|
|
PutSecrets(ctx context.Context, orgID platform.ID, m map[string]string) error
|
2018-11-16 16:45:00 +00:00
|
|
|
|
|
|
|
// PatchSecrets patches all provided secrets and updates any previous values.
|
2021-03-30 18:10:02 +00:00
|
|
|
PatchSecrets(ctx context.Context, orgID platform.ID, m map[string]string) error
|
2018-11-16 16:45:00 +00:00
|
|
|
|
|
|
|
// DeleteSecret removes a single secret from the secret store.
|
2021-03-30 18:10:02 +00:00
|
|
|
DeleteSecret(ctx context.Context, orgID platform.ID, ks ...string) error
|
2018-11-13 20:20:37 +00:00
|
|
|
}
|
2019-07-26 19:38:30 +00:00
|
|
|
|
|
|
|
// SecretField contains a key string, and value pointer.
|
|
|
|
type SecretField struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
Value *string `json:"value,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the key of the secret.
|
|
|
|
func (s SecretField) String() string {
|
|
|
|
if s.Key == "" {
|
|
|
|
return ""
|
|
|
|
}
|
2019-12-10 19:27:48 +00:00
|
|
|
return "secret: " + s.Key
|
2019-07-26 19:38:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implement the json marshaler interface.
|
|
|
|
func (s SecretField) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(s.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implement the json unmarshaler interface.
|
|
|
|
func (s *SecretField) UnmarshalJSON(b []byte) error {
|
|
|
|
var ss string
|
|
|
|
if err := json.Unmarshal(b, &ss); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ss == "" {
|
|
|
|
s.Key = ""
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(ss, "secret: ") {
|
|
|
|
s.Key = ss[len("secret: "):]
|
|
|
|
} else {
|
|
|
|
s.Value = strPtr(ss)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func strPtr(s string) *string {
|
|
|
|
ss := new(string)
|
|
|
|
*ss = s
|
|
|
|
return ss
|
|
|
|
}
|