使用rbac Service Principal进行身份验证会导致403听众验证失败。受众群体不匹配

时间:2019-04-05 08:43:36

标签: java azure-storage-blobs adal azure-cli adal4j

使用rbac Service Principal进行身份验证会导致403听众验证失败。受众群体不匹配

我正在尝试授予应用程序对Blob存储容器的访问权限,但具有最低限度的必需权限(读/写)

我使用天蓝色cli进行了以下操作来尝试此操作:

az group create -l ${LOCATION} -n ${RESOURCE_GROUP_NAME}

az role definition create --role-definition rw-blob-role.json

rw-blob-role.json:
{
  "assignableScopes": [
    "/subscriptions/{{SUBSCRIPTION_ID}}"
  ],
  "description": "Custom role to allow for read and write access to Azure Storage blob containers and data",
  "name": "{{APP_RW_ROLE_NAME}}",
  "permissions": [
    {
      "actions": [
        "Microsoft.Storage/storageAccounts/blobServices/containers/read",
        "Microsoft.Storage/storageAccounts/blobServices/containers/write"
      ],
      "dataActions": [
        "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/read",
        "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/write"
      ],
      "notActions": [],
      "notDataActions": []
    }
  ],
  "type": "Microsoft.Authorization/roleDefinitions"
}


az ad sp create-for-rbac --name ${AZ_SERVICE_PRINCIPAL_NAME} --password ${APP_CLIENT_SECRET}

az role assignment delete --assignee ${AZ_SERVICE_PRINCIPAL_NAME} --role Contributor

az role assignment create --assignee ${AZ_SERVICE_PRINCIPAL_NAME} --role ${APP_RW_ROLE_NAME}

az storage account create --name ${STORAGE_ACCOUNT_NAME} --resource-group ${RESOURCE_GROUP_NAME} --location ${LOCATION} --kind BlobStorage --sku ${STORAGE_ACCOUNT_SKU} --access-tier ${STORAGE_ACCOUNT_ACCESS_TIER}

az storage container create --name ${BLOB_STORAGE_CONTAINER} --account-name ${STORAGE_ACCOUNT_NAME} --public-access off

由此,我保存以下属性以供我的应用程序使用:
 -TENANT_ID =“ $(az帐户显示--output tsv --query tenantId)”
 -CLIENT_ID =“ $(az ad sp list --spn $ {AZ_SERVICE_PRINCIPAL_NAME} --output tsv --query [0] .appId)”
 -客户机密:$ {APP_CLIENT_SECRET}
 -资源:$ {AZ_SERVICE_PRINCIPAL_NAME}
 -存储帐户名称:$ {STORAGE_ACCOUNT_NAME}
 -容器名称:$ {BLOB_STORAGE_CONTAINER}

我使用com.microsoft.azure:adal4j获取令牌:

public AuthenticationResult getToken() {
    ExecutorService service = Executors.newFixedThreadPool(1);
    ClientCredential credential = new ClientCredential(CLIENT_ID, APP_CLIENT_SECRET);
    String authorityTenantUrl = String.format(https://login.microsoftonline.com/%s/oauth2/token, TENANT_ID);

    AuthenticationContext context;
    AuthenticationResult result;
    try {
      context = new AuthenticationContext(authorityTenantUrl, true, service);
      Future<AuthenticationResult> future = context.acquireToken(AZ_SERVICE_PRINCIPAL_NAME, credential, null);
      result = future.get();
    } catch (Exception e) {
      throw new RuntimeException(e);
    } finally {
      service.shutdown();
    }

    if (result == null) {
      throw new RuntimeException("authentication result was null");
    }

    return result;
}

使用AuthenticationResult和com.microsoft.azure:azure-storage-blob中的accessToken,我尝试获取一个blob:

TokenCredentials credential = new TokenCredentials(accessToken);
HttpPipeline pipeline = StorageURL.createPipeline(credentials, new PipelineOptions());
ServiceURL serviceURL = new ServiceURL("https://STORAGE_ACCOUNT_NAME.blob.core.windows.net", pipeline);
ContainerURL containerURL = serviceURL.createContainerURL(BLOB_STORAGE_CONTAINER);
BlockBlobURL blobURL = containerURL.createBlockBlobURL(identifier);
ByteBuffer byteBuffer = FlowableUtil.collectBytesInBuffer(blobURL.download().blockingGet().body(new ReliableDownloadOptions())).blockingGet();

这会导致StorageException:

<Error>
  <Code>AuthenticationFailed</Code>
  <Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.</Message>
  <AuthenticationErrorDetail>Audience validation failed. Audience did not match.</AuthenticationErrorDetail>
</Error>

我很确定问题是AZ_SERVICE_PRINCIPAL_NAME传递给了acquireToken()上的adal4j,但我不知道正确的值是什么。我已尝试在租户AD和服务主体上使用CLIENT_ID和其他属性。

1 个答案:

答案 0 :(得分:1)

您应该使用https://storage.azure.com/作为Resource的值。