Changeset - 08ba658f3c35
[Not reviewed]
0 2 0
Branko Majic (branko) - 8 years ago 2018-05-04 22:45:43
branko@majic.rs
GC-23: Updated documentation related to move of --update-dns-option from server to renew command:

- Updated the server command usage instructions to include reference
to --update-dns-option in the renew command.
- Added relevant documentation on use of the --update-dns-names option
to the usage instructions for renew command.
- Update the CLI examples.
2 files changed with 25 insertions and 26 deletions:
0 comments (0 inline, 0 general)
docs/usage.rst
Show inline comments
 
@@ -25,341 +25,340 @@ commands with short description on what they do, run one of the
 
following::
 

	
 
  gimmecert -h
 
  gimmecert --help
 
  gimmecert help
 

	
 
To get more details on specific command, along what mandatory and
 
positional arguments are available, simply provide the help flag when
 
running them. For example::
 

	
 
  gimmecert init -h
 
  gimmecert init --help
 

	
 

	
 
Quickstart
 
----------
 

	
 
Gimmecert stores all of its artefacts within the ``.gimmecert``
 
sub-directory (relative to where the command is run).
 

	
 
Start off by switching to your project directory::
 

	
 
  cd ~/myproject/
 

	
 
Initialise the necessary directories and CA hierarchy::
 

	
 
  gimmecert init
 

	
 
This will create a single CA, providing the following artifacts:
 

	
 
- ``.gimmecert/ca/level1.key.pem`` (private key)
 
- ``.gimmecert/ca/level1.cert.pem`` (certificate)
 
- ``.gimmecert/ca/chain-full.cert.pem`` (full CA chain, in this case
 
  same as ``level1.cert.pem``)
 

	
 
Issue a server certificate::
 

	
 
  gimmecert server myserver1
 

	
 
This will create the following artifacts for the server:
 

	
 
- ``.gimmecert/server/myserver1.key.pem`` (private key)
 
- ``.gimmecert/server/myserver1.cert.pem`` (certificate)
 

	
 
Resulting certificate will include its own name as one of the DNS
 
subject alternative names.
 

	
 
Issue a client certificate::
 

	
 
  gimmecert client myclient1
 

	
 
This will create the following artifacts for the client:
 

	
 
- ``.gimmecert/client/myclient1.key.pem`` (private key)
 
- ``.gimmecert/client/myclient1.cert.pem`` (certificate)
 

	
 
Issue a server certificate with additional DNS subject alternative
 
names::
 

	
 
  gimmecert server myserver2 myserver2.local service.example.com
 

	
 
This will create the following artifacts for the server:
 

	
 
- ``.gimmecert/server/myserver2.key.pem`` (private key)
 
- ``.gimmecert/server/myserver2.cert.pem`` (certificate)
 

	
 
This time around, the ``myserver2`` certificate will include
 
``myserver2``, ``myserver2.local``, and ``service.example.com`` as DNS
 
subject alternative names.
 

	
 
Issue a server certificate by passing-in certificate signing request
 
(CSR) from which the public key should be extracted::
 

	
 
  openssl req -new -newkey rsa:2048 -nodes -keyout "/tmp/myserver3.key.pem" -subj "/CN=ignoredname" -out "/tmp/myserver3.csr.pem"
 
  gimmecert server --csr /tmp/myserver3.csr.pem myserver3
 

	
 
This will create the following artifacts for the server:
 

	
 
- ``.gimmecert/server/myserver3.csr.pem`` (CSR)
 
- ``.gimmecert/server/myserver3.cert.pem`` (certificate)
 

	
 
Renew existing certificates, keeping the same private key and naming::
 

	
 
  gimmecert renew server myserver1
 
  gimmecert renew server myclient1
 

	
 
Show information about CA hierarchy and issued certificates::
 

	
 
  gimmecert status
 

	
 

	
 
Initialisation
 
--------------
 

	
 
Initialisation has to be run one before being being able to issue
 
server and client certificates. This is done with::
 

	
 
  gimmecert init
 

	
 
Initialisation will:
 

	
 
- Set-up a local directory.
 
