-
Notifications
You must be signed in to change notification settings - Fork 491
/
secretbackend.go
53 lines (45 loc) · 1.48 KB
/
secretbackend.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright 2022 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package secrets
import (
"fmt"
"time"
"github.com/juju/errors"
"github.com/juju/utils/v3"
)
// IsInternalSecretBackendID returns true if the supplied backend ID is the internal backend ID.
func IsInternalSecretBackendID(backendID string) bool {
return utils.IsValidUUIDString(backendID)
}
// SecretBackend defines a secrets backend.
type SecretBackend struct {
ID string
Name string
BackendType string
TokenRotateInterval *time.Duration
Config map[string]interface{}
}
// ValueRef represents a reference to a secret
// content value stored in a backend.
type ValueRef struct {
BackendID string
RevisionID string
}
func (r *ValueRef) String() string {
return fmt.Sprintf("%s:%s", r.BackendID, r.RevisionID)
}
// NextBackendRotateTime returns the next time a token rotate is due,
// given the supplied rotate interval.
func NextBackendRotateTime(now time.Time, rotateInterval time.Duration) (*time.Time, error) {
if rotateInterval > 0 && rotateInterval < time.Hour {
return nil, errors.NotValidf("token rotate interval %q less than 1h", rotateInterval)
}
// Rotate a reasonable time before the token is due to expire.
const maxInterval = 24 * time.Hour
nextInterval := time.Duration(0.75*rotateInterval.Seconds()) * time.Second
if nextInterval > maxInterval {
nextInterval = maxInterval
}
when := now.Add(nextInterval)
return &when, nil
}