Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide terraform scripts for AAD samples #217

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
141ba2a
aad-resource-server-by-filter terraform support
backwind1233 Mar 8, 2022
8a65bea
update scripts to support terraform with aad
backwind1233 Mar 11, 2022
5a93358
- update scripts with random_uuid
backwind1233 Mar 11, 2022
d517201
- remove identifier_uris
backwind1233 Mar 11, 2022
0e11d61
update sh to set identifier_uris
backwind1233 Mar 11, 2022
7324e7a
update scripts
backwind1233 Mar 11, 2022
a6c6dc7
update scripts to run all samples
backwind1233 Mar 11, 2022
69cd458
Merge branch 'spring-cloud-azure_4.0.0-beta.4' into aad_add_support_t…
backwind1233 Mar 14, 2022
b56f726
update terraform scripts for resource filter
backwind1233 Mar 15, 2022
49febc8
update terraform scripts for aad-resource-server
backwind1233 Mar 15, 2022
70e8165
update scripts
backwind1233 Mar 15, 2022
fec7f75
update terraform scripts
backwind1233 Mar 16, 2022
3bbd498
Merge branch 'spring-cloud-azure_4.0.0-beta.4' into aad_add_support_t…
backwind1233 Mar 16, 2022
b5dd154
Merge branch 'spring-cloud-azure_4.0.0-beta.4' into aad_add_support_t…
backwind1233 Mar 16, 2022
74c3f8f
update terraform scripts for random provider
backwind1233 Mar 16, 2022
66ee4a0
update terraform scripts for resource "random_string"
backwind1233 Mar 16, 2022
dd478cf
combine 3 samples within folder aad-web-application-and-resource-server
backwind1233 Mar 16, 2022
be6355a
reformat pom and README.md
backwind1233 Mar 16, 2022
f39a888
update scripts
backwind1233 Mar 17, 2022
8498723
update scripts to each sample
backwind1233 Mar 17, 2022
545e93a
update for login
backwind1233 Mar 17, 2022
9e2022a
update scripts: add random string for aad
backwind1233 Mar 17, 2022
d45e12e
update scripts: add random string for azuread_application
backwind1233 Mar 17, 2022
2489940
unify the username prefix
backwind1233 Mar 17, 2022
e0bb59f
format scripts
backwind1233 Mar 17, 2022
7fe3684
format markdown
backwind1233 Mar 17, 2022
a94d755
format terraform
backwind1233 Mar 17, 2022
ba8f3dd
unify user_principal_name
backwind1233 Mar 17, 2022
aa9c347
add description for terraform
backwind1233 Mar 17, 2022
551f40a
update readme.md
backwind1233 Mar 17, 2022
3bd0d95
update run_all.sh and README.md
backwind1233 Mar 17, 2022
6c742a5
update README.md
backwind1233 Mar 17, 2022
d26cc41
scripts update
backwind1233 Mar 17, 2022
43d80eb
scripts update, small fix
backwind1233 Mar 17, 2022
29ba162
- update terraform scripts
backwind1233 Mar 18, 2022
b7bc326
- update aad-resource-server-by-filter-stateless
backwind1233 Mar 18, 2022
c5e1632
- fix bugs
backwind1233 Mar 18, 2022
4fc5fea
Merge branch 'spring-cloud-azure_4.0.0-beta.4' into aad_add_support_t…
backwind1233 Mar 21, 2022
c724d3c
format markdown
backwind1233 Mar 21, 2022
b76da6d
add an empty line
backwind1233 Mar 21, 2022
b6d7298
add an empty line
backwind1233 Mar 21, 2022
0e43776
add an empty line
backwind1233 Mar 21, 2022
1935559
Merge branch 'spring-cloud-azure_4.0.0-beta.4' into aad_add_support_t…
backwind1233 Mar 21, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 38 additions & 39 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
This demo project explains the usage of the stateless authentication filter `AadAppRoleStatelessAuthenticationFilter`.
This project is composed of a vue.js frontend and a simple backend with three endpoints
* `/public` (accessible by anyone)
* `/authorized` (role "user" required)
* `/authorized` (role "UserRule" required)
* `/admin/demo` (role "admin" required).

