Skip to content

Commit 2c81950

Browse files
authored
Merge pull request #93 from redis/feature/token-cli-bootstrap-docs
Document token CLI JSON output and CI/Terraform bootstrapping
2 parents c6366de + 322444f commit 2c81950

File tree

2 files changed

+114
-3
lines changed

2 files changed

+114
-3
lines changed

docs/authentication.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,76 @@ uv run agent-memory token remove abc12345
8686
# Remove a token without confirmation
8787
uv run agent-memory token remove abc12345 --force
8888
```
89+
### CI/Terraform token bootstrapping
90+
91+
You can use the token CLI to bootstrap authentication tokens in CI pipelines and infrastructure-as-code tools like Terraform.
92+
93+
The key features that make this work well are:
94+
95+
- `--format json` on token commands (`add`, `list`, `show`) for machine-readable output
96+
- `--token` on `token add` to register a pre-generated secret from your CI or secrets manager
97+
98+
#### GitHub Actions example (generate token in CI)
99+
100+
The example below generates a short-lived token during a workflow run and exposes it via `GITHUB_ENV` for later steps:
101+
102+
```yaml
103+
- name: Create agent-memory token for CI
104+
run: |
105+
TOKEN_JSON=$(agent-memory token add \
106+
--description "CI bootstrap token" \
107+
--expires-days 30 \
108+
--format json)
109+
echo "AGENT_MEMORY_TOKEN=$(echo "$TOKEN_JSON" | jq -r '.token')" >> "$GITHUB_ENV"
110+
```
111+
112+
Later steps can use `AGENT_MEMORY_TOKEN` as the bearer token when calling the API.
113+
**JSON output format for `token add`**
114+
115+
When you use `--format json`, `agent-memory token add` returns a JSON object with fields like:
116+
117+
```json
118+
{
119+
"token": "the-plaintext-token",
120+
"hash": "abc12345def67890...",
121+
"description": "CI bootstrap token",
122+
"created_at": "2025-07-10T18:30:00.000000+00:00",
123+
"expires_at": "2025-08-09T18:30:00.000000+00:00"
124+
}
125+
```
126+
127+
You can extract any of these fields in your scripts (for example, `.token` or `.hash` with `jq`).
128+
129+
#### Terraform example (register a pre-generated token)
130+
131+
If you generate tokens outside Redis Agent Memory Server (for example via a secrets manager), you can register them using `--token` so the server only ever stores a hash:
132+
133+
```hcl
134+
variable "agent_memory_token" {
135+
type = string
136+
sensitive = true
137+
}
138+
139+
resource "terraform_data" "agent_memory_token" {
140+
provisioner "local-exec" {
141+
environment = {
142+
AGENT_MEMORY_TOKEN = var.agent_memory_token
143+
}
144+
145+
command = <<EOT
146+
TOKEN_JSON=$(agent-memory token add \
147+
--description "Terraform bootstrap" \
148+
--token "$AGENT_MEMORY_TOKEN" \
149+
--format json)
150+
echo "$TOKEN_JSON" > agent-memory-token.json
151+
EOT
152+
}
153+
}
154+
```
155+
156+
For Terraform versions earlier than 1.4, you can use a `null_resource` instead of `terraform_data`.
157+
158+
In both cases, store the plaintext token in a secure secret store (GitHub Actions secrets, Terraform variables, Vault, etc.). The server will hash it before storing. When the CLI generates a token (without `--token`), it displays the plaintext only once; when using `--token` with a pre-generated value, ensure you've already stored it securely.
89159

90160
**Security Features:**
91161
- Tokens are hashed using bcrypt before storage

docs/cli.md

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,15 @@ Manages authentication tokens for token-based authentication. The token command
134134
Creates a new authentication token.
135135

136136
```bash
137-
agent-memory token add --description "DESCRIPTION" [--expires-days DAYS]
137+
agent-memory token add --description "DESCRIPTION" [--expires-days DAYS] [--format text|json] [--token TOKEN_VALUE]
138138
```
139139

140140
**Options:**
141141

142142
- `--description TEXT` / `-d TEXT`: **Required**. Description for the token (e.g., "API access for service X")
143143
- `--expires-days INTEGER` / `-e INTEGER`: **Optional**. Number of days until token expires. If not specified, token never expires.
144+
- `--format [text|json]`: **Optional**. Output format. `text` (default) is human-readable; `json` is machine-readable and recommended for CI or scripting.
145+
- `--token TEXT`: **Optional**. Use a pre-generated token value instead of having the CLI generate one. The CLI will hash and store the provided token; make sure you've stored the plaintext securely in your secrets manager or CI system.
144146

145147
**Examples:**
146148

@@ -150,6 +152,12 @@ agent-memory token add --description "API access token" --expires-days 30
150152

151153
# Create a permanent token (no expiration)
152154
agent-memory token add --description "Service account token"
155+
156+
# Create a token and return JSON (for CI/scripts)
157+
agent-memory token add --description "CI token" --expires-days 30 --format json
158+
159+
# Register a pre-generated token (e.g., from a secrets manager)
160+
agent-memory token add --description "Terraform bootstrap" --token "$MY_TOKEN" --format json
153161
```
154162

155163
**Security Note:** The generated token is displayed only once. Store it securely as it cannot be retrieved again.
@@ -159,7 +167,28 @@ agent-memory token add --description "Service account token"
159167
Lists all authentication tokens, showing masked token hashes, descriptions, and expiration dates.
160168

161169
```bash
162-
agent-memory token list
170+
agent-memory token list [--format text|json]
171+
```
172+
173+
When `--format json` is used, the command prints a JSON array of token summaries suitable for scripting and CI pipelines. The default `text` format produces human-readable output like the example below.
174+
**JSON Output Example:**
175+
```json
176+
[
177+
{
178+
"hash": "abc12345def67890xyz",
179+
"description": "API access token",
180+
"created_at": "2025-07-10T18:30:00.000000+00:00",
181+
"expires_at": "2025-08-09T18:30:00.000000+00:00",
182+
"status": "Active"
183+
},
184+
{
185+
"hash": "def09876uvw54321...",
186+
"description": "Service account token",
187+
"created_at": "2025-07-10T19:00:00.000000+00:00",
188+
"expires_at": null,
189+
"status": "Never Expires"
190+
}
191+
]
163192
```
164193

165194
**Example Output:**
@@ -183,7 +212,19 @@ Expires: Never
183212
Shows detailed information about a specific token. Supports partial hash matching for convenience.
184213

185214
```bash
186-
agent-memory token show TOKEN_HASH
215+
agent-memory token show TOKEN_HASH [--format text|json]
216+
```
217+
218+
When `--format json` is used, the command prints a JSON object with token details (including status) suitable for scripting and CI pipelines. The default `text` format produces human-readable output.
219+
**JSON Output Example:**
220+
```json
221+
{
222+
"hash": "abc12345def67890xyz",
223+
"description": "API access token",
224+
"created_at": "2025-07-10T18:30:00.000000+00:00",
225+
"expires_at": "2025-08-09T18:30:00.000000+00:00",
226+
"status": "Active"
227+
}
187228
```
188229

189230
**Arguments:**

0 commit comments

Comments
 (0)