@cap-js/audit-logging 0.6.0 → 0.8.0

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,6 +4,22 @@ 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.8.0 - 2024-05-24
8
+
9
+ ### Added
10
+
11
+ - Allow to specify undefined tenant in order to log to provider account in multi-tenant scenarios
12
+
13
+ ### Changed
14
+
15
+ - Use kind `audit-log-to-restv2` in profile `hybrid`
16
+
17
+ ## Version 0.7.0 - 2024-05-15
18
+
19
+ ### Added
20
+
21
+ - Automatically promote entities that are associated with data subjects
22
+
7
23
  ## Version 0.6.0 - 2024-02-05
8
24
 
9
25
  ### Added
package/README.md CHANGED
@@ -1,18 +1,125 @@
1
1
  # Welcome to @cap-js/audit-logging
2
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
3
 
4
- ## About this project
5
-
6
4
  `@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
5
 
8
- Documentation can be found at [cap.cloud.sap](https://cap.cloud.sap/docs/guides/data-privacy).
6
+ Documentation can be found at [cap.cloud.sap](https://cap.cloud.sap/docs/guides/data-privacy).
7
+
8
+ > [!IMPORTANT]
9
+ > 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.
10
+
11
+
12
+ ## Preliminaries
13
+
14
+ 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:
15
+
16
+ ```sh
17
+ git clone https://github.com/cap-js/incidents-app
18
+ cd incidents-app
19
+ npm i
20
+ ```
21
+
22
+ ## Setup
23
+ To enable audit logging, simply add this self-configuring plugin package to your project:
24
+
25
+ ```sh
26
+ npm add @cap-js/audit-logging
27
+ ```
28
+
29
+ ## Annotate Personal Data
30
+ Identify entities and elements (potentially) holding personal data using `@PersonalData` annotations. Create a `db/data-privacy.cds` file and add the following:
31
+
32
+ ```cds
33
+ using { sap.capire.incidents as my } from '../db/extensions';
34
+
35
+ annotate my.Customers with @PersonalData : {
36
+ DataSubjectRole : 'Customer',
37
+ EntitySemantics : 'DataSubject'
38
+ } {
39
+ ID @PersonalData.FieldSemantics: 'DataSubjectID';
40
+ firstName @PersonalData.IsPotentiallyPersonal;
41
+ lastName @PersonalData.IsPotentiallyPersonal;
42
+ email @PersonalData.IsPotentiallyPersonal;
43
+ phone @PersonalData.IsPotentiallyPersonal;
44
+ creditCardNo @PersonalData.IsPotentiallySensitive;
45
+ };
9
46
 
