-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
executable file
·341 lines (301 loc) · 7.97 KB
/
util.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package icannclient
import (
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"math"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
func ConsoleClearLastLine() {
fmt.Println("")
fmt.Print("\033[1A\033[K")
}
func encryptEnvVars(envFile string) error {
if !FileOrDirExists(envFile) {
return fmt.Errorf("file: %s does not exist", envFile)
}
saltValuePlain := ""
envSaltValue, envSaltValueExist := os.LookupEnv("SALT_PHRASE")
if envSaltValueExist && envSaltValue != "" {
// Salt phrase is set on the machine
saltValuePlain = envSaltValue
}
b, err := ioutil.ReadFile(envFile)
if err == nil {
lines := strings.Split(string(b), "\n")
if saltValuePlain == "" {
// Look for the salt value in the file
for i := 0; i < len(lines); i++ {
lines[i] = strings.Trim(lines[i], " ")
if lines[i] == "" || strings.HasPrefix(lines[i], "#") {
continue
}
v := strings.Split(lines[i], "=")
key := v[0]
value := v[1]
if key == "SALT_PHRASE" {
saltValuePlain = value
break
}
}
}
if saltValuePlain == "" {
// still blank; salt value is needed, whether set in
// file or exported on the machine-level
return errors.New("SALT_PHRASE is blank")
}
// if saltValuePlain is already encrypted, bail out
_, err := hex.DecodeString(saltValuePlain)
if err == nil {
return nil
}
m := make(map[string]string, 1)
// Encrypt the salt value; with blank salt phrase.
enc, err := EncryptLight([]byte(saltValuePlain), "")
if err != nil {
// stop the show if there is any error on
// encryption
log.Fatal(err)
}
s := hex.EncodeToString(enc)
m["SALT_PHRASE"] = s
// Now the rest
for i := 0; i < len(lines); i++ {
lines[i] = strings.Trim(lines[i], " ")
if lines[i] == "" || strings.HasPrefix(lines[i], "#") {
continue
}
v := strings.Split(lines[i], "=")
key := v[0]
value := v[1]
if key == "SALT_PHRASE" {
continue
}
enc, err := EncryptLight([]byte(value), saltValuePlain)
if err != nil {
// stop the show if there is any error on
// encryption
log.Fatal(err)
}
s := hex.EncodeToString(enc)
m[key] = s
}
var newLines []string
for i := 0; i < len(lines); i++ {
lines[i] = strings.Trim(lines[i], " ")
if lines[i] == "" || strings.HasPrefix(lines[i], "#") {
newLines = append(newLines, lines[i])
continue
}
v := strings.Split(lines[i], "=")
key := v[0]
str := fmt.Sprintf("%s=%s", key, m[key])
newLines = append(newLines, str)
}
f, err := os.Create(envFile)
if err != nil {
// stop the show; can't re-create the file!
log.Fatal(err)
}
defer f.Close()
for _, line := range newLines {
_, err := f.WriteString(line + "\n")
if err != nil {
// stop the show; must be able to write to the env file
log.Fatal(err)
}
}
} else {
// sotp the show if there an error reading the env file.
log.Fatal(err)
}
return nil
}
// SetEnvFromFile read target key/val from the icann.env
// file. It encrypts plain-text values before returning.
func SetEnvFromFile(envFile string) error {
if !FileOrDirExists(envFile) {
return fmt.Errorf("%s does not exist", envFile)
}
encryptEnvVars(envFile)
b, err := ioutil.ReadFile(envFile)
if err != nil {
return err
}
lines := strings.Split(string(b), "\n")
saltValue := ""
for i := 0; i < len(lines); i++ {
lines[i] = strings.TrimSpace(lines[i])
if lines[i] == "" || strings.HasPrefix(lines[i], "#") {
continue
}
pos := strings.Index(lines[i], "=")
if pos < 0 {
continue
}
key := strings.TrimSpace(lines[i][:pos])
value := strings.TrimSpace(lines[i][pos+1:])
if key == "SALT_PHRASE" {
bx, err := hex.DecodeString(value)
if err != nil {
log.Fatal(err)
}
bValue, err := DecryptLight(bx, "")
if err != nil {
log.Fatal(err)
}
saltValue = string(bValue)
os.Setenv(key, string(bValue))
} else {
bx, err := hex.DecodeString(value)
if err != nil || len(value) < 13 {
// as-is not encrypted
os.Setenv(key, value)
continue
}
bValue, _ := DecryptLight(bx, saltValue)
strValue := string(bValue)
os.Setenv(key, strValue)
}
}
return nil
}
func EncryptLight(data []byte, passphrase string) ([]byte, error) {
block, _ := aes.NewCipher([]byte(CreateHash(passphrase)))
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
nonce := make([]byte, gcm.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return nil, err
}
ciphertext := gcm.Seal(nonce, nonce, data, nil)
return ciphertext, nil
}
// CreateHash --
func CreateHash(key string) string {
hasher := md5.New()
hasher.Write([]byte(key))
return hex.EncodeToString(hasher.Sum(nil))
}
// DecryptLight --d
func DecryptLight(data []byte, passphrase string) ([]byte, error) {
var plaintext []byte
if len(data) == 0 {
return nil, errors.New("data is empty; nothing to decrypt")
}
key := []byte(CreateHash(passphrase))
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
nonceSize := gcm.NonceSize()
if nonceSize < 1 || len(data) < nonceSize {
return data, nil
}
nonce, ciphertext := data[:nonceSize], data[nonceSize:]
plaintext, err = gcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, err
}
return plaintext, nil
}
// formatDuration returns the time portion of time in format: hh:mm:ss.
// This has originally been suggested in a public netowrk article on
// https://stackoverflow.com/questions/47341278/how-to-format-a-duration,
// which was written to hold only hh:mm. This function has modified
// it to also hold seconds (hh:mm:ss).
func formatDuration(d time.Duration) string {
d = d.Round(time.Second)
h := d / time.Hour
d -= h * time.Hour
m := d / time.Minute
d -= m * time.Minute
s := d / time.Second
return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
}
// getNeutralDate gets padded data from the left in
// the format: YYYYMMDD.
func getNeutralDate(t time.Time) string {
return fmt.Sprintf("%d%02d%02d", time.Now().Year(), time.Now().Month(), time.Now().Day())
}
func RemoveItemFromIntArry(s []interface{}, i int) []interface{} {
s[len(s)-1], s[i] = s[i], s[len(s)-1]
return s[:len(s)-1]
}
// FileOrDirExists checks to see if a file or dir exist.
// Note that os.Stat(path) works for any path (file or
// directory).
func FileOrDirExists(path string) bool {
if path == "" {
return false
}
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
// getFreeDiskSpace reads the output of df -h /
// and returned the free disk-space on the system in GB (linux only).
func getFreeDiskSpace() float64 {
var freedksp float64
lsCmd := exec.Command("bash", "-c", " df -h /")
lsOut, _ := lsCmd.Output()
values := strings.Split(string(lsOut), "\n")
for i := 1; i < len(values); i++ {
oneLine := values[i]
v := strings.Split(oneLine, " ")
src := v[0]
isDev := strings.HasPrefix(src, "/dev")
if !isDev || strings.Contains(src, "/loop") {
continue
}
for i := 0; i < 5; i++ {
oneLine = strings.ReplaceAll(oneLine, " ", " ")
}
v = strings.Split(oneLine, " ")
avail := v[3]
avail = avail[:len(avail)-1]
freedksp, _ = strconv.ParseFloat(avail, 64)
if v[3][len(v[3])-1:] == "T" {
freedksp = freedksp * 1024
}
break
}
return freedksp
}
// itemExists is used to check for duplicates. It returns true
// if the element already exists in the array.
func itemExists(arry []interface{}, item interface{}) bool {
for i := 0; i < len(arry); i++ {
if arry[i] == item {
return true
}
}
return false
}
// roundNumber rounds a number to a target decimail point.
func roundNumber(n float64, percision uint32) float64 {
return math.Round(n*math.Pow(10, float64(percision))) / math.Pow(10, float64(percision))
}
// getMaxFreeDiskForZoneFile return the amount of
// free-disk-space reported by the system minus 10%
func getMaxFreeDiskForZoneFile() float64 {
freeDisk := getFreeDiskSpace()
workbenchRatio := freeDisk - (freeDisk * 0.1)
return workbenchRatio
}