File manager - Edit - /opt/saltstack/salt/lib/python3.10/site-packages/salt/renderers/__pycache__/aws_kms.cpython-310.pyc
Back
o ;j# � @ s� d Z ddlZddlZddlZddlmZ zddlZddlZ e� d��ej� W n e y0 Y nw z ddlmZ dZW n e yF dZY nw dd� Ze� e�Zdd d �Zdd� Zd d� Zdd� Zdd� Zdd� Zdd� Zd dd�Zd dd�Zd!dd�ZdS )"a+ .. _`AWS KMS Envelope Encryption`: https://docs.aws.amazon.com/kms/latest/developerguide/workflow.html Renderer that will decrypt ciphers encrypted using `AWS KMS Envelope Encryption`_. Any key in the data to be rendered can be a urlsafe_b64encoded string, and this renderer will attempt to decrypt it before passing it off to Salt. This allows you to safely store secrets in source control, in such a way that only your Salt master can decrypt them and distribute them only to the minions that need them. The typical use-case would be to use ciphers in your pillar data, and keep the encrypted data key on your master. This way developers with appropriate AWS IAM privileges can add new secrets quickly and easily. This renderer requires the boto3_ Python library. .. _boto3: https://boto3.readthedocs.io/ Setup ----- First, set up your AWS client. For complete instructions on configuration the AWS client, please read the `boto3 configuration documentation`_. By default, this renderer will use the default AWS profile. You can override the profile name in salt configuration. For example, if you have a profile in your aws client configuration named "salt", you can add the following salt configuration: .. code-block:: yaml aws_kms: profile_name: salt .. _boto3 configuration documentation: https://boto3.readthedocs.io/en/latest/guide/configuration.html The rest of these instructions assume that you will use the default profile for key generation and setup. If not, export AWS_PROFILE and set it to the desired value. Once the aws client is configured, generate a KMS customer master key and use that to generate a local data key. .. code-block:: bash # data_key=$(aws kms generate-data-key --key-id your-key-id --key-spec AES_256 --query 'CiphertextBlob' --output text) # echo 'aws_kms:' # echo ' data_key: !!binary "%s"\n' "$data_key" >> config/master To apply the renderer on a file-by-file basis add the following line to the top of any pillar with gpg data in it: .. code-block:: yaml #!yaml|aws_kms Now with your renderer configured, you can include your ciphers in your pillar data like so: .. code-block:: yaml #!yaml|aws_kms a-secret: gAAAAABaj5uzShPI3PEz6nL5Vhk2eEHxGXSZj8g71B84CZsVjAAtDFY1mfjNRl-1Su9YVvkUzNjI4lHCJJfXqdcTvwczBYtKy0Pa7Ri02s10Wn1tF0tbRwk= � N)�SaltConfigurationError�boto3TFc C s t otjj�� S )zg Only load if boto libraries exist and if boto libraries are greater than a given version. )� HAS_FERNET�salt�utilsZversionsZcheck_boto_reqs� r r �J/opt/saltstack/salt/lib/python3.10/site-packages/salt/renderers/aws_kms.py�__virtual__W s r c C s$ t �dtj�}|di �}|�| |�S )zz Return the requested value from the aws_kms key in salt configuration. If it's not set, return the default. z config.getZaws_kms)Z__salt__�getZ__opts__)�key�defaultZroot_cfgZkms_cfgr r r �_cfgb s r c C s t dd�} | r | S td��)zn Return the encrypted KMS data key from configuration. Raises SaltConfigurationError if not set. �data_key� zaws_kms:data_key is not set)r r )r r r r � _cfg_data_keym s r c C s� t d�} | r t�d| � nt�d� ztj| d�W S tjjy1 } z td� | p)d��|�d}~w tjj yH } z td� | p@d��|�d}~ww ) aH Return the boto3 session to use for the KMS client. If aws_kms:profile_name is set in the salt configuration, use that profile. Otherwise, fall back on the default aws profile. We use the boto3 profile system to avoid having to duplicate individual boto3 configuration settings in salt configuration. �profile_namezUsing the "%s" aws profile.zIaws_kms:profile_name is not set in salt. Falling back on default profile.)r z9Boto3 could not find the "{}" profile configured in Salt.r NzKBoto3 was unable to determine the AWS endpoint region using the {} profile.)r �log�infor ZSession�botocore� exceptionsZProfileNotFoundr �formatZ NoRegionError)r �orig_excr r r �_sessiony s4 ����� ����r c C s t � } | �d�S )z2 Return the boto3 client for the KMS API. �kms)r �client)�sessionr r r �_kms� � r c C sb t � } t� }z| j|d�W S tjjy0 } z|j�di ��dd�}|dkr'� td�|�d}~ww )zG Return the response dictionary from the KMS decrypt API call. )ZCiphertextBlob�ErrorZCoder ZInvalidCiphertextExceptionz,aws_kms:data_key is not a valid KMS data keyN) r r �decryptr r ZClientError�responser r )r r r Z error_coder r r �_api_decrypt� s ����r! c C sn t tdd�} | du}|st� } ttd| � | d }| d }t|d�r'|�t�}t�d||r2d� |S d� |S ) z� Return the configured KMS data key decrypted and encoded in urlsafe base64. Cache the result to minimize API calls to AWS. r NZKeyIdZ Plaintext�encodezUsing key %s from %s�cachezapi call) �getattr�_plaintext_data_keyr! �setattr�hasattrr"