Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add audit-log-node back #2632

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/hip-games-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
2 changes: 2 additions & 0 deletions plugins/audit-log-node/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist-dynamic
dist-scalprum
1 change: 1 addition & 0 deletions plugins/audit-log-node/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@backstage/cli/config/eslint-factory')(__dirname);
4 changes: 4 additions & 0 deletions plugins/audit-log-node/.lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"*": "prettier --ignore-unknown --write",
"*.{js,jsx,ts,tsx,mjs,cjs}": "backstage-cli package lint --fix"
}
12 changes: 12 additions & 0 deletions plugins/audit-log-node/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
dist
dist-types
coverage
.vscode
CHANGELOG.md
generated
templates
*.hbs
renovate.json
dist-dynamic
dist-scalprum
playwright-report
20 changes: 20 additions & 0 deletions plugins/audit-log-node/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @ts-check

/** @type {import("@ianvs/prettier-plugin-sort-imports").PrettierConfig} */
module.exports = {
...require('@spotify/prettier-config'),
plugins: ['@ianvs/prettier-plugin-sort-imports'],
importOrder: [
'^react(.*)$',
'',
'^@backstage/(.*)$',
'',
'<THIRD_PARTY_MODULES>',
'',
'^@janus-idp/(.*)$',
'',
'<BUILTIN_MODULES>',
'',
'^[.]',
],
};
2 changes: 2 additions & 0 deletions plugins/audit-log-node/.versionhistory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Bumped to 1.5.0 in main branch for next release 1.3.0
- Bumped to 1.8.0 in main branch, in prep for release of 1.4.0
61 changes: 61 additions & 0 deletions plugins/audit-log-node/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
## @janus-idp/backstage-plugin-audit-log-node [1.4.0](https://github.com/janus-idp/backstage-plugins/compare/@janus-idp/backstage-plugin-audit-log-node@1.3.0...@janus-idp/backstage-plugin-audit-log-node@1.4.0) (2024-07-25)

## 1.8.1

### Patch Changes

- 837d5d0: bump express to 4.21.2

## 1.8.0

### Minor Changes

- 9671df5: Bump plugins/audit-log-node to 1.8.0 in main branch, in prep for release of 1.4.0

## 1.7.1

### Patch Changes

- 0e6bfd3: feat: update Backstage to the latest version

Update to Backstage 1.32.5

## 1.7.0

### Minor Changes

- 8244f28: chore(deps): update to backstage 1.32

## 1.6.0

### Minor Changes

- d9551ae: feat(deps): update to backstage 1.31

### Patch Changes

- d9551ae: upgrade to yarn v3

### Features