## Getting started
Expand Down Expand Up @@ -42,11 +42,11 @@ For the test SPA provided with this example you should create the following role
"allowedMemberTypes": [
"User"
],
"displayName": "User",
"displayName": "UserRule",
"id": "f8ed78b5-fabc-488e-968b-baa48a570001",
"isEnabled": true,
"description": "Normal user access",
"value": "User"
"value": "UserRule"
}
],
```
Expand All @@ -61,7 +61,11 @@ Furthermore enable the implicit flow in the manifest for the demo application
"oauth2AllowImplicitFlow": "true",
```

## Examples
## Running Sample With Terraform
Please refer to [README.md](terraform/README.md) if you want to start the sample with Terraform in just a few steps.

## Running Sample Step by Step

### Configure the sample

#### Configure application.properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public String publicMethod() {

@GetMapping("/authorized")
@ResponseBody
@PreAuthorize("hasRole('ROLE_User')")
@PreAuthorize("hasRole('ROLE_UserRule')")
public String onlyAuthorizedUsers() {
return "authorized endpoint response";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Spring Boot application with Azure Active Directory

## What You Need

- [An Azure subscription](https://azure.microsoft.com/free/)
- [Terraform](https://www.terraform.io/)
- [Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli)
- [JDK8](https://www.oracle.com/java/technologies/downloads/) or later
- Maven
- You can also import the code straight into your IDE:
- [IntelliJ IDEA](https://www.jetbrains.com/idea/download)

## Provision Azure Resources Required to Run This Sample

### Authenticate Using the Azure CLI
Terraform must authenticate to Azure to create infrastructure.

In your terminal, use the Azure CLI tool to setup your account permissions locally.

```shell
az login --tenant [your-tenant] --allow-no-subscriptions
```

Your browser window will open and you will be prompted to enter your Azure login credentials. After successful authentication, your terminal will display your subscription information. You do not need to save this output as it is saved in your system for Terraform to use.

```shell
You have logged in. Now let us find all the subscriptions to which you have access...

[
{
"cloudName": "AzureCloud",
"homeTenantId": "home-Tenant-Id",
"id": "subscription-id",
"isDefault": true,
"managedByTenants": [],
"name": "Subscription-Name",
"state": "Enabled",
"tenantId": "0envbwi39-TenantId",
"user": {
"name": "[email protected]",
"type": "user"
}
}
]
```

### Provision the Resources

After login Azure CLI with your account, now you can use the terraform script to create Azure Resources.

#### Run with Bash

```shell
# In the root directory of aad-resource-server-by-filter-stateless
# Initialize your Terraform configuration
terraform -chdir=./terraform init

# Apply your Terraform Configuration
terraform -chdir=./terraform apply -auto-approve

```

It may take a few minutes to run the script. After successful running, you will see prompt information like below:

```shell
...
Apply complete! Resources: * added, * changed, * destroyed.

```

You can go to [Azure portal](https://ms.portal.azure.com/) in your web browser to check the resources you created.

### Export Output to Your Local Environment
Running the command below to export environment values:

#### Run with Bash

```shell
source ./terraform/setup_env.sh
```
You will see output like below, save this output to use later.
```shell

AZURE_CLIENT_ID=...
AZURE_TENANT_ID=...
--------created user--------
USER_NAME=...
USER_PASSWORD=...

```

## Run Locally

In your current terminal, run `mvn clean spring-boot:run`.

```shell
mvn clean spring-boot:run
```

## Verify This Sample


## Clean Up Resources
After running the sample, if you don't want to run the sample, remember to destroy the Azure resources you created to avoid unnecessary billing.

The terraform destroy command terminates resources managed by your Terraform project.
To destroy the resources you created.

#### Run with Bash

```shell
terraform -chdir=./terraform destroy -auto-approve
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
terraform {
required_providers {
azuread = {
source = "hashicorp/azuread"
version = "2.19.0"
}
random = {
source = "hashicorp/random"
version = "3.1.0"
}
null = {
source = "hashicorp/null"
version = "3.1.0"
}
}
}

resource "random_string" "random" {
length = 5
min_lower = 5
special = false
}

data "azuread_client_config" "current" {}

resource "random_uuid" "role-admin" {
}

resource "random_uuid" "role-user" {
}

# Configure the Azure Active Directory Provider
provider "azuread" {
}

# Configure an app
resource "azuread_application" "resourceserver" {
display_name = "aad-resource-server-by-filter-stateless-${random_string.random.result}"

owners = [data.azuread_client_config.current.object_id]
sign_in_audience = "AzureADMultipleOrgs"

required_resource_access {
resource_app_id = "00000003-0000-0000-c000-000000000000" # Microsoft Graph

resource_access {
id = "df021288-bdef-4463-88db-98f22de89214" # User.Read.All
type = "Role"
}

resource_access {
id = "b4e74841-8e56-480b-be8b-910348b18b4c" # User.ReadWrite
type = "Scope"
}

resource_access {
id = "06da0dbc-49e2-44d2-8312-53f166ab848a" # Directory.Read.All
type = "Scope"
}
}

single_page_application {
redirect_uris = ["http://localhost:8080/"]
}

app_role {
allowed_member_types = ["User"]
description = "Full admin access"
display_name = "Admin"
enabled = true
id = random_uuid.role-admin.result
value = "Admin"
}

app_role {
allowed_member_types = ["User"]
description = "User rule"
display_name = "UserRule"
enabled = true
id = random_uuid.role-user.result
value = "UserRule"
}

web {
implicit_grant {
access_token_issuance_enabled = true
id_token_issuance_enabled = true
}
}
}

resource "azuread_service_principal" "resourceserver" {
application_id = azuread_application.resourceserver.application_id
app_role_assignment_required = false
owners = [data.azuread_client_config.current.object_id]
}

# Retrieve domain information
data "azuread_domains" "current" {
only_initial = true
}

# Create a user
resource "azuread_user" "user" {
user_principal_name = "security-${random_string.random.result}@${data.azuread_domains.current.domains.0.domain_name}"
display_name = "security-${random_string.random.result}"
password = "Azure123456@"
}

resource "azuread_app_role_assignment" "admin" {
app_role_id = random_uuid.role-admin.result
principal_object_id = azuread_user.user.object_id
resource_object_id = azuread_service_principal.resourceserver.object_id
}

resource "azuread_app_role_assignment" "user_role" {
app_role_id = random_uuid.role-user.result
principal_object_id = azuread_user.user.object_id
resource_object_id = azuread_service_principal.resourceserver.object_id
}

resource "null_resource" "set_env" {
depends_on = [azuread_service_principal.resourceserver]

provisioner "local-exec" {
command = "/bin/bash set_identifier_uris.sh"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
output "AZURE_TENANT_ID" {
value = data.azuread_client_config.current.tenant_id
description = "The Azure tenant id."
}

output "AZURE_CLIENT_ID" {
value = azuread_application.resourceserver.application_id
description = "The application id."
}

output "USER_NAME" {
value = azuread_user.user.user_principal_name
description = "The user name of the user created by terraform."
}

output "USER_PASSWORD" {
value = azuread_user.user.password
sensitive = true
description = "The password of the user created by terraform."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
AZURE_CLIENT_ID=$(terraform output -raw AZURE_CLIENT_ID)

# set identifier_uris
echo "----------update identifier-uris start----------"
az ad app update --id $AZURE_CLIENT_ID --identifier-uris api://$AZURE_CLIENT_ID

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, we have plan to remove this. Issue created: Azure/azure-sdk-for-java#27766

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the issue

echo "----------update identifier-uris completed----------"
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export AZURE_TENANT_ID=$(terraform -chdir=./terraform output -raw AZURE_TENANT_ID)
export AZURE_CLIENT_ID=$(terraform -chdir=./terraform output -raw AZURE_CLIENT_ID)
export USER_NAME=$(terraform -chdir=./terraform output -raw USER_NAME)
export USER_PASSWORD=$(terraform -chdir=./terraform output -raw USER_PASSWORD)

echo AZURE_CLIENT_ID=${AZURE_CLIENT_ID}
echo AZURE_TENANT_ID=${AZURE_TENANT_ID}

echo "--------created user--------"
echo USER_NAME=${USER_NAME}
echo USER_PASSWORD=${USER_PASSWORD}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ To run this sample, you'll need:
#### Note
- If you are not the admin, you need consent from your admin for the the `Directory.Read.All` permission. For details see [Directory Permissions](https://docs.microsoft.com/graph/permissions-reference#directory-permissions)

## Examples
## Running Sample With Terraform

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating Azure Resources by Terraform

Please refer to [README.md](terraform/README.md) if you want to start the sample with Terraform in just a few steps.

## Running Sample Step by Step

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating Azure Resources by Azure Portal


### Step 1: Clone or download this repository

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ spring:
user-group:
allowed-group-names: group1,group2
redirect-uri-template: http://localhost:8080/

jwt-connect-timeout: 5000
Loading