public void CloudQueueAddUpdateEncryptedBinaryMessage()
        {
            // Create the Key to be used for wrapping.
            SymmetricKey aesKey = new SymmetricKey("symencryptionkey");

            CloudQueueClient client = GenerateCloudQueueClient();
            string           name   = GenerateNewQueueName();
            CloudQueue       queue  = client.GetQueueReference(name);

            try
            {
                queue.CreateIfNotExists();

                byte[] messageBytes = new byte[100];
                Random rand         = new Random();
                rand.NextBytes(messageBytes);

                CloudQueueMessage message = new CloudQueueMessage(messageBytes);

                QueueEncryptionPolicy policy  = new QueueEncryptionPolicy(aesKey, null);
                QueueRequestOptions   options = new QueueRequestOptions()
                {
                    EncryptionPolicy = policy
                };

                // Add message
                queue.AddMessage(message, null, null, options, null);

                // Retrieve message
                CloudQueueMessage retrMessage = queue.GetMessage(null, options, null);
                TestHelper.AssertBuffersAreEqual(messageBytes, retrMessage.AsBytes);
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
        public void CloudQueueAddEncrypted64KMessage()
        {
            // Create the Key to be used for wrapping.
            SymmetricKey aesKey = new SymmetricKey("symencryptionkey");

            CloudQueueClient client = GenerateCloudQueueClient();
            string           name   = GenerateNewQueueName();
            CloudQueue       queue  = client.GetQueueReference(name);

            try
            {
                queue.CreateIfNotExists();

                string            inputMessage = new string('a', 64 * 1024);
                CloudQueueMessage message      = new CloudQueueMessage(inputMessage);
                queue.EncodeMessage = false;

                QueueEncryptionPolicy policy  = new QueueEncryptionPolicy(aesKey, null);
                QueueRequestOptions   options = new QueueRequestOptions()
                {
                    EncryptionPolicy = policy
                };

                // Add message
                queue.AddMessage(message, null, null, null, null);

                // Add encrypted Message
                TestHelper.ExpectedException <ArgumentException>(
                    () => queue.AddMessage(message, null, null, options, null),
                    "Adding an encrypted message that exceeds message limits should throw");
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
 public virtual Task<ServiceStats> GetServiceStatsAsync(QueueRequestOptions options, OperationContext operationContext)
 {
     return this.GetServiceStatsAsync(options, operationContext, CancellationToken.None);
 }
        private RESTCommand<NullType> SetServicePropertiesImpl(ServiceProperties properties, QueueRequestOptions requestOptions)
        {
            MultiBufferMemoryStream memoryStream = new MultiBufferMemoryStream(null /* bufferManager */, (int)(1 * Constants.KB));
            try
            {
                properties.WriteServiceProperties(memoryStream);
            }
            catch (InvalidOperationException invalidOpException)
            {
                throw new ArgumentException(invalidOpException.Message, "properties");
            }

            RESTCommand<NullType> retCmd = new RESTCommand<NullType>(this.Credentials, this.StorageUri, this.HttpClient);
            requestOptions.ApplyToStorageCommand(retCmd);
            retCmd.BuildRequest = (cmd, uri, builder, cnt, serverTimeout, ctx) => QueueHttpRequestMessageFactory.SetServiceProperties(uri, serverTimeout, cnt, ctx, this.GetCanonicalizer(), this.Credentials);
            retCmd.BuildContent = (cmd, ctx) => HttpContentFactory.BuildContentFromStream(memoryStream, 0, memoryStream.Length, null /* md5 */, cmd, ctx);
            retCmd.StreamToDispose = memoryStream;
            retCmd.PreProcessResponse =
                (cmd, resp, ex, ctx) =>
                HttpResponseParsers.ProcessExpectedStatusCodeNoException(HttpStatusCode.Accepted, resp, null /* retVal */, cmd, ex);
            requestOptions.ApplyToStorageCommand(retCmd);
            return retCmd;
        }
 public virtual Task SetServicePropertiesAsync(ServiceProperties properties, QueueRequestOptions requestOptions, OperationContext operationContext)
 {
     return this.SetServicePropertiesAsync(properties, requestOptions, operationContext, CancellationToken.None);
 }
        /// <summary>
        /// Core implementation for the ListQueues method.
        /// </summary>
        /// <param name="prefix">The queue prefix.</param>
        /// <param name="detailsIncluded">The details included.</param>
        /// <param name="currentToken">The continuation token.</param>
        /// <param name="maxResults">A non-negative integer value that indicates the maximum number of results to be returned at a time, up to the 
        /// per-operation limit of 5000. If this value is <c>null</c>, the maximum possible number of results will be returned, up to 5000.</param>
        /// <returns>A <see cref="TaskSequence"/> that lists the queues.</returns>
        private RESTCommand<ResultSegment<CloudQueue>> ListQueuesImpl(string prefix, int? maxResults, QueueListingDetails detailsIncluded, QueueRequestOptions options, QueueContinuationToken currentToken)
        {
            ListingContext listingContext = new ListingContext(prefix, maxResults)
            {
                Marker = currentToken != null ? currentToken.NextMarker : null
            };

            RESTCommand<ResultSegment<CloudQueue>> getCmd = new RESTCommand<ResultSegment<CloudQueue>>(this.Credentials, this.StorageUri, this.HttpClient);

            options.ApplyToStorageCommand(getCmd);
            getCmd.CommandLocationMode = CommonUtility.GetListingLocationMode(currentToken);
            getCmd.RetrieveResponseStream = true;
            getCmd.BuildRequest = (cmd, uri, builder, cnt, serverTimeout, ctx) => QueueHttpRequestMessageFactory.List(uri, serverTimeout, listingContext, detailsIncluded, cnt, ctx, this.GetCanonicalizer(), this.Credentials);
            getCmd.PreProcessResponse = (cmd, resp, ex, ctx) => HttpResponseParsers.ProcessExpectedStatusCodeNoException(HttpStatusCode.OK, resp, null /* retVal */, cmd, ex);
            getCmd.PostProcessResponseAsync = async (cmd, resp, ctx, ct) =>
            {
                ListQueuesResponse listQueuesResponse = await ListQueuesResponse.ParseAsync(cmd.ResponseStream, ct).ConfigureAwait(false);

                List<CloudQueue> queuesList = listQueuesResponse.Queues.Select(item => new CloudQueue(item.Metadata, item.Name, this)).ToList();

                QueueContinuationToken continuationToken = null;
                if (listQueuesResponse.NextMarker != null)
                {
                    continuationToken = new QueueContinuationToken()
                    {
                        NextMarker = listQueuesResponse.NextMarker,
                        TargetLocation = cmd.CurrentResult.TargetLocation,
                    };
                }

                return new ResultSegment<CloudQueue>(queuesList)
                {
                    ContinuationToken = continuationToken,
                };
            };

            return getCmd;
        }
        public virtual async Task<QueueResultSegment> ListQueuesSegmentedAsync(string prefix, QueueListingDetails detailsIncluded, int? maxResults, QueueContinuationToken currentToken, QueueRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
        {
            QueueRequestOptions modifiedOptions = QueueRequestOptions.ApplyDefaults(options, this);
            operationContext = operationContext ?? new OperationContext();

            ResultSegment<CloudQueue> resultSegment = await Executor.ExecuteAsync(
                this.ListQueuesImpl(prefix, maxResults, detailsIncluded, modifiedOptions, currentToken),
                modifiedOptions.RetryPolicy,
                operationContext,
                cancellationToken).ConfigureAwait(false);

            return new QueueResultSegment(resultSegment.Results, (QueueContinuationToken)resultSegment.ContinuationToken);
        }
Exemple #8
0
        /// <summary>
        /// Selects the peek message response.
        /// </summary>
        /// <param name="protocolMessage">The protocol message.</param>
        /// <param name="options">A <see cref="QueueRequestOptions"/> object that specifies additional options for the request.</param>
        /// <returns>The parsed message.</returns>
        private CloudQueueMessage SelectPeekMessageResponse(QueueMessage protocolMessage, QueueRequestOptions options)
        {
            CloudQueueMessage message = null;

            byte[] dest = null;
#if !(WINDOWS_RT || NETCORE)
            if (options != null && options.EncryptionPolicy != null)
            {
                // If EncryptionPolicy is set, decrypt the message and set it.
                dest = options.EncryptionPolicy.DecryptMessage(protocolMessage.Text, options.RequireEncryption);
            }
#endif

            if (this.EncodeMessage)
            {
                if (dest != null)
                {
                    protocolMessage.Text = Convert.ToBase64String(dest, 0, dest.Length);
                }

                // if EncodeMessage is true, we assume the string returned from server is Base64 encoding of original message;
                // if this is not true, exception will likely be thrown.
                // it is user's responsibility to make sure EncodeMessage setting matches the queue that is being read.
                message = new CloudQueueMessage(protocolMessage.Text, true);
            }
            else
            {
#if !(WINDOWS_RT || NETCORE)
                if (dest != null)
                {
                    message = new CloudQueueMessage(dest);
                }
                else
#endif
                {
                    message = new CloudQueueMessage(protocolMessage.Text);
                }
            }

            message.Id             = protocolMessage.Id;
            message.InsertionTime  = protocolMessage.InsertionTime;
            message.ExpirationTime = protocolMessage.ExpirationTime;
            message.DequeueCount   = protocolMessage.DequeueCount;

            // PopReceipt and TimeNextVisible are not returned during peek
            return(message);
        }
 public virtual void SetServiceProperties(ServiceProperties properties, QueueRequestOptions requestOptions = null, OperationContext operationContext = null)
 {
     requestOptions   = QueueRequestOptions.ApplyDefaults(requestOptions, this);
     operationContext = operationContext ?? new OperationContext();
     Executor.ExecuteSync(this.SetServicePropertiesImpl(properties, requestOptions), requestOptions.RetryPolicy, operationContext);
 }
        public virtual ICancellableAsyncResult BeginSetServiceProperties(ServiceProperties properties, QueueRequestOptions requestOptions, OperationContext operationContext, AsyncCallback callback, object state)
        {
            requestOptions   = QueueRequestOptions.ApplyDefaults(requestOptions, this);
            operationContext = operationContext ?? new OperationContext();

            return(CancellableAsyncResultTaskWrapper.Create(token => this.SetServicePropertiesAsync(properties, requestOptions, operationContext), callback, state));
        }
 public virtual Task <ServiceProperties> GetServicePropertiesAsync(QueueRequestOptions requestOptions, OperationContext operationContext)
 {
     return(this.GetServicePropertiesAsync(requestOptions, operationContext, CancellationToken.None));
 }
 /// <summary>
 /// Begins an asynchronous operation to return a result segment containing a collection of queues.
 /// </summary>
 /// <param name="prefix">A string containing the queue name prefix.</param>
 /// <param name="queueListingDetails">A <see cref="QueueListingDetails"/> enumeration describing which items to include in the listing.</param>
 /// <param name="maxResults">A non-negative integer value that indicates the maximum number of results to be returned at a time, up to the
 /// per-operation limit of 5000. If this value is <c>null</c>, the maximum possible number of results will be returned, up to 5000.</param>
 /// <param name="currentToken">A <see cref="QueueContinuationToken"/> returned by a previous listing operation.</param>
 /// <param name="options">A <see cref="QueueRequestOptions"/> object that specifies additional options for the request.</param>
 /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
 /// <param name="callback">An <see cref="AsyncCallback"/> delegate that will receive notification when the asynchronous operation completes.</param>
 /// <param name="state">A user-defined object that will be passed to the callback delegate.</param>
 /// <returns>An <see cref="ICancellableAsyncResult"/> that references the asynchronous operation.</returns>
 public virtual ICancellableAsyncResult BeginListQueuesSegmented(string prefix, QueueListingDetails queueListingDetails, int?maxResults, QueueContinuationToken currentToken, QueueRequestOptions options, OperationContext operationContext, AsyncCallback callback, object state)
 {
     return(CancellableAsyncResultTaskWrapper.Create(token => this.ListQueuesSegmentedAsync(prefix, queueListingDetails, maxResults, currentToken, options, operationContext, token), callback, state));
 }
 /// <summary>
 /// Returns a result segment containing a collection of queues.
 /// </summary>
 /// <param name="prefix">A string containing the queue name prefix.</param>
 /// <param name="queueListingDetails">A <see cref="QueueListingDetails"/> enumeration describing which items to include in the listing.</param>
 /// <param name="maxResults">A non-negative integer value that indicates the maximum number of results to be returned at a time, up to the
 /// per-operation limit of 5000. If this value is <c>null</c>, the maximum possible number of results will be returned, up to 5000.</param>
 /// <param name="currentToken">A <see cref="QueueContinuationToken"/> returned by a previous listing operation.</param>
 /// <param name="options">A <see cref="QueueRequestOptions"/> object that specifies additional options for the request.</param>
 /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
 /// <returns>A <see cref="ResultSegment{T}"/> of type <see cref="CloudQueue"/>.</returns>
 private ResultSegment <CloudQueue> ListQueuesSegmentedCore(string prefix, QueueListingDetails queueListingDetails, int?maxResults, QueueContinuationToken currentToken, QueueRequestOptions options, OperationContext operationContext)
 {
     return(Executor.ExecuteSync(
                this.ListQueuesImpl(prefix, maxResults, queueListingDetails, options, currentToken),
                options.RetryPolicy,
                operationContext));
 }
        /// <summary>
        /// Returns a result segment containing a collection of queues.
        /// </summary>
        /// <param name="prefix">A string containing the queue name prefix.</param>
        /// <param name="queueListingDetails">A <see cref="QueueListingDetails"/> enumeration describing which items to include in the listing.</param>
        /// <param name="maxResults">A non-negative integer value that indicates the maximum number of results to be returned at a time, up to the
        /// per-operation limit of 5000. If this value is <c>null</c>, the maximum possible number of results will be returned, up to 5000.</param>
        /// <param name="currentToken">A <see cref="QueueContinuationToken"/> returned by a previous listing operation.</param>
        /// <param name="options">A <see cref="QueueRequestOptions"/> object that specifies additional options for the request. If <c>null</c>, default options are applied to the request.</param>
        /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
        /// <returns>A <see cref="QueueResultSegment"/> object.</returns>
        public virtual QueueResultSegment ListQueuesSegmented(string prefix, QueueListingDetails queueListingDetails, int?maxResults, QueueContinuationToken currentToken, QueueRequestOptions options = null, OperationContext operationContext = null)
        {
            QueueRequestOptions modifiedOptions = QueueRequestOptions.ApplyDefaults(options, this);

            operationContext = operationContext ?? new OperationContext();

            ResultSegment <CloudQueue> resultSegment = this.ListQueuesSegmentedCore(prefix, queueListingDetails, maxResults, currentToken, modifiedOptions, operationContext);

            return(new QueueResultSegment(resultSegment.Results, (QueueContinuationToken)resultSegment.ContinuationToken));
        }
        public void CloudQueueMessageEncryptionWithStrictMode()
        {
            // Create the Key to be used for wrapping.
            SymmetricKey aesKey = new SymmetricKey("symencryptionkey");

            // Create the resolver to be used for unwrapping.
            DictionaryKeyResolver resolver = new DictionaryKeyResolver();

            resolver.Add(aesKey);

            CloudQueueClient client = GenerateCloudQueueClient();
            string           name   = GenerateNewQueueName();
            CloudQueue       queue  = client.GetQueueReference(name);

            try
            {
                queue.CreateIfNotExists();

                string            messageStr = Guid.NewGuid().ToString();
                CloudQueueMessage message    = new CloudQueueMessage(messageStr);

                QueueEncryptionPolicy policy = new QueueEncryptionPolicy(aesKey, null);

                // Add message with policy.
                QueueRequestOptions createOptions = new QueueRequestOptions()
                {
                    EncryptionPolicy = policy
                };
                createOptions.RequireEncryption = true;

                queue.AddMessage(message, null, null, createOptions, null);

                // Set policy to null and add message while RequireEncryption flag is still set to true. This should throw.
                createOptions.EncryptionPolicy = null;

                TestHelper.ExpectedException <InvalidOperationException>(
                    () => queue.AddMessage(message, null, null, createOptions, null),
                    "Not specifying a policy when RequireEnryption is set to true should throw.");

                // Retrieve message
                QueueEncryptionPolicy retrPolicy      = new QueueEncryptionPolicy(null, resolver);
                QueueRequestOptions   retrieveOptions = new QueueRequestOptions()
                {
                    EncryptionPolicy = retrPolicy
                };
                retrieveOptions.RequireEncryption = true;

                CloudQueueMessage retrMessage = queue.GetMessage(null, retrieveOptions, null);

                // Update message with plain text.
                string updatedMessage = Guid.NewGuid().ToString("N");
                retrMessage.SetMessageContent(updatedMessage);

                queue.UpdateMessage(retrMessage, TimeSpan.FromSeconds(0), MessageUpdateFields.Content | MessageUpdateFields.Visibility);

                // Retrieve updated message with RequireEncryption flag but no metadata on the service. This should throw.
                TestHelper.ExpectedException <StorageException>(
                    () => queue.GetMessage(null, retrieveOptions, null),
                    "Retrieving with RequireEncryption set to true and no metadata on the service should fail.");

                // Set RequireEncryption to false and retrieve.
                retrieveOptions.RequireEncryption = false;
                queue.GetMessage(null, retrieveOptions, null);
            }
            finally
            {
                queue.DeleteIfExists();
            }
        }
 public virtual Task<QueueResultSegment> ListQueuesSegmentedAsync(string prefix, QueueListingDetails detailsIncluded, int? maxResults, QueueContinuationToken currentToken, QueueRequestOptions options, OperationContext operationContext)
 {
     return this.ListQueuesSegmentedAsync(prefix, detailsIncluded, maxResults, currentToken, options, operationContext, CancellationToken.None);
 }
Exemple #17
0
        /// <summary>
        /// Selects the get message response.
        /// </summary>
        /// <param name="protocolMessage">The protocol message.</param>
        /// <param name="options">A <see cref="QueueRequestOptions"/> object that specifies additional options for the request.</param>
        /// <returns>The parsed message.</returns>
        private CloudQueueMessage SelectGetMessageResponse(QueueMessage protocolMessage, QueueRequestOptions options = null)
        {
            CloudQueueMessage message = this.SelectPeekMessageResponse(protocolMessage, options);

            message.PopReceipt = protocolMessage.PopReceipt;

            if (protocolMessage.NextVisibleTime.HasValue)
            {
                message.NextVisibleTime = protocolMessage.NextVisibleTime.Value;
            }

            return(message);
        }
 public virtual ICancellableAsyncResult BeginGetServiceStats(QueueRequestOptions options, OperationContext operationContext, AsyncCallback callback, object state)
 {
     return(CancellableAsyncResultTaskWrapper.Create(token => this.GetServiceStatsAsync(options, operationContext, token), callback, state));
 }
Exemple #19
0
        /// <summary>
        /// Gets the content of the message for transfer (internal use only).
        /// </summary>
        /// <param name="shouldEncodeMessage">Indicates if the message should be encoded.</param>
        /// <param name="options">A <see cref="QueueRequestOptions"/> object that specifies additional options for the request.</param>
        /// <returns>The message content as a string.</returns>
        internal string GetMessageContentForTransfer(bool shouldEncodeMessage, QueueRequestOptions options = null)
        {
            if (!shouldEncodeMessage && this.MessageType != QueueMessageType.RawString)
            {
                throw new ArgumentException(SR.BinaryMessageShouldUseBase64Encoding);
            }

            string outgoingMessageString = null;

#if !(WINDOWS_RT || NETCORE)
            if (options != null)
            {
                options.AssertPolicyIfRequired();

                if (options.EncryptionPolicy != null)
                {
                    // Create an encrypted message that will hold the message contents along with encryption related metadata and return it.
                    // The encrypted message is already Base 64 encoded. So no need to process further in this method.
                    string encryptedMessageString = options.EncryptionPolicy.EncryptMessage(this.AsBytes);

                    // the size of Base64 encoded string is the number of bytes this message will take up on server.
                    if (encryptedMessageString.Length > CloudQueueMessage.MaxMessageSize)
                    {
                        throw new ArgumentException(string.Format(
                                                        CultureInfo.InvariantCulture,
                                                        SR.EncryptedMessageTooLarge,
                                                        CloudQueueMessage.MaxMessageSize));
                    }

                    return(encryptedMessageString);
                }
            }
#endif

            if (this.MessageType != QueueMessageType.Base64Encoded)
            {
                if (shouldEncodeMessage)
                {
                    outgoingMessageString = Convert.ToBase64String(this.AsBytes);

                    // the size of Base64 encoded string is the number of bytes this message will take up on server.
                    if (outgoingMessageString.Length > CloudQueueMessage.MaxMessageSize)
                    {
                        throw new ArgumentException(string.Format(
                                                        CultureInfo.InvariantCulture,
                                                        SR.MessageTooLarge,
                                                        CloudQueueMessage.MaxMessageSize));
                    }
                }
                else
                {
                    outgoingMessageString = this.RawString;

                    // we need to calculate the size of its UTF8 byte array, as that will be the storage usage on server.
                    if (Encoding.UTF8.GetBytes(outgoingMessageString).Length > CloudQueueMessage.MaxMessageSize)
                    {
                        throw new ArgumentException(string.Format(
                                                        CultureInfo.InvariantCulture,
                                                        SR.MessageTooLarge,
                                                        CloudQueueMessage.MaxMessageSize));
                    }
                }
            }
            else
            {
                // at this point, this.EncodeMessage must be true
                outgoingMessageString = this.RawString;

                // the size of Base64 encoded string is the number of bytes this message will take up on server.
                if (outgoingMessageString.Length > CloudQueueMessage.MaxMessageSize)
                {
                    throw new ArgumentException(string.Format(
                                                    CultureInfo.InvariantCulture,
                                                    SR.MessageTooLarge,
                                                    CloudQueueMessage.MaxMessageSize));
                }
            }

            return(outgoingMessageString);
        }
        /// <summary>
        /// Returns an enumerable collection of the queues in the storage account whose names begin with the specified prefix and that are retrieved lazily.
        /// </summary>
        /// <param name="prefix">A string containing the queue name prefix.</param>
        /// <param name="queueListingDetails">A <see cref="QueueListingDetails"/> enumeration value that indicates which details to include in the listing.</param>
        /// <param name="options">A <see cref="QueueRequestOptions"/> object that specifies additional options for the request. If <c>null</c>, default options are applied to the request.</param>
        /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
        /// <returns>An enumerable collection of objects of type <see cref="CloudQueue"/> that are retrieved lazily.</returns>
        public virtual IEnumerable <CloudQueue> ListQueues(string prefix = null, QueueListingDetails queueListingDetails = QueueListingDetails.None, QueueRequestOptions options = null, OperationContext operationContext = null)
        {
            QueueRequestOptions modifiedOptions = QueueRequestOptions.ApplyDefaults(options, this);

            operationContext = operationContext ?? new OperationContext();

            return(CommonUtility.LazyEnumerable(
                       (token) => this.ListQueuesSegmentedCore(prefix, queueListingDetails, null, token as QueueContinuationToken, modifiedOptions, operationContext),
                       long.MaxValue));
        }