示例#1
0
        public static string ToSqsQueueName(this Address address, SqsConnectionConfiguration connectionConfiguration)
        {
            // SQS queue names can only have alphanumeric characters, hyphens and underscores.
            // Any other characters will be replaced with a hyphen.
            var s = connectionConfiguration.QueueNamePrefix + address.Queue;

            for (var i = 0; i < s.Length; ++i)
            {
                var c = s[i];
                if (!char.IsLetterOrDigit(c) &&
                    c != '-' &&
                    c != '_')
                {
                    s = s.Replace(c, '-');
                }
            }

            if (connectionConfiguration.TruncateLongQueueNames)
            {
                return(s.Substring(0, Math.Min(80, s.Length)));
            }

            if (s.Length > 80)
            {
                throw new InvalidOperationException(
                          String.Format("Address {0} with configured prefix {1} is longer than 80 characters and therefore cannot be used to create an SQS queue. Use a shorter queue name.",
                                        address.Queue, connectionConfiguration.QueueNamePrefix));
            }

            return(s);
        }
		public static TransportMessage ToTransportMessage(this SqsTransportMessage sqsTransportMessage, IAmazonS3 amazonS3, SqsConnectionConfiguration connectionConfiguration)
        {
            var messageId = sqsTransportMessage.Headers[Headers.MessageId];

			var result = new TransportMessage(messageId, sqsTransportMessage.Headers);

            if (!string.IsNullOrEmpty(sqsTransportMessage.S3BodyKey))
            {
                var s3GetResponse = amazonS3.GetObject(connectionConfiguration.S3BucketForLargeMessages, sqsTransportMessage.S3BodyKey);
                result.Body = new byte[s3GetResponse.ResponseStream.Length];
                using (BufferedStream bufferedStream = new BufferedStream(s3GetResponse.ResponseStream))
                {
                    int count;
                    int transferred = 0;
                    while ((count = bufferedStream.Read(result.Body, transferred, 8192)) > 0)
                    {
                        transferred += count;
                    }
                }
            }
            else
			{
				result.Body = Convert.FromBase64String(sqsTransportMessage.Body);
			}

            result.TimeToBeReceived = sqsTransportMessage.TimeToBeReceived;

			if (sqsTransportMessage.ReplyToAddress != null)
			{
				result.Headers[Headers.ReplyToAddress] = sqsTransportMessage.ReplyToAddress.ToString();
			}

            return result;
        }
        public static string ToSqsQueueName(this Address address, SqsConnectionConfiguration connectionConfiguration)
        {
			// SQS queue names can only have alphanumeric characters, hyphens and underscores.
			// Any other characters will be replaced with a hyphen.
			var s = connectionConfiguration.QueueNamePrefix + address.Queue;
			for (var i = 0; i<s.Length; ++i)
			{
				var c = s[i];
				if ( !char.IsLetterOrDigit(c) 
					&& c != '-'
					&& c != '_')
				{
					s = s.Replace(c, '-');
				}
			}

			if (connectionConfiguration.TruncateLongQueueNames)
	        {
				return s.Substring(0, Math.Min(80, s.Length));
	        }
			
			if (s.Length > 80)
			{
				throw new InvalidOperationException(
					String.Format("Address {0} with configured prefix {1} is longer than 80 characters and therefore cannot be used to create an SQS queue. Use a shorter queue name.",
					address.Queue, connectionConfiguration.QueueNamePrefix));
			}

	        return s;
        }
		public static TransportMessage ToTransportMessage(this SqsTransportMessage sqsTransportMessage, IAmazonS3 amazonS3, SqsConnectionConfiguration connectionConfiguration)
        {
            var messageId = sqsTransportMessage.Headers[Headers.MessageId];

			var result = new TransportMessage(messageId, sqsTransportMessage.Headers);
			
			if (!string.IsNullOrEmpty(sqsTransportMessage.S3BodyKey))
			{
				var s3GetResponse = amazonS3.GetObject(connectionConfiguration.S3BucketForLargeMessages, sqsTransportMessage.S3BodyKey);
				result.Body = new byte[s3GetResponse.ResponseStream.Length];
				s3GetResponse.ResponseStream.Read(result.Body, 0, result.Body.Length);
			}
			else
			{
				result.Body = Convert.FromBase64String(sqsTransportMessage.Body);
			}

            result.TimeToBeReceived = sqsTransportMessage.TimeToBeReceived;

			if (sqsTransportMessage.ReplyToAddress != null)
			{
				result.Headers[Headers.ReplyToAddress] = sqsTransportMessage.ReplyToAddress.ToString();
			}

            return result;
        }
