对在 PCF 中运行的容器使用托管标识来访问 Azure Blob 存储

时间:2021-03-08 21:31:08

标签: azure azure-blob-storage azure-managed-identity

我想知道是否可以通过使用托管标识在 Azure 中的 Pivotal Cloud Foundry 中运行的容器访问 blob 存储,例如系统分配的托管标识,或者我需要使用服务主体对象。任何帮助表示赞赏。 早些时候,我们通过在代码中编码 URL 来使用 SAS,但想要使用 Azure AD 对我们在容器内运行的 API 应用程序进行身份验证。那么实现这一目标的最佳方法是什么

1 个答案:

答案 0 :(得分:0)

在 Pivotal Cloud Foundry 中,您无法使用托管身份 (MSI) 进行身份验证,当使用 MSI 进行身份验证时,它本质上是对 azure 实例元数据端点进行 API 调用以获取访问令牌,然后使用令牌进行身份验证,它仅在 Azure environment support MSI 中可用。

在这种情况下,最佳做法是使用您提到的服务主体进行身份验证,请按照以下步骤操作。

1.Register an application with Azure AD and create a service principal.

2.Get values for signing increate a new application secret.

3. 导航到门户中的存储帐户 -> Access Control (IAM) -> 为服务主体分配 Storage Blob Data Contributor/Storage Blob Data Owner 角色,如下所示。

enter image description here

4.使用下面的java代码做一个快速测试,在我的示例中,我列出了一个容器中的所有blob,只是做其他事情取决于你的要求,用你从第2步得到的值替换。

pom.xml

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-identity</artifactId>
    <version>1.2.0</version>
</dependency>
<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-storage-blob</artifactId>
    <version>12.11.0-beta.1</version>
</dependency>

代码

import com.azure.identity.ClientSecretCredential;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.blob.models.BlobItem;

public  class vacate {
    public static void main(String[] args) {
        String clientId="xxxxxx";
        String clientSecret="xxxxxx";
        String tenantId="xxxxxx";

        ClientSecretCredential credential1 = new ClientSecretCredentialBuilder()
                .tenantId(tenantId)
                .clientId(clientId)
                .clientSecret(clientSecret)
                .build();

        BlobServiceClient storageClient = new BlobServiceClientBuilder()
                .endpoint("https://joystoragev2.blob.core.windows.net")
                .credential(credential1)
                .buildClient();

        BlobContainerClient containerClient = storageClient.getBlobContainerClient("tescon1");
        System.out.println("\nListing blobs...");

        for (BlobItem blobItem : containerClient.listBlobs()) {
            System.out.println("\t" + blobItem.getName());
        }
    }
}

enter image description here

有关详细信息,请参阅Quickstart: Manage blobs with Java v12 SDK

更新:

如果您想要一种用户交互方式进行身份验证,只需使用这些方式 - Authenticating Users 而不是 ClientSecretCredential

enter image description here

实际上 SDK 中不同的身份验证方式在 Azure AD 中使用不同的身份验证流程,例如ClientSecretCredential 使用 client credential flow,它是一种非交互方式,DeviceCodeCredential 使用 device code flow,它是用户交互方式。

要在您的案例中使用这些用户交互方式,您需要导航到门户中的 AD 应用程序 -> 添加 user_impersonation 的委派权限 Azure storage

enter image description here

enter image description here

<块引用>

假设一个特定用户应该只有对 Blob 端点的读取访问权限,而另一个用户可能对 Blob 端点具有完整的 CRUD 访问权限,当用户从前端应用程序调用此 API 时,这将如何确定?

是的,这可以通过上面添加的 delegated permission 来实现。当用户使用auth方式登录AD App时,App会让用户同意user_impersonation权限,同意后(用户的AAD租户需要让用户自己同意权限), AD App 会得到用户的许可,然后代表用户进行操作。总之,app的权限来自用户,不同的用户有不同的权限,那么app就会有不同的权限。

因此,在您的情况下,只需在存储帐户中添加具有不同角色的用户,如上述第 3 步。读权限加Storage Blob Data Reader,CURD权限加Storage Blob Data Contributor/Storage Blob Data Owner,然后使用上面的用户交互方式,就可以了。