Skip to content

Commit 5b10710

Browse files
add validate cert
1 parent 78bd220 commit 5b10710

4 files changed

Lines changed: 74 additions & 34 deletions

File tree

samples/web-app-sql-database/python/scripts/deploy.sh

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,15 @@ fi
392392
# Create certificate in Key Vault
393393
echo "Creating certificate [$CERT_NAME] in Key Vault [$KEY_VAULT_NAME]..."
394394
$AZ keyvault certificate create \
395-
--vault-name "$KEY_VAULT_NAME" \
396-
--name "$CERT_NAME" \
397-
--policy "$(az keyvault certificate get-default-policy)" \
398-
--only-show-errors 1>/dev/null
395+
--vault-name "$KEY_VAULT_NAME" \
396+
--name "$CERT_NAME" \
397+
--policy '{
398+
"issuerParameters": {"name": "Self"},
399+
"keyProperties": {"exportable": true, "keySize": 2048, "keyType": "RSA", "reuseKey": false},
400+
"secretProperties": {"contentType": "application/x-pkcs12"},
401+
"x509CertificateProperties": {"subject": "CN=sample-web-app-sql", "validityInMonths": 12}
402+
}' \
403+
--only-show-errors
399404

400405
if [ $? -eq 0 ]; then
401406
echo "Certificate [$CERT_NAME] created successfully in Key Vault [$KEY_VAULT_NAME]."
@@ -482,34 +487,6 @@ WEB_APP_URL=$($AZ webapp show \
482487
--query "defaultHostName" \
483488
--output tsv)
484489

485-
# Wait for web app to be ready
486-
echo "Waiting for web app to be ready..."
487-
MAX_RETRIES=10
488-
for i in $(seq 1 $MAX_RETRIES); do
489-
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "https://$WEB_APP_URL" --insecure)
490-
if [ "$HTTP_STATUS" -eq 200 ]; then
491-
echo "Web app is responding with HTTP 200"
492-
break
493-
fi
494-
echo "Attempt $i/$MAX_RETRIES - HTTP $HTTP_STATUS. Retrying in 5 seconds..."
495-
sleep 5
496-
done
497-
498-
if [ "$HTTP_STATUS" -ne 200 ]; then
499-
echo "Web app failed to respond with HTTP 200 after $MAX_RETRIES attempts"
500-
exit 1
501-
fi
502-
503-
echo "Validating certificate from Key Vault..."
504-
CERT_NAME_RESPONSE=$(curl -s "https://$WEB_APP_URL/api/certificate/validate" --insecure | jq -r '.name')
505-
506-
if [ "$CERT_NAME_RESPONSE" == "$CERT_NAME" ]; then
507-
echo "Certificate [$CERT_NAME] validated successfully from web app."
508-
else
509-
echo "Certificate validation failed. Expected [$CERT_NAME], got [$CERT_NAME_RESPONSE]."
510-
exit 1
511-
fi
512-
513490
# Remove the zip package of the web app
514491
if [ -f "$ZIPFILE" ]; then
515492
rm "$ZIPFILE"

samples/web-app-sql-database/python/scripts/get-web-app-url.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ get_docker_container_port_mapping() {
6565
echo "$host_port"
6666
}
6767

68+
wait_for_http_response() {
69+
local url="$1"
70+
local description="$2"
71+
local max_retries="${3:-5}"
72+
local retry_interval="${4:-5}"
73+
74+
echo "Waiting for [$description] to respond at [$url]..."
75+
76+
for i in $(seq 1 $max_retries); do
77+
http_status=$(curl -s -o /dev/null -w "%{http_code}" "$url" --max-time 5)
78+
if [ "$http_status" -eq 200 ]; then
79+
echo "[$description] is responding with HTTP 200"
80+
return 0
81+
fi
82+
echo "Attempt $i/$max_retries - HTTP $http_status. Retrying in ${retry_interval}s..."
83+
sleep $retry_interval
84+
done
85+
86+
echo "Error: [$description] failed to respond with HTTP 200 after $max_retries attempts" >&2
87+
return 1
88+
}
89+
6890
call_web_app() {
6991
# Get the web app name
7092
echo "Getting web app name..."
@@ -180,6 +202,35 @@ call_web_app() {
180202
else
181203
echo "Failed to retrieve host port"
182204
fi
205+
206+
echo "Validating certificate from Key Vault..."
207+
KV_RESPONSE=$(curl -sk "https://$container_ip:8443/api/certificate/validate")
208+
KV_THUMBPRINT=$(echo "$KV_RESPONSE" | jq -r '.thumbprint')
209+
KV_NAME=$(echo "$KV_RESPONSE" | jq -r '.name')
210+
KV_SUBJECT=$(echo "$KV_RESPONSE" | jq -r '.subject')
211+
212+
SSL_THUMBPRINT=$(echo | openssl s_client -connect "$container_ip:8443" 2>/dev/null \
213+
| openssl x509 -fingerprint -noout -sha1 \
214+
| sed 's/.*=//;s/://g' \
215+
| tr '[:upper:]' '[:lower:]')
216+
217+
if [ "$KV_THUMBPRINT" == "$SSL_THUMBPRINT" ]; then
218+
echo "Certificate [$KV_NAME] validated: SSL cert matches Key Vault cert."
219+
else
220+
echo "Certificate mismatch! KV: $KV_THUMBPRINT, SSL: $SSL_THUMBPRINT"
221+
exit 1
222+
fi
223+
224+
SSL_SUBJECT=$(echo "$SSL_CERT" \
225+
| openssl x509 -noout -subject \
226+
| sed 's/subject=//')
227+
228+
if echo "$SSL_SUBJECT" | grep -q "$KV_SUBJECT"; then
229+
echo "Certificate subject [$KV_SUBJECT] matches SSL certificate."
230+
else
231+
echo "Certificate subject mismatch! KV: $KV_SUBJECT, SSL: $SSL_SUBJECT"
232+
exit 1
233+
fi
183234
}
184235

185236
call_web_app

samples/web-app-sql-database/python/src/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,6 @@ def validate_certificate():
183183

184184
if vault_uri and cert_name:
185185
ssl_ctx = get_ssl_context_from_keyvault(vault_uri, cert_name)
186-
app.run(host='0.0.0.0', port=443, ssl_context=ssl_ctx)
186+
app.run(host='0.0.0.0', port=8443, ssl_context=ssl_ctx)
187187
else:
188188
app.run(debug=debug)

samples/web-app-sql-database/python/src/certificates.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Certificate helper module for Azure Key Vault integration."""
22
import base64
3+
import hashlib
34
import logging
45
import os
56
import ssl
@@ -81,6 +82,17 @@ def get_certificate_info(vault_url: str, cert_name: str) -> dict:
8182
cert_client = CertificateClient(vault_url=vault_url, credential=credential)
8283
cert = cert_client.get_certificate(cert_name)
8384

85+
x509_cert_bytes = cert.cer
86+
if x509_cert_bytes is None:
87+
raise ValueError(f"Certificate '{cert_name}' has no public bytes (cer is None)")
88+
89+
if cert.policy is None:
90+
raise ValueError(f"Certificate '{cert_name}' has no policy")
91+
92+
thumbprint = hashlib.sha1(x509_cert_bytes).hexdigest()
93+
8494
return {
85-
"name": cert.name
95+
"name": cert.name,
96+
"subject": cert.policy.subject,
97+
"thumbprint": thumbprint,
8698
}

0 commit comments

Comments
 (0)