Skip to content

Commit 50b2bf8

Browse files
authored
Live ACI managed identity test (#22652)
1 parent cd77088 commit 50b2bf8

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

sdk/azidentity/managed_identity_credential_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/http"
1313
"net/url"
1414
"os"
15+
"os/exec"
1516
"path/filepath"
1617
"strings"
1718
"testing"
@@ -158,6 +159,27 @@ func TestManagedIdentityCredential_AzureArcErrors(t *testing.T) {
158159
})
159160
}
160161

162+
func TestManagedIdentityCredential_AzureContainerInstanceLive(t *testing.T) {
163+
// This test triggers the managed identity test app deployed to an Azure Container Instance.
164+
// See the bicep file and test resources scripts for details.
165+
// It triggers the app with az because the test subscription prohibits opening ports to the internet.
166+
name := os.Getenv("AZIDENTITY_ACI_NAME")
167+
rg := os.Getenv("AZIDENTITY_RESOURCE_GROUP")
168+
if name == "" || rg == "" {
169+
t.Skip("set AZIDENTITY_ACI_NAME and AZIDENTITY_RESOURCE_GROUP to run this test")
170+
}
171+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
172+
defer cancel()
173+
command := fmt.Sprintf("az container exec -g %s -n %s --exec-command 'wget -qO- localhost'", rg, name)
174+
// using "script" as a workaround for "az container exec" requiring a tty
175+
// https://github.com/Azure/azure-cli/issues/17530
176+
cmd := exec.CommandContext(ctx, "script", "-q", "-O", "/dev/null", "-c", command)
177+
b, err := cmd.CombinedOutput()
178+
s := string(b)
179+
require.NoError(t, err, s)
180+
require.Equal(t, "test passed", s)
181+
}
182+
161183
func TestManagedIdentityCredential_AzureFunctionsLive(t *testing.T) {
162184
// This test triggers the managed identity test app deployed to Azure Functions.
163185
// See the bicep file and test resources scripts for details.

sdk/azidentity/test-resources-post.ps1

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ docker push $image
4444

4545
$rg = $DeploymentOutputs['AZIDENTITY_RESOURCE_GROUP']
4646

47+
# ACI is easier to provision here than in the bicep file because the image isn't available before now
48+
Write-Host "Deploying Azure Container Instance"
49+
$aciName = "azidentity-test"
50+
az container create -g $rg -n $aciName --image $image `
51+
--acr-identity $($DeploymentOutputs['AZIDENTITY_USER_ASSIGNED_IDENTITY']) `
52+
--assign-identity [system] $($DeploymentOutputs['AZIDENTITY_USER_ASSIGNED_IDENTITY']) `
53+
--role "Storage Blob Data Reader" `
54+
--scope $($DeploymentOutputs['AZIDENTITY_STORAGE_ID']) `
55+
-e AZIDENTITY_STORAGE_NAME=$($DeploymentOutputs['AZIDENTITY_STORAGE_NAME']) `
56+
AZIDENTITY_STORAGE_NAME_USER_ASSIGNED=$($DeploymentOutputs['AZIDENTITY_STORAGE_NAME_USER_ASSIGNED']) `
57+
AZIDENTITY_USER_ASSIGNED_IDENTITY=$($DeploymentOutputs['AZIDENTITY_USER_ASSIGNED_IDENTITY']) `
58+
FUNCTIONS_CUSTOMHANDLER_PORT=80
59+
Write-Host "##vso[task.setvariable variable=AZIDENTITY_ACI_NAME;]$aciName"
60+
4761
# Azure Functions deployment: copy the Windows binary from the Docker image, deploy it in a zip
4862
Write-Host "Deploying to Azure Functions"
4963
$container = docker create $image
@@ -96,7 +110,3 @@ spec:
96110
"@
97111
kubectl apply -f "$PSScriptRoot/k8s.yaml"
98112
Write-Host "##vso[task.setvariable variable=AZIDENTITY_POD_NAME;]$podName"
99-
100-
if ($CI) {
101-
az logout
102-
}

sdk/azidentity/test-resources.bicep

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ param sshPubKey string = ''
1818
param location string = resourceGroup().location
1919

2020
// https://learn.microsoft.com/azure/role-based-access-control/built-in-roles
21-
var blobContributor = subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'ba92f5b4-2d11-453d-a403-e96b0029c9fe') // Storage Blob Data Contributor
21+
var acrPull = subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '7f951dda-4ed3-4680-a7ca-43fe172d538d')
22+
var blobReader = subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '2a2b9908-6ea1-4ae2-8e65-a410df84e7d1')
2223

