Skip to content

Commit 35753d3

Browse files
authored
[Monitor] Use federated auth for live tests (#36497)
Signed-off-by: Paul Van Eck <[email protected]>
1 parent ed86370 commit 35753d3

File tree

6 files changed

+52
-21
lines changed

6 files changed

+52
-21
lines changed

sdk/monitor/azure-monitor-query/tests/conftest.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,4 @@ def monitor_info(environment_variables):
4949
"workspace_id": environment_variables.get(ENV_WORKSPACE_ID),
5050
"secondary_workspace_id": environment_variables.get(ENV_SECONDARY_WORKSPACE_ID),
5151
"table_name": environment_variables.get(ENV_TABLE_NAME),
52-
"client_id": environment_variables.get(ENV_CLIENT_ID),
53-
"tenant_id": environment_variables.get(ENV_TENANT_ID)
5452
}

sdk/monitor/azure-monitor-query/tests/test_exceptions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ def test_logs_resource_query_partial_exception(self, recorded_test, monitor_info
5656

5757
def test_logs_batch_query_fatal_exception(self, recorded_test, monitor_info):
5858
credential = ClientSecretCredential(
59-
client_id = monitor_info['client_id'],
60-
client_secret = 'bad_secret',
61-
tenant_id = monitor_info['tenant_id']
59+
client_id = "00000000-0000-0000-0000-000000000000",
60+
client_secret = "bad_secret",
61+
tenant_id = "00000000-0000-0000-0000-000000000000"
6262
)
6363
client = self.get_client(LogsQueryClient, credential)
6464
requests = [

sdk/monitor/azure-monitor-query/tests/test_exceptions_async.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ async def test_logs_resource_query_partial_exception(self, recorded_test, monito
6565
@pytest.mark.asyncio
6666
async def test_logs_batch_query_fatal_exception(self, recorded_test, monitor_info):
6767
credential = ClientSecretCredential(
68-
client_id = monitor_info['client_id'],
69-
client_secret = 'bad_secret',
70-
tenant_id = monitor_info['tenant_id']
68+
client_id = "00000000-0000-0000-0000-000000000000",
69+
client_secret = "bad_secret",
70+
tenant_id = "00000000-0000-0000-0000-000000000000"
7171
)
7272
client = self.get_client(LogsQueryClient, credential)
7373
async with client:

sdk/monitor/test-resources-post.ps1

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@
44
# IMPORTANT: Do not invoke this file directly. Please instead run eng/New-TestResources.ps1 from the repository root.
55

66
param (
7-
[hashtable] $DeploymentOutputs
7+
[hashtable] $DeploymentOutputs,
8+
9+
[Parameter()]
10+
[ValidatePattern('^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$')]
11+
[string] $TestApplicationId,
12+
13+
[Parameter()]
14+
[ValidatePattern('^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$')]
15+
[string] $SubscriptionId,
16+
17+
[Parameter(ValueFromRemainingArguments = $true)]
18+
$RemoveTestResourcesRemainingArguments
819
)
920

1021
# Outputs from the Bicep deployment passed in from New-TestResources
11-
$tenantId = $DeploymentOutputs['MONITOR_TENANT_ID']
12-
$clientId = $DeploymentOutputs['MONITOR_CLIENT_ID']
13-
$clientSecret = $DeploymentOutputs['MONITOR_CLIENT_SECRET']
22+
$tenantId = $DeploymentOutputs['AZURE_MONITOR_TENANT_ID']
1423
$dcrImmutableId = $DeploymentOutputs['AZURE_MONITOR_DCR_ID']
1524
$dceEndpoint = $DeploymentOutputs['AZURE_MONITOR_DCE']
1625
$streamName = $DeploymentOutputs['AZURE_MONITOR_STREAM_NAME']
1726
$environment = $DeploymentOutputs['MONITOR_ENVIRONMENT']
18-
$authorityHost = $DeploymentOutputs['AZURE_AUTHORITY_HOST']
1927

2028
##################
2129
### Step 0: Wait for role assignment to propagate
@@ -35,11 +43,16 @@ $audienceMappings = @{
3543

3644
$audience = $audienceMappings[$environment]
3745

38-
$scope= [System.Web.HttpUtility]::UrlEncode("$audience/.default")
39-
$body = "client_id=$clientId&scope=$scope&client_secret=$clientSecret&grant_type=client_credentials";
40-
$headers = @{"Content-Type"="application/x-www-form-urlencoded"};
41-
$uri = "$authorityHost/$tenantId/oauth2/v2.0/token"
42-
$bearerToken = (Invoke-RestMethod -Uri $uri -Method "Post" -Body $body -Headers $headers).access_token
46+
az cloud set --name $environment
47+
48+
if ($CI) {
49+
az login --service-principal -u $TestApplicationId --tenant $tenantId --allow-no-subscriptions --federated-token $env:ARM_OIDC_TOKEN
50+
} else {
51+
az login
52+
}
53+
az account set --subscription $SubscriptionId
54+
55+
$bearerToken = az account get-access-token --output json --resource $audience | ConvertFrom-Json | Select-Object -ExpandProperty accessToken
4356

4457
##################
4558
### Step 2: Load up some sample data.

sdk/monitor/test-resources.bicep

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ param baseName string = resourceGroup().name
44
@description('Which Azure Region to deploy the resource to. Defaults to the resource group location.')
55
param location string = resourceGroup().location
66

7+
@description('The tenant ID')
8+
param tenantId string = ''
9+
710
@description('The principal to assign the role to. This is application object id.')
811
param testApplicationOid string
912

@@ -238,3 +241,4 @@ output AZURE_MONITOR_DCE string = dataCollectionEndpoint.properties.logsIngestio
238241
output AZURE_MONITOR_DCR_ID string = dataCollectionRule.properties.immutableId
239242
output AZURE_MONITOR_STREAM_NAME string = streamName
240243
output AZURE_MONITOR_TABLE_NAME string = tableName
244+
output AZURE_MONITOR_TENANT_ID string = tenantId

sdk/monitor/tests.yml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
1+
# cSpell:ignore pscore
2+
# cSpell:ignore issecret
13
trigger: none
24

35
extends:
46
template: /eng/pipelines/templates/stages/archetype-sdk-tests.yml
57
parameters:
8+
PreSteps:
9+
- task: AzureCLI@2
10+
displayName: Set OIDC variables
11+
inputs:
12+
azureSubscription: azure-sdk-tests
13+
scriptType: pscore
14+
scriptLocation: inlineScript
15+
addSpnToEnvironment: true
16+
inlineScript: |
17+
Write-Host "##vso[task.setvariable variable=ARM_OIDC_TOKEN;issecret=true]$($env:idToken)"
618
ServiceDirectory: monitor
719
TestTimeoutInMinutes: 300
820
BuildTargetingString: azure-monitor-*
921
SupportedClouds: 'Public,UsGov,China'
1022
CloudConfig:
1123
Public:
1224
SubscriptionConfiguration: $(sub-config-azure-cloud-test-resources)
25+
ServiceConnection: azure-sdk-tests
26+
SubscriptionConfigurationFilePaths:
27+
- eng/common/TestResources/sub-config/AzurePublicMsft.json
1328
UsGov:
29+
ServiceConnection: azure-sdk-tests
1430
SubscriptionConfiguration: $(sub-config-gov-test-resources)
1531
China:
32+
ServiceConnection: azure-sdk-tests
1633
SubscriptionConfiguration: $(sub-config-cn-test-resources)
1734
Location: chinanorth3
35+
UseFederatedAuth: true
1836
EnvVars:
1937
AZURE_SUBSCRIPTION_ID: $(MONITOR_SUBSCRIPTION_ID)
20-
AZURE_TENANT_ID: $(MONITOR_TENANT_ID)
21-
AZURE_CLIENT_ID: $(MONITOR_CLIENT_ID)
22-
AZURE_CLIENT_SECRET: $(MONITOR_CLIENT_SECRET)
2338
AZURE_TEST_RUN_LIVE: 'true'
2439
AZURE_SKIP_LIVE_RECORDING: 'true'
40+
ARM_OIDC_TOKEN: $(ARM_OIDC_TOKEN)

0 commit comments

Comments
 (0)