-
Notifications
You must be signed in to change notification settings - Fork 48
/
utils.go
154 lines (129 loc) · 4.43 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
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache v2.0 license.
package auth
import (
"net"
"os"
"github.com/microsoft/moc/pkg/certs"
"github.com/microsoft/moc/pkg/marshal"
wssdnet "github.com/microsoft/moc/pkg/net"
)
func readAccessFile(accessFileLocation string) (WssdConfig, error) {
accessFile := WssdConfig{}
err := marshal.FromJSONFile(accessFileLocation, &accessFile)
if err != nil {
return WssdConfig{}, err
}
return accessFile, nil
}
// GenerateClientKey generates key and self-signed cert if the file does not exist in WssdConfigLocation
// If the file exists the values from the fie is returned
func GenerateClientKey(loginconfig LoginConfig) (string, WssdConfig, error) {
certBytes, err := marshal.FromBase64(loginconfig.Certificate)
if err != nil {
return "", WssdConfig{}, err
}
accessFile, err := readAccessFile(GetWssdConfigLocation())
if err != nil {
x509CertClient, keyClient, err := certs.GenerateClientCertificate(loginconfig.Name)
if err != nil {
return "", WssdConfig{}, err
}
certBytesClient := certs.EncodeCertPEM(x509CertClient)
keyBytesClient := certs.EncodePrivateKeyPEM(keyClient)
accessFile = WssdConfig{
CloudCertificate: "",
ClientCertificate: marshal.ToBase64(string(certBytesClient)),
ClientKey: marshal.ToBase64(string(keyBytesClient)),
}
}
if accessFile.CloudCertificate != "" {
serverPem, err := marshal.FromBase64(accessFile.CloudCertificate)
if err != nil {
return "", WssdConfig{}, err
}
if string(certBytes) != string(serverPem) {
certBytes = append(certBytes, serverPem...)
}
}
accessFile.CloudCertificate = marshal.ToBase64(string(certBytes))
return accessFile.ClientCertificate, accessFile, nil
}
func GenerateClientCsr(loginconfig LoginConfig) (string, WssdConfig, error) {
certBytes, err := marshal.FromBase64(loginconfig.Certificate)
if err != nil {
return "", WssdConfig{}, err
}
accessFile, err := readAccessFile(GetWssdConfigLocation())
cloudAgentIpAddress, err := wssdnet.GetIPAddress()
if err != nil {
return "", WssdConfig{}, err
}
localHostName, err := os.Hostname()
if err != nil {
return "", WssdConfig{}, err
}
cloudAgentIPAddress := wssdnet.StringToNetIPAddress(cloudAgentIpAddress)
ipAddresses := []net.IP{wssdnet.StringToNetIPAddress(wssdnet.LOOPBACK_ADDRESS), cloudAgentIPAddress}
dnsNames := []string{"localhost", localHostName}
conf := &certs.Config{
CommonName: loginconfig.Name,
AltNames: certs.AltNames{
DNSNames: dnsNames,
IPs: ipAddresses,
},
}
x509Csr, keyClient, err := certs.GenerateCertificateRequest(conf, nil)
if err != nil {
return "", WssdConfig{}, err
}
accessFile = WssdConfig{
CloudCertificate: "",
ClientCertificate: "",
ClientKey: marshal.ToBase64(string(keyClient)),
}
if accessFile.CloudCertificate != "" {
serverPem, err := marshal.FromBase64(accessFile.CloudCertificate)
if err != nil {
return "", WssdConfig{}, err
}
if string(certBytes) != string(serverPem) {
certBytes = append(certBytes, serverPem...)
}
}
accessFile.CloudCertificate = marshal.ToBase64(string(certBytes))
return string(x509Csr), accessFile, nil
}
// GenerateClientKeyWithName generates key and self-signed cert if the file does not exist in GetWssdConfigLocationName
// If the file exists the values from the fie is returned
func GenerateClientKeyWithName(loginconfig LoginConfig, subfolder, filename string) (string, WssdConfig, error) {
certBytes, err := marshal.FromBase64(loginconfig.Certificate)
if err != nil {
return "", WssdConfig{}, err
}
accessFile, err := readAccessFile(GetMocConfigLocationName(subfolder, filename))
if err != nil {
x509CertClient, keyClient, err := certs.GenerateClientCertificate(loginconfig.Name)
if err != nil {
return "", WssdConfig{}, err
}
certBytesClient := certs.EncodeCertPEM(x509CertClient)
keyBytesClient := certs.EncodePrivateKeyPEM(keyClient)
accessFile = WssdConfig{
CloudCertificate: "",
ClientCertificate: marshal.ToBase64(string(certBytesClient)),
ClientKey: marshal.ToBase64(string(keyBytesClient)),
}
}
if accessFile.CloudCertificate != "" {
serverPem, err := marshal.FromBase64(accessFile.CloudCertificate)
if err != nil {
return "", WssdConfig{}, err
}
if string(certBytes) != string(serverPem) {
certBytes = append(certBytes, serverPem...)
}
}
accessFile.CloudCertificate = marshal.ToBase64(string(certBytes))
return accessFile.ClientCertificate, accessFile, nil
}