Skip to content
This repository was archived by the owner on Nov 25, 2024. It is now read-only.

Commit 67aecdc

Browse files
committed
feat: python refactor
1 parent d01123d commit 67aecdc

7 files changed

+97
-17
lines changed

.pre-commit-config.yaml

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
# vim:ff=unix ts=2 sw=2 ai expandtab
22
---
33
repos:
4-
- repo: https://github.com/pre-commit/pre-commit-hooks
5-
rev: v3.3.0
6-
hooks:
7-
- id: check-added-large-files
8-
- id: check-merge-conflict
9-
- id: detect-private-key
10-
- id: trailing-whitespace
11-
- repo: https://github.com/adrienverge/yamllint
12-
rev: v1.25.0
13-
hooks:
14-
- id: yamllint
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
5+
rev: v3.3.0
6+
hooks:
7+
- id: check-added-large-files
8+
- id: check-merge-conflict
9+
- id: detect-private-key
10+
- id: trailing-whitespace
11+
- repo: https://github.com/adrienverge/yamllint
12+
rev: v1.26.1
13+
hooks:
14+
- id: yamllint
15+
- repo: git://github.com/Lucas-C/pre-commit-hooks
16+
rev: v1.1.9
17+
hooks:
18+
- id: forbid-crlf
19+
- id: remove-crlf
20+
- id: forbid-tabs
21+
- id: remove-tabs

.pre-commit-hooks.yaml

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
---
2-
- id: sops-encrypted-check
3-
name: Verify sops files
4-
description: Verify that sops files are encrypted.
5-
language: script
1+
- id: forbid-secrets
2+
name: Check for unencrypted Kubernetes secrets in manifest files
3+
description: "Forbid files containing unencrypted Kubernetes secrets to be commited"
4+
entry: forbid_secrets
5+
language: python
66
files: ((^|/)*.(ya?ml)$)
7-
entry: find-unencrypted-secrets.sh

.pylintrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[MESSAGES CONTROL]
2+
disable = bad-continuation, duplicate-code, import-error, missing-docstring, multiple-imports
3+
4+
[FORMAT]
5+
max-line-length = 150

dev-requirements.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pre-commit
2+
pytest
3+
pytest-cov
4+
coverage

hooks/forbid_secrets.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from __future__ import print_function
2+
3+
import argparse
4+
import re
5+
import sys
6+
7+
SECRET_REGEX = r"kind:\ssecret"
8+
SOPS_REGEX = r"ENC.AES256"
9+
10+
def contains_secret(filename):
11+
with open(filename, mode="r") as file_checked:
12+
lines = file_checked.read()
13+
kubernetes_secret = re.findall(SECRET_REGEX, lines, re.IGNORECASE)
14+
if kubernetes_secret:
15+
sops_secret = re.findall(SOPS_REGEX, lines, re.IGNORECASE)
16+
if not sops_secret:
17+
return True
18+
return False
19+
20+
def main(argv=None):
21+
parser = argparse.ArgumentParser()
22+
parser.add_argument('filenames', nargs='*', help='filenames to check')
23+
args = parser.parse_args(argv)
24+
files_with_secrets = [f for f in args.filenames if contains_secret(f)]
25+
return_code = 0
26+
for file_with_secrets in files_with_secrets:
27+
print('Unencrypted Kubernetes secret detected in file: {0}'.format(file_with_secrets))
28+
return_code = 1
29+
return return_code
30+
31+
if __name__ == '__main__':
32+
sys.exit(main(sys.argv[1:]))

pytest.ini

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
addopts = --cov=pre_commit_hooks --cov-report term-missing

setup.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from setuptools import find_packages, setup
2+
3+
setup(
4+
name='sops-pre-commit',
5+
description='Check for unencrypted Kubernetes secrets in manifest files',
6+
url='https://github.com/k8s-at-home/sops-pre-commit',
7+
version='1.2.0',
8+
9+
author='Devin Buhl',
10+
author_email='[email protected]',
11+
12+
platforms='linux',
13+
classifiers=[
14+
'License :: OSI Approved :: MIT License',
15+
'Programming Language :: Python :: 2',
16+
'Programming Language :: Python :: 2.6',
17+
'Programming Language :: Python :: 2.7',
18+
'Programming Language :: Python :: 3',
19+
'Programming Language :: Python :: 3.3',
20+
'Programming Language :: Python :: 3.4',
21+
'Programming Language :: Python :: Implementation :: CPython',
22+
'Programming Language :: Python :: Implementation :: PyPy',
23+
],
24+
25+
packages=find_packages('.'),
26+
entry_points={
27+
'console_scripts': [
28+
'forbid_secrets = hooks.forbid_secrets:main',
29+
],
30+
},
31+
)

0 commit comments

Comments
 (0)