Skip to content

Commit 7ae34f8

Browse files
authored
Feature/bicep (#4)
1 parent 9d75f26 commit 7ae34f8

33 files changed

+927
-26
lines changed

.devcontainer/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"version": "9.0"
1010
},
1111
"ghcr.io/devcontainers/features/azure-cli:latest": {
12-
"extensions": "account,containerapp,deploy-to-azure,functionapp,staticwebapp,subscription,webapp"
12+
"extensions": "account,authV2,containerapp,deploy-to-azure,functionapp,staticwebapp,subscription,webapp"
1313
},
1414
"ghcr.io/devcontainers/features/github-cli:latest": {},
1515
"ghcr.io/devcontainers/features/docker-in-docker": {},

Dockerfile.containerapp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ COPY ./src/EasyAuth.Components /source/EasyAuth.Components
77

88
WORKDIR /source/EasyAuth.ContainerApp
99

10-
RUN dotnet publish -c Release -o /app
10+
ARG TARGETARCH=amd64
11+
12+
RUN dotnet publish -a ${TARGETARCH/amd64/x64} -c Release -o /app
1113

1214
FROM mcr.microsoft.com/dotnet/aspnet:9.0-alpine AS final
1315

README.md

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,97 @@
1-
# azure-easyauth-sample
2-
This provides a sample code for the EasyAuth feature on Azure App Service, Azure Static Web App and Azure Container Apps
1+
# Azure EasyAuth Sample
2+
3+
This provides sample [Blazor](https://learn.microsoft.com/aspnet/core/blazor/) apps for the EasyAuth feature on Azure App Service, Azure Container Apps and Azure Static Web App.
4+
5+
## Prerequisites
6+
7+
- [.NET 9+ SDK](https://dotnet.microsoft.com/download/dotnet/9.0)
8+
- [Visual Studio 2022](https://visualstudio.microsoft.com/vs/) or [Visual Studio Code](https://code.visualstudio.com/) with [C# Dev Kit](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csdevkit)
9+
- [GitHub CLI](https://github.com/cli/cli#installation)
10+
- [Azure CLI](https://learn.microsoft.com/cli/azure/install-azure-cli)
11+
- [Azure Developer CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/install-azd)
12+
- [SWA CLI](https://learn.microsoft.com/azure/static-web-apps/static-web-apps-cli-install)
13+
14+
## Getting Started
15+
16+
1. Clone this repository.
17+
18+
```bash
19+
gh repo fork devkimchi/azure-easyauth-sample --clone
20+
```
21+
22+
1. Change the directory to the repository.
23+
24+
```bash
25+
cd azure-easyauth-sample
26+
```
27+
28+
1. Build the sample apps.
29+
30+
```bash
31+
dotnet restore && dotnet build
32+
```
33+
34+
1. Create artifacts for each app
35+
36+
```bash
37+
dotnet publish -c Release
38+
```
39+
40+
1. Login to Azure.
41+
42+
```bash
43+
# Login to Azure with Azure Developer CLI
44+
azd auth login
45+
46+
# Login to Azure with Azure CLI
47+
az login
48+
```
49+
50+
1. Run the following command to provision and deploy the Azure resources.
51+
52+
```bash
53+
azd up
54+
```
55+
56+
It will ask you to provide the following parameters:
57+
58+
- New environment name
59+
- Azure subscription to use
60+
- Azure location to provision resources
61+
- `sttappLocation` to provision Azure Static Web App
62+
63+
It provisions Azure Container Apps, Azure App Service, and Azure Static Web App instances and deploys the sample apps to each of them.
64+
65+
1. Get each app's URL.
66+
67+
```bash
68+
# Web App URL
69+
azd env get-value AZURE_RESOURCE_EASYAUTH_WEBAPP_URL
70+
71+
# Container App URL
72+
azd env get-value AZURE_RESOURCE_EASYAUTH_CONTAINERAPP_URL
73+
74+
# Static Web App URL
75+
azd env get-value AZURE_RESOURCE_EASYAUTH_STATICAPP_URL
76+
```
77+
78+
1. With the URLs above, navigate each app with your browser. You'll be redirected to login your apps with Entra ID first. Once you logged in, you'll see the logged-in user's information.
79+
80+
## Known Limitations of Azure EasyAuth
81+
82+
Azure EasyAuth is supposed to protect your entire app, not for specific pages. Therefore, if you want to protect certain pages of your app, you have to implement the authentication/authorisation logic by yourself.
83+
84+
## Clean Up
85+
86+
If you want to clean up the resources provisioned by the `azd up` command, run the following command:
87+
88+
```bash
89+
azd down --force --purge
90+
```
91+
92+
## Additional Resources
93+
94+
- [Azure EasyAuth](https://learn.microsoft.com/azure/app-service/scenario-secure-app-authentication-app-service?tabs=workforce-configuration)
95+
- [Azure App Service](https://learn.microsoft.com/azure/app-service/overview)
96+
- [Azure Container Apps](https://learn.microsoft.com/azure/container-apps/overview)
97+
- [Azure Static Web Apps](https://learn.microsoft.com/azure/static-web-apps/overview)

azure.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# yaml-language-server: $schema=https://raw.githubusercontent.com/Azure/azure-dev/main/schemas/v1.0/azure.yaml.json
22

33
name: azure-easyauth-sample
4+
45
metadata:
56
template: azd-init@1.11.0
7+
68
services:
79
easyauth-containerapp:
810
project: src/EasyAuth.ContainerApp
@@ -20,3 +22,49 @@ services:
2022
project: src/EasyAuth.WebApp
2123
host: appservice
2224
language: dotnet
25+
26+
hooks:
27+
preup:
28+
posix:
29+
shell: sh
30+
continueOnError: false
31+
interactive: true
32+
run: infra/hooks/preup.sh
33+
windows:
34+
shell: pwsh
35+
continueOnError: false
36+
interactive: true
37+
run: infra/hooks/preup.ps1
38+
preprovision:
39+
posix:
40+
shell: sh
41+
continueOnError: false
42+
interactive: true
43+
run: infra/hooks/preprovision.sh
44+
windows:
45+
shell: pwsh
46+
continueOnError: false
47+
interactive: true
48+
run: infra/hooks/preprovision.ps1
49+
postprovision:
50+
posix:
51+
shell: sh
52+
continueOnError: false
53+
interactive: true
54+
run: infra/hooks/postprovision.sh
55+
windows:
56+
shell: pwsh
57+
continueOnError: false
58+
interactive: true
59+
run: infra/hooks/postprovision.ps1
60+
postdeploy:
61+
posix:
62+
shell: sh
63+
continueOnError: false
64+
interactive: true
65+
run: infra/hooks/postdeploy.sh
66+
windows:
67+
shell: pwsh
68+
continueOnError: false
69+
interactive: true
70+
run: infra/hooks/postdeploy.ps1

infra/hooks/deploy_swa.ps1

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Runs the deploy_swa script
2+
# It does the following:
3+
# 1. Loads the azd environment variables
4+
# 2. Logs in to the Azure CLI if not running in a GitHub Action
5+
# 3. Build SWA app
6+
# 4. Deploy SWA app
7+
8+
# $REPOSITORY_ROOT = git rev-parse --show-toplevel
9+
$REPOSITORY_ROOT = "$(Split-Path $MyInvocation.MyCommand.Path)/../.."
10+
11+
# Load the azd environment variables
12+
& "$REPOSITORY_ROOT/infra/hooks/load_azd_env.ps1" -ShowMessage
13+
14+
if ([string]::IsNullOrEmpty($env:GITHUB_WORKSPACE)) {
15+
# The GITHUB_WORKSPACE is not set, meaning this is not running in a GitHub Action
16+
& "$REPOSITORY_ROOT/infra/hooks/login.ps1"
17+
}
18+
19+
$AZURE_ENV_NAME = $env:AZURE_ENV_NAME
20+
21+
# Run only if GITHUB_WORKSPACE is NOT set - this is NOT running in a GitHub Action workflow
22+
if ([string]::IsNullOrEmpty($env:GITHUB_WORKSPACE)) {
23+
Write-Host "Deploying to Azure Static Web Apps..."
24+
25+
$RESOURCE_GROUP = "rg-$env:AZURE_ENV_NAME"
26+
$STATICAPP_NAME = $env:AZURE_RESOURCE_EASYAUTH_STATICAPP_NAME
27+
28+
# Build SWA app
29+
swa build
30+
31+
# Get deployment token
32+
$deploymentToken = az staticwebapp secrets list `
33+
--resource-group $RESOURCE_GROUP `
34+
--name $STATICAPP_NAME `
35+
--query "properties.apiKey" -o tsv
36+
37+
# Deploy SWA app
38+
swa deploy `
39+
--api-location src/EasyAuth.FunctionApp/bin/Release/net9.0 `
40+
--env Production `
41+
-d $deploymentToken
42+
43+
Write-Host "...Done"
44+
} else {
45+
Write-Host "Skipping to deploy the application Azure Static Web Apps..."
46+
}

infra/hooks/load_azd_env.ps1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Loads the azd .env file into the current environment
2+
# It does the following:
3+
# 1. Loads the azd .env file from the current environment
4+
5+
Param(
6+
[switch]
7+
[Parameter(Mandatory=$false)]
8+
$ShowMessage
9+
)
10+
11+
if ($ShowMessage) {
12+
Write-Host "Loading azd .env file from current environment" -ForegroundColor Cyan
13+
}
14+
15+
foreach ($line in (& azd env get-values)) {
16+
if ($line -match "([^=]+)=(.*)") {
17+
$key = $matches[1]
18+
$value = $matches[2] -replace '^"|"$'
19+
[Environment]::SetEnvironmentVariable($key, $value)
20+
}
21+
}

infra/hooks/load_azd_env.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Loads the azd .env file into the current environment
2+
# It does the following:
3+
# 1. Loads the azd .env file from the current environment
4+
5+
Param(
6+
[switch]
7+
[Parameter(Mandatory=$false)]
8+
$ShowMessage
9+
)
10+
11+
if ($ShowMessage) {
12+
Write-Host "Loading azd .env file from current environment" -ForegroundColor Cyan
13+
}
14+
15+
foreach ($line in (& azd env get-values)) {
16+
if ($line -match "([^=]+)=(.*)") {
17+
$key = $matches[1]
18+
$value = $matches[2] -replace '^"|"$'
19+
[Environment]::SetEnvironmentVariable($key, $value)
20+
}
21+
}

infra/hooks/login.ps1

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Logs in to Azure through AZD and AZ CLI
2+
# It does the following:
3+
# 1. Checks if the user is logged in to Azure
4+
# 2. Logs in to Azure Developer CLI if the user is not logged in
5+
# 3. Logs in to Azure CLI if the user is not logged in
6+
# 4. Sets the active subscription if the user is logged in
7+
# 5. Prompts the user to select a subscription if the subscription is not set
8+
# 6. Sets the active subscription to the selected subscription
9+
# 7. Exits if the subscription is not found
10+
11+
# $REPOSITORY_ROOT = git rev-parse --show-toplevel
12+
$REPOSITORY_ROOT = "$(Split-Path $MyInvocation.MyCommand.Path)/../.."
13+
14+
# Load the azd environment variables
15+
& "$REPOSITORY_ROOT/infra/hooks/load_azd_env.ps1"
16+
17+
# AZD LOGIN
18+
# Check if the user is logged in to Azure
19+
$login_status = azd auth login --check-status
20+
21+
# Check if the user is not logged in
22+
if ($login_status -like "*Not logged in*") {
23+
Write-Host "Not logged in, initiating login process..."
24+
# Command to log in to Azure
25+
azd auth login
26+
}
27+
28+
# AZ LOGIN
29+
$EXPIRED_TOKEN = az ad signed-in-user show --query 'id' -o tsv 2>$null
30+
31+
if ([string]::IsNullOrEmpty($EXPIRED_TOKEN)) {
32+
az login --scope https://graph.microsoft.com/.default -o none
33+
}
34+
35+
if ([string]::IsNullOrEmpty($env:AZURE_SUBSCRIPTION_ID)) {
36+
$ACCOUNT = az account show --query '[id,name]'
37+
Write-Host "You can set the 'AZURE_SUBSCRIPTION_ID' environment variable with 'azd env set AZURE_SUBSCRIPTION_ID'."
38+
Write-Host $ACCOUNT
39+
40+
$response = Read-Host "Do you want to use the above subscription? (Y/n) "
41+
$response = if ([string]::IsNullOrEmpty($response)) { "Y" } else { $response }
42+
switch ($response) {
43+
{ $_ -match "^[yY](es)?$" } {
44+
# Do nothing
45+
break
46+
}
47+
default {
48+
Write-Host "Listing available subscriptions..."
49+
$SUBSCRIPTIONS = az account list --query 'sort_by([], &name)' --output json
50+
Write-Host "Available subscriptions:"
51+
Write-Host ($SUBSCRIPTIONS | ConvertFrom-Json | ForEach-Object { "{0} {1}" -f $_.name, $_.id } | Format-Table)
52+
$subscription_input = Read-Host "Enter the name or ID of the subscription you want to use: "
53+
$AZURE_SUBSCRIPTION_ID = ($SUBSCRIPTIONS | ConvertFrom-Json | Where-Object { $_.name -eq $subscription_input -or $_.id -eq $subscription_input } | Select-Object -exp id)
54+
if (-not [string]::IsNullOrEmpty($AZURE_SUBSCRIPTION_ID)) {
55+
Write-Host "Setting active subscription to: $AZURE_SUBSCRIPTION_ID"
56+
az account set -s $AZURE_SUBSCRIPTION_ID
57+
}
58+
else {
59+
Write-Host "Subscription not found. Please enter a valid subscription name or ID."
60+
exit 1
61+
}
62+
break
63+
}
64+
}
65+
}
66+
else {
67+
az account set -s $env:AZURE_SUBSCRIPTION_ID
68+
}

0 commit comments

Comments
 (0)