- Initialise the CA hierarchy used for issuing server and client
 
  certificates. This includes creation of CA private keys (RSA 2048),
 
  as well as issuance of corresponding certificates.
 

	
 
If you attempt to run initialisation from the same directory twice,
 
Gimmecert will refuse to do so. Should you need to recreate the
 
hierarchy, simply remove the ``.gimmcert/`` directory, and start
 
over. Keep in mind you will need to throw away all of generated key
 
material and certificates.
 

	
 
The following directories are created as part of initialisation
 
process:
 

	
 
- ``.gimmecert/``, base directory.
 
- ``.gimmecert/ca/``, used for storing CA private keys and
 
  certificates.
 
- ``.gimmecert/server/``, used for storing server private keys and
 
  certificates.
 
- ``.gimmecert/client/``, used for storing client private keys and
 
  certificates.
 

	
 
Both CA private keys and certificates are stored as OpenSSL-style PEM
 
files. The naming convention for keys is ``levelN.key.pem``, while for
 
certificates it is ``levelN.cert.pem``. ``N`` corresponds to CA
 
level. Level 1 is the root/self-signed CA, level 2 is CA signed by
 
level 1 CA and so forth.
 

	
 
In addition to individual CA certificates, Gimmecert will also store
 
the full certificate chain (including the level 1 CA certificate) in
 
file ``chain-full.cert.pem``.
 

	
 
Subject DN naming convention for all CAs is ``CN=BASENAME Level N
 
CA``. ``N`` is the CA level, while ``BASENAME`` is by default equal to
 
current (working) directory name.
 

	
 
By defualt the tool will initialise a one-level CA hierarchy
 
(i.e. just the root CA).
 

	
 
Both the base name and CA hierarchy depth can be easily overridden by
 
providing options (both long and short forms are available)::
 

	
 
  gimmecert init --ca-base-name "My Project" --ca-hierarchy-depth 3
 
  gimmecert init -b "My Project" -d 3
 

	
 
The above examples would both result in creation of the following CA
 
artifacts:
 

	
 
- ``.gimmecert/ca/level1.key.pem``
 
- ``.gimmecert/ca/level1.cert.pem`` (subject DN ``My Project Level 1 CA``)
 
- ``.gimmecert/ca/level2.key.pem``
 
- ``.gimmecert/ca/level2.cert.pem`` (subject DN ``My Project Level 2 CA``)
 
- ``.gimmecert/ca/level3.key.pem``
 
- ``.gimmecert/ca/level3.cert.pem`` (subject DN ``My Project Level 3 CA``)
 
- ``.gimmecert/ca/chain-full.cert.pem``
 

	
 

	
 
Issuing server certificates
 
---------------------------
 

	
 
Server certificates can be issued once the initialisation is
 
complete. Command supports passing-in additional DNS subject
 
alternative names as additional positional arguments::
 

	
 
  gimmecert server NAME [DNS_NAME [DNS_NAME ...]]
 

	
 
The command will:
 

	
 
- Generate a 2048-bit RSA private key.
 
- Issue a certificate associated with the generated private key using
 
  the leaf CA (the one deepest in hierachy).
 

	
 
Resulting private keys and certificates are stored within directory
 
``.gimmecert/server/``. Private key naming convention is
 
``NAME.key.pem``, while certificates are stored as
 
``NAME.cert.pem``. In both cases the OpenSSL-style PEM format is used
 
for storage.
 

	
 
Subject DN naming convention for server certificates is ``CN=NAME``,
 
where ``NAME`` is passed-in via positional argument.
 

	
 
By default the certificate will include the passed-in server name as
 
one of its DNS subject alternative names, but additional DNS names can
 
be passed-in as well. For example::
 

	
 
  gimmecert server myserver myserver.local service.example.com
 

	
 
Key usage and extended key usage in certificate are set typical TLS
 
server use (e.g. *digital signature* + *key encipherment* for KU, and
 
*TLS WWW server authentication* for EKU).
 

	
 
Rerunning the command will not overwrite existing data. However, if
 
you made a mistake with additional DNS subject alternative names, you
 
