forked from alexandre-vasc/certificate-retriever
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateLoginPwd.py
executable file
·169 lines (141 loc) · 5.6 KB
/
generateLoginPwd.py
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
#!/usr/bin/python
#
import os
import sys
import binascii
from OpenSSL import crypto
from time import sleep
import requests
import getpass
import jwt
import certUtils
from parseArgs import parseArgs
import conf
def dojotLogin(dojot, username, httpsVerify):
if not username:
username = input('enter your dojot username: ')
passwd = getpass.getpass('enter password for ' + username + ': ')
try:
r = requests.post(dojot + conf.authpath, verify=httpsVerify,
json={
"username": username,
"passwd": passwd
})
r.raise_for_status()
except requests.exceptions.ConnectionError:
print("Could not connect to Auth service at " + dojot + conf.authpath)
exit(-1)
except requests.exceptions.HTTPError as err:
print('Authentication faied')
if err.response.status_code in [403, 401]:
print('Wrong user or password')
else:
print('Err code %d. mesage %s' %
(err.response.status_code, err.response.text))
exit(-1)
return r.json()['jwt']
def generateKeys(devname, length, overwrite):
filename = conf.certsDir + "/" + devname + ".key"
if not os.path.isfile(filename) or overwrite:
certUtils.generatePrivateKey(filename, length)
print(devname + " key pair created at " + filename)
else:
print("Key pair already exists at " + filename + ". Skiping.")
def generateCSR(devname, overwrite, dns, ip):
filename = conf.certsDir + "/" + devname + ".csr"
if not os.path.isfile(filename) or overwrite:
certUtils.generateCSR(
CName=devname,
privateKeyFile=conf.certsDir + "/" + devname + ".key",
csrFileName=filename,
dnsname=dns, ipaddr=ip)
else:
print("CSR file already exists at " + filename + ". Skiping.")
def askCertSign(ejbcaHost, devname, archivename, overwrite):
filename = conf.certsDir + "/" + archivename + ".crt"
if not os.path.isfile(filename) or overwrite:
try:
cert = certUtils.signCert(ejbcaHost,
conf.certsDir + "/" + archivename + ".csr",
devname, 'dojot')
except requests.exceptions.HTTPError as err:
print("Cant sign the CRT. EJBCA-REST return code: "
" EJBCA-REST return code: "
+ str(err.response.status_code))
print(str(err.response.text))
helperErrorDesc(err.response.status_code)
exit(-1)
certUtils.saveCRT(filename, cert)
print(devname + " certificate signed. Avaliable at " + filename)
else:
print("Certificate file already exists at " + filename + ". Skiping.")
def retrieveCAChain(ejbcaHost, caName, overwrite):
filename = conf.certsDir + "/" + caName + ".crt"
if not os.path.isfile(filename) or overwrite:
try:
rawCrt = certUtils.retrieveCAChain(ejbcaHost, caName)
certUtils.saveCRT(filename, rawCrt)
print("CA certificates retrieved")
except KeyError:
print("Invalid answer returned from EJBCA.")
exit(-1)
except requests.exceptions.HTTPError as err:
print("Can't retrieve CA chain certificate."
" EJBCA-REST return code: "
+ str(err.response.status_code))
print(str(err.response.text))
helperErrorDesc(err.response.status_code)
exit(-1)
else:
print("CA Certificate file already exists at "
+ filename + ". Skiping.")
def retrieveCRL(ejbcaHost, caName):
try:
rawCRL = certUtils.retrieveCACRL(ejbcaHost, caName)
except KeyError:
print("Invalid answer returned from EJBCA.")
exit(-1)
except requests.exceptions.HTTPError as err:
print("Can't retrieve CA CRL."
" EJBCA-REST return code: "
+ str(err.response.status_code))
print(str(err.response.text))
helperErrorDesc(err.response.status_code)
exit(-1)
try:
certUtils.saveCRL(conf.certsDir + "/" + caName + ".crl", rawCRL)
except crypto.Error:
print("Could not decode retrieved CRL")
exit(-1)
def helperErrorDesc(code):
if code == 401 or code == 403:
print("Your user is not allowed to do this")
elif code == 500:
print("internal server error")
elif code == 503:
print("Service unavailable. "
"If you are using dojot, check if Kong is configurated,"
" and if EJBCA-REST and Auth are running")
if __name__ == '__main__':
userConf = parseArgs()
userAuth = dojotLogin(userConf.dojot, userConf.username,
userConf.skipHttpsVerification)
print('Authenticated')
if not os.path.exists(conf.certsDir):
os.makedirs(conf.certsDir)
# get tenant from auth user
tenant = (jwt.decode(userAuth, verify=False))['service']
userConf.archiveName = tenant+'-'+userConf.deviceName
certUtils.defaultHeader['Authorization'] = 'Bearer ' + userAuth
retrieveCAChain(userConf.dojot, userConf.caName, userConf.overwrite)
# retrieveCRL(userConf.dojot, userConf.caName)
generateKeys(userConf.archiveName,
userConf.keyLength,
userConf.overwrite)
generateCSR(userConf.archiveName,
userConf.overwrite,
userConf.dns, userConf.ip)
askCertSign(userConf.dojot, userConf.deviceName,
userConf.archiveName, userConf.overwrite)
# deslog
exit(0)