nu_plugin_secret 0.7.0

Production-grade secret handling plugin for Nushell with secure CustomValue types that prevent accidental exposure of sensitive data
Documentation
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# nu_plugin_secret Configuration Guide

This guide covers all configuration options and the powerful templating system for customizing how secrets are displayed.

## Table of Contents

1. [Configuration File Location]#configuration-file-location
2. [Configuration Structure]#configuration-structure
3. [Redaction Configuration]#redaction-configuration
4. [Security Configuration]#security-configuration
5. [Templating System]#templating-system
6. [Template Variables]#template-variables
7. [Template Functions]#template-functions
8. [Configuration Commands]#configuration-commands
9. [Examples]#examples
10. [Security Considerations]#security-considerations

## Configuration File Location

The plugin uses a TOML configuration file located at:
- **Linux/macOS**: `~/.local/share/nushell/plugins/secret/config.toml`
- **Windows**: `%APPDATA%\nushell\plugins\secret\config.toml`

You can view the current configuration file path with:
```nushell
secret config show --file-path
```

## Configuration Structure

The configuration file uses TOML format with the following sections:

```toml
version = "1.0"

[redaction]
mask_secret = false
show_unredacted = false
redaction_template = "<redacted:{{secret_type}}>"

[security]
level = "standard"
audit_enabled = true
```

## Redaction Configuration

### `mask_secret`
**Type**: Boolean
**Default**: `false`
**Description**: When enabled, replaces the actual secret value with asterisks (`*`) in template functions that access the secret content.

```toml
[redaction]
mask_secret = true
```

### `show_unredacted`
**Type**: Boolean
**Default**: `false`
**Environment Override**: `SHOW_UNREDACTED=1` or `SHOW_UNREDACTED=true`
**Description**: **⚠️ DANGEROUS**: When enabled, secrets display their actual values instead of redacted output. Only use for debugging in secure environments.

```toml
[redaction]
show_unredacted = false  # Keep this false in production!
```

### `redaction_template`
**Type**: String
**Default**: `"<redacted:{{secret_type}}>"`
**Description**: Tera template string that defines how secrets are displayed. This is the core customization point for secret presentation.

```toml
[redaction]
redaction_template = "<redacted:{{secret_type}}>"
```

## Security Configuration

### `level`
**Type**: String
**Options**: `"minimal"`, `"standard"`, `"paranoid"`
**Default**: `"standard"`
**Description**: Sets overall security posture affecting various security checks.

- **`minimal`**: Basic security checks, allows audit disabling
- **`standard`**: Balanced security, requires audit logging
- **`paranoid`**: Maximum security, strictest validation

```toml
[security]
level = "standard"
```

### `audit_enabled`
**Type**: Boolean
**Default**: `true`
**Description**: Enables audit logging of secret operations. Required for `standard` and `paranoid` security levels.

```toml
[security]
audit_enabled = true
```

## Templating System

The plugin uses the **Tera templating engine** for flexible secret redaction. Templates allow you to customize exactly how secrets appear when displayed.

### Basic Template Syntax

Templates use Tera's template syntax with double curly braces:
```
{{variable_name}}
{{function_name(parameter=value)}}
```

### Default Templates

The default template for all secrets:
```
<redacted:{{secret_type}}>
```

This produces output like:
- `<redacted:string>` for SecretString
- `<redacted:int>` for SecretInt
- `<redacted:record>` for SecretRecord

## Template Variables

The following variables are available in templates:

### `secret_type`
**Type**: String
**Description**: The type of the secret (e.g., "string", "int", "bool", "record", "list", "float", "binary", "date")
**Always Available**: Yes

```
{{secret_type}}
```

### `secret_length`
**Type**: Number
**Description**: The length of the secret value (character count for strings, element count for lists, etc.)
**Availability**: When the secret has content

```
Secret has {{secret_length}} characters
```

