Skip to content

Commit c45e416

Browse files
committed
Create core module
* basic python package supporting the scan endpoint * unit tests * docs via Sphinx * CI/CD via GitHub Actions and PyPI
0 parents  commit c45e416

22 files changed

+664
-0
lines changed

.coveragerc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[run]
2+
omit =
3+
env/*
4+
venv/*
5+
tests/*
6+
*/__init__.py
7+
setup.py
8+
9+
source =
10+
.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Nightfall Package Deploy to PyPI
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
test-and-publish:
9+
name: Publish to PyPI
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
- name: Set up Python
14+
uses: actions/setup-python@v2
15+
with:
16+
python-version: 3.9
17+
- name: Install dependencies
18+
run: |
19+
python -m pip install --upgrade pip
20+
pip install -r dev-requirements.txt
21+
pip install -e .
22+
- name: Test with unittest
23+
env:
24+
NIGHTFALL_CONDITION_SET: ${{ secrets.NIGHTFALL_CONDITION_SET }}
25+
NIGHTFALL_TOKEN: ${{ secrets.NIGHTFALL_TOKEN }}
26+
run: |
27+
python -m unittest discover
28+
- name: Build a binary wheel and source tarball
29+
run: |
30+
python -m build --sdist --wheel --outdir dist/
31+
- name: Publish distribution to PyPI
32+
uses: pypa/gh-action-pypi-publish@master
33+
with:
34+
password: ${{ secrets.PYPI_API_TOKEN }}

.github/workflows/build-test.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Nightfall Package Tests
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
matrix:
10+
python-version: [3.6, 3.7, 3.8, 3.9]
11+
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: Set up Python ${{ matrix.python-version }}
15+
uses: actions/setup-python@v2
16+
with:
17+
python-version: ${{ matrix.python-version }}
18+
- name: Install dependencies and package
19+
run: |
20+
python -m pip install --upgrade pip
21+
pip install -r dev-requirements.txt
22+
pip install -e .
23+
- name: Lint with flake8
24+
run: |
25+
# stop the build if there are Python syntax errors or undefined names
26+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
27+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
28+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
29+
- name: Test with unittest
30+
env:
31+
NIGHTFALL_CONDITION_SET: ${{ secrets.NIGHTFALL_CONDITION_SET }}
32+
NIGHTFALL_TOKEN: ${{ secrets.NIGHTFALL_TOKEN }}
33+
run: |
34+
python -m unittest discover

.gitignore

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
98+
__pypackages__/
99+
100+
# Celery stuff
101+
celerybeat-schedule
102+
celerybeat.pid
103+
104+
# SageMath parsed files
105+
*.sage.py
106+
107+
# Environments
108+
.env
109+
.venv
110+
env/
111+
venv/
112+
ENV/
113+
env.bak/
114+
venv.bak/
115+
116+
# Spyder project settings
117+
.spyderproject
118+
.spyproject
119+
120+
# Rope project settings
121+
.ropeproject
122+
123+
# mkdocs documentation
124+
/site
125+
126+
# mypy
127+
.mypy_cache/
128+
.dmypy.json
129+
dmypy.json
130+
131+
# Pyre type checker
132+
.pyre/
133+
134+
# pytype static type analyzer
135+
.pytype/
136+
137+
# Cython debug symbols
138+
cython_debug/

