Skip to content

Instantly share code, notes, and snippets.

@prabhu
prabhu / github-repo-template.tf
Created July 18, 2020 14:01
Create GitHub repository using template
data "github_repositories" "java_ms_template" {
query = "org:${var.organization} language:java topic:microservice topic:template"
}
resource "github_repository" "new_ms" {
name = "new-java-microservice"
description = "New Java Microservice"
private = true
@prabhu
prabhu / github-on-deploy.yml
Created July 18, 2020 14:04
GitHub snippet to perform actions when a deployment is created
on:
deployment
@prabhu
prabhu / github-on-label.yml
Created July 18, 2020 14:05
Snippet to run a command based on the presence of a label
on:
label:
types: [created]
steps:
- name: Analyze with NG SAST
if: ${{ contains(github.context.payload.pull_request.labels.*.name, 'Ready for AppSec') }}
run: |
sl analyze --app ShiftLeftHSLGo14 --tag branch=${GITHUB_REF} --go --cpg $(pwd)
@prabhu
prabhu / bitbucket-branch-protect.tf
Created July 26, 2020 13:46
Terraform snippet for Bitbucket branch protection
resource "bitbucket_branch_restriction" "master" {
owner = "myteam"
repository = "terraform-shiftleft"
# force, restrict_merges, enforce_merge_checks, allow_auto_merge_when_builds_pass, require_passing_builds_to_merge
kind = "push"
# feature/*, release/*
pattern = "master"
}
@prabhu
prabhu / bitbucket-repo-variable.tf
Created July 26, 2020 13:50
Bitbucket repository variable with Terraform
provider "bitbucket" {
version = "~> 1.2"
username = var.username
password = var.password
}
resource "bitbucket_repository_variable" "sl_org_id_secret" {
for_each = toset(var.repos)
key = "SHIFTLEFT_ORG_ID"
value = var.sl_org_id
@prabhu
prabhu / bitbucket-reusable-pipelines.yml
Created July 26, 2020 13:56
Reusable Bitbucket pipelines configuration with YAML anchors
definitions:
steps:
- step: &build
name: Build microservices jar
script:
- mvn package
artifacts:
- target/**
- step: &build-react
name: Build React app
@prabhu
prabhu / bitbucket-proxy-api.py
Created July 26, 2020 14:09
Example for making Bitbucket api calls from pipelines using the local proxy
import requests
# Use local bitbucket proxy to avoid the need for app password
proxies = {
"http": "http://localhost:29418",
"https": "http://localhost:29418",
}
# Use the proxies object in requests for making
# authenticated calls without app passwords
@prabhu
prabhu / shiftleft-bitbucket-insights.py
Created July 26, 2020 14:13
Python script to present ShiftLeft NG SAST findings as Bitbucket code insights
#!/usr/bin/python
# pip install requests
import os
import sys
import requests
# Collect the required variables
APP_ID = os.getenv("BITBUCKET_REPO_SLUG")
SHIFTLEFT_ORG_ID = os.getenv("SHIFTLEFT_ORG_ID")
@prabhu
prabhu / bitbucket-pipelines.yml
Last active July 26, 2020 14:16
Bitbucket pipeline step to integrate ShiftLeft Insights script
- step:
name: ShiftLeft NextGen Analysis
script:
- curl https://cdn.shiftleft.io/download/sl > $HOME/sl && chmod a+rx $HOME/sl
- $HOME/sl analyze --no-diagnostic --force --app ${BITBUCKET_REPO_SLUG} --tag branch=${BITBUCKET_BRANCH} --go --cpg $(pwd)
- step:
image: python:3.7-slim
name: ShiftLeft NG SAST Code Insights
script:
- pip install requests
@prabhu
prabhu / shiftleft.rego
Created April 3, 2021 12:20
OPA rego policy with ShiftLeft API integration
package shiftleft
default allow = true
runtime := opa.runtime()
sl_app_name := runtime.env.SHIFTLEFT_APP
sl_access_token := runtime.env.SHIFTLEFT_ACCESS_TOKEN
payload := io.jwt.decode(sl_access_token)
sl_org_id := payload[1].orgID
headers := {"Content-Type": "application/json", "Authorization": sprintf("Bearer %s", [sl_access_token])}