-
Notifications
You must be signed in to change notification settings - Fork 2
/
format.go
83 lines (70 loc) · 1.59 KB
/
format.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package keyring
import (
"errors"
"fmt"
"os"
"time"
"github.com/appgate/sdpctl/pkg/hashcode"
zkeyring "github.com/zalando/go-keyring"
)
const (
keyringService = "sdpctl"
password = "password"
username = "username"
bearer = "bearer"
refreshToken = "refreshToken"
)
// ErrKeyringTimeOut is returned when the keyring operation takes too long to complete.
var ErrKeyringTimeOut = errors.New("keyring: timeout")
// keyringTimeout max time for a keyring syscall
var keyringTimeout = time.Second * 5
func runWithTimeout(task func() error) error {
if len(os.Getenv("SDPCTL_NO_KEYRING")) > 0 {
return nil
}
ch := make(chan error)
go func() {
ch <- task()
}()
select {
case err := <-ch:
return err
case <-time.After(keyringTimeout):
return ErrKeyringTimeOut
}
}
func format(prefix, value string) string {
return fmt.Sprintf("%d.%s", hashcode.String(prefix), value)
}
func getSecret(key string) (string, error) {
if len(os.Getenv("SDPCTL_NO_KEYRING")) > 0 {
return "", nil
}
ch := make(chan struct {
response string
err error
})
go func() {
v, err := zkeyring.Get(keyringService, key)
ch <- struct {
response string
err error
}{v, err}
}()
select {
case result := <-ch:
return result.response, result.err
case <-time.After(keyringTimeout):
return "", ErrKeyringTimeOut
}
}
func setSecret(key, value string) error {
return runWithTimeout(func() error {
return zkeyring.Set(keyringService, key, value)
})
}
func deleteSecret(key string) error {
return runWithTimeout(func() error {
return zkeyring.Delete(keyringService, key)
})
}