Skip to content

Commit c075ddf

Browse files
authored
Add resource ID validation (#129)
1 parent 1d08744 commit c075ddf

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

azure.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ metadata:
77
88

99
hooks:
10+
preup:
11+
posix:
12+
shell: sh
13+
run: chmod u+r+x ./scripts/validate_env_vars.sh; ./scripts/validate_env_vars.sh
14+
interactive: true
15+
continueOnError: false
16+
windows:
17+
shell: pwsh
18+
run: ./scripts/validate_env_vars.ps1
19+
interactive: true
20+
continueOnError: false
1021
preprovision:
1122
posix:
1223
shell: sh

scripts/validate_env_vars.ps1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Define environment variables and their regex patterns
2+
$envValidationRules = @{
3+
"AZURE_EXISTING_AIPROJECT_RESOURCE_ID" = '^/subscriptions/[0-9a-fA-F-]{36}/resourceGroups/[^/]+/providers/Microsoft\.CognitiveServices/accounts/[^/]+/projects/[^/]+$'
4+
}
5+
6+
$hasError = $false
7+
8+
foreach ($envVar in $envValidationRules.Keys) {
9+
$pattern = $envValidationRules[$envVar]
10+
$value = [Environment]::GetEnvironmentVariable($envVar)
11+
12+
if ($value) {
13+
if ($value -notmatch $pattern) {
14+
Write-Host "❌ Invalid value for '$envVar'. Expected pattern: $pattern"
15+
$hasError = $true
16+
}
17+
}
18+
}
19+
20+
if ($hasError) {
21+
exit 1
22+
}

scripts/validate_env_vars.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
# Define environment variables and their regex patterns
4+
declare -A envValidationRules=(
5+
["AZURE_EXISTING_AIPROJECT_RESOURCE_ID"]='^/subscriptions/[0-9a-fA-F-]{36}/resourceGroups/[^/]+/providers/Microsoft\.CognitiveServices/accounts/[^/]+/projects/[^/]+$'
6+
)
7+
8+
hasError=0
9+
10+
for envVar in "${!envValidationRules[@]}"; do
11+
pattern="${envValidationRules[$envVar]}"
12+
value="${!envVar}"
13+
14+
if [[ -n "$value" ]]; then
15+
if ! echo "$value" | grep -Eq "$pattern"; then
16+
echo "❌ Invalid value for '$envVar'. Expected pattern: $pattern" >&2
17+
hasError=1
18+
fi
19+
fi
20+
done
21+
22+
exit $hasError

0 commit comments

Comments
 (0)