示例#5
0
        public static IAmazonS3 CreateS3Client(SqsConnectionConfiguration connectionConfiguration)
        {
            var config = new AmazonS3Config
            {
                RegionEndpoint = connectionConfiguration.Region,
            };

            SetProxyConfig(config, connectionConfiguration);

            return(new AmazonS3Client(CreateCredentials(connectionConfiguration), config));
        }
	    static AWSCredentials CreateCredentials(SqsConnectionConfiguration connectionConfiguration)
	    {
		    switch (connectionConfiguration.CredentialSource)
		    {
			    case SqsCredentialSource.EnvironmentVariables:
					return new EnvironmentVariablesAWSCredentials();
				case SqsCredentialSource.InstanceProfile:
					return new InstanceProfileAWSCredentials();
		    }
		    throw new NotImplementedException(String.Format("No implementation for credential type {0}", connectionConfiguration.CredentialSource));
	    }
示例#7
0
        static void SetProxyConfig(ClientConfig clientConfig, SqsConnectionConfiguration connectionConfig)
        {
            if (!string.IsNullOrEmpty(connectionConfig.ProxyHost))
            {
                var userName = Environment.GetEnvironmentVariable(NSERVICEBUS_AMAZONSQS_PROXY_AUTHENTICATION_USERNAME);
                var password = Environment.GetEnvironmentVariable(NSERVICEBUS_AMAZONSQS_PROXY_AUTHENTICATION_PASSWORD);

                clientConfig.ProxyCredentials = new NetworkCredential(userName, password);
                clientConfig.ProxyHost        = connectionConfig.ProxyHost;
                clientConfig.ProxyPort        = connectionConfig.ProxyPort;
            }
        }
示例#8
0
        static AWSCredentials CreateCredentials(SqsConnectionConfiguration connectionConfiguration)
        {
            switch (connectionConfiguration.CredentialSource)
            {
            case SqsCredentialSource.EnvironmentVariables:
                return(new EnvironmentVariablesAWSCredentials());

            case SqsCredentialSource.InstanceProfile:
                return(new InstanceProfileAWSCredentials());
            }
            throw new NotImplementedException($"No implementation for credential type {connectionConfiguration.CredentialSource}");
        }
        public static string GetSqsQueueName(string destination, SqsConnectionConfiguration connectionConfiguration)
        {
            if (string.IsNullOrWhiteSpace(destination))
            {
                throw new ArgumentNullException(nameof(destination));
            }


            var s = connectionConfiguration.QueueNamePrefix + destination;

            if (connectionConfiguration.PreTruncateQueueNames && s.Length > 80)
            {
                int charsToTake = 80 - connectionConfiguration.QueueNamePrefix.Length;
                s = connectionConfiguration.QueueNamePrefix +
                    new string(s.Reverse().Take(charsToTake).Reverse().ToArray());
            }

            // SQS queue names can only have alphanumeric characters, hyphens and underscores.
            // Any other characters will be replaced with a hyphen.
            for (var i = 0; i < s.Length; ++i)
            {
                var c = s[i];
                if (!char.IsLetterOrDigit(c) &&
                    c != '-' &&
                    c != '_')
                {
                    s = s.Replace(c, '-');
                }
            }

            if (s.Length > 80)
            {
                throw new InvalidOperationException(
                          $"Address {destination} with configured prefix {connectionConfiguration.QueueNamePrefix} is longer than 80 characters and therefore cannot be used to create an SQS queue. Use a shorter queue name.");
            }

            return(s);
        }
        public static async Task <IncomingMessage> ToIncomingMessage(this SqsTransportMessage sqsTransportMessage,
                                                                     IAmazonS3 amazonS3,
                                                                     SqsConnectionConfiguration connectionConfiguration,
                                                                     CancellationToken cancellationToken)
        {
            var messageId = sqsTransportMessage.Headers[Headers.MessageId];

            byte[] body;

            if (!string.IsNullOrEmpty(sqsTransportMessage.S3BodyKey))
            {
                var s3GetResponse = await amazonS3.GetObjectAsync(connectionConfiguration.S3BucketForLargeMessages,
                                                                  sqsTransportMessage.S3BodyKey,
                                                                  cancellationToken).ConfigureAwait(false);

                body = new byte[s3GetResponse.ResponseStream.Length];
                using (BufferedStream bufferedStream = new BufferedStream(s3GetResponse.ResponseStream))
                {
                    int       count;
                    int       transferred  = 0;
                    const int maxChunkSize = 8 * 1024;
                    int       bytesToRead  = Math.Min(maxChunkSize, body.Length - transferred);
                    while ((count = await bufferedStream.ReadAsync(body, transferred, bytesToRead, cancellationToken).ConfigureAwait(false)) > 0)
                    {
                        transferred += count;
                        bytesToRead  = Math.Min(maxChunkSize, body.Length - transferred);
                    }
                }
            }
            else
            {
                body = Convert.FromBase64String(sqsTransportMessage.Body);
            }

            return(new IncomingMessage(messageId, sqsTransportMessage.Headers, body));
        }
