44using System ;
55using System . Collections . Generic ;
66using System . Diagnostics ;
7- using System . IdentityModel . Tokens . Jwt ;
87using System . IO ;
98using System . Linq ;
109using System . Net . Http ;
1514using Microsoft . AspNetCore . Builder ;
1615using Microsoft . AspNetCore . Hosting ;
1716using Microsoft . AspNetCore . TestHost ;
18- using Microsoft . Azure . Web . DataProtection ;
1917using Microsoft . Azure . WebJobs . Host . Executors ;
2018using Microsoft . Azure . WebJobs . Script . ExtensionBundle ;
19+ using Microsoft . Azure . WebJobs . Script . Grpc ;
2120using Microsoft . Azure . WebJobs . Script . Models ;
2221using Microsoft . Azure . WebJobs . Script . WebHost ;
23- using Microsoft . Azure . WebJobs . Script . WebHost . Authentication ;
2422using Microsoft . Azure . WebJobs . Script . WebHost . DependencyInjection ;
2523using Microsoft . Azure . WebJobs . Script . WebHost . Middleware ;
2624using Microsoft . Azure . WebJobs . Script . WebHost . Models ;
3432using Microsoft . Extensions . Logging ;
3533using Microsoft . Extensions . Logging . Abstractions ;
3634using Microsoft . Extensions . Options ;
37- using Microsoft . IdentityModel . Tokens ;
3835using Microsoft . WebJobs . Script . Tests ;
3936using Newtonsoft . Json . Linq ;
4037using IApplicationLifetime = Microsoft . AspNetCore . Hosting . IApplicationLifetime ;
@@ -62,10 +59,9 @@ public TestFunctionHost(string scriptPath,
6259 Action < IWebJobsBuilder > configureScriptHostWebJobsBuilder = null ,
6360 Action < IConfigurationBuilder > configureScriptHostAppConfiguration = null ,
6461 Action < ILoggingBuilder > configureScriptHostLogging = null ,
65- Action < IServiceCollection > configureScriptHostServices = null ,
66- Action < IConfigurationBuilder > configureWebHostAppConfiguration = null )
62+ Action < IServiceCollection > configureScriptHostServices = null )
6763 : this ( scriptPath , Path . Combine ( Path . GetTempPath ( ) , @"Functions" ) , configureWebHostServices , configureScriptHostWebJobsBuilder ,
68- configureScriptHostAppConfiguration , configureScriptHostLogging , configureScriptHostServices , configureWebHostAppConfiguration )
64+ configureScriptHostAppConfiguration , configureScriptHostLogging , configureScriptHostServices )
6965 {
7066 }
7167
@@ -74,9 +70,7 @@ public TestFunctionHost(string scriptPath, string logPath,
7470 Action < IWebJobsBuilder > configureScriptHostWebJobsBuilder = null ,
7571 Action < IConfigurationBuilder > configureScriptHostAppConfiguration = null ,
7672 Action < ILoggingBuilder > configureScriptHostLogging = null ,
77- Action < IServiceCollection > configureScriptHostServices = null ,
78- Action < IConfigurationBuilder > configureWebHostAppConfiguration = null ,
79- bool addTestSettings = true )
73+ Action < IServiceCollection > configureScriptHostServices = null )
8074 {
8175 _appRoot = scriptPath ;
8276
@@ -129,10 +123,7 @@ public TestFunctionHost(string scriptPath, string logPath,
129123 } )
130124 . ConfigureScriptHostAppConfiguration ( scriptHostConfigurationBuilder =>
131125 {
132- if ( addTestSettings )
133- {
134- scriptHostConfigurationBuilder . AddTestSettings ( ) ;
135- }
126+ scriptHostConfigurationBuilder . AddTestSettings ( ) ;
136127 configureScriptHostAppConfiguration ? . Invoke ( scriptHostConfigurationBuilder ) ;
137128 } )
138129 . ConfigureScriptHostLogging ( scriptHostLoggingBuilder =>
@@ -155,11 +146,7 @@ public TestFunctionHost(string scriptPath, string logPath,
155146 }
156147
157148 config . Add ( new ScriptEnvironmentVariablesConfigurationSource ( ) ) ;
158- if ( addTestSettings )
159- {
160- config . AddTestSettings ( ) ;
161- }
162- configureWebHostAppConfiguration ? . Invoke ( config ) ;
149+ config . AddTestSettings ( ) ;
163150 } )
164151 . UseStartup < TestStartup > ( ) ;
165152
@@ -195,9 +182,7 @@ public TestFunctionHost(string scriptPath, string logPath,
195182
196183 public ScriptJobHostOptions ScriptOptions => JobHostServices . GetService < IOptions < ScriptJobHostOptions > > ( ) . Value ;
197184
198- public ISecretManagerProvider SecretManagerProvider => _testServer . Host . Services . GetService < ISecretManagerProvider > ( ) ;
199-
200- public ISecretManager SecretManager => SecretManagerProvider . Current ;
185+ public ISecretManager SecretManager => _testServer . Host . Services . GetService < ISecretManagerProvider > ( ) . Current ;
201186
202187 public string LogPath => _hostOptions . LogPath ;
203188
@@ -207,11 +192,6 @@ public TestFunctionHost(string scriptPath, string logPath,
207192
208193 public async Task < string > GetMasterKeyAsync ( )
209194 {
210- if ( ! SecretManagerProvider . SecretsEnabled )
211- {
212- return null ;
213- }
214-
215195 HostSecretsInfo secrets = await SecretManager . GetHostSecretsAsync ( ) ;
216196 return secrets . MasterKey ;
217197 }
@@ -365,44 +345,13 @@ public async Task<FunctionStatus> GetFunctionStatusAsync(string functionName)
365345
366346 public async Task < HostStatus > GetHostStatusAsync ( )
367347 {
368- HttpRequestMessage request = new HttpRequestMessage ( HttpMethod . Get , "admin/host/status" ) ;
369-
370- if ( SecretManagerProvider . SecretsEnabled )
371- {
372- // use admin key
373- HostSecretsInfo secrets = await SecretManager . GetHostSecretsAsync ( ) ;
374- request . Headers . Add ( AuthenticationLevelHandler . FunctionsKeyHeaderName , secrets . MasterKey ) ;
375- }
376- else
377- {
378- // use admin jwt token
379- string token = GenerateAdminJwtToken ( ) ;
380- request . Headers . Authorization = new System . Net . Http . Headers . AuthenticationHeaderValue ( "Bearer" , token ) ;
381- }
382-
383- HttpResponseMessage response = await HttpClient . SendAsync ( request ) ;
348+ HostSecretsInfo secrets = await SecretManager . GetHostSecretsAsync ( ) ;
349+ string uri = $ "admin/host/status?code={ secrets . MasterKey } ";
350+ HttpResponseMessage response = await HttpClient . GetAsync ( uri ) ;
384351 response . EnsureSuccessStatusCode ( ) ;
385352 return await response . Content . ReadAsAsync < HostStatus > ( ) ;
386353 }
387354
388- public string GenerateAdminJwtToken ( )
389- {
390- var tokenHandler = new JwtSecurityTokenHandler ( ) ;
391- string defaultKey = Util . GetDefaultKeyValue ( ) ;
392- var key = Encoding . ASCII . GetBytes ( defaultKey ) ;
393- var tokenDescriptor = new SecurityTokenDescriptor
394- {
395- Audience = string . Format ( ScriptConstants . AdminJwtValidAudienceFormat , Environment . GetEnvironmentVariable ( EnvironmentSettingNames . AzureWebsiteName ) ) ,
396- Issuer = string . Format ( ScriptConstants . AdminJwtValidIssuerFormat , Environment . GetEnvironmentVariable ( EnvironmentSettingNames . AzureWebsiteName ) ) ,
397- Expires = DateTime . UtcNow . AddHours ( 1 ) ,
398- SigningCredentials = new SigningCredentials ( new SymmetricSecurityKey ( key ) , SecurityAlgorithms . HmacSha256Signature )
399- } ;
400- var token = tokenHandler . CreateToken ( tokenDescriptor ) ;
401- string tokenHeaderValue = tokenHandler . WriteToken ( token ) ;
402-
403- return tokenHeaderValue ;
404- }
405-
406355 public void Dispose ( )
407356 {
408357 if ( ! _isDisposed )
0 commit comments