Skip to main content

Python SDK for Nightfall

Project description

Nightfall Python SDK

Embed Nightfall scanning and detection functionality into Python applications

PyPI version

Features

This SDK provides Python functions for interacting with the Nightfall API. It allows you to add functionality to your applications to scan plain text and files in order to detect different categories of information. You can leverage any of the detectors in Nightfall's pre-built library, or you may programmatically define your own custom detectors.

Additionally, this library provides convenience features such as encapsulating the steps to chunk and upload files.

To obtain an API Key, login to the Nightfall dashboard and click the section titled "Manage API Keys".

See our developer documentation for more details about integrating with the Nightfall API.

Dependencies

The Nightfall Python SDK requires Python 3.7 or later.

For a full list of external dependencies please consult setup.py.

Installation

pip install nightfall

Usage

Scanning Plain Text

Nightfall provides pre-built detector types, covering data types ranging from PII to PHI to credentials. The following snippet shows an example of how to scan using pre-built detectors.

Sample Code

from nightfall import Confidence, DetectionRule, Detector, Nightfall

# By default, the client reads the API key from the environment variable NIGHTFALL_API_KEY
nightfall = Nightfall()

# A rule contains a set of detectors to scan with
detection_rule = DetectionRule([
    Detector(min_confidence=Confidence.LIKELY, nightfall_detector="CREDIT_CARD_NUMBER"),
    Detector(min_confidence=Confidence.POSSIBLE, nightfall_detector="US_SOCIAL_SECURITY_NUMBER"),
])

findings, _ = nightfall.scan_text(
        ["hello world", "my SSN is 678-99-8212", "4242-4242-4242-4242"],
        [detection_rule]
)

print(findings)

Scanning Files

Scanning common file types like PDF's or office documents typically requires cumbersome text extraction methods like OCR.

Rather than implementing this functionality yourself, the Nightfall API allows you to upload the original files, and then we'll handle the heavy lifting.

The file upload process is implemented as a series of requests to upload the file in chunks. The library provides a single method that wraps the steps required to upload your file. Please refer to the API Reference for more details.

The file is uploaded synchronously, but as files can be arbitrarily large, the scan itself is conducted asynchronously. The results from the scan are delivered by webhook; for more information about setting up a webhook server, refer to the docs.

Sample Code

from nightfall import Confidence, DetectionRule, Detector, Nightfall

# By default, the client reads the API key from the environment variable NIGHTFALL_API_KEY
nightfall = Nightfall()

# A rule contains a set of detectors to scan with
detection_rule = DetectionRule([
    Detector(min_confidence=Confidence.LIKELY, nightfall_detector="CREDIT_CARD_NUMBER"),
    Detector(min_confidence=Confidence.POSSIBLE, nightfall_detector="US_SOCIAL_SECURITY_NUMBER"),
])


# Upload the file and start the scan.
# These are conducted asynchronously, so provide a webhook route to an HTTPS server to send results to.
id, message = nightfall.scan_file(
    "./super-secret-credit-cards.pdf",
    "https://my-service.com/nightfall/listener",
    detection_rules=[detection_rule]
)
print("started scan", id, message)

Contributing

Contributions are welcome! Open a pull request to fix a bug, or open an issue to discuss a new feature or change. Please adhere to the linting criteria expected by flake8, and be sure to add unit tests for any new functionality you add.

Refer to CONTRIBUTING.md for the full details.

License

This code is licensed under the terms of the MIT License. See here for more information.

Please create an issue with a description of your problem, or open a pull request with the fix.

Development

Installing Development Dependencies

If you want to hack on this project, you should set up your local development environment with the following commands:

  1. Fork and clone this repo and open a terminal with the root of this repository in your working directory.
  2. Create and activate a virtualenv python3 -m venv venv && source venv/bin/activate
  3. Install development dependencies with pip install -r dev-requirements.txt
  4. Install an editable version of this package pip install -e .

Run Unit Tests

Unit and Integration tests can be found in the tests/ directory. You can run them with pytest. Be sure to have NIGHTFALL_API_KEY set as an environment variable before running the tests.

View Code Coverage

You can view the code coverage report by running coverage html and python3 -m http.server --directory htmlcov after running the unit tests.

Creating a Release

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.

The steps to do this are:

  1. Add what changed to the CHANGELOG file
  2. Update the version in setup.py
  3. Commit changes and push to the main branch.
  4. Create a new release in the GitHub UI.
  5. Observe the release action succeed and see the latest version of this library on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nightfall-1.1.1.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

nightfall-1.1.1-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file nightfall-1.1.1.tar.gz.

File metadata

  • Download URL: nightfall-1.1.1.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for nightfall-1.1.1.tar.gz
Algorithm Hash digest
SHA256 9a1535d43779893de7a3005ed8547683e9513f7b8be386ca58cee3d1a305d555
MD5 50292bea2c84d9f0ad6f94f480464fda
BLAKE2b-256 b985abeb78280ef05e6e01a3bccafd3a163b1294e5a59da1305f651bbd3c24c3

See more details on using hashes here.

File details

Details for the file nightfall-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: nightfall-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for nightfall-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ff588e57428adbf2ccabea85477ca3f370c9fc3d164d17e1614a71c417597586
MD5 32c8e8f660cae7e178ad1f89325ac220
BLAKE2b-256 d0ddcec421ff885e01b346e61ac05a2cffd80755ad28c1b4f7bd1e2cdb9e33c8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page