Skip to content

Commit 46dcd3f

Browse files
committed
Add Audit Service to CE and PDP in OE federator
1 parent 8b29986 commit 46dcd3f

18 files changed

Lines changed: 1188 additions & 142 deletions

File tree

audit-service/README.md

Lines changed: 98 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Audit Service
22

3-
A Go microservice for centralized audit logging across OpenDIF services, providing distributed tracing and comprehensive event tracking.
3+
A generic Go microservice for centralized audit logging across distributed services, providing distributed tracing and comprehensive event tracking.
44

5-
[![Go Version](https://img.shields.io/badge/Go-1.21%2B-blue)](https://golang.org/)
5+
[![Go Version](https://img.shields.io/badge/Go-1.24.6%2B-blue)](https://golang.org/)
66
[![License](https://img.shields.io/badge/License-Apache%202.0-green.svg)](../LICENSE)
77

88
## Overview
@@ -11,26 +11,26 @@ The Audit Service tracks "who did what, when, and with what result" by providing
1111

1212
**Key Features:**
1313

14-
- 📝 Create and retrieve audit logs via REST API
15-
- 🔍 Filter by trace ID, event type, status, and more
16-
- 🗄️ Multiple database backends (SQLite, PostgreSQL)
17-
- 🚀 Zero configuration - works out of the box with in-memory database
18-
- 📊 Distributed tracing support
19-
- 🔌 CORS-enabled for cross-origin requests
14+
- Create and retrieve audit logs via REST API
15+
- Filter by trace ID, event type, status, and more
16+
- Multiple database backends (SQLite, PostgreSQL)
17+
- Zero configuration - works out of the box with in-memory database
18+
- Distributed tracing support
19+
- CORS-enabled for cross-origin requests
20+
- Configurable event types and enums via YAML
2021

2122
## Quick Start
2223

2324
### Prerequisites
2425

25-
- Go 1.21 or higher
26+
- Go 1.24.6 or higher
2627
- (Optional) PostgreSQL for production deployments
2728

2829
### Installation & Running
2930

3031
```bash
31-
# Clone the repository (if not already cloned)
32-
git clone https://github.com/OpenDIF/opendif-core.git
33-
cd opendif-core/audit-service
32+
# Navigate to the audit-service directory
33+
cd audit-service
3434

3535
# Install dependencies
3636
go mod tidy
@@ -115,6 +115,31 @@ Copy `.env.example` to `.env` and configure:
115115

116116
For PostgreSQL configuration and advanced settings, see [.env.example](.env.example).
117117

118+
### Event Type Configuration
119+
120+
Event types are configurable via `config/enums.yaml`. This allows you to customize the audit service for your specific use case. The service comes with generic default event types, but you can add project-specific ones.
121+
122+
**Default Event Types:**
123+
- `MANAGEMENT_EVENT` - Administrative operations
124+
- `USER_MANAGEMENT` - User-related operations
125+
- `DATA_FETCH` - Data retrieval operations
126+
127+
**Customizing Event Types:**
128+
129+
Edit `config/enums.yaml` to add your own event types:
130+
131+
```yaml
132+
enums:
133+
eventTypes:
134+
- MANAGEMENT_EVENT
135+
- USER_MANAGEMENT
136+
- DATA_FETCH
137+
- YOUR_CUSTOM_EVENT_TYPE
138+
- ANOTHER_EVENT_TYPE
139+
```
140+
141+
See [config/README.md](config/README.md) for detailed configuration options.
142+
118143
## API Endpoints
119144
120145
### Core Endpoints
@@ -136,13 +161,13 @@ curl -X POST http://localhost:3001/api/audit-logs \
136161
-d '{
137162
"traceId": "550e8400-e29b-41d4-a716-446655440000",
138163
"timestamp": "2024-01-20T10:00:00Z",
139-
"eventType": "POLICY_CHECK",
164+
"eventType": "MANAGEMENT_EVENT",
140165
"eventAction": "READ",
141166
"status": "SUCCESS",
142167
"actorType": "SERVICE",
143-
"actorId": "orchestration-engine",
168+
"actorId": "my-service",
144169
"targetType": "SERVICE",
145-
"targetId": "policy-decision-point"
170+
"targetId": "target-service"
146171
}'
147172
```
148173

@@ -156,18 +181,9 @@ curl http://localhost:3001/api/audit-logs
156181
curl http://localhost:3001/api/audit-logs?traceId=550e8400-e29b-41d4-a716-446655440000
157182
158183
# Filter by event type
159-
curl http://localhost:3001/api/audit-logs?eventType=POLICY_CHECK&status=SUCCESS
184+
curl http://localhost:3001/api/audit-logs?eventType=MANAGEMENT_EVENT&status=SUCCESS
160185
```
161186

162-
See [docs/API.md](docs/API.md) for complete API documentation.
163-
164-
## Documentation
165-
166-
- **[API Documentation](docs/API.md)** - Complete API reference with examples
167-
- **[Database Configuration](docs/DATABASE_CONFIGURATION.md)** - Database setup and configuration guide
168-
- **[Architecture](docs/ARCHITECTURE.md)** - Project structure and design patterns
169-
- **[OpenAPI Spec](openapi.yaml)** - OpenAPI 3.0 specification
170-
171187
## Development
172188

173189
### Project Structure
@@ -187,8 +203,6 @@ audit-service/
187203
└── main.go # Entry point
188204
```
189205
190-
See [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) for detailed architecture documentation.
191-
192206
### Running Tests
193207
194208
```bash
@@ -221,6 +235,49 @@ go build -o audit-service
221235
go build -ldflags="-X main.Version=1.0.0 -X main.GitCommit=$(git rev-parse HEAD)" -o audit-service
222236
```
223237

238+
## Integration
239+
240+
The Audit Service is designed to be integrated into any microservices architecture. It provides a simple REST API that can be called from any service.
241+
242+
### Integration Pattern
243+
244+
1. **Configure your service** to point to the audit service URL
245+
2. **Send audit events** via HTTP POST to `/api/audit-logs`
246+
3. **Query audit logs** via HTTP GET from `/api/audit-logs`
247+
248+
### Client Libraries
249+
250+
You can integrate the audit service using:
251+
252+
- **HTTP Client**: Direct HTTP calls to the REST API
253+
- **Shared Client Package**: Use the `shared/audit` package (if available in your project)
254+
- **Custom Wrapper**: Create your own client library
255+
256+
### Example Integration
257+
258+
```go
259+
// Example: Log an audit event from your service
260+
auditRequest := map[string]interface{}{
261+
"traceId": traceID,
262+
"timestamp": time.Now().UTC().Format(time.RFC3339),
263+
"eventType": "YOUR_EVENT_TYPE",
264+
"status": "SUCCESS",
265+
"actorType": "SERVICE",
266+
"actorId": "your-service-name",
267+
"targetType": "RESOURCE",
268+
"targetId": "resource-id",
269+
}
270+
271+
// POST to http://audit-service:3001/api/audit-logs
272+
```
273+
274+
### Graceful Degradation
275+
276+
- Services continue to function normally if audit service is unavailable
277+
- No errors are thrown when audit service URL is not configured
278+
- Audit operations should be asynchronous (fire-and-forget) to avoid blocking requests
279+
- Services can be started before audit service is ready
280+
224281
## Deployment
225282

226283
### Docker
@@ -243,13 +300,17 @@ docker run -d \
243300
-e DB_TYPE=postgres \
244301
-e DB_HOST=postgres \
245302
-e DB_PASSWORD=your_password \
303+
-e DB_NAME=audit_db \
246304
audit-service
247305
```
248306

249307
### Docker Compose
250308

309+
The audit service includes a `docker-compose.yml` for standalone deployment:
310+
251311
```bash
252-
# Start service
312+
# Deploy audit service
313+
cd audit-service
253314
docker compose up -d
254315

255316
# View logs
@@ -266,30 +327,8 @@ docker compose down
266327
3. **CORS**: Configure `CORS_ALLOWED_ORIGINS` appropriately
267328
4. **Monitoring**: Monitor service health via `/health` endpoint
268329
5. **Backup**: Implement database backup strategy
269-
270-
## Integration with OpenDIF Services
271-
272-
The Audit Service integrates with:
273-
274-
- **Orchestration Engine** - Tracks data exchange operations
275-
- **Portal Backend** - Logs administrative actions
276-
- **Consent Engine** - Records consent changes
277-
278-
Audit logging is **optional** - services function normally without it.
279-
280-
### Configuration in Other Services
281-
282-
```bash
283-
# Enable audit logging in orchestration-engine
284-
export AUDIT_SERVICE_ENABLED=true
285-
export AUDIT_SERVICE_URL=http://audit-service:3001
286-
287-
# Enable audit logging in portal-backend
288-
export AUDIT_SERVICE_ENABLED=true
289-
export AUDIT_SERVICE_URL=http://audit-service:3001
290-
```
291-
292-
See [../exchange/AUDIT_SERVICE.md](../exchange/AUDIT_SERVICE.md) for integration documentation.
330+
6. **High Availability**: Consider deploying multiple instances behind a load balancer
331+
7. **Security**: Implement authentication/authorization if exposing the service publicly
293332

294333
## Troubleshooting
295334

@@ -312,6 +351,12 @@ See [../exchange/AUDIT_SERVICE.md](../exchange/AUDIT_SERVICE.md) for integration
312351
- Check credentials and SSL settings
313352
- Verify network connectivity
314353

354+
**Event type validation errors:**
355+
356+
- Check that your event types are defined in `config/enums.yaml`
357+
- Verify the enum configuration file is being loaded correctly
358+
- Check service logs for validation error details
359+
315360
See [docs/DATABASE_CONFIGURATION.md](docs/DATABASE_CONFIGURATION.md) for detailed troubleshooting.
316361

317362
## Contributing
@@ -328,12 +373,4 @@ This project is licensed under the Apache License 2.0 - see [LICENSE](../LICENSE
328373

329374
## Support
330375

331-
- **Issues**: [GitHub Issues](https://github.com/OpenDIF/opendif-core/issues)
332-
- **Discussions**: [GitHub Discussions](https://github.com/OpenDIF/opendif-core/discussions)
333-
- **Documentation**: [OpenDIF Documentation](https://github.com/OpenDIF/opendif-core/tree/main/docs)
334-
335-
## Related Services
336-
337-
- [Orchestration Engine](../exchange/orchestration-engine/) - Data exchange orchestration
338-
- [Portal Backend](../portal-backend/) - Admin portal backend
339-
- [Consent Engine](../exchange/consent-engine/) - Consent management
376+
For issues, questions, or contributions, please use the project's issue tracker and discussion forums.

audit-service/config/config.go

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,34 @@ type Config struct {
3232
Enums AuditEnums `yaml:"enums"`
3333
}
3434

35-
// DefaultEnums provides default enum values if config file is not found
36-
var DefaultEnums = AuditEnums{
37-
EventTypes: []string{
38-
"POLICY_CHECK",
39-
"MANAGEMENT_EVENT",
40-
"USER_MANAGEMENT",
41-
"DATA_FETCH",
42-
"CONSENT_CHECK",
43-
},
44-
EventActions: []string{
45-
"CREATE",
46-
"READ",
47-
"UPDATE",
48-
"DELETE",
49-
},
50-
ActorTypes: []string{
51-
"SERVICE",
52-
"ADMIN",
53-
"MEMBER",
54-
"SYSTEM",
55-
},
56-
TargetTypes: []string{
57-
"SERVICE",
58-
"RESOURCE",
59-
},
60-
}
35+
var (
36+
// DefaultEnums provides default enum values if config file is not found
37+
// Note: OpenDIF-specific event types (ORCHESTRATION_REQUEST_RECEIVED, POLICY_CHECK, CONSENT_CHECK, PROVIDER_FETCH)
38+
// should be added to config/enums.yaml for project-specific configurations
39+
DefaultEnums = AuditEnums{
40+
EventTypes: []string{
41+
"MANAGEMENT_EVENT",
42+
"USER_MANAGEMENT",
43+
"DATA_FETCH",
44+
},
45+
EventActions: []string{
46+
"CREATE",
47+
"READ",
48+
"UPDATE",
49+
"DELETE",
50+
},
51+
ActorTypes: []string{
52+
"SERVICE",
53+
"ADMIN",
54+
"MEMBER",
55+
"SYSTEM",
56+
},
57+
TargetTypes: []string{
58+
"SERVICE",
59+
"RESOURCE",
60+
},
61+
}
62+
)
6163

6264
// LoadEnums loads enum configuration from YAML file
6365
// If the file is not found or cannot be read, returns default enums

audit-service/config/enums.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ enums:
1111
- USER_MANAGEMENT
1212
- DATA_FETCH
1313
- CONSENT_CHECK
14+
- ORCHESTRATION_REQUEST_RECEIVED
15+
- PROVIDER_FETCH
1416

1517
# Event Action: CRUD operations
1618
eventActions:

audit-service/v1/database/gorm_repository.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ func (r *GormRepository) GetAuditLogs(ctx context.Context, filters *AuditLogFilt
7777
}
7878

7979
// Apply pagination and ordering
80+
// Note: Results are ordered by timestamp DESC (newest first) for general queries.
81+
// For trace-specific queries, use GetAuditLogsByTraceID which orders by ASC (chronological).
8082
limit := filters.Limit
8183
if limit <= 0 {
8284
limit = 100 // default

0 commit comments

Comments
 (0)