You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- You have a trial or a productive account for SAP Business Technology Platform (SAP BTP). If you don't have such yet, you can create one so you can [try out services for free] (https://developers.sap.com/tutorials/btp-free-tier-account.html).
20
20
- You have created a subaccount and a space on Cloud Foundry Environment.
21
21
-[cf CLI] (https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/4ef907afb1254e8286882a2bdef0edf4.html) is installed locally.
22
-
-[Node.js] (https://nodejs.org/en/about/releases/) and [npm] (https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) are installed locally. Make sure you have the latest Node.js version. In this tutorial, we use v.18.
22
+
-[Node.js] (https://nodejs.org/en/about/releases/) and [npm] (https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) are installed locally. Make sure you have the latest Node.js version. In this tutorial, we use v.16.
23
23
- You have installed an integrated development environment, for example [Visual Studio Code] (https://code.visualstudio.com/).
24
24
25
25
## Intro
26
-
This tutorial will guide you through creating and setting up a simple Node.js application by using cf CLI. You will start by building and deploying a web application that returns simple data – a **Hello World!** message, and then invoking this app through another one - a web microservice (application router).
26
+
This tutorial will guide you through creating and setting up a simple Node.js application in cf CLI. You will start by building and deploying a web application that returns simple data – a **Hello World!** message, and then invoking this app through a web microservice (application router). Finally, you will set authentication checks and authorization roles to properly access and manage your web application.
27
27
28
28
### Log on to SAP BTP
29
29
First, you need to connect to the SAP BTP, Cloud Foundry environment with your productive subaccount. Your Cloud Foundry URL depends on the region where the API endpoint belongs to. To find out which one is yours, see: [Regions and API Endpoints Available for the CF Environment] (https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/f344a57233d34199b2123b9620d0bb41.html?version=Cloud)
@@ -49,6 +49,9 @@ In this tutorial, we use `eu20.hana.ondemand.com` as an example.
49
49
50
50
51
51
5. Choose the org name and space where you want to create your application.
52
+
53
+
> This step is skipped if you're using a trial account.
54
+
52
55
53
56
54
57
#### RESULT
@@ -127,7 +130,7 @@ You're going to create a simple Node.js application.
127
130
"description": "My simple Node.js app",
128
131
"main": "index.js",
129
132
"engines": {
130
-
"node": "14.x.x"
133
+
"node": "16.x.x"
131
134
},
132
135
"scripts": {
133
136
"start": "node start.js"
@@ -140,7 +143,7 @@ You're going to create a simple Node.js application.
140
143
}
141
144
```
142
145
143
-
8. Inside the `myapp` folder, create another file called `start.js` with the following content:
146
+
8. Inside the `myapp` folder, create a file `start.js` with the following content:
144
147
145
148
```JavaScript
146
149
const express = require('express');
@@ -199,8 +202,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
@@ -380,7 +382,6 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
380
382
381
383
382
384
383
-
384
385
### Run an Authorization Check
385
386
Authorization in the SAP BTP, Cloud Foundry environment is also provided by the XSUAA service. In the previous example, the `@sap/approuter` package was added to provide a central entry point for the business application and to enable authentication. Now to extend the example, authorization will be added through the implementation of a `users` REST service. Different authorization checks will be introduced for the GET and CREATE operations to demonstrate how authorization works. The authorization concept includes elements such as roles, scopes, and attributes provided in the security descriptor file `xs-security.json`. For more information, see: [Application Security Descriptor Configuration Syntax] (https://help.sap.com/docs/BTP/65de2977205c403bbc107264b8eccf4b/517895a9612241259d6941dbf9ad81cb.html?version=Cloud)
386
387
@@ -431,7 +432,7 @@ Authorization in the SAP BTP, Cloud Foundry environment is also provided by the
431
432
cf update-service nodeuaa -c xs-security.json
432
433
```
433
434
434
-
3. In the `myapp` folder, create a new file called `users.json` with the following content:
435
+
3. In the `myapp` folder, create a file `users.json` with the following content:
Copy file name to clipboardExpand all lines: tutorials/btp-cf-buildpacks-python-create/btp-cf-buildpacks-python-create.md
+44-37Lines changed: 44 additions & 37 deletions
Original file line number
Diff line number
Diff line change
@@ -2,39 +2,35 @@
2
2
parser: v2
3
3
author_name: Gergana Tsakova
4
4
author_profile: https://github.com/Joysie
5
+
title: Create a Python Application with Cloud Foundry Python Buildpack
6
+
description: Create a simple application and enable services for it, by using the Cloud Foundry Python Buildpack and Cloud Foundry Command Line Interface (cf CLI).
# Create a Python Application via Cloud Foundry Command Line Interface
12
-
<!-- description --> Create a simple Python application in the Cloud Foundry Command Line Interface (cf CLI) and enable services for it.
13
+
## You will learn
14
+
- How to create a simple "Hello World" application in Python
15
+
- How to consume an SAP BTP service from it
16
+
- How to run authentication and authorization checks via the XSUAA service
13
17
14
18
## Prerequisites
15
-
- You have a productive account for SAP Business Technology Platform (SAP BTP). If you don't have such yet, you can create one so you can [try out services for free] (https://developers.sap.com/tutorials/btp-free-tier-account.html).
19
+
- You have a trial or productive account for SAP Business Technology Platform (SAP BTP). If you don't have such yet, you can create one so you can [try out services for free] (https://developers.sap.com/tutorials/btp-free-tier-account.html).
16
20
- You have created a subaccount and a space on Cloud Foundry Environment.
17
-
- You have created a service instance for [SAP HANA Cloud] (https://help.sap.com/docs/HANA_CLOUD/9ae9104a46f74a6583ce5182e7fb20cb/f7febb16072b41f7ac90abf5ea1d4b86.html) with `hana-free` plan.
18
-
-[Python] (https://www.python.org/downloads/) version 3.6.x or higher is installed locally. In this tutorial, we use Python **3.10.1**.
21
+
- You have created a service instance for [SAP HANA Cloud] (https://help.sap.com/docs/HANA_CLOUD/9ae9104a46f74a6583ce5182e7fb20cb/f7febb16072b41f7ac90abf5ea1d4b86.html). To learn how to set it up, watch the first half of this video: [SAP HANA Cloud Trial Setup](https://www.youtube.com/watch?v=GSNQpfxPuLU)
22
+
-[Python] (https://www.python.org/downloads/) is installed locally. To check which Python versions are supported in the current buildpack, see: [Developing Python in the Cloud Foundry Environment](https://help.sap.com/docs/btp/sap-business-technology-platform/developing-python-in-cloud-foundry-environment#buildpack-versioning). In this tutorial, we use Python **3.11.1**.
19
23
-[cf CLI] (https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/4ef907afb1254e8286882a2bdef0edf4.html) is installed locally.
20
24
-[npm] (https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) is installed locally.
21
25
- You have installed an integrated development environment, for example [Visual Studio Code] (https://code.visualstudio.com/).
22
26
- You have installed the `virtualenv` tool. It creates a folder, which contains all the necessary executables to use the packages that your Python project would need. To install it locally, execute the following command in the Python installation path:
- How to create a simple "Hello World" application in Python
31
-
- How to consume SAP BTP services from it
32
-
- How to run authentication checks for your app
33
-
- How to run authorization checks for your app
34
-
35
-
36
32
## Intro
37
-
This tutorial will guide you through creating and setting up a simple Python application by using cf CLI. You will start by building and deploying a web application that returns simple data – a **Hello World!** message, and then invoking this app through another one - a web microservice (application router).
33
+
This tutorial will guide you through creating and setting up a simple Python application by using cf CLI. You will start by building and deploying a web application that returns simple data – a **Hello World!** message. This simple app will consume an SAP BTP service, and then will be invoked through a web microservice (application router). Finally, you will set authentication checks and authorization roles to properly access and manage your web application.
38
34
39
35
---
40
36
@@ -58,9 +54,15 @@ In this tutorial, we use `eu20.hana.ondemand.com` as an example.
58
54
cf login
59
55
```
60
56
61
-
4. When prompted, enter your user credentials – the email and password you have used to register your productive SAP BTP account.
57
+
4. When prompted, enter your user credentials – the email and password you have used to register your trial or productive SAP BTP account.
58
+
59
+
> **IMPORTANT**: If the authentication fails, even though you've entered correct credentials, try [logging in via single sign-on] (https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/e1009b4aa486462a8951c4d499ce6d4c.html?version=Cloud).
60
+
61
+
62
+
5. Choose the org name and space where you want to create your application.
63
+
64
+
> This step is skipped if you're using a trial account.
62
65
63
-
> **IMPORTANT**: If the authentication fails, even though you've entered correct credentials, try [logging in via single sign-on] (https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/e1009b4aa486462a8951c4d499ce6d4c.html?version=Cloud).
64
66
65
67
66
68
#### RESULT
@@ -102,7 +104,7 @@ You're going to create a simple Python application.
102
104
4. Specify the Python runtime version that your application will run on. To do that, create a `runtime.txt` file with the following content:
103
105
104
106
```TXT
105
-
python-3.10.1
107
+
python-3.11.1
106
108
```
107
109
108
110
5. This application will be a web server utilizing the Flask web framework. To specify Flask as an application dependency, create a `requirements.txt` file with the following content:
@@ -133,7 +135,7 @@ You're going to create a simple Python application.
133
135
cf push
134
136
```
135
137
136
-
> Make sure you always execute `cf push` in the folder where the `manifest.yml` file is located! In this case, that's `python-tutorial`.
138
+
> Make sure you always execute `cf push` in the directory where the `manifest.yml` file is located! In this case, that's `python-tutorial`.
137
139
138
140
8. When the staging and deployment steps are completed, the `myapp` application should be successfully started and its details displayed in the command console.
139
141
@@ -153,13 +155,7 @@ Your Python application is successfully deployed and running on the SAP BTP, Clo
153
155
154
156
You have created a service instance for SAP HANA Cloud (see **Prerequisites** at the beginning). Now you're going to make a connection to your SAP HANA database from SAP HANA Schemas & HDI Containers - a service that runs on the SAP BTP, Cloud Foundry environment - and consume this service in your application.
155
157
156
-
1. Create a `hana` service instance named `pyhana`. As a service plan, you can use `securestore` or `hdi-shared`. They both work for this scenario, so - whichever you prefer (or whichever is available in your subaccount). Execute one of the following commands, respectively:
157
-
158
-
```Bash/Shell
159
-
cf create-service hana securestore pyhana
160
-
```
161
-
162
-
or
158
+
1. Create a `hana` service instance named `pyhana` with service plan `hdi-shared`. Execute:
163
159
164
160
```Bash/Shell
165
161
cf create-service hana hdi-shared pyhana
@@ -258,6 +254,21 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
258
254
}
259
255
```
260
256
257
+
> **NOTE**: For trial accounts, enter the following additional `oauth2-configuration` lines in your `xs-security.json` file:
258
+
259
+
260
+
```JSON
261
+
{
262
+
"xsappname" : "myapp",
263
+
"tenant-mode" : "dedicated",
264
+
"oauth2-configuration": {
265
+
"redirect-uris": [
266
+
"https://*.cfapps.eu20.hana.ondemand.com/**"
267
+
]
268
+
}
269
+
}
270
+
```
271
+
261
272
2. Create an `xsuaa` service instance named `pyuaa` with plan `application`, by executing the following command:
262
273
263
274
```Bash
@@ -299,7 +310,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
299
310
</head>
300
311
<body>
301
312
<h1>Python Tutorial</h1>
302
-
<a href="/myapp/">My Application</a>
313
+
<a href="/myapp/">My Python Application</a>
303
314
</body>
304
315
</html>
305
316
```
@@ -312,7 +323,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
312
323
npm init
313
324
```
314
325
315
-
This will walk you through creating a `package.json` file in the `web` folder. Press **Enter** on every step.
326
+
Press **Enter** on every step. This process will walk you through creating a `package.json` file in the `web` folder.
316
327
317
328
8. Now you need to create a directory `web/node_modules/@sap` and install an `approuter` package in it. To do that, in the `web` directory execute:
318
329
@@ -376,7 +387,7 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
376
387
377
388
> ### What's going on?
378
389
379
-
>As of this point of the tutorial, the URL of the `web` application will be requested instead of the `myapp` URL. It will then forward the requests to the `myapp` application.
390
+
>At this point of the tutorial, the URL of the `web` application will be requested instead of the `myapp` URL. It will then forward the requests to the `myapp` application.
380
391
381
392
382
393
13. When the staging and deployment steps are completed, the `web` application should be successfully started and its details displayed in the command console.
@@ -389,13 +400,12 @@ Authentication in the SAP BTP, Cloud Foundry environment is provided by the Auth
389
400
390
401
#### RESULT
391
402
392
-
A simple application page with title **Python Tutorial** is displayed. When you click the `My Application` link, the current SAP HANA time is displayed, in UTC time zone.
393
-
403
+
- A simple application page with title **Python Tutorial** is displayed. When you click the `My Application` link, the current SAP HANA time is displayed, in UTC time zone.
404
+
405
+
- If you directly access the `myapp` URL, it displays the same result - the current SAP HANA time in UTC time zone.
394
406
395
407
396
408
### Run an Authorization Check
397
-
398
-
399
409
Authorization in the SAP BTP, Cloud Foundry environment is also provided by the XSUAA service. In the previous example, the `@sap/approuter` package was added to provide a central entry point for the business application and to enable authentication. Now to extend the example, authorization will be added.
400
410
401
411
1. Add the `sap-xssec` security library to the `requirements.txt` file, to place restrictions on the content you serve. The file should look like this:
@@ -472,9 +482,6 @@ Authorization in the SAP BTP, Cloud Foundry environment is also provided by the
472
482
473
483
Accessing the `myapp` application results in the following:
474
484
475
-
- If you try to access it directly, a `403 Forbidden` response is displayed due to lack or permissions (lack of authorization header).
485
+
- If you try to access it directly, a `403 Forbidden` response is displayed due to lack of permissions (lack of authorization header). This is a correct and expected behavior.
476
486
477
487
- If you try to access it through the `web` application router, the current SAP HANA time is displayed (in UTC time zone) – provided that you have the `openid` scope assigned to your user. Since the OAuth 2.0 client is used, the `openid` scope is assigned to your user by default, the correct authorization header is declared, and thus you are allowed to access the `myapp` application.
0 commit comments