Changeset - 6c993789adf8
[Not reviewed]
0 3 0
Branko Majic (branko) - 6 years ago 2018-03-22 22:07:29
branko@majic.rs
GC-18: Fail the renew command if requested entity has not certificate:

- Added functional test for scenario where renewal is requested for
server or client certificate that do not exist already.
- Updated the renew command to return a new error code and show
appropriate error message.
- Added unit tests.
3 files changed with 73 insertions and 0 deletions:
0 comments (0 inline, 0 general)
functional_tests/test_renew.py
Show inline comments
 
@@ -72,3 +72,32 @@ def test_renew_command_requires_initialised_hierarchy(tmpdir):
 
    assert exit_code != 0
 
    assert stdout == ""
 
    assert stderr == "No CA hierarchy has been initialised yet. Run the gimmecert init command and issue some certificates first.\n"
 

	
 

	
 
def test_renew_command_reports_error_if_entity_does_not_exist(tmpdir):
 
    # John finally finds his way around to the project directory where
 
    # Gimmecert has already been used to set-up a hierarchy, and where
 
    # a couple of server and client certificates have been issued.
 
    tmpdir.chdir()
 
    run_command("gimmecert", "init")
 
    run_command("gimmecert", "server", "someserver")
 
    run_command("gimmecert", "client", "someclient")
 

	
 
    # He runs the command for renewing a server certificate.
 
    stdout, stderr, exit_code = run_command('gimmecert', 'renew', 'server', 'myserver')
 

	
 
    # Unfortunately for him, this server certificate has not been
 
    # issued before, and he is presented with an error.
 
    assert exit_code != 0
 
    assert stdout == ''
 
    assert stderr == "Cannot renew certificate. No existing certificate found for server myserver.\n"
 

	
 
    # This is going to be one of those days... He tries then to renew
 
    # a client certificate instead.
 
    stdout, stderr, exit_code = run_command('gimmecert', 'renew', 'client', 'myclient')
 

	
 
    # To his dismay, this results in error as well. He hasn't issued
 
    # such a certificate before either.
 
    assert exit_code != 0
 
    assert stdout == ''
 
    assert stderr == "Cannot renew certificate. No existing certificate found for client myclient.\n"
gimmecert/commands.py
Show inline comments
 
@@ -33,6 +33,7 @@ class ExitCode:
 
    ERROR_ALREADY_INITIALISED = 10
 
    ERROR_NOT_INITIALISED = 11
 
    ERROR_CERTIFICATE_ALREADY_ISSUED = 12
 
    ERROR_UNKNOWN_ENTITY = 13
 

	
 

	
 
def init(stdout, stderr, project_directory, ca_base_name, ca_hierarchy_depth):
 
@@ -254,9 +255,16 @@ def client(stdout, stderr, project_directory, entity_name):
 

	
 
def renew(stdout, stderr, project_directory, entity_type, entity_name):
 

	
 
    certificate_path = os.path.join(project_directory, '.gimmecert', entity_type, '%s.cert.pem' % entity_name)
 

	
 
    if not gimmecert.storage.is_initialised(project_directory):
 
        print("No CA hierarchy has been initialised yet. Run the gimmecert init command and issue some certificates first.", file=stderr)
 

	
 
        return ExitCode.ERROR_NOT_INITIALISED
 

	
 
    if not os.path.exists(certificate_path):
 
        print("Cannot renew certificate. No existing certificate found for %s %s." % (entity_type, entity_name), file=stderr)
 

	
 
        return ExitCode.ERROR_UNKNOWN_ENTITY
 

	
 
    return ExitCode.SUCCESS
tests/test_commands.py
Show inline comments
 
@@ -485,3 +485,39 @@ def test_renew_reports_error_if_directory_is_not_initialised(tmpdir):
 
    assert "No CA hierarchy has been initialised yet" in stderr
 
    assert stdout == ""
 
    assert status_code == gimmecert.commands.ExitCode.ERROR_NOT_INITIALISED
 

	
 

	
 
def test_renew_reports_error_if_no_existing_server_certificate_is_present(tmpdir):
 
    depth = 1
 
    gimmecert.commands.init(io.StringIO(), io.StringIO(), tmpdir.strpath, tmpdir.basename, depth)
 

	
 
    stdout_stream = io.StringIO()
 
    stderr_stream = io.StringIO()
 

	
 
    status_code = gimmecert.commands.renew(stderr_stream, stderr_stream, tmpdir.strpath, 'server', 'myserver')
 

	
 
    stdout = stdout_stream.getvalue()
 
    stderr = stderr_stream.getvalue()
 

	
 
    assert status_code == gimmecert.commands.ExitCode.ERROR_UNKNOWN_ENTITY
 
    assert "Cannot renew certificate" in stderr
 
    assert "server myserver" in stderr
 
    assert stdout == ""
 

	
 

	
 
def test_renew_reports_error_if_no_existing_client_certificate_is_present(tmpdir):
 
    depth = 1
 
    gimmecert.commands.init(io.StringIO(), io.StringIO(), tmpdir.strpath, tmpdir.basename, depth)
 

	
 
    stdout_stream = io.StringIO()
 
    stderr_stream = io.StringIO()
 

	
 
    status_code = gimmecert.commands.renew(stderr_stream, stderr_stream, tmpdir.strpath, 'client', 'myclient')
 

	
 
    stdout = stdout_stream.getvalue()
 
    stderr = stderr_stream.getvalue()
 

	
 
    assert status_code == gimmecert.commands.ExitCode.ERROR_UNKNOWN_ENTITY
 
    assert "Cannot renew certificate" in stderr
 
    assert "client myclient" in stderr
 
    assert stdout == ""
0 comments (0 inline, 0 general)