private void AddEntities <T>(T[] entities, QBasicConnection connection)
        {
            var qTable    = QMapper.ConvertToQTable <T>(entities);
            var tableName = SchemaBuilder.GetQTableName(typeof(T));

            connection.Sync("upsert", tableName, qTable);
        }
Ejemplo n.º 2
0
        public static void Main(string[] Args)
        {
            QConnection q = new QBasicConnection(Args.Length >= 1 ? Args[0] : "localhost",
                                                 Args.Length >= 2 ? int.Parse(Args[1]) : 5001, null, null);

            try
            {
                q.Open();
                Console.WriteLine("conn: " + q + "  protocol: " + q.ProtocolVersion);
                Console.WriteLine("WARNING: this application overwrites: .u.upd function on q process");
                Console.WriteLine("Press <ENTER> to close application");
                q.Sync(".u.upd:{[x;y] show (x;y)};");

                var pt           = new PublisherTask(q);
                var workerThread = new Thread(pt.Run);
                workerThread.Start();

                Console.ReadLine();
                pt.Stop();
                workerThread.Join();
            }
            catch (Exception e)
            {
                Console.WriteLine("`" + e.Message);
            }
            finally
            {
                q.Close();
            }
        }
        private void AddUsers(QBasicConnection connection)
        {
            var users = new[] { Users.Bart, Users.Homer, Users.Marge, Users.MrBurns, Users.Smithers, };

            AddEntities(users, connection);
            AddIdentityPrincipals(users, connection);
        }
Ejemplo n.º 4
0
        public static void Main(String[] Args)
        {
            QConnection q = new QBasicConnection(Args.Length >= 1 ? Args[0] : "localhost",
                                                 Args.Length >= 2 ? int.Parse(Args[1]) : 5001, null, null);
            try
            {
                q.Open();
                Console.WriteLine("conn: " + q + "  protocol: " + q.ProtocolVersion);
                Console.WriteLine("WARNING: this application overwrites: .u.upd function on q process");
                Console.WriteLine("Press <ENTER> to close application");
                q.Sync(".u.upd:{[x;y] show (x;y)};");

                PublisherTask pt = new PublisherTask(q);
                Thread workerThread = new Thread(pt.Run);
                workerThread.Start();

                Console.ReadLine();
                pt.Stop();
                workerThread.Join();
            }
            catch (Exception e)
            {
                Console.WriteLine("`" + e.Message);
            }
            finally
            {
                q.Close();
            }
        }
Ejemplo n.º 5
0
        private void ReadSpringfieldAccessControlDb()
        {
            using (var connection = new QBasicConnection(port: 5010))
            {
                connection.Open();

                var userPrincipals = connection.QueryObjects <Acl.UserPrincipal>("select from .acl.userPrincipal")
                                     .ToLookup(u => u.User);

                // Pull out the users and initialise the objects with all relevant principal Ids
                var users = connection.QueryObjects <Auth.User>("select from .auth.user")
                            .Select(u =>
                {
                    u.PrincipalIds = userPrincipals[u.Id].Select(p => p.Principal).ToArray();
                    return(u);
                })
                            .ToDictionary(u => u.Id);

                // pull out the grant acl and restructure the data for rapid in-proc queries
                var grantResourceAcl = connection.QueryObjects <Acl.GrantResourceAcl>("select from .acl.grantResourceAcl")
                                       .GroupBy(row => row.Resource)
                                       .ToDictionary(group => group.Key,
                                                     group => group.ToLookup(entry => entry.Operation,
                                                                             entry => entry.Principal));

                var denyResourceAcl = connection.QueryObjects <Acl.DenyResourceAcl>("select from .acl.denyResourceAcl")
                                      .GroupBy(row => row.Resource)
                                      .ToDictionary(group => group.Key,
                                                    group => group.ToLookup(entry => entry.Operation,
                                                                            entry => entry.Principal));

                // Now we can test our ACL

                Auth.User dbUser = users[Users.Bart.Id];

                var denyOutcome  = denyResourceAcl.OnResource(Resources.SimpsonHome).PermissionTo(Operations.Enter).ExistsFor(dbUser);
                var grantOutcome = grantResourceAcl.OnResource(Resources.SimpsonHome).PermissionTo(Operations.Enter).ExistsFor(dbUser);

                Console.WriteLine($"{dbUser.Name} is {IsPermitted(denyOutcome, grantOutcome)} to Enter the Simpson Home");

                // We gave Smithers specific access to enter the Simpsons Home
                dbUser = users[Users.Smithers.Id];

                denyOutcome  = denyResourceAcl.OnResource(Resources.SimpsonHome).PermissionTo(Operations.Leave).ExistsFor(dbUser);
                grantOutcome = grantResourceAcl.OnResource(Resources.SimpsonHome).PermissionTo(Operations.Leave).ExistsFor(dbUser);

                Console.WriteLine($"{dbUser.Name} is {IsPermitted(denyOutcome, grantOutcome)} specifically to Enter the Simpson Home");

                dbUser = users[Users.MrBurns.Id];

                denyOutcome  = denyResourceAcl.OnResource(Resources.HeadOffice).PermissionTo(Operations.Leave).ExistsFor(dbUser);
                grantOutcome = grantResourceAcl.OnResource(Resources.HeadOffice).PermissionTo(Operations.Leave).ExistsFor(dbUser);

                Console.WriteLine($"{dbUser.Name} is {IsPermitted(denyOutcome, grantOutcome)} to Leave the office");

                Console.ReadKey();
            }
        }
