This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
gce_vcs_proxy.py
executable file
·167 lines (132 loc) · 5.38 KB
/
gce_vcs_proxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import os
import socket
import sys
from logging.handlers import RotatingFileHandler
from flask import Flask, request, Blueprint
from lib.vcs_management import get_vcs_handler
from lib.utils import create_json_response
def manually_read_app_config():
config = {}
try:
import yaml # pylint: disable=import-outside-toplevel
except ImportError:
return None
with open("vcs_proxy.yaml") as file:
try:
config = yaml.load(file, Loader=yaml.SafeLoader)
except yaml.YAMLError as err:
print(err)
return config
vcs_config = manually_read_app_config()
if not vcs_config:
vcs_config = {}
# Attention: Only enabled for local / qa debugging.
# This will enable pretty formatting of JSON and have other negative
# side-effects when being run in prod.
DEBUG = False
IS_PROD = False
if "PROD_HOSTNAME" in vcs_config:
if vcs_config["PROD_HOSTNAME"] == socket.gethostname():
IS_PROD = True
if not IS_PROD:
print("[!] Running in dev mode!")
DEBUG = True
else:
print("[!] Running in prod mode!")
# This will lead to UnicodeEncodeError: 'ascii' codec can't encode [...] errors
# as it will try to log unicode results as str.
# logging.basicConfig(level=logging.DEBUG)
app = Flask(__name__, static_url_path="", template_folder="templates")
# Used to remove spaces from long JSON responses.
app.config["JSONIFY_PRETTYPRINT_REGULAR"] = False
app.config["GITHUB_API_ACCESS_TOKEN"] = None
bp = Blueprint("vcs_proxy", "main_api")
# Note: This has to match the app/vcs_proxy.py blueprint.
@bp.route("/main_api")
def main_api():
commit_hash = request.args.get("commit_hash", 0, type=str)
item_hash = request.args.get("item_hash", 0, type=str)
item_path = request.args.get("item_path", None, type=str)
commit_link = request.args.get("commit_link", "", type=str)
repo_url = request.args.get("repo_url", "", type=str)
if "github.com" in commit_link:
resource_url = commit_link
else:
resource_url = repo_url or commit_link
vcs_handler = get_vcs_handler(app, resource_url)
if not vcs_handler:
return create_json_response("Please provide a valid resource URL.", 400)
# try:
# Return a specific file's content if requested instead.
if item_hash:
content = vcs_handler.get_file_content(item_hash, item_path)
if not content:
err = f"Could not retrieve object with hash {item_hash}."
logging.error(err)
return create_json_response(str(err), 400)
logging.info("Retrieved %s: %d bytes", item_hash, len(content))
return content
return vcs_handler.fetch_commit_data(commit_hash)
# except Exception as err:
# if DEBUG:
# return create_json_response(str(err), 400, tb=traceback.format_exc())
# else:
# return create_json_response(str(err), 400)
app.register_blueprint(bp)
def enable_cloud_logging():
import google.cloud.logging # pylint: disable=import-outside-toplevel
import google.auth.exceptions # pylint: disable=import-outside-toplevel
print("[*] Enabling cloud logging")
# Instantiates a client
try:
client = google.cloud.logging.Client()
# Retrieves a Cloud Logging handler based on the environment
# you're running in and integrates the handler with the
# Python logging module. By default this captures all logs
# at INFO level and higher
print("Default handler:", client.get_default_handler())
client.setup_logging(log_level="WARNING")
print("[+] Cloud logging enabled:", logging.getLogger().handlers)
except google.auth.exceptions.DefaultCredentialsError:
print("[!] Cloud logging not enabled due to missing credentials")
if IS_PROD:
enable_cloud_logging()
def start():
root_dir = os.path.dirname(os.path.realpath(__file__))
error_file = os.path.join(root_dir, "vcs_error.log")
handler = RotatingFileHandler(error_file, maxBytes=100000, backupCount=1)
handler.setLevel(logging.WARNING)
app.logger.addHandler(handler)
app.logger.addHandler(logging.StreamHandler(stream=sys.stdout))
if DEBUG:
app.logger.setLevel(logging.DEBUG)
else:
app.logger.setLevel(logging.INFO)
if "GITHUB_ACCESS_TOKEN" in vcs_config:
app.config["GITHUB_API_ACCESS_TOKEN"] = vcs_config["GITHUB_ACCESS_TOKEN"]
cert_dir = os.path.join(root_dir, "cert")
cert_file = os.path.join(cert_dir, "cert.pem")
key_file = os.path.join(cert_dir, "key.pem")
ssl_context = (cert_file, key_file)
use_host = "0.0.0.0"
use_port = 8088
use_protocol = "https" if ssl_context else "http"
print(f"[+] Listening on: {use_protocol}://{use_host}:{use_port}")
app.run(host=use_host, port=use_port, ssl_context=ssl_context, debug=DEBUG)
if __name__ == "__main__":
start()