삽질/개발,엔지니어링

slack bot 실행시 CERTIFICATE_VERIFY_FAILED (_ssl.c:1051)

maengis 2023. 5. 11. 18:03

개발 환경에서는 문제가 없다가 실서버에서 돌리니 인증서 오류가 발생 했다.

 

Failed to send a request to Slack API server: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1051)>
Failed to send a request to Slack API server: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1051)>
Traceback (most recent call last):
  File "/home/apps/python3.7/lib/python3.7/urllib/request.py", line 1317, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/home/apps/python3.7/lib/python3.7/http/client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/home/apps/python3.7/lib/python3.7/http/client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/home/apps/python3.7/lib/python3.7/http/client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/home/apps/python3.7/lib/python3.7/http/client.py", line 1016, in _send_output
    self.send(msg)
  File "/home/apps/python3.7/lib/python3.7/http/client.py", line 956, in send
    self.connect()
  File "/home/apps/python3.7/lib/python3.7/http/client.py", line 1392, in connect
    server_hostname=server_hostname)
  File "/home/apps/python3.7/lib/python3.7/ssl.py", line 412, in wrap_socket
    session=session
  File "/home/apps/python3.7/lib/python3.7/ssl.py", line 853, in _create
    self.do_handshake()
  File "/home/apps/python3.7/lib/python3.7/ssl.py", line 1117, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1051)
...
이하 생략

 

구글링 해보니 certifi를 설치(혹은 업그레이드)하라고 했으나 해도 동일.

 

pip3 install --upgrade certifi

 

아니면 인증 하지 말라고 파이썬 파일 최상단에서 선언하라고 함.

 

import ssl

ssl._create_default_https_context = ssl._create_unverified_context

 

위처럼 하면, 실행은 되지만 웹소켓에서 아래와 같은 인증서 오류가 발생함.

 

Failed to establish a connection (session id: 0ccc1fbe-a31d-43ee-9661-bb166623f268, error: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1051))
on_error invoked (session id: 7361cf68-ff80-4561-8b83-5c6bda0db479, error: SSLCertVerificationError, message: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1051))

 

구글링을 해보니, slack-sdk에 있는 socket mode는 내부에서 알아서 ssl_context를 생성함.

패치가 된 거 같은데 왜 안 되지 해서 커밋 내용을 쭉 보니 내부에서 알아서 생성하던 걸, 밖에서 받아오는 방식으로 변경 함.

 

https://github.com/slackapi/python-slack-sdk/pull/1177/commits/51ed8c322c927cebc28ec2ebdfdc4fa3fc82aa8c

 

결국 웹클라이언트를 생성시 인증서 확인 안 하게 하고 넘겨줘야 함.

 

from slack_sdk.web import WebClient
from slack_bolt.adapter.socket_mode import SocketModeHandler
import certifi
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

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

SLACK_APP_TOKEN = 'xapp-앱토큰'
SLACK_BOT_TOKEN = 'xoxb-봇토큰'

slack_client = WebClient(token=SLACK_BOT_TOKEN, ssl=ssl_context)

app = App(client=slack_client)
반응형