Ë
    3ýj=u  ã                  óÄ   — d Z ddlmZ ddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ d	d
lmZmZmZ erd	dlmZ  ee«      Z G d„ d«      Z G d„ d«      Zy)ab  Amazon SNS fanout support for the AWS SQS transport module for Kombu.

This module provides a `SNS` class that can be used to manage SNS topics and subscriptions.
It's primarily used to provide fanout support via AWS Simple Notification Service (SNS)
topics and subscriptions. The module also provides methods for handling the lifecycle
of these topics.
é    )ÚannotationsN)Údatetime)ÚTYPE_CHECKING)ÚClientError)Ú
KombuError)Ú
get_loggeré   )Ú&UnableToSubscribeQueueToTopicExceptionÚ*UnableToUnsubscribeQueueFromTopicExceptionÚUndefinedExchangeException)ÚChannelc                  óŽ   — e Zd ZdZdd„Zdd„Z	 	 d	 	 	 	 	 	 	 	 	 dd„Zdd„Zdd„Zdd„Z	e
dd	„«       Zddd
„Zdd„Zdd„Z	 dd„Zy)ÚSNSzåA class to manage AWS Simple Notification Service (SNS) for fanout exchanges.

    This class maintains caches of SNS subscriptions, clients, topic ARNs etc to
    enable efficient management of SNS topics and subscriptions.
    c                óª   — || _         d | _        t        | «      | _        i | _        i | _        i | _        d | _        t        j                  «       | _
        y ©N)ÚchannelÚ_clientÚ_SnsSubscriptionÚsubscriptionsÚ_predefined_clientsÚ_topic_arn_cacheÚ_exchange_topic_cacheÚsts_expirationÚ	threadingÚLockÚ_lock)Úselfr   s     úQ/root/telegram-saas/.venv/lib/python3.12/site-packages/kombu/transport/SQS/SNS.pyÚ__init__zSNS.__init__'   sK   € ØˆŒØˆŒÜ-¨dÓ3ˆÔØ#%ˆÔ Ø02ˆÔØ57ˆÔ"Ø/3ˆÔÜ—^‘^Ó%ˆ�
ó    c                ó\   — | j                   j                  |«       | j                  |«       y)az  Initialise SNS topic for a fanout exchange.

        This method will create the SNS topic if it doesn't exist, and
        check for any SNS topic subscriptions that no longer exist.
        If there are any SNS topic subscriptions that no longer exist,
        then they will be removed.

        :param exchange_name: The name of the exchange.
        :returns: None
        N)r   ÚcleanupÚ_get_or_create_topic)r   Úexchange_names     r   Úinitialise_exchangezSNS.initialise_exchange1   s)   € ð 	×Ñ×"Ñ" =Ô1Ø×!Ñ! -Ô0Ør    Nc                ó  — | j                  |«      }||dœ}|j                  |xs i «       | j                  |«      x}r||d<    | j                  |«      j                  d	i |¤Ž}|d   d   x}	dk7  rt        d|› d|	› �«      ‚y)
a_  Send a notification to AWS Simple Notification Service (SNS).

        :param exchange_name: The name of the exchange.
        :param message: The message to be sent as a JSON string
        :param message_attributes: Attributes for the message.
        :param request_params: Additional parameters for SNS notification.
        :return: None
        )ÚTopicArnÚMessageÚMessageAttributesÚResponseMetadataÚHTTPStatusCodeéÈ   z!Unable to send message to topic 'z': status code was N© )r#   ÚupdateÚserialise_message_attributesÚ
get_clientÚpublishr   )
r   r$   ÚmessageÚmessage_attributesÚrequest_paramsÚ	topic_arnÚrequest_argsÚserialised_attrsÚresponseÚstatus_codes
             r   r1   zSNS.publish@   s¼   € ð  ×-Ñ-¨mÓ<ˆ	ð "Øñ/