示例#11
0
        public static SqsConnectionConfiguration Parse(string connectionString)
        {
            var connectionConfiguration = new SqsConnectionConfiguration();

            var values = connectionString.Split(';');

            if (string.IsNullOrEmpty(values.Last()))
            {
                values = values.Take(values.Count() - 1).ToArray();
            }
            foreach (var v in values)
            {
                var keyAndValue = v.Split('=');
                if (keyAndValue.Length != 2)
                {
                    throw new ArgumentException(String.Format("Malformed connection string around value: \"{0}\"", v));
                }

                if (keyAndValue[0].ToLower() == "region")
                {
                    foreach (var r in Amazon.RegionEndpoint.EnumerableAllRegions)
                    {
                        if (keyAndValue[1].ToLower() == r.SystemName)
                        {
                            connectionConfiguration.Region = r;
                            break;
                        }
                    }

                    if (connectionConfiguration.Region == null)
                    {
                        throw new ArgumentException(String.Format("Unknown region: \"{0}\"", keyAndValue[1]));
                    }
                }
                else if (keyAndValue[0].ToLower() == "s3bucketforlargemessages")
                {
                    connectionConfiguration.S3BucketForLargeMessages = keyAndValue[1].ToLower();

                    // https://forums.aws.amazon.com/message.jspa?messageID=315883
                    // S3 bucket names have the following restrictions:
                    // - Should not contain uppercase characters
                    // - Should not contain underscores (_)
                    // - Should be between 3 and 63 characters long
                    // - Should not end with a dash
                    // - Cannot contain two, adjacent periods
                    // - Cannot contain dashes next to periods (e.g., "my-.bucket.com" and "my.-bucket" are invalid)
                    if (connectionConfiguration.S3BucketForLargeMessages.Length < 3 ||
                        connectionConfiguration.S3BucketForLargeMessages.Length > 63)
                    {
                        throw new ArgumentException("S3 Bucket names must be between 3 and 63 characters in length.");
                    }

                    if (connectionConfiguration.S3BucketForLargeMessages.Any(c => !char.IsLetterOrDigit(c) &&
                                                                             c != '-' &&
                                                                             c != '.'))
                    {
                        throw new ArgumentException("S3 Bucket names must only contain letters, numbers, hyphens and periods.");
                    }

                    if (connectionConfiguration.S3BucketForLargeMessages.EndsWith("-"))
                    {
                        throw new ArgumentException("S3 Bucket names must not end with a hyphen.");
                    }

                    if (connectionConfiguration.S3BucketForLargeMessages.Contains(".."))
                    {
                        throw new ArgumentException("S3 Bucket names must not contain two adjacent periods.");
                    }

                    if (connectionConfiguration.S3BucketForLargeMessages.Contains(".-") ||
                        connectionConfiguration.S3BucketForLargeMessages.Contains("-."))
                    {
                        throw new ArgumentException("S3 Bucket names must not contain hyphens adjacent to periods.");
                    }
                }
                else if (keyAndValue[0].ToLower() == "s3keyprefix")
                {
                    connectionConfiguration.S3KeyPrefix = keyAndValue[1];
                }
                else if (keyAndValue[0].ToLower() == "maxttldays")
                {
                    connectionConfiguration.MaxTTLDays = int.Parse(keyAndValue[1]);
                    if (connectionConfiguration.MaxTTLDays <= 0 || connectionConfiguration.MaxTTLDays > 14)
                    {
                        throw new ArgumentException("Max TTL needs to be greater than 0 and less than 15.");
                    }
                }
                else if (keyAndValue[0].ToLower() == "maxreceivemessagebatchsize")
                {
                    connectionConfiguration.MaxReceiveMessageBatchSize = int.Parse(keyAndValue[1]);
                    if (connectionConfiguration.MaxReceiveMessageBatchSize <= 0 || connectionConfiguration.MaxReceiveMessageBatchSize > 10)
                    {
                        throw new ArgumentException("Max receive message batch size needs to be a number from 1 to 10.");
                    }
                }
                else if (keyAndValue[0].ToLower() == "truncatelongqueuenames")
                {
                    connectionConfiguration.TruncateLongQueueNames = bool.Parse(keyAndValue[1]);
                }
                else if (keyAndValue[0].ToLower() == "queuenameprefix")
                {
                    connectionConfiguration.QueueNamePrefix = keyAndValue[1];
                }
                else if (keyAndValue[0].ToLower() == "credentialsource")
                {
                    connectionConfiguration.CredentialSource = (SqsCredentialSource)Enum.Parse(typeof(SqsCredentialSource), keyAndValue[1]);
                }
                else
                {
                    throw new ArgumentException(String.Format("Unknown configuration key \"{0}\"", keyAndValue[0]));
                }
            }

            if (!string.IsNullOrEmpty(connectionConfiguration.S3BucketForLargeMessages) &&
                string.IsNullOrEmpty(connectionConfiguration.S3KeyPrefix))
            {
                throw new ArgumentException("An S3 bucket for large messages was specified, but no S3 key prefix was supplied. Supply an S3 key prefix.");
            }

            return(connectionConfiguration);
        }
