产品战略专家梁宁确认出席AICon北京站,分享AI时代下的商业逻辑与产品需求 了解详情
写点什么

AWS KMS 实现跨租户的安全数据加密(二)

  • 2019-12-26
  • 本文字数:4809 字

    阅读完需:约 16 分钟

AWS KMS 实现跨租户的安全数据加密(二)

3 配置 Cognito identity pool 及 IAM 角色

3.1 创建 Identity pool 及 IAM role

3.2 配置 Developer provider name

3.3 创建 IAM 角色

利用 identity pool 生成的 身份证书管理 CMK(权限已在 CMK 管理中指定),删除其他的权限


4 实现 Cognito Developer provider

Java


/*  *   *  Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. *   *  Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except *  in compliance with the License. A copy of the License is located at *   *  http://aws.amazon.com/apache2.0 *   *  or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *  specific language governing permissions and limitations under the License. *    */
package com.amazon.saas.idp;
import java.util.Collections;import java.util.Map;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;import com.amazonaws.services.cognitoidentity.AmazonCognitoIdentity;import com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClientBuilder;import com.amazonaws.services.cognitoidentity.model.GetCredentialsForIdentityRequest;import com.amazonaws.services.cognitoidentity.model.GetCredentialsForIdentityResult;import com.amazonaws.services.cognitoidentity.model.GetOpenIdTokenForDeveloperIdentityRequest;import com.amazonaws.services.cognitoidentity.model.GetOpenIdTokenForDeveloperIdentityResult;
public class CognitoDeveloperIdentityProvider { private String region = "cn-north-1"; //cognito 对于 sts 的 provider id 中国区选择 "cognito-identity.cn-north-1.amazonaws.com.cn" private String cognitoProviderId= "cognito-identity.cn-north-1.amazonaws.com.cn"
// 前面配置的 Developer Identity Provider Name private String developerIdentityProviderName = "developerIdentityProviderName"; // indentity pool id 如 cn-north-1:cc310c44-de78-4661-8e6e-8cc21d974058
private String identityPoolId="cn-north-1:cc310c44-de78-4661-8e6e-8cc21d974058";
private AmazonCognitoIdentity amazonCognitoIdentity; // 构建 AmazonCognitoIdentityClient public void init() { AmazonCognitoIdentityClientBuilder cidBuilder = AmazonCognitoIdentityClientBuilder.standard(); cidBuilder.setRegion(region); cidBuilder.setCredentials(DefaultAWSCredentialsProviderChain.getInstance()); amazonCognitoIdentity = cidBuilder.build(); } //获取 cognito OpenIdToken public GetOpenIdTokenForDeveloperIdentityResult getOpenIdTokenFromCognito(String tenantid, String userid) { GetOpenIdTokenForDeveloperIdentityRequest request = new GetOpenIdTokenForDeveloperIdentityRequest(); request.setIdentityPoolId(identityPoolId); Map<String, String> logins = Collections.singletonMap(developerIdentityProviderName, tenantid + ":" + userid); request.setLogins(logins); GetOpenIdTokenForDeveloperIdentityResult result = amazonCognitoIdentity .getOpenIdTokenForDeveloperIdentity(request); return result; } //获取 aws credentials 用于调用 KMS public GetCredentialsForIdentityResult getCredentialsForIdentity(String identityid, String token, TenantUserInfo userinfo) { GetCredentialsForIdentityRequest request = new GetCredentialsForIdentityRequest(); request.setIdentityId(identityid); request.setLogins(Collections.singletonMap(cognitoProviderId, token)); GetCredentialsForIdentityResult result = amazonCognitoIdentity.getCredentialsForIdentity(request); return result; }
}
复制代码

5 实现数据加密解密

5.1 AWS 证书与 data key 缓存

Java


