Skip to content

Commit 2ffaadf

Browse files
{App Service} az webapp deploy/deployment: Added checksum header to check file integrity in service, for linux app service (#29516)
1 parent 35f5fde commit 2ffaadf

File tree

5 files changed

+1243
-1096
lines changed

5 files changed

+1243
-1096
lines changed

src/azure-cli/azure/cli/command_modules/appservice/custom.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -789,11 +789,16 @@ def enable_zip_deploy(cmd, resource_group_name, name, src, timeout=None, slot=No
789789
import os
790790
import requests
791791
from azure.cli.core.util import should_disable_connection_verify
792-
# Read file content
792+
# check if the app is a linux web app
793+
app_is_linux_webapp = is_linux_webapp(app)
793794

795+
# Read file content
794796
with open(os.path.realpath(os.path.expanduser(src)), 'rb') as fs:
795797
zip_content = fs.read()
796798
logger.warning("Starting zip deployment. This operation can take a while to complete ...")
799+
if app_is_linux_webapp and track_status is not None and track_status:
800+
headers["x-ms-artifact-checksum"] = _compute_checksum(zip_content)
801+
797802
res = requests.post(zip_url, data=zip_content, headers=headers, verify=not should_disable_connection_verify())
798803
logger.warning("Deployment endpoint responded with status code %d", res.status_code)
799804

@@ -6990,12 +6995,22 @@ def _get_onedeploy_status_url(params):
69906995

69916996
def _get_onedeploy_request_body(params):
69926997
import os
6998+
file_hash = None
6999+
app_is_linux_webapp = False
69937000

69947001
if params.src_path:
69957002
logger.warning('Deploying from local path: %s', params.src_path)
7003+
7004+
if params.track_status is not None and params.track_status:
7005+
client = web_client_factory(params.cmd.cli_ctx)
7006+
app = client.web_apps.get(params.resource_group_name, params.webapp_name)
7007+
app_is_linux_webapp = is_linux_webapp(app)
7008+
69967009
try:
69977010
with open(os.path.realpath(os.path.expanduser(params.src_path)), 'rb') as fs:
69987011
body = fs.read()
7012+
if app_is_linux_webapp:
7013+
file_hash = _compute_checksum(body)
69997014
except Exception as e: # pylint: disable=broad-except
70007015
raise ResourceNotFoundError("Either '{}' is not a valid local file path or you do not have permissions to "
70017016
"access it".format(params.src_path)) from e
@@ -7016,7 +7031,7 @@ def _get_onedeploy_request_body(params):
70167031
else:
70177032
raise ResourceNotFoundError('Unable to determine source location of the artifact being deployed')
70187033

7019-
return body
7034+
return body, file_hash
70207035

70217036

70227037
def _update_artifact_type(params):
@@ -7043,11 +7058,14 @@ def _make_onedeploy_request(params):
70437058
from azure.cli.core.util import should_disable_connection_verify
70447059

70457060
# Build the request body, headers, API URL and status URL
7046-
body = _get_onedeploy_request_body(params)
7061+
body, file_hash = _get_onedeploy_request_body(params)
70477062
deploy_url = _build_onedeploy_url(params)
70487063
deployment_status_url = _get_onedeploy_status_url(params)
70497064
headers = _get_ondeploy_headers(params)
70507065

7066+
if file_hash:
7067+
headers["x-ms-artifact-checksum"] = file_hash
7068+
70517069
# For debugging purposes only, you can change the async deployment into a sync deployment by polling the API status
70527070
# For that, set poll_async_deployment_for_debugging=True
70537071
logger.info("Deployment API: %s", deploy_url)
@@ -8322,3 +8340,16 @@ def _encrypt_github_actions_secret(public_key, secret_value):
83228340

83238341
def show_webapp(cmd, resource_group_name, name, slot=None): # adding this to not break extensions
83248342
return show_app(cmd, resource_group_name, name, slot)
8343+
8344+
8345+
def _compute_checksum(input_bytes):
8346+
file_hash = None
8347+
try:
8348+
import hashlib
8349+
logger.info("Computing checksum of the file ...")
8350+
file_hash = hashlib.sha256(input_bytes).hexdigest()
8351+
logger.info("Computed checksum for deployment request header x-ms-artifact-checksum '%s'", file_hash)
8352+
except Exception as ex: # pylint: disable=broad-except
8353+
logger.info("Computing the checksum of the file failed with exception:'%s'", ex)
8354+
8355+
return file_hash

0 commit comments

Comments
 (0)