public override async Task <ResponseMessage> ReadNextAsync(CancellationToken cancellationToken = default)
        {
            EncryptionSettings encryptionSettings = await this.encryptionContainer.GetOrUpdateEncryptionSettingsFromCacheAsync(obsoleteEncryptionSettings : null, cancellationToken : cancellationToken);

            await this.GetIteratorWithEncryptionHeaderAndEncryptPartitionKeyIfRequiredAsync(encryptionSettings);

            ResponseMessage responseMessage = await this.FeedIterator.ReadNextAsync(cancellationToken);

            EncryptionDiagnosticsContext encryptionDiagnosticsContext = new EncryptionDiagnosticsContext();

            // check for Bad Request and Wrong intended RID and update the cached RID and Client Encryption Policy.
            await this.encryptionContainer.ThrowIfRequestNeedsARetryPostPolicyRefreshAsync(responseMessage, encryptionSettings, encryptionDiagnosticsContext, cancellationToken);

            if (responseMessage.IsSuccessStatusCode && responseMessage.Content != null)
            {
                Stream decryptedContent = await EncryptionProcessor.DeserializeAndDecryptResponseAsync(
                    responseMessage.Content,
                    encryptionSettings,
                    encryptionDiagnosticsContext,
                    cancellationToken);

                encryptionDiagnosticsContext.AddEncryptionDiagnosticsToResponseMessage(responseMessage);

                return(new DecryptedResponseMessage(responseMessage, decryptedContent));
            }

            return(responseMessage);
        }
Exemple #2
0
        public override ChangeFeedProcessorBuilder GetChangeFeedProcessorBuilderWithManualCheckpoint(
            string processorName,
            ChangeFeedStreamHandlerWithManualCheckpoint onChangesDelegate)
        {
            return(this.container.GetChangeFeedProcessorBuilderWithManualCheckpoint(
                       processorName,
                       async(
                           ChangeFeedProcessorContext context,
                           Stream changes,
                           Func <Task> tryCheckpointAsync,
                           CancellationToken cancellationToken) =>
            {
                EncryptionSettings encryptionSettings = await this.GetOrUpdateEncryptionSettingsFromCacheAsync(
                    obsoleteEncryptionSettings: null,
                    cancellationToken: cancellationToken);

                Stream decryptedChanges = await EncryptionProcessor.DeserializeAndDecryptResponseAsync(
                    changes,
                    encryptionSettings,
                    operationDiagnostics: null,
                    cancellationToken);

                // Call the original passed in delegate
                await onChangesDelegate(context, decryptedChanges, tryCheckpointAsync, cancellationToken);
            }));
        }
Exemple #3
0
        public override async Task <ResponseMessage> ReadNextAsync(CancellationToken cancellationToken = default)
        {
            EncryptionSettings encryptionSettings = await this.encryptionContainer.GetOrUpdateEncryptionSettingsFromCacheAsync(obsoleteEncryptionSettings : null, cancellationToken : cancellationToken);

            encryptionSettings.SetRequestHeaders(this.requestOptions);

            ResponseMessage responseMessage = await this.feedIterator.ReadNextAsync(cancellationToken);

            // check for Bad Request and Wrong RID intended and update the cached RID and Client Encryption Policy.
            if (responseMessage.StatusCode == HttpStatusCode.BadRequest &&
                string.Equals(responseMessage.Headers.Get(Constants.SubStatusHeader), Constants.IncorrectContainerRidSubStatus))
            {
                await this.encryptionContainer.GetOrUpdateEncryptionSettingsFromCacheAsync(
                    obsoleteEncryptionSettings : encryptionSettings,
                    cancellationToken : cancellationToken);

                throw new CosmosException(
                          "Operation has failed due to a possible mismatch in Client Encryption Policy configured on the container. Please refer to https://aka.ms/CosmosClientEncryption for more details. " + responseMessage.ErrorMessage,
                          responseMessage.StatusCode,
                          int.Parse(Constants.IncorrectContainerRidSubStatus),
                          responseMessage.Headers.ActivityId,
                          responseMessage.Headers.RequestCharge);
            }

            if (responseMessage.IsSuccessStatusCode && responseMessage.Content != null)
            {
                EncryptionDiagnosticsContext decryptDiagnostics = new EncryptionDiagnosticsContext();

                Stream decryptedContent = await EncryptionProcessor.DeserializeAndDecryptResponseAsync(
                    responseMessage.Content,
                    encryptionSettings,
                    decryptDiagnostics,
                    cancellationToken);

                decryptDiagnostics.AddEncryptionDiagnosticsToResponseMessage(responseMessage);

                return(new DecryptedResponseMessage(responseMessage, decryptedContent));
            }

            return(responseMessage);
        }