Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(BA-524): Deprecate X-BackendAI-SSO header for pipeline service authentication #3353

Merged
Merged
1 change: 1 addition & 0 deletions changes/3353.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate the JWT-based `X-BackendAI-SSO` header to reduce complexity in authentication process for the pipeline service
1 change: 0 additions & 1 deletion configs/webserver/halfstack.conf
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ max_file_upload_size = 4294967296
[pipeline]
endpoint = "http://127.0.0.1:9500"
frontend-endpoint = "http://127.0.0.1:3000"
jwt.secret = "7<:~[X,^Z1XM!*,Pe:PHR!bv,H~Q#l177<7gf_XHD6.<*<.t<[o|V5W(=0x:jTh-"

[ui]
brand = "Lablup Cloud"
Expand Down
2 changes: 0 additions & 2 deletions configs/webserver/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ show_non_installed_images = false
#endpoint = "http://127.0.0.1:9500"
# Endpoint to the pipeline service's frontend
#frontend-endpoint = "http://127.0.0.1:3000"
# A secret to sign JWTs used to authenticate users from the pipeline service
#jwt.secret = "7<:~[X,^Z1XM!*,Pe:PHR!bv,H~Q#l177<7gf_XHD6.<*<.t<[o|V5W(=0x:jTh-"

[ui]
brand = "Lablup Cloud"
Expand Down
7 changes: 0 additions & 7 deletions src/ai/backend/web/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,6 @@
{
t.Key("endpoint", default=_config_defaults["pipeline"]["endpoint"]): tx.URL,
t.Key("frontend-endpoint", default=None): t.Null | tx.URL,
t.Key("jwt", default=_config_defaults["pipeline"]["jwt"]): t.Dict(
{
t.Key(
"secret", default=_config_defaults["pipeline"]["jwt"]["secret"]
): t.String,
},
).allow_extra("*"),
},
).allow_extra("*"),
t.Key("ui"): t.Dict({
Expand Down
28 changes: 8 additions & 20 deletions src/ai/backend/web/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import json
import logging
import random
from datetime import datetime, timedelta
from typing import Optional, Tuple, Union, cast

import aiohttp
import jwt
from aiohttp import web
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
Expand Down Expand Up @@ -164,7 +162,10 @@ async def web_handler(request: web.Request, *, is_anonymous=False) -> web.Stream
raise RuntimeError("'pipeline' config must be set to handle pipeline requests.")
endpoint = pipeline_config["endpoint"]
log.info(f"WEB_HANDLER: {request.path} -> {endpoint}/{real_path}")
api_session = await asyncio.shield(get_api_session(request, endpoint))
if real_path.rstrip("/") == "login":
api_session = await asyncio.shield(get_api_session(request, endpoint))
else:
api_session = await asyncio.shield(get_anonymous_session(request, endpoint))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there another way to identify it as a login path? Relying on the real_path could be fragile.

elif is_anonymous:
api_session = await asyncio.shield(get_anonymous_session(request))
else:
Expand Down Expand Up @@ -207,23 +208,10 @@ async def web_handler(request: web.Request, *, is_anonymous=False) -> web.Stream
for hdr in HTTP_HEADERS_TO_FORWARD:
if request.headers.get(hdr) is not None:
api_rqst.headers[hdr] = request.headers[hdr]
if proxy_path == "pipeline":
session_id = request.headers.get("X-BackendAI-SessionID", "")
if not (sso_token := request.headers.get("X-BackendAI-SSO")):
jwt_secret = config["pipeline"]["jwt"]["secret"]
now = datetime.now().astimezone()
payload = {
# Registered claims
"exp": now + timedelta(seconds=config["session"]["max_age"]),
"iss": "Backend.AI Webserver",
"iat": now,
# Private claims
"aiohttp_session": session_id,
"access_key": api_session.config.access_key, # since 23.03.10
}
sso_token = jwt.encode(payload, key=jwt_secret, algorithm="HS256")
api_rqst.headers["X-BackendAI-SSO"] = sso_token
api_rqst.headers["X-BackendAI-SessionID"] = session_id
Comment on lines -210 to -226
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does Fasttrack handle version compatibility?
When making changes this time, is there any risk of breaking functionality for clients using previous versions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is recommended to use the same version as Backend.AI Core.
To enhance version compatibility, we may adopt a dedicated header (e.g., X-BackendAI-FastTrack-Version).

if proxy_path == "pipeline" and real_path.rstrip("/") == "login":
api_rqst.headers["X-BackendAI-SessionID"] = request.headers.get(
"X-BackendAI-SessionID", ""
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont' think rqst is a commonly used abbreviation. Would it be possible to rename it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renaming the api_rqst will be a over-scoped task, as the name is used in various modules in this project.
Would you mind if I issue a new ticket for it and proceed in next work?
Thank you.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create the rename task with priority: low and handle it when you have time. :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The task will be tracked by BA-469

# Uploading request body happens at the entering of the block,
# and downloading response body happens in the read loop inside.
async with api_rqst.fetch() as up_resp:
Expand Down
Loading