Skip to content

Commit e17df2b

Browse files
authored
Move GHE requests to UI server (#369)
* Move GHE requests to UI server Signed-off-by: Christian Kadner <[email protected]> * Fix incorrect url matching code Signed-off-by: Christian Kadner <[email protected]> Signed-off-by: Christian Kadner <[email protected]>
1 parent a969ff9 commit e17df2b

File tree

16 files changed

+225
-149
lines changed

16 files changed

+225
-149
lines changed

api/examples/catalog_api.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525

2626
catalog_upload_file = "./../../bootstrapper/catalog_upload.json"
2727

28-
IBM_GHE_API_TOKEN = env.get("IBM_GHE_API_TOKEN")
28+
GHE_API_TOKEN = env.get("GHE_API_TOKEN")
29+
GHE_WEB_URL = env.get("GHE_WEB_URL", "github.ibm.com")
2930

3031

3132
def get_swagger_client():
@@ -59,7 +60,7 @@ def upload_catalog_assets(upload_file=catalog_upload_file) -> ApiCatalogUploadRe
5960
upload_items = json.load(f)
6061

6162
upload_body = ApiCatalogUpload(
62-
api_access_tokens=[ApiAccessToken(api_token=IBM_GHE_API_TOKEN, url_host="github.ibm.com")],
63+
api_access_tokens=[ApiAccessToken(api_token=GHE_API_TOKEN, url_host=GHE_WEB_URL)],
6364
components=upload_items.get("components"),
6465
datasets=upload_items.get("datasets"),
6566
models=upload_items.get("models"),

api/examples/components_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ def main():
364364
component = list_components(filter_dict={"name": 'Create Secret - Kubernetes Cluster'})[0]
365365
generate_code(component.id)
366366
args = {
367-
'token': env.get("IBM_GHE_API_TOKEN"),
367+
'token': env.get("GHE_API_TOKEN"),
368368
'url': 'https://raw.github.ibm.com/user/repo/master/secret.yml',
369369
'name': 'my-test-credential'
370370
}

api/examples/notebooks_api.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535

3636
yaml_files = sorted(filter(lambda f: "template" not in f, glob("./../../../katalog/notebook-samples/*.yaml", recursive=True)))
3737

38-
IBM_GHE_API_TOKEN = env.get("IBM_GHE_API_TOKEN")
38+
GHE_API_TOKEN = env.get("GHE_API_TOKEN")
39+
GHE_WEB_URL = env.get("GHE_WEB_URL", "github.ibm.com")
40+
GHE_RAW_URL = env.get("GHE_RAW_URL", "raw.github.ibm.com")
3941

4042

4143
def get_swagger_client():
@@ -103,8 +105,8 @@ def upload_notebook_templates(yaml_files: [str] = yaml_files) -> [str]:
103105
with open(yaml_file, "rb") as f:
104106
yaml_dict = yaml.load(f, Loader=yaml.SafeLoader)
105107

106-
if "github.ibm.com" in yaml_dict["implementation"]["github"]["source"]:
107-
api_token = IBM_GHE_API_TOKEN
108+
if GHE_WEB_URL in yaml_dict["implementation"]["github"]["source"]:
109+
api_token = GHE_API_TOKEN
108110
else:
109111
api_token = None
110112

@@ -365,9 +367,9 @@ def download_notebooks_from_github():
365367

366368
download_url = url.replace("/blob", "")\
367369
.replace("github.com", "raw.githubusercontent.com")\
368-
.replace("github.ibm.com", "raw.github.ibm.com")
370+
.replace(GHE_WEB_URL, GHE_RAW_URL)
369371

370-
if "github.ibm.com" in url:
372+
if GHE_WEB_URL in url:
371373
headers = {'Authorization': 'token %s' % env.get("IBM_GHE_API_TOKEN")}
372374
else:
373375
headers = {}

api/server/swagger_server/controllers_impl/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
# TODO: move into controllers_impl/util.py
1919
###############################################################################
2020

21-
ghe_api_token = env.get("GHE_API_TOKEN")
21+
GHE_API_TOKEN = env.get("GHE_API_TOKEN")
22+
GHE_WEB_URL = env.get("GHE_WEB_URL", "github.ibm.com")
23+
GHE_RAW_URL = env.get("GHE_RAW_URL", "raw.github.ibm.com")
2224

2325

2426
def get_yaml_file_content_from_uploadfile(uploadfile: FileStorage):
@@ -73,15 +75,15 @@ def download_file_content_from_url(url: str, bearer_token: str = None) -> bytes:
7375
if bearer_token and "?token=" not in url:
7476
request_headers.update({"Authorization": f"Bearer {bearer_token}"})
7577

76-
if "github.ibm.com" in url and "?token=" not in url:
77-
if not bearer_token and not ghe_api_token:
78+
if GHE_WEB_URL in url and "?token=" not in url:
79+
if not bearer_token and not GHE_API_TOKEN:
7880
raise ApiError(f"Must provide API token to access files on GitHub Enterprise: {url}", 422)
7981
else:
80-
request_headers.update({'Authorization': f'token {bearer_token or ghe_api_token}'})
82+
request_headers.update({'Authorization': f'token {bearer_token or GHE_API_TOKEN}'})
8183

8284
try:
8385
raw_url = url.replace("/blob/", "/") \
84-
.replace("/github.ibm.com/", "/raw.github.ibm.com/") \
86+
.replace(GHE_WEB_URL, GHE_RAW_URL) \
8587
.replace("/github.com/", "/raw.githubusercontent.com/")
8688

8789
response = requests.get(raw_url, allow_redirects=True, headers=request_headers)

api/server/swagger_server/controllers_impl/notebook_service_controller_impl.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
from werkzeug.datastructures import FileStorage
1616

1717
from swagger_server.controllers_impl import download_file_content_from_url, \
18-
get_yaml_file_content_from_uploadfile, validate_id, ghe_api_token
18+
get_yaml_file_content_from_uploadfile, validate_id, GHE_API_TOKEN, \
19+
GHE_WEB_URL, GHE_RAW_URL
1920
from swagger_server.data_access.minio_client import store_file, delete_objects, \
2021
get_file_content_and_url, enable_anonymous_read_access, NoSuchKey, \
2122
create_tarfile, get_object_url
@@ -454,7 +455,7 @@ def _upload_notebook_yaml(yaml_file_content: AnyStr, name=None, access_token=Non
454455
file_name="requirements.txt", file_content=requirements_all.encode())
455456

456457
# if the url included an access token, replace the original url with the s3 url
457-
if "?token=" in url or "github.ibm.com" in url:
458+
if "?token=" in url or GHE_WEB_URL in url:
458459
api_notebook.url = s3_url
459460
update_multiple(ApiNotebook, [notebook_id], "url", s3_url)
460461
enable_anonymous_read_access(bucket_name="mlpipeline", prefix="notebooks/*")
@@ -467,14 +468,14 @@ def _download_notebook(url: str, enterprise_github_api_token: str) -> dict:
467468
request_headers = dict()
468469

469470
# TODO: re-use ./init.py#download_file_content_from_url
470-
if "github.ibm.com" in url and "?token=" not in url:
471-
if not enterprise_github_api_token and not ghe_api_token:
471+
if GHE_WEB_URL in url and "?token=" not in url:
472+
if not enterprise_github_api_token and not GHE_API_TOKEN:
472473
raise ApiError(f"Must provide API token to access notebooks on Enterprise GitHub: {url}", 422)
473474
else:
474-
request_headers.update({'Authorization': f'token {enterprise_github_api_token or ghe_api_token}'})
475+
request_headers.update({'Authorization': f'token {enterprise_github_api_token or GHE_API_TOKEN}'})
475476

476477
try:
477-
raw_url = url.replace("/github.ibm.com/", "/raw.github.ibm.com/")\
478+
raw_url = url.replace(GHE_WEB_URL, GHE_RAW_URL)\
478479
.replace("/github.com/", "/raw.githubusercontent.com/")\
479480
.replace("/blob/", "/")
480481
response = requests.get(raw_url, allow_redirects=True, headers=request_headers)

dashboard/origin-mlx/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ USER node
2828
# mark as production build
2929
ENV NODE_ENV=production
3030

31-
# run build on container startup in order to build in environment variables
31+
# run `build` at container startup time to render the REACT_APP environment
32+
# variables into the JavaScript bundle that will run on the client Web browser
3233
# - https://create-react-app.dev/docs/adding-custom-environment-variables/
3334
# TODO: find a better solution, i.e.
3435
# - https://www.tutorialworks.com/openshift-deploy-react-app/

dashboard/origin-mlx/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ There are a few environment variables that can be defined that dictate how MLX i
213213
* `REACT_APP_TTL` - The amount of seconds a cached entry remains valid for (24 hours by default)
214214
* `REACT_APP_CACHE_INTERVAL` - The minimum amount of time in seconds between two checks on the validity of the cache's
215215
contents (24 hours by default)
216-
* `REACT_APP_GHE_API_TOKEN` - Enterprise GitHub API Token to "read" Markdown files from GitHub Enterprise. Only use when
216+
* `GHE_API_TOKEN` - Enterprise GitHub API Token to "read" Markdown files from GitHub Enterprise. Only use when
217217
MLX deployment is behind corporate firewall. The minimal set of permission required for the token are `repo` and
218218
`admin:org/read:org` (on a private repository).
219219

0 commit comments

Comments
 (0)