1
1
import codecs
2
2
import json
3
3
import logging
4
+ import os
4
5
import re
5
6
import sys
6
7
import time
19
20
from ark_sdk_python .args import ArkArgsFormatter , ArkInquirerRender
20
21
from ark_sdk_python .auth .identity .ark_identity_fqdn_resolver import ArkIdentityFQDNResolver
21
22
from ark_sdk_python .common import ArkKeyring , ArkSystemConfig , get_logger
23
+ from ark_sdk_python .common .env import AwsEnv
22
24
from ark_sdk_python .models import ArkException , ArkNonInteractiveException
23
25
from ark_sdk_python .models .ark_exceptions import ArkAuthException
24
26
from ark_sdk_python .models .ark_profile import ArkProfile
@@ -75,6 +77,7 @@ def __init__(
75
77
username : str ,
76
78
password : Optional [str ],
77
79
identity_url : Optional [str ] = None ,
80
+ identity_tenant_subdomain : Optional [str ] = None ,
78
81
mfa_type : Optional [str ] = None ,
79
82
logger : Optional [logging .Logger ] = None ,
80
83
cache_authentication : bool = True ,
@@ -84,7 +87,7 @@ def __init__(
84
87
) -> None :
85
88
self .__username = username
86
89
self .__password = password
87
- self .__identity_url = identity_url or self .__resolve_fqdn_from_username ( )
90
+ self .__identity_url = self .__resolve_fqdn_from_username_or_subdomain ( identity_url , identity_tenant_subdomain )
88
91
if not self .__identity_url .startswith ('https://' ):
89
92
self .__identity_url = f'https://{ self .__identity_url } '
90
93
self .__mfa_type = mfa_type
@@ -158,15 +161,24 @@ def __save_cache(self, profile: Optional[ArkProfile] = None) -> None:
158
161
f'{ self .__username } _identity_session' ,
159
162
)
160
163
161
- def __resolve_fqdn_from_username (self ) -> str :
164
+ def __resolve_fqdn_from_username_or_subdomain (self , identity_url : Optional [str ], identity_tenant_subdomain : Optional [str ]) -> str :
165
+ if identity_tenant_subdomain and not identity_url :
166
+ try :
167
+ identity_url = ArkIdentityFQDNResolver .resolve_tenant_fqdn_from_tenant_subdomain (
168
+ identity_tenant_subdomain , AwsEnv (os .environ .get ('DEPLOY_ENV' , AwsEnv .PROD .value ))
169
+ )
170
+ except Exception as ex :
171
+ self .__logger .warning (f'Failed to resolve url from tenant subdomain, falling back to user [{ str (ex )} ]' )
172
+ if identity_url :
173
+ return identity_url
162
174
tenant_suffix = self .__username [self .__username .index ('@' ) :]
163
175
return ArkIdentityFQDNResolver .resolve_tenant_fqdn_from_tenant_suffix (tenant_suffix = tenant_suffix )
164
176
165
177
def __start_authentication (self ) -> StartAuthResponse :
166
178
self .__logger .info (f'Starting authentication with user { self .__username } and fqdn { self .__identity_url } ' )
167
179
response = self .__session .post (
168
180
url = f'{ self .__identity_url } /Security/StartAuthentication' ,
169
- json = {'User' : self .__username , 'Version' : '1.0' , 'PlatformTokenResponse' : True },
181
+ json = {'User' : self .__username , 'Version' : '1.0' , 'PlatformTokenResponse' : True , 'MfaRequestor' : 'DeviceAgent' },
170
182
)
171
183
try :
172
184
parsed_res : StartAuthResponse = StartAuthResponse .parse_raw (response .text )
@@ -381,19 +393,26 @@ def has_cache_record(cls, profile: ArkProfile, username: str, refresh_auth_allow
381
393
382
394
@classmethod
383
395
@cached (cache = LRUCache (maxsize = 1024 ))
384
- def is_idp_user (cls , username : str ) -> bool :
396
+ def is_idp_user (cls , username : str , identity_url : Optional [ str ], identity_tenant_subdomain : Optional [ str ] ) -> bool :
385
397
"""
386
398
Checks whether or not the specified username is from an external IDP.
387
399
388
400
Args:
389
401
username (str): _description_
402
+ identity_url (Optional[str]): _description_
403
+ identity_tenant_subdomain (Optional[str]): _description_
390
404
391
405
Returns:
392
406
bool: _description_
393
407
"""
394
408
if re .match ('.*@cyberark\\ .cloud\\ .(\\ d)+' , username ) is not None :
395
409
return False
396
- identity = ArkIdentity (username = username , password = '' )
410
+ identity = ArkIdentity (
411
+ username = username ,
412
+ password = '' ,
413
+ identity_url = identity_url ,
414
+ identity_tenant_subdomain = identity_tenant_subdomain ,
415
+ )
397
416
resp = identity .__start_authentication ()
398
417
return resp .result .idp_redirect_url != None
399
418
0 commit comments