10
- ## Requirements
47
+ annotate my.Addresses with @PersonalData: {
48
+ EntitySemantics : 'DataSubjectDetails'
49
+ } {
50
+ customer @PersonalData.FieldSemantics: 'DataSubjectID';
51
+ city @PersonalData.IsPotentiallyPersonal;
52
+ postCode @PersonalData.IsPotentiallyPersonal;
53
+ streetAddress @PersonalData.IsPotentiallyPersonal;
54
+ };
11
55
 
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.
56
+ annotate my.Incidents with @PersonalData : {
57
+ EntitySemantics : 'Other'
58
+ } {
59
+ customer @PersonalData.FieldSemantics: 'DataSubjectID';
60
+ };
61
+
62
+ ```
63
+ Learn more about the annotations in capire:
64
+ - [@PersonalData.EntitySemantics](https://cap.cloud.sap/docs/guides/data-privacy/annotations#entitysemantics)
65
+ - [@PersonalData.EntitySemantics: 'DataSubject'](https://cap.cloud.sap/docs/guides/data-privacy/annotations#datasubjectrole)
66
+ - [@PersonalData.FieldSemantics: 'DataSubjectID'](https://cap.cloud.sap/docs/guides/data-privacy/annotations#fieldsemantics-datasubjectid)
67
+ - [@PersonalData.IsPotentiallyPersonal](https://cap.cloud.sap/docs/guides/data-privacy/annotations#ispotentiallypersonal)
68
+ - [@PersonalData.IsPotentiallySensitive](https://cap.cloud.sap/docs/guides/data-privacy/annotations#ispotentiallysensitive)
69
+
70
+
71
+ ## Test-Drive Locally
72
+ You've prepared everything to log personal data-related events. Let's see that in action.
73
+
74
+ Start the server as usual:
75
+ ```sh
76
+ cds watch
77
+ ```
78
+
79
+ Send an update request that changes personal data:
80
+ ```http
81
+ PATCH http://localhost:4004/admin/Customers(8e2f2640-6866-4dcf-8f4d-3027aa831cad) HTTP/1.1
82
+ Authorization: Basic alice:in-wonderland
83
+ Content-Type: application/json
84
+
85
+ {
86
+ "firstName": "Johnny",
87
+ "lastName": "Doey"
88
+ }
89
+ ```
90
+
91
+ See the audit logs in the server's console output:
92
+ ```sh
93
+ {
94
+ data_subject: {
95
+ type: 'AdminService.Customers',
96
+ id: { ID: '8e2f2640-6866-4dcf-8f4d-3027aa831cad' },
97
+ role: 'Customer',
98
+ },
99
+ object: {
100
+ type: 'AdminService.Customers',
101
+ id: { ID: '8e2f2640-6866-4dcf-8f4d-3027aa831cad' }
102
+ },
103
+ attributes: [
104
+ { name: 'firstName', old: 'John', new: 'Johnny' },
105
+ { name: 'lastName', old: 'Doe', new: 'Doey' }
106
+ ],
107
+ user: 'alice',
108
+ tenant: 't1',
109
+ uuid: '1391A703E2CBE52E817269EC7527368C',
110
+ time: '2023-02-26T08:13:48.287Z'
111
+ }
112
+ ```
113
+
114
+
115
+ ## In Production
13
116
 
14
117
  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
118
 
119
+ [_Learn more about using the SAP Audit Log service._](https://cap.cloud.sap/docs/guides/data-privacy/audit-logging#use-sap-audit-log-service)
120
+
121
+ [_Learn more about custom audit logging._](https://cap.cloud.sap/docs/guides/data-privacy/audit-logging#custom-audit-logging)
122
+
16
123
  ## Support, Feedback, Contributing
17
124
 
18
125
  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).
package/cds-plugin.js CHANGED
@@ -19,6 +19,20 @@ cds.on('served', services => {
19
19
  for (const entity of service.entities) if (hasPersonalData(entity)) relevantEntities.push(entity)
20
20
  if (!relevantEntities.length) continue
21
21
 
22
+ // automatically promote entities that are associated with data subjects
23
+ for (const entity of relevantEntities) {
24
+ if (entity['@PersonalData.EntitySemantics'] !== 'DataSubject') continue
25
+ for (const e of service.entities) {
26
+ for (const k in e.associations) {
27
+ if (e.associations[k].target === entity.name && k !== 'SiblingEntity') {
28
+ e['@PersonalData.EntitySemantics'] ??= 'Other'
29
+ e.associations[k]['@PersonalData.FieldSemantics'] ??= 'DataSubjectID'
30
+ if (!relevantEntities.includes(e)) relevantEntities.push(e)
31
+ }
32
+ }
33
+ }
34
+ }
35
+
22
36
  for (const entity of relevantEntities) {
23
37
  /*
24
38
  * data access
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cap-js/audit-logging",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
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)",
@@ -19,7 +19,7 @@
19
19
  "test-old-db": "npx jest --silent"
20
20
  },
21
21
  "peerDependencies": {
22
- "@sap/cds": "^7"
22
+ "@sap/cds": ">=7 || ^8.0.0-beta"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@cap-js/audit-logging": "file:.",
@@ -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
  }
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
  }
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
  })