Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
R
re6stnet
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
Kirill Smelkov
re6stnet
Commits
df77b6a2
Commit
df77b6a2
authored
Sep 06, 2012
by
Julien Muchembled
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
re6st-conf: reusing existing cert or key if possible
parent
446b34a4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
16 deletions
+34
-16
re6st-conf
re6st-conf
+34
-16
No files found.
re6st-conf
View file @
df77b6a2
#!/usr/bin/env python
import
argparse
,
atexit
,
os
,
subprocess
,
sqlite3
,
sys
,
xmlrpclib
import
argparse
,
atexit
,
errno
,
os
,
subprocess
,
sqlite3
,
sys
,
xmlrpclib
from
OpenSSL
import
crypto
from
re6st
import
utils
def
create
(
path
,
text
=
None
,
mode
=
0666
):
fd
=
os
.
open
(
path
,
os
.
O_CREAT
|
os
.
O_WRONLY
|
os
.
O_TRUNC
,
mode
)
if
text
is
None
:
return
fd
try
:
os
.
write
(
fd
,
text
)
finally
:
...
...
@@ -56,11 +54,22 @@ def main():
sys
.
exit
(
r
)
req
=
crypto
.
X509Req
()
subj
=
req
.
get_subject
()
try
:
with
open
(
cert_path
)
as
f
:
cert
=
crypto
.
load_certificate
(
crypto
.
FILETYPE_PEM
,
f
.
read
())
components
=
dict
(
cert
.
get_subject
().
get_components
())
components
.
pop
(
'CN'
,
None
)
except
IOError
,
e
:
if
e
.
errno
!=
errno
.
ENOENT
:
raise
components
=
{}
if
config
.
req
:
for
k
,
v
in
config
.
req
:
if
k
==
'CN'
:
sys
.
exit
(
"CN field is reserved."
)
components
.
update
(
config
.
req
)
subj
=
req
.
get_subject
()
for
k
,
v
in
components
.
iteritems
():
if
k
==
'CN'
:
sys
.
exit
(
"CN field is reserved."
)
if
v
:
setattr
(
subj
,
k
,
v
)
cert_fd
=
token_advice
=
None
...
...
@@ -72,31 +81,40 @@ def main():
token_advice
=
"Use --token to retry without asking a new token
\
n
"
config
.
token
=
raw_input
(
'Please enter your token: '
)
# First make sure we can store private key and open certificate file
# for writing, to avoid using our token for nothing.
print
"Generating key and cert requests ..."
cert_fd
=
create
(
cert_path
)
pkey
=
crypto
.
PKey
()
pkey
.
generate_key
(
crypto
.
TYPE_RSA
,
2048
)
key
=
crypto
.
dump_privatekey
(
crypto
.
FILETYPE_PEM
,
pkey
)
try
:
with
open
(
key_path
)
as
f
:
pkey
=
crypto
.
load_privatekey
(
crypto
.
FILETYPE_PEM
,
f
.
read
())
key
=
None
print
"Reusing existing key."
except
IOError
,
e
:
if
e
.
errno
!=
errno
.
ENOENT
:
raise
print
"Generating 2048-bit key ..."
pkey
=
crypto
.
PKey
()
pkey
.
generate_key
(
crypto
.
TYPE_RSA
,
2048
)
key
=
crypto
.
dump_privatekey
(
crypto
.
FILETYPE_PEM
,
pkey
)
create
(
key_path
,
key
,
0600
)
req
.
set_pubkey
(
pkey
)
req
.
sign
(
pkey
,
'sha1'
)
req
=
crypto
.
dump_certificate_request
(
crypto
.
FILETYPE_PEM
,
req
)
create
(
key_path
,
key
,
0600
)
# First make sure we can open certificate file for writing,
# to avoid using our token for nothing.
cert_fd
=
os
.
open
(
cert_path
,
os
.
O_CREAT
|
os
.
O_WRONLY
,
0666
)
print
"Requesting certificate ..."
cert
=
s
.
requestCertificate
(
config
.
token
,
req
)
if
not
cert
:
token_advice
=
None
sys
.
exit
(
"Error: invalid or expired token"
)
except
:
if
cert_fd
:
if
cert_fd
is
not
None
and
not
os
.
lseek
(
cert_fd
,
0
,
os
.
SEEK_END
)
:
os
.
remove
(
cert_path
)
if
token_advice
:
atexit
.
register
(
sys
.
stdout
.
write
,
token_advice
)
raise
os
.
write
(
cert_fd
,
cert
)
os
.
ftruncate
(
cert_fd
,
len
(
cert
))
os
.
close
(
cert_fd
)
print
"Certificate setup complete."
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment