This repository has been archived by the owner on Jan 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyHttpTrigger.cs
77 lines (72 loc) · 3.25 KB
/
MyHttpTrigger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
namespace zyin.Function
{
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Configuration;
/// <summary>
/// class demoing HttpTrigger with user secrets and keyvault support
/// </summary>
public class MyHttpTrigger
{
/// <summary>
/// Reference to the ICofiguratoin object
/// </summary>
private readonly IConfiguration configuration;
/// <summary>
/// Reference to app config
/// </summary>
private readonly AppConfig appConfig;
/// <summary>
/// Initializes a new MyHttpTriggerFunction class.
/// It takes IAzureKeyVaultService from the DI container.
/// </summary>
/// <param name="configuration">configuration</param>
/// <param name="appConfigOptions">app config options</param>
public MyHttpTrigger(IConfiguration configuration, IOptions<AppConfig> appConfigOptions)
{
// Usually you only need to access your settings via IOptions pattern.
// I am referencing configuration here for demo purpose.
this.configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
this.appConfig = appConfigOptions?.Value ?? throw new ArgumentNullException(nameof(appConfigOptions));
}
/// <summary>
/// An Azure function endpoint to dump secrets.
/// This is for demo purpose, don't do this for real secrets or in Production.
/// 1. We use ActionResult<T> instead of IActionResult for better return type checking.
/// </summary>
/// <param name="req">http request</param>
/// <param name="secretName">secret name</param>
/// <returns>message contains the secret value</returns>
[FunctionName("ShowKeyVaultSecret")]
public ActionResult<string> ShowKeyVaultSecret(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "vault/{secretName}")] HttpRequest req,
string secretName,
ILogger log)
{
// !!!This is just for demo purpose!!!
// DO NOT do this in prod - you should never dump secrets.
var secretValue = this.configuration.GetValue<string>(secretName);
var message = secretValue != null ? $"Hush, this is our secret - {secretName} : {secretValue}" : $"Secret {secretName} doesn't exist in keyvault.";
return message;
}
/// <summary>
/// An Azure function endpoint to render AppConfig
/// </summary>
/// <param name="req">http request</param>
/// <returns>AppConfig</returns>
[FunctionName("ShowAppConfig")]
public ActionResult<AppConfig> ShowAppConfig(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "appconfig")] HttpRequest req,
ILogger log)
{
// !!!This is just for demo purpose!!!
// DO NOT do this in prod - you should never blindly dump config settings since it can contain secrets.
return this.appConfig;
}
}
}