Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre-commit config #293

Merged
merged 7 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
ignore = E501, W503, E203
exclude = .git,__pycache__,docs/conf.py,build,dist,opencanary/modules/des.py
max-complexity = 10
min_python_version = 3.9
13 changes: 13 additions & 0 deletions .github/workflows/opencanary_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ on:
- "pull_request"

jobs:
precommit_tests:
runs-on: "ubuntu-20.04"
steps:
- name: "Check out repository code"
uses: "actions/checkout@v3"
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install pre-commit
run: pip install pre-commit
- name: Check pre-commit is happy
run: pre-commit run --all-files
opencanary_tests:
strategy:
matrix:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
steps:
# retrieve your distributions here
- name: Set up Python
- name: Set up Python
uses: actions/setup-python@v3
- name: "Check out repository code"
uses: "actions/checkout@v3"
Expand All @@ -37,9 +37,9 @@ jobs:
else
echo "Versions do not match - not publishing"
echo "Opencanary version is: $version_to_release"
echo "Git tag is: $tag_name -> $tag_name_without_v"
echo "Git tag is: $tag_name -> $tag_name_without_v"
exit 1
fi

- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
29 changes: 29 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
exclude: >
(?x)^(
dist/|
.devcontainer/devcontainer.json
)
fail_fast: true
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-docstring-first
- id: check-json
- id: check-added-large-files
- id: check-yaml
- id: debug-statements
# - id: no-commit-to-branch
# # GitHub only allows branch protection for teams or enterprise.
# args: ['--pattern', '^(?!T\d+.*)']
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
hooks:
- id: flake8
additional_dependencies: [flake8-typing-imports==1.12.0]
2 changes: 1 addition & 1 deletion .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ sphinx:
# Optionally declare the Python requirements required to build your docs
python:
install:
- requirements: docs/requirements.txt
- requirements: docs/requirements.txt
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ recursive-exclude docs *
exclude Dockerfile.latest
exclude Dockerfile.*
exclude docker-compose.yml
exclude .gitignore
exclude .gitignore
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ NOTE: The portscan module is automatically disabled for Dockerised OpenCanary.

