|
| 1 | +# AGENTS.md |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +The Auth0 Deploy CLI is a TypeScript-based tool for managing Auth0 tenant configurations through bidirectional sync (export/import) operations. It supports both YAML and directory formats with dynamic keyword replacement for multi-environment workflows. |
| 6 | + |
| 7 | +**Key Capabilities:** |
| 8 | + |
| 9 | +- Export Auth0 tenant configurations to local files (YAML or directory structure) |
| 10 | +- Import configurations from local files to Auth0 tenants |
| 11 | +- Keyword replacement for multi-environment deployments |
| 12 | +- Resource-specific handlers for 30+ Auth0 resource types |
| 13 | + |
| 14 | +## Setup Commands |
| 15 | + |
| 16 | +```bash |
| 17 | +# Install dependencies |
| 18 | +npm install |
| 19 | + |
| 20 | +# Build TypeScript to JavaScript |
| 21 | +npm run build |
| 22 | + |
| 23 | +# Build in watch mode during development |
| 24 | +npm run dev |
| 25 | + |
| 26 | +# Run unit tests |
| 27 | +npm test |
| 28 | + |
| 29 | +# Run E2E tests (requires Auth0 tenant) |
| 30 | +npm run test:e2e:node-module |
| 31 | +npm run test:e2e:cli |
| 32 | + |
| 33 | +# Run linter |
| 34 | +npm run lint |
| 35 | +``` |
| 36 | + |
| 37 | +## Development Workflow |
| 38 | + |
| 39 | +### Local Development Commands |
| 40 | + |
| 41 | +Common operations for working with the CLI: |
| 42 | + |
| 43 | +```bash |
| 44 | +# Export tenant configuration to local/ |
| 45 | +npm run build && node lib/index.js export -c config-dev.json -f directory -o ./local/ |
| 46 | + |
| 47 | +# Import configuration |
| 48 | +npm run build && node lib/index.js import -c config-dev.json -i ./local/tenant.json |
| 49 | + |
| 50 | +# Export as YAML |
| 51 | +npm run build && node lib/index.js export -c config-dev.json -f yaml -o ./local-export/ |
| 52 | + |
| 53 | +# Import from YAML |
| 54 | +npm run build && node lib/index.js import -c config-dev.json -i ./local-export/tenant.yaml |
| 55 | +``` |
| 56 | + |
| 57 | +### Build Process |
| 58 | + |
| 59 | +- Source TypeScript files live in `src/` |
| 60 | +- Compiled JavaScript output goes to `lib/` (published to npm) |
| 61 | +- Always run `npm run build` before testing CLI changes |
| 62 | +- Use `npm run dev` for watch mode during active development |
| 63 | + |
| 64 | +### File Structure |
| 65 | + |
| 66 | +``` |
| 67 | +src/ # TypeScript source |
| 68 | +├── index.ts # CLI entry point |
| 69 | +├── commands/ # import.ts, export.ts |
| 70 | +├── tools/ # Core logic (deploy.ts, calculateChanges.ts) |
| 71 | +│ └── auth0/ # Auth0 API integration |
| 72 | +│ ├── handlers/ # 30+ resource-specific handlers |
| 73 | +│ └── schema/ # JSON validation schemas |
| 74 | +└── context/ # Format parsers (yaml/, directory/) |
| 75 | +
|
| 76 | +test/ # Test suite mirrors src/ |
| 77 | +├── tools/auth0/handlers/ # Handler unit tests |
| 78 | +├── context/ # Context parser tests |
| 79 | +└── e2e/ # End-to-end tests |
| 80 | +``` |
| 81 | + |
| 82 | +## Code Style & Conventions |
| 83 | + |
| 84 | +### TypeScript Standards |
| 85 | + |
| 86 | +- Use strict TypeScript configuration (`tsconfig.json`) |
| 87 | +- Define types in `src/types.ts` for shared interfaces |
| 88 | +- Follow existing patterns for handler implementations |
| 89 | +- Use proper async/await patterns, avoid callbacks |
| 90 | + |
| 91 | +### Handler Implementation Pattern |
| 92 | + |
| 93 | +Every resource handler in `src/tools/auth0/handlers/` must implement: |
| 94 | + |
| 95 | +```typescript |
| 96 | +class ResourceHandler { |
| 97 | + validate(); // Schema validation using AJV |
| 98 | + processChanges(); // CRUD operations with API calls |
| 99 | + calcChanges(); // Determine create/update/delete ops |
| 100 | + dump(); // Format for export |
| 101 | + getType(); // Fetch from Auth0 API |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +### Configuration Management |
| 106 | + |
| 107 | +- All environment variables prefixed with `AUTH0_` |
| 108 | +- Config priority: CLI args → env vars → config files → defaults |
| 109 | +- Support both direct values and JSON serialization for complex types |
| 110 | + |
| 111 | +### Error Handling |
| 112 | + |
| 113 | +- Use `ValidationError` class from `src/tools/validationError.ts` |
| 114 | +- Provide clear, actionable error messages |
| 115 | +- Preserve context when bubbling errors up |
| 116 | +- Validate early before making API calls |
| 117 | + |
| 118 | +## Testing Instructions |
| 119 | + |
| 120 | +### Running Tests |
| 121 | + |
| 122 | +```bash |
| 123 | +# Unit tests only (fast) |
| 124 | +npm test |
| 125 | + |
| 126 | +# E2E tests as node module |
| 127 | +npm run test:e2e:node-module |
| 128 | + |
| 129 | +# E2E tests as CLI (requires real tenant credentials) |
| 130 | +npm run test:e2e:cli |
| 131 | +``` |
| 132 | + |
| 133 | +### Test Structure |
| 134 | + |
| 135 | +- Unit tests mirror `src/` directory structure in `test/` |
| 136 | +- Use `.test.js` or `.test.ts` extensions |
| 137 | +- Handler tests use sinon stubs for Auth0 clients |
| 138 | +- Context tests use temporary directories with fixtures |
| 139 | +- E2E tests require real Auth0 tenant (configured via env vars) |
| 140 | + |
| 141 | +### Writing Handler Tests |
| 142 | + |
| 143 | +Pattern for testing resource handlers: |
| 144 | + |
| 145 | +```javascript |
| 146 | +const mockClient = { |
| 147 | + resource: { |
| 148 | + getAll: sinon.stub(), |
| 149 | + create: sinon.stub(), |
| 150 | + update: sinon.stub(), |
| 151 | + delete: sinon.stub(), |
| 152 | + }, |
| 153 | +}; |
| 154 | + |
| 155 | +const mockConfig = (key) => { |
| 156 | + const config = { |
| 157 | + AUTH0_ALLOW_DELETE: true, |
| 158 | + AUTH0_EXCLUDED: [], |
| 159 | + }; |
| 160 | + return config[key]; |
| 161 | +}; |
| 162 | +``` |
| 163 | + |
| 164 | +### Test Coverage Requirements |
| 165 | + |
| 166 | +- Add tests for any new handlers or features |
| 167 | +- Test both success and error paths |
| 168 | +- Check resource identifier mapping logic |
| 169 | +- Test keyword replacement in context parsers |
| 170 | + |
| 171 | +### Running Specific Tests |
| 172 | + |
| 173 | +```bash |
| 174 | +# Run specific test file |
| 175 | +npm test -- test/tools/auth0/handlers/clients.test.js |
| 176 | + |
| 177 | +# Run tests matching pattern |
| 178 | +npm test -- --grep "should validate clients" |
| 179 | +``` |
| 180 | + |
| 181 | +## Common Development Tasks |
| 182 | + |
| 183 | +### Adding a New Resource Handler |
| 184 | + |
| 185 | +1. Create handler in `src/tools/auth0/handlers/<resource>.ts` |
| 186 | +2. Implement all required methods (validate, processChanges, dump, etc.) |
| 187 | +3. Add resource type to `src/tools/constants.ts` |
| 188 | +4. Create JSON schema in `src/tools/auth0/schema/` |
| 189 | +5. Write unit tests in `test/tools/auth0/handlers/<resource>.test.js` |
| 190 | +6. Add E2E test coverage if applicable |
| 191 | +7. Update documentation in `docs/resource-specific-documentation.md` |
| 192 | + |
| 193 | +### Working with Context Parsers |
| 194 | + |
| 195 | +- **DirectoryContext** (`src/context/directory/`): Loads nested JSON files |
| 196 | +- **YAMLContext** (`src/context/yaml/`): Parses single YAML file |
| 197 | +- Both support keyword patterns: `@@KEY@@` (JSON-stringified) and `##KEY##` (literal) |
| 198 | +- Test with fixtures in `test/context/{directory,yaml}/` |
| 199 | + |
| 200 | +### Configuration Testing |
| 201 | + |
| 202 | +- Mock config functions return expected values for each key |
| 203 | +- Test resource exclusion with `AUTH0_EXCLUDED` and `AUTH0_EXCLUDED_*` patterns |
| 204 | +- Test property exclusion with `EXCLUDED_PROPS` and `INCLUDED_PROPS` |
| 205 | +- Verify keyword replacement mappings work correctly |
| 206 | + |
| 207 | +## Pull Request Guidelines |
| 208 | + |
| 209 | +### Before Committing |
| 210 | + |
| 211 | +```bash |
| 212 | +# Always run before commit |
| 213 | +npm run build |
| 214 | +npm test |
| 215 | +npm run lint |
| 216 | +``` |
| 217 | + |
| 218 | +### PR Checklist |
| 219 | + |
| 220 | +- [ ] All tests pass locally |
| 221 | +- [ ] New code has corresponding tests |
| 222 | +- [ ] TypeScript compiles without errors |
| 223 | +- [ ] Updated relevant documentation in `docs/` |
| 224 | +- [ ] Added entry to `CHANGELOG.md` if user-facing change |
| 225 | +- [ ] Tested with both YAML and directory formats if applicable |
| 226 | +- [ ] Checked backward compatibility |
| 227 | + |
| 228 | +### Commit Message Format |
| 229 | + |
| 230 | +Follow conventional commits style: |
| 231 | + |
| 232 | +- `feat: add support for new resource type` |
| 233 | +- `fix: resolve table formatting issue` |
| 234 | +- `docs: update handler implementation guide` |
| 235 | +- `test: add coverage for keyword replacement` |
| 236 | +- `refactor: simplify change calculation logic` |
| 237 | + |
| 238 | +## Security Considerations |
| 239 | + |
| 240 | +### API Credentials |
| 241 | + |
| 242 | +- Never commit Auth0 credentials or API keys |
| 243 | +- Config files with credentials should be gitignored |
| 244 | +- Use environment variables for sensitive data |
| 245 | +- Example configs use `.json.example` suffix |
| 246 | + |
| 247 | +### Validation & Safety |
| 248 | + |
| 249 | +- All resources validated against JSON schemas before processing |
| 250 | +- Delete operations require explicit `AUTH0_ALLOW_DELETE=true` |
| 251 | +- Resource identifiers properly sanitized before API calls |
| 252 | + |
| 253 | +### Testing with Real Tenants |
| 254 | + |
| 255 | +- Use dedicated development tenants for E2E tests |
| 256 | +- Never run E2E tests against production tenants |
| 257 | +- Credentials stored in environment variables only |
| 258 | +- Clean up test resources after test runs |
| 259 | + |
| 260 | +## Debugging Tips |
| 261 | + |
| 262 | +### Enable Debug Logging |
| 263 | + |
| 264 | +```bash |
| 265 | +export AUTH0_DEBUG=true |
| 266 | +npm run build && node lib/index.js import -c config.json |
| 267 | +``` |
| 268 | + |
| 269 | +### Common Issues |
| 270 | + |
| 271 | +- **Build errors**: Check `tsconfig.json` and ensure all imports resolve |
| 272 | +- **Handler not found**: Verify resource added to `src/tools/constants.ts` |
| 273 | +- **Schema validation fails**: Check JSON schema in `src/tools/auth0/schema/` |
| 274 | +- **Keyword replacement not working**: Verify mappings in config and context parser |
| 275 | + |
| 276 | +### Useful Commands |
| 277 | + |
| 278 | +```bash |
| 279 | +# Check compiled output |
| 280 | +cat lib/index.js |
| 281 | + |
| 282 | +# Test CLI directly |
| 283 | +node lib/index.js export -c config.json --output_folder ./test-output |
| 284 | + |
| 285 | +# Run with verbose errors |
| 286 | +NODE_ENV=development npm test |
| 287 | + |
| 288 | +# Check Auth0 API pagination |
| 289 | +AUTH0_DEBUG=true npm run test:e2e:node-module |
| 290 | +``` |
| 291 | + |
| 292 | +## Architecture Notes |
| 293 | + |
| 294 | +### Request Flow |
| 295 | + |
| 296 | +1. **CLI Entry** (`src/index.ts`) → Command routing |
| 297 | +2. **Commands** (`src/commands/import.ts`) → Load config and context |
| 298 | +3. **Deploy** (`src/tools/deploy.ts`) → Orchestrate deployment |
| 299 | +4. **Handlers** (`src/tools/auth0/handlers/*`) → Resource-specific logic |
| 300 | +5. **Auth0 Client** (`src/tools/auth0/client.ts`) → Management API calls |
| 301 | + |
| 302 | +### Change Calculation |
| 303 | + |
| 304 | +- Compare local assets (from YAML/directory) with remote state (from API) |
| 305 | +- Use resource identifiers (name, id, etc.) to match resources |
| 306 | +- Determine CREATE (new), UPDATE (changed), DELETE (removed) operations |
| 307 | +- Respect exclusion patterns and allow-delete config |
| 308 | + |
| 309 | +## Additional Resources |
| 310 | + |
| 311 | +- **Full documentation**: See `docs/` directory |
| 312 | +- **Examples**: Check `examples/yaml/` and `examples/directory/` |
| 313 | +- **Contributing**: Read `CONTRIBUTING.md` |
| 314 | +- **Migration guide**: See `docs/v8_MIGRATION_GUIDE.md` |
| 315 | +- **Issue templates**: `.github/ISSUE_TEMPLATE/` |
| 316 | + |
| 317 | +## Quick Reference |
| 318 | + |
| 319 | +### Environment Variables |
| 320 | + |
| 321 | +```bash |
| 322 | +AUTH0_DOMAIN # Tenant domain |
| 323 | +AUTH0_CLIENT_ID # Client ID |
| 324 | +AUTH0_CLIENT_SECRET # Client secret |
| 325 | +AUTH0_ALLOW_DELETE=true # Enable deletions |
| 326 | +AUTH0_KEYWORD_REPLACE_MAPPINGS # JSON mapping for replacements |
| 327 | +AUTH0_EXCLUDED # Array of resources to exclude |
| 328 | +AUTH0_DEBUG=true # Verbose logging |
| 329 | +``` |
| 330 | + |
| 331 | +### Key Files to Know |
| 332 | + |
| 333 | +- `src/tools/deploy.ts` - Main deployment orchestrator |
| 334 | +- `src/tools/calculateChanges.ts` - Change detection logic |
| 335 | +- `src/tools/constants.ts` - Supported resource types |
| 336 | +- `src/configFactory.ts` - Configuration management |
| 337 | +- `src/types.ts` - TypeScript type definitions |
| 338 | +- `test/utils.js` - Test helper functions |
0 commit comments