can easily fix this with the ``--update-dns-names`` option::
 

	
 
  # Replace existing additional names.
 
  gimmecert server --update-dns-names myserver correctname.example.com
 

	
 
  # Remove additional names altogether.
 
  gimmecert server --update-dns-names myserver
 
Rerunning the command will not overwrite existing data.
 

	
 
The ``--update-dns-command`` will keep the private key intact - only
 
the certificate will be renewed. If you haven't issued any certificate
 
for this server entity before, though, the option is ignored, and the
 
command behaves as if it was not specified (so you still get a private
 
key and certificate).
 
.. note::
 
   For changing the list of additional subject alternative names
 
   included in already issued server certificates, see the
 
   ``--update-dns-names`` option in the ``gimmecert renew`` command.
 

	
 
In addition to generating a private key, it is also possible to
 
pass-in a certificate signing request (CSR). If specified path is a
 
dash (``-``), CSR is read from standard input. The resulting
 
certificate will contain public key from the CSR. All other
 
information stored in the CSR (naming, extensions) is ignored. For
 
example::
 

	
 
  # Issue server certificate by passing-in path to a generated CSR.
 
  gimmecert server --csr /tmp/myown.csr.pem myserver
 

	
 
  # Issue server certificate by reading the CSR from standard input.
 
  gimmecert server --csr - myserver
 

	
 
  # Issue server certificate by reading the CSR from standard input,
 
  # using redirection.
 
  gimmecert server --csr - myserver < /tmp/myown.csr.pem
 

	
 
The passed-in CSR will be stored alongside certificate, under
 
``.gimmecert/server/NAME.csr.pem``.
 

	
 

	
 
Issuing client certificates
 
---------------------------
 

	
 
Client certificates can be issued once the initialisation is
 
complete. Command accepts a single positional argument::
 

	
 
  gimmecert client NAME
 

	
 
The command will:
 

	
 
- Generate a 2048-bit RSA private key.
 
- Issue a certificate associated with the generated private key using
 
  the leaf CA (the one deepest in hierachy).
 

	
 
Rerunning the command will not overwrite existing data.
 

	
 
Resulting private keys and certificates are stored within directory
 
``.gimmecert/client/``. Private key naming convention is
 
``NAME.key.pem``, while certificates are stored as
 
``NAME.cert.pem``. In both cases the OpenSSL-style PEM format is used
 
for storage.
 

	
 
Subject DN naming convention for client certificates is ``CN=NAME``,
 
where ``NAME`` is passed-in via positional argument.
 

	
 
Key usage and extended key usage in certificate are set typical TLS
 
client use (e.g. *digital signature* + *key encipherment* for KU, and
 
*TLS WWW client authentication* for EKU).
 

	
 
In addition to generating a private key, it is also possible to
 
pass-in a certificate signing request (CSR). If specified path is a
 
dash (``-``), CSR is read from standard input. The resulting
 
certificate will contain public key from the CSR. All other
 
information stored in the CSR (naming, extensions) is ignored. For
 
example::
 

	
 
  # Issue client certificate by passing-in path to a generated CSR.
 
  gimmecert client --csr /tmp/myown.csr.pem myclient
 

	
 
  # Issue client certificate by reading the CSR from standard input.
 
  gimmecert client --csr - myclient
 

	
 
  # Issue client certificate by reading the CSR from standard input,
 
  # using redirection.
 
  gimmecert client --csr - myclient < /tmp/myown.csr.pem
 

	
 
The passed-in CSR will be stored alongside certificate, under
 
``.gimmecert/client/NAME.csr.pem``.
 

	
 

	
 
Renewing certificates
 
---------------------
 

	
 
Both client and server certificates can be renewed by simply providing
 
the type and name. This is useful when a certificate has expired, and
 
it should be renewed with identical naming and private key. Command
 
requires two positional argumensts::
 

	
 
  gimmecert renew (server|client) NAME
 

	
 
The command will:
 

	
 
- By default keep the existing private key generated for end entity
 
  (new one can be requested as well).
 
- Re-use naming and any extensions stored in existing certificate.
 
- Overwrite the existing certificate with a new one.
 
- Show information where the artifacts can be grabbed from.
 

	
 
.. note::
 
   For changing the list of additional subject alternative names
 
   included in server certificates, see the ``--update-dns-names`` for
 
   the ``gimmecert server`` command.
 

	
 
