
    gh"F                        S SK r S SKrS SKrS SKrS SKJr  S SKJrJrJr  S SK	J
r
  S SKrS SKJr  S SKJr  S SKJr  S SKJr  S S	KJrJrJrJrJrJrJr  S S
KJr  S SKJr  S SK J!r!  \!(       a.  S SK J"r"  S SK J#r#  S SK J$r$  S SK J%r%  S SK&J'r'J(r(J)r)  \#" S\$S\"4   S9r*Sr+Sr,S r-S r.S r/ " S S\5      r0S r1S r2S r3S r4S  r5S! r6g)"    N)deepcopy)datetime	timedeltatimezone)environ)continue_trace)OP)should_send_default_pii)TransactionSource)AnnotatedValuecapture_internal_exceptionsensure_integration_enabledevent_from_exceptionloggerTimeoutThreadreraise)Integration)_filter_headers)TYPE_CHECKING)Any)TypeVar)Callable)Optional)EventProcessorEventHintF.)boundi  g     @@c                 :   ^  [        [        T 5      U 4S j5       nU$ )Nc                  $  > [         R                  " 5       n[        5          [         R                  " 5       R	                  5         [
        R                  " 5       nU(       a?  [        U5      (       a/  [        UUR                  SSS.S9u  pE[         R                  " XES9  OQU S   n[        U[        5      (       a  [        R                  " U5      n[        U5      n[         R                  " U5        S S S 5        T" U 0 UD6$ ! , (       d  f       N= f)N
aws_lambdaFtypehandledclient_options	mechanismhint   )
sentry_sdk
get_clientr   get_isolation_scopeclear_breadcrumbssysexc_infoallr   optionscapture_event
isinstancestrjsonloads_event_from_error_json)argskwargsclientr0   sentry_eventr)   
error_info
init_errors          e/home/kali/devsecops-assessor/venv/lib/python3.13/site-packages/sentry_sdk/integrations/aws_lambda.pysentry_init_error+_wrap_init_error.<locals>.sentry_init_error-   s     &&((***,>>@||~HCMM%9#)>>'3F&"
 ((A "!W
j#..!%J!7J5jA((6% +( 4*6**) +*s   CD
D)r   AwsLambdaIntegration)r>   r@   s   ` r?   _wrap_init_errorrC   +   s&     4jA+ B+4     c                 F   ^  [         R                  " T 5      U 4S j5       nU$ )Nc           
      P  > [         R                  " 5       nUR                  [        5      nUc  T" X/UQ70 UD6$ [	        U [
        5      (       a   [        U 5      S:  a  U S   n[        U 5      nOU nSn[	        U[        5      (       d  0 nUR                  5       n[         R                  " 5        n	S n
[        5          U	R                  5         U	R                  [        XaU5      5        U	R                  SUR                  R!                  S5      S   5        US:  a$  U	R                  SS5        U	R                  SU5        UR"                  (       a=  U[$        :  a3  U[$        -
  [&        -  n[)        UU[&        -  5      n
U
R+                  5         S S S 5        UR-                  S	0 5      n[	        U[        5      (       d  0 n[/        U[0        R2                  UR4                  [6        R8                  [        R:                  S
9n[         R<                  " UU US.S9    T" X/UQ70 UD6U
(       a  U
R?                  5         sS S S 5        sS S S 5        $ ! , (       d  f       N= f! [@         aP    [B        RD                  " 5       n[G        UURH                  SSS.S9u  nn[         RJ                  " UUS9  [M        U6    Of = f U
(       a  U
R?                  5         O! U
(       a  U
R?                  5         f f = fS S S 5        O! , (       d  f       O= fS S S 5        g ! , (       d  f       g = f)Nr*   r   
aws_region:   batch_requestT
batch_sizeheaders)opnamesourceorigin)	aws_eventaws_context)custom_sampling_contextr!   Fr"   r%   r(   )'r+   r,   get_integrationrB   r4   listlendictget_remaining_time_in_millisisolation_scoper   r.   add_event_processor_make_request_event_processorset_taginvoked_function_arnsplittimeout_warningTIMEOUT_WARNING_BUFFERMILLIS_TO_SECONDSr   startgetr   r	   FUNCTION_AWSfunction_namer   	COMPONENTrP   start_transactionstop	Exceptionr/   r0   r   r2   r3   r   )rQ   rR   r9   r:   r;   integrationrequest_datarK   configured_timescopetimeout_threadwaiting_timerL   transactionr0   r<   r)   handlers                    r?   sentry_handler%_wrap_handler.<locals>.sentry_handlerM   s    &&(,,-AB9CDCFCCi&&3y>Q+>$Q<LYJ$LJ,-- L%BBD'')U!N,.'')))1$?
  +"B"B"H"H"Ma"P >MM/48MM,
;
  //'*@@ (*@@)$*L &3$'*;;&N #((*= /@ #&&y"5G gt,,(?? ..(22+22K --!*#.)."9KDKFK &&++-) _ *)..l ! '"||~H)= '-~~+7E"J*&L$
 ,,\EX&' '%&++- &&++- &'  _ *))s{   0L=CIB
LK=I!&K==	L
I	L!AJ;8K:J;;K?K=K11K=4	L=
L	L
L%)	functoolswraps)rq   rr   s   ` r?   _wrap_handlerrv   K   s)    __We. e.N rD   c                      [        5          [        R                  " 5       n U R                  [        5      nUb  U R                  5         S S S 5        g ! , (       d  f       g = fN)r   r+   r,   rT   rB   flush)r;   rj   s     r?   _drain_queuerz      sD    	$	&&&(,,-AB" LLN 
'	&	&s   >A
A c                   <    \ rS rSrSrS\ 3rSS jr\S 5       rSr	g)	rB      r!   zauto.function.c                     Xl         g rx   r_   )selfr_   s     r?   __init__AwsLambdaIntegration.__init__   s    .rD   c                    ^^^ [        5       n U (       d  [        R                  " S5        g [        U S5      (       d  [        R                  " S5        g [        U S5      nU(       aI  U R                  mU4S jnX l        U R
                  mU4S jnX0l        U R                  mU4S jnX@l        g [        U R                  R                  5      U R                  l	        U R                  mU4S jnX l        S	 nU" U R                  R                  5      U R                  l
        U" U R                  R                  5      U R                  l        g )
NzfNot running in AWS Lambda environment, AwsLambdaIntegration disabled (could not find bootstrap module)handle_event_requestzjNot running in AWS Lambda environment, AwsLambdaIntegration disabled (could not find handle_event_request)handle_http_requestc                 2   > [        U 5      n T" U /UQ70 UD6$ rx   rv   )request_handlerr9   r:   old_handle_event_requests      r?   sentry_handle_event_requestDAwsLambdaIntegration.setup_once.<locals>.sentry_handle_event_request   s"    "/"@/Q$Q&QQrD   c                 2   > [        U 5      n T" U /UQ70 UD6$ rx   r   )r   r9   r:   old_handle_http_requests      r?   sentry_handle_http_requestCAwsLambdaIntegration.setup_once.<locals>.sentry_handle_http_request   s"    "/"@.PPPPrD   c                  (   > [        5         T" U 0 UD6$ rx   rz   )r9   r:   old_to_jsons     r?   sentry_to_json7AwsLambdaIntegration.setup_once.<locals>.sentry_to_json   s    "D3F33rD   c                 2   > [        U5      nT" X/UQ70 UD6$ rx   r   )lambda_runtime_clientr   r9   r:   r   s       r?   r   r     s/     #0"@/)=AEK rD   c                    ^  U 4S jnU$ )Nc                  (   > [        5         T" U 0 UD6$ rx   r   )r9   r:   fs     r?   innerKAwsLambdaIntegration.setup_once.<locals>._wrap_post_function.<locals>.inner  s     Nd-f--rD    )r   r   s   ` r?   _wrap_post_function<AwsLambdaIntegration.setup_once.<locals>._wrap_post_function  s    .
 rD   )get_lambda_bootstrapr   warninghasattrr   r   to_jsonrC   LambdaRuntimeClientpost_init_errorpost_invocation_resultpost_invocation_error)	lambda_bootstrappre_37r   r   r   r   r   r   r   s	         @@@r?   
setup_onceAwsLambdaIntegration.setup_once   sB    01NNR ')?@@NNV )+@A'7'L'L$R
 5P1&6&J&J#Q
 4N0
 +22K4
 (6$CS 44DDD00@ (8'L'L$ 5P1
 $$88OO 00G $$88NN 00FrD   r~   N)F)
__name__
__module____qualname____firstlineno__
identifierrP   r   staticmethodr   __static_attributes__r   rD   r?   rB   rB      s.    Jj\*F/ X XrD   rB   c                  b   S[         R                  ;   a  [         R                  S   $ S[         R                  ;   at  [         R                  S   n [        U S5      (       a1  [        U R                  S5      (       a  U R                  R                  $ [        U S5      (       a  U R                  $ U $ g )N	bootstrap__main__awslambdaricmain)r/   modulesr   r   r   )modules    r?   r   r   '  s    $ ckk!{{;''	s{{	"Z(6-..7##[4
 4
 **444V[))### rD   c                 j   ^ ^^ [         R                  " [        R                  5      nU4UU U4S jjnU$ )Nc                 l  > TR                  5       nTU-
  nU R                  S0 5      nTR                  TR                  TR                  TR
                  UUS.US'   [        TU5      TR                  TR                  S.US'   U R                  S0 5      nST;   a  TS   US'   [        TT5      US	'   S
T;   a  TS
   US'   ST;   a  [        TS   5      US'   [        5       (       a  U R                  S0 5      nTR                  S5      nUc  0 nUR                  S5      n	U	b  UR                  SU	5        UR                  S5      n
U
b  UR                  SU
5        ST;   a  TR                  SS5      US'   O/TR                  SS 5      (       a  [        R                  " 5       US'   [        U5      U S'   U $ )Nextra)re   function_versionr]   aws_request_idexecution_duration_in_millisremaining_time_in_millislambda)url	log_group
log_streamzcloudwatch logsrequest
httpMethodmethodr   queryStringParametersquery_stringrL   useridentityuserArnidsourceIp
ip_addressbody data)rX   
setdefaultre   r   r]   r   _get_cloudwatch_logs_urllog_group_namelog_stream_namerc   _get_urlr   r
   r   removed_because_raw_datar   )r<   r)   
start_timeremaining_time_in_milisexec_durationr   r   	user_infor   r   iprR   rQ   configured_timeouts              r?   event_processor6_make_request_event_processor.<locals>.event_processorP  s   "-"J"J"L*-DD''4(66 + < <$/$D$D)88,9(?
h ,KD$33%55$
  ""9b19$ ), 7GH!)[9"i/&/0G&HGN#	!!091E!FGI"$$$//;I }}Z0Hi(B~$$T2.j)B~$$\26""+--";}}VT** #1"I"I"K"*7"3YrD   )r   nowr   utc)rQ   rR   r   r   r   s   ```  r?   r[   r[   L  s+    hll+J7A ; ;z rD   c                    U R                  SS 5      nU R                  S5      nUc  0 nUR                  SS 5      nUR                  SS 5      nU(       a   U(       a  U(       a  SR                  XTU5      $ SR                  UR                  5      $ )NpathrL   HostzX-Forwarded-Protoz	{}://{}{}zawslambda:///{})rc   formatre   )rQ   rR   r   rL   hostprotos         r?   r   r     s|    ==&DmmI&G;;vt$DKK+T2E$!!%t44##K$=$=>>rD   c                 `   Sn[         R                  " SS5      nSR                  UR                  S5      (       a  SOSUU R                  U R
                  U[        SS	9-
  R                  U5      [        R                  " [        R                  5      [        S
S	9-   R                  U5      S9nU$ )z
Generates a CloudWatchLogs console URL based on the context object

Arguments:
    aws_context {Any} -- context from lambda handler

Returns:
    str -- AWS Console URL to logs.
z%Y-%m-%dT%H:%M:%SZ
AWS_REGIONr   zhttps://console.{domain}/cloudwatch/home?region={region}#logEventViewer:group={log_group};stream={log_stream};start={start_time};end={end_time}zcn-zamazonaws.cnzaws.amazon.comr*   )seconds   )domainregionr   r   r   end_time)r   rc   r   
startswithr   r   r   strftimer   r   r   r   )rR   r   formatstringr   r   s        r?   r   r     s     (L[[r*F	- f!'!2!25!9!9~?O,,..1!55??M,,x||,y/CCMM
  		  JrD   c                     / nU  Hh  n[         R                  " SUR                  5       5      nU(       d  M1  UR                  5       u  pEn[	        U5      nUR                  UUUS S S S S.5        Mj     U$ )Nz File "(.+)", line (\d+), in (.+))filenamefunctionlinenovarspre_contextcontext_linepost_context)rematchstripgroupsintappend)formatted_tbframesframer   	file_nameline_number	func_names          r?   _parse_formatted_tracebackr    st    F<ekkmL505-IIk*KMM ) )) #'$($(
   MrD   c                     SSU R                  S5      U R                  S5      S[        U R                  S/ 5      5      0SSS	.S
./0S.nU$ )a%  
Converts the error JSON from AWS Lambda into a Sentry error event.
This is not a full fletched event, but better than nothing.

This is an example of where AWS creates the error JSON:
https://github.com/aws/aws-lambda-python-runtime-interface-client/blob/2.2.1/awslambdaric/bootstrap.py#L479
errorvalues	errorTypeerrorMessager   
stackTracer!   Fr"   )r#   value
stacktracer'   )level	exception)rc   r  )
error_jsonevents     r?   r8   r8     sj     &NN;7'^^N; "<&NN<<## !-#("
E* LrD   )7rt   r6   r   r/   copyr   r   r   r   osr   r+   sentry_sdk.apir   sentry_sdk.constsr	   sentry_sdk.scoper
   sentry_sdk.tracingr   sentry_sdk.utilsr   r   r   r   r   r   r   sentry_sdk.integrationsr   $sentry_sdk.integrations._wsgi_commonr   typingr   r   r   r   r   sentry_sdk._typesr   r   r   r   r`   ra   rC   rv   rz   rB   r   r[   r   r   r  r8   r   rD   r?   <module>r     s      	 
  2 2   )   4 0   0 @  ==8CH-.A   @jZa; aH"JAH?@,rD   