This repository was archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplain.go
More file actions
63 lines (49 loc) · 1.23 KB
/
plain.go
File metadata and controls
63 lines (49 loc) · 1.23 KB
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
54
55
56
57
58
59
60
61
62
63
package secrets
import (
"encoding/base64"
"errors"
"fmt"
)
var ErrNotImplemented = errors.New("not implemented")
// implements plain "storage" for secret config
type PlainSecretProvider struct {
GenericConfig
}
func NewPlainSecretProviderFromConfig(cfg GenericConfig) *PlainSecretProvider {
return &PlainSecretProvider{
GenericConfig: cfg,
}
}
var _ SecretStorage = &PlainSecretProvider{}
func (fp *PlainSecretProvider) SetSecret(name string, secret []byte) error {
return ErrNotImplemented // and not really possible to implement...
}
func (fp *PlainSecretProvider) GetSecret(name string) (secret []byte, err error) {
b := []byte(name)
var result []byte
if fp.Base64 {
result = make([]byte, fp.encoder().DecodedLen(len(b)))
written, err := fp.encoder().Decode(result, b)
if err != nil {
return nil, fmt.Errorf("base64 decoding: %w", err)
}
result = result[:written]
return result, nil
}
return b, nil
}
func (fp *PlainSecretProvider) encoder() *base64.Encoding {
if fp.Base64URLEncoded {
if fp.Base64Raw {
return base64.RawURLEncoding
} else {
return base64.URLEncoding
}
} else { // std encoding
if fp.Base64Raw {
return base64.RawStdEncoding
} else {
return base64.StdEncoding
}
}
}