public void EndpointRegistry_should_load_endpoints_from_store()
        {
            var endpoint = new Endpoint(Guid.NewGuid(), MonitorMock.Mock("monitor"), "address", "name", "group");
            _configurationStore.Setup(s => s.LoadEndpoints(_monitorRegistry.Object)).Returns(new[] { endpoint });

            var registry = new EndpointRegistry(_monitorRegistry.Object, _configurationStore.Object,_statsRepository.Object);

            Assert.Same(endpoint, registry.GetById(endpoint.Id));
        }
        public void EndpointRegistry_should_load_endpoints_from_repository()
        {
            var endpoint = new Endpoint(_timeCoordinator.Object, new EndpointIdentity(Guid.NewGuid(), "monitor", "address"), new EndpointMetadata("name", "group", new[] { "t1", "t2" }, EndpointMetadata.DefaultMonitorTag, DateTime.UtcNow, DateTime.UtcNow));
            _configurationStore.Setup(s => s.LoadEndpoints()).Returns(new[] { endpoint });

            var registry = new EndpointRegistry(_healthMonitorTypeRegistry.Object, _configurationStore.Object, _statsManager.Object, _timeCoordinator.Object);

            Assert.Same(endpoint, registry.GetById(endpoint.Identity.Id));
        }
        public void EndpointRegistry_should_load_endpoints_from_repository()
        {
            var endpoint = new Endpoint(_timeCoordinator.Object, new EndpointIdentity(Guid.NewGuid(), "monitor", "address"), new EndpointMetadata("name", "group", new[] { "t1", "t2" }, EndpointMetadata.DefaultMonitorTag, DateTime.UtcNow, DateTime.UtcNow));

            _configurationStore.Setup(s => s.LoadEndpoints()).Returns(new[] { endpoint });

            var registry = new EndpointRegistry(_healthMonitorTypeRegistry.Object, _configurationStore.Object, _statsManager.Object, _timeCoordinator.Object);

            Assert.Same(endpoint, registry.GetById(endpoint.Identity.Id));
        }
        public void EndpointRegistry_should_load_endpoints_from_store()
        {
            var endpoint = new Endpoint(Guid.NewGuid(), MonitorMock.Mock("monitor"), "address", "name", "group");

            _configurationStore.Setup(s => s.LoadEndpoints(_monitorRegistry.Object)).Returns(new[] { endpoint });

            var registry = new EndpointRegistry(_monitorRegistry.Object, _configurationStore.Object, _statsRepository.Object);

            Assert.Same(endpoint, registry.GetById(endpoint.Id));
        }
        public void RegisterOrUpdate_should_update_existing_endpoint_and_return_same_id_but_not_emit_NewEndpointAdded_event()
        {
            MockMonitor("monitor");
            var id = _registry.RegisterOrUpdate("monitor", "address", "group", "name");

            Endpoint newEndpointCapture = null;

            _registry.NewEndpointAdded += e => { newEndpointCapture = e; };

            var id2 = _registry.RegisterOrUpdate("monitor", "ADDRESS", "group2", "name2");

            Assert.Equal(id, id2);
            Assert.Null(newEndpointCapture);

            var endpoint = _registry.GetById(id);

            Assert.NotNull(endpoint);
            Assert.Equal("monitor", endpoint.MonitorType);
            Assert.Equal("address", endpoint.Address);
            Assert.Equal("name2", endpoint.Name);
            Assert.Equal("group2", endpoint.Group);
        }
        public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
        {
            IPrincipal      principal = null;
            GenericIdentity identity;

            var credentials = context.ParseAuthorizationHeader();
            var adminCred   = CredentialsProvider.GetAdminCredentials();
            var pullCred    = CredentialsProvider.GetMonitorCredentials();

            if (credentials == null)
            {
                return(Task.FromResult(0));
            }

            if (credentials.Equals(adminCred))
            {
                identity  = new GenericIdentity(credentials.Id.ToString());
                principal = new GenericPrincipal(identity, new[] { SecurityRole.Admin.ToString() });
            }
            else if (credentials.Equals(pullCred))
            {
                identity  = new GenericIdentity(pullCred.Id.ToString());
                principal = new GenericPrincipal(identity, new[] { SecurityRole.Monitor.ToString() });
            }
            else
            {
                string encryptedPassword = credentials.Password.ToSha256Hash();
                var    endpoint          = EndpointRegistry.GetById(credentials.Id);

                if (endpoint?.Password == encryptedPassword)
                {
                    context.Request.Properties[_passwordKey] = encryptedPassword;
                    identity  = new GenericIdentity(credentials.Id.ToString());
                    principal = new GenericPrincipal(identity, null);
                }
            }

            context.Principal = principal;

            return(Task.FromResult(0));
        }
        public void RegisterOrUpdate_should_register_new_endpoint_which_should_be_retrievable_later_by_GetById()
        {
            SetupMonitors("monitor");

            var expectedLastModifiedTime = DateTime.UtcNow;

            _timeCoordinator.Setup(c => c.UtcNow).Returns(expectedLastModifiedTime);

            var endpointId = _registry.RegisterOrUpdate("monitor", "address", "group", "name", new[] { "t1" }, null, "password");

            Assert.NotEqual(Guid.Empty, endpointId);

            var endpoint = _registry.GetById(endpointId);

            Assert.NotNull(endpoint);
            Assert.Equal("monitor", endpoint.Identity.MonitorType);
            Assert.Equal("address", endpoint.Identity.Address);
            Assert.Equal("password".ToSha256Hash(), endpoint.Password);
            Assert.Equal("name", endpoint.Metadata.Name);
            Assert.Equal("group", endpoint.Metadata.Group);
            Assert.Equal(endpointId, endpoint.Identity.Id);
            Assert.Equal("t1", endpoint.Metadata.Tags[0]);
            Assert.Equal(expectedLastModifiedTime, endpoint.LastModifiedTimeUtc);
        }
        public void RegisterOrUpdate_should_register_new_endpoint_which_should_be_retrievable_later_by_GetById()
        {
            SetupMonitors("monitor");

            var id = _registry.RegisterOrUpdate("monitor", "address", "group", "name", new[] { "t1" });

            Assert.NotEqual(Guid.Empty, id);

            var endpoint = _registry.GetById(id);

            Assert.NotNull(endpoint);
            Assert.Equal("monitor", endpoint.Identity.MonitorType);
            Assert.Equal("address", endpoint.Identity.Address);
            Assert.Equal("name", endpoint.Metadata.Name);
            Assert.Equal("group", endpoint.Metadata.Group);
            Assert.Equal(id, endpoint.Identity.Id);
            Assert.Equal("t1", endpoint.Metadata.Tags[0]);
            Assert.True(endpoint.LastModifiedTime > DateTime.UtcNow.AddSeconds(-1), "LastModifiedTime should be updated");
        }