Debian Codegraph @

test/sanity/code-smell/no-s3.py

raw
1 """
2 Disallow direct linking to S3 buckets.
3 S3 buckets should be accessed through a CloudFront distribution.
4 """
5
6 from __future__ import annotations
7
8 import re
9 import sys
10
11
12 def main():
13 """Main entry point."""
14 for path in sys.argv[1:] or sys.stdin.read().splitlines():
15 with open(path, 'rb') as path_fd:
16 for line, b_text in enumerate(path_fd.readlines()):
17 try:
18 text = b_text.decode()
19 except UnicodeDecodeError:
20 continue
21
22 if match := re.search(r'(http.*?s3\..*?amazonaws\.com)', text):
23 print(f'{path}:{line + 1}:{match.start(1) + 1}: use a CloudFront distribution instead of an S3 bucket: {match.group(1)}')
24
25
26 if __name__ == '__main__':
27 main()