2324
resource sa 'Microsoft.Storage/storageAccounts@2021-08-01' = if (deployResources) {
2425
kind: 'StorageV2'
@@ -49,21 +50,31 @@ resource usermgdid 'Microsoft.ManagedIdentity/userAssignedIdentities@2018-11-30'
4950
name: baseName
5051
}
5152

53+
resource acrPullContainerInstance 'Microsoft.Authorization/roleAssignments@2022-04-01' = if (deployResources) {
54+
name: guid(resourceGroup().id, acrPull, 'containerInstance')
55+
properties: {
56+
principalId: deployResources ? usermgdid.properties.principalId : ''
57+
principalType: 'ServicePrincipal'
58+
roleDefinitionId: acrPull
59+
}
60+
scope: containerRegistry
61+
}
62+
5263
resource blobRoleUserAssigned 'Microsoft.Authorization/roleAssignments@2022-04-01' = if (deployResources) {
5364
scope: saUserAssigned
54-
name: guid(resourceGroup().id, blobContributor, usermgdid.id)
65+
name: guid(resourceGroup().id, blobReader, usermgdid.id)
5566
properties: {
5667
principalId: deployResources ? usermgdid.properties.principalId : ''
5768
principalType: 'ServicePrincipal'
58-
roleDefinitionId: blobContributor
69+
roleDefinitionId: blobReader
5970
}
6071
}
6172

6273
resource blobRoleFunc 'Microsoft.Authorization/roleAssignments@2022-04-01' = if (deployResources) {
63-
name: guid(resourceGroup().id, blobContributor, 'azfunc')
74+
name: guid(resourceGroup().id, blobReader, 'azfunc')
6475
properties: {
6576
principalId: deployResources ? azfunc.identity.principalId : ''
66-
roleDefinitionId: blobContributor
77+
roleDefinitionId: blobReader
6778
principalType: 'ServicePrincipal'
6879
}
6980
scope: sa
@@ -200,6 +211,7 @@ output AZIDENTITY_ACR_LOGIN_SERVER string = deployResources ? containerRegistry.
200211
output AZIDENTITY_ACR_NAME string = deployResources ? containerRegistry.name : ''
201212
output AZIDENTITY_AKS_NAME string = deployResources ? aks.name : ''
202213
output AZIDENTITY_FUNCTION_NAME string = deployResources ? azfunc.name : ''
214+
output AZIDENTITY_STORAGE_ID string = deployResources ? sa.id : ''
203215
output AZIDENTITY_STORAGE_NAME string = deployResources ? sa.name : ''
204216
output AZIDENTITY_STORAGE_NAME_USER_ASSIGNED string = deployResources ? saUserAssigned.name : ''
205217
output AZIDENTITY_USER_ASSIGNED_IDENTITY string = deployResources ? usermgdid.id : ''

sdk/azidentity/workload_identity_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,13 @@ func TestWorkloadIdentityCredential_Live(t *testing.T) {
5555
if pod == "" {
5656
t.Skip("set AZIDENTITY_POD_NAME to run this test")
5757
}
58-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
58+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
5959
defer cancel()
6060
cmd := exec.CommandContext(ctx, "kubectl", "exec", pod, "--", "wget", "-qO-", "localhost")
6161
b, err := cmd.CombinedOutput()
62-
require.NoError(t, err)
63-
require.EqualValues(t, "test passed", b)
62+
s := string(b)
63+
require.NoError(t, err, s)
64+
require.Equal(t, "test passed", s)
6465
}
6566

6667
func TestWorkloadIdentityCredential_Recorded(t *testing.T) {

0 commit comments

Comments
 (0)