Skip to content

Commit b6c957f

Browse files
committed
feat(plugins): add environment variable support for plugins
1 parent 7282525 commit b6c957f

10 files changed

Lines changed: 439 additions & 135 deletions

File tree

Cargo.lock

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,6 @@ hmac = "0.12"
6565
md5 = "0.7"
6666
lazy_static = "1.4"
6767
url = "2.2"
68+
69+
[dev-dependencies]
70+
tempfile = "3.8"

plugins/README.md

Lines changed: 89 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,20 @@ allowed_domains = [ # Whitelist of allowed domains
3737
]
3838
cache_read = true # Allow reading from cache
3939
cache_write = true # Allow writing to cache
40+
user_agent = "MyPlugin/1.0" # Custom User-Agent for HTTP requests (optional)
41+
env_vars = [ # Environment variables from .plugins.env to access
42+
"API_KEY",
43+
"API_SECRET"
44+
]
4045
```
4146

4247
**Plugin Configuration Options:**
4348
- `timeout` - Maximum execution time in seconds for `handle_query` (default: 5, minimum: 1)
4449

50+
**Permission Options:**
51+
- `user_agent` - Custom User-Agent string for HTTP requests (optional, default: "whois-server-plugin/<version>")
52+
- `env_vars` - List of environment variable names from `.plugins.env` that this plugin can access (optional)
53+
4554
## Lua Plugin API
4655

4756
### Required Functions
@@ -151,48 +160,110 @@ log_warn("API rate limit approaching")
151160
log_error("Failed to fetch data")
152161
```
153162

154-
## Complete Example
163+
### Environment Variable API
164+
165+
**`env_get(key: string) -> string`**
166+
167+
Get an environment variable value from `.plugins.env`. Only variables listed in `env_vars` in `meta.toml` are accessible.
155168

156-
Here's a complete example of a weather plugin:
169+
**Returns:** The environment variable value as a string
157170

171+
**Example:**
158172
```lua
159-
-- meta.toml
173+
local api_key = env_get("API_KEY")
174+
local url = "https://api.example.com/data?key=" .. api_key
175+
local response = http_get(url)
176+
```
177+
178+
**`env_list() -> table`**
179+
180+
Get a list of all available environment variable names for this plugin.
181+
182+
**Returns:** Array of variable names
183+
184+
**Example:**
185+
```lua
186+
local vars = env_list()
187+
for i, var_name in ipairs(vars) do
188+
log_info("Available env var: " .. var_name)
189+
end
190+
```
191+
192+
### The `.plugins.env` File
193+
194+
The `.plugins.env` file in the server root directory stores environment variables that plugins can access. This is useful for API keys, tokens, and other sensitive configuration.
195+
196+
**File Format:**
197+
```
198+
# Comment lines start with #
199+
API_KEY=your_api_key_here
200+
API_SECRET=your_secret_here
201+
BASE_URL=https://api.example.com
202+
203+
# Values can be quoted or unquoted
204+
DB_HOST="database.example.com"
205+
DB_PORT=5432
206+
```
207+
208+
**Security Notes:**
209+
- Each plugin can only access environment variables explicitly listed in its `env_vars` configuration
210+
- Never commit `.plugins.env` to version control (add it to `.gitignore`)
211+
- Use environment variables for sensitive data instead of hardcoding in plugin files
212+
213+
## Complete Example
214+
215+
Here's a complete example of a plugin using custom User-Agent and environment variables:
216+
217+
```toml
218+
# meta.toml
160219
[plugin]
161220
name = "weather"
162221
version = "1.0.0"
163222
suffix = "-WEATHER"
164223
author = "Your Name"
165224
description = "Get weather information"
166225
enabled = true
226+
timeout = 15
167227

168228
[permissions]
169229
network = true
170-
allowed_domains = ["wttr.in"]
230+
allowed_domains = ["api.weather.com"]
171231
cache_read = true
172232
cache_write = true
233+
user_agent = "MyWeatherPlugin/1.0 (contact@example.com)"
234+
env_vars = ["WEATHER_API_KEY"]
173235
```
174236

175237
```lua
176238
-- init.lua
177239
local function fetch_weather(location)
178-
-- Check cache
240+
-- Check cache first
179241
local cached = cache_get("weather:" .. location)
180242
if cached then
181243
return cached
182244
end
183245

184-
-- Fetch from API
185-
local url = "https://wttr.in/" .. location .. "?format=3"
246+
-- Get API key from environment
247+
local api_key = env_get("WEATHER_API_KEY")
248+
249+
-- Fetch from API with custom User-Agent
250+
local url = "https://api.weather.com/v1/current?location=" .. location .. "&apikey=" .. api_key
186251
local ok, result = pcall(http_get, url)
187252

188253
if not ok then
254+
log_error("Weather API request failed: " .. result)
189255
return nil
190256
end
191257

192-
-- Parse and use result
258+
-- Parse response
193259
local data = parse_json(result)
194-
cache_set("weather:" .. location, data, 1800)
195-
return data
260+
if data.status ~= 200 then
261+
return nil
262+
end
263+
264+
-- Cache for 30 minutes
265+
cache_set("weather:" .. location, data.body, 1800)
266+
return data.body
196267
end
197268

198269
function handle_query(query)
@@ -204,17 +275,23 @@ function handle_query(query)
204275

205276
local weather = fetch_weather(query)
206277
if not weather then
207-
return "% Error: Failed to fetch weather\n"
278+
return "% Error: Failed to fetch weather data\n"
208279
end
209280

210281
return "% Weather: " .. weather .. "\n"
211282
end
212283

213284
function init()
214-
log_info("Weather plugin loaded")
285+
log_info("Weather plugin initialized with custom User-Agent")
215286
end
216287
```
217288

289+
**Corresponding `.plugins.env` file:**
290+
```
291+
# Weather API credentials
292+
WEATHER_API_KEY=your_actual_api_key_here
293+
```
294+
218295
## Security Model
219296

220297
Plugins run in a secure sandbox with the following restrictions:

0 commit comments

Comments
 (0)