Skip to content

Commit 09c3983

Browse files
committed
s3 bucket workflow try-1
1 parent b04f7f0 commit 09c3983

36 files changed

+549
-153
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Encrypt and Upload Model
2+
3+
on:
4+
push:
5+
paths:
6+
- models/** # Trigger workflow when files in the models folder change
7+
8+
jobs:
9+
upload-model:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
# Step 1: Checkout the repo
14+
- name: Checkout code
15+
uses: actions/checkout@v3
16+
17+
# Step 2: Install AWS CLI
18+
- name: Install AWS CLI
19+
run: |
20+
sudo apt-get update
21+
sudo apt-get install -y awscli
22+
23+
# Step 3: Configure AWS credentials
24+
- name: Configure AWS credentials
25+
env:
26+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
27+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
28+
AWS_REGION: ${{ secrets.AWS_REGION }}
29+
run: |
30+
aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
31+
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
32+
aws configure set region $AWS_REGION
33+
34+
# Step 4: Encrypt the model using AWS KMS
35+
- name: Encrypt model
36+
run: |
37+
MODEL_FILE=$(find models -type f) # Find the model file in the models folder
38+
ENCRYPTED_FILE="${MODEL_FILE}.encrypted"
39+
aws kms encrypt \
40+
--key-id ${{ secrets.KMS_KEY_ID }} \
41+
--plaintext fileb://$MODEL_FILE \
42+
--output text \
43+
--query CiphertextBlob \
44+
> $ENCRYPTED_FILE
45+
46+
# Step 5: Upload encrypted model to S3
47+
- name: Upload to S3
48+
run: |
49+
MODEL_FILE=$(find models -type f)
50+
ENCRYPTED_FILE="${MODEL_FILE}.encrypted"
51+
aws s3 cp $ENCRYPTED_FILE s3://${{ secrets.S3_BUCKET_NAME }}/encrypted_models/$(basename $ENCRYPTED_FILE)

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.DS_Store
2-
node_modules
2+
node_modules
3+
.env

backend/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ENCRYPTION_KEY = "Hello"
1+
ENCRYPTION_KEY=Or58qxhsliuNqOqf93YMUHbJOGG/5k9ncuttg9f4VkE=

backend/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
media
528 Bytes
Binary file not shown.
1.84 KB
Binary file not shown.
-140 Bytes
Binary file not shown.
-1.06 KB
Binary file not shown.

backend/core/apps.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1+
# core/apps.py
2+
13
from django.apps import AppConfig
4+
from django.db.models.signals import post_migrate
5+
from .tasks import encrypt_model_if_needed
26

37

48
class CoreConfig(AppConfig):
59
default_auto_field = "django.db.models.BigAutoField"
610
name = "core"
711

812
def ready(self):
9-
# Import the task function
10-
from .tasks import encrypt_model_if_needed
11-
12-
# Run the function immediately on startup
13+
post_migrate.connect(run_encryption_on_startup, sender=self)
1314
encrypt_model_if_needed()
1415

1516

17+
def run_encryption_on_startup(sender, **kwargs):
18+
"""
19+
A helper function that will run encrypt_model_if_needed after Django has finished migrating.
20+
"""
21+
print("In run_encryption_on_startup function")
22+

backend/core/tasks.py

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,54 @@
1-
# core/tasks.py
2-
3-
# from celery import shared_task
41
import os
52
from django.conf import settings
6-
73
from utils.encryption_utils import encrypt_model
84

9-
# @shared_task
105
def encrypt_model_if_needed():
6+
# Paths for the model and encrypted model
117
model_path = os.path.join(settings.MEDIA_ROOT, 'models', 'model.onnx')
8+
encrypted_model_path = os.path.join(settings.MEDIA_ROOT, 'encrypted_models', 'model.onnx.enc')
129
model_version_path = os.path.join(settings.MEDIA_ROOT, 'models', 'model_version.txt')
1310

14-
# Check if the model needs to be updated
15-
if os.path.exists(model_version_path):
16-
with open(model_version_path, 'r') as f:
17-
stored_version = f.read().strip()
18-
19-
# Compare version or timestamp to determine if update is needed
20-
current_version = get_model_version(model_path) # Implement this function based on your versioning strategy
11+
# Ensure the encrypted_models directory exists
12+
if not os.path.exists(os.path.dirname(encrypted_model_path)):
13+
os.makedirs(os.path.dirname(encrypted_model_path))
14+
15+
# Check if the encrypted model already exists
16+
if not os.path.exists(encrypted_model_path):
17+
print("Encrypted model not found or directory is empty. Encrypting the model.")
2118

22-
if current_version != stored_version:
23-
encrypt_model(model_path) # Call encryption function
24-
# Update stored version file
19+
# Encrypt the model and save it to the encrypted_models folder
20+
encrypt_model(model_path, encrypted_model_path) # Pass the path to save the encrypted model
21+
22+
# After encryption, create or update the model version file
23+
model_timestamp = str(int(os.path.getmtime(model_path)))
24+
with open(model_version_path, 'w') as f:
25+
f.write(model_timestamp) # Store the current model's last modified timestamp
26+
else:
27+
# If encrypted model exists, check if an update is needed based on timestamps
28+
if os.path.exists(model_version_path):
29+
with open(model_version_path, 'r') as f:
30+
stored_timestamp = f.read().strip() # Read the stored timestamp
31+
32+
# Get the last modified timestamp of the model file
33+
model_timestamp = str(int(os.path.getmtime(model_path))) # Last modified time as a string
34+
35+
# Compare timestamps to determine if update is needed
36+
if model_timestamp != stored_timestamp:
37+
print("Model has been updated. Encrypting the updated model.")
38+
39+
# Encrypt the model and save it to the encrypted_models folder
40+
encrypt_model(model_path, encrypted_model_path) # Pass the path to save the encrypted model
41+
42+
# Update the stored timestamp file
43+
with open(model_version_path, 'w') as f:
44+
f.write(model_timestamp) # Store the current model's last modified timestamp
45+
else:
46+
# If model_version.txt doesn't exist, assume the model needs to be encrypted
47+
print("Model version file not found. Encrypting the model.")
48+
model_timestamp = str(int(os.path.getmtime(model_path)))
49+
50+
# Encrypt the model and save it to the encrypted_models folder
51+
encrypt_model(model_path, encrypted_model_path) # Pass the path to save the encrypted model
52+
2553
with open(model_version_path, 'w') as f:
26-
f.write(current_version)
54+
f.write(model_timestamp) # Create the timestamp file with the current model's timestamp

0 commit comments

Comments
 (0)