@cap-js/audit-logging 0.7.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,7 +4,24 @@ All notable changes to this project will be documented in this file.
4
4
  This project adheres to [Semantic Versioning](http://semver.org/).
5
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/).
6
6
 
7
- ## Version 0.7.0 - tbd
7
+ ## Version 0.8.1 - 2024-09-13
8
+
9
+ ### Fixed
10
+
11
+ - Support for @sap/cds^8.2
12
+ - Reduce clutter in error raised for outbound requests
13
+
14
+ ## Version 0.8.0 - 2024-05-24
15
+
16
+ ### Added
17
+
18
+ - Allow to specify undefined tenant in order to log to provider account in multi-tenant scenarios
19
+
20
+ ### Changed
21
+
22
+ - Use kind `audit-log-to-restv2` in profile `hybrid`
23
+
24
+ ## Version 0.7.0 - 2024-05-15
8
25
 
9
26
  ### Added
10
27
 
package/README.md CHANGED
@@ -1,26 +1,142 @@
1
1
  # Welcome to @cap-js/audit-logging
2
- [![REUSE status](https://api.reuse.software/badge/github.com/cap-js/audit-logging)](https://api.reuse.software/info/github.com/cap-js/audit-logging)
3
2
 
4
- ## About this project
3
+ [![REUSE status](https://api.reuse.software/badge/github.com/cap-js/audit-logging)](https://api.reuse.software/info/github.com/cap-js/audit-logging)
5
4
 
6
5
  `@cap-js/audit-logging` is a CDS plugin providing integration to the SAP Audit Log service as well as out-of-the-box personal data-related audit logging based on annotations.
7
6
 
8
- Documentation can be found at [cap.cloud.sap](https://cap.cloud.sap/docs/guides/data-privacy).
7
+ Documentation can be found at [cap.cloud.sap](https://cap.cloud.sap/docs/guides/data-privacy).
8
+
9
+ > [!IMPORTANT]
10
+ > The information in this file is by no means complete but enables you to get started quickly. Make sure to read the provided documentation at [cap.cloud.sap](https://cap.cloud.sap/docs/guides/data-privacy) to get the full picture.
11
+
12
+
13
+ ## Preliminaries
14
+
15
+ In this guide, we use the [Incidents Management reference sample app](https://github.com/cap-js/incidents-app) as the base to add change tracking to. Clone the repository and apply the step-by-step instructions:
16
+
17
+ ```sh
18
+ git clone https://github.com/cap-js/incidents-app
19
+ cd incidents-app
20
+ npm i
21
+ ```
22
+
23
+
24
+ ## Setup
25
+
26
+ To enable audit logging, simply add this self-configuring plugin package to your project:
27
+
28
+ ```sh
29
+ npm add @cap-js/audit-logging
30
+ ```
31
+
32
+
33
+ ## Annotate Personal Data
34
+
35
+ Identify entities and elements (potentially) holding personal data using `@PersonalData` annotations. Create a `db/data-privacy.cds` file and add the following:
36
+
37
+ ```cds
38
+ using { sap.capire.incidents as my } from './schema';
39
+
40
+ annotate my.Customers with @PersonalData : {
41
+ DataSubjectRole : 'Customer',
42
+ EntitySemantics : 'DataSubject'
43
+ } {
44
+ ID @PersonalData.FieldSemantics: 'DataSubjectID';
45
+ firstName @PersonalData.IsPotentiallyPersonal;
46
+ lastName @PersonalData.IsPotentiallyPersonal;
47
+ email @PersonalData.IsPotentiallyPersonal;
48
+ phone @PersonalData.IsPotentiallyPersonal;
49
+ creditCardNo @PersonalData.IsPotentiallySensitive;
50
+ };
9
51
 
10
- ## Requirements
52
+ annotate my.Addresses with @PersonalData: {
53
+ EntitySemantics : 'DataSubjectDetails'
54
+ } {
55
+ customer @PersonalData.FieldSemantics: 'DataSubjectID';
56
+ city @PersonalData.IsPotentiallyPersonal;
57
+ postCode @PersonalData.IsPotentiallyPersonal;
58
+ streetAddress @PersonalData.IsPotentiallyPersonal;
59
+ };
11
60
 
12
- See [Getting Started](https://cap.cloud.sap/docs/get-started) on how to jumpstart your development and grow as you go with SAP Cloud Application Programming Model.
61
+ annotate my.Incidents with @PersonalData : {
62
+ EntitySemantics : 'Other'
63
+ } {
64
+ customer @PersonalData.FieldSemantics: 'DataSubjectID';
65
+ };
66
+
67
+ ```
68
+ Learn more about the annotations in capire:
69
+ - [@PersonalData.EntitySemantics](https://cap.cloud.sap/docs/guides/data-privacy/annotations#entitysemantics)
70
+ - [@PersonalData.EntitySemantics: 'DataSubject'](https://cap.cloud.sap/docs/guides/data-privacy/annotations#datasubjectrole)
71
+ - [@PersonalData.FieldSemantics: 'DataSubjectID'](https://cap.cloud.sap/docs/guides/data-privacy/annotations#fieldsemantics-datasubjectid)
72
+ - [@PersonalData.IsPotentiallyPersonal](https://cap.cloud.sap/docs/guides/data-privacy/annotations#ispotentiallypersonal)
73
+ - [@PersonalData.IsPotentiallySensitive](https://cap.cloud.sap/docs/guides/data-privacy/annotations#ispotentiallysensitive)
74
+
75
+
76
+ ## Test-Drive Locally
77
+
78
+ You've prepared everything to log personal data-related events. Let's see that in action.
79
+
80
+ Start the server as usual:
81
+ ```sh
82
+ cds watch
83
+ ```
84
+
85
+ Send an update request that changes personal data:
86
+ ```http
87
+ PATCH http://localhost:4004/odata/v4/admin/Customers('1004155')
88
+ Authorization: Basic alice:in-wonderland
89
+ Content-Type: application/json
90
+
91
+ {
92
+ "firstName": "Danny",
93
+ "lastName": "Joules"
94
+ }
95
+ ```
96
+
97
+ See the audit logs in the server's console output:
98
+ ```sh
99
+ [audit-log] - PersonalDataModified: {
100
+ data_subject: {
101
+ id: { ID: '1004155' },
102
+ role: 'Customer',
103
+ type: 'AdminService.Customers'
104
+ },
105
+ object: {
106
+ type: 'AdminService.Customers',
107
+ id: { ID: '1004155' }
108
+ },
109
+ attributes: [
110
+ { name: 'firstName', old: 'Daniel', new: 'Danny' },
111
+ { name: 'lastName', old: 'Watts', new: 'Joules' }
112
+ ],
113
+ uuid: '71fa93d9-c993-405f-ba1b-a9ef42668199',
114
+ tenant: 't1',
115
+ user: 'alice',
116
+ time: 2023-02-26T08:13:48.287Z
117
+ }
118
+ ```
119
+
120
+
121
+ ## In Production
13
122
 
14
123
  The end-to-end out-of-the-box functionality provided by this plugin requires a paid-for instance of the [SAP Audit Log service for customers](https://help.sap.com/docs/btp/sap-business-technology-platform/audit-log-write-api-for-customers?locale=en-US). However, it is possible to provide an own implementation that writes the audit logs to a custom store.
15
124
 
125
+ [_Learn more about using the SAP Audit Log service._](https://cap.cloud.sap/docs/guides/data-privacy/audit-logging#use-sap-audit-log-service)
126
+
127
+ [_Learn more about custom audit logging._](https://cap.cloud.sap/docs/guides/data-privacy/audit-logging#custom-audit-logging)
128
+
129
+
16
130
  ## Support, Feedback, Contributing
17
131
 
18
132
  This project is open to feature requests/suggestions, bug reports etc. via [GitHub issues](https://github.com/cap-js/audit-logging/issues). Contribution and feedback are encouraged and always welcome. For more information about how to contribute, the project structure, as well as additional contribution information, see our [Contribution Guidelines](CONTRIBUTING.md).
19
133
 
134
+
20
135
  ## Code of Conduct
21
136
 
22
137
  We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone. By participating in this project, you agree to abide by its [Code of Conduct](CODE_OF_CONDUCT.md) at all times.
23
138
 
139
+
24
140
  ## Licensing
25
141
 
26
142
  Copyright 2023 SAP SE or an SAP affiliate company and contributors. Please see our [LICENSE](LICENSE) for copyright and license information. Detailed information including third-party components and their licensing/copyright information is available [via the REUSE tool](https://api.reuse.software/info/github.com/cap-js/audit-logging).
package/lib/access.js CHANGED
@@ -2,7 +2,6 @@ const cds = require('@sap/cds')
2
2
 
3
3
  // REVISIT: don't require internal stuff
4
4
  const getTemplate = require('@sap/cds/libx/_runtime/common/utils/template')
5
- const templateProcessor = require('@sap/cds/libx/_runtime/common/utils/templateProcessor')
6
5
 
7
6
  const {
8
7
  getRootEntity,
@@ -56,8 +55,16 @@ const auditAccess = async function (data, req) {
56
55
  if (!template.elements.size) return
57
56
 
58
57
  const accessLogs = {}
59
- const _data = Array.isArray(data) ? data : [data]
60
- _data.forEach(row => templateProcessor({ processFn: _processorFnAccess(accessLogs, this.model, req), row, template }))
58
+ const processFn = _processorFnAccess(accessLogs, this.model, req)
59
+ // cds internal templating mechanism api changed in 8.2.0 -> polyfill
60
+ if (!template.process) {
61
+ module.exports._templateProcessor ??= require('@sap/cds/libx/_runtime/common/utils/templateProcessor')
62
+ template.process = (data, processFn) => {
63
+ const _data = Array.isArray(data) ? data : [data]
64
+ _data.forEach(row => module.exports._templateProcessor({ processFn, row, template }))
65
+ }
66
+ }
67
+ template.process(data, processFn)
61
68
 
62
69
  for (const each of Object.keys(accessLogs)) if (!accessLogs[each].attributes.length) delete accessLogs[each]
63
70
  if (!Object.keys(accessLogs).length) return
@@ -2,7 +2,6 @@ const cds = require('@sap/cds')
2
2
 
3
3
  // REVISIT: don't require internal stuff
4
4
  const getTemplate = require('@sap/cds/libx/_runtime/common/utils/template')
5
- const templateProcessor = require('@sap/cds/libx/_runtime/common/utils/templateProcessor')
6
5
 
7
6
  const {
8
7
  getMapKeyForCurrentRequest,
@@ -166,7 +165,14 @@ const _getDataModificationLogs = (req, tx, diff, beforeWrite) => {
166
165
 
167
166
  const modificationLogs = {}
168
167
  const processFn = _processorFnModification(modificationLogs, tx.model, req, beforeWrite)
169
- templateProcessor({ processFn, row: diff, template })
168
+ // cds internal templating mechanism api changed in 8.2.0 -> polyfill
169
+ if (!template.process) {
170
+ module.exports._templateProcessor ??= require('@sap/cds/libx/_runtime/common/utils/templateProcessor')
171
+ template.process = (data, processFn) => {
172
+ module.exports._templateProcessor({ processFn, row: data, template })
173
+ }
174
+ }
175
+ template.process(diff, processFn)
170
176
 
171
177
  return modificationLogs
172
178
  }
package/lib/utils.js CHANGED
@@ -1,3 +1,5 @@
1
+ const cds = require('@sap/cds')
2
+
1
3
  const WRITE = { CREATE: 1, UPDATE: 1, DELETE: 1 }
2
4
 
3
5
  const $hasPersonalData = Symbol('@cap-js/audit-logging:hasPersonalData')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cap-js/audit-logging",
3
- "version": "0.7.0",
3
+ "version": "0.8.1",
4
4
  "description": "CDS plugin providing integration to the SAP Audit Log service as well as out-of-the-box personal data-related audit logging based on annotations.",
5
5
  "repository": "cap-js/audit-logging",
6
6
  "author": "SAP SE (https://www.sap.com)",
@@ -16,16 +16,16 @@
16
16
  "lint": "npx eslint .",
17
17
  "test": "npm run test-new-db && npm run test-old-db",
18
18
  "test-new-db": "CDS_ENV='better-sqlite' npx jest --silent",
19
- "test-old-db": "npx jest --silent"
19
+ "test-old-db": "CDS_ENV='legacy-sqlite' npx jest --silent"
20
20
  },
21
21
  "peerDependencies": {
22
- "@sap/cds": "^7"
22
+ "@sap/cds": ">=7"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@cap-js/audit-logging": "file:.",
26
26
  "@cap-js/sqlite": "^1",
27
27
  "axios": "^1",
28
- "eslint": "^8",
28
+ "eslint": "^9",
29
29
  "express": "^4",
30
30
  "jest": "^29",
31
31
  "sqlite3": "^5.1.6"
@@ -44,6 +44,9 @@
44
44
  "[development]": {
45
45
  "kind": "audit-log-to-console"
46
46
  },
47
+ "[hybrid]": {
48
+ "kind": "audit-log-to-restv2"
49
+ },
47
50
  "[production]": {
48
51
  "kind": "audit-log-to-restv2"
49
52
  }
@@ -5,6 +5,7 @@ module.exports = class AuditLog2Console extends AuditLogService {
5
5
  this.on('*', function (req) {
6
6
  const { event, data } = req
7
7
 
8
+ // eslint-disable-next-line no-console
8
9
  console.log(`[audit-log] - ${event}:`, data)
9
10
  })
10
11
 
package/srv/log2restv2.js CHANGED
@@ -93,6 +93,7 @@ module.exports = class AuditLog2RESTv2 extends AuditLogService {
93
93
  } else {
94
94
  url = this.options.credentials.url + PATHS.OAUTH2[path]
95
95
  data.tenant ??= this._provider //> if request has no tenant, stay in provider account
96
+ if (data.tenant === '$PROVIDER') data.tenant = this._provider
96
97
  headers.authorization = 'Bearer ' + (await this._getToken(data.tenant))
97
98
  data.tenant = data.tenant === this._provider ? '$PROVIDER' : '$SUBSCRIBER'
98
99
  }
@@ -153,12 +154,16 @@ async function _post(url, data, options) {
153
154
  const chunks = []
154
155
  res.on('data', chunk => chunks.push(chunk))
155
156
  res.on('end', () => {
157
+ const { statusCode, statusMessage } = res
156
158
  let body = Buffer.concat(chunks).toString()
157
159
  if (res.headers['content-type']?.match(/json/)) body = JSON.parse(body)
158
160
  if (res.statusCode >= 400) {
159
- const err = new Error(res.statusMessage)
160
- err.response = res
161
- err.body = body
161
+ // prettier-ignore
162
+ const err = new Error(`Request failed with${statusMessage ? `: ${statusCode} - ${statusMessage}` : ` status ${statusCode}`}`)
163
+ err.request = { method: options.method, url, headers: options.headers, body: data }
164
+ if (err.request.headers.authorization)
165
+ err.request.headers.authorization = err.request.headers.authorization.split(' ')[0] + ' ***'
166
+ err.response = { statusCode, statusMessage, headers: res.headers, body }
162
167
  reject(err)
163
168
  } else {
164
169
  resolve(body)
package/srv/service.js CHANGED
@@ -8,7 +8,8 @@ module.exports = class AuditLogService extends Base {
8
8
  this.before('*', req => {
9
9
  const { tenant, user, timestamp } = cds.context
10
10
  req.data.uuid ??= cds.utils.uuid()
11
- req.data.tenant ??= tenant
11
+ // allows to specify undefined tenant in order to log to provider in multi-tenant scenarios
12
+ if (!('tenant' in req.data)) req.data.tenant = tenant
12
13
  req.data.user ??= user.id
13
14
  req.data.time ??= timestamp
14
15
  })