Skip to content

Commit aa7fa33

Browse files
authored
Replace PAT with job access token to improve security. (#25006)
* Replace PAT with job access token to improve security. * Terminate the process when wiki content cannot be retrieved. * Add null check for contacts list
1 parent e675318 commit aa7fa33

File tree

2 files changed

+70
-63
lines changed

2 files changed

+70
-63
lines changed

.azure-pipelines/sync-aliases.yml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,38 @@ schedules:
66
include:
77
- main
88

9+
# The 'resources' and 'uses' below are used to resolve the error 'Repository associated with wiki ID <WikiId> does not exist or you do not have permissions for the operation you are attempting.'
10+
resources:
11+
repositories:
12+
- repository: ServiceContactList
13+
type: git
14+
name: internal.wiki
15+
916
jobs:
1017
- job: UpdateYaml
1118
displayName: Update resourceManagement.yml
1219
pool: pool-windows-2019
20+
uses:
21+
repositories:
22+
- ServiceContactList
1323

1424
steps:
1525
- task: UseDotNet@2
1626
displayName: Install .NET 8 SDK
1727
inputs:
1828
packageType: sdk
1929
version: 8.0.x
20-
- template: util/get-github-pat-steps.yml
2130

2231
- pwsh: |
2332
dotnet --version
2433
dotnet new tool-manifest --force
2534
dotnet tool install powershell --version 7.4.*
2635
displayName: Install PowerShell 7.4.x
2736
28-
- template: util/get-keyvault-secret-steps.yml
29-
parameters:
30-
serviceConnectionName: $(AzureSubscription)
31-
keyVaultName: $(KeyVaultName)
32-
secretName: $(ADOTokenName)
33-
outVar: 'ADOToken'
34-
3537
- pwsh: |
36-
dotnet tool run pwsh -NoLogo -NoProfile -NonInteractive -File "./tools/Github/ParseServiceContactsList.ps1 -ADOToken $(ADOToken)"
38+
dotnet tool run pwsh -NoLogo -NoProfile -NonInteractive -File "./tools/Github/ParseServiceContactsList.ps1 -AccessToken $env:SYSTEM_ACCESSTOKEN"
39+
env:
40+
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
3741
displayName: Update resourceManagement.yml file locally
3842
3943
- pwsh: |
@@ -47,6 +51,8 @@ jobs:
4751
}
4852
displayName: Check if Wiki table has any changes
4953
54+
- template: util/get-github-pat-steps.yml
55+
5056
- pwsh: |
5157
git config --global user.email "65331932+azure-powershell-bot@users.noreply.github.com"
5258
git config --global user.name "azure-powershell-bot"

tools/Github/ParseServiceContactsList.ps1

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#>
2020
param(
2121
[Parameter(Mandatory = $true)]
22-
[string]$ADOToken
22+
[string] $AccessToken
2323
)
2424

2525
function InitializeRequiredPackages {
@@ -37,7 +37,6 @@ function InitializeRequiredPackages {
3737
$requiredPackages = @(
3838
@{ PackageName = "Newtonsoft.Json"; PackageVersion = "13.0.2"; DllName = "Newtonsoft.Json.dll" },
3939
@{ PackageName = "YamlDotNet"; PackageVersion = "13.2.0"; DllName = "YamlDotNet.dll" }
40-
4140
)
4241

4342
$requiredPackages | ForEach-Object {
@@ -51,71 +50,73 @@ function InitializeRequiredPackages {
5150

5251
# get wiki content
5352
$username = ""
54-
$password = $ADOToken
53+
$password = $AccessToken
5554
$pair = "{0}:{1}" -f ($username, $password)
5655
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
5756
$token = [System.Convert]::ToBase64String($bytes)
5857
$headers = @{
5958
Authorization = "Basic {0}" -f ($token)
6059
}
6160

62-
$response = Invoke-RestMethod 'https://dev.azure.com/azclitools/internal/_apis/wiki/wikis/internal.wiki/pages?path=/Service%20Contact%20List&includeContent=true' -Headers $headers
61+
$response = Invoke-RestMethod 'https://dev.azure.com/azclitools/internal/_apis/wiki/wikis/internal.wiki/pages?path=/Service%20Contact%20List&includeContent=true' -Headers $headers -ErrorAction Stop
6362
$contactsList = ($response.content -split "\n") | Where-Object { $_ -like '|*' } | Select-Object -Skip 2
6463

65-
$idxServiceTeamLabel = 2
66-
$idxPSNotifyGithubHandler = 6
67-
$serviceContacts = [System.Collections.Generic.SortedList[System.String, PSCustomObject]]::new()
68-
69-
foreach ($contacts in $contactsList) {
70-
$items = $contacts -split "\|"
71-
$colServiceTeamLabel = $items[$idxServiceTeamLabel]
72-
if (![string]::IsNullOrWhiteSpace($colServiceTeamLabel)) {
73-
$serviceTeamLabel = $colServiceTeamLabel.Trim()
74-
$colPSNotifyGithubHandler = $items[$idxPSNotifyGithubHandler]
75-
76-
if (![string]::IsNullOrWhiteSpace($colPSNotifyGithubHandler)) {
77-
$psNotifyGithubHandler = $colPSNotifyGithubHandler.Trim()
78-
[array]$mentionees = $psNotifyGithubHandler.Split(",", [StringSplitOptions]::RemoveEmptyEntries) | ForEach-Object {
79-
$_.Trim()
80-
}
81-
82-
$serviceContacts.Add($serviceTeamLabel, [PSCustomObject]@{
83-
if = @(
84-
[PSCustomObject]@{
85-
or = @(
86-
[PSCustomObject]@{
87-
labelAdded = [PSCustomObject]@{
88-
label = 'Service Attention'
89-
}
90-
},
91-
[PSCustomObject]@{
92-
labelAdded = [PSCustomObject]@{
93-
label = $serviceTeamLabel
64+
if ($null -ne $contactsList) {
65+
$idxServiceTeamLabel = 2
66+
$idxPSNotifyGithubHandler = 6
67+
$serviceContacts = [System.Collections.Generic.SortedList[System.String, PSCustomObject]]::new()
68+
69+
foreach ($contacts in $contactsList) {
70+
$items = $contacts -split "\|"
71+
$colServiceTeamLabel = $items[$idxServiceTeamLabel]
72+
if (![string]::IsNullOrWhiteSpace($colServiceTeamLabel)) {
73+
$serviceTeamLabel = $colServiceTeamLabel.Trim()
74+
$colPSNotifyGithubHandler = $items[$idxPSNotifyGithubHandler]
75+
76+
if (![string]::IsNullOrWhiteSpace($colPSNotifyGithubHandler)) {
77+
$psNotifyGithubHandler = $colPSNotifyGithubHandler.Trim()
78+
[array]$mentionees = $psNotifyGithubHandler.Split(",", [StringSplitOptions]::RemoveEmptyEntries) | ForEach-Object {
79+
$_.Trim()
80+
}
81+
82+
$serviceContacts.Add($serviceTeamLabel, [PSCustomObject]@{
83+
if = @(
84+
[PSCustomObject]@{
85+
or = @(
86+
[PSCustomObject]@{
87+
labelAdded = [PSCustomObject]@{
88+
label = 'Service Attention'
89+
}
90+
},
91+
[PSCustomObject]@{
92+
labelAdded = [PSCustomObject]@{
93+
label = $serviceTeamLabel
94+
}
9495
}
96+
)
97+
},
98+
[PSCustomObject]@{
99+
hasLabel = [PSCustomObject]@{
100+
label = 'Service Attention'
101+
}
102+
},
103+
[PSCustomObject]@{
104+
hasLabel = [PSCustomObject]@{
105+
label = $serviceTeamLabel
95106
}
96-
)
97-
},
98-
[PSCustomObject]@{
99-
hasLabel = [PSCustomObject]@{
100-
label = 'Service Attention'
101-
}
102-
},
103-
[PSCustomObject]@{
104-
hasLabel = [PSCustomObject]@{
105-
label = $serviceTeamLabel
106107
}
107-
}
108-
)
109-
then = @(
110-
[PSCustomObject]@{
111-
mentionUsers = [PSCustomObject]@{
112-
mentionees = $mentionees
113-
replyTemplate = 'Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc ${mentionees}.'
114-
assignMentionees = 'False'
108+
)
109+
then = @(
110+
[PSCustomObject]@{
111+
mentionUsers = [PSCustomObject]@{
112+
mentionees = $mentionees
113+
replyTemplate = 'Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc ${mentionees}.'
114+
assignMentionees = 'False'
115+
}
115116
}
116-
}
117-
)
118-
})
117+
)
118+
})
119+
}
119120
}
120121
}
121122
}

0 commit comments

Comments
 (0)