### `secret_string`
**Type**: String
**Description**: **⚠️ SENSITIVE**: The actual secret value as a string
**Availability**: When `show_unredacted` is enabled OR when used with template functions
**Security**: Respects `mask_secret` setting

```
The secret is: {{secret_string}}              → Direct variable access (no parentheses)
Length: {{strlen(s=secret_string)}}           → Used as function parameter
Prefix: {{take(n=3, s=secret_string)}}       → Used in other functions
```

## Template Functions

The templating system provides several built-in functions for flexible redaction:

### `replicate(s, n)`
**Purpose**: Repeat a string pattern n times
**Parameters**:
- `s` (string): The string to repeat
- `n` (number): Number of repetitions

**Examples**:
```
{{replicate(s="*", n=8)}}          → "********"
{{replicate(s="-", n=secret_length)}} → "-------" (matches secret length)
{{replicate(s="X", n=5)}}          → "XXXXX"
```

### `mask_partial(s, l, r, c)`
**Purpose**: Show parts of a string while masking the middle
**Parameters**:
- `s` (string, required): The string to mask
- `l` (number, optional): Characters to show from left (default: 0)
- `r` (number, optional): Characters to show from right (default: 0)
- `c` (string, optional): Masking character (default: "*")

**Examples**:
```
{{mask_partial(s="password123", l=2, r=2)}}     → "pa*******23"
{{mask_partial(s="secret", l=1, r=1, c="#")}}   → "s####t"
{{mask_partial(s="api-key")}}                   → "******"
```

**⚠️ Security Warning**: This function can expose parts of secrets. Use with extreme caution.

### `take(n, s)`
**Purpose**: Take the first n characters from a string
**Parameters**:
- `n` (number): Number of characters to take
- `s` (string): Source string

**Examples**:
```
{{take(n=3, s="hello world")}}     → "hel"
{{take(n=5, s="testing")}}         → "testi"
```

### `reverse(s)`
**Purpose**: Reverse a string
**Parameters**:
- `s` (string): String to reverse

**Examples**:
```
{{reverse(s="hello")}}             → "olleh"
{{reverse(s="123abc")}}            → "cba321"
```

### `strlen(s)`
**Purpose**: Get the length of a string
**Parameters**:
- `s` (string): String to measure

**Examples**:
```
{{strlen(s="hello")}}              → "5"
{{strlen(s=secret_string)}}        → Length of the secret
```


## Configuration Commands

### View Configuration
```nushell
# Show structured configuration
secret config show

# Show raw TOML
secret config show --raw

# Show config file path
secret config show --file-path
```

### Interactive Configuration
```nushell
# Interactive configuration with prompts
secret configure

# Set security level directly
secret configure --security-level paranoid
```

### Validate Configuration
```nushell
# Check configuration validity
secret config validate

# Validate with detailed output
secret config validate --verbose
```

### Backup and Restore
```nushell
# Export configuration
secret config export backup.toml

# Import configuration
secret config import backup.toml
```

### Reset Configuration
```nushell
# Reset to defaults (with confirmation)
secret config reset
```

## Examples

### Basic Templates

**Simple type indicator**:
```toml
redaction_template = "[{{secret_type}}]"
```
Output: `[string]`, `[int]`, etc.

**With length information**:
```toml
redaction_template = "{{secret_type}}({{secret_length}})"
```
Output: `string(8)`, `int(5)`, etc.

### Masking Templates

**Fixed-length asterisks**:
```toml
redaction_template = "{{replicate(s='*', n=8)}}"
```
Output: `********` (always 8 characters)

**Length-matched masking**:
```toml
redaction_template = "{{replicate(s='*', n=secret_length)}}"
```
Output: `*******` (matches actual secret length)

**Custom characters**:
```toml
redaction_template = "[{{replicate(s='-', n=secret_length)}}]"
```
Output: `[-------]` (dashes within brackets)

### Partial Reveal Templates

