【问题标题】:Authenticate storage account with service principal使用服务主体对存储帐户进行身份验证
【发布时间】:2019-12-17 05:13:00
【问题描述】:

在 java 中,我想使用服务主体(首选使用客户端证书)进行身份验证,以从 blobstorage 写入/读取文件。

我开始使用 StorageAccount 和访问密钥,但我需要更细粒度的权限控制(例如仅限于读取或写入)。

我在 azure 文档中找不到任何关于如何执行此操作的示例,代码中也没有任何入口点。

【问题讨论】:

    标签: java azure azure-blob-storage


    【解决方案1】:

    根据我的研究,Azure 不提供仅对 Blob 存储资源具有读/写权限的 build-in role。所以我们需要根据您的需要create a custom role。然后,您可以将角色分配给服务主体。关于 Gow创建自定义角色,请参考以下步骤

    1. 定义一个角色json

      {
        "Name": "Azure blob Writer",
        "Id": null,
        "IsCustom": true,
        "Description": "Read and write blob",
        "Actions": [
          "Microsoft.Storage/storageAccounts/blobServices/containers/read", 
          "Microsoft.Storage/storageAccounts/blobServices/containers/write", 
          "Microsoft.Storage/storageAccounts/blobServices/generateUserDelegationKey/action"
        ],
        "DataActions": [
          "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/read", 
           "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/write"
        ],
        "NotActions": [],
        "AssignableScopes": [
          "/subscriptions/<subscription id>"
        ]
      
      }
      
      1. 使用 PowerShell 创建自定义角色

        New-AzureRmRoleDefinition -InputFile "你的 JSON 文件的路径"

    此外,如果您使用访问密钥,您可以使用帐户密钥生成 SAS toekn。 SAS toekn 可以根据需要配置权限。关于如何创建 SAS 令牌,请参考以下代码。

    SharedKeyCredentials credential = new SharedKeyCredentials(accountName, accountKey);
    
            // This is the name of the container and blob that we're creating a SAS to.
            String containerName = "mycontainer"; // Container names require lowercase.
            String blobName = "HelloWorld.txt"; // Blob names can be mixed case.
            String snapshotId = "2018-01-01T00:00:00.0000000Z"; // SAS can be restricted to a specific snapshot
    
            /*
            Set the desired SAS signature values and sign them with the shared key credentials to get the SAS query
            parameters.
             */
            ServiceSASSignatureValues values = new ServiceSASSignatureValues()
                    .withProtocol(SASProtocol.HTTPS_ONLY) // Users MUST use HTTPS (not HTTP).
                    .withExpiryTime(OffsetDateTime.now().plusDays(2)) // 2 days before expiration.
                    .withContainerName(containerName)
                    .withBlobName(blobName)
                    .withSnapshotId(snapshotId);
    
            /*
            To produce a container SAS (as opposed to a blob SAS), assign to Permissions using ContainerSASPermissions, and
            make sure the blobName and snapshotId fields are null (the default).
             */
            BlobSASPermission permission = new BlobSASPermission()
                    .withRead(true)
                    .withAdd(true)
                    .withWrite(true);
            values.withPermissions(permission.toString());
    
            SASQueryParameters params = values.generateSASQueryParameters(credential);
    

    更多deatis,可以参考sample

    【讨论】:

      猜你喜欢
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      • 2019-08-25
      相关资源
      最近更新 更多