forked from kramphub/kiya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
67 lines (58 loc) · 1.67 KB
/
utils.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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
cloudstore "cloud.google.com/go/storage"
"github.com/emicklei/tre"
"google.golang.org/api/cloudkms/v1"
"math/big"
"crypto/rand"
)
func getValueByKey(kmsService *cloudkms.Service, storageService *cloudstore.Client, key string, target profile) (string, error) {
encryptedValue, err := loadSecret(storageService, target, key)
if err != nil {
log.Fatal(tre.New(err, "get failed", "key", key))
return "", err
}
decryptedValue, err := getDecryptedValue(kmsService, target, encryptedValue)
if err != nil {
log.Fatal(tre.New(err, "get failed", "cipherText", encryptedValue))
return "", err
}
return decryptedValue, nil
}
// valueOrReadFrom returns the value argument if not empty or the contents of the file argument if empty.
func valueOrReadFrom(value string, file *os.File) string {
if len(value) != 0 {
return value
}
buffer, err := ioutil.ReadAll(file)
if err != nil {
log.Fatal("Error while reading from file", file, err)
}
return string(buffer)
}
func promptForYes(message string) bool {
reader := bufio.NewReader(os.Stdin)
fmt.Print(message)
yn, _ := reader.ReadString('\n')
return strings.HasPrefix(yn, "Y") || strings.HasPrefix(yn, "y")
}
func generate_secret(length int, chars string) (string, error) {
if len(chars) == 0 {
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$%^&*()_+`-={}|[]\\:\"<>?,./"
}
randomString := make([]byte, length)
for i := 0; i < length; i ++ {
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(chars))))
if err != nil {
return "", err
}
randomString[i] = chars[n.Int64()]
}
return string(randomString), nil
}