Example #1
0
        public static void Main(string[] args)
        {
            var kernel = CastleConfiguration.Build();
            var runner = kernel.Resolve <IConsoleBackupRunner>();

            runner.Run();
        }
Example #2
0
        public void Should_get_default_from_http_request(HttpRequest request, CastleConfiguration cfg)
        {
            CastleConfiguration.SetConfiguration(cfg);

            var result = Context.FromHttpRequest(request);

            result.Should().NotBe(null);
        }
        public void Should_set_null_clientid_to_empty(ActionRequest request, CastleConfiguration options)
        {
            request.Context.ClientId = null;

            var result = request.PrepareApiCopy(options.Whitelist, options.Blacklist);

            result.Context.ClientId.Should().Be("");
        }
        public void Should_not_be_null_when_set(CastleConfiguration configuration)
        {
            CastleConfiguration.SetConfiguration(configuration);

            var result = CastleConfiguration.Configuration;

            result.Should().NotBe(null);
        }
        public void Should_create_fake_sender_if_do_not_track_is_enabled(CastleConfiguration configuration)
        {
            var logger = Substitute.For <IInternalLogger>();

            configuration.DoNotTrack = true;
            var result = MessageSenderFactory.Create(configuration, logger);

            result.Should().BeOfType <NoTrackMessageSender>();
        }
        public void Should_be_able_to_set_loglevel(CastleConfiguration configuration)
        {
            configuration.LogLevel = LogLevel.None;
            CastleConfiguration.SetConfiguration(configuration);

            var result = configuration.LogLevel;

            result.Should().Be(LogLevel.None);
        }
        public void Should_be_able_to_set_timeout(CastleConfiguration configuration, int timeout)
        {
            configuration.Timeout = timeout;
            CastleConfiguration.SetConfiguration(configuration);

            var result = configuration.Timeout;

            result.Should().Be(timeout);
        }
        public void Should_be_able_to_set_baseurl(CastleConfiguration configuration, string baseUrl)
        {
            configuration.BaseUrl = baseUrl;
            CastleConfiguration.SetConfiguration(configuration);

            var result = configuration.BaseUrl;

            result.Should().Be(baseUrl);
        }
        public void Should_be_able_to_set_denylist(CastleConfiguration configuration, string[] denyList)
        {
            configuration.DenyList = denyList;
            CastleConfiguration.SetConfiguration(configuration);

            var result = configuration.DenyList;

            result.Should().BeEquivalentTo(denyList);
        }
        public void Should_be_able_to_set_trustedproxies(CastleConfiguration configuration, string[] trustedProxies)
        {
            configuration.TrustedProxies = trustedProxies;
            CastleConfiguration.SetConfiguration(configuration);

            var result = configuration.TrustedProxies;

            result.Should().BeEquivalentTo(trustedProxies);
        }
Example #11
0
        /// <exception cref="ArgumentException">Thrown when <paramref name="configuration"/> is null</exception>
        public CastleClient(CastleConfiguration configuration)
        {
            ArgumentGuard.NotNull(configuration, nameof(configuration));

            _configuration = configuration;

            _logger = new LoggerWithLevels(configuration.Logger, configuration.LogLevel);

            _messageSender = MessageSenderFactory.Create(configuration, _logger);
        }
Example #12
0
        public async Task Should_return_response_if_successful(
            CastleConfiguration configuration)
        {
            var response = new VoidResponse();

            Task <VoidResponse> Send() => Task.FromResult(response);

            var result = await Track.Execute(Send, configuration);

            result.Should().Be(response);
        }
Example #13
0
        public void Should_get_first_available_with_all_trusted_proxies(CastleConfiguration cfg, string defaultIp)
        {
            var headers = new NameValueCollection
            {
                ["Remote-Addr"]     = "127.0.0.1",
                ["X-Forwarded-For"] = "127.0.0.1,10.0.0.1,172.31.0.1,192.168.0.1"
            };

            var result = Context.GetIpForFramework(headers, null, () => defaultIp, () => cfg);

            result.Should().Be("127.0.0.1");
        }
