Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: TT-293 add extra security layer to avoid use cli in a non-devel…
…opment environment
  • Loading branch information
jcalarcon98 committed Jul 21, 2021
commit 734b522b71976934f76794385ffa28e6b69c337b
21 changes: 8 additions & 13 deletions cosmosdb_emulator/cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@
COMMAND=$@
API_CONTAINER_NAME="time-tracker-backend_api"
TIME_TRACKER_CLI_URL="cosmosdb_emulator/time_tracker_cli"
TIME_TRACKER_CLI="python3 $COMMAND"
DEFAULT_SCRIPT_NAME='main.py'
DEFAULT_SCRIPT_NAME="main.py"
FIRST_ARG=$1

if [ "$FIRST_ARG" != "$DEFAULT_SCRIPT_NAME" ]; then
execute(){
docker exec -it $API_CONTAINER_NAME sh "cosmosdb_emulator/verify_environment.sh"

if [ "$FIRST_ARG" != "$DEFAULT_SCRIPT_NAME" ]; then
echo "Do not forget that the file name is $DEFAULT_SCRIPT_NAME and needs to be sent as first parameter"
echo "For example: ./cli.sh main.py"
exit 0
fi
fi

DATABASE_EMULATOR_KEY="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
DATABASE_ENV_KEY=$DATABASE_MASTER_KEY
TIME_TRACKER_CLI="python3 $COMMAND"

if [ "$DATABASE_EMULATOR_KEY" != "$DATABASE_ENV_KEY" ]; then
echo "You are trying to run this CLI in a non-development environment. We can not proceed with this action"
exit 0
fi

execute(){
docker exec -ti $API_CONTAINER_NAME sh -c "cd $TIME_TRACKER_CLI_URL && $TIME_TRACKER_CLI"
docker exec -it $API_CONTAINER_NAME sh -c "cd $TIME_TRACKER_CLI_URL && $TIME_TRACKER_CLI"
}

execute
10 changes: 1 addition & 9 deletions cosmosdb_emulator/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@ until curl -ksf "${DATABASE_ACCOUNT_URI}/_explorer/emulator.pem" -o 'cosmosdb_em
sleep 10
done

echo "Development environment check..."
DATABASE_EMULATOR_KEY="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
DATABASE_ENV_KEY=$DATABASE_MASTER_KEY

if [ "$DATABASE_EMULATOR_KEY" != "$DATABASE_ENV_KEY" ]; then
echo "You are trying to build an environment different from the development, this can have negative effects."
exit 0
fi
echo "GREAT! You are on development environment"
source cosmosdb_emulator/verify_environment.sh

echo "Container cosmosemulator started."

Expand Down
17 changes: 12 additions & 5 deletions cosmosdb_emulator/time_tracker_cli/data_target/cosmos.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import os
import sys

from azure.cosmos import CosmosClient
from azure.cosmos.exceptions import CosmosResourceExistsError
from azure.cosmos.exceptions import (
CosmosResourceExistsError,
CosmosResourceNotFoundError,
)

from cosmosdb_emulator.time_tracker_cli.data_target.data_target import (
DataTarget,
Expand Down Expand Up @@ -79,10 +83,13 @@ def delete(self, entities: dict):
)
)
entity_container_id = entity_container_definition.get('id')
self.database.create_container_if_not_exists(
**entity_container_definition
)
self.database.delete_container(entity_container_id)
try:
self.database.delete_container(entity_container_id)
self.database.create_container_if_not_exists(
**entity_container_definition
)
except CosmosResourceNotFoundError:
pass

def save(self, entities: dict):
for entity in entities:
Expand Down
13 changes: 13 additions & 0 deletions cosmosdb_emulator/verify_environment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

echo "We are checking the development environment..."

DATABASE_EMULATOR_KEY="C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
DATABASE_ENV_KEY=$DATABASE_MASTER_KEY

if [ "$DATABASE_EMULATOR_KEY" != "$DATABASE_ENV_KEY" ]; then
echo "You are trying to run this CLI in a non-development environment. We can not proceed with this action"
exit 0
fi

echo "GREAT! You are on development environment"