> Requires [Docker](https://docs.docker.com/get-docker/) installed.

NOTE: The portscan module is automatically disabled for Dockerised OpenCanary.
NOTE: The portscan module is automatically disabled for Dockerised OpenCanary.

1. Edit the `data/.opencanary.conf` file to enable, disable or customize the services that will run.

Expand Down
3 changes: 1 addition & 2 deletions bin/opencanary-correlator
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
from opencanary.correlator import main

if __name__ == "__main__":
main()

main()
103 changes: 54 additions & 49 deletions bin/opencanary.tac
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import traceback
# import warnings
# warnings.filterwarnings("ignore", category=DeprecationWarning)
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
import sys
from twisted.application import service
from twisted.application import internet
from twisted.internet.protocol import Factory
from pkg_resources import iter_entry_points

from opencanary.config import config, is_docker
Expand All @@ -29,8 +23,16 @@ from opencanary.modules.redis import CanaryRedis
from opencanary.modules.tcpbanner import CanaryTCPBanner
from opencanary.modules.rdp import CanaryRDP

#from opencanary.modules.example0 import CanaryExample0
#from opencanary.modules.example1 import CanaryExample1

def warn(*args, **kwargs):
pass


warnings.warn = warn


# from opencanary.modules.example0 import CanaryExample0
# from opencanary.modules.example1 import CanaryExample1

ENTRYPOINT = "canary.usermodule"
MODULES = [
Expand All @@ -54,91 +56,94 @@ MODULES = [
# CanaryExample1,
]

if config.moduleEnabled('snmp'):
if config.moduleEnabled("snmp"):
try:
#Module need Scapy, but the rest of OpenCanary doesn't
# Module need Scapy, but the rest of OpenCanary doesn't
from opencanary.modules.snmp import CanarySNMP

MODULES.append(CanarySNMP)
except ImportError:
print("Can't import SNMP. Please ensure you have Scapy installed.")
pass

# NB: imports below depend on inotify, only available on linux
import sys
if sys.platform.startswith("linux"):
from opencanary.modules.samba import CanarySamba

MODULES.append(CanarySamba)
if config.moduleEnabled('portscan') and is_docker():
if config.moduleEnabled("portscan") and is_docker():
# Remove portscan if running in DOCKER (specified in Dockerfile)
print("Can't use portscan in Docker. Portscan module disabled.")
else:
from opencanary.modules.portscan import CanaryPortscan

MODULES.append(CanaryPortscan)


logger = getLogger(config)

def start_mod(application, klass):

def start_mod(application, klass): # noqa: C901
try:
obj = klass(config=config, logger=logger)
except Exception as e:
err = 'Failed to instantiate instance of class %s in %s. %s' % (
except Exception:
err = "Failed to instantiate instance of class %s in %s. %s" % (
klass.__name__,
klass.__module__,
traceback.format_exc()
traceback.format_exc(),
)
logMsg({'logdata': err})
logMsg({"logdata": err})
return

if hasattr(obj, 'startYourEngines'):
if hasattr(obj, "startYourEngines"):
try:
obj.startYourEngines()
msg = 'Ran startYourEngines on class %s in %s' % (
msg = "Ran startYourEngines on class %s in %s" % (
klass.__name__,
klass.__module__
)
logMsg({'logdata': msg})
klass.__module__,
)
logMsg({"logdata": msg})

except Exception as e:
err = 'Failed to run startYourEngines on %s in %s. %s' % (
except Exception:
err = "Failed to run startYourEngines on %s in %s. %s" % (
klass.__name__,
klass.__module__,
traceback.format_exc()
traceback.format_exc(),
)
logMsg({'logdata': err})
elif hasattr(obj, 'getService'):
logMsg({"logdata": err})
elif hasattr(obj, "getService"):
try:
service = obj.getService()
if not isinstance(service, list):
service = [service]
for s in service:
s.setServiceParent(application)
msg = 'Added service from class %s in %s to fake' % (
msg = "Added service from class %s in %s to fake" % (
klass.__name__,
klass.__module__
)
logMsg({'logdata': msg})
except Exception as e:
err = 'Failed to add service from class %s in %s. %s' % (
klass.__module__,
)
logMsg({"logdata": msg})
except Exception:
err = "Failed to add service from class %s in %s. %s" % (
klass.__name__,
klass.__module__,
traceback.format_exc()
traceback.format_exc(),
)
logMsg({'logdata': err})
logMsg({"logdata": err})
else:
err = 'The class %s in %s does not have any required starting method.' % (
err = "The class %s in %s does not have any required starting method." % (
klass.__name__,
klass.__module__
klass.__module__,
)
logMsg({'logdata': err})
logMsg({"logdata": err})


def logMsg(msg):
data = {}
# data['src_host'] = device_name
# data['dst_host'] = node_id
data['logdata'] = {'msg': msg}
data["logdata"] = {"msg": msg}
logger.log(data, retry=False)


application = service.Application("opencanaryd")

# List of modules to start
Expand All @@ -150,18 +155,18 @@ for ep in iter_entry_points(ENTRYPOINT):
try:
klass = ep.load(require=False)
start_modules.append(klass)
except Exception as e:
err = 'Failed to load class from the entrypoint: %s. %s' % (
except Exception:
err = "Failed to load class from the entrypoint: %s. %s" % (
str(ep),
traceback.format_exc()
)
logMsg({'logdata': err})
traceback.format_exc(),
)
logMsg({"logdata": err})

# Add only enabled modules
start_modules.extend(filter(lambda m: config.moduleEnabled(m.NAME), MODULES))

for klass in start_modules:
start_mod(application, klass)

msg = 'Canary running!!!'
logMsg({'logdata': msg})
msg = "Canary running!!!"
logMsg({"logdata": msg})
Loading