Example #14
0
        public void Should_get_remote_addr_if_others_internal(CastleConfiguration cfg, string defaultIp)
        {
            var headers = new NameValueCollection
            {
                ["Remote-Addr"]     = "6.5.4.3",
                ["X-Forwarded-For"] = "127.0.0.1,10.0.0.1,172.31.0.1,192.168.0.1"
            };

            var result = Context.GetIpForFramework(headers, null, () => defaultIp, () => cfg);

            result.Should().Be("6.5.4.3");
        }
        public async Task Should_return_response_if_successful(
            CastleConfiguration configuration,
            Verdict response)
        {
            Task <Verdict> Send() => Task.FromResult(response);

            var logger = Substitute.For <IInternalLogger>();

            var result = await Authenticate.Execute(Send, configuration, logger);

            result.Should().Be(response);
        }
        public async Task Should_log_failover_exception_as_warning(
            Exception exception,
            CastleConfiguration configuration)
        {
            configuration.FailOverStrategy = ActionType.Allow;
            var logger = Substitute.For <IInternalLogger>();

            Task <Verdict> Send() => throw exception;

            await Authenticate.Execute(Send, configuration, logger);

            logger.Received().Warn(Arg.Is <Func <string> >(x => x() == "Failover, " + exception));
        }
        public async Task Should_throw_exception_if_failing_over_with_no_strategy(
            Exception exception,
            CastleConfiguration configuration)
        {
            configuration.FailOverStrategy = ActionType.None;
            var logger = Substitute.For <IInternalLogger>();

            Task <Verdict> Send() => throw exception;

            Func <Task> act = async() => await Authenticate.Execute(Send, configuration, logger);

            await act.Should().ThrowAsync <CastleExternalException>();
        }
        public async Task Should_return_failover_response_if_any_exception(
            Exception exception,
            CastleConfiguration configuration)
        {
            configuration.FailOverStrategy = ActionType.Allow;
            var logger = Substitute.For <IInternalLogger>();

            Task <Verdict> Send() => throw exception;

            var result = await Authenticate.Execute(Send, configuration, logger);

            result.Failover.Should().Be(true);
            result.FailoverReason.Should().Be("server error");
        }
        public void Should_throw_timeout_exception_if_operation_cancelled(
            CastleConfiguration configuration,
            HttpMessageHandler handler)
        {
            var logger = Substitute.For <IInternalLogger>();
            var sut    = new HttpMessageSender(configuration, logger, handler);

            foreach (var testMethod in TestMethods)
            {
                Func <Task> act = async() => await testMethod(sut);

                act.Should().Throw <CastleTimeoutException>();
            }
        }
        public async Task Should_return_response_if_success_code(
            CastleConfiguration configuration,
            HttpMessageHandler handler)
        {
            var logger = Substitute.For <IInternalLogger>();
            var sut    = new HttpMessageSender(configuration, logger, handler);

            foreach (var testMethod in TestMethods)
            {
                var result = await testMethod(sut);

                result.Should().BeOfType <VoidResponse>();
            }
        }
Example #21
0
        public void Should_get_other_ip_header(CastleConfiguration cfg, string cfConnectiongIp)
        {
            var headers = new NameValueCollection
            {
                ["Cf-Connecting-Ip"] = cfConnectiongIp,
                ["X-Forwarded-For"]  = "1.1.1.1, 1.2.2.2, 1.2.3.5"
            };

            var ipHeaders = new[] { "Cf-Connecting-Ip", "X-Forwarded-For" };

            var result = Context.GetIpForFramework(headers, ipHeaders, () => cfConnectiongIp, () => cfg);

            result.Should().Be(cfConnectiongIp);
        }
