-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault_grpc_client.py
141 lines (110 loc) · 4.13 KB
/
vault_grpc_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""
Vault gRPC Client.
This program is free software: you can redistribute it under the terms
of the GNU General Public License, v. 3.0. If a copy of the GNU General
Public License was not distributed with this file, see <https://www.gnu.org/licenses/>.
"""
import functools
import grpc
import vault_pb2
import vault_pb2_grpc
from utils import get_logger, get_env_var, mask_sensitive_info
logger = get_logger(__name__)
def get_channel(internal=True):
"""Get the appropriate gRPC channel based on the mode.
Args:
internal (bool, optional): Flag indicating whether to use internal ports.
Defaults to True.
Returns:
grpc.Channel: The gRPC channel.
"""
mode = get_env_var("MODE", default_value="development")
hostname = get_env_var("VAULT_GRPC_HOST")
if internal:
port = get_env_var("VAULT_GRPC_INTERNAL_PORT")
secure_port = get_env_var("VAULT_GRPC_INTERNAL_SSL_PORT")
else:
port = get_env_var("VAULT_GRPC_PORT")
secure_port = get_env_var("VAULT_GRPC_SSL_PORT")
if mode == "production":
logger.info("Connecting to vault gRPC server at %s:%s", hostname, secure_port)
credentials = grpc.ssl_channel_credentials()
logger.info("Using secure channel for gRPC communication")
return grpc.secure_channel(f"{hostname}:{secure_port}", credentials)
logger.info("Connecting to vault gRPC server at %s:%s", hostname, port)
logger.warning("Using insecure channel for gRPC communication")
return grpc.insecure_channel(f"{hostname}:{port}")
def grpc_call(internal=True):
"""Decorator to handle gRPC calls."""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
channel = get_channel(internal)
with channel as conn:
kwargs["stub"] = (
vault_pb2_grpc.EntityInternalStub(conn)
if internal
else vault_pb2_grpc.EntityStub(conn)
)
return func(*args, **kwargs)
except grpc.RpcError as e:
return None, e
except Exception as e:
raise e
return wrapper
return decorator
@grpc_call()
def decrypt_payload(payload_ciphertext, **kwargs):
"""
Decrypts the payload.
Args:
payload_ciphertext (bytes): The ciphertext of the payload to be decrypted.
Returns:
tuple: A tuple containing:
- server response (object): The vault server response.
- error (Exception): The error encountered if the request fails, otherwise None.
"""
stub = kwargs["stub"]
device_id = kwargs.get("device_id")
phone_number = kwargs.get("phone_number")
request = vault_pb2.DecryptPayloadRequest(
device_id=device_id,
payload_ciphertext=payload_ciphertext,
phone_number=phone_number,
)
identifier = mask_sensitive_info(device_id or phone_number)
logger.debug(
"Initiating decryption request using %s='%s'.",
"device_id" if device_id else "phone_number",
identifier,
)
response = stub.DecryptPayload(request)
logger.info(
"Decryption successful using %s.",
"device_id" if device_id else "phone_number",
)
return response, None
@grpc_call()
def encrypt_payload(device_id, payload_plaintext, **kwargs):
"""
Encrypts the payload.
Args:
device_id (str): The ID of the device.
payload_plaintext (str): The plaintext of the payload to be encrypted.
Returns:
tuple: A tuple containing:
- server response (object): The vault server response.
- error (Exception): The error encountered if the request fails, otherwise None.
"""
stub = kwargs["stub"]
request = vault_pb2.EncryptPayloadRequest(
device_id=device_id, payload_plaintext=payload_plaintext
)
logger.debug(
"Sending request to encrypt payload for device_id: %s",
device_id,
)
response = stub.EncryptPayload(request)
logger.info("Successfully encrypted payload.")
return response, None