Skip to content

Commit d434751

Browse files
committed
chore: merge Identity and IdentityClient
Signed-off-by: Xie Zhihao <[email protected]>
1 parent 8ebd76f commit d434751

File tree

4 files changed

+171
-168
lines changed

4 files changed

+171
-168
lines changed

veadk/cloud/cloud_agent_engine.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from veadk.config import getenv, veadk_environments
2727
from veadk.integrations.ve_apig.ve_apig import APIGateway
2828
from veadk.integrations.ve_faas.ve_faas import VeFaaS
29-
from veadk.integrations.ve_identity.ve_identity import Identity
29+
from veadk.integrations.ve_identity.identity_client import IdentityClient
3030
from veadk.utils.logger import get_logger
3131
from veadk.utils.misc import formatted_timestamp
3232

@@ -47,7 +47,7 @@ class CloudAgentEngine(BaseModel):
4747
region (str): Region for Volcengine services. Defaults to "cn-beijing".
4848
_vefaas_service (VeFaaS): Internal VeFaaS client instance, initialized post-creation.
4949
_veapig_service (APIGateway): Internal VeAPIG client instance, initialized post-creation.
50-
_veidentity_service (Identity): Internal Identity client instance, initialized post-creation.
50+
_veidentity_service (IdentityClient): Internal Identity client instance, initialized post-creation.
5151
5252
Note:
5353
Credentials must be set via environment variables for default behavior.
@@ -91,7 +91,7 @@ def model_post_init(self, context: Any, /) -> None:
9191
secret_key=self.volcengine_secret_key,
9292
region=self.region,
9393
)
94-
self._veidentity_service = Identity(
94+
self._veidentity_service = IdentityClient(
9595
access_key=self.volcengine_access_key,
9696
secret_key=self.volcengine_secret_key,
9797
region=self.region,
@@ -293,14 +293,9 @@ def deploy(
293293
)
294294
_ = function_id # for future use
295295

296-
app = self._vefaas_service.get_application_details(app_id=app_id)
297-
cloud_resource = json.loads(app["CloudResource"])
298-
veapig_gateway_id = cloud_resource["framework"]["triggers"][0][
299-
"DetailedConfig"
300-
]["GatewayId"]
301-
veapig_route_id = cloud_resource["framework"]["triggers"][0]["Routes"][0][
302-
"Id"
303-
]
296+
veapig_gateway_id, _, veapig_route_id = (
297+
self._vefaas_service.get_application_route(app_id=app_id)
298+
)
304299

305300
if auth_method == "oauth2":
306301
# Get or create the Identity user pool.
@@ -311,6 +306,7 @@ def deploy(
311306
identity_user_pool_id = self._veidentity_service.create_user_pool(
312307
name=identity_user_pool_name,
313308
)
309+
issuer = f"https://auth.id.{self.region}.volces.com/userpool/{identity_user_pool_id}"
314310

315311
# Create APIG upstream for Identity.
316312
identity_domain = f"auth.id.{self.region}.volces.com"
@@ -364,9 +360,9 @@ def deploy(
364360

365361
plugin_name = "wasm-oauth2-sso"
366362
plugin_config = {
367-
"AuthorizationUrl": f"https://auth.id.{self.region}.volces.com/userpool/{identity_user_pool_id}/authorize",
363+
"AuthorizationUrl": f"{issuer}/authorize",
368364
"UpstreamId": veapig_identity_upstream_id,
369-
"TokenUrl": f"https://auth.id.{self.region}.volces.com/userpool/{identity_user_pool_id}/oauth/token",
365+
"TokenUrl": f"{issuer}/oauth/token",
370366
"RedirectPath": "/callback",
371367
"SignoutPath": "/signout",
372368
"ClientId": identity_client_id,
@@ -377,12 +373,11 @@ def deploy(
377373
plugin_config = {
378374
"RemoteJwks": {
379375
"UpstreamId": veapig_identity_upstream_id,
380-
"Url": f"auth.id.{self.region}.volces.com/userpool/{identity_user_pool_id}/keys",
376+
"Url": f"{issuer}/keys",
381377
},
382-
"Issuer": f"https://auth.id.{self.region}.volces.com/userpool/{identity_user_pool_id}",
378+
"Issuer": issuer,
383379
"ValidateConsumer": False,
384380
}
385-
386381
self._vefaas_service.apig_client.create_plugin_binding(
387382
scope="ROUTE",
388383
target=veapig_route_id,

veadk/integrations/ve_faas/ve_faas.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,11 @@ def _release_application(self, app_id: str):
206206
logs = "\n".join(self._get_application_logs(app_id=app_id))
207207
log_text = re.sub(
208208
r'([{"\']?(key|secret|token|pass|auth|credential|access|api|ak|sk|doubao|volces|coze)[^"\'\s]*["\']?\s*[:=]\s*)(["\']?)([^"\'\s]+)(["\']?)|([A-Za-z0-9+/=]{20,})',
209-
lambda m: f"{m.group(1)}{m.group(3)}******{m.group(5)}"
210-
if m.group(1)
211-
else "******",
209+
lambda m: (
210+
f"{m.group(1)}{m.group(3)}******{m.group(5)}"
211+
if m.group(1)
212+
else "******"
213+
),
212214
logs,
213215
flags=re.IGNORECASE,
214216
)
@@ -232,9 +234,11 @@ def _list_application(self, app_id: str = None, app_name: str = None):
232234
request_body = {
233235
"OrderBy": {"Key": "CreateTime", "Ascend": False},
234236
"FunctionId": app_id if app_id else None,
235-
"Filters": [{"Item": {"Key": "Name", "Value": [app_name]}}]
236-
if app_name and not app_id
237-
else None,
237+
"Filters": (
238+
[{"Item": {"Key": "Name", "Value": [app_name]}}]
239+
if app_name and not app_id
240+
else None
241+
),
238242
}
239243
# remove None
240244
request_body = {k: v for k, v in request_body.items() if v is not None}
@@ -354,6 +358,26 @@ def get_application_details(self, app_id: str = None, app_name: str = None):
354358
if app["Name"] == app_name:
355359
return app
356360

361+
def get_application_route(
362+
self, app_id: str = None, app_name: str = None
363+
) -> tuple[str, str, str] | None:
364+
app = self.get_application_details(
365+
app_id=app_id,
366+
app_name=app_name,
367+
)
368+
if not app:
369+
return None
370+
371+
cloud_resource = json.loads(app["CloudResource"])
372+
gateway_id = cloud_resource["framework"]["triggers"][0]["DetailedConfig"][
373+
"GatewayId"
374+
]
375+
service_id = cloud_resource["framework"]["triggers"][0]["Routes"][0][
376+
"ServiceId"
377+
]
378+
route_id = cloud_resource["framework"]["triggers"][0]["Routes"][0]["Id"]
379+
return gateway_id, service_id, route_id
380+
357381
def find_app_id_by_name(self, name: str):
358382
apps = self._list_application(app_name=name)
359383
for app in apps:

veadk/integrations/ve_identity/identity_client.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,3 +533,133 @@ async def create_oauth2_credential_provider_with_dcr(
533533

534534
# Create the credential provider with updated config
535535
return self.create_oauth2_credential_provider(request_params)
536+
537+
def create_user_pool(self, name: str) -> str:
538+
from volcenginesdkid import CreateUserPoolRequest, CreateUserPoolResponse
539+
540+
request = CreateUserPoolRequest(
541+
name=name,
542+
)
543+
response: CreateUserPoolResponse = self._api_client.create_user_pool(request)
544+
545+
return response.uid
546+
547+
def get_user_pool(self, name: str) -> str | None:
548+
from volcenginesdkid import (
549+
ListUserPoolsRequest,
550+
ListUserPoolsResponse,
551+
FilterForListUserPoolsInput,
552+
DataForListUsersOutput,
553+
)
554+
555+
request = ListUserPoolsRequest(
556+
page_number=1,
557+
page_size=1,
558+
filter=FilterForListUserPoolsInput(
559+
name=name,
560+
),
561+
)
562+
response: ListUserPoolsResponse = self._api_client.list_user_pools(request)
563+
if response.total_count == 0:
564+
return None
565+
566+
user_pool: DataForListUsersOutput = response.data[0]
567+
return user_pool.uid
568+
569+
def create_user_pool_client(
570+
self, user_pool_uid: str, name: str, client_type: str
571+
) -> tuple[str, str]:
572+
from volcenginesdkid import (
573+
CreateUserPoolClientRequest,
574+
CreateUserPoolClientResponse,
575+
)
576+
577+
request = CreateUserPoolClientRequest(
578+
user_pool_uid=user_pool_uid,
579+
name=name,
580+
client_type=client_type,
581+
)
582+
response: CreateUserPoolClientResponse = (
583+
self._api_client.create_user_pool_client(request)
584+
)
585+
return response.uid, response.client_secret
586+
587+
def register_callback_for_user_pool_client(
588+
self,
589+
user_pool_uid: str,
590+
client_uid: str,
591+
callback_url: str,
592+
web_origin: str,
593+
):
594+
from volcenginesdkid import (
595+
GetUserPoolClientRequest,
596+
GetUserPoolClientResponse,
597+
UpdateUserPoolClientRequest,
598+
)
599+
600+
request = GetUserPoolClientRequest(
601+
user_pool_uid=user_pool_uid,
602+
client_uid=client_uid,
603+
)
604+
response: GetUserPoolClientResponse = self._api_client.get_user_pool_client(
605+
request
606+
)
607+
608+
allowed_callback_urls = response.allowed_callback_urls
609+
if not allowed_callback_urls:
610+
allowed_callback_urls = []
611+
allowed_callback_urls.append(callback_url)
612+
allowed_web_origins = response.allowed_web_origins
613+
if not allowed_web_origins:
614+
allowed_web_origins = []
615+
allowed_web_origins.append(web_origin)
616+
617+
request2 = UpdateUserPoolClientRequest(
618+
user_pool_uid=user_pool_uid,
619+
client_uid=client_uid,
620+
name=response.name,
621+
description=response.description,
622+
allowed_callback_urls=allowed_callback_urls,
623+
allowed_logout_urls=response.allowed_logout_urls,
624+
allowed_web_origins=allowed_web_origins,
625+
allowed_cors=response.allowed_cors,
626+
id_token=response.id_token,
627+
refresh_token=response.refresh_token,
628+
)
629+
self._api_client.update_user_pool_client(request2)
630+
631+
def get_user_pool_client(
632+
self, user_pool_uid: str, name: str
633+
) -> tuple[str, str] | None:
634+
from volcenginesdkid import (
635+
ListUserPoolClientsRequest,
636+
ListUserPoolClientsResponse,
637+
FilterForListUserPoolClientsInput,
638+
DataForListUserPoolClientsOutput,
639+
GetUserPoolClientRequest,
640+
GetUserPoolClientResponse,
641+
)
642+
643+
request = ListUserPoolClientsRequest(
644+
user_pool_uid=user_pool_uid,
645+
page_number=1,
646+
page_size=1,
647+
filter=FilterForListUserPoolClientsInput(
648+
name=name,
649+
),
650+
)
651+
response: ListUserPoolClientsResponse = self._api_client.list_user_pool_clients(
652+
request
653+
)
654+
if response.total_count == 0:
655+
return None
656+
657+
client: DataForListUserPoolClientsOutput = response.data[0]
658+
request2 = GetUserPoolClientRequest(
659+
user_pool_uid=user_pool_uid,
660+
client_uid=client.uid,
661+
)
662+
response2: GetUserPoolClientResponse = self._api_client.get_user_pool_client(
663+
request2
664+
)
665+
return response2.uid, response2.client_secret

0 commit comments

Comments
 (0)