Ejemplo n.º 6
0
 public RequestController(
     ILogger <RequestController> logger,
     IConfiguration configuration,
     QBasicConnection qConnection)
 {
     _logger              = logger;
     _configuration       = configuration;
     _qConnection         = qConnection;
     _kdbConnectorOptions = new KdbConnectorOptions();
     configuration.GetSection(KdbConnectorOptions.KdbOptions).Bind(_kdbConnectorOptions);
 }
        private void AddDenyAcl(QBasicConnection connection)
        {
            // Owners are explicitly prohibited from leaving the office
            var denyAcl = new[]
            {
                new Acl.DenyResourceAcl {
                    Resource = Resources.HeadOffice.Id, Operation = Operations.Leave.Id, Principal = Principals.Owner.Id
                },
            };

            AddEntities(denyAcl, connection);
        }
        private void DefineSchema(Type[] types, QBasicConnection connection)
        {
            var fullAclSchema = SchemaBuilder.DeclareEmptySchema(types);

            using (var sr = new StringReader(fullAclSchema))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    connection.Sync(line);
                }
            }
        }
        private void AddUserPrincipals(QBasicConnection connection)
        {
            var userPrincipals = new[]
            {
                new Acl.UserPrincipal {
                    User = Users.Bart.Id, Principal = Principals.Everyone.Id
                },
                new Acl.UserPrincipal {
                    User = Users.Bart.Id, Principal = Principals.Simpson.Id
                },
                new Acl.UserPrincipal {
                    User = Users.Marge.Id, Principal = Principals.Everyone.Id
                },
                new Acl.UserPrincipal {
                    User = Users.Marge.Id, Principal = Principals.Simpson.Id
                },
                new Acl.UserPrincipal {
                    User = Users.Homer.Id, Principal = Principals.Everyone.Id
                },
                new Acl.UserPrincipal {
                    User = Users.Homer.Id, Principal = Principals.Simpson.Id
                },
                new Acl.UserPrincipal {
                    User = Users.Homer.Id, Principal = Principals.Employee.Id
                },
                new Acl.UserPrincipal {
                    User = Users.Smithers.Id, Principal = Principals.Everyone.Id
                },
                new Acl.UserPrincipal {
                    User = Users.Smithers.Id, Principal = Principals.Employee.Id
                },
                new Acl.UserPrincipal {
                    User = Users.Smithers.Id, Principal = Principals.Supervisor.Id
                },
                new Acl.UserPrincipal {
                    User = Users.MrBurns.Id, Principal = Principals.Everyone.Id
                },
                new Acl.UserPrincipal {
                    User = Users.MrBurns.Id, Principal = Principals.Employee.Id
                },
                new Acl.UserPrincipal {
                    User = Users.MrBurns.Id, Principal = Principals.Supervisor.Id
                },
                new Acl.UserPrincipal {
                    User = Users.MrBurns.Id, Principal = Principals.Owner.Id
                },
            };

            AddEntities(userPrincipals, connection);
        }