**⚠️ Use with extreme caution - these expose secret data**:

```toml
# Show first 2 and last 2 characters
redaction_template = "{{mask_partial(s=secret_string, l=2, r=2)}}"
```
Output: `pa****rd` for "password"

```toml
# Show only first 3 characters
redaction_template = "{{take(n=3, s=secret_string)}}..."
```
Output: `sec...` for "secret123"

### Complex Templates

**Conditional-style display**:
```toml
redaction_template = "{{secret_type}}: {{replicate(s='█', n=secret_length)}}"
```
Output: `string: ████████`

**Security-conscious partial display**:
```toml
redaction_template = "{{secret_type}}[{{strlen(s=secret_string)}}]: {{mask_partial(s=secret_string, l=1, r=0, c='*')}}"
```
Output: `string[8]: s*******`

**Multiple function combination**:
```toml
redaction_template = "<{{reverse(s=secret_type)}}:{{replicate(s='#', n=3)}}>"
```
Output: `<gnirts:###>` (reversed type with hash marks)

## Security Considerations

### Safe Templates ✅
These templates don't expose secret content:
```toml
redaction_template = "<redacted:{{secret_type}}>"                    # Default, safest
redaction_template = "{{secret_type}}({{secret_length}})"           # Shows only length
redaction_template = "{{replicate(s='*', n=secret_length)}}"        # Length-matched masking
redaction_template = "[PROTECTED:{{secret_type}}]"                  # Custom safe format
```

### Potentially Unsafe Templates ⚠️
These templates may expose secret data:
```toml
redaction_template = "{{secret_string}}"                          # Exposes full secret!
redaction_template = "{{mask_partial(s=secret_string, l=3, r=3)}}" # Exposes partial content
redaction_template = "{{take(n=4, s=secret_string)}}"             # Exposes prefix
```

### Template Security Guidelines

1. **Never use `secret_string` directly** unless `show_unredacted` is intentionally enabled
2. **Avoid `mask_partial` in production** unless you specifically need partial reveals
3. **Test templates thoroughly** before deploying to production
4. **Use `secret config validate`** to check template syntax
5. **Consider attack scenarios** where partial reveals might compromise security
6. **Document your template choices** and their security implications
7. **Regular security review** of custom templates

### Environment-Based Configuration

For development environments:
```bash
export SHOW_UNREDACTED=1  # Temporarily show actual values
```

For production environments:
```toml
[redaction]
show_unredacted = false    # Always false in production
mask_secret = false        # Unless you need extra masking
redaction_template = "<redacted:{{secret_type}}>"  # Safe default

[security]
level = "standard"         # Or "paranoid" for high-security environments
audit_enabled = true       # Always enabled in production
```

---

## Quick Reference

| Configuration | Default | Purpose |
|--------------|---------|---------|
| `mask_secret` | `false` | Mask secret values in template functions |
| `show_unredacted` | `false` | **⚠️ DANGEROUS**: Show actual secret values |
| `redaction_template` | `"<redacted:{{secret_type}}>"` | Template for secret display |
| `security.level` | `"standard"` | Overall security posture |
| `security.audit_enabled` | `true` | Enable audit logging |

| Template Function            | Purpose               | Security Risk       |
|------------------------------|-----------------------|----------------------|
| `replicate(s, n)`            | Repeat pattern        | ✅ Safe              |
| `strlen(s)`                  | String length         | ✅ Safe              |
| `reverse(s)`                 | Reverse string        | ✅ Depends on input  |
| `take(n, s)`                 | First n characters    | ✅ Can expose data   |
| `mask_partial(s, l, r, c)`   | Partial masking       | ⚠️ Exposes parts     |
| `secret_string` (variable)   | **Full secret value** | 🚨 **HIGH RISK**    |

For questions or security concerns, please review the [Security Guidelines](../SECURITY.md) and [Best Practices](BEST_PRACTICES.md).