- **deps:** update to backstage 1.29 ([#1900](https://github.com/janus-idp/backstage-plugins/issues/1900)) ([f53677f](https://github.com/janus-idp/backstage-plugins/commit/f53677fb02d6df43a9de98c43a9f101a6db76802))

## @janus-idp/backstage-plugin-audit-log-node [1.3.0](https://github.com/janus-idp/backstage-plugins/compare/@janus-idp/backstage-plugin-audit-log-node@1.2.0...@janus-idp/backstage-plugin-audit-log-node@1.3.0) (2024-07-23)

### Features

- **deps:** update to backstage 1.28 ([#1891](https://github.com/janus-idp/backstage-plugins/issues/1891)) ([1ba1108](https://github.com/janus-idp/backstage-plugins/commit/1ba11088e0de60e90d138944267b83600dc446e5))

## @janus-idp/backstage-plugin-audit-log-node [1.2.0](https://github.com/janus-idp/backstage-plugins/compare/@janus-idp/backstage-plugin-audit-log-node@1.1.0...@janus-idp/backstage-plugin-audit-log-node@1.2.0) (2024-06-13)

### Features

- **deps:** update to backstage 1.27 ([#1683](https://github.com/janus-idp/backstage-plugins/issues/1683)) ([a14869c](https://github.com/janus-idp/backstage-plugins/commit/a14869c3f4177049cb8d6552b36c3ffd17e7997d))

## @janus-idp/backstage-plugin-audit-log-node [1.1.0](https://github.com/janus-idp/backstage-plugins/compare/@janus-idp/backstage-plugin-audit-log-node@1.0.3...@janus-idp/backstage-plugin-audit-log-node@1.1.0) (2024-06-05)

### Features

- **rbac:** add type checks with generics for audit log ([#1789](https://github.com/janus-idp/backstage-plugins/issues/1789)) ([ac69838](https://github.com/janus-idp/backstage-plugins/commit/ac698382f64fe91e0f9f9232dd3eecd9cc9247be))

## @janus-idp/backstage-plugin-audit-log-node [1.0.3](https://github.com/janus-idp/backstage-plugins/compare/@janus-idp/backstage-plugin-audit-log-node@1.0.2...@janus-idp/backstage-plugin-audit-log-node@1.0.3) (2024-06-04)
223 changes: 221 additions & 2 deletions plugins/audit-log-node/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,222 @@
# ❗DEPRECATED❗
# @janus-idp/backstage-plugin-audit-log-node

Starting from Backstage v1.36.0, the Auditor Service will be included in Backstage's core services. As a result, this package is now deprecated.
This package contains common types and utility functions for audit logging the backend

## Installation

To install this plugin in a package/plugin, run the following command:

```console
yarn workspace <package/plugin> add @janus-idp/backstage-plugin-audit-log-node
```

### Usage

The audit logging node package contains a helper class for generating audit logs with a common structure, as well as logging them.

The `auditLog` function can be used to log out an audit log using the backstage `LoggerService`. You can provide a log level to the `auditLog` function. The supported levels are: `info`, `debug`, `warn`, and `error`. If no log level is provided, it defaults to the `info` level.

Alternatively, if you want to generate the audit log object (does not contain message) without it being logged out for you, the `createAuditLogDetails` helper function of the `DefaultAuditLogger` can be used.

The `DefaultAuditLogger.createAuditLogDetails` will generate the `actorId` of the actor with the following priority (highest to lowest):

- The `actorId` provided in the arguments
- The actor id generated from the `express.Request` object provided in the arguments
- `null` if neither of the above fields were provided in the arguments

#### Event Naming Convention

It is recommended that you prefix the `eventName` value with the name of the component you are audit logging. This will help with searchability in the central log collector.

For example, "ScaffolderTaskRead", "CatalogEntityFetch", etc.

---

**IMPORTANT**

Any fields containing secrets provided to these helper functions should have secrets redacted or else they will be logged as is.

For the `DefaultAuditLogger`, these fields would include:

- The `metadata` field
- The following fields in the `request`:
- `request.body`
- `request.params`
- `request.query`
- The `response.body` field

---

The `getActorId` helper function grabs the specified entityRef of the user or service associated with the provided credentials in the provided express Request object. If no request is provided or no user/service was associated to the request, `undefined` is returned.

### Example

#### Audit Log Example

In the following example, we add a simple audit log for the `/health` endpoint of a plugin's router to the `debug` log level.

```ts plugins/test/src/service/router.ts
import {
AuthService,
HttpAuthService,
LoggerService,
} from '@backstage/backend-plugin-api';
import { Config } from '@backstage/config';

/* highlight-add-start */
import { DefaultAuditLogger } from '@janus-idp/backstage-plugin-audit-log-node';

/* highlight-add-end */

export interface RouterOptions {
logger: LoggerService;
config: Config;
auth: AuthService;
httpAuth: HttpAuthService;
}

export async function createRouter(
options: RouterOptions,
): Promise<express.Router> {
const { logger, config, auth, httpAuth } = options;

/* highlight-add-start */
const auditLogger = new DefaultAuditLogger({
logger,
auth,
httpAuth,
});
/* highlight-add-end */

const router = Router();
router.use(express.json());

router.get('/health', async (request, response) => {
logger.info('PONG!');
response.json({ status: 'ok' });

/* highlight-add-start */
// Note: if `level` is not provided, it defaults to `info`
auditLogger.auditLog({
eventName: 'HealthEndpointHit',
stage: 'completion',
status: 'succeeded',
level: 'debug',
request,
response: {
status: 200,
body: { status: 'ok' },
},
message: `The Health Endpoint was hit by ${await auditLogger.getActorId(
request,
)}`,
});
/* highlight-add-end */
});

const middleware = MiddlewareFactory.create({ logger, config });

router.use(middleware.error());
return router;
}
```

Assuming the `user:default/tester` user hit requested this endpoint, something similar to the following would be outputted if the logger format is JSON:

```JSON
{"actor":{"actorId":"user:default/tester","hostname":"localhost","ip":"::1","userAgent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"},"eventName":"HealthEndpointHit","isAuditLog":true,"level":"debug","message":"The Health Endpoint was hit by user:default/tester","meta":{},"plugin":"test","request":{"body": "","method":"GET","params":{},"query":{},"url":"/api/test/health"},"service":"backstage","stage":"completion","status":"succeeded","timestamp":"2024-05-17 11:17:07","type":"plugin"}
```

#### Audit Log Error Example

In the following example, we utilize the `auditLog` utility function to generate and output an error log to the `error` log level:

```ts plugins/test/src/service/router.ts
import {
AuthService,
HttpAuthService,
LoggerService,
} from '@backstage/backend-plugin-api';

/* highlight-add-start */
import { DefaultAuditLogger } from '@janus-idp/backstage-plugin-audit-log-node';

/* highlight-add-end */

export interface RouterOptions {
logger: LoggerService;
auth: AuthService;
httpAuth: HttpAuthService;
}

export async function createRouter(
options: RouterOptions,
): Promise<express.Router> {
const { logger, auth, httpAuth } = options;

/* highlight-add-start */
const auditLogger = new DefaultAuditLogger({
logger,
auth,
httpAuth,
});
/* highlight-add-end */

const router = Router();
router.use(express.json());

router.get('/error', async (request, response) => {
try {
const customErr = new Error('Custom Error Occurred');
customErr.name = 'CustomError';

throw customErr;

response.json({
status: 'ok',
});
} catch (err) {
/* highlight-add-start */
auditLogger.auditLog({
eventName: 'ErrorEndpointHit',
stage: 'completion',
status: 'failed',
level: 'error',
request,
response: {
status: 501,
body: {
errors: [
{
name: (err as Error).name,
message: (err as Error).message,
},
],
},
},
errors: [customErr],
message: `An error occurred when querying the '/errors' endpoint`,
});
/* highlight-add-end */
// Do something with the caught error
response.status(501).json({
errors: [
{
name: (err as Error).name,
message: (err as Error).message,
},
],
});
}
});
router.use(errorHandler());
return router;
}
```

An example error audit log would be in the following form:
Note: the stack trace was removed redacted in this example due to its size.

```JSON
{"actor":{"actorId":"user:development/guest","hostname":"localhost","ip":"::1","userAgent":"curl/8.2.1"},"errors":[{"message":"Custom Error Occurred","name":"CustomError","stack":"CustomError: Custom Error Occurred\n at STACK_TRACE]"}],"eventName":"ErrorEndpointHit","isAuditLog":true,"level":"error","message":"An error occurred when querying the '/errors' endpoint","meta":{},"plugin":"test","request":{"body":{},"method":"GET","params":{},"query":{},"url":"/api/test/error"},"response":{"body":{"errors":[{"name":"CustomError","message":"Custom Error Occurred"}]},"status":501},"service":"backstage","stage":"completion","status":"failed","timestamp":"2024-05-23 10:09:04"}
```
51 changes: 51 additions & 0 deletions plugins/audit-log-node/catalog-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# https://backstage.io/docs/features/software-catalog/descriptor-format#kind-component
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: janus-idp-audit-log
title: Audit Log plugin
description: Backend for the audit-log
annotations:
backstage.io/source-location: url:https://github.com/janus-idp/backstage-plugins/tree/main/plugins/audit-log-node
backstage.io/view-url: https://github.com/janus-idp/backstage-plugins/blob/main/plugins/audit-log-node/catalog-info.yaml
backstage.io/edit-url: https://github.com/janus-idp/backstage-plugins/edit/main/plugins/audit-log-node/catalog-info.yaml
github.com/project-slug: janus-idp/backstage-plugins
github.com/team-slug: janus-idp/maintainers-plugins
sonarqube.org/project-key: janus-idp_backstage-plugins
links:
- url: https://github.com/janus-idp/backstage-plugins/tree/main/plugins/audit-log-node
title: GitHub Source
icon: source
type: source
spec:
type: backstage-plugin
lifecycle: production
owner: rhdh-team
system: rhdh
subcomponentOf: janus-idp-backstage-plugins
---
# https://backstage.io/docs/features/software-catalog/descriptor-format#kind-component
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: janus-idp-audit-log-node
title: '@janus-idp/backstage-plugin-audit-log-node'
description: Node.js library for the audit-log plugin
annotations:
backstage.io/source-location: url:https://github.com/janus-idp/backstage-plugins/tree/main/plugins/audit-log-node
backstage.io/view-url: https://github.com/janus-idp/backstage-plugins/blob/main/plugins/audit-log-node/catalog-info.yaml
backstage.io/edit-url: https://github.com/janus-idp/backstage-plugins/edit/main/plugins/audit-log-node/catalog-info.yaml
github.com/project-slug: janus-idp/backstage-plugins
github.com/team-slug: janus-idp/maintainers-plugins
sonarqube.org/project-key: janus-idp_backstage-plugins
links:
- url: https://github.com/janus-idp/backstage-plugins/tree/main/plugins/audit-log-node
title: GitHub Source
icon: source
type: source
spec:
type: backstage-node-library
lifecycle: production
owner: rhdh-team
system: rhdh
subcomponentOf: janus-idp-audit-log
Loading
Loading