1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import json
1516import os
1617import socket
1718import subprocess
2324
2425from veadk .cloud .cloud_app import CloudApp
2526from veadk .config import getenv , veadk_environments
27+ from veadk .integrations .ve_apig .ve_apig import APIGateway
2628from veadk .integrations .ve_faas .ve_faas import VeFaaS
29+ from veadk .integrations .ve_identity .ve_identity import Identity
2730from veadk .utils .logger import get_logger
2831from veadk .utils .misc import formatted_timestamp
2932
@@ -43,6 +46,8 @@ class CloudAgentEngine(BaseModel):
4346 Defaults to VOLCENGINE_SECRET_KEY environment variable.
4447 region (str): Region for Volcengine services. Defaults to "cn-beijing".
4548 _vefaas_service (VeFaaS): Internal VeFaaS client instance, initialized post-creation.
49+ _veapig_service (APIGateway): Internal VeAPIG client instance, initialized post-creation.
50+ _veidentity_service (Identity): Internal Identity client instance, initialized post-creation.
4651
4752 Note:
4853 Credentials must be set via environment variables for default behavior.
@@ -81,6 +86,16 @@ def model_post_init(self, context: Any, /) -> None:
8186 secret_key = self .volcengine_secret_key ,
8287 region = self .region ,
8388 )
89+ self ._veapig_service = APIGateway (
90+ access_key = self .volcengine_access_key ,
91+ secret_key = self .volcengine_secret_key ,
92+ region = self .region ,
93+ )
94+ self ._veidentity_service = Identity (
95+ access_key = self .volcengine_access_key ,
96+ secret_key = self .volcengine_secret_key ,
97+ region = self .region ,
98+ )
8499
85100 def _prepare (self , path : str , name : str ):
86101 """Prepares the local project for deployment by validating path and name.
@@ -197,8 +212,10 @@ def deploy(
197212 gateway_name : str = "" ,
198213 gateway_service_name : str = "" ,
199214 gateway_upstream_name : str = "" ,
200- auth_method : str = "none" ,
201215 use_adk_web : bool = False ,
216+ auth_method : str = "none" ,
217+ identity_user_pool_name : str = "" ,
218+ identity_client_name : str = "" ,
202219 local_test : bool = False ,
203220 ) -> CloudApp :
204221 """Deploys a local agent project to Volcengine FaaS, creating necessary resources.
@@ -211,8 +228,10 @@ def deploy(
211228 gateway_name (str, optional): Custom gateway resource name. Defaults to timestamped.
212229 gateway_service_name (str, optional): Custom service name. Defaults to timestamped.
213230 gateway_upstream_name (str, optional): Custom upstream name. Defaults to timestamped.
214- auth_method (str, optional): Authentication for the agent. Defaults to none.
215231 use_adk_web (bool): Enable ADK Web configuration. Defaults to False.
232+ auth_method (str, optional): Authentication for the agent. Defaults to none.
233+ identity_user_pool_name (str, optional): Custom user pool name. Defaults to timestamped.
234+ identity_client_name (str, optional): Custom client name. Defaults to timestamped.
216235 local_test (bool): Perform FastAPI server test before deploy. Defaults to False.
217236
218237 Returns:
@@ -256,6 +275,12 @@ def deploy(
256275 gateway_service_name = f"{ application_name } -gw-svr-{ formatted_timestamp ()} "
257276 if not gateway_upstream_name :
258277 gateway_upstream_name = f"{ application_name } -gw-us-{ formatted_timestamp ()} "
278+ if not identity_user_pool_name :
279+ identity_user_pool_name = (
280+ f"{ application_name } -id-up-{ formatted_timestamp ()} "
281+ )
282+ if not identity_client_name :
283+ identity_client_name = f"{ application_name } -id-cli-{ formatted_timestamp ()} "
259284
260285 try :
261286 vefaas_application_url , app_id , function_id = self ._vefaas_service .deploy (
@@ -268,6 +293,103 @@ def deploy(
268293 )
269294 _ = function_id # for future use
270295
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+ ]
304+
305+ if auth_method == "oauth2" :
306+ # Get or create the Identity user pool.
307+ identity_user_pool_id = self ._veidentity_service .get_user_pool (
308+ name = identity_user_pool_name ,
309+ )
310+ if not identity_user_pool_id :
311+ identity_user_pool_id = self ._veidentity_service .create_user_pool (
312+ name = identity_user_pool_name ,
313+ )
314+
315+ # Create APIG upstream for Identity.
316+ identity_domain = f"auth.id.{ self .region } .volces.com"
317+ veapig_identity_upstream_id = (
318+ self ._veapig_service .check_domain_upstream_exist (
319+ domain = identity_domain ,
320+ port = 443 ,
321+ gateway_id = veapig_gateway_id ,
322+ )
323+ )
324+ if not veapig_identity_upstream_id :
325+ veapig_identity_upstream_id = (
326+ self ._veapig_service .create_domain_upstream (
327+ domain = f"auth.id.{ self .region } .volces.com" ,
328+ port = 443 ,
329+ is_https = True ,
330+ gateway_id = veapig_gateway_id ,
331+ upstream_name = f"id-{ formatted_timestamp ()} " ,
332+ )
333+ )
334+
335+ # Create plugin binding.
336+ plugin_name = ""
337+ plugin_config = {}
338+ if use_adk_web :
339+ # Get or create the Identity client.
340+ identity_client_id = ""
341+ identity_client_secret = ""
342+ identity_client = self ._veidentity_service .get_user_pool_client (
343+ user_pool_uid = identity_user_pool_id ,
344+ name = identity_client_name ,
345+ )
346+ if identity_client :
347+ identity_client_id = identity_client [0 ]
348+ identity_client_secret = identity_client [1 ]
349+ else :
350+ identity_client_id , identity_client_secret = (
351+ self ._veidentity_service .create_user_pool_client (
352+ user_pool_uid = identity_user_pool_id ,
353+ name = identity_client_name ,
354+ client_type = "WEB_APPLICATION" ,
355+ )
356+ )
357+
358+ self ._veidentity_service .register_callback_for_user_pool_client (
359+ user_pool_uid = identity_user_pool_id ,
360+ client_uid = identity_client_id ,
361+ callback_url = f"{ vefaas_application_url } /callback" ,
362+ web_origin = vefaas_application_url ,
363+ )
364+
365+ plugin_name = "wasm-oauth2-sso"
366+ plugin_config = {
367+ "AuthorizationUrl" : f"https://auth.id.{ self .region } .volces.com/userpool/{ identity_user_pool_id } /authorize" ,
368+ "UpstreamId" : veapig_identity_upstream_id ,
369+ "TokenUrl" : f"https://auth.id.{ self .region } .volces.com/userpool/{ identity_user_pool_id } /oauth/token" ,
370+ "RedirectPath" : "/callback" ,
371+ "SignoutPath" : "/signout" ,
372+ "ClientId" : identity_client_id ,
373+ "ClientSecret" : identity_client_secret ,
374+ }
375+ else :
376+ plugin_name = "wasm-jwt-auth"
377+ plugin_config = {
378+ "RemoteJwks" : {
379+ "UpstreamId" : veapig_identity_upstream_id ,
380+ "Url" : f"auth.id.{ self .region } .volces.com/userpool/{ identity_user_pool_id } /keys" ,
381+ },
382+ "Issuer" : f"https://auth.id.{ self .region } .volces.com/userpool/{ identity_user_pool_id } " ,
383+ "ValidateConsumer" : False ,
384+ }
385+
386+ self ._vefaas_service .apig_client .create_plugin_binding (
387+ scope = "ROUTE" ,
388+ target = veapig_route_id ,
389+ plugin_name = plugin_name ,
390+ plugin_config = json .dumps (plugin_config ),
391+ )
392+
271393 return CloudApp (
272394 vefaas_application_name = application_name ,
273395 vefaas_endpoint = vefaas_application_url ,
0 commit comments