Skip to content

Commit 0e11068

Browse files
committed
fix guide including using passwordless approach with '--registry-identity' option to pull image from acr in aca
Signed-off-by: Jianguo Ma <[email protected]>
1 parent e03aea6 commit 0e11068

File tree

1 file changed

+65
-63
lines changed

1 file changed

+65
-63
lines changed

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

Lines changed: 65 additions & 63 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, 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,22 +190,20 @@ 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
```
207208

208209
## 5. Create and connect a PostgreSQL database with identity connectivity
@@ -213,65 +214,66 @@ Next, create a PostgreSQL Database and configure your container app to connect t
213214

214215
```azurecli-interactive
215216
DB_SERVER_NAME='msdocs-quarkus-postgres-webapp-db'
216-
ADMIN_USERNAME='demoadmin'
217-
ADMIN_PASSWORD='<admin-password>'
218217
219218
az postgres flexible-server create \
220219
--resource-group $RESOURCE_GROUP \
221220
--name $DB_SERVER_NAME \
222221
--location $LOCATION \
223-
--admin-user $DB_USERNAME \
224-
--admin-password $DB_PASSWORD \
225-
--sku-name GP_Gen5_2
222+
--public-access None \
223+
--sku-name Standard_B1ms \
224+
--tier Burstable \
225+
--active-directory-auth Enabled
226226
```
227227

228228
The following parameters are used in the above Azure CLI command:
229229

230230
* *resource-group* &rarr; Use the same resource group name in which you created the web app, for example `msdocs-quarkus-postgres-webapp-rg`.
231231
* *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-
232+
* *location* &rarr; Use the same location used for the web app. Change to a different location if it doesn't work.
239233
* *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/).
234+
* *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/).
235+
* *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/).
236+
* *active-directory-auth* &rarr; `Enabled` to enable Microsoft Entra authentication.
241237

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

244240
```azurecli-interactive
241+
DB_NAME=fruits
245242
az postgres flexible-server db create \
246243
--resource-group $RESOURCE_GROUP \
247244
--server-name $DB_SERVER_NAME \
248-
--database-name fruits
245+
--database-name $DB_NAME
249246
```
250247

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

253250
```azurecli-interactive
254-
az extension add --name serviceconnector-passwordless --upgrade
251+
az extension add --name serviceconnector-passwordless --upgrade --allow-preview true
255252
```
256253

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

259256
```azurecli-interactive
260257
az containerapp connection create postgres-flexible \
261258
--resource-group $RESOURCE_GROUP \
262-
--name my-container-app \
259+
--name $APP_NAME \
263260
--target-resource-group $RESOURCE_GROUP \
264261
--server $DB_SERVER_NAME \
265-
--database fruits \
266-
--managed-identity
262+
--database $DB_NAME \
263+
--system-identity \
264+
--container $APP_NAME
267265
```
268266

269267
## 6. Review your changes
270268

271269
You can find the application URL(FQDN) by using the following command:
272270

273271
```azurecli-interactive
274-
az containerapp list --resource-group $RESOURCE_GROUP
272+
echo https://$(az containerapp show \
273+
--name $APP_NAME \
274+
--resource-group $RESOURCE_GROUP \
275+
--query properties.configuration.ingress.fqdn \
276+
-o tsv)
275277
```
276278

277279
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)