Commit Graph

28 Commits

Author SHA1 Message Date
Alexander Hughes 452734fb72 Making certificate check more robust
Some operators may have externally managed certificates that do not
conform to the originally expected certificate pattern of

---BEGIN CERTIFICATE-----
foo
-----END CERTIFICATE-----

and may instead include additional information on the subject/issuer. In
these cases the current regex will fail to load certs that can be parsed
with the existing cfssl command. Addressing this by tightening up the
regex prior to trying to obtain certificate information.

Change-Id: Ief9993632718caa46b52761b49a97621f134ca53
Signed-off-by: Alexander Hughes <Alexander.Hughes@pm.me>
2020-12-29 16:16:57 -05:00
Ian H. Pittwood fe592312aa Change wrap command to add YAML section delimiter
Change-Id: Ic2f57e14c1c71d49bbd92a241dec9ed3163f6195
2020-01-21 13:24:12 -06:00
Alexander Hughes c6e34b47ca Check cert expiry for multiple types
This patch adds support for:
- Checking expiration of CAs in manifests
- Multiple certs per data field of a YAML document

Change-Id: I9dae69acb4252d4de4469eb6733b533ef479f7b4
2020-01-08 19:05:52 +00:00
Ian H Pittwood ff9c95f423 Add path option to encrypt command
Adds a path option to the encrypt command to specify what directory of
file to encrypt. If path is not specified, all applicable files in the
repositories will be encrypted (this is the current behavior).

Change-Id: Idd5e063a54cf157a8ec761de85cbd67edd05364c
2020-01-07 15:40:05 -06:00
Ian H. Pittwood fff70ad861 Refactors pegleg CLI to use single commands
Debugging pegleg can currently be difficult and the Click CLI does not
easily allow debuggers like pdb or PyCharm to use breakpoints. By moving
all CLI command calls into singular functions, we can easily create an
"if __name__ == '__main__'" entry point to call these functions and
investigate any bugs that may arise.

We also gain the ability to reuse more portions of our code by
refactoring these methods.

Change-Id: Ia9739931273eb6458f82dbb7e702a505ae397ae3
2019-12-17 19:32:28 +00:00
Zuul ae5db00f83 Merge "Improve check-pki-certs output" 2019-12-11 15:25:35 +00:00
Ian H. Pittwood 9163ef08ca Add passphrase catalog override option
Adds an option to specify a passphrase catalog to override catalogs
discovered in the site repository. This allows the generation of a
specified subset of passphrases instead of the entire site's catalog.

Change-Id: I797107234292eea8ca788b7a94ed5e2c90566bf5
2019-12-10 20:40:31 +00:00
Ian H. Pittwood a97c9cdba2 Improve check-pki-certs output
Adds alternate message when no certificates are expiring

Exit code will now be 1 if there are certificates expiring and 0 if no
certificates are expiring

Change-Id: I94a7a5af0c5469b83001b5439f18691140de6245
2019-12-10 13:40:07 -06:00
Ian H. Pittwood 87d24d530a Add cert name to check-pki-certs command
Adds a table column for the certificate name. Each row of data will now
be printed with the file name, certificate name, and certificate
expiration date.

Change-Id: I088ba2e794f33cd858f36275d00dd431862f1c25
2019-12-10 15:20:27 +00:00
Alexander Hughes f56d20a2da bugfix for global encrypt/decrypt
This patch updates layer for wrapped documents to preserve original
layer.  Previously all encrypted documents had site layer.

Update encrypt/decrypt logic when determining global keys.

Update units tests.

Change-Id: I447aeaea08a4514655fcabfc7077b6d4b282e27f
2019-10-02 19:13:33 +00:00
Ian H. Pittwood e2dad75a99 Default to non-interactive passphrase generation
This change disables and skips input prompts for generate passphrases.
Using the -i option will now only enable prompts for passphrases that
are set to prompt=True.

Change-Id: Ia932305891259d9d1430e1d184dbf39892d4a5d3
2019-09-16 13:13:04 -05:00
Ian H Pittwood eb6c2574bc Set a fixed order in which data is dumped to YAML files
One of the well-known issues of Python is that dictionaries do not
maintain order in their keys once created. This causes YAML data dumps
to output in a seemingly random order or alphabetically. As these output
files are often kept in their own repositories, they must go through
review or comparison in VCS. If the order of keys is switching for these
files every time Pegleg is ran, it makes it difficult for a user to
compare newly generated files with the old.

To fix this issue, we can change all dictionaries used to template
YAML files into OrderedDict objects. The OrderedDict objects will
maintain order through their dumping to YAML.

Change-Id: I0c1ee3f3f37ed8598d2ba81528d5c61447cbd0d0
2019-08-02 18:33:26 +00:00
Alexander Hughes 1c8d92ef6b Standardize Pegleg code with YAPF
This patch addresses inconsistent code style and enforces it with a
gate for future submissions.

Separate work will be done in the future to address several of the
PEP8 ignores for docstrings, and attempt to bring the tests directory
to PEP8 compliance.