Example #22
0
        public void Should_get_equivalent_to_trusted_proxy_depth_1(CastleConfiguration cfg, string defaultIp)
        {
            var headers = new NameValueCollection
            {
                ["Remote-Addr"]     = "6.6.6.4",
                ["X-Forwarded-For"] = "6.6.6.6, 2.2.2.3, 6.6.6.5"
            };

            cfg.TrustedProxyDepth = 1;

            var result = Context.GetIpForFramework(headers, null, () => defaultIp, () => cfg);

            result.Should().Be("2.2.2.3");
        }
        public async Task Should_return_failover_response_if_timeout(
            string requestUri,
            CastleConfiguration configuration)
        {
            configuration.FailOverStrategy = ActionType.Allow;
            var logger = Substitute.For <IInternalLogger>();

            Task <Verdict> Send() => throw new CastleTimeoutException(requestUri, configuration.Timeout);

            var result = await Authenticate.Execute(Send, configuration, logger);

            result.Failover.Should().Be(true);
            result.FailoverReason.Should().Be("timeout");
        }
Example #24
0
        public void Should_get_first_available_with_trust_proxy_chain(CastleConfiguration cfg, string defaultIp)
        {
            var headers = new NameValueCollection
            {
                ["Remote-Addr"]     = "6.6.6.4",
                ["X-Forwarded-For"] = "6.6.6.6, 2.2.2.3, 6.6.6.5"
            };

            cfg.TrustProxyChain = true;

            var result = Context.GetIpForFramework(headers, null, () => defaultIp, () => cfg);

            result.Should().Be("6.6.6.6");
        }
        public async Task Should_return_failover_response_if_do_not_track_is_set(
            CastleConfiguration configuration,
            Verdict response)
        {
            configuration.DoNotTrack       = true;
            configuration.FailOverStrategy = ActionType.Allow;
            var logger = Substitute.For <IInternalLogger>();

            Task <Verdict> Send() => Task.FromResult(response);

            var result = await Authenticate.Execute(Send, configuration, logger);

            result.Failover.Should().Be(true);
            result.FailoverReason.Should().Be("do not track");
        }
        /// <summary>
        ///     Configures the container wirth the specified file path.
        /// </summary>
        /// <param name="filePath"> The filePath. </param>
        public void Configure(string filePath)
        {
            if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
            {
                this.Container.Install(Configuration.FromXmlFile(filePath));
            }
            else
            {
                this.Container.Install();
            }

            this.container.Kernel.Resolver.AddSubResolver(new ArraySubDependencyResolver(this.container.Kernel));
            this.container.Kernel.Resolver.AddSubResolver(new LoggerSubDependencyResolver());
            CastleConfiguration.RegisterInterceptor(this.Container);
        }
Example #27
0
        public void Should_get_regular_ip(
            CastleConfiguration cfg,
            string ipHeader,
            string ip
            )
        {
            var headers = new Dictionary <string, StringValues>()
            {
                [ipHeader] = ip,
            };

            var result = Context.GetIpForCore(headers, null, () => ip, () => cfg);

            result.Should().Be(ip);
        }
Example #28
0
        public void Should_get_equivalent_to_trusted_proxy_depth_2_ip_headers(CastleConfiguration cfg, string defaultIp)
        {
            var headers = new Dictionary <string, StringValues>
            {
                ["Remote-Addr"]     = "6.6.6.4",
                ["X-Forwarded-For"] = "6.6.6.6, 2.2.2.3, 6.6.6.5, 6.6.6.7"
            };

            cfg.TrustedProxyDepth = 2;
            cfg.IpHeaders         = new[] { "X-Forwarded-For", "Remote-Addr" };

            var result = Context.GetIpForCore(headers, null, () => defaultIp, () => cfg);

            result.Should().Be("2.2.2.3");
        }
Example #29
0
        public void Should_get_regular_ip(
            CastleConfiguration cfg,
            string ipHeader,
            string ip
            )
        {
            var headers = new NameValueCollection
            {
                [ipHeader] = ip,
            };

            var result = Context.GetIpForFramework(headers, null, () => ip, () => cfg);

            result.Should().Be(ip);
        }
        internal HttpMessageSender(
            CastleConfiguration configuration,
            IInternalLogger logger,
            HttpMessageHandler handler)
        {
            _logger = logger;

            _httpClient = handler != null ? new HttpClient(handler) : new HttpClient();

            _httpClient.BaseAddress = new Uri(configuration.BaseUrl);
            _httpClient.Timeout     = TimeSpan.FromMilliseconds(configuration.Timeout);

            var authToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(":" + configuration.ApiSecret));

            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authToken);
        }