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

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

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

解决方法

在 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,然后使用上面的用户交互方式,就可以了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-