Skip to content

Commit 663da18

Browse files
Merge pull request #2 from Contrast-Security-OSS/feature/application-add-tags
add tags to applications with a little remove functionality
2 parents 5541109 + 0a22575 commit 663da18

5 files changed

Lines changed: 462 additions & 1 deletion

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ output.json
33
*.log
44
*.logs
55
.DS_Store
6-
*.csv
6+
output.csv
7+
apps_and_servers.csv

app-add-label/README.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# App Add Label
2+
3+
This script allows you to add or remove tags from Contrast Security applications in bulk.
4+
5+
## Prerequisites
6+
7+
- Python 3.x installed
8+
- Required Python packages (install with `pip install -r ../requirements.txt`)
9+
- Contrast Security account with API access
10+
11+
## Setup
12+
13+
1. **Create a `.creds` file** in the parent directory (`CSR-Helpful-Scripts/.creds`) with your Contrast credentials:
14+
15+
```
16+
CONTRAST_URL=https://your-contrast-instance.com/Contrast
17+
ORG_ID=your-organization-id
18+
USERNAME=your-username
19+
API_KEY=your-api-key
20+
SERVICE_KEY=your-service-key
21+
```
22+
23+
2. **Configure tags.json** - This file defines the default tags to add or remove:
24+
25+
```json
26+
{
27+
"applications_id": [""],
28+
"tags": [],
29+
"tags_remove": ["Test"]
30+
}
31+
```
32+
33+
- `tags`: Array of tags to add to applications
34+
- `tags_remove`: Array of tags to remove from applications
35+
36+
3. **Configure specific_apps.csv (Optional)** - Use this file to specify which applications should be tagged and with what tags:
37+
38+
**Format:**
39+
```csv
40+
application_id
41+
application_id,tag1,tag2,tag3
42+
```
43+
44+
**Examples:**
45+
```csv
46+
f3a1fcfd-ad2d-4f25-b2ea-1a27e175d12f
47+
443f5d89-6265-495f-950f-9eab221ddf56,CustomTag1,CustomTag2
48+
```
49+
50+
## How It Works
51+
52+
### Tagging Behavior
53+
54+
1. **If `specific_apps.csv` is empty or doesn't exist:**
55+
- The script will apply the tags from `tags.json` to **all applications** in your organization
56+
57+
2. **If `specific_apps.csv` contains app IDs only (no tags):**
58+
- The script will apply the tags from `tags.json` to **only those specific applications**
59+
60+
3. **If `specific_apps.csv` contains app IDs with specific tags:**
61+
- The script will **override** the tags from `tags.json` and use the specific tags for those applications
62+
- Format: `application_id,tag1,tag2,tag3`
63+
64+
### Tag Removal
65+
66+
- Tags specified in the `tags_remove` field in `tags.json` will be removed from the applications
67+
- **Note:** At this time, there is no way to specify tags to be removed from specific applications via `specific_apps.csv`, but this may be added in the future
68+
69+
## Running the Script
70+
71+
1. **Open a terminal or command prompt**
72+
73+
2. **Navigate to the app-add-label directory:**
74+
```bash
75+
cd app-add-label
76+
```
77+
78+
3. **Run the script:**
79+
```bash
80+
python app-add-label.py
81+
```
82+
83+
4. **Enter credentials:**
84+
- If all your credentials are defined in the `.creds` file, simply **press Enter 5 times** to accept the default values
85+
- Otherwise, enter the requested information when prompted:
86+
- Contrast URL
87+
- Organization ID
88+
- Username
89+
- API Key
90+
- Service Key
91+
92+
5. **The script will:**
93+
- Load your credentials
94+
- Read the `tags.json` file
95+
- Read the `specific_apps.csv` file (if it exists)
96+
- Fetch applications from your Contrast organization
97+
- Apply or remove tags based on your configuration
98+
- Display progress and results
99+
100+
## Examples
101+
102+
### Example 1: Add "Production" tag to all applications
103+
104+
**tags.json:**
105+
```json
106+
{
107+
"applications_id": [""],
108+
"tags": ["Production"],
109+
"tags_remove": []
110+
}
111+
```
112+
113+
**specific_apps.csv:** (empty or doesn't exist)
114+
115+
**Result:** Adds "Production" tag to all applications in your organization
116+
117+
### Example 2: Add tags to specific applications only
118+
119+
**tags.json:**
120+
```json
121+
{
122+
"applications_id": [""],
123+
"tags": ["WebApp"],
124+
"tags_remove": []
125+
}
126+
```
127+
128+
**specific_apps.csv:**
129+
```csv
130+
f3a1fcfd-ad2d-4f25-b2ea-1a27e175d12f
131+
443f5d89-6265-495f-950f-9eab221ddf56
132+
```
133+
134+
**Result:** Adds "WebApp" tag to only the two specified applications
135+
136+
### Example 3: Add different tags to different applications
137+
138+
**tags.json:**
139+
```json
140+
{
141+
"applications_id": [""],
142+
"tags": ["Default"],
143+
"tags_remove": []
144+
}
145+
```
146+
147+
**specific_apps.csv:**
148+
```csv
149+
f3a1fcfd-ad2d-4f25-b2ea-1a27e175d12f,CriticalApp,Production
150+
443f5d89-6265-495f-950f-9eab221ddf56,InternalTool,Development
151+
```
152+
153+
**Result:**
154+
- First app gets "CriticalApp" and "Production" tags (tags.json is ignored)
155+
- Second app gets "InternalTool" and "Development" tags (tags.json is ignored)
156+
157+
### Example 4: Remove tags from all applications
158+
159+
**tags.json:**
160+
```json
161+
{
162+
"applications_id": [""],
163+
"tags": [],
164+
"tags_remove": ["Test", "Deprecated"]
165+
}
166+
```
167+
168+
**specific_apps.csv:** (empty or doesn't exist)
169+
170+
**Result:** Removes "Test" and "Deprecated" tags from all applications
171+
172+
## Troubleshooting
173+
174+
- **File not found errors:** Ensure `tags.json` exists in the `app-add-label` directory
175+
- **API errors:** Verify your credentials in the `.creds` file are correct
176+
- **No applications processed:** Check that your `specific_apps.csv` format is correct and app IDs are valid
177+
178+
## Notes
179+
180+
- Application IDs can be found in the Contrast UI or via the API
181+
- Tags are case-sensitive
182+
- The script will display detailed progress information as it processes applications
183+
- All API responses are logged for debugging purposes

0 commit comments

Comments
 (0)