Exemplo n.º 1
0
        public void Parsing_queue_name_prefix_works()
        {
            var extensions = new TransportExtensions <SqsTransport>(new SettingsHolder());

            var result = extensions.QueueNamePrefix("DEV");

            Assert.AreEqual("DEV", result.GetSettings().Get("NServiceBus.AmazonSQS.QueueNamePrefix"));
        }
        public override TransportInfrastructure Initialize(SettingsHolder settings, string connectionString)
        {
            var builder = new DbConnectionStringBuilder {
                ConnectionString = connectionString
            };

            if (builder.ContainsKey("AccessKeyId") || builder.ContainsKey("SecretAccessKey"))
            {
                PromoteEnvironmentVariableFromConnectionString(builder, "AccessKeyId", "AWS_ACCESS_KEY_ID");
                PromoteEnvironmentVariableFromConnectionString(builder, "SecretAccessKey", "AWS_SECRET_ACCESS_KEY");

                // if the user provided the access key and secret access key they should always be loaded from environment credentials
                var transport = new TransportExtensions <SqsTransport>(settings);
                transport.ClientFactory(() => new AmazonSQSClient(new EnvironmentVariablesAWSCredentials()));
            }
            else
            {
                //See https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-creds.html#creds-assign
                log.Info("BasicAWSCredentials have not been supplied in the connection string. Attempting to use existing environment or IAM role credentials.");
            }

            var region = PromoteEnvironmentVariableFromConnectionString(builder, "Region", "AWS_REGION");

            var awsRegion = RegionEndpoint.EnumerableAllRegions
                            .SingleOrDefault(x => x.SystemName == region);

            if (awsRegion == null)
            {
                throw new ArgumentException($"Unknown region: \"{region}\"");
            }

            settings.Set("NServiceBus.AmazonSQS.Region", awsRegion);

            if (builder.TryGetValue("QueueNamePrefix", out var queueNamePrefix))
            {
                var queueNamePrefixAsString = (string)queueNamePrefix;
                if (!string.IsNullOrEmpty(queueNamePrefixAsString))
                {
                    var extensions = new TransportExtensions <SqsTransport>(settings);
                    extensions.QueueNamePrefix(queueNamePrefixAsString);
                }
            }

            //HINT: This is needed to make sure Core doesn't load a connection string value from the app.config.
            //      This prevents SQS from throwing on startup.
            var connectionStringSetting = settings.Get("NServiceBus.TransportConnectionString");

            connectionStringSetting.GetType()
            .GetField("GetValue", BindingFlags.NonPublic | BindingFlags.Instance)
            ?.SetValue(connectionStringSetting, (Func <string>)(() => null));

            // SQS doesn't support connection strings so pass in null.
            return(base.Initialize(settings, null));
        }
        public static void ConfigureSqsTransport(this TransportExtensions <SqsTransport> transportConfiguration, string queueNamePrefix = null)
        {
            transportConfiguration.ClientFactory(ClientFactory.CreateSqsClient);
            if (queueNamePrefix != null)
            {
                transportConfiguration.QueueNamePrefix(queueNamePrefix);
            }

            var s3BucketName = EnvironmentHelper.GetEnvironmentVariable(S3BucketEnvironmentVariableName);

            if (!string.IsNullOrEmpty(S3BucketName))
            {
                transportConfiguration.S3(S3BucketName, "test");
            }
        }
        static void ConfigureTransport(TransportExtensions <SqsTransport> transport, TransportSettings transportSettings)
        {
            var builder = new DbConnectionStringBuilder {
                ConnectionString = transportSettings.ConnectionString
            };

            PromoteEnvironmentVariableFromConnectionString(builder, "AccessKeyId", "AWS_ACCESS_KEY_ID");
            PromoteEnvironmentVariableFromConnectionString(builder, "SecretAccessKey", "AWS_SECRET_ACCESS_KEY");
            var region = PromoteEnvironmentVariableFromConnectionString(builder, "Region", "AWS_REGION");

            var awsRegion = RegionEndpoint.EnumerableAllRegions
                            .SingleOrDefault(x => x.SystemName == region);

            if (awsRegion == null)
            {
                throw new ArgumentException($"Unknown region: \"{region}\"");
            }

            if (builder.TryGetValue("QueueNamePrefix", out var queueNamePrefix))
            {
                var queueNamePrefixAsString = (string)queueNamePrefix;
                if (!string.IsNullOrEmpty(queueNamePrefixAsString))
                {
                    transport.QueueNamePrefix(queueNamePrefixAsString);
                }
            }

            //HINT: This is needed to make Core doesn't load a connection string value from the app.config.
            //      This prevents SQS from throwing on startup.
            var connectionString = transport.GetSettings().Get("NServiceBus.TransportConnectionString");

            connectionString.GetType()
            .GetField("GetValue", BindingFlags.NonPublic | BindingFlags.Instance)
            ?.SetValue(connectionString, (Func <string>)(() => null));

            transport.Transactions(TransportTransactionMode.ReceiveOnly);
        }
        static void ConfigureTransport(TransportExtensions <SqsTransport> transport, TransportSettings transportSettings)
        {
            var builder = new DbConnectionStringBuilder {
                ConnectionString = transportSettings.ConnectionString
            };

            var alwaysLoadFromEnvironmentVariable = false;

            if (builder.ContainsKey("AccessKeyId") || builder.ContainsKey("SecretAccessKey"))
            {
                PromoteEnvironmentVariableFromConnectionString(builder, "AccessKeyId", "AWS_ACCESS_KEY_ID");
                PromoteEnvironmentVariableFromConnectionString(builder, "SecretAccessKey", "AWS_SECRET_ACCESS_KEY");

                // if the user provided the access key and secret access key they should always be loaded from environment credentials
                alwaysLoadFromEnvironmentVariable = true;
                transport.ClientFactory(() => new AmazonSQSClient(new EnvironmentVariablesAWSCredentials()));
            }
            else
            {
                //See https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-creds.html#creds-assign
                log.Info("BasicAWSCredentials have not been supplied in the connection string. Attempting to use existing environment or IAM role credentials for SQS Client.");
            }

            var region = PromoteEnvironmentVariableFromConnectionString(builder, "Region", "AWS_REGION");

            var awsRegion = RegionEndpoint.EnumerableAllRegions
                            .SingleOrDefault(x => x.SystemName == region);

            if (awsRegion == null)
            {
                throw new ArgumentException($"Unknown region: \"{region}\"");
            }

            if (builder.TryGetValue("QueueNamePrefix", out var queueNamePrefix))
            {
                var queueNamePrefixAsString = (string)queueNamePrefix;
                if (!string.IsNullOrEmpty(queueNamePrefixAsString))
                {
                    transport.QueueNamePrefix(queueNamePrefixAsString);
                }
            }

            if (builder.TryGetValue("S3BucketForLargeMessages", out var bucketForLargeMessages))
            {
                var bucketForLargeMessagesAsString = (string)bucketForLargeMessages;
                if (!string.IsNullOrEmpty(bucketForLargeMessagesAsString))
                {
                    var keyPrefixAsString = string.Empty;
                    if (builder.TryGetValue("S3KeyPrefix", out var keyPrefix))
                    {
                        keyPrefixAsString = (string)keyPrefix;
                    }

                    var s3Settings = transport.S3(bucketForLargeMessagesAsString, keyPrefixAsString);
                    if (alwaysLoadFromEnvironmentVariable)
                    {
                        s3Settings.ClientFactory(() => new AmazonS3Client(new EnvironmentVariablesAWSCredentials()));
                    }
                    else
                    {
                        log.Info("BasicAWSCredentials have not been supplied in the connection string. Attempting to use existing environment or IAM role credentials for S3 Client.");
                    }
                }
            }

            //HINT: This is needed to make Core doesn't load a connection string value from the app.config.
            //      This prevents SQS from throwing on startup.
            var connectionString = transport.GetSettings().Get("NServiceBus.TransportConnectionString");

            connectionString.GetType()
            .GetField("GetValue", BindingFlags.NonPublic | BindingFlags.Instance)
            ?.SetValue(connectionString, (Func <string>)(() => null));

            transport.Transactions(TransportTransactionMode.ReceiveOnly);
        }