Skip to content

Commit 068aac1

Browse files
committed
Supports BROKER_TOKEN for scripts
Signed-off-by: Daniel Kastl <[email protected]>
1 parent f257975 commit 068aac1

File tree

3 files changed

+74
-15
lines changed

3 files changed

+74
-15
lines changed

doc/broker_scripts.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,21 @@ URL specified in the header for each subscription.
3232
`api_key` argument is not provided.
3333
- `BROKER_URL`: The base URL for your Orion Context Broker. Defaults to
3434
`http://app.local:1026` if not set.
35+
- `BROKER_TOKEN`: The bearer token for authentication. If provided, it will be
36+
used in the `Authorization` header for the requests.
3537

36-
**Note:** Make sure to set the `BROKER_URL` environment variable to your context
37-
broker URL before running this script.
38+
**Note:** Make sure to set the `BROKER_URL` and `BROKER_TOKEN` (optional) environment
39+
variables to your context broker URL and authentication token respectively
40+
before running this script.
3841

3942
To prepend an environment variable to a command in the terminal, you can do so
4043
like this:
4144

4245
```bash
43-
BROKER_URL=your_broker_url ./scripts/register_all_subscriptions.sh <your_api_key>
46+
BROKER_URL=your_broker_url ./scripts/register_all_subscriptions.sh <redmine_api_key>
4447
```
4548

46-
Replace `your_api_key` with your actual Redmine API key and `your_broker_url`
49+
Replace `redmine_api_key` with your actual Redmine API key and `your_broker_url`
4750
with the correct URL. This will set the `BROKER_URL` environment variables for
4851
the duration of the `register_all_subscriptions.sh` script.
4952

@@ -66,9 +69,12 @@ subscriptions, and then makes DELETE requests to the
6669

6770
- `BROKER_URL`: The base URL for your Orion Context Broker. Defaults to
6871
`http://app.local:1026` if not set.
72+
- `BROKER_TOKEN`: The bearer token for authentication. If provided, it will be
73+
used in the `Authorization` header for the requests.
6974

7075
**Note:** This script does not require any arguments. Make sure to set the
71-
`BROKER_URL` environment variable to your context broker URL before running this script.
76+
`BROKER_URL` and `BROKER_TOKEN` (optional) environment variables to your context
77+
broker URL and authentication token respectively before running this script.
7278

7379
To prepend an environment variable to a command in the terminal, you can do so
7480
like this:

doc/scripts/delete_all_subscriptions.sh

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,32 @@
33
# The base URL for your Orion Context Broker
44
BROKER_URL=${BROKER_URL:-"http://app.local:1026"}
55

6+
# The bearer token for authentication
7+
BROKER_TOKEN=${BROKER_TOKEN:-""}
8+
9+
# Define the headers for the cURL commands
10+
HEADERS=(-H "Accept: application/json")
11+
if [ -n "$BROKER_TOKEN" ]; then
12+
HEADERS+=(-H "Authorization: Bearer $BROKER_TOKEN")
13+
fi
14+
615
# Fetch all subscription IDs using a GET request
7-
# Include additional headers as needed, e.g., for multi-tenant setups or authentication
8-
subscription_ids=$(curl -s -X GET "${BROKER_URL}/v2/subscriptions" \
9-
-H "Accept: application/json" | jq -r '.[].id')
16+
response=$(curl -s -o /dev/null -w "%{http_code}" -X GET "${BROKER_URL}/v2/subscriptions" "${HEADERS[@]}")
17+
http_code="$response"
18+
19+
echo "HTTP Code: $http_code"
20+
21+
# Check if the GET request returned a 401 status code
22+
if [ "$http_code" -eq 401 ]; then
23+
echo "Unauthorized. Please check your token."
24+
exit 1
25+
fi
26+
27+
# If the GET request was successful, fetch the response body and parse it to get the subscription IDs
28+
if [ "$http_code" -eq 200 ]; then
29+
response_body=$(curl -s -X GET "${BROKER_URL}/v2/subscriptions" "${HEADERS[@]}")
30+
subscription_ids=$(echo "$response_body" | jq -r '.[].id')
31+
fi
1032

1133
# Check if there are any subscriptions to delete
1234
if [ -z "$subscription_ids" ]; then
@@ -16,10 +38,16 @@ fi
1638

1739
# Loop through the subscription IDs and delete each one
1840
for id in $subscription_ids; do
19-
curl -X DELETE "${BROKER_URL}/v2/subscriptions/${id}" \
20-
-H "Accept: application/json"
21-
echo "Deleted subscription $id"
41+
response=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE "${BROKER_URL}/v2/subscriptions/${id}" "${HEADERS[@]}")
42+
http_code="$response"
43+
44+
# Check if the DELETE request returned a 401 status code (optional: just to notify but not to exit)
45+
if [ "$http_code" -eq 401 ]; then
46+
echo "Failed to delete subscription $id. Unauthorized. Please check your token."
47+
continue
48+
fi
49+
50+
echo "Deleted subscription $id"
2251
done
2352

2453
echo "All subscriptions have been deleted."
25-

doc/scripts/register_all_subscriptions.sh

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,42 @@
44
REDMINE_API_KEY=${REDMINE_API_KEY:-$1}
55

66
# Check if API key is provided
7-
if [ -z "$REDMINE_API_KEY" ]
8-
then
7+
if [ -z "$REDMINE_API_KEY" ]; then
98
echo "No API key provided. Usage: ./register_all_subscriptions.sh <api_key> or set the REDMINE_API_KEY environment variable."
109
exit 1
1110
fi
1211

1312
# The base URL for your Orion Context Broker
1413
BROKER_URL=${BROKER_URL:-"http://app.local:1026"}
1514

15+
# The bearer token for authentication
16+
BROKER_TOKEN=${BROKER_TOKEN:-""}
17+
18+
# Define the headers for the cURL commands
19+
HEADERS=(-H "Accept: application/json")
20+
if [ -n "$BROKER_TOKEN" ]; then
21+
HEADERS+=(-H "Authorization: Bearer $BROKER_TOKEN")
22+
fi
23+
1624
# Fetch all subscriptions using a GET request
17-
subscriptions=$(curl -s -X GET "${BROKER_URL}/v2/subscriptions" -H "Accept: application/json")
25+
response=$(curl -s -o /dev/null -w "%{http_code}" -X GET "${BROKER_URL}/v2/subscriptions" "${HEADERS[@]}")
26+
http_code="$response"
27+
28+
echo "HTTP Code: $http_code"
29+
30+
# Check if the GET request returned a 401 status code
31+
if [ "$http_code" -eq 401 ]; then
32+
echo "Unauthorized. Please check your token."
33+
exit 1
34+
fi
35+
36+
# If the GET request was successful, fetch the response body and parse it
37+
if [ "$http_code" -eq 200 ]; then
38+
subscriptions=$(curl -s -X GET "${BROKER_URL}/v2/subscriptions" "${HEADERS[@]}")
39+
else
40+
echo "Failed to fetch subscriptions. HTTP Code: $http_code"
41+
exit 1
42+
fi
1843

1944
# Use jq to parse the JSON response and filter subscriptions with the "X-Redmine-GTT-Subscription-Template-URL" header
2045
filtered_subscriptions=$(echo "$subscriptions" | jq -c '.[] | select(.notification.httpCustom.headers."X-Redmine-GTT-Subscription-Template-URL" != null)')

0 commit comments

Comments
 (0)