将函数源代码添加到源代码控制存储库时如何正确处理 local.settings.json 文件中的机密

All*_* Xu 11 git security azure-functions

我有一个 Azure 函数,其文件中包含一些机密local.settings.json

当我想在 GitHub 中共享函数的源代码时,最佳实践是什么?

到目前为止,我可以想到以下选项,但每个选项都有一些问题或挑战:

local.settings.json1-记住在我提交更改时更改秘密。提交完成后,撤消更改,以便我可以运行该函数并对其进行调试。这个选项非常容易出错并且乏味。

2- 添加 local.settings.json到 .gitignore 文件。通过这种方法,从 GitHub 获取代码的人需要记住恢复local.settings.json

3- 将机密存储在 Azure Key Vault 中。但这对于我正在创建的这么小的功能来说太多了。

我想问这里处理 local.settings.json源代码控制存储库中的机密的最佳实践是什么。

Tom*_*sek 8

如此处所述,您可以为您的机密添加另一个配置文件 ( secret.settings.json)。

{
    "ConnectionStrings": {
        "SqlConnectionString": "server=myddatabaseserver;user=tom;password=123;"
    },
    "MyCustomStringSetting": "Override Some Name",
    "MailSettings": {
        "PrivateKey": "xYasdf5678asjifSDFGhasn1234sDGFHg"
    }
}
Run Code Online (Sandbox Code Playgroud)

将新设置文件添加到.gitignore. 然后local.settings.json从 中删除.gitignore并编辑任何秘密值。

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet"
    },
    "ConnectionStrings": {
        "SqlConnectionString": "--SECRET--"
    },
    "MyCustomStringSetting": "Some Name",
    "MyCustomNumberSetting": 123,
    "MailSettings": {
        "FromAddress": "local-testing123@email.com",
        "ToAddress": "receiver@email.com",
        "MailServer": "smtp.mymailserver.com",
        "PrivateKey": "--SECRET--"
    }
}
Run Code Online (Sandbox Code Playgroud)

然后确保包含额外的配置文件。

var config = new ConfigurationBuilder()
    .SetBasePath(context.FunctionAppDirectory)
    .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
    .AddJsonFile("secret.settings.json", optional: true, reloadOnChange: true)
    .AddEnvironmentVariables()
    .Build();
Run Code Online (Sandbox Code Playgroud)

使用此技术,至少可以在源代码管理中跟踪所有设置。任何秘密值都会被安全地编辑。

  • 这种方法非常适合您在函数正文中读取的设置。但是,我不确定它是否适用于需要在 Run 函数签名的属性中包含连接字符串名称的绑定,例如 [BlobTrigger ("ConnectionSettingName")] 。博客文章中的 exmaple 使用 [HttpTrigger] 属性,不需要应用程序设置中的任何设置名称。 (5认同)