本指南將引導你了解如何管理 Service Fabric 應用程式中的秘密。 秘密可以是任何敏感資訊,例如儲存連接字串、密碼或其他不應該以明文處理的值。
在 Service Fabric 應用程式中使用加密秘密包含三個步驟:
- 建立加密憑證並加密秘密。
- 在應用程式中指定加密的秘密。
- 從服務代碼中解密加密的秘密。
設定加密憑證並加密秘密
設定加密憑證並用它來加密秘密,在 Windows 和 Linux 之間有所不同。
在應用程式中指定加密祕密
前一步說明如何用憑證加密祕密,並產生一個以 base-64 編碼的字串用於應用程式。 這個以 64 為基礎編碼的字串可以指定為服務 Settings.xml 中的加密 參數 ,或作為服務 ServiceManifest.xml中的加密 環境變數 。
在服務的 Settings.xml 設定檔中指定一個加密 參數 ,屬性 IsEncrypted 設為 true:
<?xml version="1.0" encoding="utf-8" ?>
<Settings xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2011/01/fabric">
<Section Name="MySettings">
<Parameter Name="MySecret" IsEncrypted="true" Value="I6jCCAeYCAxgFhBXABFxzAt ... gNBRyeWFXl2VydmjZNwJIM=" />
</Section>
</Settings>
在服務的 ServiceManifest.xml 檔案中指定一個加密 的環境變數 ,屬性 Type 設為 Encrypted:
<CodePackage Name="Code" Version="1.0.0">
<EnvironmentVariables>
<EnvironmentVariable Name="MyEnvVariable" Type="Encrypted" Value="I6jCCAeYCAxgFhBXABFxzAt ... gNBRyeWFXl2VydmjZNwJIM=" />
</EnvironmentVariables>
</CodePackage>
這些秘密也應該包含在您的 Service Fabric 應用程式中,透過在應用程式清單中指定憑證。 在 ApplicationManifest.xml 中加入 SecretsCertificate 元素,並附上所需憑證的指紋。
<ApplicationManifest … >
...
<Certificates>
<SecretsCertificate Name="MyCert" X509FindType="FindByThumbprint" X509FindValue="[YourCertThumbrint]"/>
</Certificates>
</ApplicationManifest>
備註
當啟用指定 SecretsCertificate 的應用程式時,Service Fabric 將會找出相符的憑證,並將正在以完整權限執行應用程式的身分識別授與憑證的私人金鑰。 Service Fabric 也會監控憑證是否有變更,並相應地重新套用權限。 為了偵測以公共名稱宣告的憑證變更,Service Fabric 會執行一個定期任務,尋找所有相符的憑證,並與快取的拇指指紋清單進行比較。 當偵測到新的指紋時,表示該主體的證書已被續期。 該任務在叢集的每個節點每分鐘執行一次。
雖然 SecretsCertificate 的確允許主體型宣告,但請注意,加密設定是繫結到用來加密用戶端設定的金鑰對。 你必須確保原始加密憑證(或類似的憑證)與主體宣告相符,並且它(包括對應的私鑰)安裝在叢集中每個可能承載該應用程式的節點上。 所有符合主體型宣告且從與原始加密憑證相同的金鑰對建置的時間有效憑證,皆視為等效項目。
將應用程式機密資料注入應用程式實例
理想情況下,部署到不同環境應該盡可能自動化。 這可以透過在建置環境中執行秘密加密,並在建立應用程式實例時提供加密的秘密作為參數來達成。
在 Settings.xml 中使用可覆寫參數
Settings.xml 設定檔允許在應用程式建立時提供可覆寫的參數。 使用屬性 MustOverride 代替參數值:
<?xml version="1.0" encoding="utf-8" ?>
<Settings xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2011/01/fabric">
<Section Name="MySettings">
<Parameter Name="MySecret" IsEncrypted="true" Value="" MustOverride="true" />
</Section>
</Settings>
若要覆蓋 Settings.xml中的值,請宣告服務的覆蓋參數 ApplicationManifest.xml:
<ApplicationManifest ... >
<Parameters>
<Parameter Name="MySecret" DefaultValue="" />
</Parameters>
<ServiceManifestImport>
<ServiceManifestRef ServiceManifestName="Stateful1Pkg" ServiceManifestVersion="1.0.0" />
<ConfigOverrides>
<ConfigOverride Name="Config">
<Settings>
<Section Name="MySettings">
<Parameter Name="MySecret" Value="[MySecret]" IsEncrypted="true" />
</Section>
</Settings>
</ConfigOverride>
</ConfigOverrides>
</ServiceManifestImport>
現在,當建立應用程式實例時,可以指定該值作為 應用程式參數 。 建立應用程式實例可以用 PowerShell 撰寫腳本,或用 C# 撰寫,方便整合進建置流程。
使用 PowerShell 時,參數以New-ServiceFabricApplication的形式提供給指令:
New-ServiceFabricApplication -ApplicationName fabric:/MyApp -ApplicationTypeName MyAppType -ApplicationTypeVersion 1.0.0 -ApplicationParameter @{"MySecret" = "I6jCCAeYCAxgFhBXABFxzAt ... gNBRyeWFXl2VydmjZNwJIM="}
使用 C# 時,應用程式參數會以 ApplicationDescriptionNameValueCollection: 形式指定:
FabricClient fabricClient = new FabricClient();
NameValueCollection applicationParameters = new NameValueCollection();
applicationParameters["MySecret"] = "I6jCCAeYCAxgFhBXABFxzAt ... gNBRyeWFXl2VydmjZNwJIM=";
ApplicationDescription applicationDescription = new ApplicationDescription(
applicationName: new Uri("fabric:/MyApp"),
applicationTypeName: "MyAppType",
applicationTypeVersion: "1.0.0",
applicationParameters: applicationParameters)
);
await fabricClient.ApplicationManager.CreateApplicationAsync(applicationDescription);
從服務代碼中解密加密的秘密
用於存取 參數 與 環境變數 的 API 允許輕鬆解密加密值。 由於加密字串包含用於加密的憑證資訊,你不需要手動指定憑證。 憑證只需要安裝在服務執行的節點上即可。
// Access decrypted parameters from Settings.xml
ConfigurationPackage configPackage = FabricRuntime.GetActivationContext().GetConfigurationPackageObject("Config");
bool MySecretIsEncrypted = configPackage.Settings.Sections["MySettings"].Parameters["MySecret"].IsEncrypted;
if (MySecretIsEncrypted)
{
SecureString MySecretDecryptedValue = configPackage.Settings.Sections["MySettings"].Parameters["MySecret"].DecryptValue();
}
// Access decrypted environment variables from ServiceManifest.xml
// Note: you do not have to call any explicit API to decrypt the environment variable.
string MyEnvVariable = Environment.GetEnvironmentVariable("MyEnvVariable");
下一步
- Service Fabric 秘密商店
- 了解更多關於應用程式與服務安全的資訊