Changeset - 4d0bacead6e2
[Not reviewed]
0 4 0
Branko Majic (branko) - 6 years ago 2018-02-28 16:27:28
branko@majic.rs
GC-3: Updated init command to avoid errors and overwrites on already initialised directories:

- Added functional test covering the scenario where user has already
initialised the directory and is re-running the tool.
- Updated the init command to return status based on whether the
directory is already initialised or not.
- Implemented unit tests.
4 files changed with 73 insertions and 6 deletions:
0 comments (0 inline, 0 general)
functional_tests/test_init.py
Show inline comments
 
@@ -98,3 +98,24 @@ def test_initialisation_on_fresh_directory(tmpdir):
 
    # to realise they are identical.
 
    with open(".gimmecert/ca/level1.cert.pem") as cert_file, open(".gimmecert/ca/chain-full.cert.pem") as chain_file:
 
        assert cert_file.read() == chain_file.read()
 

	
 

	
 
def test_initialisation_on_existing_directory(tmpdir):
 
    # After a wild weekend out, John comes back to the office on
 
    # Monday morning, still a bit hangover. Back on Friday, John has
 
    # already initialised the CA hierarchy for one of his projects.
 
    tmpdir.chdir()
 
    run_command('gimmecert', 'init')
 

	
 
    # Unfortunately, John has forgot that he has done so. Therefore he
 
    # switches to his project directory and runs the command again.
 
    tmpdir.chdir()
 
    stdout, stderr, exit_code = run_command('gimmecert', 'init')
 

	
 
    # Instead of viewing information about his CA hierarchy
 
    # initialised, John is (somewhat pleasantly) surprised to see that
 
    # the tool has informed him the initialisation has already been
 
    # run.
 
    assert exit_code == 0
 
    assert stderr == ""
 
    assert "CA hierarchy has already been initialised." in stdout
gimmecert/cli.py
Show inline comments
 
@@ -47,12 +47,13 @@ def setup_init_subcommand_parser(parser, subparsers):
 
    def init_wrapper(args):
 
        project_directory = os.getcwd()
 

	
 
        init(project_directory)
 

	
 
        print("CA hierarchy initialised. Generated artefacts:")
 
        print("    CA Level 1 private key: .gimmecert/ca/level1.key.pem")
 
        print("    CA Level 1 certificate: .gimmecert/ca/level1.cert.pem")
 
        print("    Full certificate chain: .gimmecert/ca/chain-full.cert.pem")
 
        if init(project_directory):
 
            print("CA hierarchy initialised. Generated artefacts:")
 
            print("    CA Level 1 private key: .gimmecert/ca/level1.key.pem")
 
            print("    CA Level 1 certificate: .gimmecert/ca/level1.cert.pem")
 
            print("    Full certificate chain: .gimmecert/ca/chain-full.cert.pem")
 
        else:
 
            print("CA hierarchy has already been initialised.")
 

	
 
    subparser.set_defaults(func=init_wrapper)
 

	
gimmecert/commands.py
Show inline comments
 
@@ -31,6 +31,9 @@ def init(project_directory):
 

	
 
    :param project_directory: Path to directory where the structure should be initialised. Should be top-level project directory normally.
 
    :type project_directory: str
 

	
 
    :returns: False, if directory has been initialised in previous run, True if project has been initialised in this run.
 
    :rtype: bool
 
    """
 

	
 
    # Set-up various paths.
 
@@ -40,6 +43,9 @@ def init(project_directory):
 
    level1_certificate_path = os.path.join(ca_directory, 'level1.cert.pem')
 
    full_chain_path = os.path.join(ca_directory, 'chain-full.cert.pem')
 

	
 
    if os.path.exists(base_directory):
 
        return False
 

	
 
    # Initialise the directory.
 
    gimmecert.storage.initialise_storage(project_directory)
 

	
 
@@ -56,3 +62,5 @@ def init(project_directory):
 
    gimmecert.storage.write_private_key(level1_private_key, level1_private_key_path)
 
    gimmecert.storage.write_certificate(level1_certificate, level1_certificate_path)
 
    gimmecert.storage.write_certificate(full_chain, full_chain_path)
 

	
 
    return True
tests/test_commands.py
Show inline comments
 
@@ -43,3 +43,40 @@ def test_init_generates_ca_artifacts(tmpdir):
 
    assert os.path.exists(tmpdir.join('.gimmecert', 'ca', 'level1.key.pem').strpath)
 
    assert os.path.exists(tmpdir.join('.gimmecert', 'ca', 'level1.cert.pem').strpath)
 
    assert os.path.exists(tmpdir.join('.gimmecert', 'ca', 'chain-full.cert.pem').strpath)
 

	
 

	
 
def test_init_returns_true_if_directory_has_not_been_previously_initialised(tmpdir):
 
    tmpdir.chdir()
 

	
 
    initialised = gimmecert.commands.init(tmpdir.strpath)
 

	
 
    assert initialised is True
 

	
 

	
 
def test_init_returns_false_if_directory_has_been_previously_initialised(tmpdir):
 
    tmpdir.chdir()
 

	
 
    gimmecert.commands.init(tmpdir.strpath)
 
    initialised = gimmecert.commands.init(tmpdir.strpath)
 

	
 
    assert initialised is False
 

	
 

	
 
def test_init_does_not_overwrite_artifcats_if_already_initialised(tmpdir):
 
    tmpdir.chdir()
 

	
 
    gimmecert.commands.init(tmpdir.strpath)
 

	
 
    level1_private_key_before = tmpdir.join('.gimmecert', 'ca', 'level1.key.pem').read()
 
    level1_certificate_before = tmpdir.join('.gimmecert', 'ca', 'level1.cert.pem').read()
 
    full_chain_before = tmpdir.join('.gimmecert', 'ca', 'chain-full.cert.pem').read()
 

	
 
    gimmecert.commands.init(tmpdir.strpath)
 

	
 
    level1_private_key_after = tmpdir.join('.gimmecert', 'ca', 'level1.key.pem').read()
 
    level1_certificate_after = tmpdir.join('.gimmecert', 'ca', 'level1.cert.pem').read()
 
    full_chain_after = tmpdir.join('.gimmecert', 'ca', 'chain-full.cert.pem').read()
 

	
 
    assert level1_private_key_before == level1_private_key_after
 
    assert level1_certificate_before == level1_certificate_after
 
    assert full_chain_before == full_chain_after
0 comments (0 inline, 0 general)