Skip to content

Commit 3c516b3

Browse files
Merge pull request #248266 from KarlErickson/xfz11-addpasswordlessjboss-231511
edit "Update Service Connector doc and add new one for passwordless on jboss eap #231511"
2 parents 32ebb0b + 9c7818d commit 3c516b3

File tree

3 files changed

+350
-31
lines changed

3 files changed

+350
-31
lines changed

articles/app-service/tutorial-java-tomcat-connect-managed-identity-postgresql-database.md

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ title: 'Tutorial: Access data with managed identity in Java'
33
description: Secure Azure Database for PostgreSQL connectivity with managed identity from a sample Java Tomcat app, and apply it to other Azure services.
44
ms.devlang: java
55
ms.topic: tutorial
6-
ms.date: 09/26/2022
6+
ms.date: 08/14/2023
77
author: KarlErickson
88
ms.author: karler
99
ms.custom: passwordless-java, service-connector, devx-track-azurecli, devx-track-extended-java
1010
---
1111

1212
# Tutorial: Connect to a PostgreSQL Database from Java Tomcat App Service without secrets using a managed identity
1313

14-
[Azure App Service](overview.md) provides a highly scalable, self-patching web hosting service in Azure. It also provides a [managed identity](overview-managed-identity.md) for your app, which is a turn-key solution for securing access to [Azure Database for PostgreSQL](../postgresql/index.yml) and other Azure services. Managed identities in App Service make your app more secure by eliminating secrets from your app, such as credentials in the environment variables. In this tutorial, you will learn how to:
14+
[Azure App Service](overview.md) provides a highly scalable, self-patching web hosting service in Azure. It also provides a [managed identity](overview-managed-identity.md) for your app, which is a turn-key solution for securing access to [Azure Database for PostgreSQL](../postgresql/index.yml) and other Azure services. Managed identities in App Service make your app more secure by eliminating secrets from your app, such as credentials in the environment variables. In this tutorial, you learn how to:
1515

1616
> [!div class="checklist"]
1717
> * Create a PostgreSQL database.
@@ -39,7 +39,7 @@ cd Passwordless-Connections-for-Java-Apps/Tomcat/
3939

4040
## Create an Azure Database for PostgreSQL
4141

42-
Follow these steps to create an Azure Database for Postgres in your subscription. The Spring Boot app will connect to this database and store its data when running, persisting the application state no matter where you run the application.
42+
Follow these steps to create an Azure Database for Postgres in your subscription. The Spring Boot app connects to this database and store its data when running, persisting the application state no matter where you run the application.
4343

4444
1. Sign into the Azure CLI, and optionally set your subscription if you have more than one connected to your login credentials.
4545

@@ -51,21 +51,21 @@ Follow these steps to create an Azure Database for Postgres in your subscription
5151
1. Create an Azure Resource Group, noting the resource group name.
5252

5353
```azurecli-interactive
54-
RESOURCE_GROUP=<resource-group-name>
55-
LOCATION=eastus
54+
export RESOURCE_GROUP=<resource-group-name>
55+
export LOCATION=eastus
5656
5757
az group create --name $RESOURCE_GROUP --location $LOCATION
5858
```
5959

60-
1. Create an Azure Database for PostgreSQL server. The server is created with an administrator account, but it won't be used because we'll use the Azure Active Directory (Azure AD) admin account to perform administrative tasks.
60+
1. Create an Azure Database for PostgreSQL server. The server is created with an administrator account, but it isn't used because we're going to use the Azure Active Directory (Azure AD) admin account to perform administrative tasks.
6161

6262
### [Flexible Server](#tab/flexible)
6363

6464
```azurecli-interactive
65-
POSTGRESQL_ADMIN_USER=azureuser
65+
export POSTGRESQL_ADMIN_USER=azureuser
6666
# PostgreSQL admin access rights won't be used because Azure AD authentication is leveraged to administer the database.
67-
POSTGRESQL_ADMIN_PASSWORD=<admin-password>
68-
POSTGRESQL_HOST=<postgresql-host-name>
67+
export POSTGRESQL_ADMIN_PASSWORD=<admin-password>
68+
export POSTGRESQL_HOST=<postgresql-host-name>
6969
7070
# Create a PostgreSQL server.
7171
az postgres flexible-server create \
@@ -75,16 +75,16 @@ Follow these steps to create an Azure Database for Postgres in your subscription
7575
--admin-user $POSTGRESQL_ADMIN_USER \
7676
--admin-password $POSTGRESQL_ADMIN_PASSWORD \
7777
--public-access 0.0.0.0 \
78-
--sku-name Standard_D2s_v3
78+
--sku-name Standard_D2s_v3
7979
```
8080

