public void Start(string use_backend = "sqlite") { tmpPath = "/tmp/rainy-test-data/"; Directory.CreateDirectory(tmpPath); DbConfig.SetSqliteFile(Path.Combine(tmpPath, "rainy-test.db")); // tmpPath = Path.GetTempPath () + Path.GetRandomFileName (); IDataBackend backend; if (use_backend == "sqlite") { backend = new DatabaseBackend(tmpPath, credentialsVerifier); DbConfig.CreateSchema(reset: true); using (var c = DbConfig.GetConnection()) { c.Insert <DBUser>(new DBUser() { Username = TEST_USER }); } } else { backend = new RainyFileSystemBackend(tmpPath, credentialsVerifier); } rainyServer = new RainyStandaloneServer(backend, RainyListenUrl); rainyServer.Start(); }
private void WireupGenericTestClasses(Funq.Container container) { container.Register <IAuthenticator> (c => { var factory = c.Resolve <IDbConnectionFactory> (); var dbauth = new DbAuthenticator(factory); //var dbauth = new DbTestAuthenticator (); var test_user = new DBUser { Username = RainyTestServer.TEST_USER, IsActivated = true, IsVerified = true }; test_user.CreateCryptoFields(RainyTestServer.TEST_PASS); // insert a dummy testuser using (var db = factory.OpenDbConnection()) { db.InsertParam <DBUser> (test_user); } return(dbauth); }); container.Register <IAdminAuthenticator> (c => { var admin_auth = new DummyAdminAuthenticator(ADMIN_TEST_PASS); return((IAdminAuthenticator)admin_auth); }); RegisterStorageFactory(container, false); container.Register <IDataBackend> (c => { var conn_factory = c.Resolve <IDbConnectionFactory> (); var storage_factory = c.Resolve <DbStorageFactory> (); var auth = c.Resolve <IAuthenticator> (); var handler = c.Resolve <OAuthHandler> (); return(new DatabaseBackend(conn_factory, storage_factory, auth, handler)); }); container.Register <OAuthHandler> (c => { var auth = c.Resolve <IAuthenticator> (); var factory = c.Resolve <IDbConnectionFactory> (); // ITokenRepository<AccessToken> access_tokens = new SimpleTokenRepository<AccessToken> (); // ITokenRepository<RequestToken> request_tokens = new SimpleTokenRepository<RequestToken> (); ITokenRepository <AccessToken> access_tokens = new DbAccessTokenRepository <AccessToken> (factory); ITokenRepository <RequestToken> request_tokens = new DbRequestTokenRepository <RequestToken> (factory); ITokenStore token_store = new RainyTokenStore(access_tokens, request_tokens); OAuthHandler handler = new OAuthHandler(auth, access_tokens, request_tokens, token_store); return(handler); }); var connFactory = container.Resolve <IDbConnectionFactory> (); DatabaseBackend.CreateSchema(connFactory, true); // HACK so the user is inserted when a fixture SetUp is run container.Resolve <IAuthenticator> (); }
// This is the "Composition Root" in the IoC pattern that wires all // our objects/implementations up, based on a given configuration. // config sanity checks SHOULD NOT go here private static void ComposeObjectGraph(Funq.Container container) { var config = Config.Global; container.Register <SqliteConfig> (c => new SqliteConfig { File = Path.Combine(config.DataPath, "rainy.db") }); container.Register <PostgreConfig> (c => { dynamic txt_conf = config.Postgre; var psql_conf = new PostgreConfig(); if (!string.IsNullOrEmpty(txt_conf.Username)) { psql_conf.Username = txt_conf.Username; } if (!string.IsNullOrEmpty(txt_conf.Password)) { psql_conf.Password = txt_conf.Password; } if (!string.IsNullOrEmpty(txt_conf.Database)) { psql_conf.Database = txt_conf.Database; } if (!string.IsNullOrEmpty(txt_conf.Host)) { psql_conf.Host = txt_conf.Host; } if (txt_conf.Port > 0) { psql_conf.Port = (uint)txt_conf.Port; } return(psql_conf); }); if (config.Backend == "xml") { // use username/password pairs from the config file container.Register <IAuthenticator> (c => { return(new ConfigFileAuthenticator(config.User)); }); // we store notes in XML files in the DataPath container.Register <IDataBackend> (c => { var auth = c.Resolve <IAuthenticator> (); var factory = c.Resolve <IDbConnectionFactory> (); var oauth_handler = c.Resolve <OAuthHandler> (); return(new FileSystemBackend(config.DataPath, factory, auth, oauth_handler, false)); }); } else { // database based backends switch ((string)config.Backend) { case "sqlite": container.Register <IDbConnectionFactory> (c => { var conf = container.Resolve <SqliteConfig> (); var connection_string = conf.ConnectionString; var factory = new OrmLiteConnectionFactory(connection_string, SqliteDialect.Provider); if (!File.Exists(conf.File)) { DatabaseBackend.CreateSchema(factory); } return((IDbConnectionFactory)factory); }); break; case "postgre": container.Register <IDbConnectionFactory> (c => { var connection_string = container.Resolve <PostgreConfig> ().ConnectionString; var factory = new OrmLiteConnectionFactory(connection_string, PostgreSqlDialect.Provider); DatabaseBackend.CreateSchema(factory); return(factory); }); break; } container.Register <IAuthenticator> (c => { var factory = c.Resolve <IDbConnectionFactory> (); // var sfactory = new OrmLiteConnectionFactory (); var dbauth = new DbAuthenticator(factory); //var dbauth = new ConfigFileAuthenticator (Config.Global.Users); // we have to make sure users from the config file exist with the configured password // in the db // TODO delete old users? or restrict to webinterface? if (dbauth is ConfigFileAuthenticator) { foreach (dynamic user in Config.Global.Users) { string username = user.Username; string password = user.Password; using (var db = factory.OpenDbConnection()) { var db_user = db.FirstOrDefault <DBUser> (u => u.Username == username); if (db_user != null) { var need_update = db_user.UpdatePassword(password); if (need_update) { db.UpdateOnly(new DBUser { PasswordHash = db_user.PasswordHash }, u => new { u.PasswordHash }, (DBUser p) => p.Username == username); } } else { // create the user in the db var new_user = new DBUser(); new_user.Username = username; new_user.CreateCryptoFields(password); new_user.UpdatePassword(password); db.Insert <DBUser> (new_user); } } } } return(dbauth); }); // container.Register <IAdminAuthenticator> (c => { var auth = new ConfigFileAdminAuthenticator(); return(auth); }); container.Register <OAuthHandler> (c => { var auth = c.Resolve <IAuthenticator> (); var factory = c.Resolve <IDbConnectionFactory> (); // ITokenRepository<AccessToken> access_tokens = new SimpleTokenRepository<AccessToken> (); // ITokenRepository<RequestToken> request_tokens = new SimpleTokenRepository<RequestToken> (); ITokenRepository <AccessToken> access_tokens = new DbAccessTokenRepository <AccessToken> (factory); ITokenRepository <RequestToken> request_tokens = new DbRequestTokenRepository <RequestToken> (factory); ITokenStore token_store = new RainyTokenStore(access_tokens, request_tokens); OAuthHandler handler = new OAuthHandler(auth, access_tokens, request_tokens, token_store); return(handler); }); container.Register <DbStorageFactory> (c => { var conn_factory = c.Resolve <IDbConnectionFactory> (); bool use_encryption = (bool)Config.Global.UseNoteEncryption; var storage_factory = new DbStorageFactory(conn_factory, use_encryption, use_history: true); return(storage_factory); }); container.Register <IDataBackend> (c => { var conn_factory = c.Resolve <IDbConnectionFactory> (); var storage_factory = c.Resolve <DbStorageFactory> (); var handler = c.Resolve <OAuthHandler> (); var auth = c.Resolve <IAuthenticator> (); return(new DatabaseBackend(conn_factory, storage_factory, auth, handler)); }); /* container.Register<OAuthHandler> (c => { * var factory = c.Resolve<IDbConnectionFactory> (); * var access_token_repo = new DbAccessTokenRepository<AccessToken> (factory); * var request_token_repo = new SimpleTokenRepository<RequestToken> (); * var auth = c.Resolve<IAuthenticator> (); * var token_store = new Rainy.OAuth.SimpleStore.SimpleTokenStore (access_token_repo, request_token_repo); * * var handler = new OAuthHandler (auth, token_store); * return handler; * }); */ if (config.Development == true) { AddDummyUserIfRequired(container); } } }
public void Start(string use_backend = "sqlite") { tmpPath = "/tmp/rainy-test-data/"; Directory.CreateDirectory (tmpPath); DbConfig.SetSqliteFile (Path.Combine (tmpPath, "rainy-test.db")); // tmpPath = Path.GetTempPath () + Path.GetRandomFileName (); IDataBackend backend; if (use_backend == "sqlite") { backend = new DatabaseBackend (tmpPath, credentialsVerifier); DbConfig.CreateSchema(reset: true); using (var c = DbConfig.GetConnection ()) { c.Insert<DBUser>(new DBUser() { Username = TEST_USER }); } } else backend = new RainyFileSystemBackend (tmpPath, credentialsVerifier); rainyServer = new RainyStandaloneServer (backend, RainyListenUrl); rainyServer.Start (); }
public static void Main(string[] args) { // parse command line arguments string config_file = "settings.conf"; string cert_file = null, pvk_file = null; int loglevel = 0; bool show_help = false; bool open_browser = false; var p = new OptionSet() { { "c|config=", "use config file", (string file) => config_file = file }, { "v", "increase log level, where -vvvv is highest", v => { if (v != null) { ++loglevel; } } }, { "h|help", "show this message and exit", v => show_help = v != null }, { "cert=", "use this certificate for SSL", (string file) => cert_file = file }, { "pvk=", "use private key for certSSL", (string file2) => pvk_file = file2 }, //{ "b|nobrowser", "do not open browser window upon start", // v => { if (v != null) open_browser = false; } }, }; p.Parse(args); if (show_help) { p.WriteOptionDescriptions(Console.Out); return; } if (!File.Exists(config_file)) { Console.WriteLine("Could not find a configuration file (try the -c flag)!"); return; } // set the configuration from the specified file Config.Global = Config.ApplyJsonFromPath(config_file); DataPath = Config.Global.DataPath; if (string.IsNullOrEmpty(DataPath)) { DataPath = Directory.GetCurrentDirectory(); } else { if (!Directory.Exists(DataPath)) { Directory.CreateDirectory(DataPath); } } var sqlite_file = Path.Combine(DataPath, "rainy.db"); DbConfig.SetSqliteFile(sqlite_file); SetupLogging(loglevel); logger = LogManager.GetLogger("Main"); string listen_url = Config.Global.ListenUrl; if (string.IsNullOrEmpty(listen_url)) { listen_url = "https://localhost:443/"; logger.InfoFormat("no ListenUrl set in the settings.conf, using the default: {0}", listen_url); } // servicestack expects trailing slash, else error is thrown if (!listen_url.EndsWith("/")) { listen_url += "/"; } ConfigureSslCerts(listen_url, cert_file, pvk_file); // determine and setup data backend string backend = Config.Global.Backend; IDataBackend data_backend; // simply use user/password list from config for authentication CredentialsVerifier config_authenticator = (username, password) => { // call the authenticater callback if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) { return(false); } foreach (dynamic credentials in Config.Global.Users) { if (credentials.Username == username && credentials.Password == password) { return(true); } } return(false); }; // by default we use the filesystem backend if (string.IsNullOrEmpty(backend)) { backend = "filesystem"; } if (backend == "sqlite") { /* if (string.IsNullOrEmpty (Config.Global.AdminPassword)) { * Console.WriteLine ("FATAL: Field 'AdminPassword' in the settings config may not " + * "be empty when using the sqlite backend"); * return; * } */ data_backend = new DatabaseBackend(DataPath, config_authenticator); } else { data_backend = new RainyFileSystemBackend(DataPath, config_authenticator); } string admin_ui_url = listen_url.Replace("*", "localhost"); if (open_browser) { admin_ui_url += "admin/#?admin_pw=" + Config.Global.AdminPassword; } using (var listener = new RainyStandaloneServer(data_backend, listen_url)) { listener.Start(); Uptime = DateTime.UtcNow; if (open_browser) { Process.Start(admin_ui_url); } if (Environment.OSVersion.Platform != PlatformID.Unix && Environment.OSVersion.Platform != PlatformID.MacOSX) { // we run on windows, can't wait for unix signals Console.WriteLine("Press return to stop Rainy"); Console.ReadLine(); Environment.Exit(0); } else { // we run UNIX UnixSignal [] signals = new UnixSignal[] { new UnixSignal(Signum.SIGINT), new UnixSignal(Signum.SIGTERM), }; // Wait for a unix signal for (bool exit = false; !exit;) { int id = UnixSignal.WaitAny(signals); if (id >= 0 && id < signals.Length) { if (signals[id].IsSet) { exit = true; } logger.Debug("received signal, exiting"); } } } } }
public static void StartNewServer(string use_backend = "sqlite") { tmpPath = "/tmp/rainy-test-data/"; if (Directory.Exists (tmpPath)) { Directory.Delete (tmpPath, true); } Directory.CreateDirectory (tmpPath); DbConfig.SetSqliteFile (Path.Combine (tmpPath, "rainy-test.db")); // tmpPath = Path.GetTempPath () + Path.GetRandomFileName (); // for debugging, we only use a simple single user authentication CredentialsVerifier debug_authenticator = (user,pass) => { if (user == TEST_USER && pass == TEST_PASS) return true; else return false; }; IDataBackend backend; if (use_backend == "sqlite") backend = new DatabaseBackend (tmpPath, auth: debug_authenticator, reset: true); else backend = new RainyFileSystemBackend (tmpPath, debug_authenticator); rainyServer = new RainyStandaloneServer (backend, RainyListenUrl, test_server: true); rainyServer.Start (); }
public static void Main(string[] args) { // parse command line arguments string config_file = "settings.conf"; string cert_file = null, pvk_file = null; int loglevel = 0; bool show_help = false; bool open_browser = false; var p = new OptionSet () { { "c|config=", "use config file", (string file) => config_file = file }, { "v", "increase log level, where -vvvv is highest", v => { if (v != null) ++loglevel; } }, { "h|help", "show this message and exit", v => show_help = v != null }, { "cert=", "use this certificate for SSL", (string file) => cert_file = file }, { "pvk=", "use private key for certSSL", (string file2) => pvk_file = file2 }, //{ "b|nobrowser", "do not open browser window upon start", // v => { if (v != null) open_browser = false; } }, }; p.Parse (args); if (show_help) { p.WriteOptionDescriptions (Console.Out); return; } if (!File.Exists (config_file)) { Console.WriteLine ("Could not find a configuration file (try the -c flag)!"); return; } // set the configuration from the specified file Config.Global = Config.ApplyJsonFromPath (config_file); DataPath = Config.Global.DataPath; if (string.IsNullOrEmpty (DataPath)) { DataPath = Directory.GetCurrentDirectory (); } else { if (!Directory.Exists (DataPath)) Directory.CreateDirectory (DataPath); } var sqlite_file = Path.Combine (DataPath, "rainy.db"); DbConfig.SetSqliteFile (sqlite_file); SetupLogging (loglevel); logger = LogManager.GetLogger ("Main"); string listen_url = Config.Global.ListenUrl; if (string.IsNullOrEmpty (listen_url)) { listen_url = "https://localhost:443/"; logger.InfoFormat ("no ListenUrl set in the settings.conf, using the default: {0}", listen_url); } // servicestack expects trailing slash, else error is thrown if (!listen_url.EndsWith ("/")) listen_url += "/"; ConfigureSslCerts (listen_url, cert_file, pvk_file); // determine and setup data backend string backend = Config.Global.Backend; IDataBackend data_backend; // simply use user/password list from config for authentication CredentialsVerifier config_authenticator = (username, password) => { // call the authenticater callback if (string.IsNullOrEmpty (username) || string.IsNullOrEmpty (password)) return false; foreach (dynamic credentials in Config.Global.Users) { if (credentials.Username == username && credentials.Password == password) return true; } return false; }; // by default we use the filesystem backend if (string.IsNullOrEmpty (backend)) { backend = "filesystem"; } if (backend == "sqlite") { /* if (string.IsNullOrEmpty (Config.Global.AdminPassword)) { Console.WriteLine ("FATAL: Field 'AdminPassword' in the settings config may not " + "be empty when using the sqlite backend"); return; } */ data_backend = new DatabaseBackend (DataPath, config_authenticator); } else { data_backend = new RainyFileSystemBackend (DataPath, config_authenticator); } string admin_ui_url = listen_url.Replace ("*", "localhost"); if (open_browser) { admin_ui_url += "admin/#?admin_pw=" + Config.Global.AdminPassword; } using (var listener = new RainyStandaloneServer (data_backend, listen_url)) { listener.Start (); Uptime = DateTime.UtcNow; if (open_browser) { Process.Start (admin_ui_url); } if (Environment.OSVersion.Platform != PlatformID.Unix && Environment.OSVersion.Platform != PlatformID.MacOSX) { // we run on windows, can't wait for unix signals Console.WriteLine ("Press return to stop Rainy"); Console.ReadLine (); Environment.Exit(0); } else { // we run UNIX UnixSignal [] signals = new UnixSignal[] { new UnixSignal(Signum.SIGINT), new UnixSignal(Signum.SIGTERM), }; // Wait for a unix signal for (bool exit = false; !exit; ) { int id = UnixSignal.WaitAny(signals); if (id >= 0 && id < signals.Length) { if (signals[id].IsSet) exit = true; logger.Debug ("received signal, exiting"); } } } } }