This patch:
1. Updates .style.yapf to set the knobs desired for YAPF.
2. Updates tox.ini to allow one of the knobs to work.
3. Removes unused code from several __init__.py files.
4. Updates the YAPF version in test-requirements.txt to latest (this
   is needed for several knobs to work).
5. Stylistic changes to the python codebase in Pegleg.
6. Updates to tox.ini to run YAPF during PEP8 check.

Change-Id: Ieaa0fdef2b601d01c875d64b840986e54df73abf
2019-07-25 17:28:18 +00:00
HUGHES, ALEXANDER (ah8742) d888b3e138 Add support for globally encrypted secrets
This patchset adds support for globally encrypted secrets.
Documents with a "site" layer will be encrypted/decrypted with the
standard PEGLEG_PASSPHRASE and PEGLEG_SALT environment variables.

If any secrets exist for the site with a schema of "global_passphrase"
or "global_salt" their values will be captured and used to decrypt
any secrets that do not belong to "site" layer.  If the global keys
do not exist, Pegleg will default to using site keys.

Expected usage:
1. Set site passphrase/salt environment variables
2. Select a global passphrase and salt
3. Use Pegleg's "wrap" command to wrap and encrypt the global keys
4. Encrypt or wrap documents with "global" layer
5. Provide Pegleg path to decrypt

In the case of (4) and (5) Pegleg will determine the correct keys
to use automatically

Change-Id: I5de6d63573619b346fe011628ae21e053e0711f6
2019-07-02 13:54:04 -05:00
HUGHES, ALEXANDER (ah8742) a8620cfd8d Implement default umask for 640 file permissions
Some secrets are being created with undesirable permissions. Upon
inspection it was noticed that in general Pegleg is creating files,
then changing permissions after the fact. This leads to a small
window where the permissions on a file are overly permissive.

This patchset:
1. Sets default umask of 0o027 (640 permissions for files)
2. Explicitly adds the open flag ('r', 'w' etc.) to all open() calls.
3. Replaces sys.stdout.write calls with click.echo() calls to be more
   in line with the rest of the project.
4. Re-orders methods that write so that data is always first, and the
   path is always second.
5. Updates unit tests.
6. Adds unit tests for testing directory and file permissions.
7. Minor style changes.

Change-Id: I0c154aa311ea371940fd24b0aabf58fffaf1d231
2019-06-29 17:56:55 +00:00
Lev Morgan 52b61b8cfd Added cleartext option to passphrase generation
Added a force-cleartext option (false by default) which forces
passphrases to be generated in cleartext rather than encrypted.

Change-Id: I157a40103f67f85a24976b4f59aa46f2d4b92334
2019-06-12 00:42:03 +00:00
Ian H. Pittwood 2fa6a1a7bd Allow decryption of directories
This change allows users to specify a directory or file to be decrypted.

Allows directory decryption.

Adds flag to overwrite encrypted file with decrypted data.

Intelligently recognizes paths vs files in CLI input and outputs data 
accordingly.

Change-Id: I0d5e77f0eb1adb42165aa9b214aa90a0db0a3131
2019-05-28 15:08:14 +00:00
Alexander Hughes fb5d54fdb9 Update decrypt command
Decrypt command was previously requiring that specified files have
in their paths the site name.  This isn't necessarily always the case
for example we can have global files that need to be decrypted and do
not contain the site name in the filepath, but the site name is
relevant in ensuring based on the site-definition.yaml file that
pegleg uses the correct revision of the global repository.

The end result should be that when decrypting a file, we specify the
site name, pegleg ensures we're on correct revisions of the repos
and if the file exists, decrypt and print to stdout

This patch addresses this by:
1. Updating pegleg.engine.secrets.decrypt to no longer require a
   site name.
2. Updating pegleg.cli.decrypt to no longer pass a site name to
   pegleg.engine.secrets.decrypt
3. Updating documentation for CLI.
4. Updating unit tests for CLI and secrets.

Change-Id: Ia97518b06a58b069a4d6c0b8d68a37f45e5d31bb
2019-05-09 11:30:54 -05:00
Alexander Hughes 7c52ab68da PKI Cert generation and check updates
This patch:
1. Allows user to change valid duration of newly generated certs
default=1yr
2. Allows user to check certs that are expiring soon default=60d

Change-Id: Ia5c87a0c52b39b778f425599fa215fb67147c65b
2019-04-08 07:48:32 -05:00
Lev Morgan 215444ef80 Added document wrapping command
Added a new command, site secrets wrap, to wrap bare files (e.g. pem or
crt) in a PeglegManagedDocument and optionally encrypt them.

Change-Id: I12689275c8e5a8854496fd6bbf69ce6e7cd9ad47
2019-03-27 22:20:45 -05:00
Alexander Hughes 671b77f6a7 Add CLI generation of salt
Salts and Passphrases are both strings used in cryptography.  This patch:
1. Adds CLI generation of salt
2. Adds unit test for CLI generation of salt
3. Updates passphrase.py code to be more generic as it is used to generate
both a passphrase and a salt
4. Update name of passphrase.py to be more generic
5. Update all references to, and tests of passphrase.py
6. Add documentation for CLI generation of salt

Co-Authored-By: chittibabu <cg329x@att.com>

Change-Id: I71858d63a2846290d22be96686ccfea3ba8aa6c0
2019-02-20 16:51:01 +00:00
Alexander Hughes 4b00a4340c Add CLI passphrase generation
1. Add support to pegleg to generate a passphrase from CLI
2. Update unit test to ensure encryption/decryption supports passphrase rotation
3. Update order of import statements to satisfy pep8
4. Add unit test for CLI passphrase generation
5. Resolve merge conflicts via rebase

Change-Id: I5cb9e41b2f0fac2451bd2b74f33c48cda417c22d
2019-02-04 12:32:39 -06:00
pallav b79d5b7a98 CLI capability to generate and encrypt passphrases
1. Adds the passphrases generation capability in Pegleg CLI,
so that pegleg can generation random passwords based on a
specification declared in pegleg/PassphrasesCatalog documents
2. Pegleg also wraps the generated passphrase documents in
pegleg managed documents, and encrypts the data.
3. Adds unit test cases for passphrase generation.
4. Updates pegleg CLI document.

Change-Id: I21d7668788cc24a8e0cc9cb0fb11df97600d0090
2019-01-29 16:24:31 -06:00
Felipe Monteiro 2a8d2638b3 pki: Port Promenade's PKI catalog into Pegleg
This patch set implements the PKICatalog [0] requirements
as well as PeglegManagedDocument [1] generation requirements
outlined in the spec [2].

Included in this patch set:

* New CLI entry point called "pegleg site secrets generate-pki"
* PeglegManagedDocument generation logic in
  engine.cache.managed_document
* Refactored PKICatalog logic in engine.cache.pki_catalog derived
  from the Promenade PKI implementation [3], responsible for
  generating certificates, CAs, and keypairs
* Refactored PKIGenerator logic in engine.cache.pki_generator
  derived from Promenade Generator implementation [4],
  responsible for reading in pegleg/PKICatalog/v1 documents (as
  well as promenade/PKICatalog/v1 documents for backwards
  compatibility) and generating required secrets and storing
  them into the paths specified under [0]
* Unit tests for all of the above [5]
* Example pki-catalog.yaml document under pegleg/site_yamls
* Validation schema for pki-catalog.yaml (TODO: implement
  validation logic here: [6])
* Updates to CLI documentation and inclusion of PKICatalog
  and PeglegManagedDocument documentation
* Documentation updates with PKI information [7]

TODO (in follow-up patch sets):

* Expand on overview documentation to include new Pegleg
  responsibilities
* Allow the original repository (not the copied one) to
  be the destination where the secrets are written to
* Finish up cert expiry/revocation logic

[0] https://airship-specs.readthedocs.io/en/latest/specs/approved/pegleg-secrets.html#document-generation
[1] https://airship-specs.readthedocs.io/en/latest/specs/approved/pegleg-secrets.html#peglegmanageddocument
[2] https://airship-specs.readthedocs.io/en/latest/specs/approved/pegleg-secrets.html
[3] https://github.com/openstack/airship-promenade/blob/master/promenade/pki.py
[4] https://github.com/openstack/airship-promenade/blob/master/promenade/generator.py
[5] https://review.openstack.org/#/c/611739/
[6] https://review.openstack.org/#/c/608159/
[7] https://review.openstack.org/#/c/611738/

Change-Id: I3010d04cac6d22c656d144f0dafeaa5e19a13068
2019-01-15 13:29:21 -06:00
Tin Lam 1a325a400b Add hacking extension
This patch set adds hacking rule to pegleg and fixes outstanding non-
docstring related violations.

Change-Id: I5bb5e78c211f24cf95669124bfcf9603bea8bf15
Signed-off-by: Tin Lam <tin@irrational.io>
2019-01-01 00:18:03 -06:00
Tin Lam a3da86e311 Flake8 fix
This removes all PEP8 ignores and places in default settings for flake8.

Change-Id: I3c4df02dea959dfe58f44e7c0e0ac58078a81abc
Signed-off-by: Tin Lam <tin@irrational.io>
2018-11-13 14:50:42 -06:00
Ahmad Mahmoudi fb8e6f73ac Update decrypt secrets to return a list of docs
1. Added the method to decrypt a secret file and return its contents
as a list of documents (instead of printing out the file content).
2. Added clarifications for a encrypt and decrypt commands.

Change-Id: I77bce21be214c880c8413f5e6a2d0c2d1993fc8e
2018-11-06 00:22:10 -06:00
Ahmad Mahmoudi eb0deeb9e5 Pegleg encryption of site secrets
Added secret encryption/decryption to pegleg cli.

Change-Id: I95b993748d99fc4398eee1d1c59e74f382497f74
2018-10-30 16:53:51 +00:00