From 4d09c89837673acc804ebbb599f7a40c9c41efed Mon Sep 17 00:00:00 2001 From: Noa Resare Date: Wed, 5 Mar 2014 10:44:09 +0100 Subject: [PATCH] some cleanup from pawel.rozlach@gmail.com --- LICENSE | 202 +++++++++++++++++++ README | 29 --- README.md | 6 + debian/changelog | 4 +- debian/control | 6 +- debian/rules | 3 - doc/README.md | 50 +++++ doc/USAGE.md | 103 ++++++++++ doc/index.asciidoc | 4 - setup.py | 12 +- spotify_certcheck/__init__.py | 2 - test/.coveragerc | 2 +- test/fabric/certcheck_mopconfig.yml | 9 +- test/moduletests/certcheck/test_certcheck.py | 6 +- 14 files changed, 375 insertions(+), 63 deletions(-) create mode 100644 LICENSE delete mode 100644 README create mode 100644 README.md create mode 100644 doc/README.md create mode 100644 doc/USAGE.md delete mode 100644 doc/index.asciidoc delete mode 100644 spotify_certcheck/__init__.py diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README b/README deleted file mode 100644 index c14bcaf..0000000 --- a/README +++ /dev/null @@ -1,29 +0,0 @@ - SPOTIFY PROJECT - ----------------- - - -Copyright (c) 2013 Spotify AB - -THIS REPOSITORY IS THE PROPERTY OF SPOTIFY AB -UNAUTHORIZED ACCESS TO THIS DATA IS STRICTLY PROHIBITED. - -README Template Version 7 - -Project : certcheck -Maintainer : Pawel Rozlach -Guild : (the guild name this project belongs to) <(the-guild@spotify.com)> -Dependencies: (project names, comma separated) -Related : (project names, comma separated) -System : (name of system this project belongs to, if any) -Environment : (Python, C/C++/Objective C, Java, Ruby, PHP) - -DESCRIPTION ------------- - -( A one paragraph description that will be used to describe the project. (e.g. on the techdoc index page) ) - -DOCUMENTATION -------------- - -Documentation for this project should be found in the doc directory. -Information on how Spotify projects should be documented is available on https://techdoc.spotify.net/projbooks/docs/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e73e833 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +Copyright (c) 2013 Spotify AB +Certcheck - Simple certificate checker +Documentation for this project should be found in the doc directory. + +Currently this is a dump of an internal tool; YMMV trying to get this to work +outside Spotify. diff --git a/debian/changelog b/debian/changelog index 54826b0..0a6c4a3 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,11 +1,11 @@ -spotify-certcheck (0.2.0) stable; urgency=low +certcheck (0.2.0) stable; urgency=low * Git integration. * Drop scanning directories in favour of direct git interfacing. -- Pawel Rozlach Mon, 18 Sep 2013 14:33:43 +0000 -spotify-certcheck (0.1.0) unstable; urgency=low +certcheck (0.1.0) unstable; urgency=low * Initial release. diff --git a/debian/control b/debian/control index abad135..f0f82ad 100644 --- a/debian/control +++ b/debian/control @@ -1,15 +1,15 @@ -Source: spotify-certcheck +Source: certcheck Section: non-free/net Priority: extra Maintainer: Pawel Rozlach -Build-Depends: python (>= 2.6.6-3~), debhelper (>= 8), spotify-common, +Build-Depends: python (>= 2.6.6-3~), debhelper (>= 8), python-coverage, openssl, python-openssl, python-bernhard, python-argparse, python-protobuf, python-unittest2, python-yaml, python-dulwich (>= 0.8), openssh-client Standards-Version: 3.9.3 X-Python-Version: >= 2.6 -Package: spotify-certcheck +Package: certcheck Version: 0.2.0 Architecture: any Depends: ${python:Depends}, python-openssl, python-bernhard, python-argparse, diff --git a/debian/rules b/debian/rules index 4f63217..ffa3e2c 100755 --- a/debian/rules +++ b/debian/rules @@ -6,6 +6,3 @@ override_dh_fixperms: dh_fixperms - # We can remove this rule when all packages have been converted to - # python-support. See #BIT-139. - find $(DESTDIR) -name top_level.txt -delete diff --git a/doc/README.md b/doc/README.md new file mode 100644 index 0000000..421e489 --- /dev/null +++ b/doc/README.md @@ -0,0 +1,50 @@ +# _Certchecker_ + +_Certchecker is a certificate expiration check capable of scanning GIT repos +and sending data on expiring/expired certificates back to the monitoring system +(currently only Riemann)._ + +## Project Setup + +In order to run certchecker you need to following dependencies installed: +- Bernhard - Riemann client library (https://github.com/banjiewen/bernhard) +- Google's protobuf library +- yaml bindings for python (http://pyyaml.org/) +- Dulwich - python implementation of GIT (https://www.samba.org/~jelmer/dulwich/docs/) +- ssh command in your PATH +- argparse library + +You can also use debian packaging rules from debian/ directory to build a deb +package. + +## Testing + +Currenlty the unittest python library is used to perform all the testing. In +test/ directory you can find: +- modules/ - modules used by unittests +- moduletests/ - the unittests themselves +- fabric/ - sample input files and test certificates temporary directories +- output_coverage_html/ - coverage tests results in a form of an html webpage +- test.py - script to start all the unittests + +All the dependencies required for performing the unittests are decribed in debian +packaging scripts and are as follows: +- unittests2 +- coverage +- python-mock +- openssl command in the PATH + +Plus all the dependencies mentioned in 'Project Setup' section. + +## Usage + +Please see the doc/USAGE.md file for details. + +## Contributing + +All patches are welcome ! Please use Github issue tracking and/or create a pull +request. + +## License + +FIXME - Put the licence here diff --git a/doc/USAGE.md b/doc/USAGE.md new file mode 100644 index 0000000..6e5c809 --- /dev/null +++ b/doc/USAGE.md @@ -0,0 +1,103 @@ +## Configuration + +Actions taken by the script are determined by its command line and the +configuration file. The command line has a build-in help system: + + +usage: certcheck.py [-h] [--version] -c CONFIG_FILE [-v] [-s] [-d] + +Simple certificate expiration check + +optional arguments: + -h, --help show this help message and exit + --version show program's version number and exit + -c CONFIG_FILE, --config-file CONFIG_FILE + Location of the configuration file + -v, --verbose Provide extra logging messages. + -s, --std-err Log to stderr instead of syslog + -d, --dont-send Do not send data to Riemann [use for debugging] + +Author: pawel.rozlach a t gmail.com + +The configuration file is a plain YAML document. It's syntax is as follows: + +--- +lockfile: /tmp/certcheck.lock +warn_treshold: 30 +critical_treshold: 15 +riemann_hosts: + static: + - 192.168.122.16:5555:udp + - 192.168.122.16:5555:tcp + by_srv: + - _riemann._tcp + - _riemann._udp +riemann_tags: + - production + - class::certcheck +repo_host: git.example.net +repo_port: 22 +repo_url: /example-repo +repo_masterbranch: refs/heads/production +repo_localdir: /tmp/certcheck-temprepo +repo_user: certcheck +repo_pubkey: ./certcheck_id_rsa +# format - dict, hash as a key, and value as a comment +# sha1sum ./certificate_to_be_ignored +ignored_certs: + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: "some VPN key" + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: "some unused certificate" + +## Operation + +The script connects to the $repo_user@$repo_host:$repo_port via SSH and clones +repository $repo_url to a *bare* repository in "$repo_tmpdir/repository". If +the repository already exists, it is only updated with newest referances. Only +$repo_masterbranch branch is pulled in along with all the objects it points to, +topic branches are not downloaded. + +The connection is established using the $repo_pubkey pubkey, and the $repo_user +itself should have very limited privileges. + +Next, the repository is scanned in search of files ending with one of the +certcheck.py:CERTIFICATE_EXTENSIONS extensions. Currently all possible +certificate extensions are listed but only ['pem', 'crt', 'cer'] are currently +supported (see certcheck.py:get_cert_expiration method). For the remaing ones +only a warning is issued. + +For each certificate found a sha1sum is computed, and if the result is found in +$ignored_certs hash, then the certificate is ignored even if it expires/exp- +ired. + +If the number of days till the certificate expires is less than $critical_tresh +(by default 15) - a "critical" partial status is generated, if it less than +$warn_tresh but more than $critical_tresh - a "warning" partial status is gene- +rated. Unsuported certificate yields an 'unknown' state and expired ones of +course the 'critical'. + +All the 'partial status' updates are agregated and each message can only ele- +vate up the final status of the metric send to Riemann. Currently, the hierar- +chy is as follows: + + (lowest)ok->warn->critical->unknown(highest) + +script errors, exceptions and unexcpected conditions result in imidiate elevation +to 'unknown' status and sending the metric to Riemann ASAP if only possible. + +IP addresses/ports of the Riemann instances can be defined in two ways: + * statically, by providing a list of riemann instances in $riemann_servers + var. The format of the list entry is hostname:port:proto. 'proto' can be one + of 'udp' or 'tcp'. + * by providing a SRV record, i.e. '_riemann._udp'. All the values + (host, port) will be resolved automatically. Protocol is chosen basing on + the SRV entry itself. + +The final metric is send to *all* Riemann instances with TTL equal to +certcheck.py:DATA_TTL == 25 hours. + + +=== Maintenance + +In order to not to let the "$repo_tmpdir/repository" repository grow endlessly +a 'git gc' command should be executed once a day by i.e. a cronjob. It should +repack all the packs and remove dangling objects. diff --git a/doc/index.asciidoc b/doc/index.asciidoc deleted file mode 100644 index a308d2b..0000000 --- a/doc/index.asciidoc +++ /dev/null @@ -1,4 +0,0 @@ -Project book for certcheck -=================================== -Pawel Rozlach prozlach@spotify.com -$Id$ \ No newline at end of file diff --git a/setup.py b/setup.py index 8f23064..d454f9f 100755 --- a/setup.py +++ b/setup.py @@ -1,17 +1,11 @@ #! /usr/bin/env python -# -*- coding: utf-8 -*- -# Copyright (c) 2013 Spotify AB -try: - from spotify.build import setup -except ImportError: - from setuptools import setup +from setuptools import setup -setup(name='spotify-certcheck', +setup(name='certcheck', version='1', author=u'Pawel Rozlach', author_email='prozlach@spotify.com', - url='https://wiki.spotify.net/wiki/Python_packaging_policy', description='Simplified certificate check', - packages=['spotify_certcheck'], + packages=['certcheck'], scripts=['bin/certcheck.py']) diff --git a/spotify_certcheck/__init__.py b/spotify_certcheck/__init__.py deleted file mode 100644 index 0aa3be7..0000000 --- a/spotify_certcheck/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright (c) 2013 Spotify AB diff --git a/test/.coveragerc b/test/.coveragerc index 962da0a..4a280d0 100644 --- a/test/.coveragerc +++ b/test/.coveragerc @@ -18,7 +18,7 @@ exclude_lines = if __name__ == .__main__.: source = ../bin/ - ../spotify_certcheck/ + ../certcheck/ [html] diff --git a/test/fabric/certcheck_mopconfig.yml b/test/fabric/certcheck_mopconfig.yml index 089b585..321cd5f 100644 --- a/test/fabric/certcheck_mopconfig.yml +++ b/test/fabric/certcheck_mopconfig.yml @@ -7,15 +7,14 @@ riemann_hosts: - 192.168.122.16:5555:udp - 192.168.122.16:5555:tcp by_srv: - - _spotify-riemann._tcp - - _spotify-riemann._udp + - _riemann._tcp + - _riemann._udp riemann_tags: - production - - site::lon - class::certcheck -repo_host: git.spotify.net +repo_host: git.example.com repo_port: 22 -repo_url: /spotify-puppet +repo_url: /sample-repo repo_masterbranch: refs/heads/production repo_localdir: /tmp/certcheck-temprepo repo_user: certcheck diff --git a/test/moduletests/certcheck/test_certcheck.py b/test/moduletests/certcheck/test_certcheck.py index 3923bf5..005b25a 100644 --- a/test/moduletests/certcheck/test_certcheck.py +++ b/test/moduletests/certcheck/test_certcheck.py @@ -20,11 +20,7 @@ import unittest2 as unittest else: import unittest -#no everyone has spotify-mock8/mock installed: -try: - import spotify.util.mock8 as mock -except ImportError: - import mock +import mock #To perform local imports first we need to fix PYTHONPATH: pwd = os.path.abspath(os.path.dirname(__file__))