共用方式為


連線到安全的叢集

當用戶端連線到 Service Fabric 叢集節點時,用戶端可以使用憑證安全性或 Microsoft Entra ID 來接受驗證及保護已建立的通訊。 此驗證可確保只有已獲授權的使用者可以存取叢集和已部署的應用程式,以及執行管理工作。 憑證或 Microsoft Entra 安全性必須在叢集建立之時即事先在叢集上啟用。 如需有關叢集安全性案例的詳細資訊,請參閱 叢集安全性。 如果您要連接到使用憑證保護的叢集,請在要連接到叢集的電腦上設定用戶端憑證

使用 Azure Service Fabric CLI (sfctl) 連線到安全的叢集

有幾個不同的方式可使用 Service Fabric CLI (sfctl) 來連線到安全的叢集。 使用用戶端憑證來進行驗證時,憑證詳細資料必須與部署至叢集節點的憑證相符。 如果您的憑證有「憑證授權單位」(CA),就必須額外指定信任的 CA。

您可以使用 sfctl cluster select 命令來連線到叢集。

指定用戶端憑證時,可以使用兩種不同的方式,以憑證與金鑰組方式指定,或是以單一 PFX 檔案方式指定。 針對受密碼保護的 PEM 檔案,系統會自動提示您輸入密碼。 如果您已取得用戶端憑證作為 PFX 檔案,先使用下列命令,將 PFX 檔案轉換為 PEM 檔案。

openssl pkcs12 -in your-cert-file.pfx -out your-cert-file.pem -nodes -passin pass:your-pfx-password

如果您的 .pfx 檔案未受密碼保護,請針對最後一個參數使用 -passin pass:。

若要以 pem 檔案指定用戶端憑證,請在 --pem 引數中指定檔案路徑。 例如:

sfctl cluster select --endpoint https://testsecurecluster.com:19080 --pem ./client.pem

受密碼保護的 pem 檔案會在執行任何命令之前,先提示您輸入密碼。

為了指定憑證,金鑰組會使用 --cert--key 引數來指定每個個別檔案的檔案路徑。

sfctl cluster select --endpoint https://testsecurecluster.com:19080 --cert ./client.crt --key ./keyfile.key

有時,用來保護測試或開發叢集的憑證會讓憑證驗證失敗。 若要略過憑證驗證,請指定 --no-verify 選項。 例如:

警告

連線到生產環境 Service Fabric 叢集時,請勿使用 no-verify 選項。

sfctl cluster select --endpoint https://testsecurecluster.com:19080 --pem ./client.pem --no-verify

此外,您可以指定受信任 CA 憑證,或個別憑證的目錄路徑。 若要指定這些路徑,請使用 --ca 引數。 例如:

sfctl cluster select --endpoint https://testsecurecluster.com:19080 --pem ./client.pem --ca ./trusted_ca

連線之後,您應該能夠執行其他 sfctl 命令來與叢集互動。

使用 PowerShell 來連線到叢集

在您透過 PowerShell 執行叢集上的作業之前,先建立叢集的連接。 叢集連接適用於指定 PowerShell 工作階段中的所有後續命令。

連線到不安全的叢集

若要連接至不安全的叢集,請提供叢集端點位址至 Connect-ServiceFabricCluster 命令︰

Connect-ServiceFabricCluster -ConnectionEndpoint <Cluster FQDN>:19000 

使用 Microsoft Entra ID 來連線到安全的叢集

若要連接至使用 Microsoft Entra ID 來授權叢集系統管理員存取權的安全叢集,請提供叢集憑證指紋,並使用 AzureActiveDirectory 旗標。

Connect-ServiceFabricCluster -ConnectionEndpoint <Cluster FQDN>:19000 `
-ServerCertThumbprint <Server Certificate Thumbprint> `
-AzureActiveDirectory

使用用戶端憑證連線到安全的叢集

執行下列 PowerShell 命令來連線至使用用戶端憑證授權系統管理員存取權的安全叢集。

使用憑證一般名稱的連線

