44
55import json
66import logging
7+ import os
78import subprocess
89import tempfile
910import time
2223class FuncApp :
2324 """Basic class for managing function apps."""
2425
25- def __init__ (self , name : str , resource_group : str , output_path : Path ) -> None :
26+ def __init__ (
27+ self ,
28+ name : str ,
29+ resource_group : str ,
30+ output_path : Path ,
31+ subscription : Optional [str ] = None ,
32+ ) -> None :
2633 """Create a FuncApp object."""
2734 self .name = name
2835 self .resource_group = resource_group
2936 self .output_path = output_path
37+ self .subscription = subscription
3038
3139 def wait_for_event_trigger (self ) -> None :
3240 """Wait until the function app has an eventGridTrigger function."""
@@ -42,7 +50,8 @@ def wait_for_event_trigger(self) -> None:
4250 self .resource_group ,
4351 "--query" ,
4452 "[].name" ,
45- ]
53+ ],
54+ subscription = self .subscription ,
4655 )
4756 log .info ("Awaiting event trigger on function app %s" , self .name )
4857
@@ -85,10 +94,14 @@ def deploy(self) -> None:
8594class FuncAppZip (FuncApp ):
8695 """Class for managing zipped function apps."""
8796
88- def __init__ (self , name : str , resource_group : str ) -> None :
97+ def __init__ (
98+ self , name : str , resource_group : str , subscription : Optional [str ] = None
99+ ) -> None :
89100 """Create a FuncAppZip object."""
90101 self .tempfile = tempfile .NamedTemporaryFile (suffix = ".zip" , delete = False )
91- super ().__init__ (name , resource_group , Path (self .tempfile .name ))
102+ super ().__init__ (
103+ name , resource_group , Path (self .tempfile .name ), subscription = subscription
104+ )
92105
93106 self .zip_paths = [
94107 Path ("host.json" ),
@@ -119,7 +132,8 @@ def deploy(self) -> None:
119132 str (self .output_path ),
120133 "--build-remote" ,
121134 "true" ,
122- ]
135+ ],
136+ subscription = self .subscription ,
123137 )
124138 log .info ("Deploying function app code to %s" , self .name )
125139 cmd .run ()
@@ -129,34 +143,48 @@ def deploy(self) -> None:
129143class FuncAppBundle (FuncApp ):
130144 """Publishes the function app using the core-tools tooling."""
131145
132- def __init__ (self , name : str , resource_group : str ) -> None :
146+ def __init__ (
147+ self , name : str , resource_group : str , subscription : Optional [str ] = None
148+ ) -> None :
133149 """Create a FuncAppBundle object."""
134- super ().__init__ (name , resource_group , Path ("function_app.zip" ))
150+ super ().__init__ (
151+ name , resource_group , Path ("function_app.zip" ), subscription = subscription
152+ )
135153
136154 def deploy (self ) -> None :
137155 """Deploy the function application."""
138156 log .info ("Deploying function app code" )
139157 cwd = Path .cwd ()
140- home = Path .home ()
141- azure_config = home / ".azure"
158+
159+ # Mint an access token on the host: the host 'az' can read its own
160+ # credential cache, whereas the (older) 'az' inside the core-tools image
161+ # cannot deserialise a cache written by a newer host 'az'. So instead of
162+ # mounting ~/.azure into the container, pass a token to 'func' directly.
163+ token_cmd = ["az" , "account" , "get-access-token" , "--query" , "accessToken" ]
164+ token = AzCmdJson (token_cmd , subscription = self .subscription ).run ()
165+
166+ # Pass the token via the environment so it never appears in the logged
167+ # command line or the container's process arguments.
168+ func_cmd = f"func azure functionapp publish { self .name } --python --build remote --access-token \" $FUNC_ACCESS_TOKEN\" "
169+ if self .subscription :
170+ func_cmd += f" --subscription { self .subscription } "
142171
143172 # Publish the application using the core-tools tooling
144173 cmd = [
145174 "docker" ,
146175 "run" ,
147- "-it" ,
148176 "--rm" ,
149- "-v " ,
150- f" { azure_config } :/root/.azure " ,
177+ "-e " ,
178+ "FUNC_ACCESS_TOKEN " ,
151179 "-v" ,
152180 f"{ cwd } :/function_app" ,
153181 "-w" ,
154182 "/function_app" ,
155183 "mcr.microsoft.com/azure-functions/python:4-python3.11-core-tools" ,
156184 "bash" ,
157185 "-c" ,
158- f"func azure functionapp publish { self . name } --python --build remote" ,
186+ func_cmd ,
159187 ]
160188 log .debug ("Running %s" , cmd )
161- subprocess .run (cmd , check = True )
189+ subprocess .run (cmd , check = True , env = { ** os . environ , "FUNC_ACCESS_TOKEN" : token } )
162190 log .info ("Function app code published to %s" , self .name )
0 commit comments