-
Notifications
You must be signed in to change notification settings - Fork 69
/
utils.go
158 lines (134 loc) · 3.62 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
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
package utils
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/json"
"fmt"
"log"
"math/big"
"os"
"path/filepath"
"time"
"github.com/google/uuid"
"github.com/sethvargo/go-password/password"
yaml "gopkg.in/yaml.v3"
akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/api/v1"
)
// LoadUserProjectConfig load configuration into object
func LoadUserProjectConfig(path string) *akov2.AtlasProject {
var config akov2.AtlasProject
ReadInYAMLFileAndConvert(path, &config)
return &config
}
func RandomName(base string) string {
randomSuffix := uuid.New().String()[0:6]
return fmt.Sprintf("%s-%s", base, randomSuffix)
}
func UserSecretPassword() string {
return uuid.New().String()
}
func SaveToFile(path string, data []byte) error {
err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
if err != nil {
return err
}
err = os.WriteFile(path, data, os.ModePerm)
if err != nil {
return err
}
return nil
}
func JSONToYAMLConvert(cnfg interface{}) []byte {
var jsonI interface{}
j, _ := json.Marshal(cnfg)
err := yaml.Unmarshal(j, &jsonI)
if err != nil {
return nil
}
y, _ := yaml.Marshal(jsonI)
return y
}
// ReadInYAMLFileAndConvert reads in the yaml file given by the path given
func ReadInYAMLFileAndConvert(pathToYamlFile string, cnfg interface{}) interface{} {
// Read in the yaml file at the path given
yamlFile, err := os.ReadFile(filepath.Clean(pathToYamlFile))
if err != nil {
log.Printf("Error while parsing YAML file %v, error: %s", filepath.Clean(pathToYamlFile), err)
}
// Map yamlFile to interface
var body interface{}
if err := yaml.Unmarshal(yamlFile, &body); err != nil {
panic(err)
}
// Convert yaml to its json counterpart
body = ConvertYAMLtoJSONHelper(body)
// Generate json string from data structure
jsonFormat, err := json.Marshal(body)
if err != nil {
panic(err)
}
if err := json.Unmarshal(jsonFormat, &cnfg); err != nil {
panic(err)
}
return cnfg
}
// ConvertYAMLtoJSONHelper converts the yaml to json recursively
func ConvertYAMLtoJSONHelper(i interface{}) interface{} {
switch item := i.(type) {
case map[interface{}]interface{}:
document := map[string]interface{}{}
for k, v := range item {
document[k.(string)] = ConvertYAMLtoJSONHelper(v)
}
return document
case []interface{}:
for i, arr := range item {
item[i] = ConvertYAMLtoJSONHelper(arr)
}
}
return i
}
func GenUniqID() string {
return uuid.New().String()
}
func GenID() string {
id, _ := password.Generate(10, 3, 0, true, true)
return id
}
func CopyFile(source, target string) {
data, _ := os.ReadFile(filepath.Clean(source))
err := os.WriteFile(target, data, os.ModePerm)
if err != nil {
panic(err)
}
}
func GenerateX509Cert() ([]byte, *rsa.PrivateKey, *rsa.PublicKey, error) {
template := &x509.Certificate{
IsCA: true,
BasicConstraintsValid: true,
SubjectKeyId: []byte{1, 2, 3},
SerialNumber: big.NewInt(1234),
Issuer: pkix.Name{
CommonName: "x509-user",
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(5, 5, 5),
DNSNames: []string{"x509-user"},
// see http://golang.org/pkg/crypto/x509/#KeyUsage
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
}
privatekey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, nil, nil, err
}
publickey := &privatekey.PublicKey
var parent = template
cert, err := x509.CreateCertificate(rand.Reader, template, parent, publickey, privatekey)
if err != nil {
return nil, nil, nil, err
}
return cert, privatekey, publickey, nil
}