/*  *   *    Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. *     *    Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except *    in compliance with the License. A copy of the License is located at *     *    http://aws.amazon.com/apache2.0 *     *    or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *    specific language governing permissions and limitations under the License. *    */
package com.amazon.saas.tes;
import java.util.concurrent.TimeUnit;
import com.amazon.saas.tes.TenantCryptoMaterialsManagerHolder.Cachekey;import com.amazonaws.auth.BasicSessionCredentials;import com.amazonaws.encryptionsdk.CryptoMaterialsManager;import com.amazonaws.encryptionsdk.MasterKeyProvider;import com.amazonaws.encryptionsdk.caching.CachingCryptoMaterialsManager;import com.amazonaws.encryptionsdk.caching.CryptoMaterialsCache;import com.amazonaws.encryptionsdk.caching.LocalCryptoMaterialsCache;import com.amazonaws.encryptionsdk.kms.KmsMasterKey;import com.amazonaws.encryptionsdk.kms.KmsMasterKeyProvider;import com.google.common.cache.CacheBuilder;import com.google.common.cache.CacheLoader;import com.google.common.cache.LoadingCache;import lombok.AllArgsConstructor;import lombok.EqualsAndHashCode;import lombok.Getter;import lombok.Setter;public class TenantCryptoMaterialsManagerHolder extends CacheLoader<Cachekey, CryptoMaterialsManager> { @Getter @Setter @AllArgsConstructor public static class Cachekey { private String tenantID; private String userid; } @Data public static class Config { private String keyArn; private int maxCacheSize; private int maxEntryAge; private int credentialsDuration;
} private LoadingCache<Cachekey, CryptoMaterialsManager> cache; private Config config; public void init() { //配置缓存参数 config=new Config(); cache = CacheBuilder.newBuilder().maximumSize(10000) .expireAfterWrite(config.getCredentialsDuration(), TimeUnit.MINUTES).build(this); }
public CryptoMaterialsManager createMaterialsManager(Cachekey key) { Credentials credentialsForIdentity = cognitoDeveloperIdentityProviderClient;

MasterKeyProvider<KmsMasterKey> keyProvider = KmsMasterKeyProvider.builder() .withKeysForEncryption(config.getKeyArn()) .withCredentials(new BasicSessionCredentials(credentialsForIdentity.getAccessKeyId(), credentialsForIdentity.getSecretKey(), credentialsForIdentity.getSessionToken())) .build();

int MAX_CACHE_SIZE = config.getMaxCacheSize(); CryptoMaterialsCache cache = new LocalCryptoMaterialsCache(MAX_CACHE_SIZE);
int MAX_ENTRY_AGE_SECONDS = config.getMaxEntryAge(); int MAX_ENTRY_MSGS = config.getMaxCacheSize();

CryptoMaterialsManager cachingCmm = CachingCryptoMaterialsManager.newBuilder() .withMasterKeyProvider(keyProvider).withCache(cache).withMaxAge(MAX_ENTRY_AGE_SECONDS, TimeUnit.SECONDS) .withMessageUseLimit(MAX_ENTRY_MSGS).build(); return cachingCmm; }
public CryptoMaterialsManager getMaterialsManager(TenantUserInfo userinfo) { return cache.get(new Cachekey(userinfo.getTenantID(), userinfo.getUserid())); }
@Override public CryptoMaterialsManager load(Cachekey key) throws Exception { return createMaterialsManager(key); }
}
复制代码

5.2 数据加密解密

Java


/*  *   *    Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. *     *    Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except *    in compliance with the License. A copy of the License is located at *     *    http://aws.amazon.com/apache2.0 *     *    or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *    specific language governing permissions and limitations under the License. *    */
package com.amazon.saas.tes;
import java.util.Collections;
import com.amazonaws.encryptionsdk.AwsCrypto;import com.amazonaws.encryptionsdk.CryptoMaterialsManager;import com.amazonaws.encryptionsdk.CryptoResult;
import org.springframework.beans.factory.annotation.Autowired;
public class EncDecService { private final AwsCrypto crypto = new AwsCrypto(); @Autowired TenantCryptoMaterialsManagerHolder tenantCryptoMaterialsManagerHolder;
public TESEncryptedMessage encrypt(String plaintext, String authorizationHeader, TenantUserInfo userinfo) { CryptoMaterialsManager cmm = tenantCryptoMaterialsManagerHolder.getMaterialsManager(authorizationHeader, userinfo); String message = crypto.encryptString(cmm, plaintext, Collections.singletonMap("tenantid.userid", userinfo.getTenantID() + "." + userinfo.getUserid())) .getResult(); TESEncryptedMessage re = new TESEncryptedMessage(); re.setEncyptedMsg(message); return re; }
public TesPlainText dectypt(String encryptMessage, String authorizationHeader, TenantUserInfo userinfo) { CryptoMaterialsManager cmm = tenantCryptoMaterialsManagerHolder.getMaterialsManager(authorizationHeader, userinfo); CryptoResult<String, ?> decryptResult = crypto.decryptString(cmm, encryptMessage); TesPlainText re = new TesPlainText(); re.setText(decryptResult.getResult()); return re;
}
复制代码


作者介绍:


*


!



### [](https://amazonaws-china.com/cn/blogs/china/tag/%E4%BB%BB%E8%80%80%E6%B4%B2/)
AWS解决方案架构师,负责企业客户应用在AWS的架构咨询和 设计。在微服务架构设计、数据库等领域有丰富的经验
复制代码


本文转载自 AWS 技术博客。


原文链接:https://amazonaws-china.com/cn/blogs/china/aws-kms-enables-secure-data-encryption-across-tenants/


2019-12-26 13:47726

评论

发布
暂无评论
发现更多内容

M1电脑运行Windows10弹出“内部版本已过期”的解决方法

Rose

pd虚拟机 M1电脑 Windows内部版本已经过期

Sovit3D平台快速构建智慧渔业三维可视化养殖管理系统

2D3D前端可视化开发

智慧渔业 智慧渔场 智慧水产养殖 数字渔业 渔业数字孪生

详解神经网络中反向传播和梯度下降

华为云开发者联盟

人工智能 神经网络 华为云 华为云开发者联盟 企业号 3 月 PK 榜

你代码的异味是故意的还是不小心?是故意的!

禅道项目管理

React等前端框架如何与小程序结合

Onegun

前端 前端框架 React Vue 3

FDF循环互助游戏系统开发智能合约搭建

薇電13242772558

智能合约

用户分享 | 达梦第三方客户端DockQuery使用体会

BinTools图尔兹

数据库 用户体验 国产数据库工具

京东云RASP云原生安全免疫创新实践

京东科技开发者

Web 安全 漏洞 业务安全 企业号 3 月 PK 榜

数据测试实践:从一个bug开始的大数据引擎兼容性探索

京东科技开发者

大数据 bug修复 引擎 测试数据构造 企业号 3 月 PK 榜

解决mac电脑打开应用“意外退出”的问题 (点按“重新打开”以再次打开应用程序)

理理

PHPStorm 意外退出 mac电脑

探索 Pixelmator Pro 3新功能——AI智能模板

Rose

Pixelmator Pro Mac修图软件

技术沙龙 | 探索软件测试前沿技术及最佳实践,体验ChatGPT在测试领域中的应用

测试人

软件测试 沙龙 ChatGPT

小程序技术如何提升企业的移动研发效率?

FinFish

降本增效 小程序容器 移动研发 小程序技术

适用于 Apple Silicon (M1芯片)的 Photoshop常见问题及解决方案

理理

PhotoShop PS常见问题

告别数据开发中的人工审核!火山引擎DataLeap落地“自动校验开发规范”能力

字节跳动数据平台

大数据 数据治理 数据研发 企业号 3 月 PK 榜

Dubbo Triple 协议

昵称不能为null

dubbo RPC triple协议

ins视频保姆级图文教程,快学起来!

frank

取得成功的 13 个方法

宇宙之一粟

个人成长 翻译 成功

云智慧助力中国信通院组装式应用开发平台系列标准建设

云智慧AIOps社区

跨端技术或许是提升软件运维效率的利器

FinFish

小程序化 小程序技术 高效运维 软件运维

精选案例 | 博睿数据30w+监测节点护航新华网、人民网两会重保工作

博睿数据

可观测性 智能运维 博睿数据 精选案例 主动式拨测

TypeScript 与 JavaScript:你应该知道的区别

京东科技开发者

JavaScript typescript 前端 后端 企业号 3 月 PK 榜

Higress on K8s 5分钟开箱即用

阿里巴巴中间件

阿里云 云原生 Higress

R-Drop论文复现与理论讲解

华为云开发者联盟

人工智能 华为云 深度神经网络 华为云开发者联盟 企业号 3 月 PK 榜

量化合约对冲交易app系统开发源代码

开发微hkkf5566

OceanBase 生态产品:时序数据库CeresDB 正式发布 1.0 版本

OceanBase 数据库

数据库 oceanbase

探索以小程序提升运维效率

Onegun

运维 小程序容器

聊聊线上发布这件事

老张

软件测试 权限管理 服务部署

融云入选中国信通院《高质量数字化转型产品及服务全景图》

融云 RongCloud

产品 数字化 通讯

Flink Table Store 0.3 构建流式数仓最佳实践

Apache Flink

大数据 flink 实时计算

Vineyard 论文被 SIGMOD'2023 接收,助力计算引擎之间高效数据交换

阿里巴巴中间件

阿里云 计算引擎

AWS KMS 实现跨租户的安全数据加密(二)_语言 & 开发_亚马逊云科技 (Amazon Web Services)_InfoQ精选文章