ˆð 	×Ñ˜NÒ0¨bÔ1ð  $×@Ñ@ÐASÓTÐTÐÐTØ0@ˆLÐ,Ñ-ð :�4—?‘? =Ó1×9Ñ9ÑI¸LÑIˆØ#Ð$6Ñ7Ð8HÑIÐIˆKÈcÒQÜ,Ø3°I°;Ð>QÐR]ÐQ^Ð_óð ð Rr    c                óŽ  — | j                   j                  |«      x}r|S | j                  5  | j                   j                  |«      x}r|cddd«       S | j                  j                  r| j                  |«      cddd«       S | j                  |«      | j                   |<   | j                   |   cddd«       S # 1 sw Y   yxY w)a  Create the SNS topic if not found, otherwise get the topic ARN.

        This method will get the SNS topic ARN for the given exchange from the cache.
        If the topic ARN is not in the cache, it will create the topic and add the ARN
        to the cache before returning it.

        As specified here, if the topic already exists, AWS will return the existing
        topic ARN without creating a new topic:
        https://docs.aws.amazon.com/boto3/latest/reference/services/sns/client/create_topic.html

        :param exchange_name: The exchange to create the SNS topic for
        :return: The SNS topic ARN
        :raises UndefinedExchangeException: If predefined_exchanges are used and the
                topic is not defined in the predefined_exchanges
        N)r   Úgetr   r   Úpredefined_exchangesÚ&_get_topic_arn_for_predefined_exchangeÚ_create_sns_topic)r   r$   r5   s      r   r#   zSNS._get_or_create_topicd   s½   € ð" ×-Ñ-×1Ñ1°-Ó@Ð@ˆ9Ð@ØÐà�Z‰Zñ 	8à ×1Ñ1×5Ñ5°mÓDÐDˆyÐDØ ÷	8ñ 	8ð �|‰|×0Ò0Ø×BÑBÀ=ÓQ÷	8ñ 	8ð 48×3IÑ3IÈ-Ó3XˆD×!Ñ! -Ñ0Ø×(Ñ(¨Ñ7÷	8÷ 	8ò 	8ús   ¬B;Á&B;Â,B;Â;Cc                ó°   — | j                   j                  j                  |i «      j                  d«      x}r|| j                  |<   |S t	        d|› d�«      ‚)a%  Handles getting the topic ARN for predefined exchanges.

        :param exchange_name: The exchange name to get the topic ARN for
        :return: The SNS topic ARN for the exchange
        :raises UndefinedExchangeException: If the exchange is not defined in the predefined_exchanges
        ÚarnúExchange with name 'ú,' must be defined in 'predefined_exchanges'.)r   r<   r;   r   r   )r   r$   Úpre_defined_exchange_arns      r   r=   z*SNS._get_topic_arn_for_predefined_exchange†   sf   € ð (,§|¡|×'HÑ'H×'LÑ'LÈ]Ð\^Ó'_×'cÑ'cÐdiÓ'jÐjÐ#ÐjØ3KˆD×!Ñ! -Ñ0Ø+Ð+ô )Ø" = /Ð1]Ð^ó
ð 	
r    c                óZ  — | j                   j                  |«      }t        j                  d|› d�«       | j	                  «       j                  |d|j                  d«      rdndiddd	œd
dd	œg¬«      }|d   d   dk7  rt        d«      ‚|d   }t        j                  d|› d|› d�«       |S )zàCreates an AWS SNS topic.

        If the topic already exists, AWS will return it's ARN without creating a new one.

        :param exchange_name: The exchange to create the SNS topic for
        :return: Topic ARN
        zCreating SNS topic 'ú'Ú	FifoTopicz.fifoÚtrueÚfalseÚ	ManagedByzCelery/Kombu)ÚKeyÚValueÚDescriptionzEThis SNS topic is used by Kombu to enable Fanout support for AWS SQS.)ÚNameÚ
