Skip to content

Commit 6e948be

Browse files
committed
Improving GCP cloud logging in function. RE:natcap#1580
1 parent a69804b commit 6e948be

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

codesigning/gcp-cloudfunc/main.py

+12
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ def get_lock():
5353
def main(request):
5454
data = request.get_json()
5555
if data['token'] != os.environ['ACCESS_TOKEN']:
56+
logging.info('Rejecting request due to invalid token')
5657
return jsonify('Invalid token'), 403
5758

5859
if request.method != 'POST':
60+
logging.info('Rejecting request due to invalid HTTP method')
5961
return jsonify('Invalid request method'), 405
6062

6163
storage_client = storage.Client()
@@ -69,6 +71,7 @@ def main(request):
6971
next_file_url = queue_dict['queue'].pop(0)
7072
except IndexError:
7173
# No items in the queue!
74+
logging.info('No binaries are currently queued for signing')
7275
return jsonify('No items in the queue'), 204
7376

7477
queuefile.upload_from_string(json.dumps(queue_dict))
@@ -84,16 +87,21 @@ def main(request):
8487

8588
elif data['action'] == 'enqueue':
8689
url = data['url']
90+
logging.info('Attempting to enqueue url %s', url)
8791

8892
if not url.endswith('.exe'):
93+
logging.info("Rejecting URL because it doesn't end in .exe"')
8994
return jsonify('Invalid URL to sign'), 400
9095

9196
if not url.startswith(GOOGLE_PREFIX):
97+
logging.info('Rejecting URL because it does not start with %s',
98+
GOOGLE_PREFIX)
9299
return jsonify('Invalid host'), 400
93100

94101
if not url.startswith((
95102
f'{GOOGLE_PREFIX}/releases.naturalcapitalproject.org/',
96103
f'{GOOGLE_PREFIX}/natcap-dev-build-artifacts/')):
104+
logging.info('Rejecting URL because the bucket is incorrect')
97105
return jsonify("Invalid target bucket"), 400
98106

99107
# Remove http character quoting
@@ -106,6 +114,7 @@ def main(request):
106114
# If the file does not exist at this URL, reject it.
107115
response = requests.head(url)
108116
if response.status_code > 400:
117+
logging.info('Rejecting URL because it does not exist')
109118
return jsonify('Requested file does not exist'), 403
110119

111120
# If the file is too old, reject it. Trying to avoid a
@@ -114,6 +123,7 @@ def main(request):
114123
modified_time = datetime.datetime.strptime(
115124
' '.join((mday, mmonth, myear)), '%d %b %Y')
116125
if modified_time < datetime.datetime(year=2024, month=6, day=1):
126+
logging.info('Rejecting URL because it is too old')
117127
return jsonify('File is too old'), 400
118128

119129
with get_lock():
@@ -126,6 +136,8 @@ def main(request):
126136
signed_files_list.download_as_string())
127137

128138
if url in signed_files_dict['signed_files']:
139+
logging.info(
140+
'Rejecting URL because it has already been signed')
129141
return jsonify('File has already been signed'), 400
130142

131143
# Since the file has not already been signed, add the file to the

0 commit comments

Comments
 (0)