Ejemplo n.º 10
0
        // ReSharper disable InconsistentNaming
        public object qOpen(string alias, string hostname, object port, string username = null, string password = null)
        // ReSharper restore InconsistentNaming
        {
            try
            {
                if (String.IsNullOrEmpty(alias))
                {
                    return("Invalid alias");
                }
                var c = GetConnection(alias);
                if (c != null)
                {
                    return(alias);
                }

                if (String.IsNullOrEmpty(hostname))
                {
                    return("Invalid hostname");
                }

                int prt;
                try
                {
                    prt = Int32.Parse(port.ToString());
                }
                catch
                {
                    return("Invalid port");
                }

                try
                {
                    c = new QBasicConnection(hostname, prt, username, password);
                    c.Open();
                    Connections[alias] = c;
                }
                catch (QException e)
                {
                    return("ERR: " + e.Message);
                }
            }
            catch (Exception e)
            {
                return("ERR: " + e.Message);
            }
            return(alias);
        }
        private void DefineSchema(QBasicConnection connection)
        {
            // input types can be added in any order. They are sorted by dependency later
            var types = new[]
            {
                typeof(Auth.User),
                typeof(Auth.LoginInfo),
                typeof(Acl.Principal),
                typeof(Acl.UserPrincipal),
                typeof(Acl.Operation),
                typeof(Acl.Resource),
                typeof(Acl.GrantResourceAcl),
                typeof(Acl.DenyResourceAcl),
            };

            DefineSchema(types, connection);
        }
        public void BuildDb()
        {
            using (var connection = new QBasicConnection(port: 5010))
            {
                connection.Open();

                DefineSchema(connection);

                AddUsers(connection);
                AddPrincipals(connection);
                AddOperations(connection);
                AddResources(connection);
                AddUserPrincipals(connection);
                AddGrantAcl(connection);
                AddDenyAcl(connection);
            }
        }