8181
### [Single Server](#tab/single)
8282

8383
```azurecli-interactive
84-
POSTGRESQL_ADMIN_USER=azureuser
84+
export POSTGRESQL_ADMIN_USER=azureuser
8585
# PostgreSQL admin access rights won't be used because Azure AD authentication is leveraged to administer the database.
86-
POSTGRESQL_ADMIN_PASSWORD=<admin-password>
87-
POSTGRESQL_HOST=<postgresql-host-name>
86+
export POSTGRESQL_ADMIN_PASSWORD=<admin-password>
87+
export POSTGRESQL_HOST=<postgresql-host-name>
8888
8989
# Create a PostgreSQL server.
9090
az postgres server create \
@@ -93,16 +93,16 @@ Follow these steps to create an Azure Database for Postgres in your subscription
9393
--location $LOCATION \
9494
--admin-user $POSTGRESQL_ADMIN_USER \
9595
--admin-password $POSTGRESQL_ADMIN_PASSWORD \
96-
--public-network-access 0.0.0.0 \
97-
--sku-name B_Gen5_1
96+
--public-access 0.0.0.0 \
97+
--sku-name B_Gen5_1
9898
```
9999

100100
1. Create a database for the application.
101101

102102
### [Flexible Server](#tab/flexible)
103103

104104
```azurecli-interactive
105-
DATABASE_NAME=checklist
105+
export DATABASE_NAME=checklist
106106
107107
az postgres flexible-server db create \
108108
--resource-group $RESOURCE_GROUP \
@@ -113,7 +113,7 @@ Follow these steps to create an Azure Database for Postgres in your subscription
113113
### [Single Server](#tab/single)
114114

115115
```azurecli-interactive
116-
DATABASE_NAME=checklist
116+
export DATABASE_NAME=checklist
117117
118118
az postgres db create \
119119
--resource-group $RESOURCE_GROUP \
@@ -134,8 +134,8 @@ Follow these steps to build a WAR file and deploy to Azure App Service on Tomcat
134134
1. Create an Azure App Service resource on Linux using Tomcat 9.0.
135135

136136
```azurecli-interactive
137-
APPSERVICE_PLAN=<app-service-plan>
138-
APPSERVICE_NAME=<app-service-name>
137+
export APPSERVICE_PLAN=<app-service-plan>
138+
export APPSERVICE_NAME=<app-service-name>
139139
# Create an App Service plan
140140
az appservice plan create \
141141
--resource-group $RESOURCE_GROUP \
@@ -149,7 +149,7 @@ Follow these steps to build a WAR file and deploy to Azure App Service on Tomcat
149149
--resource-group $RESOURCE_GROUP \
150150
--name $APPSERVICE_NAME \
151151
--plan $APPSERVICE_PLAN \
152-
--runtime "TOMCAT:9.0-jre8"
152+
--runtime "TOMCAT:10.0-java11"
153153
```
154154

155155
1. Deploy the WAR package to App Service.
@@ -176,7 +176,7 @@ Then, connect your app to a Postgres database with a system-assigned managed ide
176176

177177
### [Flexible Server](#tab/flexible)
178178