CHANGELOG

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Changelog
2+
=========
3+
4+
Here you can see the full list of changes between each Nightfall release.
5+
6+
Version 0.1.0
7+
-------------
8+
9+
.. note::
10+
This is an initial Beta release.
11+
12+
Released on June 13, 2021
13+
14+
- Basic project tooling put into place.
15+
- Continuous Integration with GitHub Actions
16+
- Packaging and uploading to PyPI
17+
- Code Coverage
18+
- Testing with unittest
19+
- Basic Documentation in place
20+
- Add support for using the API with a token
21+
- Add support for scan API endpoint
22+

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Nightfall
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.PHONY: help clean dev docs serve-docs package test
2+
3+
help:
4+
@echo "This project assumes that an active Python virtualenv is present."
5+
@echo "The following make targets are available:"
6+
@echo " dev install all deps for dev env"
7+
@echo " docs create pydocs for all relveant modules"
8+
@echo " serve-docs serve generated documentation locally"
9+
@echo " test run all tests with coverage"
10+
11+
clean:
12+
rm -rf dist/*
13+
14+
dev:
15+
pip install -r dev-requirements.txt
16+
pip install -e .
17+
18+
docs:
19+
$(MAKE) -C docs html
20+
21+
serve-docs:
22+
python3 -m http.server --directory docs/_build/html
23+
24+
package:
25+
python -m build --sdist --wheel --outdir dist/
26+
27+
test:
28+
coverage run -m unittest discover
29+
coverage html

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Nightfall Python SDK
2+
3+
This is a python SDK for working with the Nightfall API.
4+
5+
## Installation
6+
7+
This module requires Python 3.6 or higher.
8+
9+
```
10+
pip install nightfall
11+
```
12+
13+
## Quickstart
14+
15+
Make a new [API Token](https://app.nightfall.ai/api/) and [Detection Rule Set](https://app.nightfall.ai/detection-engine/detection-rules) in Nightfall and store the values as environment variables.
16+
17+
```
18+
from nightfall import Api
19+
20+
nightfall = Api(
21+
os.getenv('NIGHTFALL_TOKEN'),
22+
os.getenv('NIGHTFALL_CONDITION_SET')
23+
)
24+
25+
response = nightfall.scan(['test string'])
26+
27+
findings = response.json()
28+
print(findings)
29+
```
30+
31+
## Contributing
32+
33+
Please create an issue with a description of your problem, or open a pull request with the fix.
34+
35+
## Development
36+
37+
### Installing Development Dependencies
38+
39+
If you want to hack on this project, you should set up your local development
40+
environment with the following commands:
41+
42+
1. Fork and clone this repo and open a terminal with the root of this repository in your working directory.
43+
1. Create and activate a virtualenv `python3 -m venv venv && source venv/bin/activate`
44+
1. Install development dependencies with `pip install -r dev-requirements.txt`
45+
1. Install an editable version of this package `pip install -e .`
46+
47+
### Run Unit Tests
48+
49+
Unit tests can be found in the `tests/` directory. You can run them with `make test`. Be sure to have `NIGHTFALL_TOKEN` and `NIGHTFALL_CONDITION_SET` set as environment variables before running the tests.
50+
51+
### View Code Coverage
52+
53+
You can view the code coverage report by running `coverage html` and `python3 -m http.server --directory htmlcov` after running the unit tests.
54+
55+
### SDK Documentation
56+
57+
The SDK is documented using the Sphinx library. You can generatre the developer documentation using `make docs` and preview it on `localhost:8000` by running `python3 -m http.server --directory docs/_build/html`.
58+
59+
### Creating a Release
60+
61+
Releases are automatically published to PyPI using GitHub Actions. Creating a release in GitHub will trigger a new build that will publish the latest version of this library to [PyPI](https://pypi.org/project/nightfall/).
62+
63+
The steps to do this are:
64+
65+
1. Add what changed to the CHANGELOG file.
66+
2. Update the version in `setup.py`
67+
3. Commit changes and push to the main branch.
68+
4. Create a new release in the GitHub UI.
69+
5. Observe the release action succeed and see the latest version of this library on PyPI.
70+
## License
71+
72+
MIT
73+
74+

dev-requirements.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pylint
2+
doc8
3+
coverage
4+
codecov
5+
twine
6+
sphinx
7+
sphinx-autobuild
8+
sphinx_rtd_theme
9+
build
10+
flake8

0 commit comments

Comments
 (0)