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 .identity_client import IdentityClient
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 (IdentityClient): Internal Identity client instance, initialized post-creation.
4651
4752 Note:
4853 Credentials must be set via environment variables for default behavior.
@@ -85,6 +90,16 @@ def model_post_init(self, context: Any, /) -> None:
8590 secret_key = self .volcengine_secret_key ,
8691 region = self .region ,
8792 )
93+ self ._veapig_service = APIGateway (
94+ access_key = self .volcengine_access_key ,
95+ secret_key = self .volcengine_secret_key ,
96+ region = self .region ,
97+ )
98+ self ._veidentity_service = IdentityClient (
99+ access_key = self .volcengine_access_key ,
100+ secret_key = self .volcengine_secret_key ,
101+ region = self .region ,
102+ )
88103
89104 def _prepare (self , path : str , name : str ):
90105 """Prepares the local project for deployment by validating path and name.
@@ -202,6 +217,9 @@ def deploy(
202217 gateway_service_name : str = "" ,
203218 gateway_upstream_name : str = "" ,
204219 use_adk_web : bool = False ,
220+ auth_method : str = "none" ,
221+ identity_user_pool_name : str = "" ,
222+ identity_client_name : str = "" ,
205223 local_test : bool = False ,
206224 ) -> CloudApp :
207225 """Deploys a local agent project to Volcengine FaaS, creating necessary resources.
@@ -215,6 +233,9 @@ def deploy(
215233 gateway_service_name (str, optional): Custom service name. Defaults to timestamped.
216234 gateway_upstream_name (str, optional): Custom upstream name. Defaults to timestamped.
217235 use_adk_web (bool): Enable ADK Web configuration. Defaults to False.
236+ auth_method (str, optional): Authentication for the agent. Defaults to none.
237+ identity_user_pool_name (str, optional): Custom user pool name. Defaults to timestamped.
238+ identity_client_name (str, optional): Custom client name. Defaults to timestamped.
218239 local_test (bool): Perform FastAPI server test before deploy. Defaults to False.
219240
220241 Returns:
@@ -236,6 +257,10 @@ def deploy(
236257 # prevent deepeval writing operations
237258 veadk_environments ["DEEPEVAL_TELEMETRY_OPT_OUT" ] = "YES"
238259
260+ enable_key_auth = False
261+ if auth_method == "api-key" :
262+ enable_key_auth = True
263+
239264 if use_adk_web :
240265 veadk_environments ["USE_ADK_WEB" ] = "True"
241266 else :
@@ -254,6 +279,12 @@ def deploy(
254279 gateway_service_name = f"{ application_name } -gw-svr-{ formatted_timestamp ()} "
255280 if not gateway_upstream_name :
256281 gateway_upstream_name = f"{ application_name } -gw-us-{ formatted_timestamp ()} "
282+ if not identity_user_pool_name :
283+ identity_user_pool_name = (
284+ f"{ application_name } -id-up-{ formatted_timestamp ()} "
285+ )
286+ if not identity_client_name :
287+ identity_client_name = f"{ application_name } -id-cli-{ formatted_timestamp ()} "
257288
258289 try :
259290 vefaas_application_url , app_id , function_id = self ._vefaas_service .deploy (
@@ -262,9 +293,102 @@ def deploy(
262293 gateway_name = gateway_name ,
263294 gateway_service_name = gateway_service_name ,
264295 gateway_upstream_name = gateway_upstream_name ,
296+ enable_key_auth = enable_key_auth ,
265297 )
266298 _ = function_id # for future use
267299
300+ veapig_gateway_id , _ , veapig_route_id = (
301+ self ._vefaas_service .get_application_route (app_id = app_id )
302+ )
303+
304+ if auth_method == "oauth2" :
305+ # Get or create the Identity user pool.
306+ identity_user_pool_id = self ._veidentity_service .get_user_pool (
307+ name = identity_user_pool_name ,
308+ )
309+ if not identity_user_pool_id :
310+ identity_user_pool_id = self ._veidentity_service .create_user_pool (
311+ name = identity_user_pool_name ,
312+ )
313+ issuer = f"https://auth.id.{ self .region } .volces.com/userpool/{ identity_user_pool_id } "
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"{ issuer } /authorize" ,
368+ "UpstreamId" : veapig_identity_upstream_id ,
369+ "TokenUrl" : f"{ issuer } /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"{ issuer } /keys" ,
381+ },
382+ "Issuer" : issuer ,
383+ "ValidateConsumer" : False ,
384+ }
385+ self ._vefaas_service .apig_client .create_plugin_binding (
386+ scope = "ROUTE" ,
387+ target = veapig_route_id ,
388+ plugin_name = plugin_name ,
389+ plugin_config = json .dumps (plugin_config ),
390+ )
391+
268392 return CloudApp (
269393 vefaas_application_name = application_name ,
270394 vefaas_endpoint = vefaas_application_url ,
0 commit comments