-
Notifications
You must be signed in to change notification settings - Fork 130
/
secret.go
111 lines (98 loc) · 2.81 KB
/
secret.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package secret
import (
"encoding/json"
"io/ioutil"
"os"
"regexp"
"strings"
"time"
"github.com/LambdaTest/test-at-scale/pkg/core"
"github.com/LambdaTest/test-at-scale/pkg/errs"
"github.com/LambdaTest/test-at-scale/pkg/global"
"github.com/LambdaTest/test-at-scale/pkg/lumber"
)
type secretParser struct {
logger lumber.Logger
secretRegex *regexp.Regexp
}
// New return new secret parser
func New(logger lumber.Logger) core.SecretParser {
return &secretParser{
logger: logger,
secretRegex: regexp.MustCompile(global.SecretRegex),
}
}
// GetRepoSecret read repo secrets from given path
func (s *secretParser) GetRepoSecret(path string) (map[string]string, error) {
var secretData map[string]string
if _, err := os.Lstat(path); os.IsNotExist(err) {
s.logger.Debugf("failed to find user env secrets in path %s, as path does not exists", path)
return nil, nil
}
body, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, &secretData); err != nil {
s.logger.Errorf("failed to unmarshal user env secrets, error %v", err)
return nil, errs.ErrUnMarshalJSON
}
// extract secretmap from data map[data: map[secretname:secretvalue]]
return secretData, nil
}
// GetOauthSecret parses the oauth secret
func (s *secretParser) GetOauthSecret(path string) (*core.Oauth, error) {
o := &core.Oauth{
Type: core.Bearer,
}
if _, err := os.Lstat(path); os.IsNotExist(err) {
s.logger.Errorf("failed to find oauth secret in path %s", path)
return nil, err
}
body, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, o); err != nil {
s.logger.Errorf("failed to unmarshal oauth secret, error %v", err)
return nil, errs.ErrUnMarshalJSON
}
if o.AccessToken == "" {
return nil, errs.ErrMissingAccessToken
}
// If tokentype is not basic set it to bearer
if o.Type != core.Basic {
o.Type = core.Bearer
}
return o, err
}
// SubstituteSecret replace secret placeholders with their respective values
func (s *secretParser) SubstituteSecret(command string, secretData map[string]string) (string, error) {
matches := s.secretRegex.FindAllStringSubmatch(command, -1)
if matches == nil {
return command, nil
}
result := command
for _, match := range matches {
if len(match) < 2 {
return "", errs.ErrSecretRegexMatch
}
// validating secret key exists or not
if _, ok := secretData[match[1]]; !ok {
s.logger.Warnf("secret with name %s not found in map", match[0])
continue
}
result = strings.ReplaceAll(result, match[0], secretData[match[1]])
}
return result, nil
}
func (s *secretParser) Expired(token *core.Oauth) bool {
if token.RefreshToken == "" {
return false
}
if token.Expiry.IsZero() && token.AccessToken != "" {
return false
}
return token.Expiry.Add(-global.ExpiryDelta).
Before(time.Now())
}