-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcottage-plugin-zoho-vault.py
More file actions
executable file
·220 lines (186 loc) · 6.55 KB
/
Copy pathcottage-plugin-zoho-vault.py
File metadata and controls
executable file
·220 lines (186 loc) · 6.55 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.14"
# dependencies = [
# "pydantic>=2.13.4",
# "pyreqwest>=0.10.1",
# ]
# ///
# cottage.toml
"""
[upstream.dev-zoho]
envfile = "./zoho/dev.env.cott.age" # Should export ZOHO_CLIENT_ID, ZOHO_CLIENT_SECRET, ZOHO_REFRESH_TOKEN.
vars = {
ZOHO_SECRET_ID = "1234567890",
ZOHO_VAULT_URL = "https://vault.zoho.com",
}
plugin = "./examples/plugins/cottage-plugin-zoho-vault.py"
"""
# myapp/dev.json.cott.toml
"""
[upstream.dev-zoho]
pull = true
push = true
"""
import json
import os
import sys
from pydantic import BaseModel, Field
from pyreqwest.client import SyncClientBuilder
class ZohoVaultConfig(BaseModel):
model_config = {"extra": "ignore"}
zoho_client_id: str = Field(..., alias="ZOHO_CLIENT_ID")
zoho_client_secret: str = Field(..., alias="ZOHO_CLIENT_SECRET")
zoho_refresh_token: str = Field(..., alias="ZOHO_REFRESH_TOKEN")
zoho_secret_id: str = Field(..., alias="ZOHO_SECRET_ID")
zoho_vault_url: str = Field("https://vault.zoho.com", alias="ZOHO_VAULT_URL")
zoho_passphrase: str | None = Field(None, alias="ZOHO_PASSPHRASE")
def get_access_token(cfg: ZohoVaultConfig) -> str:
# Resolve regional accounts URL
accounts_url = "https://accounts.zoho.com/oauth/v2/token"
if ".zoho.eu" in cfg.zoho_vault_url:
accounts_url = "https://accounts.zoho.eu/oauth/v2/token"
elif ".zoho.com.cn" in cfg.zoho_vault_url:
accounts_url = "https://accounts.zoho.com.cn/oauth/v2/token"
elif ".zoho.in" in cfg.zoho_vault_url:
accounts_url = "https://accounts.zoho.in/oauth/v2/token"
elif ".zoho.com.au" in cfg.zoho_vault_url:
accounts_url = "https://accounts.zoho.com.au/oauth/v2/token"
print(f"Refreshing OAuth2 token from {accounts_url}...", file=sys.stderr)
with SyncClientBuilder().build() as client:
resp = (
client.post(accounts_url)
.query(
{
"refresh_token": cfg.zoho_refresh_token,
"client_id": cfg.zoho_client_id,
"client_secret": cfg.zoho_client_secret,
"grant_type": "refresh_token",
}
)
.build()
.send()
)
data = resp.json()
if "access_token" not in data:
print(f"OAuth refresh failed: {data}", file=sys.stderr)
sys.exit(1)
return data["access_token"]
def decrypt_data(encrypted_payload: str, passphrase: str | None) -> dict:
"""
Decryption Hook for Zoho Vault Host-Proof Security.
Zoho Vault returns secrets encrypted on the server. To decrypt, you must use the
PBKDF2/AES-256 decryption utility provided in the Zoho Vault API documentation/files.
Contact support@zohovault.com to obtain their official crypto helper files (e.g. `zohovault_crypto.py`).
"""
if not passphrase:
print(
"Warning: ZOHO_PASSPHRASE not set. Returning encrypted payload directly.",
file=sys.stderr,
)
return {"encrypted_data": encrypted_payload}
# Standard Import placeholder:
# try:
# import zohovault_crypto
# return zohovault_crypto.decrypt(encrypted_payload, passphrase)
# except ImportError:
# pass
# For demonstration/custom implementations:
print(
"Please integrate the zohovault_crypto module to perform host-proof decryption.",
file=sys.stderr,
)
return {"encrypted_data": encrypted_payload}
def encrypt_data(plain_payload: dict, passphrase: str | None) -> str:
"""
Encryption Hook for Zoho Vault Host-Proof Security.
Encrypts the JSON payload on the client side before uploading to Zoho Vault.
"""
if not passphrase:
print(
"Warning: ZOHO_PASSPHRASE not set. Uploading plain string as payload.",
file=sys.stderr,
)
return json.dumps(plain_payload)
# Standard Import placeholder:
# try:
# import zohovault_crypto
# return zohovault_crypto.encrypt(plain_payload, passphrase)
# except ImportError:
# pass
print(
"Please integrate the zohovault_crypto module to perform host-proof encryption.",
file=sys.stderr,
)
return json.dumps(plain_payload)
def pull():
cfg = ZohoVaultConfig.model_validate(os.environ)
token = get_access_token(cfg)
urlpath = f"/api/rest/json/v1/secrets/{cfg.zoho_secret_id}"
print( # Use --debug to see this message
"Pulling secret from Zoho Vault...",
file=sys.stderr,
)
with (
SyncClientBuilder()
.base_url(cfg.zoho_vault_url)
.default_headers(
{
"Authorization": f"Zoho-oauthtoken {token}",
"Accept": "application/json",
}
)
.error_for_status()
.build()
) as client:
resp = client.get(urlpath).build().send()
response_json = resp.json()
# Zoho Vault responses nest secret information inside a standard structure
# e.g., {"operationName": "getSecret", "status": "Success", "secretData": "..."}
secret_data_str = response_json.get("secretData", "")
decrypted = decrypt_data(secret_data_str, cfg.zoho_passphrase)
print(json.dumps(decrypted))
def push():
cfg = ZohoVaultConfig.model_validate(os.environ)
token = get_access_token(cfg)
payload = json.loads(input())
encrypted_payload_str = encrypt_data(payload, cfg.zoho_passphrase)
urlpath = f"/api/rest/json/v1/secrets/{cfg.zoho_secret_id}"
print( # Use --debug to see this message
"Pushing secret to Zoho Vault...",
file=sys.stderr,
)
# Prepare form data required by Zoho Vault API
form_data = {
"INPUT_DATA": json.dumps(
{
"secretData": encrypted_payload_str,
# Additional metadata fields can go here
}
)
}
with (
SyncClientBuilder()
.base_url(cfg.zoho_vault_url)
.default_headers(
{
"Authorization": f"Zoho-oauthtoken {token}",
"Accept": "application/json",
}
)
.error_for_status()
.build()
) as client:
client.put(urlpath).form(form_data).build().send()
if __name__ == "__main__":
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} [pull|push]", file=sys.stderr)
sys.exit(1)
match sys.argv[1]:
case "pull":
pull()
case "push":
push()
case cmd:
print(f"Unknown command: {cmd}", file=sys.stderr)
sys.exit(1)