o
    |#g                     @   s   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 e ZG dd deZG d	d
 d
eZG dd deZdS )    )StepTask)GatlingTask)HttpCmd)FailException)kubectl)get_pyrock_runc                   @   sP   e Zd 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dS )!EnableDsRemotePasswordHashingTaskz ds-password-hashing-microservicec                 C   s   | j jdstddS )Checking task configz	ds-idrepoz"Source must be ds-idrepo componentN)sourceinstance_name
startswithr   self r   Z/mnt/disk1/xslou/workshop/lodestar-fork/pyrock/tests/stress/am/authn_rest/tasks/mytasks.pypre   s   z%EnableDsRemotePasswordHashingTask.prec                 C   s   t d| j | jd dS )z8Verify that the password hashing microserice is deployedzget service 	componentN)r   microservice_namer
   r   r   r   r   step1   s   z'EnableDsRemotePasswordHashingTask.step1c                 C   s&   t d| j d| j  | jd dS )zPScale the password hashing microserice to the same replica number than ds-idrepozscale deployment z --replicas=r   N)r   r   r
   get_expected_replica_countr   r   r   r   step2!   s   
z'EnableDsRemotePasswordHashingTask.step2c                 C      |  d dS )zsIncrease the number of PBKDF2 iterations first to see the gains
        (and to be close to production environment)z--set pbkdf2-iterations:10000N$update_pbkdf2_password_storage_chemer   r   r   r   step3&   s   z'EnableDsRemotePasswordHashingTask.step3c                 C   s   |  d| j d dS )z-Configure the remote password hashing featurez/ --set remote-password-hashing-base-uri:http://zn:8080/ --set remote-password-hashing-connection-timeout:20s --set remote-password-hashing-request-timeout:120sN)r   r   r   r   r   r   step4+   s   z'EnableDsRemotePasswordHashingTask.step4c                 C   r   )z*Enable the remote password hashing featurez*--set remote-password-hashing-enabled:trueNr   r   r   r   r   step52   s   z'EnableDsRemotePasswordHashingTask.step5c                 C      |    dS zPost methodNset_result_passr   r   r   r   post6      z&EnableDsRemotePasswordHashingTask.postc                 C   s(   | j jD ]}| j j|d| d qd S )NzBset-password-storage-scheme-prop --scheme-name PBKDF2-HMAC-SHA256 )podcommand)r
   podsdsconfig)r   dsconfig_set_clausesr$   r   r   r   r   :   s   zFEnableDsRemotePasswordHashingTask.update_pbkdf2_password_storage_chemeN)__name__
__module____qualname__r   r   r   r   r   r   r   r"   r   r   r   r   r   r      s    r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )ConfigureCTSPurgec                 C   s   dS )z
Pre methodNr   r   r   r   r   r   A   s   zConfigureCTSPurge.prec                 C   s   t d}|d dS )z"Reduce the replication purge delayzds-ctsi`T  N)
pyrock_runget_componentset_replication_purge_delay)r   
ds_cts_idpr   r   r   r   E   s   
zConfigureCTSPurge.step1c                 C   r   r   r    r   r   r   r   r"   J   r#   zConfigureCTSPurge.postN)r)   r*   r+   r   r   r"   r   r   r   r   r,   @   s    r,   c                   @   s   e Zd ZdZdZdZdZdZdZdZ	dZ
dZddd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dd Zdd Zdd ZdS )EnablePrometheusMonitoringNzno-cachezapplication/json)zcache-controlzcontent-typeacceptc                 C   s    t  | _| jjdkrtddS )r	   amzSource must be am componentN)r   http_cmdr
   component_typer   r   r   r   r   r   ^   s   zEnablePrometheusMonitoring.prec                 C   sp   | j j d| _| jd }| j| _| jddi | jd| j ji | jd| j ji | jj	|| jd dS )	zObtain SSO Token for amadminz/jsonz/authenticateaccept-api-versionzprotocol=2.0,resource=1.0zx-openam-usernamezx-openam-password)urlheadersN)
r
   base_urlrest_urlr8   headers_jsonupdate
admin_useradmin_passwordr4   r"   )r   auth_urlr   r   r   r   d   s   
z EnablePrometheusMonitoring.step1c                 C   >   | j d | _| j| _| jddi | jj| j| jd| _dS )z*Fetch the pay load for enabling monitoringz"/global-config/services/monitoringr6   protocol=1.0,resource=1.0r8   N)r:   monitoring_service_urlr8   r;   r<   r4   getmon_responser   r   r   r   r   n      z EnablePrometheusMonitoring.step2c                 C   s>   | j  | _d| jd< d| jd< d| jd< td| j  dS )z!Enabling AM monitoring in payloadTenabledhttpEnabled
rmiEnabledzUpdated payload : N)rE   jsonmon_payload_jsonprintr   r   r   r   r   u   s
   


z EnablePrometheusMonitoring.step3c                 C   $   dD ]}t d|  | j|= qdS zRemove '_id' from the payload)_idz  removing : N)rL   rK   r   namer   r   r   r   }      
z EnablePrometheusMonitoring.step4c                 C   6   | j | _| jddi | jj| j| j| jdd dS z;Update the configuration in AM (send the payload to server)r6   rA   z"enabled":true)r8   	json_dataexpected_stringN)r8   r;   r<   r4   putrC   rK   r   r   r   r   r      
   
z EnablePrometheusMonitoring.step5c                 C   r@   )z/Fetch the pay load for prometheus configurationz8/global-config/services/monitoring/prometheus/prometheusr6   rA   rB   N)r:   prometheus_conf_urlr8   r;   r<   r4   rD   prometheus_responser   r   r   r   step6   rF   z EnablePrometheusMonitoring.step6c                 C   s   | j  | _d| jd< dS )z)Enabling prometheus monitoring in payloadTrG   N)rZ   rJ   prometheus_payload_jsonr   r   r   r   step7   s   z EnablePrometheusMonitoring.step7c                 C   rM   rN   )rL   r\   rP   r   r   r   step8   rR   z EnablePrometheusMonitoring.step8c                 C   rS   rT   )r8   r;   r<   r4   rW   rY   r\   r   r   r   r   step9   rX   z EnablePrometheusMonitoring.step9c                 C   s   | j   |   dS r   )r4   closer!   r   r   r   r   r"      s   
zEnablePrometheusMonitoring.post)r)   r*   r+   r4   r:   rE   rC   rK   rZ   rY   r\   r;   r8   r   r   r   r   r   r   r[   r]   r^   r_   r"   r   r   r   r   r1   N   s2    
r1   N)#pyrock.lib.scheduler.tasks.StepTaskr   pyrock.tasks.scenario.gatlingr   shared.lib.utils.HttpCmdr   shared.lib.utils.exceptionr   shared.lib.cloud_utilsr   pyrock.lib.PyRockRunr   r-   r   r,   r1   r   r   r   r   <module>   s   +