Search in sources :

Example 1 with MaterialsDescriptionProvider

use of com.qcloud.cos.model.MaterialsDescriptionProvider in project cos-java-sdk-v5 by tencentyun.

the class COSCryptoModuleBase method initiateMultipartUploadSecurely.

@Override
public InitiateMultipartUploadResult initiateMultipartUploadSecurely(InitiateMultipartUploadRequest req) {
    // Generate a one-time use symmetric key and initialize a cipher to
    // encrypt object data
    ContentCryptoMaterial cekMaterial = createContentCryptoMaterial(req);
    if (cryptoConfig.getStorageMode() == ObjectMetadata) {
        ObjectMetadata metadata = req.getObjectMetadata();
        if (metadata == null)
            metadata = new ObjectMetadata();
        long dataSize = req.getDataSize();
        long partSize = req.getPartSize();
        if (dataSize < 0 || partSize < 0) {
            throw new CosClientException("initiate multipart upload with encryption client must set dataSize and partSize");
        }
        if (partSize % 16 != 0) {
            throw new CosClientException("initiat multipart uplaod with encryption client must set part size a mutiple of 16" + "but got " + partSize);
        }
        metadata.addUserMetadata(Headers.ENCRYPTION_DATA_SIZE, Long.toString(dataSize));
        metadata.addUserMetadata(Headers.ENCRYPTION_PART_SIZE, Long.toString(partSize));
        // Store encryption info in metadata
        req.setObjectMetadata(updateMetadataWithContentCryptoMaterial(metadata, null, cekMaterial));
    }
    InitiateMultipartUploadResult result = cos.initiateMultipartUpload(req);
    MultipartUploadCryptoContext uploadContext = newUploadContext(req, cekMaterial);
    if (req instanceof MaterialsDescriptionProvider) {
        MaterialsDescriptionProvider p = (MaterialsDescriptionProvider) req;
        uploadContext.setMaterialsDescription(p.getMaterialsDescription());
    }
    multipartUploadContexts.put(result.getUploadId(), uploadContext);
    return result;
}
Also used : MaterialsDescriptionProvider(com.qcloud.cos.model.MaterialsDescriptionProvider) InitiateMultipartUploadResult(com.qcloud.cos.model.InitiateMultipartUploadResult) CosClientException(com.qcloud.cos.exception.CosClientException) ObjectMetadata(com.qcloud.cos.internal.crypto.CryptoStorageMode.ObjectMetadata) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 2 with MaterialsDescriptionProvider

use of com.qcloud.cos.model.MaterialsDescriptionProvider in project cos-java-sdk-v5 by tencentyun.

the class ContentCryptoMaterial method mergeMaterialDescriptions.

static Map<String, String> mergeMaterialDescriptions(EncryptionMaterials materials, CosServiceRequest req) {
    Map<String, String> matdesc = materials.getMaterialsDescription();
    if (req instanceof MaterialsDescriptionProvider) {
        MaterialsDescriptionProvider mdp = (MaterialsDescriptionProvider) req;
        Map<String, String> matdesc_req = mdp.getMaterialsDescription();
        if (matdesc_req != null) {
            matdesc = new TreeMap<String, String>(matdesc);
            // request takes precedence
            matdesc.putAll(matdesc_req);
        }
    }
    return matdesc;
}
Also used : MaterialsDescriptionProvider(com.qcloud.cos.model.MaterialsDescriptionProvider)

Example 3 with MaterialsDescriptionProvider

use of com.qcloud.cos.model.MaterialsDescriptionProvider in project cos-java-sdk-v5 by tencentyun.

the class COSCryptoModuleBase method createContentCryptoMaterial.

/**
 * Creates and returns a non-null content crypto material for the given request.
 *
 * @throws CosClientException if no encryption material can be found.
 */
protected final ContentCryptoMaterial createContentCryptoMaterial(CosServiceRequest req) {
    if (req instanceof EncryptionMaterialsFactory) {
        // per request level encryption materials
        EncryptionMaterialsFactory f = (EncryptionMaterialsFactory) req;
        final EncryptionMaterials materials = f.getEncryptionMaterials();
        if (materials != null) {
            return buildContentCryptoMaterial(materials, cryptoConfig.getCryptoProvider(), req);
        }
    }
    if (req instanceof MaterialsDescriptionProvider) {
        // per request level material description
        MaterialsDescriptionProvider mdp = (MaterialsDescriptionProvider) req;
        Map<String, String> matdesc_req = mdp.getMaterialsDescription();
        ContentCryptoMaterial ccm = newContentCryptoMaterial(kekMaterialsProvider, matdesc_req, cryptoConfig.getCryptoProvider(), req);
        if (ccm != null)
            return ccm;
        if (matdesc_req != null) {
            // check to see if KMS is in use and if so we should fall thru
            // to the COS client level encryption material
            EncryptionMaterials material = kekMaterialsProvider.getEncryptionMaterials();
            if (!material.isKMSEnabled()) {
                throw new CosClientException("No material available from the encryption material provider for description " + matdesc_req);
            }
        }
    // if there is no material description, fall thru to use
    // the per cos client level encryption materials
    }
    // per cos client level encryption materials
    return newContentCryptoMaterial(this.kekMaterialsProvider, cryptoConfig.getCryptoProvider(), req);
}
Also used : MaterialsDescriptionProvider(com.qcloud.cos.model.MaterialsDescriptionProvider) CosClientException(com.qcloud.cos.exception.CosClientException)

Aggregations

MaterialsDescriptionProvider (com.qcloud.cos.model.MaterialsDescriptionProvider)3 CosClientException (com.qcloud.cos.exception.CosClientException)2 ObjectMetadata (com.qcloud.cos.internal.crypto.CryptoStorageMode.ObjectMetadata)1 InitiateMultipartUploadResult (com.qcloud.cos.model.InitiateMultipartUploadResult)1 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)1