提供叢集憑證一般名稱,以及已授與權限來管理叢集的用戶端憑證一般名稱。 憑證詳細資料必須與叢集節點上的憑證相符。

Connect-serviceFabricCluster -ConnectionEndpoint $ClusterName -KeepAliveIntervalInSec 10 `
    -X509Credential `
    -ServerCommonName <certificate common name>  `
    -FindType FindBySubjectName `
    -FindValue <certificate common name> `
    -StoreLocation CurrentUser `
    -StoreName My 

ServerCommonName 是安裝在叢集節點上的伺服器憑證的一般名稱。 FindValue 是系統管理員用戶端憑證的一般名稱。 填入參數後,命令看起來如下列範例所示︰

$ClusterName= "sf-commonnametest-scus.southcentralus.cloudapp.azure.com:19000"
$certCN = "sfrpe2eetest.southcentralus.cloudapp.azure.com"

Connect-serviceFabricCluster -ConnectionEndpoint $ClusterName -KeepAliveIntervalInSec 10 `
    -X509Credential `
    -ServerCommonName $certCN  `
    -FindType FindBySubjectName `
    -FindValue $certCN `
    -StoreLocation CurrentUser `
    -StoreName My 

使用憑證指紋連線

提供叢集憑證指紋,以及已授與權限來管理叢集的用戶端憑證指紋。 憑證詳細資料必須與叢集節點上的憑證相符。

Connect-ServiceFabricCluster -ConnectionEndpoint <Cluster FQDN>:19000 `  
          -KeepAliveIntervalInSec 10 `  
          -X509Credential -ServerCertThumbprint <Certificate Thumbprint> `  
          -FindType FindByThumbprint -FindValue <Certificate Thumbprint> `  
          -StoreLocation CurrentUser -StoreName My

ServerCertThumbprint 是安裝在叢集節點上的伺服器憑證指紋。 FindValue 是系統管理員用戶端憑證的指紋。 填入參數後,命令看起來如下列範例所示︰

Connect-ServiceFabricCluster -ConnectionEndpoint clustername.westus.cloudapp.azure.com:19000 `  
          -KeepAliveIntervalInSec 10 `  
          -X509Credential -ServerCertThumbprint A8136758F4AB8962AF2BF3F27921BE1DF67F4326 `  
          -FindType FindByThumbprint -FindValue 71DE04467C9ED0544D021098BCD44C71E183414E `  
          -StoreLocation CurrentUser -StoreName My 

使用 Windows Active Directory 連線到安全的叢集

如果您的獨立叢集是使用 AD 安全性部署,可加上 "WindowsCredential" 參數來連接到叢集。

