Skip to content

Commit ba6d109

Browse files
author
Jill Grant
authored
Merge pull request #288162 from majguo/em-6127
Fix guide including using passwordless approach with '--registry-identity' option to pull image from acr in aca
2 parents 0111d40 + 4b66c54 commit ba6d109

File tree

1 file changed

+72
-64
lines changed

1 file changed

+72
-64
lines changed

articles/container-apps/tutorial-java-quarkus-connect-managed-identity-postgresql-database.md

Lines changed: 72 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author: KarlErickson
66
ms.topic: tutorial
77
ms.author: edburns
88
ms.service: azure-container-apps
9-
ms.date: 06/04/2024
9+
ms.date: 10/10/2024
1010
ms.custom: devx-track-azurecli, devx-track-extended-java, devx-track-java, devx-track-javaee, devx-track-javaee-quarkus, passwordless-java, service-connector, devx-track-javaee-quarkus-aca
1111
---
1212

@@ -34,7 +34,6 @@ What you will learn:
3434
* [Java JDK](/azure/developer/java/fundamentals/java-support-on-azure)
3535
* [Maven](https://maven.apache.org)
3636
* [Docker](https://docs.docker.com/get-docker/)
37-
* [GraalVM](https://www.graalvm.org/downloads/)
3837

3938
## 2. Create a container registry
4039

@@ -43,16 +42,25 @@ Create a resource group with the [az group create](/cli/azure/group#az-group-cre
4342
The following example creates a resource group named `myResourceGroup` in the East US Azure region.
4443

4544
```azurecli-interactive
46-
az group create --name myResourceGroup --location eastus
45+
RESOURCE_GROUP="myResourceGroup"
46+
LOCATION="eastus"
47+
48+
az group create --name $RESOURCE_GROUP --location $LOCATION
4749
```
4850

49-
Create an Azure container registry instance using the [az acr create](/cli/azure/acr#az-acr-create) command. The registry name must be unique within Azure, contain 5-50 alphanumeric characters. All letters must be specified in lower case. In the following example, `mycontainerregistry007` is used. Update this to a unique value.
51+
Create an Azure container registry instance using the [az acr create](/cli/azure/acr#az-acr-create) command and retrieve its login server using the [az acr show](/cli/azure/acr#az-acr-show) command. The registry name must be unique within Azure and contain 5-50 alphanumeric characters. All letters must be specified in lower case. In the following example, `mycontainerregistry007` is used. Update this to a unique value.
5052

5153
```azurecli-interactive
54+
REGISTRY_NAME=mycontainerregistry007
5255
az acr create \
53-
--resource-group myResourceGroup \
54-
--name mycontainerregistry007 \
56+
--resource-group $RESOURCE_GROUP \
57+
--name $REGISTRY_NAME \
5558
--sku Basic
59+
60+
REGISTRY_SERVER=$(az acr show \
61+
--name $REGISTRY_NAME \
62+
--query 'loginServer' \
63+
--output tsv | tr -d '\r')
5664
```
5765

5866
## 3. Clone the sample app and prepare the container image
@@ -72,9 +80,9 @@ cd quarkus-quickstarts/hibernate-orm-panache-quickstart
7280

7381
```xml
7482
<dependency>
75-
<groupId>com.azure</groupId>
76-
<artifactId>azure-identity-providers-jdbc-postgresql</artifactId>
77-
<version>1.0.0-beta.1</version>
83+
<groupId>com.azure</groupId>
84+
<artifactId>azure-identity-extensions</artifactId>
85+
<version>1.1.20</version>
7886
</dependency>
7987
```
8088

@@ -85,8 +93,6 @@ cd quarkus-quickstarts/hibernate-orm-panache-quickstart
8593
Delete the existing content in *application.properties* and replace with the following to configure the database for dev, test, and production modes:
8694

8795
```properties
88-
quarkus.package.type=uber-jar
89-
9096
quarkus.hibernate-orm.database.generation=drop-and-create
9197
quarkus.datasource.db-kind=postgresql
9298
quarkus.datasource.jdbc.max-size=8
@@ -95,17 +101,14 @@ cd quarkus-quickstarts/hibernate-orm-panache-quickstart
95101
quarkus.hibernate-orm.sql-load-script=import.sql
96102
quarkus.datasource.jdbc.acquisition-timeout = 10
97103

98-
%dev.quarkus.datasource.username=${AZURE_CLIENT_NAME}
99-
%dev.quarkus.datasource.jdbc.url=jdbc:postgresql://${DBHOST}.postgres.database.azure.com:5432/${DBNAME}?\
100-
authenticationPluginClassName=com.azure.identity.providers.postgresql.AzureIdentityPostgresqlAuthenticationPlugin\
101-
&sslmode=require\
102-
&azure.clientId=${AZURE_CLIENT_ID}\
103-
&azure.clientSecret=${AZURE_CLIENT_SECRET}\
104-
&azure.tenantId=${AZURE_TENANT_ID}
105-
106-
%prod.quarkus.datasource.username=${AZURE_MI_NAME}
107-
%prod.quarkus.datasource.jdbc.url=jdbc:postgresql://${DBHOST}.postgres.database.azure.com:5432/${DBNAME}?\
108-
authenticationPluginClassName=com.azure.identity.providers.postgresql.AzureIdentityPostgresqlAuthenticationPlugin\
104+
%dev.quarkus.datasource.username=${CURRENT_USERNAME}
105+
%dev.quarkus.datasource.jdbc.url=jdbc:postgresql://${AZURE_POSTGRESQL_HOST}:${AZURE_POSTGRESQL_PORT}/${AZURE_POSTGRESQL_DATABASE}?\
106+
authenticationPluginClassName=com.azure.identity.extensions.jdbc.postgresql.AzurePostgresqlAuthenticationPlugin\
107+
&sslmode=require
108+
109+
%prod.quarkus.datasource.username=${AZURE_POSTGRESQL_USERNAME}
110+
%prod.quarkus.datasource.jdbc.url=jdbc:postgresql://${AZURE_POSTGRESQL_HOST}:${AZURE_POSTGRESQL_PORT}/${AZURE_POSTGRESQL_DATABASE}?\
111+
authenticationPluginClassName=com.azure.identity.extensions.jdbc.postgresql.AzurePostgresqlAuthenticationPlugin\
109112
&sslmode=require
110113

111114
%dev.quarkus.class-loading.parent-first-artifacts=com.azure:azure-core::jar,\
@@ -122,8 +125,7 @@ cd quarkus-quickstarts/hibernate-orm-panache-quickstart
122125
io.netty:netty-transport::jar,\
123126
io.netty:netty-buffer::jar,\
124127
com.azure:azure-identity::jar,\
125-
com.azure:azure-identity-providers-core::jar,\
126-
com.azure:azure-identity-providers-jdbc-postgresql::jar,\
128+
com.azure:azure-identity-extensions::jar,\
127129
com.fasterxml.jackson.core:jackson-core::jar,\
128130
com.fasterxml.jackson.core:jackson-annotations::jar,\
129131
com.fasterxml.jackson.core:jackson-databind::jar,\
@@ -140,45 +142,46 @@ cd quarkus-quickstarts/hibernate-orm-panache-quickstart
140142
com.nimbusds:nimbus-jose-jwt::jar,\
141143
net.minidev:json-smart::jar,\
142144
net.minidev:accessors-smart::jar,\
143-
io.netty:netty-transport-native-unix-common::jar
145+
io.netty:netty-transport-native-unix-common::jar,\
146+
net.java.dev.jna:jna::jar
144147
```
145148

146149
### Build and push a Docker image to the container registry
147150

148151
1. Build the container image.
149152

150-
Run the following command to build the Quarkus app image. You must tag it with the fully qualified name of your registry login server. The login server name is in the format *\<registry-name\>.azurecr.io* (must be all lowercase), for example, *mycontainerregistry007.azurecr.io*. Replace the name with your own registry name.
153+
Run the following command to build the Quarkus app image. You must tag it with the fully qualified name of your registry login server.
151154

152155
```bash
153-
mvnw quarkus:add-extension -Dextensions="container-image-jib"
154-
mvnw clean package -Pnative -Dquarkus.native.container-build=true -Dquarkus.container-image.build=true -Dquarkus.container-image.registry=mycontainerregistry007 -Dquarkus.container-image.name=quarkus-postgres-passwordless-app -Dquarkus.container-image.tag=v1
156+
CONTAINER_IMAGE=${REGISTRY_SERVER}/quarkus-postgres-passwordless-app:v1
157+
158+
mvn quarkus:add-extension -Dextensions="container-image-jib"
159+
mvn clean package -Dquarkus.container-image.build=true -Dquarkus.container-image.image=${CONTAINER_IMAGE}
155160
```
156161

157162
1. Log in to the registry.
158163

159-
Before pushing container images, you must log in to the registry. To do so, use the [az acr login][az-acr-login] command. Specify only the registry resource name when signing in with the Azure CLI. Don't use the fully qualified login server name.
164+
Before pushing container images, you must log in to the registry. To do so, use the [az acr login][az-acr-login] command.
160165

161166
```azurecli-interactive
162-
az acr login --name <registry-name>
167+
az acr login --name $REGISTRY_NAME
163168
```
164169

165170
The command returns a `Login Succeeded` message once completed.
166171

167172
1. Push the image to the registry.
168173

169-
Use [docker push][docker-push] to push the image to the registry instance. Replace `mycontainerregistry007` with the login server name of your registry instance. This example creates the `quarkus-postgres-passwordless-app` repository, containing the `quarkus-postgres-passwordless-app:v1` image.
174+
Use [docker push][docker-push] to push the image to the registry instance. This example creates the `quarkus-postgres-passwordless-app` repository, containing the `quarkus-postgres-passwordless-app:v1` image.
170175

171176
```bash
172-
docker push mycontainerregistry007/quarkus-postgres-passwordless-app:v1
177+
docker push $CONTAINER_IMAGE
173178
```
174179

175180
## 4. Create a Container App on Azure
176181

177182
1. Create a Container Apps instance by running the following command. Make sure you replace the value of the environment variables with the actual name and location you want to use.
178183

179184
```azurecli-interactive
180-
RESOURCE_GROUP="myResourceGroup"
181-
LOCATION="eastus"
182185
CONTAINERAPPS_ENVIRONMENT="my-environment"
183186
184187
az containerapp env create \
@@ -187,23 +190,24 @@ cd quarkus-quickstarts/hibernate-orm-panache-quickstart
187190
--location $LOCATION
188191
```
189192

190-
1. Create a container app with your app image by running the following command. Replace the placeholders with your values. To find the container registry admin account details, see [Authenticate with an Azure container registry](/azure/container-registry/container-registry-authentication)
193+
1. Create a container app with your app image by running the following command:
191194

192195
```azurecli-interactive
193-
CONTAINER_IMAGE_NAME=quarkus-postgres-passwordless-app:v1
194-
REGISTRY_SERVER=mycontainerregistry007
195-
REGISTRY_USERNAME=<REGISTRY_USERNAME>
196-
REGISTRY_PASSWORD=<REGISTRY_PASSWORD>
197-
196+
APP_NAME=my-container-app
198197
az containerapp create \
199198
--resource-group $RESOURCE_GROUP \
200-
--name my-container-app \
201-
--image $CONTAINER_IMAGE_NAME \
199+
--name $APP_NAME \
200+
--image $CONTAINER_IMAGE \
202201
--environment $CONTAINERAPPS_ENVIRONMENT \
203202
--registry-server $REGISTRY_SERVER \
204-
--registry-username $REGISTRY_USERNAME \
205-
--registry-password $REGISTRY_PASSWORD
203+
--registry-identity system \
204+
--ingress 'external' \
205+
--target-port 8080 \
206+
--min-replicas 1
206207
```
208+
209+
> [!NOTE]
210+
> The options `--registry-username` and `--registry-password` are still supported but aren't recommended because using the identity system is more secure.
207211
208212
## 5. Create and connect a PostgreSQL database with identity connectivity
209213

@@ -213,65 +217,69 @@ Next, create a PostgreSQL Database and configure your container app to connect t
213217

214218
```azurecli-interactive
215219
DB_SERVER_NAME='msdocs-quarkus-postgres-webapp-db'
216-
ADMIN_USERNAME='demoadmin'
217-
ADMIN_PASSWORD='<admin-password>'
218220
219221
az postgres flexible-server create \
220222
--resource-group $RESOURCE_GROUP \
221223
--name $DB_SERVER_NAME \
222224
--location $LOCATION \
223-
--admin-user $DB_USERNAME \
224-
--admin-password $DB_PASSWORD \
225-
--sku-name GP_Gen5_2
225+
--public-access None \
226+
--sku-name Standard_B1ms \
227+
--tier Burstable \
228+
--active-directory-auth Enabled
226229
```
230+
231+
> [!NOTE]
232+
> The options `--admin-user` and `--admin-password` are still supported but aren't recommended because using the identity system is more secure.
227233
228234
The following parameters are used in the above Azure CLI command:
229235

230-
* *resource-group* &rarr; Use the same resource group name in which you created the web app, for example `msdocs-quarkus-postgres-webapp-rg`.
236+
* *resource-group* &rarr; Use the same resource group name in which you created the web app - for example, `msdocs-quarkus-postgres-webapp-rg`.
231237
* *name* &rarr; The PostgreSQL database server name. This name must be **unique across all Azure** (the server endpoint becomes `https://<name>.postgres.database.azure.com`). Allowed characters are `A`-`Z`, `0`-`9`, and `-`. A good pattern is to use a combination of your company name and server identifier. (`msdocs-quarkus-postgres-webapp-db`)
232-
* *location* &rarr; Use the same location used for the web app.
233-
* *admin-user* &rarr; Username for the administrator account. It can't be `azure_superuser`, `admin`, `administrator`, `root`, `guest`, or `public`. For example, `demoadmin` is okay.
234-
* *admin-password* &rarr; Password of the administrator user. It must contain 8 to 128 characters from three of the following categories: English uppercase letters, English lowercase letters, numbers, and non-alphanumeric characters.
235-
236-
> [!IMPORTANT]
237-
> When creating usernames or passwords **do not** use the `$` character. Later in this tutorial, you will create environment variables with these values where the `$` character has special meaning within the Linux container used to run Java apps.
238-
238+
* *location* &rarr; Use the same location used for the web app. Change to a different location if it doesn't work.
239239
* *public-access* &rarr; `None` which sets the server in public access mode with no firewall rules. Rules will be created in a later step.
240-
* *sku-name* &rarr; The name of the pricing tier and compute configuration, for example `GP_Gen5_2`. For more information, see [Azure Database for PostgreSQL pricing](https://azure.microsoft.com/pricing/details/postgresql/server/).
240+
* *sku-name* &rarr; The name of the pricing tier and compute configuration - for example, `Standard_B1ms`. For more information, see [Azure Database for PostgreSQL pricing](https://azure.microsoft.com/pricing/details/postgresql/server/).
241+
* *tier* &rarr; The compute tier of the server. For more information, see [Azure Database for PostgreSQL pricing](https://azure.microsoft.com/pricing/details/postgresql/server/).
242+
* *active-directory-auth* &rarr; `Enabled` to enable Microsoft Entra authentication.
241243

242244
1. Create a database named `fruits` within the PostgreSQL service with this command:
243245

244246
```azurecli-interactive
247+
DB_NAME=fruits
245248
az postgres flexible-server db create \
246249
--resource-group $RESOURCE_GROUP \
247250
--server-name $DB_SERVER_NAME \
248-
--database-name fruits
251+
--database-name $DB_NAME
249252
```
250253

251254
1. Install the [Service Connector](../service-connector/overview.md) passwordless extension for the Azure CLI:
252255

253256
```azurecli-interactive
254-
az extension add --name serviceconnector-passwordless --upgrade
257+
az extension add --name serviceconnector-passwordless --upgrade --allow-preview true
255258
```
256259

257260
1. Connect the database to the container app with a system-assigned managed identity, using the connection command.
258261

259262
```azurecli-interactive
260263
az containerapp connection create postgres-flexible \
261264
--resource-group $RESOURCE_GROUP \
262-
--name my-container-app \
265+
--name $APP_NAME \
263266
--target-resource-group $RESOURCE_GROUP \
264267
--server $DB_SERVER_NAME \
265-
--database fruits \
266-
--managed-identity
268+
--database $DB_NAME \
269+
--system-identity \
270+
--container $APP_NAME
267271
```
268272

269273
## 6. Review your changes
270274

271275
You can find the application URL(FQDN) by using the following command:
272276

273277
```azurecli-interactive
274-
az containerapp list --resource-group $RESOURCE_GROUP
278+
echo https://$(az containerapp show \
279+
--name $APP_NAME \
280+
--resource-group $RESOURCE_GROUP \
281+
--query properties.configuration.ingress.fqdn \
282+
--output tsv)
275283
```
276284

277285
When the new webpage shows your list of fruits, your app is connecting to the database using the managed identity. You should now be able to edit fruit list as before.

0 commit comments

Comments
 (0)