AttributesÚTagsr*   r+   r,   zUnable to create SNS topicr'   zCreated SNS topic 'z' with ARN ')	r   Úcanonical_queue_nameÚloggerÚdebugr0   Úcreate_topicÚendswithr   Úinfo)r   r$   Ú
topic_namer8   r@   s        r   r>   zSNS._create_sns_topic–   sÑ   € ð —\‘\×6Ñ6°}ÓEˆ
ä�‰Ð+¨J¨<°qÐ9Ô:ð —?‘?Ó$×1Ñ1Øà z×':Ñ':¸7Ô'C™VÈðð $¨nÑ=à*Ødñðð 2ó 
ˆð Ð&Ñ'Ð(8Ñ9¸SÒ@Ü,Ð-IÓJÐJð �zÑ"ˆÜ�‰Ð)¨*¨°\À#ÀÀaÐHÔIàˆ
r    c                ód   — | si S i }| j                  «       D ]  \  }}dt        |«      dœ||<   Œ |S )zÑSerialises SQS message attributes into SNS format.

        :param message_attributes: A dictionary of message attributes
        :returns: A dictionary of serialised message attributes in SNS format.
        ÚString)ÚDataTypeÚStringValue)ÚitemsÚstr)r3   ÚattrsÚkeyÚvalues       r   r/   z SNS.serialise_message_attributesº   sI   € ñ "ØˆIàˆØ,×2Ñ2Ó4ò 	‰JˆC�à'Ü" 5›zñˆE�#ŠJð	ð ˆr    c                ó¢  — |��H| j                   j                  �r1| j                   j                  j                  |«      x}st        d|› d�«      ‚| j                   j                  j                  d«      r| j                  ||«      S | j                  j                  |«      x}r|S | j                  |j                  d| j                   j                  «      |j                  d| j                   j                  j                  «      |j                  d| j                   j                  j                  «      ¬«      x}| j                  |<   |S | j                  �| j                  S | j                  | j                   j                  | j                   j                  j                  | j                   j                  j                  ¬«      x}| _        |S )aÎ  Get or create a Boto SNS client.

        If an SNS client has already been initialised for this Channel instance, return it. If not, create a new SNS
        client, add it to this Channel instance and return it.

        If the exchange is defined in the predefined_exchanges, then return the client for the exchange and handle
        any STS token renewal.

        :param exchange_name: The name of the exchange
        :returns: A Boto SNS client.
        rA   rB   Ústs_role_arnÚregionÚaccess_key_idÚsecret_access_key)rb   rc   rd   )r   r<   r;   r   Útransport_optionsÚ_handle_sts_sessionr   Ú_create_boto_clientrb   ÚconninfoÚuseridÚpasswordr   )r   r$   ÚeÚcs       r   r0   zSNS.get_clientÍ   s˜  € ð Ñ$¨¯©×)JÓ)JàŸ™×:Ñ:×>Ñ>¸}ÓMÐM�AÐMÜ0Ø*¨=¨/Ð9eÐfóð ð
 �|‰|×-Ñ-×1Ñ1°.ÔAØ×/Ñ/°¸qÓAÐAð ×,Ñ,×0Ñ0°Ó?Ð?ˆqÐ?Ø�ð ;?×:RÑ:RØ—u‘u˜X t§|¡|×':Ñ':Ó;ØŸe™e O°T·\±\×5JÑ5J×5QÑ5QÓRØ"#§%¡%Ø'¨¯©×)>Ñ)>×)GÑ)Gó#ð ;Só ;ð ˆA�×(Ñ(¨Ñ7ð ˆHð �<‰<Ð#Ø—<‘<Ðð  ×3Ñ3Ø—<‘<×&Ñ&ØŸ,™,×/Ñ/×6Ñ6Ø"Ÿl™l×3Ñ3×<Ñ<ð 4ó 
ð 	
ˆˆDŒLð
 ˆr    c                óò   — | j                   j                  || j                  | j                  ¬«      r7| j	                  ||j                  d| j                   j                  «      ¬«      S | j                  |   S )zËChecks if the STS token needs renewing for SNS.

        :param exchange_name: The exchange name
        :param e: The exchange object
        :returns: The SNS client with a refreshed STS token
        )ÚnameÚ
client_mapÚexpire_timerb   )rb   )r   Úis_sts_token_refresh_requiredr   r   Ú$_create_boto_client_with_sts_sessionr;   rb   )r   r$   rk   s      r   rf   zSNS._handle_sts_sessionÿ   sw   € ð �<‰<×5Ñ5ØØ×/Ñ/Ø×+Ñ+ð 6ô 
ð
 ×<Ñ<Ø a§e¡e¨H°d·l±l×6IÑ6IÓ&Jð =ó ð ð
 ×'Ñ'¨Ñ6Ð6r    c                ó¨   — | j                   j                  «       }|d   | _        | j                  ||d   |d   |d   ¬«      x}| j                  |<   |S )zÙCreates a new SNS client with a refreshed STS token.

        :param exchange_name: The exchange name
        :param region: The AWS region to use.
        :returns: The SNS client with a refreshed STS token.
        Ú
ExpirationÚAccessKeyIdÚSecretAccessKeyÚSessionToken)rb   rc   rd   Úsession_token)r   Úget_sts_credentialsr   rg   r   )r   r$   rb   Ú	sts_credsrl   s        r   rr   z(SNS._create_boto_client_with_sts_session  sp   € ð —L‘L×4Ñ4Ó6ˆ	Ø'¨Ñ5ˆÔð 7;×6NÑ6NØØ# MÑ2Ø'Ð(9Ñ:Ø# NÑ3ð	 7Oó 7
ð 	
ˆˆD×$Ñ$ ]Ñ3ð ˆr    c                óB   — | j                   j                  d||||¬«      S )a€  Create a new SNS client.

        :param region: The AWS region to use.
        :param access_key_id: The AWS access key ID for authenticating with boto.
        :param secret_access_key: The AWS secret access key for authenticating with boto.
        :param session_token: The AWS session token for authenticating with boto, if required.
        :returns: A Boto SNS client.
        Úsns)Úservicerb   rc   rd   rx   )r   Ú_new_boto_client)r   rb   rc   rd   rx   s        r   rg   zSNS._create_boto_client'  s/   € ð �|‰|×,Ñ,ØØØ'Ø/Ø'ð -ó 