Connect-ServiceFabricCluster -ConnectionEndpoint <Cluster FQDN>:19000 `
          -WindowsCredential

使用 FabricClient API 來連線到叢集

Service Fabric SDK 會提供叢集管理的 FabricClient 類別。 若要使用 FabricClient API,請取得 Microsoft.ServiceFabric NuGet 封裝。

連線到不安全的叢集

若要連接至遠端不安全的叢集,建立 FabricClient 執行個體,並提供叢集位址︰

FabricClient fabricClient = new FabricClient("clustername.westus.cloudapp.azure.com:19000");

針對在叢集 (例如,Reliable Service) 內執行的程式碼,建立 FabricClient 且不指定叢集位址。 FabricClient 連接到目前正在執行程式碼之節點上的本機管理閘道,避免額外的網路躍點。

FabricClient fabricClient = new FabricClient();

使用用戶端憑證連線到安全的叢集

叢集中的節點必須具備有效的憑證,這些憑證在 SAN 中的通用名稱或 DNS 名稱會出現在於 FabricClient 上設定的 RemoteCommonNames 屬性中。 遵循此程序,就可讓用戶端與叢集節點之間進行相互驗證。

using System.Fabric;
using System.Security.Cryptography.X509Certificates;

string clientCertThumb = "71DE04467C9ED0544D021098BCD44C71E183414E";
string serverCertThumb = "A8136758F4AB8962AF2BF3F27921BE1DF67F4326";
string CommonName = "www.clustername.westus.azure.com";
string connection = "clustername.westus.cloudapp.azure.com:19000";

var xc = GetCredentials(clientCertThumb, serverCertThumb, CommonName);
var fc = new FabricClient(xc, connection);

try
{
    var ret = fc.ClusterManager.GetClusterManifestAsync().Result;
    Console.WriteLine(ret.ToString());
}
catch (Exception e)
{
    Console.WriteLine("Connect failed: {0}", e.Message);
}

static X509Credentials GetCredentials(string clientCertThumb, string serverCertThumb, string name)
{
    X509Credentials xc = new X509Credentials();
    xc.StoreLocation = StoreLocation.CurrentUser;
    xc.StoreName = "My";
    xc.FindType = X509FindType.FindByThumbprint;
    xc.FindValue = clientCertThumb;
    xc.RemoteCommonNames.Add(name);
    xc.RemoteCertThumbprints.Add(serverCertThumb);
    xc.ProtectionLevel = ProtectionLevel.EncryptAndSign;
    return xc;
}

使用 Microsoft Entra ID 以互動方式連線到安全的叢集

以下範例針對用戶端身分識別使用 Microsoft Entra ID,以及針對伺服器身分識別啟用伺服器憑證。

連線到叢集時,就會自動彈出對話方塊視窗,以供互動式登入。

string serverCertThumb = "A8136758F4AB8962AF2BF3F27921BE1DF67F4326";
string connection = "clustername.westus.cloudapp.azure.com:19000";

var claimsCredentials = new ClaimsCredentials();
claimsCredentials.ServerThumbprints.Add(serverCertThumb);

var fc = new FabricClient(claimsCredentials, connection);

try
{
    var ret = fc.ClusterManager.GetClusterManifestAsync().Result;
    Console.WriteLine(ret.ToString());
}
catch (Exception e)
{
    Console.WriteLine("Connect failed: {0}", e.Message);
}

使用 Microsoft Entra ID 以非互動方式連線到安全的叢集

以下範例依存於 Microsoft.Identity.Client,版本:4.37.0。

如需取得 Microsoft Entra 權杖的詳細資訊,請參閱 Microsoft.Identity.Client

string tenantId = "C15CFCEA-02C1-40DC-8466-FBD0EE0B05D2";
string clientApplicationId = "118473C2-7619-46E3-A8E4-6DA8D5F56E12";
string webApplicationId = "53E6948C-0897-4DA6-B26A-EE2A38A690B4";
string[] scopes = new string[] { "user.read" };

var pca = PublicClientApplicationBuilder.Create(clientApplicationId)
    .WithAuthority($"https://login.microsoftonline.com/{tenantId}")
    .WithRedirectUri("urn:ietf:wg:oauth:2.0:oob")
    .Build();

var accounts = await pca.GetAccountsAsync();
var result = await pca.AcquireTokenInteractive(scopes)
    .WithAccount(accounts.FirstOrDefault())
    .ExecuteAsync();

string token = result.AccessToken;

string serverCertThumb = "A8136758F4AB8962AF2BF3F27921BE1DF67F4326";
string connection = "clustername.westus.cloudapp.azure.com:19000";

var claimsCredentials = new ClaimsCredentials();
claimsCredentials.ServerThumbprints.Add(serverCertThumb);
claimsCredentials.LocalClaims = token;

var fc = new FabricClient(claimsCredentials, connection);

try
{
    var ret = fc.ClusterManager.GetClusterManifestAsync().Result;
    Console.WriteLine(ret.ToString());
}
catch (Exception e)
{
    Console.WriteLine("Connect failed: {0}", e.Message);
}

使用 Microsoft Entra ID 連線到安全的叢集而不需事先知道中繼資料

以下範例使用非互動權杖取得,但可以使用同樣的方法來建立自訂的互動式權杖取得經驗。 權杖取得所需的 Microsoft Entra 中繼資料是從叢集設定來讀取。

string serverCertThumb = "A8136758F4AB8962AF2BF3F27921BE1DF67F4326";
string connection = "clustername.westus.cloudapp.azure.com:19000";

var claimsCredentials = new ClaimsCredentials();
claimsCredentials.ServerThumbprints.Add(serverCertThumb);

var fc = new FabricClient(claimsCredentials, connection);

fc.ClaimsRetrieval += async (o, e) =>
{
    var accounts = await PublicClientApplicationBuilder
        .Create("<client_id>")
        .WithAuthority(AzureCloudInstance.AzurePublic, "<tenant_id>")
        .WithRedirectUri("<redirect_uri>")
        .Build()
        .GetAccountsAsync();

    var result = await PublicClientApplicationBuilder
        .Create("<client_id>")
        .WithAuthority(AzureCloudInstance.AzurePublic, "<tenant_id>")
        .WithRedirectUri("<redirect_uri>")
        .Build()
        .AcquireTokenInteractive(new[] { "<scope>" })
        .WithAccount(accounts.FirstOrDefault())
        .ExecuteAsync();

    return result.AccessToken;
};

try
{
    var ret = fc.ClusterManager.GetClusterManifestAsync().Result;
    Console.WriteLine(ret.ToString());
}
catch (Exception e)
{
    Console.WriteLine("Connect failed: {0}", e.Message);
}

使用 Service Fabric Explorer 連線到安全的叢集

若要連線到指定之叢集的 Service Fabric Explorer,請將您的瀏覽器指向:

http://<your-cluster-endpoint>:19080/Explorer

Azure 入口網站的叢集基本資訊窗格中也會提供完整 URL。

如需使用瀏覽器連線到 Windows 或 OS X 上的安全叢集,您可以匯入用戶端憑證,然後瀏覽器將會提示您指定要用於連線到叢集所需的憑證。 在 Linux 機器上,則必須使用進階瀏覽器設定 (每種瀏覽器會有不同的機制) 來匯入憑證,並指向磁碟上的憑證位置。 如需詳細資訊,請參閱設定用戶端憑證

使用 Microsoft Entra ID 來連線到安全的叢集

若要連接至使用 Microsoft Entra ID 保護的叢集,請將您的瀏覽器指向:

https://<your-cluster-endpoint>:19080/Explorer

系統將會自動提示您使用 Microsoft Entra ID 來登入。

使用用戶端憑證連線到安全的叢集

若要連接至使用憑證保護的叢集,請將您的瀏覽器指向:

https://<your-cluster-endpoint>:19080/Explorer

系統會自動提示您選取用戶端憑證。

設定遠端電腦上的用戶端憑證

至少應使用兩個憑證保護叢集,一個是叢集和伺服器憑證,另一個用於用戶端存取。 建議您也使用額外的次要憑證和用戶端存取憑證。 若要使用憑證安全性來保護用戶端與叢集節點之間的通訊,您必須先取得並安裝用戶端憑證。 此憑證可以安裝到本機電腦或目前使用者的個人 (My) 存放區。 您也需要伺服器憑證的指紋,讓用戶端可以驗證叢集。

  • 在 Windows 上:按兩下 PFX 檔案,並依照提示在您的個人存放區中安裝憑證:Certificates - Current User\Personal\Certificates。 或者,您可以使用 PowerShell 命令:

    Import-PfxCertificate -Exportable -CertStoreLocation Cert:\CurrentUser\My `
            -FilePath C:\docDemo\certs\DocDemoClusterCert.pfx `
            -Password (ConvertTo-SecureString -String test -AsPlainText -Force)
    

    如果是自我簽署憑證,您必須先把它匯入您電腦的「受信任的人」存放區,才能使用此憑證來連線到安全的叢集。

    Import-PfxCertificate -Exportable -CertStoreLocation Cert:\CurrentUser\TrustedPeople `
    -FilePath C:\docDemo\certs\DocDemoClusterCert.pfx `
    -Password (ConvertTo-SecureString -String test -AsPlainText -Force)
    
  • 在 Mac 上:按兩下 PFX 檔案,並依照提示在您的 Keychain 中安裝憑證。

下一步