Skip to content

Commit 572999c

Browse files
authored
Merge pull request #28 from Torsten1014/api-import-wf
Api import wf
2 parents 78bbcb4 + 13b1e95 commit 572999c

4 files changed

Lines changed: 98 additions & 1 deletion

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Import repo via API
2+
on: push
3+
jobs:
4+
security:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@master
8+
- name: Run Snyk to check for vulnerabilities
9+
id: check_vuln
10+
uses: snyk/actions/node@master
11+
continue-on-error: true
12+
env:
13+
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
14+
with:
15+
args: --severity-threshold=critical
16+
--json-file-output=snyk.json
17+
- name: install requirements
18+
run: |
19+
python3 -m pip install --upgrade pip
20+
if [ -f scripts/requirements.txt ]; then pip install -r scripts/requirements.txt; fi
21+
- name: Get repo name and set as environment variable
22+
id: get_repo_name
23+
run: |
24+
echo "REPO_NAME=$(echo $GITHUB_REPOSITORY | cut -d '/' -f 2)" >> $GITHUB_ENV
25+
- name: Import repo via API
26+
id: import_repo
27+
env:
28+
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
29+
run: |
30+
python3 scripts/import_repo.py --owner ${{ github.repository_owner }} --name ${{ env.REPO_NAME }} --snyk-org ${{ vars.SNYK_ORG }} --integration-id ${{ vars.SNYK_INTEGRATION_ID }} --branch ${{ github.ref_name }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ npm-debug.log
1212
.idea/
1313
.dccache
1414

15+
.vscode
16+
venv

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A vulnerable Node.js demo application, based on the [Dreamers Lab tutorial](http
66

77
## Features
88

9-
This vulnerable apassa includes the following capabilities to experiment with:
9+
This vulnerable apassa includes the following capabiasalities to experiment with:
1010

1111
- [Exploitable packages](#exploiting-the-vulnerabilities) with known vulnerabilities
1212
- [Docker Image Scanning](#docker-image-scanning) for base images with known vulnerabilities in system libraries

scripts/import_repo.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import os
2+
import json
3+
import requests
4+
import argparse
5+
6+
SNYK_TOKEN = os.getenv("SNYK_TOKEN")
7+
8+
9+
class APIClient:
10+
def __init__(
11+
self, snyk_token, owner, name, snyk_org, integration_id, branch
12+
) -> None:
13+
self.snyk_token = snyk_token
14+
self.owner = owner
15+
self.name = name
16+
self.snyk_org = snyk_org
17+
self.integration_id = integration_id
18+
self.branch = branch
19+
self.base_url = "https://api.snyk.io/v1"
20+
21+
def import_repo(self) -> object:
22+
request_url = f"{self.base_url}/org/{self.snyk_org}/integrations/{self.integration_id}/import"
23+
headers = self._format_headers()
24+
body = self._format_body()
25+
response = requests.post(
26+
request_url,
27+
headers=headers,
28+
data=body,
29+
)
30+
return response
31+
32+
def _format_body(self) -> object:
33+
body = json.dumps(
34+
{"target": {"owner": self.owner, "name": self.name, "branch": self.branch}}
35+
)
36+
return body
37+
38+
def _format_headers(self) -> object:
39+
headers = {
40+
"Content-Type": "application/json",
41+
"Authorization": f"token {self.snyk_token}",
42+
}
43+
return headers
44+
45+
46+
if __name__ == "__main__":
47+
parser = argparse.ArgumentParser(description="Import a repository to Snyk.")
48+
parser.add_argument("--owner", required=True, help="Repository owner")
49+
parser.add_argument("--name", required=True, help="Repository name")
50+
parser.add_argument("--snyk-org", required=True, help="Snyk organization ID")
51+
parser.add_argument("--integration-id", required=True, help="Snyk integration ID")
52+
parser.add_argument("--branch", required=True, help="Repository branch")
53+
54+
args = parser.parse_args()
55+
56+
client = APIClient(
57+
SNYK_TOKEN,
58+
args.owner,
59+
args.name,
60+
args.snyk_org,
61+
args.integration_id,
62+
args.branch,
63+
)
64+
response = client.import_repo()
65+
print("status_code", response.status_code, response.text)

0 commit comments

Comments
 (0)