示例#12
0
 public static IAmazonS3 CreateS3Client(SqsConnectionConfiguration connectionConfiguration)
 {
     return(new AmazonS3Client(CreateCredentials(connectionConfiguration), connectionConfiguration.Region));
 }
        public static TransportMessage ToTransportMessage(this SqsTransportMessage sqsTransportMessage, IAmazonS3 amazonS3, SqsConnectionConfiguration connectionConfiguration)
        {
            var messageId = sqsTransportMessage.Headers[Headers.MessageId];

            var result = new TransportMessage(messageId, sqsTransportMessage.Headers);

            if (!string.IsNullOrEmpty(sqsTransportMessage.S3BodyKey))
            {
                var s3GetResponse = amazonS3.GetObject(connectionConfiguration.S3BucketForLargeMessages, sqsTransportMessage.S3BodyKey);
                result.Body = new byte[s3GetResponse.ResponseStream.Length];
                s3GetResponse.ResponseStream.Read(result.Body, 0, result.Body.Length);
            }
            else
            {
                result.Body = Convert.FromBase64String(sqsTransportMessage.Body);
            }

            result.TimeToBeReceived = sqsTransportMessage.TimeToBeReceived;

            if (sqsTransportMessage.ReplyToAddress != null)
            {
                result.Headers[Headers.ReplyToAddress] = sqsTransportMessage.ReplyToAddress.ToString();
            }

            return(result);
        }
        public static SqsConnectionConfiguration Parse(string connectionString)
        {
            var connectionConfiguration = new SqsConnectionConfiguration();

            var values = connectionString.Split(';');
            if (string.IsNullOrEmpty(values.Last()))
            {
                values = values.Take(values.Count() - 1).ToArray();
            }
            foreach (var v in values)
            {
                var keyAndValue = v.Split('=');
                if (keyAndValue.Length != 2)
                    throw new ArgumentException(String.Format("Malformed connection string around value: \"{0}\"", v));

				if (keyAndValue[0].ToLower() == "region")
				{
					foreach (var r in Amazon.RegionEndpoint.EnumerableAllRegions)
					{
						if (keyAndValue[1].ToLower() == r.SystemName)
						{
							connectionConfiguration.Region = r;
							break;
						}
					}

					if (connectionConfiguration.Region == null)
					{
						throw new ArgumentException(String.Format("Unknown region: \"{0}\"", keyAndValue[1]));
					}
				}
				else if (keyAndValue[0].ToLower() == "s3bucketforlargemessages")
				{
					connectionConfiguration.S3BucketForLargeMessages = keyAndValue[1].ToLower();

					// https://forums.aws.amazon.com/message.jspa?messageID=315883
					// S3 bucket names have the following restrictions:
					// - Should not contain uppercase characters
					// - Should not contain underscores (_)
					// - Should be between 3 and 63 characters long
					// - Should not end with a dash
					// - Cannot contain two, adjacent periods
					// - Cannot contain dashes next to periods (e.g., "my-.bucket.com" and "my.-bucket" are invalid)
					if ( connectionConfiguration.S3BucketForLargeMessages.Length < 3 ||
						connectionConfiguration.S3BucketForLargeMessages.Length > 63)
						throw new ArgumentException("S3 Bucket names must be between 3 and 63 characters in length.");

					if (connectionConfiguration.S3BucketForLargeMessages.Any(c => !char.IsLetterOrDigit(c)
					                                                              && c != '-'
					                                                              && c != '.'))
					{
						throw new ArgumentException("S3 Bucket names must only contain letters, numbers, hyphens and periods.");
					}

					if ( connectionConfiguration.S3BucketForLargeMessages.EndsWith("-") )
						throw new ArgumentException("S3 Bucket names must not end with a hyphen.");

					if ( connectionConfiguration.S3BucketForLargeMessages.Contains("..") )
						throw new ArgumentException("S3 Bucket names must not contain two adjacent periods.");

					if (connectionConfiguration.S3BucketForLargeMessages.Contains(".-") || 
						connectionConfiguration.S3BucketForLargeMessages.Contains("-."))
						throw new ArgumentException("S3 Bucket names must not contain hyphens adjacent to periods.");
				}
				else if (keyAndValue[0].ToLower() == "s3keyprefix")
				{
					connectionConfiguration.S3KeyPrefix = keyAndValue[1];
				}
				else if (keyAndValue[0].ToLower() == "maxttldays")
				{
					connectionConfiguration.MaxTTLDays = int.Parse(keyAndValue[1]);
                    if (connectionConfiguration.MaxTTLDays <= 0 || connectionConfiguration.MaxTTLDays > 14)
                    {
                        throw new ArgumentException("Max TTL needs to be greater than 0 and less than 15.");
                    }
                }
                else if (keyAndValue[0].ToLower() == "maxreceivemessagebatchsize")
				{
					connectionConfiguration.MaxReceiveMessageBatchSize = int.Parse(keyAndValue[1]);
                    if (connectionConfiguration.MaxReceiveMessageBatchSize <= 0 || connectionConfiguration.MaxReceiveMessageBatchSize > 10)
                    {
                        throw new ArgumentException("Max receive message batch size needs to be a number from 1 to 10.");
                    }
				}
				else if (keyAndValue[0].ToLower() == "truncatelongqueuenames")
				{
					connectionConfiguration.TruncateLongQueueNames = bool.Parse(keyAndValue[1]);
				}
				else if (keyAndValue[0].ToLower() == "queuenameprefix")
				{
					connectionConfiguration.QueueNamePrefix = keyAndValue[1];
				}
				else if (keyAndValue[0].ToLower() == "credentialsource")
				{
					connectionConfiguration.CredentialSource = (SqsCredentialSource)Enum.Parse(typeof(SqsCredentialSource), keyAndValue[1]);
				}
				else
				{
					throw new ArgumentException(String.Format("Unknown configuration key \"{0}\"", keyAndValue[0]));
				}
            }

			if (!string.IsNullOrEmpty(connectionConfiguration.S3BucketForLargeMessages) &&
				string.IsNullOrEmpty(connectionConfiguration.S3KeyPrefix))
			{
				throw new ArgumentException("An S3 bucket for large messages was specified, but no S3 key prefix was supplied. Supply an S3 key prefix.");
			}

            return connectionConfiguration;
        }
		public static IAmazonS3 CreateS3Client(SqsConnectionConfiguration connectionConfiguration)
		{
			return new AmazonS3Client(CreateCredentials(connectionConfiguration), connectionConfiguration.Region);
		}