ð 	
r    )r   r   ©r$   r\   ÚreturnÚNone)NN)
r$   r\   r2   r\   r3   údict | Noner4   r‚   r€   r�   )r$   r\   r€   r\   )r3   r‚   r€   Údictr   )r$   z
str | None)r$   r\   rk   rƒ   )r$   r\   rb   r\   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r%   r1   r#   r=   r>   Ústaticmethodr/   r0   rf   rr   rg   r-   r    r   r   r       s�   „ ñó&óð& +/Ø&*ð"àð"ð ð"ð (ð	"ð
 $ð"ð 
ó"óH 8óD
ó "ðH òó ðô$0ód7ó(ð* GKô
r    r   c                  óÆ   — e Zd Zdd„Zdd„Z	 	 	 	 	 	 	 	 dd„Zdd„Zdd„Zdd„Z	 	 	 	 	 	 dd„Z	dd„Z
edd	„«       Zedd
„«       Zedd„«       Zdd„Zdd„Zdd„Zd d„Zdd„Zy)!r   c                ó`   — || _         i | _        i | _        t        j                  «       | _        y r   )r|   Ú_queue_arn_cacheÚ_subscription_arn_cacher   r   r   )r   Ú
sns_fanouts     r   r   z_SnsSubscription.__init__@  s'   € ØˆŒØ02ˆÔØ79ˆÔ$Ü—^‘^Ó%ˆ�
r    c                óv   — |› d|› �}| j                   j                  |«      x}r|S | j                  |||¬«      S )a'  Subscribes a queue to an AWS SNS topic.

        :param queue_name: The queue to subscribe
        :param exchange_name: The exchange to subscribe to the queue, if not provided
        :raises: UndefinedExchangeException if exchange is not defined.
        :return: The subscription ARN
        ú:)Ú	cache_keyÚ
queue_namer$   )rŒ   r;   Ú8_handle_create_new_subscription_for_predefined_exchanges)r   r‘   r$   r�   Úsubscription_arns        r   Úsubscribe_queuez _SnsSubscription.subscribe_queueF  sY   € ð %�o Q z lÐ3ˆ	ð  $×;Ñ;×?Ñ?À	ÓJÐJÐÐJØ#Ð#à×LÑLØ¨JÀmð Mó 
ð 	
r    c                ó^  — | j                   5  | j                  j                  |«      x}r|cddd«       S | j                  |«      }| j                  j                  |«      }| j                  ||¬«      }| j                  |||¬«       || j                  |<   ddd«       |S # 1 sw Y   S xY w)al  Handles creating a new subscription for predefined exchanges.

        :param cache_key: The cache key for the subscription ARN cache
        :param queue_name: The queue to subscribe
        :param exchange_name: The exchange to subscribe to the queue
        :raises UndefinedExchangeException: If the exchange is not defined in the predefined_exchanges
        N)Ú	queue_arnr5   )r5   r–   r‘   )r   rŒ   r;   Ú_get_queue_arnr|   r#   Ú_subscribe_queue_to_sns_topicÚ_set_permission_on_sqs_queue)r   r�   r‘   r$   r“   r–   r5   s          r   r’   zI_SnsSubscription._handle_create_new_subscription_for_predefined_exchangesX  sÍ   € ð �Z‰Zñ 	GØ#'×#?Ñ#?×#CÑ#CÀIÓ#NÐNÐÐNØ'÷	Gñ 	Gð
 ×+Ñ+¨JÓ7ˆIØŸ™×5Ñ5°mÓDˆIð  $×AÑAØ#¨yð  Bó  Ðð
 ×-Ñ-Ø#¨yÀZð .ô ð
 7GˆD×(Ñ(¨Ñ3÷'	Gð*  Ð÷+	Gð*  Ðús   �B"¶A"B"Â"B,c           	     ó†  — |› d|› �}| j                   j                  |«      x}sy	 | j                  |«       t        j	                  d|› d|› d�«       	 | j                   j                  |d«       y# t
        $ r$}t        j                  d|› d|› d|› �«       ‚ d}~ww xY w# | j                   j                  |d«       w xY w)	zÈUnsubscribes a queue from an AWS SNS topic.

        :param queue_name: The queue to unsubscribe
        :param exchange_name: The exchange to unsubscribe from the queue
        :return: None
        r�   NzUnsubscribed subscription 'ú' for SQS queue 'rE   zFailed to unsubscribe queue 'z' from SNS topic 'ú': )rŒ   r;   Ú_unsubscribe_sns_subscriptionrQ   rU   Ú	ExceptionÚerrorÚpop)r   r‘   r$   r�   r“   rk   s         r   Úunsubscribe_queuez"_SnsSubscription.unsubscribe_queuey  sÚ   € ð %�o Q z lÐ3ˆ	à$(×$@Ñ$@×$DÑ$DÀYÓ$OÐOÐ ÐOØð	>Ø×.Ñ.Ð/?Ô@Ü�K‰KØ-Ð.>Ð-?Ð?PÐQ[ÐP\Ð\]Ð^õð ×(Ñ(×,Ñ,¨Y¸Õ=øô ò 	Ü�L‰LØ/°
¨|Ð;MÈmÈ_Ð\_Ð`aÐ_bÐcôð ûð		ûð ×(Ñ(×,Ñ,¨Y¸Õ=ús#   §-A2 Á2	BÁ;BÂBÂB" Â"C c           
     óÈ  — | j                   j                  j                  rt        j	                  d«       yt        j	                  d|› d�«       | j                   j                  |«      }| j                  |«      D ]0  }	 | j                  |«       t        j                  d|› d|› d�«       Œ2 y# t        $ r(}t        j                  d|› d|› d|› �«       Y d}~Œ`d}~ww xY w)	a9  Removes any stale SNS topic subscriptions.

        This method will check that any SQS subscriptions on the SNS topic are associated with SQS queues. If not,
        it will remove the stale subscription.

        :param exchange_name: The exchange to check for stale subscriptions
        :return: None
        z]'predefined_exchanges' has been specified, so stale SNS subscription cleanup will be skipped.Nz3Checking for stale SNS subscriptions for exchange 'rE   zRemoved stale subscription 'z' for SNS topic 'z%Failed to remove stale subscription 'rœ   )r|   r   r<   rQ   rR   r#   Ú_get_invalid_sns_subscriptionsr�   rU   rž   Úwarning)r   r$   r5   r“   rk   s        r   r"   z_SnsSubscription.cleanup”  sð   € ð �8‰8×Ñ×0Ò0Ü�L‰LØoôð ä�‰ØAÀ-ÀÐPQÐRô	
