Repository navigation
Expand file tree
/
Copy pathregister_app_example.py
More file actions
105 lines (87 loc) · 2.79 KB
/
Copy pathregister_app_example.py
File metadata and controls
105 lines (87 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import argparse
import requests
import json
def register_app(
name: str,
scopes: list[str],
redirect_uris: list[str],
org_id: str,
auth_token: str,
api_version: str = "2024-10-15",
) -> dict:
"""
Register a new Snyk app with the specified parameters.
Args:
name (str): Name of the Snyk app.
org_id (str): Snyk organization ID.
auth_token (str): Snyk API authentication token.
api_version (str): Snyk API version.
Returns:
dict: Response from the Snyk API.
"""
url: str = (
f"https://api.snyk.io/rest/orgs/{org_id}/apps/creations?version={api_version}"
)
headers: dict = {
"Authorization": auth_token,
"Content-Type": "application/vnd.api+json",
}
data: dict = {
"data": {
"attributes": {
"context": "tenant",
"name": name,
"redirect_uris": redirect_uris,
"scopes": scopes,
},
"type": "app",
}
}
response: requests.Response = requests.post(url, headers=headers, json=data)
response.raise_for_status() # Raise an error for bad responses
snyk_app_response: dict = response.json()
return snyk_app_response
def main():
parser: argparse.ArgumentParser = argparse.ArgumentParser(
description="Create a Snyk App via API."
)
parser.add_argument("--org-id", required=True, help="Snyk organization ID")
parser.add_argument(
"--auth-token", required=True, help="Snyk API authentication token"
)
parser.add_argument("--name", required=True, help="Name for the Snyk App")
parser.add_argument(
"--scopes",
default=[
"org.read",
"org.project.read",
"org.report.read",
"org.collection.read",
"org.project.snapshot.read",
],
nargs="+",
help="Scopes for the Snyk App (default: org.read org.project.read org.report.read org.collection.read org.project.snapshot.read). Separate multiple scopes with spaces.",
)
parser.add_argument(
"--redirect-uris",
default=["https://example.com"],
nargs="+",
help="Redirect URIs for the Snyk App (default: https://example.com). Separate multiple URIs with spaces.",
)
parser.add_argument(
"--api-version",
default="2024-10-15",
help="Snyk API version (default: 2024-10-15)",
)
args: argparse.Namespace = parser.parse_args()
data: dict = register_app(
name=args.name,
scopes=args.scopes,
redirect_uris=args.redirect_uris,
org_id=args.org_id,
auth_token=args.auth_token,
api_version=args.api_version,
)
print(json.dumps(data, indent=2))
if __name__ == "__main__":
main()