-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrast_create_application.py
100 lines (89 loc) · 2.57 KB
/
contrast_create_application.py
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
# Script to create an application on the Contrast TeamServer
# https://support.contrastsecurity.com/hc/en-us/articles/360060719052-How-to-create-a-custom-name-for-a-merged-group-of-Applications
# Author: [email protected]
import argparse
import logging
import sys
from contrast_api import contrast_instance_from_json, load_config
args_parser = argparse.ArgumentParser(description="Create an application on Contrast.")
# Required arguments
args_parser.add_argument(
"-n",
"--app-name",
"--application-name",
help="Name of the application you want to create.",
type=str,
required=True,
)
args_parser.add_argument(
"-l",
"-language",
help="Agent language for the application you want to create.",
choices=["DOTNET", "DOTNET_CORE", "GO", "JAVA", "NODE", "PHP", "PYTHON", "RUBY"],
type=str.upper,
required=True,
)
args_parser.add_argument(
"-o",
"--org-id",
"--organization-id",
help="ID of the organization to create this application in.",
type=str,
required=True,
)
# Optional arguments
args_parser.add_argument(
"-c",
"--code",
"--app-code",
"--application-code",
help="Optional 'application code' / 'short name' for this application.",
type=str,
)
args_parser.add_argument(
"-g",
"--groups",
help="Optional list of application access groups to add this application to. Group(s) must exist prior to the application for this to work.",
type=str,
nargs="*",
default=[],
action="extend",
)
args_parser.add_argument(
"-m",
"--metadata",
help="Optional key/value pairs of application metadata / custom fields to set on this application.",
type=str,
)
args_parser.add_argument(
"-t",
"--tags",
help="Optional list of tags to add to this application.",
type=str,
nargs="*",
default=[],
action="extend",
)
args = args_parser.parse_args()
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
logger = logging.getLogger(__file__)
config = load_config()
contrast = contrast_instance_from_json(config)
body = {
"name": args.app_name,
"language": args.l,
"appGroups": ",".join(args.groups),
"metadata": args.metadata,
"tags": ",".join(args.tags),
}
response = contrast.api_request(
f"sca/organizations/{args.org_id}/applications/create", "POST", body=body
)
exit_code = 0
logger.info(" - ".join(response["messages"]))
if not response["success"]:
logger.error("Creation failed")
exit_code = 1
else:
logger.info(f"New Application ID = {response['application']['app_id']}")
sys.exit(exit_code)