-
Notifications
You must be signed in to change notification settings - Fork 231
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
backwind1233
merged 43 commits into
Azure-Samples:spring-cloud-azure_4.0.0-beta.4
from
backwind1233:aad_add_support_terraform
Mar 21, 2022
Merged
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 8a65bea
update scripts to support terraform with aad
backwind1233 5a93358
- update scripts with random_uuid
backwind1233 d517201
- remove identifier_uris
backwind1233 0e11d61
update sh to set identifier_uris
backwind1233 7324e7a
update scripts
backwind1233 a6c6dc7
update scripts to run all samples
backwind1233 69cd458
Merge branch 'spring-cloud-azure_4.0.0-beta.4' into aad_add_support_t…
backwind1233 b56f726
update terraform scripts for resource filter
backwind1233 49febc8
update terraform scripts for aad-resource-server
backwind1233 70e8165
update scripts
backwind1233 fec7f75
update terraform scripts
backwind1233 3bbd498
Merge branch 'spring-cloud-azure_4.0.0-beta.4' into aad_add_support_t…
backwind1233 b5dd154
Merge branch 'spring-cloud-azure_4.0.0-beta.4' into aad_add_support_t…
backwind1233 74c3f8f
update terraform scripts for random provider
backwind1233 66ee4a0
update terraform scripts for resource "random_string"
backwind1233 dd478cf
combine 3 samples within folder aad-web-application-and-resource-server
backwind1233 be6355a
reformat pom and README.md
backwind1233 f39a888
update scripts
backwind1233 8498723
update scripts to each sample
backwind1233 545e93a
update for login
backwind1233 9e2022a
update scripts: add random string for aad
backwind1233 d45e12e
update scripts: add random string for azuread_application
backwind1233 2489940
unify the username prefix
backwind1233 e0bb59f
format scripts
backwind1233 7fe3684
format markdown
backwind1233 a94d755
format terraform
backwind1233 ba8f3dd
unify user_principal_name
backwind1233 aa9c347
add description for terraform
backwind1233 551f40a
update readme.md
backwind1233 3bd0d95
update run_all.sh and README.md
backwind1233 6c742a5
update README.md
backwind1233 d26cc41
scripts update
backwind1233 43d80eb
scripts update, small fix
backwind1233 29ba162
- update terraform scripts
backwind1233 b7bc326
- update aad-resource-server-by-filter-stateless
backwind1233 c5e1632
- fix bugs
backwind1233 4fc5fea
Merge branch 'spring-cloud-azure_4.0.0-beta.4' into aad_add_support_t…
backwind1233 c724d3c
format markdown
backwind1233 b76da6d
add an empty line
backwind1233 b6d7298
add an empty line
backwind1233 0e43776
add an empty line
backwind1233 1935559
Merge branch 'spring-cloud-azure_4.0.0-beta.4' into aad_add_support_t…
backwind1233 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...er-active-directory/aad-resource-server-by-filter-stateless/terraform/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
128 changes: 128 additions & 0 deletions
128
...-azure-starter-active-directory/aad-resource-server-by-filter-stateless/terraform/main.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ure-starter-active-directory/aad-resource-server-by-filter-stateless/terraform/outputs.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
} |
6 changes: 6 additions & 0 deletions
6
...active-directory/aad-resource-server-by-filter-stateless/terraform/set_identifier_uris.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
echo "----------update identifier-uris completed----------" |
11 changes: 11 additions & 0 deletions
11
...e-starter-active-directory/aad-resource-server-by-filter-stateless/terraform/setup_env.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
### Step 1: Clone or download this repository | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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