Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable TLS for Python Puppet Service Client #70

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.9
1.0.0
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ pycodestyle
pylint
pylint-quotes
pytest
pytest-asyncio
pytype
semver
grpclib
wechaty-puppet~=0.3dev2
pre-commit
pre-commit
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def versioning(version: str) -> str:
minor = sem_ver['minor']
patch = str(sem_ver['patch'])

if minor % 2:
patch = 'dev' + patch
# if minor % 2:
# patch = 'dev' + patch

fin_ver = '%d.%d.%s' % (
major,
Expand Down
35 changes: 35 additions & 0 deletions src/wechaty_puppet_service/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,41 @@
CHUNK_SIZE = 1024 * 1024


TLS_CA_CERT = '''-----BEGIN CERTIFICATE-----
MIIFxTCCA62gAwIBAgIUYddLAoa8JnLzJ80l2u5vGuFsaEIwDQYJKoZIhvcNAQEL
BQAwcjELMAkGA1UEBhMCVVMxFjAUBgNVBAgMDVNhbiBGcmFuY2lzY28xEjAQBgNV
BAcMCVBhbG8gQWx0bzEQMA4GA1UECgwHV2VjaGF0eTELMAkGA1UECwwCQ0ExGDAW
BgNVBAMMD3dlY2hhdHktcm9vdC1jYTAeFw0yMTA4MDkxNTQ4NTJaFw0zMTA4MDcx
NTQ4NTJaMHIxCzAJBgNVBAYTAlVTMRYwFAYDVQQIDA1TYW4gRnJhbmNpc2NvMRIw
EAYDVQQHDAlQYWxvIEFsdG8xEDAOBgNVBAoMB1dlY2hhdHkxCz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7oC3LlDCsB0291b3KSOtevMeDFoh12LcliXAkdIGGTccUxrH+Cyij
cBOc+EKGJFBdLqcjLDU4M6QdMMMFOdfXyAOSpYuWGYqrxqvxQjAjvianEyMpNZWM
lajggJqiPhfF67sZTB2yzvRTmtHdUq7x+iNOVonOBcCHu31aGxa9Py91XEr9jaIQ
EBdl6sycLxKo8mxF/5tyUOns9+919aWNqTOUBmI15D68bqhhOVNyvsb7aVURIt5y
6A7Sj4gSBR9P22Ba6iFZgbvfLn0zKLzjlBonUGlSPf3rSIYUkawICtDyYPvK5mi3
mANgIChMiOw6LYCPmmUVVAWU/tDy36kr9ZV9YTIZRYAkWswsJB340whjuzvZUVaG
DgW45GPR6bGIwlFZeqCwXLput8Z3C8Sw9bE9vjlB2ZCpjPLmWV/WbDlH3J3uDjgt
9PoALW0sOPhHfYklH4/rrmsSWMYTUuGS/HqxrEER1vpIOOb0hIiAWENDT/mruq22
VqO8MHX9ebjInSxPmhYOlrSZrOgEcogyMB4Z0SOtKVqPnkWmdR5hatU=
-----END CERTIFICATE-----'''


def get_token() -> Optional[str]:
"""
get the token from environment variable
Expand Down
7 changes: 7 additions & 0 deletions src/wechaty_puppet_service/puppet.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from __future__ import annotations

import json
import ssl
from typing import Callable, Optional, List
from functools import reduce
from dataclasses import asdict
Expand Down Expand Up @@ -79,6 +80,7 @@
from wechaty_puppet_service.config import (
get_endpoint,
get_token,
TLS_CA_CERT
)
from wechaty_puppet_service.utils import (
extract_host_and_port,
Expand Down Expand Up @@ -373,6 +375,7 @@ async def message_send_file(self, conversation_id: str,
:param file:
:return:
"""
self.puppet_stub.message_send_contact()
response = await self.puppet_stub.message_send_file(
conversation_id=conversation_id,
filebox=file.to_json_str()
Expand Down Expand Up @@ -907,6 +910,10 @@ def _init_puppet(self) -> None:
)

host, port = extract_host_and_port(self.options.end_point)

context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.load_verify_locations(cadata=TLS_CA_CERT)

self.channel = Channel(host=host, port=port)

# pylint: disable=W0212
Expand Down