diff --git a/src/main/java/com/qcloud/cos/COSEncryptionClient.java b/src/main/java/com/qcloud/cos/COSEncryptionClient.java index 33e82305..b148d755 100644 --- a/src/main/java/com/qcloud/cos/COSEncryptionClient.java +++ b/src/main/java/com/qcloud/cos/COSEncryptionClient.java @@ -49,6 +49,7 @@ import com.qcloud.cos.model.PutObjectResult; import com.qcloud.cos.model.UploadPartRequest; import com.qcloud.cos.model.UploadPartResult; +import com.tencentcloudapi.common.profile.ClientProfile; public class COSEncryptionClient extends COSClient implements COSEncryption { @@ -67,6 +68,12 @@ public COSEncryptionClient(COSCredentialsProvider credentialsProvider, this(null, credentialsProvider, kekMaterialsProvider, clientConfig, cryptoConfig); } + public COSEncryptionClient(COSCredentialsProvider credentialsProvider, + EncryptionMaterialsProvider kekMaterialsProvider, ClientConfig clientConfig, + CryptoConfiguration cryptoConfig,ClientProfile clientProfile) { + this(null, credentialsProvider, kekMaterialsProvider, clientConfig, cryptoConfig, clientProfile); + } + public COSEncryptionClient(QCLOUDKMS kms, COSCredentialsProvider credentialsProvider, EncryptionMaterialsProvider kekMaterialsProvider, ClientConfig clientConfig, CryptoConfiguration cryptoConfig) { @@ -81,6 +88,20 @@ public COSEncryptionClient(QCLOUDKMS kms, COSCredentialsProvider credentialsProv kekMaterialsProvider, cryptoConfig); } + public COSEncryptionClient(QCLOUDKMS kms, COSCredentialsProvider credentialsProvider, + EncryptionMaterialsProvider kekMaterialsProvider, ClientConfig clientConfig, + CryptoConfiguration cryptoConfig, ClientProfile clientProfile) { + super(credentialsProvider.getCredentials(), clientConfig); + assertParameterNotNull(kekMaterialsProvider, + "EncryptionMaterialsProvider parameter must not be null."); + assertParameterNotNull(cryptoConfig, "CryptoConfiguration parameter must not be null."); + this.isKMSClientInternal = kms == null; + this.kms = isKMSClientInternal ? + newTencentCloudKMSClient(credentialsProvider, clientConfig, cryptoConfig, clientProfile) : kms; + this.crypto = new CryptoModuleDispatcher(this.kms, new COSDirectImpl(), credentialsProvider, + kekMaterialsProvider, cryptoConfig); + } + private TencentCloudKMSClient newTencentCloudKMSClient( COSCredentialsProvider credentialsProvider, ClientConfig clientConfig, @@ -95,6 +116,21 @@ private TencentCloudKMSClient newTencentCloudKMSClient( return kmsClient; } + private TencentCloudKMSClient newTencentCloudKMSClient( + COSCredentialsProvider credentialsProvider, + ClientConfig clientConfig, + CryptoConfiguration cryptoConfig, + ClientProfile clientProfile) { + String region = cryptoConfig.getKmsRegion(); + if (region == null) { + region = clientConfig.getRegion().getRegionName(); + } + + final TencentCloudKMSClient kmsClient = new TencentCloudKMSClient(credentialsProvider, region, clientProfile); + + return kmsClient; + } + private void assertParameterNotNull(Object parameterValue, String errorMessage) { if (parameterValue == null) throw new IllegalArgumentException(errorMessage); diff --git a/src/main/java/com/qcloud/cos/demo/KMSEncryptionClientDemo.java b/src/main/java/com/qcloud/cos/demo/KMSEncryptionClientDemo.java index 9c96fe20..c0e988ce 100644 --- a/src/main/java/com/qcloud/cos/demo/KMSEncryptionClientDemo.java +++ b/src/main/java/com/qcloud/cos/demo/KMSEncryptionClientDemo.java @@ -28,6 +28,8 @@ import com.qcloud.cos.transfer.TransferManager; import com.qcloud.cos.transfer.TransferManagerConfiguration; import com.qcloud.cos.transfer.Upload; +import com.tencentcloudapi.common.profile.ClientProfile; +import com.tencentcloudapi.common.profile.HttpProfile; public class KMSEncryptionClientDemo { static String cmk = "kms-xxxxxxx"; @@ -67,6 +69,22 @@ static COSClient createCosClient(String region) { // 生成加密客户端EncryptionClient, COSEncryptionClient是COSClient的子类, 所有COSClient支持的接口他都支持。 // EncryptionClient覆盖了COSClient上传下载逻辑,操作内部会执行加密操作,其他操作执行逻辑和COSClient一致 + + // in case you want to use different kms server: + //{ + //String kmsEndPoint = ""; + ///HttpProfile httpProfile = new HttpProfile(); + //httpProfile.setEndpoint(kmsEndPoint); + //httpProfile.setProtocol(HttpProfile.REQ_HTTP); + //ClientProfile clientProfile = new ClientProfile(); + //clientProfile.setHttpProfile(httpProfile); + + //COSEncryptionClient cosEncryptionClient = + // new COSEncryptionClient(new COSStaticCredentialsProvider(cred), + // new KMSEncryptionMaterialsProvider(encryptionMaterials), clientConfig, + // cryptoConf,clientProfile); + //} + COSEncryptionClient cosEncryptionClient = new COSEncryptionClient(new COSStaticCredentialsProvider(cred), new KMSEncryptionMaterialsProvider(encryptionMaterials), clientConfig, diff --git a/src/main/java/com/qcloud/cos/internal/crypto/TencentCloudKMSClient.java b/src/main/java/com/qcloud/cos/internal/crypto/TencentCloudKMSClient.java index 69a00031..17e26355 100644 --- a/src/main/java/com/qcloud/cos/internal/crypto/TencentCloudKMSClient.java +++ b/src/main/java/com/qcloud/cos/internal/crypto/TencentCloudKMSClient.java @@ -29,6 +29,7 @@ import com.tencentcloudapi.kms.v20190118.models.EncryptResponse; import com.tencentcloudapi.kms.v20190118.models.GenerateDataKeyRequest; import com.tencentcloudapi.kms.v20190118.models.GenerateDataKeyResponse; +import com.tencentcloudapi.common.profile.ClientProfile; /** * Client for accessing TencentCloud KMS. @@ -45,6 +46,14 @@ public TencentCloudKMSClient(COSCredentialsProvider cosCredentialsProvider, Stri this.kmsClient = new KmsClient(credential, region); } + public TencentCloudKMSClient(COSCredentialsProvider cosCredentialsProvider, String region, ClientProfile clientProfile) { + COSCredentials cosCredentials = cosCredentialsProvider.getCredentials(); + String secretId = cosCredentials.getCOSAccessKeyId(); + String secretKey = cosCredentials.getCOSSecretKey(); + + Credential credential = new Credential(secretId, secretKey); + this.kmsClient = new KmsClient(credential, region, clientProfile); + } /** * Generates a unique symmetric data key for client-side encryption. This operation returns a plaintext copy of the * data key and a copy that is encrypted under a customer master key (CMK) that you specify. You can use the