To also generate a new private key during renewal, use the
 
``--new-private-key`` or ``-p`` option. For example::
 

	
 
  gimmecert renew --new-private-key server myserver
 
  gimmecert renew -p server my server
 

	
 
To replace the existing private key or CSR during renewal with a new
 
CSR, use the ``--csr`` option and pass along path to the file. If
 
specified path is a dash (``-``), CSR is read from standard input. For
 
example::
 

	
 
  gimmecert renew --csr /tmp/myserver.csr.pem server myserver
 
  gimmecert renew --csr - server myserver < /tmp/myserver.csr.pem
 
  gimmecert renew --csr - client myclient
 

	
 
If you initially made a mistake when providing additional DNS subject
 
alternative names for a server certificate, you can easily fix this
 
with the ``--update-dns-names`` or ``-u`` option::
 

	
 
  # Replace existing additional names with just one name.
 
  gimmecert renew server --update-dns-names "correctname.example.com" myserver
 

	
 
  # Replace existing additional names with mutliple names.
 
  gimmecert renew server --update-dns-names "correctname1.example.com,correctname2.example.com" myserver 
 

	
 
  # Remove additional names altogether.
 
  gimmecert renew server --update-dns-names "" myserver
 

	
 

	
 
Getting information about CA hierarchy and issued certificates
 
--------------------------------------------------------------
 

	
 
In order to show information about the CA hierarchy and issued
 
certificates simply run the status command::
 

	
 
  gimmecert status
 

	
 
The command will:
 

	
 
- Show information about every CA in generated hierarchy (subject DN,
 
  validity, certificate paths, whether the CA is used for issuing end
 
  entity certificates).
 
- Show information about all issued server certificates (subject DN,
 
  DNS subject alternative names, validity, private key or CSR path,
 
  certificate path).
 
- Show information about all issued client certificates (subject DN,
 
  validity, private key or CSR path, certificate path).
 

	
 
Validity of all certificates is shown in UTC.
 

	
 
Command can also be used for checking if Gimmecert has been
 
initialised in local directory or not.
gimmecert/cli.py
Show inline comments
 
# -*- coding: utf-8 -*-
 
#
 
# Copyright (C) 2018 Branko Majic
 
#
 
# This file is part of Gimmecert.
 
#
 
# Gimmecert is free software: you can redistribute it and/or modify it
 
# under the terms of the GNU General Public License as published by the Free
 
# Software Foundation, either version 3 of the License, or (at your option) any
 
# later version.
 
#
 
# Gimmecert is distributed in the hope that it will be useful, but
 
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
# details.
 
#
 
# You should have received a copy of the GNU General Public License along with
 
# Gimmecert.  If not, see <http://www.gnu.org/licenses/>.
 
#
 

	
 

	
 
import argparse
 
import os
 
import sys
 

	
 
from .decorators import subcommand_parser, get_subcommand_parser_setup_functions
 
from .commands import client, help_, init, renew, server, status, usage, ExitCode
 

	
 

	
 
ERROR_GENERIC = 10
 

	
 

	
 
