-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.go
148 lines (122 loc) · 2.93 KB
/
helpers.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package sicher
import (
"bufio"
"bytes"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"regexp"
"strings"
"time"
)
type EnvStyle string
const (
YAML EnvStyle = "yaml"
YML EnvStyle = "yml"
DOTENV EnvStyle = "dotenv"
)
var envStyleDelim = map[EnvStyle]string{
YAML: ":",
YML: ":",
DOTENV: "=",
}
var envStyleExt = map[EnvStyle]string{
YAML: "yml",
YML: "yml",
DOTENV: "env",
}
var envNameRegex = "^[a-zA-Z0-9_]*$"
// cleanUpFile removes the given file
func cleanUpFile(filePath string) {
err := os.Remove(filePath)
if err != nil {
fmt.Printf("Error while cleaning up %v \n", err.Error())
}
}
// decodeFile decodes the encrypted file and returns the decoded file and nonce
func decodeFile(encFile string) (nonce []byte, fileText []byte, err error) {
if encFile == "" {
return nil, nil, nil
}
resp := strings.Split(encFile, delimiter)
if len(resp) < 2 {
return nil, nil, errors.New("invalid credentials")
}
nonce, err = hex.DecodeString(resp[1])
if err != nil {
return nil, nil, err
}
fileText, err = hex.DecodeString(resp[0])
if err != nil {
return nil, nil, err
}
return
}
// generateKey generates a random key of 32 bytes and encodes as hex string
func generateKey() string {
timestamp := time.Now().UnixNano()
key := sha256.Sum256([]byte(fmt.Sprint(timestamp)))
rand.Read(key[16:])
return hex.EncodeToString(key[:])
}
// parseConfig parses the environment variables into a map
func parseConfig(config []byte, store map[string]string, envType EnvStyle) (err error) {
delim, ok := envStyleDelim[envType]
if !ok {
return errors.New("invalid environment type")
}
var b bytes.Buffer
b.Write(config)
sc := bufio.NewScanner(&b)
for sc.Scan() {
line := strings.TrimSpace(sc.Text())
cfgLine := strings.Split(line, delim)
// ignore commented lines and invalid lines
if len(cfgLine) < 2 || canIgnore(line) {
continue
}
// invalidate keys with invalid characters (only alphanumeric and _)
regexpKey := regexp.MustCompile(envNameRegex)
if !regexpKey.MatchString(cfgLine[0]) {
continue
}
store[cfgLine[0]] = strings.Join(cfgLine[1:], delim)
if err == io.EOF {
return nil
}
}
return nil
}
// canIgnore ignores commented lines and empty lines
func canIgnore(line string) bool {
line = strings.TrimSpace(line)
return strings.HasPrefix(line, `#`) || len(line) == 0
}
func addToGitignore(filePath, gitignorePath string) error {
f, err := os.OpenFile(fmt.Sprintf("%s.gitignore", gitignorePath), os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return err
}
defer f.Close()
fr := bufio.NewReader(f)
// check if the key file is already in the .gitignore file before adding it
// if it is, don't add it again
for err == nil {
str, _, err := fr.ReadLine()
if err != nil && err != io.EOF {
return err
}
if string(str) == filePath {
return nil
}
if err == io.EOF {
break
}
}
f.Write([]byte("\n" + filePath))
return nil
}