This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
/
util.go
304 lines (263 loc) · 6.6 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
package util
import (
"bytes"
"crypto/sha1"
"crypto/sha512"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"hash"
"io"
"io/ioutil"
"math/big"
"math/rand"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"syscall"
"time"
"github.com/ghodss/yaml"
"github.com/google/uuid"
"github.com/yalp/jsonpath"
"golang.org/x/crypto/ssh/terminal"
)
func ValidateName(name string) bool {
re := regexp.MustCompile("^[a-z0-9]+$")
return re.MatchString(name)
}
/*
This function allows using a filepath or http/s url to get resource from
*/
func UseSourceUrl(resourceUrl string) (string, error) {
u, err := url.ParseRequestURI(resourceUrl)
if err != nil || u.Scheme == "" {
file, err := ioutil.ReadFile(resourceUrl) // just pass the file name
if err != nil {
return "", err
}
source := string(file)
return source, nil
}
scheme := strings.ToLower(u.Scheme)
if scheme == "http" || scheme == "https" {
resp, err := http.Get(resourceUrl)
if err != nil {
return "", err
}
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
source := string(contents)
return source, nil
}
return "", errors.New("Not Supported protocol :" + scheme)
}
func Convert(inputFormat string, outputFormat string, input string) (string, error) {
if inputFormat == outputFormat {
return input, nil
}
inputSource := []byte(input)
if inputFormat == "yaml" {
json, err := yaml.YAMLToJSON(inputSource)
if err != nil {
return "", err
}
inputSource = json
}
// convert everything to json as byte
outputSourceString := ""
if outputFormat == "yaml" {
yaml, errYaml := yaml.JSONToYAML(inputSource)
if errYaml != nil {
// fmt.Printf("YAML conversion error: %v\n", errYaml)
return "", errYaml
}
outputSourceString = string(yaml)
} else {
var prettyJSON bytes.Buffer
indentError := json.Indent(&prettyJSON, inputSource, "", " ")
if indentError != nil {
// log.Println("JSON parse error: ", indentError)
return "", indentError
}
outputSourceString = string(prettyJSON.Bytes())
}
return outputSourceString, nil
}
// DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
func DownloadFile(filepath string, url string) error {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func GetJsonPath(source string, sourceFormat string, jsonPath string) (string, error) {
var jsonInterface map[string]interface{}
err := json.Unmarshal([]byte(source), &jsonInterface)
if err != nil {
return "", err
}
resultPath, err := jsonpath.Read(jsonInterface, jsonPath)
if err != nil {
return "", err
}
str, ok := resultPath.(string)
if !ok {
return "", errors.New("There is no string representation for " + jsonPath)
}
return str, nil
}
func GetHostFromUrl(resourceUrl string) (string, error) {
u, err_url := url.ParseRequestURI(resourceUrl)
if err_url != nil {
return "", err_url
}
return u.Host, nil
}
func VerifyCertForHost(resourceUrl string, cert string) error {
u, err_url := url.ParseRequestURI(resourceUrl)
if err_url != nil {
return err_url
}
host, _, _ := net.SplitHostPort(u.Host)
block, _ := pem.Decode([]byte(cert))
if block == nil {
return errors.New("failed to decode certificate")
}
crt, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return err
}
opts := x509.VerifyOptions{DNSName: host, Roots: x509.NewCertPool()}
opts.Roots.AddCert(crt)
_, err = crt.Verify(opts)
return err
}
func GetParameterFromTerminalAsSecret(text1 string, text2 string, errorText string) (string, error) {
fmt.Println(text1)
byteInput1, errInput1 := terminal.ReadPassword(int(syscall.Stdin))
if errInput1 != nil {
return "", errInput1
}
fmt.Println()
input1 := string(byteInput1)
fmt.Println(text2)
byteInput2, errInput2 := terminal.ReadPassword(int(syscall.Stdin))
if errInput2 != nil {
return "", errInput2
}
fmt.Println()
input2 := string(byteInput2)
if input1 != input2 {
return "", errors.New(errorText)
}
return input1, nil
}
var src = rand.NewSource(time.Now().UnixNano())
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
func RandomString(n int) string {
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
func PrettyJson(input string) string {
var prettyJSON bytes.Buffer
error := json.Indent(&prettyJSON, []byte(input), "", " ")
if error != nil {
fmt.Printf("Error: %v\n", error.Error())
return ""
}
return string(prettyJSON.Bytes())
}
func ReadFilesIndirectory(root string) (map[string]string, error) {
var files []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, path)
}
return nil
})
if err != nil {
return nil, nil
}
contents := make(map[string]string, 0)
for _, file := range files {
content, readError := UseSourceUrl(file)
if readError == nil {
contents[file] = content
} else {
fmt.Printf("Warning: File %v can not be read with error: %v\n", file, readError.Error())
}
}
return contents, nil
}
func UUID() string {
id := uuid.New()
return id.String()
}
func Timestamp() string {
t := time.Now()
return t.Format("2006-01-02T15:04:05.000Z")
}
func EncodeString(value string, algorithm string, salt string) string {
text := value + salt
var h hash.Hash
switch algorithm {
case "SHA-512":
h = sha512.New()
default:
h = sha1.New()
}
h.Write([]byte(text))
return HexAsBigInt(h.Sum(nil))
}
func RandomEncodedString(length int) string {
token := make([]byte, length)
rand.New(src).Read(token)
return HexAsBigInt(token)
}
// This is the hex conversion algorithm implemented in vamp
func HexAsBigInt(bytes []byte) string {
var bigInt big.Int
bigInt.SetBytes(bytes)
return bigInt.Text(16)
}