Exemplo n.º 1
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.AddSignalR();

            SnLog.Instance = new SnFileSystemEventLogger();
            SnTrace.SnTracers.Add(new SnFileSystemTracer());
            SnTrace.EnableAll();

            services.Configure <TaskManagementConfiguration>(Configuration.GetSection("TaskManagement"));

            //TODO: inject allowed origins dynamically (do not allow everything)
            services.AddCors(c =>
            {
                c.AddPolicy("AllowAllOrigins", options =>
                {
                    options.AllowAnyOrigin();
                    options.AllowAnyHeader();
                    options.AllowAnyMethod();
                });
            });

            services.AddSingleton <TaskDataHandler>();
            services.AddSenseNetClientTokenStore();
            services.AddSingleton <ApplicationHandler>();
            services.AddHostedService <DeadTaskHostedService>();
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            var builder = CreateWebHostBuilder(args);
            var host    = builder.Build();

            SnTrace.EnableAll();

            using (InMemoryExtensions.StartInMemoryRepository(repositoryBuilder =>
            {
                repositoryBuilder
                .UseAccessProvider(new UserAccessProvider())
                .UseLogger(new SnFileSystemEventLogger())
                .UseTracer(new SnFileSystemTracer());
            }))
            {
                using (new SystemAccount())
                {
                    // FOR TESTING PURPOSES: create a default user with a well-known password
                    // login:    [email protected]
                    // password: Edvin123%

                    var parentPath = "/Root/IMS/BuiltIn/Temp";
                    var parent     = RepositoryTools.CreateStructure(parentPath, "OrganizationalUnit");

                    var user = new User(parent.ContentHandler)
                    {
                        Name         = "edvin-example.com",
                        LoginName    = "*****@*****.**",
                        PasswordHash =
                            "AQAAAAEAACcQAAAAEKEsynr6baKE5rYqS4Rn6pjqckl+NG4W9UQqqGh4g23zlJQpQvnaZnzx44+z78FVsg==",
                        Email = "*****@*****.**"
                    };
                    user.Save();

                    // set the new user as administrator
                    Group.Administrators.AddMember(user);

                    // create a container for test content
                    parent = RepositoryTools.CreateStructure("/Root/MyContent", "SystemFolder");

                    // create a doclib that contains a file
                    var docLib = RepositoryTools.CreateStructure("/Root/MyContent/MyFiles", "DocumentLibrary");
                    ((GenericContent)docLib.ContentHandler).AllowChildType("Image", save: true);

                    var file = new File(docLib.ContentHandler)
                    {
                        Name = "testfile.txt"
                    };
                    file.Binary.SetStream(RepositoryTools.GetStreamFromString($"temp text data {DateTime.UtcNow}"));
                    file.Save();

                    //var installer = new Installer();
                    //installer.Import("C:\\temp\\import\\Root");
                }

                SnTrace.EnableAll();
                host.Run();
            }
        }
Exemplo n.º 3
0
        public void WriteLock_DeletionAfterKilledProcess()
        {
            var tracer = GetTracer();

            SnTrace.EnableAll();

            // Ensure an existing but free index directory
            SnTrace.Write("1 creating index");
            var indexDir = EnsureIndexDirectoryAsync().ConfigureAwait(false).GetAwaiter().GetResult();

            SnTrace.Write("2 index created");

            // Start a process that use the directory
            var exePath = Path.GetFullPath(Path.Combine(
                                               System.Reflection.Assembly.GetExecutingAssembly().Location,
                                               "..\\..\\..\\..\\WorkerForWriteLockDeletionTests\\bin\\Debug\\netcoreapp3.1\\WorkerForWriteLockDeletionTests.exe"));

            var process = Process.Start(exePath, $"100000 {indexDir.Name} {indexDir.Path}");

            if (process == null)
            {
                Assert.Fail("Cannot start the process.");
            }

            SnTrace.Write("3 process started");

            // Wait for the indexing engine uses the index
            while (!File.Exists(indexDir.LockPath))
            {
                Task.Delay(100).ConfigureAwait(false).GetAwaiter().GetResult();
            }
            SnTrace.Write("4 index locked");

            // Start the new indexing engine in async way.
            SnTrace.Write("5 starting new engine");
            Task.Run(() => { StartNewEngineAsync(indexDir.Name, indexDir.Path).ConfigureAwait(false); });

            SnTrace.Write("6 wait a bit");
            // Wait a bit
            Task.Delay(1000).ConfigureAwait(false).GetAwaiter().GetResult();

            // Check the console: the new engine cannot delete the write.lock file
            var lastLine1 = tracer.Lines[tracer.Lines.Count - 1];

            // Kill the lock owner process
            SnTrace.Write("7 killing the process");
            process.Kill();
//Task.Delay(20).ConfigureAwait(false).GetAwaiter().GetResult();
//File.Delete(indexDir.LockPath);
            SnTrace.Write("8 wait a bit");
            // lock file remains but deletable
            Task.Delay(2000).ConfigureAwait(false).GetAwaiter().GetResult();

            SnTrace.Write("9 test finished");

            // Check the console: the new engine has started
            Assert.IsTrue(tracer.Lines.Any(x => x.EndsWith("101 started")));
        }
