Skip to content

Commit fc41f60

Browse files
committed
Sync changes from 'revised-security-guide-ams-nodejs' into revised-security-guide
1 parent 8e08443 commit fc41f60

File tree

1 file changed

+156
-14
lines changed

1 file changed

+156
-14
lines changed

guides/security/cap-users.md

Lines changed: 156 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ The interaction between the CAP application and AMS (via plugin) is as follows:
196196
3. CAP performs the authorization on the basis of the CDS authorization model and the injected user claims.
197197

198198

199-
### Adding AMS Support
199+
### Adding AMS Support { .java }
200200

201201
**AMS is transparent to CAP application code** and can be easily consumed via plugin dependency.
202202

@@ -303,6 +303,49 @@ In general, AMS provides highly flexible APIs to define and enforce authorizatio
303303
**In the context of CAP projects, only a limited subset of these APIs is relevant and is offered in a streamlined way via the CAP integration plugins**.
304304
:::
305305

306+
### Adding AMS Support { .node }
307+
308+
**AMS is transparent to CAP application code** and can be easily consumed via plugin dependency.
309+
310+
To enhance your project with AMS, you can make use of CDS CLI tooling:
311+
312+
```sh
313+
cds add ams
314+
```
315+
316+
This automatically adds required configuration for AMS, taking into account the concrete application context (tenant mode and runtime environment etc.).
317+
If required, it also runs the new `cds add ias` command to configure the project for IAS authentication.
318+
319+
::: details See dependencies added
320+
321+
```json [package.json]
322+
{
323+
"dependencies": [
324+
"@sap/ams": "^3",
325+
"@sap/xssec": "^4"
326+
],
327+
"devDependencies": [
328+
"@sap/ams-dev": "^2"
329+
}
330+
```
331+
:::
332+
333+
`@sap/ams` integrates into the CAP framework to handle incoming requests.
334+
Based on the user's assigned [policies](#policies), the user's roles are determined to decorate the [user.is](/node.js/authentication#user-is) function with additional roles.
335+
The framework then authorizes the request as usual based on the user's roles.
336+
337+
For local development, `@sap/ams-dev` needs to compile the DCL files to Data Control Notation (DCN) files in `gen/dcn` which is the machine-readable version of DCL that is required by AMS at runtime.
338+
339+
Additionally, `@sap/ams` provides multiple build-time features:
340+
341+
- Validate `ams.attributes` annotations for type coherence against the AMS schema.
342+
- Generate policies from the CDS model during the build using a [custom build task](../deployment/custom-builds#custom-build-plugins).
343+
- Generate a deployer application during the build to upload the Data Control Language (DCL) base policies.
344+
345+
::: tip
346+
In general, AMS provides highly flexible APIs to define and enforce authorization rules at runtime suitable for native Cloud applications.
347+
**In the context of CAP projects, only a limited subset of these APIs is relevant and is offered in a streamlined way via the CAP integration plugins**.
348+
:::
306349

307350
### Prepare CDS Model
308351

@@ -406,6 +449,8 @@ AMS policies represent the business-level roles of end users interacting with th
406449
Often, they reflect real-world jobs or functions.
407450
:::
408451

452+
<div class="impl java">
453+
409454
After the application is built, check the *srv/src/main/resources/ams* folder to see the generated AMS *schema* and a *basePolicies* DCL file in a package called *cap*:
410455

411456
::: code-group
@@ -419,6 +464,23 @@ After the application is built, check the *srv/src/main/resources/ams* folder to
419464

420465
:::
421466

467+
</div>
468+
469+
<div class="impl node">
470+
After the application is built, check the *ams/dcl* folder to see the generated AMS *schema* and a *basePolicies* DCL file in a package called *cap*:
471+
472+
::: code-group
473+
474+
``` [./ams]
475+
└─ dcl
476+
├─ cap
477+
│ └─ basePolicies.dcl
478+
└─ schema.dcl
479+
```
480+
481+
:::
482+
</div>
483+
422484
[Learn more about policy generation](https://sap.github.io/cloud-identity-developer-guide/CAP/cds-Plugin.html#dcl-generation){.learn-more}
423485

424486

@@ -490,20 +552,23 @@ cds:
490552
491553
<div class="impl node">
492554
493-
```json
555+
```json [package.json]
494556
{
495557
"cds": {
496558
"requires": {
497559
"auth": {
498560
"[development]": {
499561
"kind": "mocked",
500562
"users": {
501-
"content-manager": {
502-
"policies": ["cap.ContentManager"]
563+
"content-manager": { // [!code ++:5]
564+
"policies": [
565+
"cap.ContentManager"
566+
]
503567
},
504-
"stock-manager": {
505-
"policies": ["cap.StockManager"]
506-
}
568+
"stock-manager": { // [!code ++:5]
569+
"policies": [
570+
"cap.StockManager"
571+
]
507572
}
508573
}
509574
}
@@ -581,19 +646,21 @@ You can verify in the UI that mock user `stock-manager-fiction` is restricted to
581646
<div class="impl node">
582647

583648
::: tip
584-
Don't miss to add the policy files in sub folders of `ams` reflecting the namespace properly: Policy `local.StockManagerFiction` is expected to be in a file within directory `/ams/dcl/local/`.
649+
Don't miss to add the policy files in sub folders of `ams/dcl` reflecting the namespace properly: Policy `local.StockManagerFiction` is expected to be in a file within directory `./ams/dcl/local/`.
585650
:::
586651

587-
```json
652+
```json [package.json]
588653
{
589654
"cds": {
590655
"requires": {
591656
"auth": {
592657
"[development]": {
593658
"kind": "mocked",
594659
"users": {
595-
"stock-manager-fiction": {
596-
"policies": ["local.StockManagerFiction"]
660+
"stock-manager-fiction": { // [!code ++:5]
661+
"policies": [
662+
"local.StockManagerFiction"
663+
]
597664
}
598665
}
599666
}
@@ -616,13 +683,15 @@ Policies can be automatically deployed to the AMS server during deployment of th
616683

617684
Enhancing the project with by `cds add ams` automatically adds task e.g. in the MTA for AMS policy deyployment.
618685

686+
<div class="impl java">
687+
619688
::: details AMS policy deployer task in the MTA
620689

621690
::: code-group
622691
```yaml [mta.yaml- deployer task]
623692
- name: bookshop-ams-policies-deployer
624693
type: javascript.nodejs
625-
path: srv/src/gen/policies
694+
path: srv/src/gen/policies # Node.js: gen/policies
626695
parameters:
627696
buildpack: nodejs_buildpack
628697
no-route: true
@@ -658,6 +727,54 @@ Note that the policy deployer task requires a path to a directory structure cont
658727
By default, the path points to `srv/src/gen/policies` which is prepared automatically during build step with the appropriate policy-content copied from `srv/src/main/resources/ams`.
659728
In addition, `@sap/ams` needs to be referenced to add the deployer logic.
660729

730+
</div>
731+
732+
<div class="impl node">
733+
734+
:: details AMS policy deployer task in the MTA
735+
736+
::: code-group
737+
```yaml [mta.yaml- deployer task]
738+
- name: bookshop-ams-policies-deployer
739+
type: javascript.nodejs
740+
path: gen/policies
741+
parameters:
742+
buildpack: nodejs_buildpack
743+
no-route: true
744+
no-start: true
745+
tasks:
746+
- name: deploy-dcl
747+
command: npm start
748+
memory: 512M
749+
requires:
750+
- name: bookshop-ias
751+
[...]
752+
```
753+
754+
755+
```json [srv/src/gen/policies/package.json - deyployer module]
756+
{
757+
"name": "ams-dcl-content-deployer",
758+
"version": "3.0.0",
759+
"dependencies": {
760+
"@sap/ams": "^3"
761+
},
762+
[...]
763+
"scripts": {
764+
"start": "npx --package=@sap/ams deploy-dcl"
765+
}
766+
}
767+
```
768+
769+
:::
770+
771+
772+
Note that the policy deployer task requires a path to a directory structure containing the `ams/dcl` root folder with the policies to be deployed.
773+
By default, the path points to `gen/policies` which is prepared automatically during build step with the appropriate policy-content copied from `ams/dcl`.
774+
In addition, `@sap/ams` needs to be referenced to add the deployer logic.
775+
776+
<div>
777+
661778
::: tip
662779
Several microservices sharing the same IAS instance need a common folder structure the deployer task operates on.
663780
It contains the common view of policies applied to all services.
@@ -1546,10 +1663,35 @@ During development, it might be useful to activate logger `com.sap.cds.security.
15461663
}
15471664
```
15481665

1549-
This makes the runtime tracing user information of authenticated users to the application log like this:
1666+
You can verify a valid configfuration of the AMS plugin by the following log output:
15501667

15511668
```sh
1552-
[basic] - authenticated: { user: 'alice', tenant: 'some-tenant', features: [ 'some-feature' ] }
1669+
[ams] - AMS Plugin loaded.
1670+
[ams] - Added AMS middleware after 'auth' middleware.
1671+
```
1672+
1673+
... and find more information about the policy evaluation at request time:
1674+
1675+
```sh
1676+
[ams] - Determined potential actions for resource '$SCOPES': stock-manager {
1677+
potentialActions: Set(1) { 'stock-manager' },
1678+
policies: [ 'local.StockManagerFiction' ],
1679+
...
1680+
}
1681+
[ams] - AMS user roles added to user.is: [ 'stock-manager' ]
1682+
[ams] - Privilege check for 'stock-manager' on '$SCOPES' was conditional. {
1683+
result: 'conditional',
1684+
dcn: "$app.genre IN ['Fantasy', 'Mystery']",
1685+
policies: [ 'local.StockManagerFiction' ],
1686+
...
1687+
}
1688+
[ams] - Resulting privileges for READ on AdminService.Books : [
1689+
{
1690+
grant: 'READ',
1691+
to: [ 'stock-manager' ],
1692+
where: "genre.name IN ('Fantasy', 'Mystery')"
1693+
}
1694+
]
15531695
```
15541696

15551697
</div>

0 commit comments

Comments
 (0)