File tree Expand file tree Collapse file tree 2 files changed +80
-0
lines changed Expand file tree Collapse file tree 2 files changed +80
-0
lines changed Original file line number Diff line number Diff line change
1
+ #! /bin/sh
2
+ # This pre-commit hook validates S3 bucket references in modified files
3
+
4
+ set -e
5
+
6
+ # Get list of staged files
7
+ staged_files=$( git diff --cached --name-only --diff-filter=ACMR | grep -E ' \.(py|ipynb|md|rst|yaml|yml|json)$' || true)
8
+
9
+ if [ -z " $staged_files " ]; then
10
+ echo " No relevant files to check for S3 bucket references."
11
+ exit 0
12
+ fi
13
+
14
+ echo " Checking S3 bucket references in staged files..."
15
+ has_invalid_buckets=0
16
+
17
+ for file in $staged_files ; do
18
+ echo " Validating S3 references in $file "
19
+ python s3_bucket_validator.py " $file "
20
+ if [ $? -ne 0 ]; then
21
+ has_invalid_buckets=1
22
+ fi
23
+ done
24
+
25
+ if [ $has_invalid_buckets -ne 0 ]; then
26
+ echo " ERROR: Invalid S3 bucket references found. Please fix them before committing."
27
+ exit 1
28
+ fi
29
+
30
+ echo " S3 bucket validation passed."
31
+ exit 0
Original file line number Diff line number Diff line change
1
+ import re
2
+
3
+ import boto3
4
+ from botocore .exceptions import ClientError
5
+
6
+
7
+ def is_bucket_accessible (bucket_name ):
8
+ s3 = boto3 .client ('s3' )
9
+ try :
10
+ s3 .head_bucket (Bucket = bucket_name )
11
+ return True
12
+ except ClientError as e :
13
+ error_code = int (e .response ['Error' ]['Code' ])
14
+ if error_code == 403 :
15
+ print (f"Bucket { bucket_name } exists, but you don't have permission to access it." )
16
+ elif error_code == 404 :
17
+ print (f"Bucket { bucket_name } does not exist." )
18
+ else :
19
+ print (f"Error checking bucket { bucket_name } : { e } " )
20
+ return False
21
+
22
+ def validate_s3_references (file_path ):
23
+ with open (file_path , 'r' ) as file :
24
+ content = file .read ()
25
+
26
+ s3_pattern = re .compile (r's3:\/\/([a-zA-Z0-9._-]+)' )
27
+ matches = s3_pattern .findall (content )
28
+
29
+ invalid_buckets = []
30
+ for bucket in matches :
31
+ if not is_bucket_accessible (bucket ):
32
+ invalid_buckets .append (bucket )
33
+
34
+ return invalid_buckets
35
+
36
+ if __name__ == "__main__" :
37
+ import sys
38
+ if len (sys .argv ) < 2 :
39
+ print ("Usage: python s3_bucket_validator.py <file_path>" )
40
+ sys .exit (1 )
41
+
42
+ file_path = sys .argv [1 ]
43
+ invalid_buckets = validate_s3_references (file_path )
44
+
45
+ if invalid_buckets :
46
+ print (f"Invalid or inaccessible S3 buckets found: { ', ' .join (invalid_buckets )} " )
47
+ sys .exit (1 )
48
+ else :
49
+ print ("All referenced S3 buckets are valid and accessible." )
You can’t perform that action at this time.
0 commit comments