# Managing Panther Content via GitHub Actions

## **Overview**

You can configure GitHub Actions to automate testing, customize detections, and upload your detection pipeline from your GitHub repository to your Panther Console. This guide will walk you through the following:

* Creating a custom workflow via GitHub Actions
* Testing your custom schemas and detections
* Uploading the schemas and detections to your Panther Console
* Customizing your GitHub Actions workflow to fit your organization's needs

See [CI/CD for Panther Content](https://docs.panther.com/~/changes/15ann7vKLltCCAGHtdQr/panther-developer-workflows/ci-cd) for information on starting your CI/CD workflow with Panther.

### Prerequisites

To get started with managing your Panther detections and schemas using GitHub Actions, you will need:

* **A Panther API Token**
  * Follow our documentation for [Creating an API Token](https://docs.panther.com/~/changes/15ann7vKLltCCAGHtdQr/api#step-1-creating-an-api-token) and ensure it has the [correct permissions](https://docs.panther.com/~/changes/15ann7vKLltCCAGHtdQr/panther-developer-workflows/ci-cd/pat#creating-an-api-token) for each command.
  * You will pass this API token as an argument to the `panther_analysis_tool` command for operations such as uploading/deleting detections, custom schemas, saved queries, and more. [See this section for usage examples.](#uploading-to-panther)
* **Your Panther API Host Name**
  * Your Panther API hostname will look like this: \
    `https://api.<your-panther-instance-name>.runpanther.net/public/graphql`
* **Your Panther API Token added as a GitHub secret under the name  `PANTHER_API_TOKEN`**
  * To add the token to Secrets, follow [GitHub's documentation: Creating encrypted secrets for a repository](https://docs.github.com/en/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository). This secret is shown later in this document as `secrets.PantherApiToken`.

{% hint style="info" %}
This guide explains how to upload to your Panther Console via GitHub Actions using Panther API keys and Github secrets. This is the recommended method if you are using GitHub Actions. You can also upload to your Panther Console directly via the `panther_analysis_tool`. For more information, see [Panther Analysis Tool Overview.](https://docs.panther.com/~/changes/15ann7vKLltCCAGHtdQr/panther-developer-workflows/ci-cd/deployment-workflows/pat)
{% endhint %}

## Configure **GitHub Actions for Panther**

### Step 1: Make use of the Panther-managed detections in the panther-analysis GitHub repo

Follow the documentation to make use of Panther-managed detections in the panther-analysis GitHub repo: [Using the Panther detections repo](https://docs.panther.com/~/changes/15ann7vKLltCCAGHtdQr/panther-developer-workflows/ci-cd/detections-repo).

### Step 2: Create a new GitHub workflow

1. Navigate to the GitHub repository where you would like to set up automation.
2. Within the GitHub repository, navigate to **Actions.**\ <img src="https://4011785613-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LgdiSWdyJcXPahGi9Rs-2910905616%2Fuploads%2FHFRgXvCJU75vAmw87VwV%2FScreen%20Shot%202022-06-14%20at%208.51.10%20AM.png?alt=media&#x26;token=7bbc8a90-7829-4659-97a6-b64689c94256" alt="The image shows the panther-analysis repo on Github. There is a red circle around the &#x22;Actions&#x22; tab. " data-size="original">
3. Click **New Workflow**.\
   ![The image shows the Panther-analysis repo in Github. There is a red circle around the "New Workflow" button on the left.](https://4011785613-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LgdiSWdyJcXPahGi9Rs-2910905616%2Fuploads%2FgV8135gsFfJGrHCa7CYF%2FScreen%20Shot%202022-06-14%20at%209.43.19%20AM.png?alt=media\&token=41388f36-b7c6-426b-8160-d84360bec1ce)
4. Click ***Set up a workflow yourself →**.*\
   ![The image shows the panther-analysis repo on Github. The "Actions" tab is open. Under "Choose a workflow," there is a red circle around "Set up a workflow yourself."](https://4011785613-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LgdiSWdyJcXPahGi9Rs-2910905616%2Fuploads%2FXumkrJSNcdxa4cS2zZL4%2FScreen%20Shot%202022-06-14%20at%209.49.27%20AM.png?alt=media\&token=879a0527-5663-4b1d-995a-3e7e325ad529)
5. On the next page, replace the default filename (`main.yml`) with a descriptive name, e.g., `panther-workflow.yml`.

### Step 3: Build a workflow to test detections and upload data

Note: This workflow assumes you have added your Panther API token as a GitHub secret under the name  `PANTHER_API_TOKEN`. If you have not yet done this, please follow the instructions under the [prerequisites](https://docs.panther.com/panther-developer-workflows/github-actions-onboarding-guide#prerequisites).

* Add the following code to the YAML file:

<details>

<summary>GitHub workflow YAML</summary>

{% code lineNumbers="true" %}

```
name: Panther Analysis CI/CD workflow

permissions:
  id-token: write
  contents: read

on:  
  push:
    branches:
      - master
    paths:
      - 'data_models/**'
      - 'lookup_tables/**'
      - 'policies/**'
      - 'queries/**'
      - 'rules/**'
      - '.github/workflows/**'

jobs: 
  run_unit_tests:    
    runs-on: ubuntu-latest
    name: Run unit tests on detections using the panther_analysis_tool
    steps:
      - name: Check out the repo
        uses: actions/checkout@v3
      
      - name: Set python version  
        uses: actions/setup-python@v4
        with:
          python-version: '3.9'
      
      - name: Install pipenv
        run: pip install pipenv
      
      - name: Install python dependencies and panther_analysis_tool
        run: make install
      
      - name: Run unit tests for all rule detections
        run: pipenv run panther_analysis_tool test --filter AnalysisType=rule
      
      - name: Run unit tests for all scheduled rule detections
        run: pipenv run panther_analysis_tool test --filter AnalysisType=scheduled_rule
      
      - name: Run unit tests for all policy detections
        run: pipenv run panther_analysis_tool test --filter AnalysisType=policy
  
  panther_analysis_tool_upload:        
    runs-on: ubuntu-latest
    name: Upload detections to panther console using panther_analysis_tool
    needs: [run_unit_tests]
    env:
      PANTHER_API_TOKEN: ${{ secrets.PANTHER_API_TOKEN }}
      PANTHER_API_HOST: "https://api.<your-panther>.runpanther.net/public/graphql"
    steps:
      - name: Checkout the repo
        uses: actions/checkout@v3
      
      - name: Set python version  
        uses: actions/setup-python@v4
        with:
          python-version: '3.9'
      
      - name: Install pipenv
        run: pip install pipenv
      
      - name: Install python dependencies and panther_analysis_tool
        run: make install
      
      - name: Upload detections to your Panther instance
        run: pipenv run panther_analysis_tool upload --skip-tests

```

{% endcode %}

</details>

* Make sure to change the environment variable `PANTHER_API_HOST` on line `51` to your Panther Instance's public GraphQL URL by replacing `<your-panther>`.&#x20;

This will run the tests you have created on your detections and then upload all your Panther content (Lookup Tables, Data Models, and detections) if they passed.&#x20;

### Step 4: Push changes

* Run `git push`.

After the Github Actions workflow is complete, the following will occur the next time you use `git push` to make changes to the folders in the `paths` section of the workflow:

* Custom detections are tested with `panther_analysis_tool`.
* Upon success, detections are uploaded to your Panther Console.

## Optional: Build a workflow for custom schemas

If you are building custom schemas, use the following YAML code to include the schemas in your workflow:

<details>

<summary>GitHub workflow YAML with schemas</summary>

{% code lineNumbers="true" %}

```
name: Panther Analysis CI/CD workflow

permissions:
  id-token: write
  contents: read

on:  
  push:
    branches:
      - main
    paths:
      - 'data_models/**'
      - 'lookup_tables/**'
      - 'policies/**'
      - 'queries/**'
      - 'rules/**'
      - '.github/workflows/**'

jobs: 
  download_pantherlog_tool:
    runs-on: ubuntu-latest
    name: Download the pantherlog tool to use for schema tests
    steps: 
      - name: Download pantherlog & unzip 
        run: curl -sSO "https://panther-community-us-east-1.s3.amazonaws.com/v1.46.0/tools/linux-amd64-pantherlog.zip" && unzip linux-amd64-pantherlog.zip
      
      - name: Create a pantherlog artifact
        uses: actions/upload-artifact@v3
        with:
          name: pantherlog
          path: pantherlog
          retention-days: 1
  
  run_schema_tests:    
    runs-on: ubuntu-latest
    name: Run schema tests with pantherlog
    needs: [download_pantherlog_tool]
    steps:
      - name: Check out the repo
        uses: actions/checkout@v3

      - name: Download Pantherlog tool from artifacts
        uses: actions/download-artifact@v3
        with: 
          name: pantherlog
      - name: Make pantherlog executable
        run: sudo chmod +x pantherlog

      - name: Perform schema tests with pantherlog
        run: ./pantherlog test ./schemas
  
  run_unit_tests:    
    runs-on: ubuntu-latest
    name: Run unit tests on detections using the panther_analysis_tool
    steps:
      - name: Check out the repo
        uses: actions/checkout@v3
      
      - name: Set python version  
        uses: actions/setup-python@v4
        with:
          python-version: '3.9'
      
      - name: Install pipenv
        run: pip install pipenv
      
      - name: Install python dependencies and panther_analysis_tool
        run: make install
      
      - name: Run unit tests for all rule detections
        run: pipenv run panther_analysis_tool test --filter AnalysisType=rule
      
      - name: Run unit tests for all scheduled rule detections
        run: pipenv run panther_analysis_tool test --filter AnalysisType=scheduled_rule
      
      - name: Run unit tests for all policy detections
        run: pipenv run panther_analysis_tool test --filter AnalysisType=policy
  
  panther_analysis_tool_upload:        
    runs-on: ubuntu-latest
    name: Upload detections to panther console using panther_analysis_tool
    needs: [download_pantherlog_tool, run_schema_tests, run_unit_tests]
    env:
      PANTHER_API_TOKEN: ${{ secrets.PANTHER_API_TOKEN }}
      PANTHER_API_HOST: "https://api.<your-panther>.runpanther.net/public/graphql"
    steps:
      - name: Checkout the repo
        uses: actions/checkout@v3
      
      - name: Set python version  
        uses: actions/setup-python@v4
        with:
          python-version: '3.9'

      - name: Install pipenv
        run: pip install pipenv

      - name: Install python dependencies and panther_analysis_tool
        run: make install
      
      - name: Upload detections to your Panther instance
        run: pipenv run panther_analysis_tool upload --batch --skip-tests
      
      - name: Upload custom schemas to your Panther Instance
        run: pipenv run panther_analysis_tool update-custom-schemas --path schemas/

```

{% endcode %}

</details>

* Make sure to change the environment variable `PANTHER_API_HOST` on line `84` to your Panther Instance's public GraphQL URL by replacing `<your-panther>`.&#x20;
* This workflow assumes you have added your Panther API token as a GitHub secret under the name  `PANTHER_API_TOKEN`. Please follow the instructions under the [prerequisites](#prerequisites) if you have not done that.

### Push changes

* Run `git push`.

Now, the following will occur the next time you use `git push` to make changes to the folders in the `paths` section of the workflow:

* Custom log schemas are tested with `pantherlog`.
* Custom detections are tested with `panther_analysis_tool`.
* Upon success, schemas and detections are uploaded to your Panther Console.

## Optional: Customize your GitHub Actions workflow in Panther

Optionally, you can extend or customize this workflow to better fit your organization. The following are common workflow customizations with Panther:

* Perform Python Linting against `.py` files.
* Trigger from an approved Pull Request (PR) instead of a Push to a specific folder.
* If you fork the [panther-analysis](https://github.com/panther-labs/panther-analysis) repository by the latest tag, learn how [syncing a fork](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork) can help keep Panther Detections up-to-date. We recommend syncing weekly by tag.

{% hint style="info" %}
For more information on GitHub Actions, please see [Github's documentation](https://docs.github.com/en/actions).
{% endhint %}