ð
 —H‘H×1Ñ1°-Ó@ˆ	ð !%× CÑ CÀIÓ Nò 	Ðð
Ø×2Ñ2Ð3CÔDÜ—‘Ø2Ð3CÐ2DÐDUÐV_ÐU`Ð`aÐbõñ		øô ò Ü—‘Ø;Ð<LÐ;MÐM^Ð_hÐ^iÐilÐmnÐloÐp÷ñ ûðús   Â -B0Â0	C!Â9CÃC!c           	     ó  — t         j                  d|› d|› d�«       | j                  j                  «       j	                  |d|ddid¬«      }|d	   d
   x}dk7  rt        d|› �«      ‚|d   }t         j                  d|› d|› d|› d�«       |S )a  Subscribes a queue to an AWS SNS topic.

        :param queue_arn: The ARN of the queue to subscribe
        :param topic_arn: The ARN of the SNS topic to subscribe to
        :raises: UndefinedExchangeException if exchange is not defined.
        :return: The subscription ARN
        zSubscribing queue 'z' to SNS topic 'rE   ÚsqsÚRawMessageDeliveryrG   T)r'   ÚProtocolÚEndpointrN   ÚReturnSubscriptionArnr*   r+   r,   z+Unable to subscribe queue: status code was ÚSubscriptionArnzCreate subscription 'r›   )rQ   rR   r|   r0   Ú	subscriber
   rU   )r   r–   r5   r8   r9   r“   s         r   r˜   z._SnsSubscription._subscribe_queue_to_sns_topicº  sÎ   € ô 	�‰Ð*¨9¨+Ð5EÀiÀ[ÐPQÐRÔSð —8‘8×&Ñ&Ó(×2Ñ2ØØØØ,¨fÐ5Ø"&ð 3ó 
ˆð $Ð$6Ñ7Ð8HÑIÐIˆKÈcÒQÜ8Ð;fÐgrÐfsÐ9tÓuÐuð $Ð$5Ñ6ÐÜ�‰Ø#Ð$4Ð#5Ð5FÀyÀkð RØ$˜+ Qð(ô	
ð
  Ðr    c                ó  — | j                   j                  j                  |¬«      }| j                   j                  j                  |«      }| j	                  |||«      }| j                  |||«      }| j                  ||||¬«       y)a   Sets the permissions on an AWS SQS queue to enable the SNS topic to publish to the queue.

        :param topic_arn: The ARN of the SNS topic
        :param queue_name: The queue name to set permissions for
        :param queue_arn: The ARN of the SQS queue
        :return: None
        ©Úqueue©Ú
