File manager - Edit - /opt/saltstack/salt/lib/python3.10/site-packages/salt/engines/__pycache__/slack_bolt_engine.cpython-310.pyc
Back
o ;j � � @ s d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddlZ ddlZ ddl Z ddlZ ddlZ ddlZ ddlZ ddlZ ddlZ ddlZ zddlZddlZdZW n eye dZY nw e�e�ZdZdd� ZG dd � d �Z ddd �ZdS )a� An engine that reads messages from Slack and can act on them .. versionadded:: 3006.0 :depends: `slack_bolt <https://pypi.org/project/slack_bolt/>`_ Python module .. important:: This engine requires a Slack app and a Slack Bot user. To create a bot user, first go to the **Custom Integrations** page in your Slack Workspace. Copy and paste the following URL, and log in with account credentials with administrative privileges: ``https://api.slack.com/apps/new`` Next, click on the ``From scratch`` option from the ``Create an app`` popup. Give your new app a unique name, eg. ``SaltSlackEngine``, select the workspace where your app will be running, and click ``Create App``. Next, click on ``Socket Mode`` and then click on the toggle button for ``Enable Socket Mode``. In the dialog give your Socket Mode Token a unique name and then copy and save the app level token. This will be used as the ``app_token`` parameter in the Slack engine configuration. Next, click on ``Event Subscriptions`` and ensure that ``Enable Events`` is in the on position. Then add the following bot events, ``message.channel`` and ``message.im`` to the ``Subcribe to bot events`` list. Next, click on ``OAuth & Permissions`` and then under ``Bot Token Scope``, click on ``Add an OAuth Scope``. Ensure the following scopes are included: - ``channels:history`` - ``channels:read`` - ``chat:write`` - ``commands`` - ``files:read`` - ``files:write`` - ``im:history`` - ``mpim:history`` - ``usergroups:read`` - ``users:read`` Once all the scopes have been added, click the ``Install to Workspace`` button under ``OAuth Tokens for Your Workspace``, then click ``Allow``. Copy and save the ``Bot User OAuth Token``, this will be used as the ``bot_token`` parameter in the Slack engine configuration. Finally, add this bot user to a channel by switching to the channel and using ``/invite @mybotuser``. Keep in mind that this engine will process messages from each channel in which the bot is a member, so it is recommended to narrowly define the commands which can be executed, and the Slack users which are allowed to run commands. This engine has two boolean configuration parameters that toggle specific features (both default to ``False``): 1. ``control`` - If set to ``True``, then any message which starts with the trigger string (which defaults to ``!`` and can be overridden by setting the ``trigger`` option in the engine configuration) will be interpreted as a Salt CLI command and the engine will attempt to run it. The permissions defined in the various ``groups`` will determine if the Slack user is allowed to run the command. The ``targets`` and ``default_target`` options can be used to set targets for a given command, but the engine can also read the following two keyword arguments: - ``target`` - The target expression to use for the command - ``tgt_type`` - The match type, can be one of ``glob``, ``list``, ``pcre``, ``grain``, ``grain_pcre``, ``pillar``, ``nodegroup``, ``range``, ``ipcidr``, or ``compound``. The default value is ``glob``. Here are a few examples: .. code-block:: text !test.ping target=* !state.apply foo target=os:CentOS tgt_type=grain !pkg.version mypkg target=role:database tgt_type=pillar 2. ``fire_all`` - If set to ``True``, all messages which are not prefixed with the trigger string will fired as events onto Salt's ref:`event bus <event-system>`. The tag for these events will be prefixed with the string specified by the ``tag`` config option (default: ``salt/engines/slack``). The ``groups_pillar_name`` config option can be used to pull group configuration from the specified pillar key. .. note:: In order to use ``groups_pillar_name``, the engine must be running as a minion running on the master, so that the ``Caller`` client can be used to retrieve that minion's pillar data, because the master process does not have pillar data. Configuration Examples ====================== .. versionchanged:: 2017.7.0 Access control group support added .. versionchanged:: 3006.0 Updated to use slack_bolt Python library. This example uses a single group called ``default``. In addition, other groups are being loaded from pillar data. The users and commands defined within these groups are used to determine whether the Slack user has permission to run the desired command. .. code-block:: text engines: - slack_bolt: app_token: "xapp-x-xxxxxxxxxxx-xxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" bot_token: 'xoxb-xxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxx' control: True fire_all: False groups_pillar_name: 'slack_engine:groups_pillar' groups: default: users: - '*' commands: - test.ping - cmd.run - list_jobs - list_commands aliases: list_jobs: cmd: jobs.list_jobs list_commands: cmd: 'pillar.get salt:engines:slack:valid_commands target=saltmaster tgt_type=list' default_target: target: saltmaster tgt_type: glob targets: test.ping: target: '*' tgt_type: glob cmd.run: target: saltmaster tgt_type: list This example shows multiple groups applying to different users, with all users having access to run test.ping. Keep in mind that when using ``*``, the value must be quoted, or else PyYAML will fail to load the configuration. .. code-block:: text engines: - slack_bolt: groups_pillar: slack_engine_pillar app_token: "xapp-x-xxxxxxxxxxx-xxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" bot_token: 'xoxb-xxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxx' control: True fire_all: True tag: salt/engines/slack groups_pillar_name: 'slack_engine:groups_pillar' groups: default: users: - '*' commands: - test.ping aliases: list_jobs: cmd: jobs.list_jobs list_commands: cmd: 'pillar.get salt:engines:slack:valid_commands target=saltmaster tgt_type=list' gods: users: - garethgreenaway commands: - '*' � NTF� slack_boltc C s t sdS tS )N)Fz2The 'slack_bolt' Python module could not be loaded)� HAS_SLACKBOLT�__virtualname__� r r �R/opt/saltstack/salt/lib/python3.10/site-packages/salt/engines/slack_bolt_engine.py�__virtual__� s r c @ s� e Zd Zdd� Zdd� Zdd� Zdd� Zd d � Zdd� Zd d� Z dd� Z dd� Zdd� Zdd� Z dd� Zdd� Zdd� Zdd� Zdd � Zd!d"� Z #d)d$d%�Zd&d'� Zd(S )*�SlackClientc C sx t j�t�| _tj|d�| _tjj � | j|�| _| j�� || _ || _t�� | _d|� d�}| j�t�|��| j� d S )N)�tokenz(^z.*))�salt�minionZMasterMinion�__opts__� master_minionr ZApp�appZadapterZsocket_modeZSocketModeHandler�handler�connect� app_token� bot_token�collections�deque� msg_queue�message�re�compile�message_trigger)�selfr r �trigger_stringZtrigger_patternr r r �__init__� s � zSlackClient.__init__c C s dS )NTr )r r r r � _run_until� s zSlackClient._run_untilc C s | j �|� d S �N)r �append)r r r r r r � s zSlackClient.message_triggerc C sb t jjjd|td�}i }d|v r/|d D ]}d|v r.|d s.|d ||d <