Skip to content

Commit 734b522

Browse files
committed
feat: TT-293 add extra security layer to avoid use cli in a non-development environment
1 parent 1dd8583 commit 734b522

File tree

4 files changed

+34
-27
lines changed

4 files changed

+34
-27
lines changed

cosmosdb_emulator/cli.sh

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,21 @@
22
COMMAND=$@
33
API_CONTAINER_NAME="time-tracker-backend_api"
44
TIME_TRACKER_CLI_URL="cosmosdb_emulator/time_tracker_cli"
5-
TIME_TRACKER_CLI="python3 $COMMAND"
6-
DEFAULT_SCRIPT_NAME='main.py'
5+
DEFAULT_SCRIPT_NAME="main.py"
76
FIRST_ARG=$1
87

9-
if [ "$FIRST_ARG" != "$DEFAULT_SCRIPT_NAME" ]; then
8+
execute(){
9+
docker exec -it $API_CONTAINER_NAME sh "cosmosdb_emulator/verify_environment.sh"
10+
11+
if [ "$FIRST_ARG" != "$DEFAULT_SCRIPT_NAME" ]; then
1012
echo "Do not forget that the file name is $DEFAULT_SCRIPT_NAME and needs to be sent as first parameter"
1113
echo "For example: ./cli.sh main.py"
1214
exit 0
13-
fi
15+
fi
1416

15-
DATABASE_EMULATOR_KEY="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
16-
DATABASE_ENV_KEY=$DATABASE_MASTER_KEY
17+
TIME_TRACKER_CLI="python3 $COMMAND"
1718

18-
if [ "$DATABASE_EMULATOR_KEY" != "$DATABASE_ENV_KEY" ]; then
19-
echo "You are trying to run this CLI in a non-development environment. We can not proceed with this action"
20-
exit 0
21-
fi
22-
23-
execute(){
24-
docker exec -ti $API_CONTAINER_NAME sh -c "cd $TIME_TRACKER_CLI_URL && $TIME_TRACKER_CLI"
19+
docker exec -it $API_CONTAINER_NAME sh -c "cd $TIME_TRACKER_CLI_URL && $TIME_TRACKER_CLI"
2520
}
2621

2722
execute

cosmosdb_emulator/entrypoint.sh

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,7 @@ until curl -ksf "${DATABASE_ACCOUNT_URI}/_explorer/emulator.pem" -o 'cosmosdb_em
55
sleep 10
66
done
77

8-
echo "Development environment check..."
9-
DATABASE_EMULATOR_KEY="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
10-
DATABASE_ENV_KEY=$DATABASE_MASTER_KEY
11-
12-
if [ "$DATABASE_EMULATOR_KEY" != "$DATABASE_ENV_KEY" ]; then
13-
echo "You are trying to build an environment different from the development, this can have negative effects."
14-
exit 0
15-
fi
16-
echo "GREAT! You are on development environment"
8+
source cosmosdb_emulator/verify_environment.sh
179

1810
echo "Container cosmosemulator started."
1911

cosmosdb_emulator/time_tracker_cli/data_target/cosmos.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import os
2+
import sys
23

34
from azure.cosmos import CosmosClient
4-
from azure.cosmos.exceptions import CosmosResourceExistsError
5+
from azure.cosmos.exceptions import (
6+
CosmosResourceExistsError,
7+
CosmosResourceNotFoundError,
8+
)
59

610
from cosmosdb_emulator.time_tracker_cli.data_target.data_target import (
711
DataTarget,
@@ -79,10 +83,13 @@ def delete(self, entities: dict):
7983
)
8084
)
8185
entity_container_id = entity_container_definition.get('id')
82-
self.database.create_container_if_not_exists(
83-
**entity_container_definition
84-
)
85-
self.database.delete_container(entity_container_id)
86+
try:
87+
self.database.delete_container(entity_container_id)
88+
self.database.create_container_if_not_exists(
89+
**entity_container_definition
90+
)
91+
except CosmosResourceNotFoundError:
92+
pass
8693

8794
def save(self, entities: dict):
8895
for entity in entities:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
echo "We are checking the development environment..."
4+
5+
DATABASE_EMULATOR_KEY="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
6+
DATABASE_ENV_KEY=$DATABASE_MASTER_KEY
7+
8+
if [ "$DATABASE_EMULATOR_KEY" != "$DATABASE_ENV_KEY" ]; then
9+
echo "You are trying to run this CLI in a non-development environment. We can not proceed with this action"
10+
exit 0
11+
fi
12+
13+
echo "GREAT! You are on development environment"

0 commit comments

Comments
 (0)