This is a guide to setting up and configuring PyUp to scan your Gitlab repositories for dependency security vulnerabilities. This enables you to configure security and compliance scans on your repositories on new commits, new branches, pull requests, and more.
You can set up PyUp to run security scans on your Python repositories in Gitlab using Gitlab Pipelines.
To scan any systems for security vulnerabilities you first need a PyUp API key. You can create a PyUp account and get your API key here.
Gitlab pipelines are an easy and powerful way to run CI/CD processes on your codebases managed on Gitlab. Adding PyUp security scans to your repositories is as easy as adding a few lines of code to your Gitlab pipeline configuration file to install Safety (our command-line tool) and then run a Safety scan.
We've created some full pipeline examples below if you don't have one set up yet. If you need help configuring your pipeline, you can read more on getting startup with Gitlab pipelines.
Gitlab pipelines are configured using a
.gitlab-ci.yml YAML file at the root of your Gitlab repository. Here is an example YAML file that installs and runs Safety to scan your Python environment for security vulnerabilities.
# PyUp Security Scans Template # This template allows you to run security scans on your Python dependencies. # The workflow allows running tests on the default branch. image: python:latest # Change pip's cache directory to be inside the project directory since we can # only cache local items. variables: PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip" cache: paths: - .cache/pip - venv/ before_script: - python --version # For debugging - pip install virtualenv - virtualenv venv - source venv/bin/activate - pip install safety run: script: # Install your requirements. Alternatively install via Poetry or Pipenv - pip install -r requirements.txt # Run Safety's environment scan - safety check --key $PYUP_API_KEY
Your pipeline YAML file will likely end up running other tests and actions and deployments. All you have to do to ensure that Safety is scanning your dependencies for security vulnerabilities is to ensure that the following code (script) is in your YAML file amongst your other tests and scripts that are running.
... script: # Install Safety - PyUp's command-line tool - pip install safety # Install your Python dependencies as per usual. # This example uses requirements.txt and pip, but you may use Poetry with its Pipfiles, or pipenv with its pyproject.toml file. - pip install -r requirements.txt # Run safety to scan the local Python environment. This will scan all installed dependencies, including any transitive dependncies that get installed during your installation - safety check --key $PYUP_API_KEY
Your safety script requires the PyUP API key to connect to PyUp and get the latest commercial vulnerability database. To link up this API key to the $PYUP_API_KEY variable defined in your pipeline YAML file (example above), you need to add your PyUp API key as a Gitlab CI/CD environment variable. To do this, navigate to your repository on Gitlab, go to your project’s Settings > CI/CD and expand the Variables section.
Once added, the new variable should display like the screenshot below on the Gitlab repository variable page:
That's it! You now have a fully working Gitlab CI/CD pipeline that will run and scan your Python dependencies for security vulnerabilities on new pushes and pull requests using PyUp's commercial vulnerability database.
If there is a vulnerability found Safety will return a non-zero exit code and fail the test. You can then see the pipeline's output in Gitlab to see what Safety found and how to patch the vulnerabilities. Here is our example running on a new pull request:
There are many more configuration options on Gitlab CI/CD Pipelines. For example, you can set up this Pipeline to only run on certain branches, or run when other conditions are met. You can also configure it to run periodically using a cron so that your repository is scanned for security vulnerabilities every hour or every day, not just when new code is committed.
You can read more about Gitlab pipelines on their documentation page.
PyUp's Safety Command-line Tool
These scans use PyUp's Safety Command-Line tool, which has many options and configurations to meet your needs. Instead of scanning your local environment after you've installed your dependencies, you can also configure it to scan specific requirements files, output different formats, or even scan for license compliance issues.
You can read more about Safety and how to use it on its Github page.
Updated about 2 months ago