簡體   English   中英

將函數源代碼添加到源代碼控制存儲庫時如何正確處理 local.settings.json 文件中的機密

[英]How to properly handle secrets in a local.settings.json file when adding the function source code to a source control repository

我有一個 Azure 函數,它的local.settings.json文件中有一些秘密。

當我想在 GitHub 中共享我的函數的源代碼時,最佳實踐是什么?

到目前為止,我可以想到以下選項,但每個選項都有一些問題或挑戰:

1- 記得在我提交更改時更改local.settings.json的機密。 提交完成后,撤消更改,以便我可以運行該函數並對其進行調試。 此選項非常容易出錯且乏味。

2- 將local.settings.json添加到 .gitignore 文件中。 用這種方式,從GitHub獲取代碼的人需要記得恢復local.settings.json

3- 將機密存儲在 Azure Key Vault 中。 但這對於我正在創建的這么小的功能來說太過分了。

我想在這里詢問如何處理源代碼控制存儲庫中local.settings.json中的機密的最佳實踐是什么。

如此處所述,您可以為您的機密添加另一個配置文件 ( secret.settings.json )。

{
    "ConnectionStrings": {
        "SqlConnectionString": "server=myddatabaseserver;user=tom;password=123;"
    },
    "MyCustomStringSetting": "Override Some Name",
    "MailSettings": {
        "PrivateKey": "xYasdf5678asjifSDFGhasn1234sDGFHg"
    }
}

將您的新設置文件添加到.gitignore 然后從.gitignore刪除local.settings.json並編輯任何秘密值。

{
    "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--"
    }
}

然后確保包含您的額外配置文件。

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();

使用這種技術,至少可以在源代碼管理中跟蹤所有設置。 任何秘密值都被安全地編輯。

記得在我提交更改時更改 local.settings.json 中的機密

使用smudge-clean機制。 smudge-clean 是一種機制,允許您在文件通過索引時對其進行修改。

smudge/clean 是過濾器,每當您將文件 (clean) 和 checkout 文件提交到工作目錄 (smudge) 時,它們就會運行。


Smudge / clean

閱讀所有相關信息並在此處進行設置:
https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes

事實證明,您可以編寫自己的過濾器,以便在commit/checkout上的文件中進行替換。

這些被稱為cleansmudge過濾器。

.gitattributes文件中,您可以為特定路徑設置過濾器,然后設置將在文件簽出之前(“塗抹”)和暫存之前(“清理”)處理文件的腳本。

這些過濾器可以設置為做各種有趣的事情。
因此,您可以編寫自己的過濾器,以便在提交/簽出時在文件中進行替換。

在此處輸入圖片說明

您可以在沒有機密的情況下提交 Json 文件,然后在本地添加機密,並且永遠不要再次提交文件。

此外,如果您在過去使用機密提交文件,然后在沒有機密的情況下再次提交文件,則您的機密仍在存儲庫中。 您必須使用鎬刪除文件(我認為檢查 filetree 命令)。

您應該真正使用此處所述的用戶機密 這也適用於 Azure Functions。 這樣,未加密的文件至少存儲在用戶配置文件目錄中,該機器上的其他用戶無法讀取。

您甚至不必為用戶機密或環境變量注冊配置源。 這是由 Azure Function 運行時自動完成的。 您可以通過注入 IConfiguration 以同樣的方式從環境變量local.settings.jsonsecrets.json訪問配置:

using Burckhardt.IoT.Compressor.Data.Function.Interfaces;
using System.Text.Json;
using Microsoft.Extensions.Configuration;

namespace Burckhardt.IoT.Compressor.Data.Function.Configuration
{
    public abstract class ConfigurationLoader<T> : IConfigurationLoader<T>
        where T : class, new()
    {
        private readonly string _environmentVariableName;
        private readonly IConfiguration _configuration;

        protected ConfigurationLoader(IConfiguration configuration, string environmentVariableName)
        {
            _environmentVariableName = environmentVariableName;
            _configuration = configuration;
        }

        public T Load()
        {
            var environmentVariableValue = _configuration.GetSection(_environmentVariableName).Value;
            return JsonSerializer.Deserialize<T>(environmentVariableValue);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM