Secure a Multitenant Application with the Authorization and Trust Management Service (XSUAA)
Bind your multitenant application and the approuter application to the xsuaa service instance in SAP BTP, Kyma runtime, which acts as an OAuth 2.0 client to your application.
Step 1Get to know SAP Authorization and Trust Management Service
—
Each multitenant application has to deploy its own application router, and the application router handles requests of all tenants to the application. The application router is able to determine the tenant identifier out of the URL and then forwards the authentication request to the tenant User Account and Authentication (UAA) service and the related identity zone.
Step 2Create XSUAA Instance and Credential with ServiceInstance and ServiceBinding
+
In Kubernetes, you can create and bind to a service instance using the Service Catalog. Create a new deployment file k8s-deployment-services.yaml to define objects for XSUAA instance and binding:
YAML
################### XSUAA ###################---apiVersion:services.cloud.sap.com/v1kind:ServiceInstancemetadata:name:xsuaa-servicespec:serviceOfferingName:xsuaaservicePlanName:applicationparameters:xsappname:multitenant-kyma-demotenant-mode:shared # define the application provider tenant as a shared tenantdescription:Security profile of called applicationscopes:- name:$XSAPPNAME.Callbackdescription:With this scope set, the callbacks for subscribe, unsubscribe and getDependencies can be called.grant-as-authority-to-apps:- $XSAPPNAME(application,sap-provisioning,tenant-onboarding) # provide access to the SAP SaaS Provisioning service SAP Authorization and Trust Management service (technical name: saas-registry) for calling callbacks and getting the dependencies API by granting scopes:oauth2-configuration:redirect-uris:- https://*.<clusterdomain>/** # Replace <clusterdomain> with your actual Kyma cluster domain---apiVersion:services.cloud.sap.com/v1kind:ServiceBindingmetadata:name:xsuaa-service-bindingspec:serviceInstanceName:xsuaa-servicesecretName:xsuaa-service-binding
Upon creation of the binding, the Service Catalog will create a Kubernetes Secret (by default with the same name as the binding) containing credentials, configurations and certificates.
Step 3Access Instance Credential from Approuter and Backend Application
+
SAP’s approuter uses @sap/xsenv package internally to parse and load service keys and secrets bound to the application, this makes the process to load secrets easy.
Kubernetes offers several ways of handling application configurations for bound services and certificates. @sap/xsenv expects that such configurations are handled as Kubernetes Secrets and mounted as files to the pod at a specific path. This path can be provided by the application developer, but the default is /etc/secrets/sapcp. From there, @sap/xsenv assumes that the directory structure is the following /etc/secrets/sapcp/<service-name>/<instance-name>. Here <service-name> and <instance-name> are both directories and the latter contains the credentials/configurations for the service instance as files, where the file name is the name of the configuration/credential and the content is respectively the value.
Now, mount the Secret just generated to the pods of both approuter and node application as a volume in the k8s-deployment-backend.yaml and k8s-deployment-approuter.yaml:
Secrets can be found in the pod directory /etc/secrets/sapcp/<service-name>/<instance-name>:
image-20220117110402061
Step 4Add Authentication and Authorization Logic into Backend Application
+
1. Add libraries for enabling authentication in the kyma-multitenant-node/app.js file:
JavaScript[3-6]
var app = express();
//**************************** Libraries for enabling authentication *****************************
var passport = require('passport');
var xsenv = require('@sap/xsenv');
//************************************************************************************************
2. Enable authorization in the kyma-multitenant-node/app.js file:
The properties name and label both are supported by CF and K8S. You are recommended to use name or label to get xsuaa credentials by. You can find the query value definition on xsenv package page.
If you want to use the label, which maps to Service name, get the XSUAA credentials with: xsenv.getServices({xsuaa: { label: 'xsuaa' }})
If you want to use the name, which maps to Service instance name, get the XSUAA credentials with: xsenv.getServices({xsuaa: { name: 'xsuaa-service' }})
3. Add new route /user in kyma-multitenant-node/routes/index.js
Separate the / route as non-authenticated. Add a new route /user to verify user authentication in the subscribed multitenancy application from the consumer’s side.
JavaScript[12-24]
router.get("/", function(req, res, next) {
try {
var responseMsg = "Welcome to the Kyma Multitenant Application!";
res.send(responseMsg);
} catch (e) {
console.log("AuthInfo object undefined.");
var responseMsg = "Hello World!";
res.send(responseMsg);
}
});
router.get("/user", function(req, res, next) {
try {
var line1 = "Hello " + req.authInfo.getLogonName();
var line2 = "your tenant sub-domain is " + req.authInfo.getSubdomain();
var line3 = "your tenant zone id is " + req.authInfo.getZoneId();
var responseMsg = line1 + "; " + line2 + "; " + line3;
res.send(responseMsg);
} catch (e) {
console.log("AuthInfo object undefined.");
var responseMsg = "Cannot get user information. Please check your authentication.";
res.send(responseMsg);
}
});
4. Add dependencies @sap/xsenv, @sap/xssec and passport in the kyma-multitenant-node/package.json file, for example:
Step 6Configure TENANT_HOST_PATTERN to Determine Identity Zone for Authentication
+
The application router must determine the tenant-specific subdomain for the UAA that in turn determines the identity zone, used for authentication. This determination is done by using a regular expression defined in the environment variable TENANT_HOST_PATTERN.
TENANT_HOST_PATTERN is a string containing a regular expression with a capturing group. The request host is matched against this regular expression. The value of the first capturing group is used as the tenant subdomain. For more details, please visit: Multitenancy.
1. Add a new ConfigMap to provide your Kyma cluster domain in the k8s-deployment-approuter.yaml file:
YAML[7]
---
apiVersion: v1
kind: ConfigMap
metadata:
name: cluster-domain
data:
cluster-domain: <clusterdomain> # Replace <clusterdomain> with your actual Kyma cluster domain
2. Refer to the ConfigMap and add environment variable TENANT_HOST_PATTERN in the k8s-deployment-approuter.yaml file:
Share feedback on this tutorial or join the conversation in SAP Community.
Submit detailed feedbackDiscuss in Community
Steps
Step 1 of 6
1. Get to know SAP Authorization and Trust Management Service2. Create XSUAA Instance and Credential with ServiceInstance and ServiceBinding3. Access Instance Credential from Approuter and Backend Application4. Add Authentication and Authorization Logic into Backend Application5. Enable Authentication for Approuter Application6. Configure TENANT_HOST_PATTERN to Determine Identity Zone for Authentication
Joule
AI Notice
Joule is an AI assistant. Generative AI may produce inaccurate, incomplete, or biased information. Always verify important details before acting on them.
Conversations are sent to SAP-hosted large language models for processing. Do not include personal data, credentials, or confidential information in your messages.
Joule's responses are based on the SAP tutorial catalog and may not reflect the latest product changes. For authoritative guidance, consult the linked tutorials and official SAP documentation.