写点什么

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:47754

评论

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

华为云CodeArts 12大安全防护机制,端到端全面保障软件供应链安全!

华为云PaaS服务小智

云计算 软件开发 华为云

Flink 任务调度策略:Lazy from Sources 深入解析

木南曌

实时计算

NumPy 数组排序、过滤与随机数生成详解

不在线第一只蜗牛

Python 数组 排序 Numpy

如何提高python程序代码的健壮性

我再BUG界嘎嘎乱杀

Python 编程 后端 软件开发

企业需要SD-WAN的十大理由

Ogcloud

SD-WAN 企业组网 SD-WAN组网 SD-WAN服务商 SDWAN

坚定投入核心软件!腾讯云数据库TDSQL荣获深圳市科技进步奖一等奖

Geek_2d6073

OpenAI“杀疯了”,GPT–4o模型保姆级使用教程!一遍就会!

快乐非自愿限量之名

openai GPT

基于向量检索服务与TextEmbedding实现语义搜索

DashVector

AI 向量检索 大模型 语义搜索

win版iSpring Suite (PowerPoint转Flash工具) v11.7.0 Build 5 (x64)激活版下载

iMac小白

Flink 任务调度策略:Eager 模式详解

木南曌

flink 实时计算

第52期|GPTSecurity周报

云起无垠

AIGC LLMs

关于接口协议,你必须要知道这些!

霍格沃兹测试开发学社

如何开展性能测试?性能测试的流程是什么样子?

测试人

软件测试 性能测试 自动化测试 测试开发

鸿蒙HarmonyOS实战-Stage模型(开发卡片事件)

EquatorCoco

microsoft 华为 鸿蒙系统

软件测试丨什么是性能测试?

测试人

软件测试

微店商品API接口:电商数据集成的新利器

Noah

低代码开发在医疗健康领域中的应用研究

EquatorCoco

低代码 医疗健康

学Python的别告诉我你还不造celery是干嘛的

我再BUG界嘎嘎乱杀

Python 编程 后端 软件开发 celery

3CX的介绍

cts喜友科技

通信 通讯 云通讯 通信通讯

AI 新质生产力创新先锋 焱融科技入选中国生成式AI企业TOP50

焱融科技

人工智能 高性能存储 软件定义存储 新质生产力

pyhttptest 实操指南:测试RESTful API的有效方法

Liam

测试 后端 测试工具 REST API pyhttptest

一文看懂分布式链路追踪

乘云数字DataBuff

应用性能监控 分布式链路追踪

PHP反射API与接口的动态分析

技术冰糖葫芦

API boy API 文档 API 性能测试

LLM实战:当网页爬虫集成gpt3.5

不在线第一只蜗牛

GPT LLM

数据驱动选品:阿里巴巴商品详情API在电商选品中的应用

tbapi

阿里巴巴 阿里巴巴API接口 阿里巴巴商品详情数据接口

视频标注已上线,支持视频分类、多目标检测|ModelWhale 版本更新

ModelWhale

计算机视觉

互联网行业,什么人看起来“必成大器”?

秃头小帅oi

斯嘉丽·约翰逊指控 OpenAI 非法使用其声音;微软推出AI 工具「回顾」(Recall)丨RTE 开发者日报 Vol.208

声网

如何提升金融业务效率的同时保障身份认证安全和用户体验(一)

芯盾时代

金融 手机银行 iam 统一身份认证 银行业

构建稳健、高效与安全的企业级API网关

RestCloud

API API网关 ipaas

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