Exemplo n.º 4
0
        // ReSharper disable once UnusedParameter.Local
        static async Task Main(string[] args)
        {
            IConfiguration config = new ConfigurationBuilder()
                                    .AddJsonFile("appsettings.json", true, true)
                                    .Build();

            config.GetSection("TaskManagement").Bind(AgentConfig);

            SnLog.Instance = new SnFileSystemEventLogger();
            SnTrace.SnTracers.Add(new SnFileSystemTracer());
            SnTrace.EnableAll();

            AgentName = AgentManager.GetAgentName();

            ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;

            try
            {
                DiscoverCapabilities();

                _updateLockTimer    = new Timer(UpdateLockTimerElapsed);
                _watchExecutorTimer = new Timer(WatchExecutorTimerElapsed);

                var started = await StartSignalR();

                _heartBeatTimerPeriodInMilliseconds = AgentConfig.HeartbeatPeriodInSeconds * 1000;
                _heartbeatTimer = new Timer(HeartBeatTimerElapsed, null, _heartBeatTimerPeriodInMilliseconds, _heartBeatTimerPeriodInMilliseconds);

#pragma warning disable 4014
                // start processing on a background thread
                if (started)
                {
                    WorkAsync();
                }
#pragma warning restore 4014

                Console.ReadLine();

                if (_hubConnection != null)
                {
                    await _hubConnection.DisposeAsync();
                }
            }
            catch (Exception ex)
            {
                SnLog.WriteException(ex, string.Empty, EventId.TaskManagement.General);
            }

            _heartbeatTimer?.Change(Timeout.Infinite, Timeout.Infinite);
            _heartbeatTimer?.Dispose();
        }
Exemplo n.º 5
0
        public void _StartTest(TestContext testContext)
        {
            //if (!SnTrace.SnTracers.Any(x => x is SnDebugViewTracer))
            //    SnTrace.SnTracers.Add(new SnDebugViewTracer());
            if (!SnTrace.SnTracers.Any(x => x is SnFileSystemTracer))
            {
                SnTrace.SnTracers.Add(new SnFileSystemTracer());
            }
            SnTrace.EnableAll();
            SnTrace.SecurityQueue.Enabled = false;

            _snTraceOperation =
                SnTrace.Test.StartOperation(
                    $"TESTMETHOD: {testContext.FullyQualifiedTestClassName}.{testContext.TestName}");
        }
Exemplo n.º 6
0
        private static async Task <bool> InitializeAsync()
        {
            ServiceProvider = new ServiceCollection()
                              .AddLogging()
                              .AddSenseNetClientTokenStore()
                              .BuildServiceProvider();

            SnTrace.EnableAll();
            Configuration.Initialize();

            SnTrace.SnTracers.Add(new SnFileSystemTracer());

            ServicePointManager.DefaultConnectionLimit = 10;

            ClientContext.Current.ChunkSizeInBytes = Config.Upload.ChunkSize;
            ClientContext.Current.AddServer(new ServerContext
            {
                Url       = SiteUrl,
                IsTrusted = Config.Environment.IsDevelopment
            });

            try
            {
                var tokenStore = ServiceProvider.GetRequiredService <TokenStore>();
                var secret     = string.IsNullOrEmpty(Password) ? "secret" : Password;
                var token      = await tokenStore.GetTokenAsync(ClientContext.Current.Server, secret);

                ClientContext.Current.Server.Authentication.AccessToken = token;

                if (string.IsNullOrEmpty(token))
                {
                    SnTrace.System.Write("Access token is empty, fallback to user name and password.");

                    ClientContext.Current.Server.Username = Username;
                    ClientContext.Current.Server.Password = Password;
                }
            }
            catch (Exception ex)
            {
                Logger.WriteError(ContentId, 0, ex: ex, startIndex: StartIndex, version: Version, message:
                                  $"Authentication failed for site {SiteUrl}: {ex.Message}");
                return(false);
            }

            return(true);
        }