179-
To do this, run the [az webapp connection create](/cli/azure/webapp/connection/create#az-webapp-connection-create-postgres-flexible) command.
179+
To make this connection, run the [az webapp connection create](/cli/azure/webapp/connection/create#az-webapp-connection-create-postgres-flexible) command.
180180

181181
```azurecli-interactive
182182
az webapp connection create postgres-flexible \
@@ -185,12 +185,13 @@ az webapp connection create postgres-flexible \
185185
--target-resource-group $RESOURCE_GROUP \
186186
--server $POSTGRESQL_HOST \
187187
--database $DATABASE_NAME \
188-
--system-identity
188+
--system-identity \
189+
--client-type java
189190
```
190191

191192
### [Single Server](#tab/single)
192193

193-
To do this, run the [az webapp connection create](/cli/azure/webapp/connection/create#az-webapp-connection-create-postgres) command.
194+
To make this connection, run the [az webapp connection create](/cli/azure/webapp/connection/create#az-webapp-connection-create-postgres) command.
194195

195196
```azurecli-interactive
196197
az webapp connection create postgres \
@@ -199,21 +200,55 @@ az webapp connection create postgres \
199200
--target-resource-group $RESOURCE_GROUP \
200201
--server $POSTGRESQL_HOST \
201202
--database $DATABASE_NAME \
202-
--system-identity
203+
--system-identity \
204+
--client-type java
203205
```
204206

205207
---
206-
This command creates a connection between your web app and your PostgreSQL server, and manages authentication through a system-assigned managed identity.
207208

208-
## View sample web app
209+
This command creates a connection between your web app and your PostgreSQL server, and manages authentication through a system-assigned managed identity.
209210

210-
Run the following command to open the deployed web app in your browser.
211+
Next, update App Settings and add plugin in connection string
211212

212213
```azurecli-interactive
213-
az webapp browse \
214+
export AZURE_POSTGRESQL_CONNECTIONSTRING=$(\
215+
az webapp config appsettings list \
216+
--resource-group $RESOURCE_GROUP \
217+
--name $APPSERVICE_NAME \
218+
| jq -c -r '.[] \
219+
| select ( .name == "AZURE_POSTGRESQL_CONNECTIONSTRING" ) \
220+
| .value')
221+
222+
az webapp config appsettings set \
214223
--resource-group $RESOURCE_GROUP \
215-
--name MyWebapp \
216-
--name $APPSERVICE_NAME
224+
--name $APPSERVICE_NAME \
225+
--settings 'CATALINA_OPTS=-DdbUrl="'"${AZURE_POSTGRESQL_CONNECTIONSTRING}"'&authenticationPluginClassName=com.azure.identity.extensions.jdbc.postgresql.AzurePostgresqlAuthenticationPlugin"'
226+
```
227+
228+
## Test the sample web app
229+
230+
Run the following command to test the application.
231+
232+
```bash
233+
export WEBAPP_URL=$(az webapp show \
234+
--resource-group $RESOURCE_GROUP \
235+
--name $APPSERVICE_NAME \
236+
--query defaultHostName \
237+
--output tsv)
238+
239+
# Create a list
240+
curl -X POST -H "Content-Type: application/json" -d '{"name": "list1","date": "2022-03-21T00:00:00","description": "Sample checklist"}' https://${WEBAPP_URL}/checklist
241+
242+
# Create few items on the list 1
243+
curl -X POST -H "Content-Type: application/json" -d '{"description": "item 1"}' https://${WEBAPP_URL}/checklist/1/item
244+
curl -X POST -H "Content-Type: application/json" -d '{"description": "item 2"}' https://${WEBAPP_URL}/checklist/1/item
245+
curl -X POST -H "Content-Type: application/json" -d '{"description": "item 3"}' https://${WEBAPP_URL}/checklist/1/item
246+
247+
# Get all lists
248+
curl https://${WEBAPP_URL}/checklist
249+
250+
# Get list 1
251+
curl https://${WEBAPP_URL}/checklist/1
217252
```
218253

219254
[!INCLUDE [cli-samples-clean-up](../../includes/cli-samples-clean-up.md)]

articles/service-connector/toc.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ items:
4545
href: ../app-service/tutorial-dotnetcore-sqldb-app.md?bc=%2fazure%2fservice-connector%2fbreadcrumb%2ftoc.json&toc=%2fazure%2fservice-connector%2fTOC.json
4646
- name: Java Tomcat app to PostgreSQL
4747
href: ../app-service/tutorial-java-tomcat-connect-managed-identity-postgresql-database.md?bc=%2fazure%2fservice-connector%2fbreadcrumb%2ftoc.json&toc=%2fazure%2fservice-connector%2fTOC.json
48+
- name: Java JBoss EAP to MySQL
49+
href: tutorial-java-jboss-connect-managed-identity-mysql-database.md
4850
- name: Python app to PostgreSQL
4951
href: tutorial-django-webapp-postgres-cli.md
5052
- name: Azure Container Apps
@@ -137,4 +139,4 @@ items:
137139
- name: Python SDK
138140
href: /python/api/azure-mgmt-servicelinker?toc=/azure/service-connector/TOC.json&bc=/azure/service-connector/breadcrumb/toc.json
139141
- name: Known limitations
140-
href: known-limitations.md
142+
href: known-limitations.md

0 commit comments

Comments
 (0)