Ejemplo n.º 13
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();

            // open a connection to kdb process
            var q = new QBasicConnection(
                _kdbConnectorOptions.Host,
                _kdbConnectorOptions.Port,
                _kdbConnectorOptions.UserName,
                _kdbConnectorOptions.Password,
                _kdbConnectorOptions.Encoding
                );

            q.Open();

            // pass it to controllers via dependency injection
            services.AddSingleton <QBasicConnection>(q);
        }
        private void AddIdentityPrincipals(Auth.User[] users, QBasicConnection connection)
        {
            var identityPrincipals     = new Acl.Principal[users.Length];
            var identityUserPrincipals = new Acl.UserPrincipal[users.Length];

            for (int i = 0; i < users.Length; ++i)
            {
                var login = users[i].Login;
                identityPrincipals[i] = new Acl.Principal {
                    Id = "principal::" + login, Name = login
                };
                identityUserPrincipals[i] = new Acl.UserPrincipal {
                    User = "******" + login, Principal = "principal::" + login
                };
            }
            AddEntities(identityPrincipals, connection);
            AddEntities(identityUserPrincipals, connection);
        }
        public static void GetKDBResults(string host, int port, Func <string, Task> func)
        {
            QConnection q = new QBasicConnection(host: host,
                                                 port: port);

            try
            {
                //var result = "";
                q.Open();
                Console.WriteLine("conn: " + q + "  protocol: " + q.ProtocolVersion);

                while (true)
                {
                    Console.Write("Q)");
                    var line = Console.ReadLine();

                    if (line.Equals("\\\\"))
                    {
                        break;
                    }
                    else
                    {
                        try
                        {
                            PrintResult(q.Sync(line), func);
                        }
                        catch (QException e)
                        {
                            Console.WriteLine("`" + e.Message);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
                Console.ReadLine();
            }
            finally
            {
                q.Close();
            }
        }
Ejemplo n.º 16
0
        private static void Main(string[] args)
        {
            IList <string> x = args;

            QConnection q = new QBasicConnection((args.Length >= 1) ? args[0] : "localhost",
                                                 (args.Length >= 2) ? int.Parse(args[1]) : 5000);

            try
            {
                q.Open();
                Console.WriteLine("conn: " + q + "  protocol: " + q.ProtocolVersion);

                while (true)
                {
                    Console.Write("Q)");
                    var line = Console.ReadLine();

                    if (line.Equals("\\\\"))
                    {
                        break;
                    }
                    try
                    {
                        PrintResult(q.Sync(line));
                    }
                    catch (QException e)
                    {
                        Console.WriteLine("`" + e.Message);
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
                Console.ReadLine();
            }
            finally
            {
                q.Close();
            }
        }
Ejemplo n.º 17
0
        static void Main(string[] args)
        {
            QConnection q = new QBasicConnection(host: (args.Length >= 1) ? args[0] : "localhost",
                                                 port: (args.Length >= 2) ? Int32.Parse(args[1]) : 5000);
            try
            {
                q.Open();
                Console.WriteLine("conn: " + q + "  protocol: " + q.ProtocolVersion);

                while (true)
                {
                    Console.Write("Q)");
                    var line = Console.ReadLine();

                    if (line.Equals("\\\\"))
                    {
                        break;
                    }
                    else
                    {
                        try
                        {
                            PrintResult(q.Sync(line));
                        }
                        catch (QException e)
                        {
                            Console.WriteLine("`" + e.Message);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
                Console.ReadLine();
            }
            finally
            {
                q.Close();
            }
        }
        private void AddGrantAcl(QBasicConnection connection)
        {
            var grantAcl = new[]
            {
                // All Simpson family members can enter and leave the family home
                new Acl.GrantResourceAcl {
                    Resource = Resources.SimpsonHome.Id, Operation = Operations.Enter.Id, Principal = Principals.Simpson.Id
                },
                new Acl.GrantResourceAcl {
                    Resource = Resources.SimpsonHome.Id, Operation = Operations.Leave.Id, Principal = Principals.Simpson.Id
                },
                // We'll give Smithers specific access to enter and leave the family home
                new Acl.GrantResourceAcl {
                    Resource = Resources.SimpsonHome.Id, Operation = Operations.Enter.Id, Principal = "principal::smithers"
                },
                new Acl.GrantResourceAcl {
                    Resource = Resources.SimpsonHome.Id, Operation = Operations.Leave.Id, Principal = "principal::smithers"
                },
                // All employees can enter and leave the Homer's office
                new Acl.GrantResourceAcl {
                    Resource = Resources.Sector7G.Id, Operation = Operations.Enter.Id, Principal = Principals.Employee.Id
                },
                new Acl.GrantResourceAcl {
                    Resource = Resources.Sector7G.Id, Operation = Operations.Leave.Id, Principal = Principals.Employee.Id
                },
                // Supervisors can enter and leave the head office
                new Acl.GrantResourceAcl {
                    Resource = Resources.HeadOffice.Id, Operation = Operations.Enter.Id, Principal = Principals.Supervisor.Id
                },
                new Acl.GrantResourceAcl {
                    Resource = Resources.HeadOffice.Id, Operation = Operations.Leave.Id, Principal = Principals.Supervisor.Id
                },
            };

            AddEntities(grantAcl, connection);
        }
        private void AddPrincipals(QBasicConnection connection)
        {
            var principals = new[] { Principals.Everyone, Principals.Employee, Principals.Supervisor, Principals.Owner, Principals.Simpson };

            AddEntities(principals, connection);
        }
        private void AddResources(QBasicConnection connection)
        {
            var resources = new[] { Resources.SimpsonHome, Resources.Sector7G, Resources.HeadOffice };

            AddEntities(resources, connection);
        }
        private void AddOperations(QBasicConnection connection)
        {
            var operations = new[] { Operations.Enter, Operations.Leave };

            AddEntities(operations, connection);
        }