sqs_clientÚ	queue_urlÚpolicyr5   N)r|   r   r¦   Ú_resolve_queue_urlÚ_get_existing_queue_policyÚ_generate_new_sqs_policyÚ_set_policy_on_sqs_queue)r   r5   r‘   r–   r±   r²   Úexisting_policyÚ
new_policys           r   r™   z-_SnsSubscription._set_permission_on_sqs_queueØ  sƒ   € ð —X‘X×%Ñ%×)Ñ)°
Ð)Ó;ˆ
Ø—H‘H×$Ñ$×7Ñ7¸
ÓCˆ	à×9Ñ9¸*ÀjÐR[Ó\ˆØ×2Ñ2°?ÀIÈyÓYˆ
à×%Ñ%Ø!ØØØð	 	&õ 	
r    c                óJ  — t        j                  |«      }|j                  dd«       | j                  |«      }ddddid|dd	|iid
œ}d}t	        |«      D ]3  \  }}	t        |	t        «      sŒ|	j                  d«      dk(  sŒ,|||<   d} n |s|j                  |«       ||d<   |S )a-  Adds a statement to the existing SQS queue policy to allow the SNS topic to publish to the queue.

        This method checks to see if there is an existing Kombu-managed statement in the policy,
        and if so, it updates the statement with the new topic ARN.
        If not, it adds a new statement to the policy.

        :param existing_policy: The existing SQS queue policy
        :param topic_arn: The ARN of the SNS topic
        :param queue_arn: The ARN of the SQS queue
        :return: The updated policy with the new statement added
        ÚVersionz
2012-10-17ÚKombuManagedÚAllowÚServicezsns.amazonaws.comzSQS:SendMessageÚArnLikezaws:SourceArn)ÚSidÚEffectÚ	PrincipalÚActionÚResourceÚ	ConditionFrÀ   TÚ	Statement)	ÚcopyÚdeepcopyÚ
setdefaultÚ_extract_statements_from_policyÚ	enumerateÚ
isinstancerƒ   r;   Úappend)
r   r¸   r5   r–   r¹   Ú
statementsÚkombu_statementÚupdatedÚindexÚstmts
             r   r¶   z)_SnsSubscription._generate_new_sqs_policyï  sÊ   € ô —]‘] ?Ó3ˆ
à×Ñ˜i¨Ô6Ø×9Ñ9¸*ÓEˆ
ð (Ø Ø#Ð%8Ð9Ø*Ø"Ø# o°yÐ%AÐBñ
ˆð ˆÜ$ ZÓ0ò 	‰KˆE�4Ü˜$¤Õ%¨$¯(©(°5«/¸^Ó*KØ$3�
˜5Ñ!Ø�Ùð		ñ Ø×Ñ˜oÔ.à",ˆ
�;ÑØÐr    c                óš   — | rt        | t        «      sg S | j                  d«      }t        |t        «      r|S t        |t        «      r|gS g S )zéExtracts the statements from an SQS queue policy.

        :param policy: The SQS queue policy to extract the statements from
        :return: A list of statements from the policy, or an empty list if there are no statements
        rÆ   )rÌ   rƒ   r;   Úlist)r³   rÎ   s     r   rÊ   z0_SnsSubscription._extract_statements_from_policy  sJ   € ñ œZ¨´Ô5ØˆIà—Z‘Z Ó,ˆ
Ü�j¤$Ô'ØÐÜ˜
¤DÔ)Ø�<ÐàˆIr    c                ó†   — | j                  |dt        j                  |«      i¬«       t        j	                  d|› d�«       y)aa  Sets the given policy on the SQS queue.

        :param sqs_client: The SQS client to use for setting the queue attributes
        :param queue_url: The URL of the SQS queue to set the policy on
        :param policy: The policy to set on the SQS queue
        :param topic_arn: The ARN of the SNS topic (used for logging)
        :return: None
        ÚPolicy)ÚQueueUrlrN   zSet permissions on SNS topic 'rE   N)Úset_queue_attributesÚjsonÚdumpsrQ   rR   r°   s       r   r·   z)_SnsSubscription._set_policy_on_sqs_queue*  sE   € ð 	×'Ñ'Øàœ$Ÿ*™* VÓ,ðð 	(ô 	
