Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
ai
cam
Commits
f92c8c99
Commit
f92c8c99
authored
Apr 09, 2015
by
godog
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
run 2to3 and fix import ConfigParser
parent
f10dc3c2
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
42 additions
and
38 deletions
+42
-38
cam/ca.py
cam/ca.py
+7
-7
cam/config.py
cam/config.py
+6
-2
cam/main.py
cam/main.py
+12
-12
cam/tests/test_cert.py
cam/tests/test_cert.py
+7
-7
cam/tests/test_config.py
cam/tests/test_config.py
+2
-2
cam/tests/test_main.py
cam/tests/test_main.py
+5
-5
cam/tests/test_openssl_wrap.py
cam/tests/test_openssl_wrap.py
+1
-1
cam/tests/test_utils.py
cam/tests/test_utils.py
+2
-2
No files found.
cam/ca.py
View file @
f92c8c99
...
...
@@ -16,7 +16,7 @@ log = logging.getLogger(__name__)
class
_CAFiles
(
object
):
def
__init__
(
self
,
basedir
,
**
attrs
):
for
key
,
value
in
attrs
.
items
():
for
key
,
value
in
list
(
attrs
.
items
()
)
:
setattr
(
self
,
key
,
os
.
path
.
join
(
basedir
,
value
))
...
...
@@ -51,7 +51,7 @@ class CA(object):
try
:
fcntl
.
lockf
(
self
.
_lockfd
,
fcntl
.
LOCK_EX
|
fcntl
.
LOCK_NB
)
break
except
IOError
,
e
:
except
IOError
as
e
:
if
e
.
errno
in
(
errno
.
EACCES
,
errno
.
EAGAIN
):
n
-=
1
if
n
==
0
:
...
...
@@ -73,7 +73,7 @@ class CA(object):
self
.
_unlock
()
def
create
(
self
):
old_umask
=
os
.
umask
(
077
)
old_umask
=
os
.
umask
(
0
o
77
)
for
pathext
in
(
''
,
'conf'
,
'public'
,
'public/certs'
,
'public/crl'
,
'private'
,
'newcerts'
):
...
...
@@ -133,9 +133,9 @@ class CA(object):
os
.
path
.
join
(
self
.
basedir
,
'public/certs'
),
self
.
files
.
public_key
):
if
os
.
path
.
isdir
(
path
):
os
.
chmod
(
path
,
0755
)
os
.
chmod
(
path
,
0
o
755
)
else
:
os
.
chmod
(
path
,
0644
)
os
.
chmod
(
path
,
0
o
644
)
def
gencrl
(
self
):
log
.
info
(
'generating CRL'
)
...
...
@@ -153,7 +153,7 @@ class CA(object):
'crl'
,
'-inform'
,
'PEM'
,
'-outform'
,
'DER'
,
'-in'
,
tmpf
,
'-out'
,
self
.
files
.
crl
)
os
.
remove
(
tmpf
)
os
.
chmod
(
self
.
files
.
crl
,
0644
)
os
.
chmod
(
self
.
files
.
crl
,
0
o
644
)
def
revoke
(
self
,
cert
):
log
.
info
(
'revoking certificate %s'
,
cert
.
name
)
...
...
@@ -205,7 +205,7 @@ class CA(object):
'req'
,
'-new'
,
'-keyout'
,
cert
.
private_key_file
,
'-'
+
self
.
config
[
'signature_algorithm'
],
'-nodes'
,
'-out'
,
csr_file
)
os
.
chmod
(
cert
.
private_key_file
,
0600
)
os
.
chmod
(
cert
.
private_key_file
,
0
o
600
)
openssl_wrap
.
run_with_config
(
self
.
basedir
,
conf_file
,
self
.
_getpw
(),
'ca'
,
'-days'
,
conf
[
'days'
],
...
...
cam/config.py
View file @
f92c8c99
import
ConfigParser
try
:
import
ConfigParser
as
configparser
except
ImportError
:
# python 3 has ConfigParser renamed
import
configparser
import
os
from
cam
import
cert
from
cam
import
ca
...
...
@@ -9,7 +13,7 @@ class ConfigError(Exception):
def
read_config
(
filename
):
parser
=
C
onfig
P
arser
.
ConfigParser
()
parser
=
c
onfig
p
arser
.
ConfigParser
()
if
not
parser
.
read
(
filename
):
raise
ConfigError
(
'File not found: %s'
%
filename
)
root_dir
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
filename
))
...
...
cam/main.py
View file @
f92c8c99
...
...
@@ -64,7 +64,7 @@ def cmd_init(global_config, ca, certs, args):
def
cmd_gen
(
global_config
,
ca
,
certs
,
args
):
if
len
(
args
)
<
1
:
print
'Nothing to do.'
print
(
'Nothing to do.'
)
for
tag
in
args
:
ca
.
generate
(
find_cert
(
certs
,
tag
))
...
...
@@ -75,11 +75,11 @@ def cmd_gencrl(global_config, ca, certs, args):
def
cmd_files
(
global_config
,
ca
,
certs
,
args
):
if
len
(
args
)
<
1
:
print
'Nothing to do.'
print
(
'Nothing to do.'
)
for
tag
in
args
:
c
=
find_cert
(
certs
,
tag
)
print
c
.
public_key_file
print
c
.
private_key_file
print
(
c
.
public_key_file
)
print
(
c
.
private_key_file
)
def
cmd_list
(
global_config
,
ca
,
certs
,
args
):
...
...
@@ -94,19 +94,19 @@ def cmd_list(global_config, ca, certs, args):
if
expiry
<
now
:
state
=
'EXPIRED'
expiry_str
=
time
.
strftime
(
'%Y/%m/%d'
,
time
.
gmtime
(
expiry
))
print
cert
.
name
,
cert
.
cn
,
state
,
expiry_str
print
(
cert
.
name
,
cert
.
cn
,
state
,
expiry_str
)
def
cmd_verify
(
global_config
,
ca
,
certs
,
args
):
if
len
(
args
)
<
1
:
print
'Nothing to do.'
print
(
'Nothing to do.'
)
failed
=
False
for
path
in
args
:
if
not
ca
.
verify
(
path
):
print
'%s: FAIL'
%
path
print
(
'%s: FAIL'
%
path
)
failed
=
True
else
:
print
'%s: OK'
%
path
print
(
'%s: OK'
%
path
)
return
failed
...
...
@@ -114,9 +114,9 @@ def cmd_fingerprint(global_config, ca, certs, args):
if
len
(
args
)
>
0
:
certs
=
[
find_cert
(
certs
,
x
)
for
x
in
args
]
for
cert
in
certs
:
print
cert
.
name
,
cert
.
cn
print
' SHA1:'
,
cert
.
get_fingerprint
(
'sha1'
)
print
' MD5:'
,
cert
.
get_fingerprint
(
'md5'
)
print
(
cert
.
name
,
cert
.
cn
)
print
(
' SHA1:'
,
cert
.
get_fingerprint
(
'sha1'
)
)
print
(
' MD5:'
,
cert
.
get_fingerprint
(
'md5'
)
)
def
cmd_check
(
global_config
,
ca
,
certs
,
args
):
...
...
@@ -126,7 +126,7 @@ def cmd_check(global_config, ca, certs, args):
for
cert
in
certs
:
exp
=
cert
.
get_expiration_date
()
if
exp
and
(
exp
-
now
)
<
warning_time
:
print
'%s (%s) is about to expire.'
%
(
cert
.
name
,
cert
.
cn
)
print
(
'%s (%s) is about to expire.'
%
(
cert
.
name
,
cert
.
cn
)
)
retval
=
1
return
retval
...
...
cam/tests/test_cert.py
View file @
f92c8c99
...
...
@@ -50,36 +50,36 @@ class CertTest(unittest.TestCase):
def
test_get_fingerprint
(
self
):
crt
=
cert
.
Cert
(
self
.
ca
,
'test'
,
{
'cn'
:
'test.com'
})
md5
=
crt
.
get_fingerprint
(
'md5'
)
self
.
assertEqual
s
(
TEST_MD5
,
md5
)
self
.
assertEqual
(
TEST_MD5
,
md5
)
sha1
=
crt
.
get_fingerprint
(
'sha1'
)
self
.
assertEqual
s
(
TEST_SHA1
,
sha1
)
self
.
assertEqual
(
TEST_SHA1
,
sha1
)
def
test_get_fingerprint_nonexist
(
self
):
crt
=
cert
.
Cert
(
self
.
ca
,
'test-nonexist'
,
{
'cn'
:
'test.com'
})
result
=
crt
.
get_fingerprint
(
'md5'
)
self
.
assertEqual
s
(
None
,
result
)
self
.
assertEqual
(
None
,
result
)
def
test_cn_in_alt_names
(
self
):
crt
=
cert
.
Cert
(
self
.
ca
,
'test'
,
{
'cn'
:
'test.com'
,
'alt_names'
:
'test2.com'
})
self
.
assert
_
(
'test.com'
in
crt
.
alt_names
)
self
.
assert
True
(
'test.com'
in
crt
.
alt_names
)
def
test_get_expiration_date
(
self
):
crt
=
cert
.
Cert
(
self
.
ca
,
'test'
,
{
'cn'
:
'test.com'
})
exp
=
crt
.
get_expiration_date
()
self
.
assertEqual
s
(
TEST_EXPIRY
,
exp
)
self
.
assertEqual
(
TEST_EXPIRY
,
exp
)
def
test_get_expiration_date_nonexist
(
self
):
crt
=
cert
.
Cert
(
self
.
ca
,
'test-nonexist'
,
{
'cn'
:
'test.com'
})
exp
=
crt
.
get_expiration_date
()
self
.
assertEqual
s
(
None
,
exp
)
self
.
assertEqual
(
None
,
exp
)
def
test_expired
(
self
):
crt
=
cert
.
Cert
(
self
.
ca
,
'test'
,
{
'cn'
:
'test.com'
})
exp
=
crt
.
get_expiration_date
()
now
=
time
.
time
()
is_expired
=
(
exp
>
now
)
self
.
assertEqual
s
(
is_expired
,
crt
.
expired
())
self
.
assertEqual
(
is_expired
,
crt
.
expired
())
if
__name__
==
'__main__'
:
...
...
cam/tests/test_config.py
View file @
f92c8c99
...
...
@@ -38,8 +38,8 @@ cn = test.com
self
.
mox
.
ReplayAll
()
global_config
,
ca_obj
,
certs
=
config
.
read_config
(
cf_file
)
self
.
assertEqual
s
(
'ca'
,
ca_obj
)
self
.
assertEqual
s
([
'cert1'
],
certs
)
self
.
assertEqual
(
'ca'
,
ca_obj
)
self
.
assertEqual
([
'cert1'
],
certs
)
def
test_read_config_nonexist
(
self
):
def
f
():
...
...
cam/tests/test_main.py
View file @
f92c8c99
...
...
@@ -37,12 +37,12 @@ cn = www.test.org
sys
.
argv
=
[
'cam'
,
'--config=%s'
%
self
.
cfgfile
]
+
list
(
args
)
try
:
return
main
.
main
()
except
SystemExit
,
e
:
except
SystemExit
as
e
:
return
e
.
code
def
test_init_and_sanity_check
(
self
):
self
.
assertEqual
s
(
None
,
self
.
_run
(
'init'
))
self
.
assertEqual
s
(
None
,
self
.
_run
(
'gen'
,
'web'
))
self
.
assertEqual
(
None
,
self
.
_run
(
'init'
))
self
.
assertEqual
(
None
,
self
.
_run
(
'gen'
,
'web'
))
ca_file
=
os
.
path
.
join
(
self
.
tmpdir
,
'public/ca.pem'
)
crt_file
=
os
.
path
.
join
(
self
.
tmpdir
,
'public/certs/web.pem'
)
...
...
@@ -59,8 +59,8 @@ cn = www.test.org
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
STDOUT
)
output
=
pipe
.
communicate
()[
0
]
result
=
pipe
.
returncode
self
.
assertEqual
s
(
0
,
result
)
self
.
assertEqual
(
0
,
result
)
print
output
print
(
output
)
self
.
assertTrue
(
'error '
not
in
output
)
cam/tests/test_openssl_wrap.py
View file @
f92c8c99
...
...
@@ -31,7 +31,7 @@ class OpensslWrapTest(unittest.TestCase):
).
AndReturn
(
pipe_stub
)
self
.
mox
.
ReplayAll
()
result
=
openssl_wrap
.
run
(
'test'
)
self
.
assertEqual
s
(
'output'
,
result
)
self
.
assertEqual
(
'output'
,
result
)
def
test_run_fails
(
self
):
self
.
mox
.
StubOutWithMock
(
subprocess
,
'Popen'
,
use_mock_anything
=
True
)
...
...
cam/tests/test_utils.py
View file @
f92c8c99
...
...
@@ -22,9 +22,9 @@ class UtilsTest(unittest.TestCase):
tfd
.
close
()
of
=
os
.
path
.
join
(
self
.
tmpdir
,
'test.out'
)
utils
.
render
(
of
,
'test'
,
{
'sub'
:
'TEST'
})
self
.
assert
_
(
os
.
path
.
exists
(
of
))
self
.
assert
True
(
os
.
path
.
exists
(
of
))
output
=
open
(
of
,
'r'
).
read
()
self
.
assertEqual
s
(
'this is a TEST
\n
'
,
output
)
self
.
assertEqual
(
'this is a TEST
\n
'
,
output
)
def
test_parse_bool
(
self
):
self
.
assertTrue
(
utils
.
parse_bool
(
'y'
))
...
...
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