Closed10

Difyのコード実行機能(SandBox内)で任意のPythonライブラリを使用する。`pip install slack-sdk` したい

yuks0810yuks0810
yuks0810yuks0810

このような感じでrequirements.txtに記載すればいけるっぽいけど、再度dockerコンテナを立ち上げたりするのがめんどくさいので、pip installとかで入れたい。

ので、その方法を探す

yuks0810yuks0810

dockerを立ち上げる時の Dockerfile, docker-compose.yml とかがあるはずなので、そこからどこで pip install slack-sdk するべきかを見極める。

slack-sdk
$ pip install slack-sdk

https://pypi.org/project/slack-sdk/

yuks0810yuks0810

sandbox部分のdocker-compose.yml

  # The DifySandbox
  sandbox:
    image: langgenius/dify-sandbox:0.2.10
    restart: always
    environment:
      # The DifySandbox configurations
      # Make sure you are changing this key for your deployment with a strong key.
      # You can generate a strong key using `openssl rand -base64 42`.
      API_KEY: ${SANDBOX_API_KEY:-dify-sandbox}
      GIN_MODE: ${SANDBOX_GIN_MODE:-release}
      WORKER_TIMEOUT: ${SANDBOX_WORKER_TIMEOUT:-15}
      ENABLE_NETWORK: ${SANDBOX_ENABLE_NETWORK:-true}
      HTTP_PROXY: ${SANDBOX_HTTP_PROXY:-http://ssrf_proxy:3128}
      HTTPS_PROXY: ${SANDBOX_HTTPS_PROXY:-http://ssrf_proxy:3128}
      SANDBOX_PORT: ${SANDBOX_PORT:-8194}
    volumes:
      - ./volumes/sandbox/dependencies:/dependencies
    healthcheck:
      test: ['CMD', 'curl', '-f', 'http://localhost:8194/health']
    networks:
      - ssrf_proxy_network
yuks0810yuks0810

結論

sandboxのコンテナに入った直後のpathで pip isntall 実行したら普通に入れれた。
slack_sdk 3.33.2

root@b42b022e6540:/# pip list
Package            Version
------------------ ---------
anyio              4.4.0
certifi            2024.8.30
charset-normalizer 3.3.2
exceptiongroup     1.2.2
h11                0.14.0
httpcore           1.0.5
httpx              0.27.2
idna               3.8
Jinja2             3.1.4
MarkupSafe         2.1.5
pip                23.0.1
PySocks            1.7.1
requests           2.32.3
setuptools         65.5.1
slack_sdk          3.33.2
sniffio            1.3.1
socksio            1.0.0
typing_extensions  4.12.2
urllib3            2.2.3
wheel              0.44.0
yuks0810yuks0810

ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED]っていうエラーが出た。

pip install したらslack sdkは使えるようになったみたいだが、このようなエラーが出た。

bash
usr/local/lib/python3.10/ssl.py", line 1104, in _create self.do_handshake() File "/usr/local/lib/python3.10/ssl.py", line 1375, in do_handshake self._sslobj.do_handshake() ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:1007) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/var/sandbox/sandbox-python/tmp/f2ab337f_611a_4c53_ac08_1fb2912ba412.py", line 48, in <module> File "<string>", line 102, in <module> File "<string>", line 46, in main File "<string>", line 63, in fetch_and_post_messages File "<string>", line 78, in post_message File "/usr/local/lib/python3.10/site-packages/slack_sdk/web/client.py", line 2710, in chat_postMessage return self.api_call("chat.postMessage", json=kwargs) File "/usr/local/lib/python3.10/site-packages/slack_sdk/web/base_client.py", line 157, in api_call return self._sync_send(api_url=api_url, req_args=req_args) File "/usr/local/lib/python3.10/site-packausr/local/lib/python3.10/ssl.py", line 1104, in _create self.do_handshake() File "/usr/local/lib/python3.10/ssl.py", line 1375, in do_handshake self._sslobj.do_handshake() ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:1007) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/var/sandbox/sandbox-python/tmp/f2ab337f_611a_4c53_ac08_1fb2912ba412.py", line 48, in <module> File "<string>", line 102, in <module> File "<string>", line 46, in main File "<string>", line 63, in fetch_and_post_messages File "<string>", line 78, in post_message File "/usr/local/lib/python3.10/site-packages/slack_sdk/web/client.py", line 2710, in chat_postMessage return self.api_call("chat.postMessage", json=kwargs) File "/usr/local/lib/python3.10/site-packages/slack_sdk/web/base_client.py", line 157, in api_call return self._sync_send(api_url=api_url, req_args=req_args) File "/usr/local/lib/python3.10/site-packapen, protocol, protocol + File "/usr/local/lib/python3.10/urllib/request.py", line 496, in _call_chain result = func(*args) File "/usr/local/lib/python3.10/urllib/request.py", line 1391, in https_open return self.do_open(http.client.HTTPSConnection, req, File "/usr/local/lib/python3.10/urllib/request.py", line 1351, in do_open raise URLError(err) urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:1007)> e_client.py", line 399, in _perform_urllib_http_request resp = self._perform_urllib_http_request_internal(url, req) File "/usr/local/lib/python3.10/site-packages/slack_sdk/web/base_client.py", line 526, in _perform_urllib_http_request_internal resp = opener.open(req, timeout=self.timeout) # skipcq: BAN-B310 File "/usr/local/lib/python3.10/urllib/request.py", line 519, in open response = self._open(req, data) File "/usr/local/lib/python3.10/urllib/request.py", line 536, in _open result = self._call_chain(self.handle_open, protocol, protocol + File "/usr/local/lib/python3.10/urllib/request.py", line 496, in _call_chain result = func(*args) File "/usr/local/lib/python3.10/urllib/request.py", line 1391, in https_open return self.do_open(http.client.HTTPSConnection, req, File "/usr/local/lib/python3.10/urllib/request.py", line 1351, in do_open raise URLError(err) urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:1007)> error: exit status 255

画面:

yuks0810yuks0810

ちょっとめんどくさそうだけどやってみる。

まずは、この資料に書いているやり方でhttps化する。
https://note.com/sangmin/n/n7126d67f2c97

これを実行すればOKだけど、自分の環境の場合は docker compose コマンドではなく、 docker-compose コマンドだったので、 vimで install-dify.shdocker composedocker-compose に置換する必要があった。

curl -fsSL https://bit.ly/3YHdSo0 -o install-dify.sh
sudo sh ./install-dify.sh email domain
yuks0810yuks0810

一旦これで対応した。

ssl_context = ssl.create_default_context(cafile=certifi.where())

(中略)

# Slack WebClientの初期化(SSL設定を追加)
client = WebClient(
    token=SLACK_API_TOKEN,
    ssl=ssl_context
)
このスクラップは19日前にクローズされました