Security in SAP BTP
Security in SAP BTP
15 90 20,809
Introduction
This blog series is mainly targeted for developers and administrators. If you are someone who has gone through the plethora
of tutorials, documentation and presentations on security topics in SAP BTP and still lacks the confidence to implement
security for your application, you have come to the right place.
How to protect an app in Cloud Foundry environment of SAP BTP from unauthorized access
How to implement role-based features
How SAP Identity Provider and Single-sign-on works
For ease of reading, I have split this in multiple blogs, which are:
For a comprehensive learning path, you may also check How to Become Expert in SAP BTP Security– A Complete Learning
Journey
Note: If you are new to SAP BTP and looking for a simple explanation of what it is and what problem it solves,
see Explaining SAP Business Technology Platform (SAP BTP) to a Beginner
Let’s be honest that most of the developers don’t design the application keeping security in mind at first place. We always try
to prove the feasibility of use-case first. Once we succeed, then only we think about securing the application. One of the
reasons, why people get on back foot when it comes to security implementation.
In SAP BTP, it’s super easy to develop a full-stack business application using sophisticated frameworks like SAP Cloud
Application Programming Model, SAP Cloud SDK and many other out-of-the-box cloud services. With little bit effort on
understanding the core concepts, you can also solve all the pieces of puzzle in security.
In this blog series, we will make it extremely simple. First, we will understand the basic concepts:
1. First deploy an Unsecured Hello World Node.js application in BTP Cloud Foundry.
2. Then step-by-step we will implement authentication and authorization.
3. We will also touch upon Identity Provider to be used for single-sign-on.
Note: This blog series is focused on SAP BTP, Cloud Foundry environment. I may skip mentioning Cloud Foundry every
time.
In SAP BTP, there are 2 options for Identity Provider – SAP ID Service and SAP Cloud Identity Authentication service
(IAS).
SAP ID Service
SAP ID Service is the default identity provider in SAP BTP. It is a pre-configured, standard SAP public IdP
(account.sap.com) that is shared by all customers.
You can view the SAP ID service pre-configured in BTP subaccounts in the cockpit, as shown below.
SAP Cloud Identity Authentication service (IAS)
For many customers, business users might be stored in corporate identity providers. SAP recommends using SAP Cloud
Identity Services – Identity Authentication Service (IAS) as a hub.
We can connect IAS as a single custom identity provider to SAP BTP. Further use IAS to integrate with corporate identity
providers.
IAS can be configured in SAP BTP cockpit, in the Trust Configuration section, as shown below.
To know more on establishing trust between SAP BTP and IAS, you may refer to the help document.
When these customers build applications on BTP, an important question comes up – “How can employees authenticate to the
applications with known credentials?”
In simple words, customer needs to provide single sign-on for their custom solution on BTP, SAP S/4HANA Cloud, SAP
SuccessFactors and other SAP solutions. The answer to this is SAP Identity Authentication Service.
As shown in the above image, IAS can either act as an IdP itself or delegate the authentication to a corporate identity
provider. IAS acts a central hub to provide single-sign-on to all SAP cloud applications as well as BTP applications.
What is XSUAA?
SAP XSUAA is an internal development of SAP.
In Cloud Foundry, there is an open-source component called UAA. UAA is an OAuth provider which takes care of
authentication and authorization. SAP took the base of UAA and extended it with SAP specific features to be used in SAP
BTP.
If you are new to OAuth, refer to this blog to know more about it:
Below image shows very high-level architecture of XSUAA. It’s important to note that, XSUAA does NOT store users data.
This is why the XSUAA needs to trust an external Identity Provider (IdP). It can establish trust either with SAP ID
Service or a Corporate Identity Provider via SAP Identity Authentication Service (IAS).
Application Router
When a business application consists of several different apps (microservices), the application router is used to provide
a single-entry-point to the business application.
1. User request for the resource from Application. The App Router takes incoming.
2. Since user is not authenticated, App Router initiates an OAuth2 flow with the XSUAA.
3. XSUAA forwards the request to Identity Provider to enforce the business user to authenticate.
4. IdP prompts the user to authenticate himself. For Example, by entering username and password.
5. User authenticates himself.
6. If the authentication was successful, Identity Provider sends a SAML token to user (web browser). The web browser
sends this new SAML token to the XSUAA for authentication.
7. XSUAA consider this request as authenticated and generates an OAuth Token which is technically a JWT token.
8. The App Router enriches each subsequent request with the JWT, before the request is routed to a dedicated
application. The application verify the JWT token and send the requested resource to user.
This blog series is mainly targeted for developers and administrators. If you are someone who has gone through the plethora
of tutorials, documentation, and presentations on security topics in SAP BTP and still lacks the confidence to implement
security for your application, you have come to the right place.
How to protect an app in SAP BTP, Cloud Foundry environment from unauthorized access
How to implement role-based features
How SAP Identity Provider and Single-sign-on works
For ease of reading, I have split this in multiple blogs, which are:
We know that we should never ever share our passwords. But there are use cases which require us to authorize a website to
use the service of another. For example, you may need to tell Facebook that it’s ok for ESPN.com to access your profile or
post updates to your timeline. Instead of asking for your Facebook password, ESPN can use a protocol called OAuth.
What is OAuth?
OAuth is an open standard for applications and websites to handle authorization.
OAuth doesn’t share password data but instead uses authorization tokens to prove an identity between consumers and
service providers. It is an authentication protocol that allows you to approve one application interacting with another on your
behalf without giving away your password.
OAuth:
In this way, OAuth mitigates some of the common concerns with authorization scenarios
Note that OAuth is about authorization and not authentication. Authorization is asking for permission to do stuff.
Authentication is about proving you are the correct person because you know things.
OAuth uses API calls extensively, which is why mobile applications, modern web applications, game consoles, and
Internet of Things (IoT) devices find OAuth a better experience for the user.
SAML, on the other hand, uses a session cookie in a browser that allows a user to access certain web pages – great for
short-lived work days, but not so great when have to log into your smart watch every day.
Resource Server: The server hosting the protected resources (e. g. Facebook, Twitter)
Resource Owner: User who owns the data in the resource server. For example, a User is the Resource Owner of his
Facebook profile.
Client: The application that wants to access your data (e. g. ESPN.com)
Authorization Server: The main engine of OAuth. It allows Resource Owner to delegate his authorization to Client.
User (Resource Owner): “Hey, ESPN, I would like you to be able to post directly to my Facebook page.”
ESPN (Client): “Authorization Server, I have a user that would like me to post to his page. Can I have a Request Token?”
ESPN sends the request to Authorization Server with desired scopes. Scope is nothing but individual tasks (e.g. access friend
list, post on page etc.)
Authorization Server: “Sure. But let me get a confirmation from the User.”
Authorization Server sends a consent dialog to User, saying “do you authorize ESPN to do X, Y and Z with your Facebook
account?
Step 4 – User Confirms the Authorization Server request
ESPN: “Authorization Server, can I exchange this Request Token for an Access Token ()?”
ESPN: “Facebook, I’d like to post this link to User’s page. Here’s my Access Token”
Facebook: “Done!”
You watch a video on YouTube, and you want to share that on your Facebook wall. Here:
Nowadays, OAuth 2.0 is the most widely used form of OAuth. OAuth 2.0 is the one used in SAP BTP.
JWT Token
We learnt that OAuth call flows uses Access Token and Request Token. Usually, these tokens are JSON Web Tokens (JWT).
JWT (pronounced “jot”) is an open standard that defines a compact and self-contained way for securely transmitting
information between parties.
JWT is widely used in OAuth for securely transmit user information and access rights. You can check how XSUAA acts as
the central administrator creating dedicated & tailored JWTs for our application and app router.
Body/payload: Might be anything. For example, information about the user, the issuer and all the scopes.
First of all, since JSON is less verbose than XML, JWT Tokens are smaller in size, making JWT more compact than SAML.
This makes JWT a good choice to be passed over network.
JSON parsers are extremely common in most programming languages because they map directly to objects. Conversely,
XML doesn’t have a natural document-to-object mapping. This makes it easier to work with JWT than SAML assertions.
A simple real-life analogy of JWT token is – access card (or key) you use every day to enter your office. Like your access
card has your user and access rights information, the JWT Token also contains the information about user e.g. id, email,
address, access rights etc.
JWT token is cryptographically signed by the XSUAA, which means others cannot alter the user information of a token.
However, if they somehow get access to the token, they can cause a lot of harm as they can access all the data the user has
access to. That’s the reason, JWT tokens are usually only be passed between applications and servers and never exposed to
end user. This is also the reason, why App Router takes care of attaching JWT token to request instead of sending JWT token
to browser.
I hope you got the basic idea of OAuth and JWT Token.
Fundamentals of Security in BTP: Implement
Authentication and Authorization in a Node.js App
22 29 7,507
This blog series is mainly targeted for developers and administrators. If you are someone who has gone through the plethora
of tutorials, documentation, and presentations on security topics in SAP BTP and still lacks the confidence to implement
security for your application, you have come to the right place.
How to protect an app in SAP BTP, Cloud Foundry environment from unauthorized access
How to implement role-based features
How SAP Identity Provider and Single-sign-on works
For ease of reading, I have split this in multiple blogs, which are:
Note: If you are new to SAP BTP and looking for a simple explanation of what it is and what problem it solves,
see Explaining SAP Business Technology Platform (SAP BTP) to a Beginner
We will first deploy an unsecured Node.js Hello World in BTP, Cloud Foundry
Then we will implement authentication in the application
Finally, we will add role based features for authorization
Note: Although, you can use any other IDE or command line interface to develop and deploy the Node.js app, we will
recommend you use SAP Business Application Studio.
If you are new to SAP Business Application Studio, you may go through this tutorial to learn how to use it.
1. Open Business Application Studio. Go to Terminal -> New Terminal and run below command.
git init
git clone https://github.com/rajagupta20/unsecured-nodejs-app.git
2. Click on Open Folder and select the application folder (unsecured-nodejs-app). Check below files.
Result
You just deployed a Hello World Node.js application to BTP. You should be able to access the application without any
authentication required.
To make it simple, I have saved completed project in GitHub. Let’s clone that by running below commands:
git init
package.json
xs-app.json
The destination (myapp) and App Router (approuter1) is specified in manifest.yml file as shown below.
Note: In later blogs, we will look into xs-app.json file in detail.
The xs-security.json file uses JSON notation to define the security options for an application. The information in this file is
used at application-deployment time, for example, to create required roles for your application.
In this example, we are making it extremely simple, by just specifying xsappname and tenant-mode.
xsappname property specifies the name of the application that the security description applies to.
tenant-mode can be “dedicated” for single-tenant application and “shared” for multitenant application
1. Go to BTP Cockpit -> Subaccount -> Space -> Instance -> Create as shown below
2. Use command line tool. Execute below command
3. In case of Multitarget Applications (MTA), use configurations specified in yml file, which automatically creates
XSUAA instances and bind it with applications. This is the most preferred way, and we will look into it later.
To make sure we understand what’s happening behind the scenes, let’s use command approach. Executing below command
to create the XSUAA instance.
Execute cf push command to deploy the application. It will deploy both app router and application. You can check them in
the BTP cockpit.
1. Scope
2. Attribute
3. Role-Collection
{
"xsappname": "myapp2",
"tenant-mode": "dedicated",
"scopes": [
{
"name": "$XSAPPNAME.Display",
"description": "Display Users"
},
{
"name": "$XSAPPNAME.Update",
"description": "Update Users"
}
],
"role-templates": [
{
"name": "Viewer",
"description": "View Users",
"scope-references": [
"$XSAPPNAME.Display"
]
},
{
"name": "Manager",
"description": "Maintain Users",
"scope-references": [
"$XSAPPNAME.Display",
"$XSAPPNAME.Update"
]
}
]
}
Scope
Scopes are functional authorizations that are assigned to users by means of security roles.
These scopes can be used for functional authorization checks. For example, in the application, we may check the scope
using checkScope() function as shown below.
Attribute
We can use attributes to perform more granular level checks. For example, a manager may have authorization to update
employee data but only for a specific country.
In xs-security.json file we create the attribute (say country) and value of the country is assigned at runtime.
Role Templates
A role template combines the scopes and attributes. It is the description of roles (for example, “manager” or “employee”) to
apply to a user.
Role
Role is created based on Role Template at runtime. You can check the generated artefacts in the BTP Cockpit as shown
below.
Assignment of Roles and Role Collection to Users
In BTP, there is something called Role Collection which helps to bind the Roles to Users.
Role Collection
Role Collections contain one or more roles. Further role collections are assigned to users by administrator.
the design time (Role Template, Attribute & Scope) and run time artifacts (Role) of XSUAA. Role
Roles are combined in Role Collection
Role Collections is assigned to users
XSUAA instance is also bound to the application
Below image shows the tasks/responsibilities of developer and administrator in this context.
We will:
To make it simple, I have saved completed project in GitHub. Let’s clone that by running below commands:
git init
in xs-security.json file
{
"xsappname": "myapp2",
"tenant-mode": "dedicated",
"scopes": [
{
"name": "$XSAPPNAME.Display",
"description": "Display Users"
},
{
"name": "$XSAPPNAME.Update",
"description": "Update Users"
}
],
"role-templates": [
{
"name": "Viewer",
"description": "View Users",
"scope-references": [
"$XSAPPNAME.Display"
]
},
{
"name": "Manager",
"description": "Maintain Users",
"scope-references": [
"$XSAPPNAME.Display",
"$XSAPPNAME.Update"
]
}
]
}
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.authenticate('JWT', { session: false }));
res.status(201).json(newUser);
});
Note, that XSUAA instance name is nodeuaa2, the same is bound to application in manifest.yml file.
---
applications:
- name: myapp-secured-demo2
routes:
- route: node-12345671-3.cfapps.eu10.hana.ondemand.com
path: myapp
memory: 128M
buildpack: nodejs_buildpack
services:
- nodeuaa2
- name: approuter2
routes:
- route: approuter1-12345671-3.cfapps.eu10.hana.ondemand.com
path: approuter
memory: 128M
env:
destinations: >
[
{
"name":"myapp",
"url":"https://node-12345671-3.cfapps.eu10.hana.ondemand.com",
"forwardAuthToken": true
}
]
services:
- nodeuaa2
Select the role collection and add Manager and Viewer roles from your application. Also add your user (or any user you want
to give access) to it.
Similarly, you may create another Role Collection called Viewer and only assign Viewer role to it.