DESCRIPTION = """\
 
Issues server and client X.509 certificates using a local CA hierarchy.
 

	
 
Examples:
 

	
 
    # Set-up and switch to directory - you can switch to existing directory too.
 
    mkdir myproject/
 
    cd myproject/
 

	
 
    # Initialise the local CA hierarchy and all the necessary directories.
 
    gimmecert init
 

	
 
    # Issue a TLS server certificate with only the server name in DNS subject alternative name.
 
    gimmecert server myserver
 

	
 
    # Issue a TLS server certificate with additional DNS subject alternative names.
 
    gimmecert server myserver extradns1.local extradns2.example.com
 

	
 
    # Issue a TLS server certificate by using public key from the CSR (naming/extensions are ignored).
 
    gimmecert server myserver --csr /tmp/myserver.csr.pem
 

	
 
    # Issue a TLS client certificate.
 
    gimmecert client myclient
 

	
 
    # Issue a TLS client certificate by using public key from the CSR (naming/extensions are ignored).
 
    gimmecert client myclient --csr /tmp/myclient.csr.pem
 

	
 
    # Renew a TLS server certificate with updated DNS subject alternative names. Keeps the private key if any.
 
    # Renew a TLS server certificate, preserving naming and private key.
 
    gimmecert renew server myserver
 

	
 
    # Renew a TLS server certificate, replacing the extra DNS names, but keeping the private key.
 
    gimmecert server myserver wrongdns.local
 
    gimmecert server --update-dns-names myserver correctdns1.local correctdns2.local
 
    gimmecert renew server myserver --update-dns-names "correctdns1.local,correctdns2.local"
 

	
 
    # Renew a TLS server certificate removing extra DNS subject alternative names. Keeps the private key if any.
 
    # Renew a TLS server certificate, removing extra DNS subject alternative names, but keeping the private key.
 
    gimmecert server myserver dontneedthisname.local
 
    gimmecert server myserver --update-dns-names
 

	
 
    # Renew a TLS server certificate, preserving naming and private key.
 
    gimmecert renew server myserver
 
    gimmecert renew server myserver --update-dns-names ""
 

	
 
    # Renew a TLS client certificate, preserving naming and private key.
 
    gimmecert renew client myclient
 

	
 
    # Show information about CA hierarchy and issued certificates.
 
    gimmecert status
 
"""
 

	
 

	
 
@subcommand_parser
 
def setup_init_subcommand_parser(parser, subparsers):
 
    subparser = subparsers.add_parser('init', description='Initialise CA hierarchy.')
 
    subparser.add_argument('--ca-base-name', '-b', help="Base name to use for CA naming. Default is to use the working directory base name.")
 
    subparser.add_argument('--ca-hierarchy-depth', '-d', type=int, help="Depth of CA hierarchy to generate. Default is 1", default=1)
 

	
 
    def init_wrapper(args):
 
        project_directory = os.getcwd()
 
        if args.ca_base_name is None:
 
            args.ca_base_name = os.path.basename(project_directory)
 

	
 
        return init(sys.stdout, sys.stderr, project_directory, args.ca_base_name, args.ca_hierarchy_depth)
 

	
 
    subparser.set_defaults(func=init_wrapper)
 

	
 
    return subparser
 

	
 

	
 
@subcommand_parser
 
def setup_help_subcommand_parser(parser, subparsers):
 
    subparser = subparsers.add_parser('help', description='shows help')
 

	
 
    def help_wrapper(args):
 
        return help_(sys.stdout, sys.stderr, parser)
 

	
 
    subparser.set_defaults(func=help_wrapper)
 

	
 
    return subparser
 

	
 

	
 
@subcommand_parser
 
def setup_server_subcommand_parser(parser, subparsers):
 
    subparser = subparsers.add_parser('server', description='Issues server certificate.')
 
    subparser.add_argument('entity_name', help='Name of the server entity.')
 
    subparser.add_argument('dns_name', nargs='*', help='Additional DNS names to include in subject alternative name.')
 
    subparser.add_argument('--csr', '-c', type=str, default=None, help='''Do not generate server private key locally, and use the passed-in \
 
    certificate signing request (CSR) instead. Use dash (-) to read from standard input. Only the public key is taken from the CSR.''')
 

	
 
    def server_wrapper(args):
 
        project_directory = os.getcwd()
 

	
 
        return server(sys.stdout, sys.stderr, project_directory, args.entity_name, args.dns_name, args.csr)
 

	
 
    subparser.set_defaults(func=server_wrapper)
 

	
 
    return subparser
 

	
 

	
 
@subcommand_parser
 
def setup_client_subcommand_parser(parser, subparsers):
 
    subparser = subparsers.add_parser('client', description='Issue client certificate.')
 
    subparser.add_argument('entity_name', help='Name of the client entity.')
 
    subparser.add_argument('--csr', '-c', type=str, default=None, help='''Do not generate client private key locally, and use the passed-in \
 
    certificate signing request (CSR) instead. Use dash (-) to read from standard input. Only the public key is taken from the CSR.''')
 

	
 
    def client_wrapper(args):
 
        project_directory = os.getcwd()
 

	
 
        return client(sys.stdout, sys.stderr, project_directory, args.entity_name, args.csr)
 

	
 
    subparser.set_defaults(func=client_wrapper)
 

	
 
    return subparser
 

	
 

	
 
