Skip to content

Instantly share code, notes, and snippets.

@prabhu
prabhu / shiftleft-branch.rego
Created April 3, 2021 12:25
OPA rego policy for branch specific ShiftLeft policy
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])}
@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])}
@prabhu
prabhu / Snippets
Last active January 31, 2021 23:08
A good browser detection logic
function detectBrowser(userAgent, language) {
var version, webkitVersion, iOSAgent, iOSDevice, iOSMajorVersion, iOSMinorVersion, browser = {};
userAgent = (userAgent || navigator.userAgent).toLowerCase();
language = language || navigator.language || navigator.browserLanguage;
version = browser.version = (userAgent.match(/.*(?:rv|chrome|webkit|opera|ie)[\/: ](.+?)([ \);]|$)/) || [])[1];
webkitVersion = (userAgent.match(/webkit\/(.+?) /) || [])[1];
iOSAgent = (userAgent.match(/\b(iPad|iPhone|iPod)\b.*\bOS (\d)_(\d)/i) || []);
iOSDevice = iOSAgent[1];
iOSMajorVersion = iOSAgent[2];
iOSMinorVersion = iOSAgent[3];
@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-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-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 / 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-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 / 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 / github-on-deploy.yml
Created July 18, 2020 14:04
GitHub snippet to perform actions when a deployment is created
on:
deployment