Exemplo n.º 7
0
        public static void Main(string[] args)
        {
            var builder = CreateWebHostBuilder(args);
            var host    = builder.Build();

            SnTrace.EnableAll();

            using (InMemoryExtensions.StartInMemoryRepository(repositoryBuilder =>
            {
                repositoryBuilder
                .UseAccessProvider(new DesktopAccessProvider())
                .UseLogger(new SnFileSystemEventLogger())
                .UseTracer(new SnFileSystemTracer());
            }))
            {
                SnTrace.EnableAll();
                host.Run();
            }
        }
        static void Main(string[] args)
        {
            var service = new SearchWindowsService();

            if (Environment.UserInteractive)
            {
                SnTrace.SnTracers.Clear();
                SnTrace.SnTracers.Add(new SnFileSystemTracer());
                SnTrace.EnableAll();

                Console.WriteLine("Starting service.");
                service.OnStart(args);
                Console.WriteLine("Service started.");
                Console.WriteLine("Press any key to stop.");
                Console.ReadKey();
                service.OnStop();
                Console.WriteLine("Service stopped.");
            }
            else
            {
                Run(service);
            }
        }
Exemplo n.º 9
0
 protected void CleanupAndEnableAll()
 {
     CleanupLog();
     SnTrace.EnableAll();
 }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            args = new[] { TestType.Backup.ToString() };

            if (args.Length == 0)
            {
                Console.WriteLine("Missing test type. Expected 'Backup', 'Restore', 'Validity' or 'Cancellation'.");
                return;
            }
            if (!Enum.TryParse <TestType>(args[0], true, out _testType))
            {
                Console.WriteLine("Invalid test type. Expected 'Backup', 'Restore' or 'Validity'.");
                return;
            }

            _serviceIndexDirectory = Path.GetFullPath($"{Environment.CurrentDirectory}\\..\\..\\..\\..\\..\\" +
                                                      //"SenseNet.Search.Lucene29.Centralized.Service\\" +
                                                      "SenseNet.Search.Lucene29.Centralized.GrpcService\\" +
                                                      "bin\\Debug\\netcoreapp3.1\\App_Data\\LocalIndex");
            Console.WriteLine("IndexDirectory of the service: ");
            Console.WriteLine(_serviceIndexDirectory);

            _backupIndexDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data", "IndexBackup");
            Console.WriteLine("Backup directory: ");
            Console.WriteLine(_backupIndexDirectory);

            IConfiguration configuration = new ConfigurationBuilder()
                                           .AddJsonFile("appsettings.json", true, true)
                                           .AddEnvironmentVariables()
                                           .Build();

            //var serviceBinding = new NetTcpBinding { Security = { Mode = SecurityMode.None } };
            //var serviceEndpoint = new EndpointAddress(configuration["sensenet:search:service:address"]);
            //WaitForServiceStarted(serviceBinding, serviceEndpoint);

            var sender = new MessageSenderManager();

            var builder = new RepositoryBuilder()
                          .SetConsole(Console.Out)
                          .UseLogger(new SnFileSystemEventLogger())
                          .UseTracer(new SnFileSystemTracer())
                          .UseConfiguration(configuration)
                          .UseDataProvider(new MsSqlDataProvider(Options.Create(ConnectionStringOptions.GetLegacyConnectionStrings())))
                          .UseSecurityDataProvider(new EFCSecurityDataProvider(sender,
                                                                               Options.Create(new SenseNet.Security.EFCSecurityStore.Configuration.DataOptions()
            {
                ConnectionString = ConnectionStrings.ConnectionString
            }),
                                                                               NullLogger <EFCSecurityDataProvider> .Instance))
                          .UseSecurityMessageProvider(new RabbitMQMessageProvider(sender,
                                                                                  Options.Create(new MessagingOptions()),
                                                                                  Options.Create(new RabbitMqOptions())))
                          .UseLucene29CentralizedSearchEngineWithGrpc(configuration["sensenet:search:service:address"], options =>
            {
                // trust the server in a development environment
                options.HttpClient = new HttpClient(new HttpClientHandler
                {
                    ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true
                });
                options.DisposeHttpClient = true;
            })
                          .StartWorkflowEngine(false)
                          .DisableNodeObservers()
                          .UseTraceCategories(SnTrace.Categories.Select(x => x.Name).ToArray()) as RepositoryBuilder;

            using (Repository.Start(builder))
            {
                Console.WriteLine("CHECK SQL SERVER CONNECTIVITY (query top level nodes):");
                var root = Node.LoadNode(Repository.RootPath);
                foreach (var node in NodeQuery.QueryChildren(Repository.RootPath).Nodes)
                {
                    Console.WriteLine(node.Path);
                }
                Console.WriteLine();

                Console.WriteLine("CHECK SEARCH SERVER CONNECTIVITY (query top level nodes):");
                var queryContext = new SnQueryContext(QuerySettings.AdminSettings, User.Current.Id);
                var result       = SnQuery.Query("InFolder:/Root", queryContext);
                foreach (var id in result.Hits)
                {
                    Console.WriteLine(NodeHead.Get(id).Path);
                }
                Console.WriteLine();

                SnTrace.EnableAll();

                var engine = (ILuceneIndexingEngine)Providers.Instance.SearchEngine.IndexingEngine;
                switch (_testType)
                {
                case TestType.Backup:
                    new ContinuousIndexTest(engine).RunAsync(CancellationToken.None)
                    .ConfigureAwait(false).GetAwaiter().GetResult();
                    break;

                case TestType.Restore:
                    new RestoreTest(engine).RunAsync(CancellationToken.None)
                    .ConfigureAwait(false).GetAwaiter().GetResult();
                    break;

                case TestType.Validity:
                    new ValidityTest(engine).RunAsync(CancellationToken.None)
                    .ConfigureAwait(false).GetAwaiter().GetResult();
                    break;

                case TestType.Cancellation:
                    new CancellationTest(engine).RunAsync(CancellationToken.None)
                    .ConfigureAwait(false).GetAwaiter().GetResult();
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                // Shut down the service to leave the index.
                IndexManager.IndexingEngine.ShutDownAsync(CancellationToken.None)
                .ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }
Exemplo n.º 11
0
        static void CheckIndexIntegrity(string indexDirectory, IConfiguration config)
        {
            var builder = new RepositoryBuilder()
                          .SetConsole(Console.Out)
                          .UseLogger(new SnFileSystemEventLogger())
                          .UseTracer(new SnFileSystemTracer())
                          .UseConfiguration(config)
                          .UseDataProvider(new MsSqlDataProvider(Options.Create(ConnectionStringOptions.GetLegacyConnectionStrings())))
                          .UseSecurityDataProvider(new EFCSecurityDataProvider(
                                                       new MessageSenderManager(),
                                                       Options.Create(new SenseNet.Security.EFCSecurityStore.Configuration.DataOptions()
            {
                ConnectionString = ConnectionStrings.ConnectionString
            }),
                                                       NullLogger <EFCSecurityDataProvider> .Instance))
                          .UseLucene29LocalSearchEngine(indexDirectory)
                          .UseTraceCategories(SnTrace.Categories.Select(x => x.Name).ToArray()) as RepositoryBuilder;

            using (Repository.Start(builder))
            {
                SnTrace.EnableAll();

                Console.WriteLine("================================");

                var savedIndexDir = Path.Combine(Environment.CurrentDirectory, "App_Data", "SavedIndex");
                if (!Directory.Exists(savedIndexDir))
                {
                    Directory.CreateDirectory(savedIndexDir);
                }

                Console.Write("Saving index: ");
                SaveIndex(savedIndexDir);
                Console.WriteLine("ok.");

                Console.Write("Index integrity: ");
                var diffs = IndexIntegrityChecker.Check().ToArray();

                var diffPath = Path.Combine(savedIndexDir, "indexIntegrity.txt");
                using (var writer = new StreamWriter(diffPath, false))
                {
                    if (diffs.Length != 0)
                    {
                        foreach (var diff in diffs)
                        {
                            writer.WriteLine($"  {diff}");
                        }
                    }
                    else
                    {
                        writer.WriteLine($"There is no any differences.");
                    }
                }

                if (diffs.Length != 0)
                {
                    Console.WriteLine($"check index integrity failed. Diff count: {diffs.Length}");
                    var count = 0;
                    foreach (var diff in diffs)
                    {
                        Console.WriteLine($"  {diff}");
                        if (++count > 20)
                        {
                            Console.WriteLine("  ...etc");
                            break;
                        }
                    }
                }
                else
                {
                    Console.WriteLine("ok.");
                }
                Console.WriteLine("================================");
            }
        }