-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
296 lines (271 loc) · 7.8 KB
/
main.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
package main
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/x509"
_ "embed"
"encoding/hex"
"encoding/pem"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
)
//go:embed cert/ZscalerRootCertificate.crt
var cert string
//go:embed cert/certBundle.pem
var certBundle string
//apps first column is windows, second is mac
var apps = map[string]string{
//Env variables
"openssl": "SSL_CERT_FILE",
"curl": "CURL_CA_BUNDLE",
"python": "REQUESTS_CA_BUNDLE",
"nodejs": "NODE_EXTRA_CA_CERTS",
"git": "GIT_SSL_CAPATH",
"aws": "AWS_CA_BUNDLE",
//commands
//"gcloud": "gcloud config set core/custom_ca_certs_file {{bundle}}",
//"composer": "composer config --global cafile {{bundle}}",
//"npm": "npm config set cafile {{bundle}}",
//registry
}
//Saving logs
var Logger *log.Logger
//Location
var Location Files
type Files struct {
Dir string
}
func (f Files) GetCert() string {
return filepath.Join(f.Dir, "/ZscalerRootCertificate.crt")
}
func (f Files) GetCertBundle() string {
return filepath.Join(f.Dir, "/zscalerCAbundle.pem")
}
func (f Files) GetLog() string {
return filepath.Join(f.Dir + "/zcertlog.txt")
}
//Init will create the certificate folder this will contain the certificate, certificate bundle and any log.
func init() {
//Create directory first.
createdir()
//Create logger
file, err := os.OpenFile(Location.GetLog(), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
//logging to stoud and file
w := io.MultiWriter(os.Stdout, file)
Logger = log.New(w, "", log.Ldate|log.Ltime|log.Lshortfile)
//Saving certificate file
certPath := Location.GetCert()
_, err = os.Stat(certPath)
if os.IsNotExist(err) {
file, err := os.Create(certPath)
if err != nil {
Logger.Fatal(err)
}
defer file.Close()
fmt.Fprintf(file, cert)
} else {
Logger.Println(certPath + " already exists")
}
//Saving certificate bundle
bundlePath := Location.GetCertBundle()
_, err = os.Stat(bundlePath)
if os.IsNotExist(err) {
file, err := os.Create(bundlePath)
if err != nil {
Logger.Fatal(err)
}
defer file.Close()
fmt.Fprintf(file, certBundle)
//Apending zscaler certificate to bundle.
fmt.Fprintln(file, "")
crt := CertPretty()
for _, l := range crt {
fmt.Fprintln(file, l)
}
} else {
Logger.Println(bundlePath + " already exists")
}
}
func main() {
//Setting command and subcommand structure
var rootCmd = &cobra.Command{Use: "zcert.exe"}
os := runtime.GOOS
if os == "darwin" {
rootCmd = &cobra.Command{Use: "./zcert"}
}
//cmdsystem to install zscaler cert on system trust store
var cmdSystem = &cobra.Command{
Use: "system",
Short: "install the certificate on the system trust store",
Long: "this will use the trust store for windows or mac so browsers, and apps following ",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
InstallSystemCert()
},
}
validArguments := []string{""}
//Adding supported apps
for k, _ := range apps {
validArguments = append(validArguments, k)
}
//cmdAppsto install zscaler cert on apps that don't follow the
var cmdApps = &cobra.Command{
Use: "apps [" + strings.Join(validArguments, " ") + "] ",
ValidArgs: validArguments,
Short: "install the certificate on all apps that don't dollow trust store unless specified",
Long: "apps that where certificate will be installed: " + strings.Join(validArguments, " "),
Args: cobra.OnlyValidArgs,
Run: func(cmd *cobra.Command, args []string) {
InstallApps(args)
},
}
rootCmd.AddCommand(cmdSystem)
rootCmd.AddCommand(cmdApps)
rootCmd.CompletionOptions.HiddenDefaultCmd = true
rootCmd.Execute()
}
func InstallSystemCert() {
os := runtime.GOOS
if os == "darwin" {
Logger.Println("OSX OS detected")
cmd1 := "sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain " + Location.GetCert()
Logger.Println("running command: " + cmd1)
cmd := exec.Command("bash", "-c", cmd1)
err := cmd.Run()
if err != nil {
Logger.Println(err)
}
} else if os == "windows" {
Logger.Println("Windows OS detected")
cmd1 := `Import-Certificate -CertStoreLocation Cert:\LocalMachine\Root -FilePath ` + Location.GetCert()
Logger.Println("running command: " + cmd1)
cmd := exec.Command("powershell", "-Command", cmd1)
err := cmd.Run()
if err != nil {
Logger.Println(err)
}
} else {
Logger.Fatalf("OS %v not supported", os)
}
}
func createdir() {
home, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
Location.Dir = filepath.Join(home, ".zscalerCerts") //this will create a hidden directory
log.Println(Location.Dir)
if _, err := os.Stat(Location.Dir); os.IsNotExist(err) {
err := os.Mkdir(Location.Dir, 0755)
if err != nil {
log.Fatal(err)
}
log.Println("Directory created")
} else {
log.Println("Directory already exists")
}
}
func InstallApps(args []string) {
all := false
if len(args) == 0 {
all = true
}
for name, cmd := range apps {
//iterating overt available apps and checking if the apps were selected
if slices.Contains(args, name) || all {
//enviroment variables case
setEnvCheck(cmd)
}
}
}
//this sets the enviroment variable value
func setEnvCheck(env string) {
value := os.Getenv(env)
if value == "" {
Logger.Printf("Enviroment variable: %v, not set. setting...", env)
setEnv(env)
} else if value == Location.GetCertBundle() {
Logger.Printf("Enviroment variable: %v, is already set to the same value. Skipping...", env)
} else {
Logger.Printf("Enviroment variable: %v, is currently set to: %v. Overriding with %v", env, value, Location.GetCertBundle())
setEnv(env)
}
}
//this sets the enviroment variable value
func setEnv(env string) {
os := runtime.GOOS
if os == "darwin" {
err := SetEnvMAC(env)
if err != nil {
Logger.Printf("Enviroment variable: %v, not set. Error: %v", env, err)
} else {
Logger.Printf("Enviroment variable: %v, set with value %v", env, Location.GetCertBundle())
}
} else if os == "windows" {
err := SetEnvWindows(env)
if err != nil {
Logger.Printf("Enviroment variable: %v, not set. Error: %v", env, err)
} else {
Logger.Printf("Enviroment variable: %v, set with value %v", env, Location.GetCertBundle())
}
} else {
Logger.Fatalf("OS %v not supported", os)
}
}
func SetEnvMAC(env string) error {
//Adding new line to env variable
cmd := "echo \"export " + env + "=" + Location.GetCertBundle() + "\" >> ~/.zshrc"
Logger.Printf("running command: %v", cmd)
out, err := exec.Command("bash", "-c", cmd).Output()
if err != nil {
return err
}
Logger.Printf("output from command: %s", out)
return nil
}
func SetEnvWindows(env string) error {
//Adding new line to env variable
cmd := "setx " + env + " \"" + Location.GetCertBundle() + "\""
Logger.Printf("running command: %v", cmd)
out, err := exec.Command("cmd", cmd).Output()
if err != nil {
return err
}
Logger.Printf("output from command: %s", out)
return nil
}
//CertPretty this returns a pem certificate prepending issuer, fingerprint, etc.
func CertPretty() []string {
res := []string{}
block, _ := pem.Decode([]byte(cert))
if block == nil || block.Type != "CERTIFICATE" {
Logger.Fatal("failed to decode PEM block certificate")
}
pub, err := x509.ParseCertificate(block.Bytes)
if err != nil {
Logger.Fatal(err)
}
res = append(res, "# Issuer: CN="+pub.Issuer.CommonName)
res = append(res, "# Subject: CN="+pub.Subject.CommonName)
res = append(res, "# Serial: "+pub.SerialNumber.String())
md5 := md5.Sum(pub.Raw)
sha1 := sha1.Sum(pub.Raw)
sha256 := sha256.Sum256(pub.Raw)
res = append(res, "# MD5 Fingerprint: "+hex.EncodeToString(md5[:]))
res = append(res, "# SHA1 Fingerprint: "+hex.EncodeToString(sha1[:]))
res = append(res, "# SHA256 Fingerprint: "+hex.EncodeToString(sha256[:]))
res = append(res, cert)
return res
}