@subcommand_parser
 
def setup_renew_subcommand_parser(parser, subparsers):
 
    subparser = subparsers.add_parser('renew', description='Renews existing certificates.')
 
    subparser.add_argument('entity_type', help='Type of entity to renew.', choices=['server', 'client'])
 
    subparser.add_argument('entity_name', help='Name of the entity')
 

	
 
    def csv_list(csv):
 
        """
 
        Small helper that converts CSV string into a list.
 
        """
 

	
 
        if csv:
 
            return csv.split(",")
 

	
 
        return []
 

	
 
    subparser.add_argument('--update-dns-names', '-u', dest="dns_names", default=None, type=csv_list,
 
                           help='''Replace the DNS subject alternative names with new values. \
 
    Valid only for server certificate renewals. Multiple DNS names can be passed-in as comma-separated list. \
 
    Passing-in an empty string will result in all additional DNS subject alternative names being removed. \
 
    The entity name is kept as DNS subject alternative name in either case.''')
 

	
 
    new_private_key_or_csr_group = subparser.add_mutually_exclusive_group()
 

	
 
    new_private_key_or_csr_group.add_argument('--new-private-key', '-p', action='store_true', help='''Generate new private key for renewal. \
 
    Default is to keep the existing key. Mutually exclusive with the --csr option.''')
 
    new_private_key_or_csr_group.add_argument('--csr', '-c', type=str, default=None, help='''Do not use local private key and public key information from \
 
    existing certificate, and use the passed-in certificate signing request (CSR) instead. Use dash (-) to read from standard input. \
 
    If private key exists, it will be removed. Mutually exclusive with the --new-private-key option. Only the public key is taken from the CSR.''')
 

	
 
    def renew_wrapper(args):
 
        project_directory = os.getcwd()
 

	
 
        return renew(sys.stdout, sys.stderr, project_directory, args.entity_type, args.entity_name, args.new_private_key, args.csr, args.dns_names)
 

	
 
    subparser.set_defaults(func=renew_wrapper)
 

	
 
    return subparser
 

	
 

	
 
@subcommand_parser
 
def setup_status_subcommand_parser(parser, subparsers):
 

	
 
    subparser = subparsers.add_parser(name="status", description="Shows status information about issued certificates.")
 

	
 
    def status_wrapper(args):
 
        project_directory = os.getcwd()
 

	
 
        status(sys.stdout, sys.stderr, project_directory)
 

	
 
        return ExitCode.SUCCESS
 

	
 
    subparser.set_defaults(func=status_wrapper)
 

	
 
    return subparser
 

	
 

	
 
def get_parser():
 
    """
 
    Sets-up and returns a CLI argument parser.
 

	
 
    :returns: argparse.ArgumentParser -- argument parser for CLI.
 
    """
 

	
 
    parser = argparse.ArgumentParser(description=DESCRIPTION, formatter_class=argparse.RawDescriptionHelpFormatter)
 

	
 
    def usage_wrapper(args):
 
        return usage(sys.stdout, sys.stderr, parser)
 

	
 
    parser.set_defaults(func=usage_wrapper)
 

	
 
    subparsers = parser.add_subparsers()
 

	
 
    for setup_subcommad_parser in get_subcommand_parser_setup_functions():
 
        setup_subcommad_parser(parser, subparsers)
 

	
 
    return parser
 

	
 

	
 
def main():
 
    """
 
    This function is a CLI entry point for the tool. It is a thin
 
    wrapper around the argument parser, and underlying command
 
    implementation.
 

	
 
    In order for this to work, the parser needs to register the
 
    callback function as a default parameter for attribute
 
    'func'. This attribute is then invoked by the main function,
 
    passing-in all the parsed arguments while at it.
 
    """
 

	
 
    parser = get_parser()
 
    args = parser.parse_args()
 

	
 
    status_code = args.func(args)
 

	
 
    if status_code != ExitCode.SUCCESS:
 
        exit(status_code)
0 comments (0 inline, 0 general)