Exemplo n.º 1
0
        public void SetTopicConfigurationTests()
        {
            var s3Config = new AmazonS3Config();

            using (var s3Client = new AmazonS3Client(s3Config))
                using (var snsClient = new AmazonSimpleNotificationServiceClient())
                {
                    var snsCreateResponse = snsClient.CreateTopic("events-test-" + DateTime.Now.Ticks);
                    var bucketName        = S3TestUtils.CreateBucketWithWait(s3Client);

                    try
                    {
                        snsClient.AuthorizeS3ToPublish(snsCreateResponse.TopicArn, bucketName);

                        PutBucketNotificationRequest putRequest = new PutBucketNotificationRequest
                        {
                            BucketName          = bucketName,
                            TopicConfigurations = new List <TopicConfiguration>
                            {
                                new TopicConfiguration
                                {
                                    Id     = "the-topic-test",
                                    Topic  = snsCreateResponse.TopicArn,
                                    Events = new List <EventType> {
                                        EventType.ObjectCreatedPut
                                    }
                                }
                            }
                        };

                        s3Client.PutBucketNotification(putRequest);

                        var getResponse = S3TestUtils.WaitForConsistency(() =>
                        {
                            var res = s3Client.GetBucketNotification(bucketName);
                            return(res.TopicConfigurations?.Count > 0 && res.TopicConfigurations[0].Id == "the-topic-test" ? res : null);
                        });

                        Assert.AreEqual(1, getResponse.TopicConfigurations.Count);
                        Assert.AreEqual(1, getResponse.TopicConfigurations[0].Events.Count);
                        Assert.AreEqual(EventType.ObjectCreatedPut, getResponse.TopicConfigurations[0].Events[0]);

#pragma warning disable 618
                        Assert.AreEqual("s3:ObjectCreated:Put", getResponse.TopicConfigurations[0].Event);
#pragma warning restore 618
                        Assert.AreEqual("the-topic-test", getResponse.TopicConfigurations[0].Id);
                        Assert.AreEqual(snsCreateResponse.TopicArn, getResponse.TopicConfigurations[0].Topic);
                    }
                    finally
                    {
                        snsClient.DeleteTopic(snsCreateResponse.TopicArn);
                        AmazonS3Util.DeleteS3BucketWithObjects(s3Client, bucketName);
                    }
                }
        }
        public void SetTopicConfigurationTests()
        {
            var s3Config = new AmazonS3Config();

            using (var s3Client = new AmazonS3Client(s3Config))
                using (var snsClient = new AmazonSimpleNotificationServiceClient())
                    using (var stsClient = new AmazonSecurityTokenServiceClient())
                    {
                        var snsCreateResponse = snsClient.CreateTopic("events-test-" + DateTime.Now.Ticks);
                        var bucketName        = S3TestUtils.CreateBucketWithWait(s3Client);

                        try
                        {
                            snsClient.AuthorizeS3ToPublish(snsCreateResponse.TopicArn, bucketName);

                            PutBucketNotificationRequest putRequest = new PutBucketNotificationRequest
                            {
                                BucketName          = bucketName,
                                TopicConfigurations = new List <TopicConfiguration>
                                {
                                    new TopicConfiguration
                                    {
                                        Id     = "the-topic-test",
                                        Topic  = snsCreateResponse.TopicArn,
                                        Events = new List <EventType> {
                                            EventType.ObjectCreatedPut
                                        }
                                    }
                                }
                            };

                            s3Client.PutBucketNotification(putRequest);

                            var getResponse = S3TestUtils.WaitForConsistency(() =>
                            {
                                var res = s3Client.GetBucketNotification(bucketName);
                                return(res.TopicConfigurations?.Count > 0 && res.TopicConfigurations[0].Id == "the-topic-test" ? res : null);
                            });

                            var getAttributeResponse = snsClient.GetTopicAttributes(new GetTopicAttributesRequest
                            {
                                TopicArn = snsCreateResponse.TopicArn
                            });

                            var policy = Policy.FromJson(getAttributeResponse.Attributes["Policy"]);

                            // SNS topics already have a default statement. We need to evaluate the second statement that the SDK appended.
                            var conditions = policy.Statements[1].Conditions;
                            Assert.AreEqual(2, conditions.Count);

                            var accountCondition = conditions.FirstOrDefault(x => string.Equals(x.ConditionKey, ConditionFactory.SOURCE_ACCOUNT_KEY));
                            Assert.IsNotNull(accountCondition);
                            Assert.AreEqual(ConditionFactory.StringComparisonType.StringEquals.ToString(), accountCondition.Type);
                            Assert.AreEqual(12, accountCondition.Values[0].Length);

                            var currentAccountId = stsClient.GetCallerIdentity(new GetCallerIdentityRequest()).Account;
                            Assert.AreEqual(currentAccountId, accountCondition.Values[0]);

                            Assert.AreEqual(1, getResponse.TopicConfigurations.Count);
                            Assert.AreEqual(1, getResponse.TopicConfigurations[0].Events.Count);
                            Assert.AreEqual(EventType.ObjectCreatedPut, getResponse.TopicConfigurations[0].Events[0]);

#pragma warning disable 618
                            Assert.AreEqual("s3:ObjectCreated:Put", getResponse.TopicConfigurations[0].Event);
#pragma warning restore 618
                            Assert.AreEqual("the-topic-test", getResponse.TopicConfigurations[0].Id);
                            Assert.AreEqual(snsCreateResponse.TopicArn, getResponse.TopicConfigurations[0].Topic);
                        }
                        finally
                        {
                            snsClient.DeleteTopic(snsCreateResponse.TopicArn);
                            AmazonS3Util.DeleteS3BucketWithObjects(s3Client, bucketName);
                        }
                    }
        }