ô 	�‰Ð5°i°[ÀÐBÕCr    c                óŽ  — 	 | j                  |dg¬«      j                  di «      }|j                  d«      x}r't        j                  |«      }t	        |t
        «      r|S i S # t        t        f$ r&}t        j                  d|› d|› �«       Y d}~i S d}~wt        $ r&}t        j                  d|› d|› �«       Y d}~i S d}~ww xY w)	a  Retrieves the existing SQS queue policy.

        We retrieve the existing policy so that we can add a statement for the SNS topic
        without overwriting any existing statements that may be required for other integrations.

        :param sqs_client: The SQS client to use for retrieving the queue attributes
        :param queue_name: The name of the SQS queue
        :param queue_url: The URL of the SQS queue
        :return: The existing queue policy as a dictionary, or an empty dictionary if the policy cannot be retrieved
        rÖ   ©r×   ÚAttributeNamesrN   zExisting SQS policy for queue 'z' is malformed: Nz2Unable to retrieve existing SQS policy for queue 'rœ   )Úget_queue_attributesr;   rÙ   ÚloadsrÌ   rƒ   Ú	TypeErrorÚ
ValueErrorrQ   r¤   r   )r±   r‘   r²   r]   r¸   Úparsed_policyrk   s          r   rµ   z+_SnsSubscription._get_existing_queue_policy<  sæ   € ð	Ø×3Ñ3Ø"Ø (˜zð 4ó ÷ ‰c�, Ó#ð ð #(§)¡)¨HÓ"5Ð5ˆÐ5Ü $§
¡
¨?Ó ;�Ü˜m¬TÔ2Ø(Ð(ð ˆ	øô œ:Ð&ò 	ä�N‰NØ1°*°Ð=MÈaÈSÐQ÷ð ð ˆ	ûô ò 	ä�N‰NØDÀZÀLÐPSÐTUÐSVÐW÷ð ð ˆ	ûð	ús$   ‚AA" Á"CÁ1BÂCÂB?Â?Cc                ó�   — | j                   j                  «       j                  |¬«      }|d   d   x}dk7  rt        d|› d�«      ‚y)a   Unsubscribes a subscription from an AWS SNS topic.

        :param subscription_arn: The ARN of the subscription to unsubscribe
        :return: None
        :raises UnableToUnsubscribeQueueFromTopicException: If the SNS unsubscribe API call fails
        )r«   r*   r+   r,   z*SNS unsubscribe API returned status code 'rE   N)r|   r0   Úunsubscriber   )r   r“   r8   r9   s       r   r�   z._SnsSubscription._unsubscribe_sns_subscription^  s]   € ð —8‘8×&Ñ&Ó(×4Ñ4ÐEUÐ4ÓVˆØ#Ð$6Ñ7Ð8HÑIÐIˆKÈcÒQÜ<Ø<¸[¸MÈÐKóð ð Rr    c                óè   — | j                   j                  «       j                  d«      }g }|j                  |¬«      D ]1  }|j	                  | j                  |j                  d«      «      «       Œ3 |S )zÓGet a list of all invalid SQS subscriptions associated with a given SNS topic.

        :param sns_topic_arn: The SNS topic ARN to check
        :return: A list of SNS subscription ARNs that are invalid
        Úlist_subscriptions_by_topic)r'   ÚSubscriptions)r|   r0   Úget_paginatorÚpaginateÚextendÚ!_filter_sns_subscription_responser;   )r   Úsns_topic_arnÚ	paginatorÚinvalid_subscription_arnsr8   s        r   r£   z/_SnsSubscription._get_invalid_sns_subscriptionsk  st   € ð —H‘H×'Ñ'Ó)×7Ñ7Ð8UÓVˆ	ð %'Ð!Ø!×*Ñ*°MÐ*ÓBò 	ˆHØ%×,Ñ,Ø×6Ñ6°x·|±|ÀOÓ7TÓUõð	ð
 )Ð(r    c                óŠ  ‡— g }|s|S |D ]r  }|j                  dd«      j                  «       dk(  sŒ'|d   j                  d«      d   }	 | j                  j                  j                  «       j                  |¬«       Œt |S # t        $ r8Šdd	g}t        ˆfd
„|D «       «      r|j                  |d   «       n‚ Y dŠ‹Œ³dŠ‹ww xY w)zÝReturns a list of SNS subscription ARNs that are not associated with a SQS queue.

        :param subscriptions: A list of subscriptions for an SNS topic
        :return: A list of subscription ARNs that are dead
        r¨   Ú r¦   r©   r�   éÿÿÿÿ)Ú	QueueNameÚQueueDoesNotExistÚNonExistentQueuec              3  ó8   •K  — | ]  }|t        ‰«      v –— Œ y ­wr   )r\   )Ú.0Úerrrk   s     €r   ú	<genexpr>zE_SnsSubscription._filter_sns_subscription_response.<locals>.<genexpr>�  s   øè ø€ ÒC¨�sœc !›f”}ÑCùs   ƒr«   N)
