Skip to content

Commit 573a5c0

Browse files
committed
refactor: restructure vault gRPC clients by version
1 parent 8f2166c commit 573a5c0

8 files changed

Lines changed: 148 additions & 38 deletions

File tree

Makefile

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,28 @@ define download-proto
1313
$(call log_message,INFO - $@ downloaded successfully!)
1414
endef
1515

16-
$(PROTO_DIR)/%.proto:
16+
protos/v%/vault.proto:
1717
$(eval PROTO_URL := $(PROTO_URL))
1818
$(call download-proto)
1919

2020
vault-proto:
21-
@rm -f "$(PROTO_DIR)/vault.proto"
22-
@$(MAKE) PROTO_URL=https://raw.githubusercontent.com/smswithoutborders/RelaySMS-Vault/$(CURRENT_BRANCH)/protos/v1/vault.proto \
23-
$(PROTO_DIR)/vault.proto
21+
@for v in v1 v2; do \
22+
rm -f "protos/$$v/vault.proto"; \
23+
$(MAKE) PROTO_URL=https://raw.githubusercontent.com/smswithoutborders/RelaySMS-Vault/$(CURRENT_BRANCH)/protos/$$v/vault.proto \
24+
protos/$$v/vault.proto; \
25+
done
2426

2527
grpc-compile:
26-
$(call log_message,INFO - Compiling gRPC protos ...)
27-
@$(python) -m grpc_tools.protoc \
28-
-I$(PROTO_DIR) \
29-
--python_out=. \
30-
--pyi_out=. \
31-
--grpc_python_out=. \
32-
$(PROTO_DIR)/*.proto
33-
$(call log_message,INFO - gRPC Compilation complete!)
28+
$(call log_message,[INFO] Compiling gRPC protos ...)
29+
@for v in v1 v2; do \
30+
$(python) -m grpc_tools.protoc \
31+
--proto_path=. \
32+
--python_out=. \
33+
--pyi_out=. \
34+
--grpc_python_out=. \
35+
./protos/$$v/*.proto ; \
36+
done
37+
$(call log_message,[INFO] gRPC Compilation complete!)
3438

3539
grpc-server-start:
3640
$(call log_message,INFO - Starting gRPC server ...)

grpc_publisher_service.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11
"""gRPC Publisher Service"""
22

3-
import datetime
43
import base64
5-
import traceback
4+
import datetime
65
import json
7-
import grpc
6+
import traceback
87

8+
import grpc
99
import sentry_sdk
1010

11-
import publisher_pb2
12-
import publisher_pb2_grpc
13-
14-
from utils import get_configs
1511
from content_parser import (
1612
decode_content,
1713
extract_content_v0,
1814
extract_content_v1,
1915
extract_content_v2,
2016
)
21-
from grpc_vault_entity_client import (
17+
from logutils import get_logger
18+
from notification_dispatcher import dispatch_notifications
19+
from platforms.adapter_ipc_handler import AdapterIPCHandler
20+
from platforms.adapter_manager import AdapterManager
21+
from protos.v1 import publisher_pb2, publisher_pb2_grpc
22+
from translations import Localization
23+
from utils import get_configs
24+
from vault_clients.v1.grpc_client import (
25+
delete_entity_token,
26+
get_entity_access_token,
2227
list_entity_stored_tokens,
2328
store_entity_token,
24-
get_entity_access_token,
25-
decrypt_payload,
2629
update_entity_token,
27-
delete_entity_token,
2830
)
29-
from notification_dispatcher import dispatch_notifications
30-
from logutils import get_logger
31-
from translations import Localization
32-
from platforms.adapter_manager import AdapterManager
33-
from platforms.adapter_ipc_handler import AdapterIPCHandler
31+
from vault_clients.v2.grpc_client import decrypt_payload
3432

3533
MOCK_DELIVERY_SMS = (
3634
get_configs("MOCK_DELIVERY_SMS", default_value="true") or ""

grpc_server.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
import grpc
77
from grpc_interceptor import ServerInterceptor
8-
import publisher_pb2_grpc
98

10-
from utils import get_configs
11-
from logutils import get_logger
12-
from sentry_config import initialize_sentry, SENTRY_ENABLED
139
from grpc_publisher_service import PublisherService
10+
from logutils import get_logger
1411
from platforms.adapter_manager import AdapterManager
12+
from protos.v1 import publisher_pb2_grpc
13+
from sentry_config import SENTRY_ENABLED, initialize_sentry
14+
from utils import get_configs
1515

1616
logger = get_logger("publisher.grpc.server")
1717

vault_clients/__init__.py

Whitespace-only changes.

vault_clients/v1/__init__.py

Whitespace-only changes.
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
"""Vault gRPC Client"""
1+
"""Vault gRPC Client V1"""
22

33
import functools
4-
import grpc
54

6-
import vault_pb2
7-
import vault_pb2_grpc
5+
import grpc
86

9-
from utils import get_configs, mask_sensitive_info
107
from logutils import get_logger
8+
from protos.v1 import vault_pb2, vault_pb2_grpc
9+
from utils import get_configs, mask_sensitive_info
1110

1211
logger = get_logger(__name__)
1312

@@ -159,7 +158,9 @@ def get_entity_access_token(platform, account_identifier, **kwargs):
159158
id_type = (
160159
"long_lived_token"
161160
if long_lived_token
162-
else "device_id" if device_id else "phone_number"
161+
else "device_id"
162+
if device_id
163+
else "phone_number"
163164
)
164165

165166
logger.debug("Requesting access tokens using %s='%s'...", id_type, identifier)

vault_clients/v2/__init__.py

Whitespace-only changes.

vault_clients/v2/grpc_client.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"""Vault gRPC Client V2"""
2+
3+
import functools
4+
5+
import grpc
6+
7+
from logutils import get_logger
8+
from protos.v2 import vault_pb2, vault_pb2_grpc
9+
from utils import get_configs, mask_sensitive_info
10+
11+
logger = get_logger(__name__)
12+
13+
14+
def get_channel(internal=True):
15+
"""Get the appropriate gRPC channel based on the mode.
16+
17+
Args:
18+
internal (bool, optional): Flag indicating whether to use internal ports.
19+
Defaults to True.
20+
21+
Returns:
22+
grpc.Channel: The gRPC channel.
23+
"""
24+
mode = get_configs("MODE", default_value="development")
25+
hostname = get_configs("VAULT_GRPC_HOST")
26+
if internal:
27+
port = get_configs("VAULT_GRPC_INTERNAL_PORT")
28+
secure_port = get_configs("VAULT_GRPC_INTERNAL_SSL_PORT")
29+
else:
30+
port = get_configs("VAULT_GRPC_PORT")
31+
secure_port = get_configs("VAULT_GRPC_SSL_PORT")
32+
33+
if mode == "production":
34+
logger.info("Connecting to vault gRPC server at %s:%s", hostname, secure_port)
35+
credentials = grpc.ssl_channel_credentials()
36+
logger.info("Using secure channel for gRPC communication")
37+
return grpc.secure_channel(f"{hostname}:{secure_port}", credentials)
38+
39+
logger.info("Connecting to vault gRPC server at %s:%s", hostname, port)
40+
logger.warning("Using insecure channel for gRPC communication")
41+
return grpc.insecure_channel(f"{hostname}:{port}")
42+
43+
44+
def grpc_call(internal=True):
45+
"""Decorator to handle gRPC calls."""
46+
47+
def decorator(func):
48+
@functools.wraps(func)
49+
def wrapper(*args, **kwargs):
50+
try:
51+
channel = get_channel(internal)
52+
53+
with channel as conn:
54+
kwargs["stub"] = (
55+
vault_pb2_grpc.EntityInternalStub(conn)
56+
if internal
57+
else vault_pb2_grpc.EntityStub(conn)
58+
)
59+
return func(*args, **kwargs)
60+
except grpc.RpcError as e:
61+
return None, e
62+
except Exception as e:
63+
raise e
64+
65+
return wrapper
66+
67+
return decorator
68+
69+
70+
@grpc_call()
71+
def decrypt_payload(payload_ciphertext, **kwargs):
72+
"""
73+
Decrypts the payload.
74+
75+
Args:
76+
payload_ciphertext (bytes): The ciphertext of the payload to be decrypted.
77+
78+
Returns:
79+
tuple: A tuple containing:
80+
- server response (object): The vault server response.
81+
- error (Exception): The error encountered if the request fails, otherwise None.
82+
"""
83+
stub = kwargs["stub"]
84+
device_id = kwargs.get("device_id")
85+
phone_number = kwargs.get("phone_number")
86+
87+
request = vault_pb2.DecryptPayloadRequest(
88+
device_id=device_id,
89+
payload_ciphertext=payload_ciphertext,
90+
phone_number=phone_number,
91+
)
92+
93+
identifier = mask_sensitive_info(device_id or phone_number)
94+
95+
logger.debug(
96+
"Initiating decryption request using %s='%s'.",
97+
"device_id" if device_id else "phone_number",
98+
identifier,
99+
)
100+
101+
response = stub.DecryptPayload(request)
102+
103+
logger.info(
104+
"Decryption successful using %s.",
105+
"device_id" if device_id else "phone_number",
106+
)
107+
return response, None

0 commit comments

Comments
 (0)