Skip to content

Commit a0b805f

Browse files
authored
Merge pull request #395 from HarperFast/env-config-4.7
Add env config doc to 4.7
2 parents c9bfb0c + a188de4 commit a0b805f

File tree

1 file changed

+156
-1
lines changed

1 file changed

+156
-1
lines changed

versioned_docs/version-4.7/deployments/configuration.md

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,161 @@ To use a custom configuration file to set values on install, use the CLI/ENV var
5454

5555
To install Harper overtop of an existing configuration file, set `HDB_CONFIG` to the root path of your install `<ROOTPATH>/harperdb-config.yaml`
5656

57+
## Environment Variable-Based Configuration
58+
59+
Harper provides two special environment variables for managing configuration: `HARPER_DEFAULT_CONFIG` and `HARPER_SET_CONFIG`. These variables allow you to configure Harper instances through environment variables using JSON-formatted configuration objects.
60+
61+
### Overview
62+
63+
Both environment variables accept JSON-formatted configuration that mirrors the structure of `harperdb-config.yaml`:
64+
65+
```bash
66+
export HARPER_DEFAULT_CONFIG='{"http":{"port":8080},"logging":{"level":"info"}}'
67+
export HARPER_SET_CONFIG='{"authentication":{"enabled":true}}'
68+
```
69+
70+
The key difference between these variables is their precedence and behavior when configuration changes:
71+
72+
| Feature | HARPER_DEFAULT_CONFIG | HARPER_SET_CONFIG |
73+
| --------------- | ----------------------------- | ------------------------------- |
74+
| **Purpose** | Provide sensible defaults | Force critical settings |
75+
| **Precedence** | Lower (respects user edits) | Highest (always overrides) |
76+
| **User edits** | Respected after detection | Always overridden |
77+
| **Key removal** | Restores original values | Deletes values |
78+
| **Use case** | Installation/runtime defaults | Security/compliance enforcement |
79+
80+
### HARPER_DEFAULT_CONFIG
81+
82+
`HARPER_DEFAULT_CONFIG` provides default configuration values while respecting user modifications. This is ideal for scenarios where you want to provide sensible defaults without preventing administrators from customizing their instances.
83+
84+
#### Behavior
85+
86+
**At installation time:**
87+
88+
- Overrides template default values
89+
- Respects values set by `HARPER_SET_CONFIG`
90+
- Respects values from existing config files (when using `HDB_CONFIG`)
91+
92+
**At runtime:**
93+
94+
- Only updates values it originally set
95+
- Automatically detects and respects manual user edits to the config file
96+
- When a key is removed from the environment variable, the original value is restored
97+
98+
#### Example: Setting Default Port
99+
100+
```bash
101+
# Set default port and logging level
102+
export HARPER_DEFAULT_CONFIG='{"http":{"port":8080},"logging":{"level":"info"}}'
103+
104+
# Install and start Harper
105+
npm install -g harperdb
106+
harperdb
107+
108+
# The config file will have port 8080 and info logging
109+
110+
# If an administrator manually edits the config to use port 9000,
111+
# Harper will detect this change and respect it on subsequent restarts
112+
113+
# If you remove http.port from the env var later:
114+
export HARPER_DEFAULT_CONFIG='{"logging":{"level":"info"}}'
115+
# The port will be restored to its original template default (9925)
116+
```
117+
118+
### HARPER_SET_CONFIG
119+
120+
`HARPER_SET_CONFIG` forces configuration values that must never be changed by users. This is designed for security policies, compliance requirements, or critical operational settings that need to be enforced across all instances.
121+
122+
#### Behavior
123+
124+
**At runtime:**
125+
126+
- Always overrides all other configuration sources
127+
- Takes precedence over user edits, file values, and `HARPER_DEFAULT_CONFIG`
128+
- When a key is removed from the environment variable, it's deleted from the config (no restoration)
129+
130+
#### Example: Enforce Security Settings
131+
132+
```bash
133+
# Force authentication and specific logging for compliance
134+
export HARPER_SET_CONFIG='{"authentication":{"enabled":true},"logging":{"level":"error","stdStreams":true}}'
135+
136+
# Install and start Harper
137+
npm install -g harperdb
138+
harperdb
139+
140+
# Any attempt to change these values in harperdb-config.yaml will be
141+
# overridden on the next restart. The SET_CONFIG values always win.
142+
143+
# If you later remove authentication from SET_CONFIG:
144+
export HARPER_SET_CONFIG='{"logging":{"level":"error","stdStreams":true}}'
145+
# The authentication section will be removed from the config entirely
146+
```
147+
148+
### Combining Both Variables
149+
150+
You can use both environment variables together for maximum flexibility:
151+
152+
```bash
153+
# Provide sensible defaults for most settings
154+
export HARPER_DEFAULT_CONFIG='{"http":{"port":8080,"cors":true},"logging":{"level":"info"}}'
155+
156+
# But enforce critical security settings that cannot be changed
157+
export HARPER_SET_CONFIG='{"authentication":{"enabled":true,"sessionTokenExpiration":3600}}'
158+
```
159+
160+
In this scenario:
161+
162+
- Administrators can customize the HTTP port, CORS settings, and logging level
163+
- Authentication settings are always enforced and cannot be changed
164+
165+
### Configuration Precedence
166+
167+
The complete configuration precedence order (highest to lowest):
168+
169+
1. **HARPER_SET_CONFIG** - Always wins
170+
2. **User manual edits** - Detected through drift detection
171+
3. **HARPER_DEFAULT_CONFIG** - Applied if no user edits detected
172+
4. **File defaults** - Original template values
173+
174+
### State Tracking
175+
176+
Harper maintains a state file at `{rootPath}/backup/.harper-config-state.json` to track the source of each configuration value. This enables:
177+
178+
- **Drift detection**: Identifying when users manually edit values set by `HARPER_DEFAULT_CONFIG`
179+
- **Restoration**: Restoring original values when keys are removed from `HARPER_DEFAULT_CONFIG`
180+
- **Conflict resolution**: Determining which source should take precedence
181+
182+
### Important Notes
183+
184+
- Both environment variables must contain valid JSON matching the structure of `harperdb-config.yaml`
185+
- Configuration validation occurs after environment variables are applied
186+
- Invalid values will be caught by Harper's configuration validator
187+
- Changes to these environment variables require a Harper restart to take effect
188+
- The state file is specific to each Harper instance (stored in the root path)
189+
190+
### Format Reference
191+
192+
The JSON structure mirrors the YAML configuration file. For example:
193+
194+
**YAML format:**
195+
196+
```yaml
197+
http:
198+
port: 8080
199+
cors: true
200+
logging:
201+
level: info
202+
rotation:
203+
enabled: true
204+
```
205+
206+
**Environment variable format:**
207+
208+
```json
209+
{ "http": { "port": 8080, "cors": true }, "logging": { "level": "info", "rotation": { "enabled": true } } }
210+
```
211+
57212
---
58213

59214
## Configuration Options
@@ -73,7 +228,7 @@ http:
73228

74229
`compressionThreshold` - _Type_: number; _Default_: 1200 (bytes)
75230

76-
For HTTP clients that support (Brotli) compression encoding, responses that are larger than than this threshold will be compressed (also note that for clients that accept compression, any streaming responses from queries are compressed as well, since the size is not known beforehand).
231+
For HTTP clients that support (Brotli) compression encoding, responses that are larger than this threshold will be compressed (also note that for clients that accept compression, any streaming responses from queries are compressed as well, since the size is not known beforehand).
77232

78233
```yaml
79234
http:

0 commit comments

Comments
 (0)