r;   ÚlowerÚsplitr|   r   r¦   Úget_queue_urlr   ÚanyrÍ   )r   r   Úsubscription_arnsÚsubscriptionr‘   Úqueue_missing_errsrk   s         @r   rë   z2_SnsSubscription._filter_sns_subscription_response|  sá   ø€ ð Ðñ Ø$Ð$ð *ò 	ˆLà×#Ñ# J°Ó3×9Ñ9Ó;¸uÒDØð
 & jÑ1×7Ñ7¸Ó<¸RÑ@ˆJð	Ø—‘× Ñ ×$Ñ$Ó&×4Ñ4¸zÐ4ÕJð	ð2 !Ð øô ò Ø&9Ð;MÐ%NÐ"ô ÓCÐ0BÔCÔCØ%×,Ñ,¨\Ð:KÑ-LÕMàô Nûðús   Á	4BÂ	CÂ
.B=Â=Cc                óà   — | j                   j                  |«      x}r|S | j                  j                  j	                  |«      }| j                  ||«      }|d   d   x}| j                   |<   |S )zîReturns the ARN of the SQS queue associated with the given queue.

        This method will return the ARN from the cache if it exists, otherwise it will fetch it from SQS.

        :param queue_name: The queue to get the ARN for
        rN   ÚQueueArn)r‹   r;   r|   r   r´   Ú_get_queue_attributes)r   r‘   r@   r²   Úqueue_attrss        r   r—   z_SnsSubscription._get_queue_arn¤  su   € ð ×'Ñ'×+Ñ+¨JÓ7Ð7ˆ3Ð7ØˆJà—H‘H×$Ñ$×7Ñ7¸
ÓCˆ	Ø×0Ñ0°¸YÓGˆØ2=¸lÑ2KÈJÑ2WÐWˆˆd×#Ñ# JÑ/Øˆ
r    c                ó´   — | j                   j                  j                  |¬«      j                  |dg¬«      }|d   d   x}dk(  r|S t	        d|› d|› d	�«      ‚)
a&  Gets the attributes of the SQS queue.

        :param queue_name: The queue to get the attributes for
        :param queue_url: The URL of the queue to get the attributes for
        :return: The attributes of the queue
        :raises: KombuError if the attributes cannot be retrieved
        r®   r  rÜ   r*   r+   r,   z!Unable to get ARN for SQS queue 'z': status code was 'rE   )r|   r   r¦   rÞ   r   )r   r‘   r²   r8   r9   s        r   r  z&_SnsSubscription._get_queue_attributes³  s{   € ð —8‘8×#Ñ#×'Ñ'¨jÐ'Ó9×NÑNØ"°J°<ð Oó ˆð $Ð$6Ñ7Ð8HÑIÐIˆKÈcÒQØˆOäØ/°
¨|Ð;OÐP[È}Ð\]Ð^ó
ð 	
r    N)r�   r   )r‘   r\   r$   r\   r€   r\   )r�   r\   r‘   r\   r$   r\   r€   r\   )r‘   r\   r$   r\   r€   r�   r   )r–   r\   r5   r\   r€   r\   )r5   r\   r‘   r\   r–   r\   )r¸   rƒ   r5   r\   r–   r\   r€   rƒ   )r³   rƒ   r€   z
list[dict])r²   r\   r³   rƒ   r5   r\   r€   r�   )r‘   r\   r²   r\   r€   rƒ   )r“   r\   r€   r�   )rì   r\   r€   ú	list[str])r   zlist[dict] | Noner€   r  )r‘   r\   r€   r\   )r„   r…   r†   r   r”   r’   r¡   r"   r˜   r™   r¶   rˆ   rÊ   r·   rµ   r�   r£   rë   r—   r  r-   r    r   r   r   ?  s¾   „ ó&ó
ð$ Øð Ø*-ð Ø>Að à	ó óB>ó6$óL ð<
Øð
Ø*-ð
Ø:=ó
ó.'ðR òó ðð" òDó ðDð" òó ðóBó)ó"&!óPô
r    r   )r‡   Ú
__future__r   rÇ   rÙ   r   r   Útypingr   Úbotocore.exceptionsr   Úkombu.exceptionsr   Ú	kombu.logr   Ú
exceptionsr
   r   r   rð   r   r„   rQ   r   r   r-   r    r   ú<module>r     s\   ðñõ #ã Û Û Ý Ý  å +å 'Ý  ÷5ñ 5ñ
 Ýá	�HÓ	€÷X
ñ X
÷~D
ò D
r    