[Improvement]: Normalize audit record fields to the audit table column widths in API Portal

Open Beginner friendly
#4,231 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
Half a day
Newbie friendliness
72/100
Issue type
Refactor
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript, sql
Domain
backend, database

Research direction

Start in portals/api-portal/src/middlewares/auditLogger.js at logUserAction, then follow the values into auditDao.record and check where util.resolveActor(req) supplies performed_by. Read the column widths in database/schema.sqlite.sql, schema.postgres.sql and schema.sqlserver.sql but do not change them. Done means one normalisation step before the insert, a field-named warning when trimming occurs, and unit tests for a normal string, a non-string, an over-long value and a missing resourceType.

Written by the indexing model from the issue text.

Description

Aspect/Other
Area

Developer Portal (api-portal)

Aspect

Aspect/Other (audit trail robustness)

Suggested Improvement

Current behaviour

logUserAction (portals/api-portal/src/middlewares/auditLogger.js) saves an audit row through auditDao.record. The values come from the caller (action, resourceUuid, resourceType, orgUuid) and from util.resolveActor(req), and go straight into the INSERT as bind parameters.

The audit table has narrow columns (database/schema.sqlite.sql, schema.postgres.sql, schema.sqlserver.sql):

Column Width
uuid, resource_uuid, org_uuid VARCHAR(40)
action, resource_type VARCHAR(50)
performed_by VARCHAR(255)

SQLite does not enforce these widths. Postgres and SQL Server do. If a value ever exceeds its column, or is not a plain string, the insert fails. The failure is caught by the best-effort .catch in logUserAction and only logged as a warning ("Failed to persist audit record"), so that audit row is lost without anything else noticing.

Likelihood today: low. The callers I checked pass ids that were resolved to internal UUIDs (36 characters) and fixed action and resource-type constants, so I do not know of a failing case. This is a defensive gap, not a reported failure.

Suggested improvement

  • Normalise the audit fields in one place before the insert: convert each to a string, trim it to its column width, and log a warning that names the field (not the value) when trimming happens.
  • Keep the write best-effort, as it is today.
  • Add unit tests for the normalisation: a normal string, a non-string value, an over-long value, and a missing resourceType.
  • No schema change.

Out of scope: changing what is audited, or making audit writes blocking.

Related Issues

None.

Dominant language
Go
Stars
85
Forks
137
Avg merge
1d 12h
Merged PRs (30d)
247

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from wso2/api-platform

All issues in wso2/api-platform

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.