diff --git a/database/src/main/java/io/hstore/db/HypergraphDatabase.java b/database/src/main/java/io/hstore/db/HypergraphDatabase.java index 3f9dc44..af17240 100644 --- a/database/src/main/java/io/hstore/db/HypergraphDatabase.java +++ b/database/src/main/java/io/hstore/db/HypergraphDatabase.java @@ -12,6 +12,7 @@ import io.hstore.db.security.Principal; import io.hstore.db.security.Quota.Usage; import io.hstore.db.security.Security; +import io.hstore.db.security.Access; import io.hstore.db.security.Scram; import io.hstore.db.security.SignIns; import io.hstore.db.security.TenantMemberships; @@ -60,6 +61,8 @@ public final class HypergraphDatabase implements AutoCloseable { private final Statistics statistics; private final MaterializedViews views; private final SignIns signIns = new SignIns(); + private final Map connections = new ConcurrentHashMap<>(); + private final Map tenantConnections = new ConcurrentHashMap<>(); private final byte[] scramSecret; private final Map legacyWarnings = new ConcurrentHashMap<>(); @@ -150,6 +153,52 @@ public SignIns signIns() { return signIns; } + public void enter(Principal principal) { + Access.Options limits = read(reader -> Access.options(reader.engineView(), principal.user())); + int tenantLimit = read(reader -> Access.tenantConnectionLimit(reader.engineView(), principal.home())); + synchronized (connections) { + if (!principal.global()) { + if (limits.connectionLimit() >= 0 && connections(principal.user()) >= limits.connectionLimit()) { + throw HStoreException.limit("too many connections for user " + principal.user() + " (connection limit " + + limits.connectionLimit() + ")"); + } + if (tenantLimit >= 0 && tenantConnections(principal.home()) >= tenantLimit) { + throw HStoreException.limit("too many connections for tenant #" + principal.home() + " (connection limit " + + tenantLimit + ")"); + } + } + connections.merge(principal.user(), 1, Integer::sum); + tenantConnections.merge(principal.home(), 1, Integer::sum); + } + } + + public boolean holds(Principal principal, Access.Privilege capability) { + return principal.global() || principal.home() == Principal.DEFAULT_TENANT + && read(reader -> Access.holds(reader.engineView(), principal.user(), capability)); + } + + public void rejoin(Principal principal) { + synchronized (connections) { + connections.merge(principal.user(), 1, Integer::sum); + tenantConnections.merge(principal.home(), 1, Integer::sum); + } + } + + public void leave(Principal principal) { + synchronized (connections) { + connections.computeIfPresent(principal.user(), (_, count) -> count <= 1 ? null : count - 1); + tenantConnections.computeIfPresent(principal.home(), (_, count) -> count <= 1 ? null : count - 1); + } + } + + public int tenantConnections(int tenant) { + return tenantConnections.getOrDefault(tenant, 0); + } + + public int connections(String user) { + return connections.getOrDefault(user, 0); + } + public static List> extensionSlots() { return Stream.of(SchemaSlots.ALL, PropertySlots.ALL, List.>of(Embedding.EMBEDDINGS), Provenance.SLOTS, StateBindings.SLOTS, Signals.SLOTS, MaterializedViews.SLOTS, Security.SLOTS) diff --git a/database/src/main/java/io/hstore/db/Reader.java b/database/src/main/java/io/hstore/db/Reader.java index e41e748..7d03e1e 100644 --- a/database/src/main/java/io/hstore/db/Reader.java +++ b/database/src/main/java/io/hstore/db/Reader.java @@ -9,6 +9,7 @@ import io.hstore.db.property.PropertySlots; import io.hstore.db.schema.Schema; import io.hstore.db.schema.TypeDef; +import io.hstore.db.security.Access; import io.hstore.db.security.Principal; import io.hstore.db.semantic.SemanticPlane; import io.hstore.db.value.Json; @@ -27,8 +28,10 @@ import io.hstore.engine.tree.Entry; import io.hstore.engine.tree.Tree; import io.hstore.engine.txn.IncidentEdge; +import io.hstore.engine.txn.Snapshot; import io.hstore.engine.txn.View; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -48,6 +51,8 @@ public sealed class Reader permits Writer { private final Principal principal; private Boolean onlyDefaultTenant; + private final Map> grantedTypes = new HashMap<>(); + private long grantsGeneration = -1; Reader(HypergraphDatabase database, View view, Principal principal) { this.database = database; @@ -67,7 +72,7 @@ private boolean onlyDefaultTenant() { Boolean known = onlyDefaultTenant; if (known == null) { try (View.Unrecorded _ = view.unrecorded()) { - known = tenant() == Principal.DEFAULT_TENANT + known = principal.role().readsEveryType() && tenant() == Principal.DEFAULT_TENANT && view.scan(EngineSlots.TYPES).last().map(entry -> entry.key() >>> 32 == Principal.DEFAULT_TENANT).orElse(true); } onlyDefaultTenant = known; @@ -76,7 +81,40 @@ private boolean onlyDefaultTenant() { } public boolean visible(long atom) { - return view.atom(atom).map(record -> record.tenant() == tenant()).orElse(false); + return view.atom(atom).map(this::admits).orElse(false); + } + + boolean admits(AtomRecord record) { + return record.tenant() == tenant() && readable(record.type()); + } + + public boolean readable(int type) { + return principal.role().readsEveryType() || privilege(type).isPresent(); + } + + public boolean writable(int type) { + return principal.role().writesEveryType() || privilege(type).filter(found -> found == Access.Privilege.WRITE).isPresent(); + } + + private Optional privilege(int type) { + long current = database.engine().transactions().current().id(); + if (current != grantsGeneration) { + grantedTypes.clear(); + grantsGeneration = current; + } + return grantedTypes.computeIfAbsent(type, found -> { + try (Snapshot latest = database.engine().snapshot()) { + return Access.onType(latest, principal.user(), found); + } + }); + } + + public TypeDef requireReadableType(String name) { + TypeDef type = requireType(name); + if (!readable(type.id())) { + throw HStoreException.invalid("user " + principal.user() + " has no READ privilege on type " + type.name()); + } + return type; } public boolean owns(long atom) { @@ -92,21 +130,21 @@ public View.Unrecorded unrecorded() { } public boolean isEdge(long atom) { - return view.atom(atom).map(record -> record.tenant() == tenant() && record instanceof AtomRecord.EdgeRecord).orElse(false); + return view.atom(atom).map(record -> admits(record) && record instanceof AtomRecord.EdgeRecord).orElse(false); } public Optional hyperedge(long id) { return view.atom(id) - .filter(record -> record.tenant() == tenant()) + .filter(this::admits) .flatMap(record -> record instanceof AtomRecord.EdgeRecord edge ? Optional.of(view.edge(id, edge)) : Optional.empty()); } public Optional incidence(long edge, long member) { - return owns(member) ? view.incidence(edge, member, tenant()) : Optional.empty(); + return owns(member) ? view.incidence(edge, member, this::admits) : Optional.empty(); } public boolean contains(long edge, long member) { - return owns(member) && view.contains(edge, member, tenant()); + return owns(member) && view.contains(edge, member, this::admits); } public Stream incidentEdges(long atom) { @@ -148,7 +186,7 @@ public long generation() { public Optional atom(long id) { return view.atom(id) - .filter(record -> record.tenant() == tenant()) + .filter(this::admits) .map(record -> new Atom(id, schema().require(view, record.type()), record)); } @@ -165,7 +203,7 @@ public TypeDef requireType(String name) { } public OptionalLong find(String type, String key) { - return type(type).map(found -> view.resolve(tenant(), found.id(), key)).orElse(OptionalLong.empty()); + return type(type).map(found -> view.resolve(tenant(), requireReadableType(found.name()).id(), key)).orElse(OptionalLong.empty()); } public long resolve(String type, String key) { @@ -173,19 +211,19 @@ public long resolve(String type, String key) { } public LongStream atoms(String type) { - return atomsOfType(requireType(type).id()); + return atomsOfType(requireReadableType(type).id()); } public LongStream atomsOfType(int typeId) { - return view.atomsOfType(tenant(), typeId); + return readable(typeId) ? view.atomsOfType(tenant(), typeId) : LongStream.empty(); } public long count(String type) { - return countOfType(requireType(type).id()); + return countOfType(requireReadableType(type).id()); } public long countOfType(int typeId) { - return view.countOfType(tenant(), typeId); + return readable(typeId) ? view.countOfType(tenant(), typeId) : 0; } public long population(boolean edges) { @@ -250,7 +288,7 @@ public Optional document(long atom) { } public Hyperedge edge(long id) { - AtomRecord record = view.atom(id).filter(found -> found.tenant() == tenant()) + AtomRecord record = view.atom(id).filter(this::admits) .orElseThrow(() -> HStoreException.invalid("atom " + id + " does not exist")); if (!(record instanceof AtomRecord.EdgeRecord edge)) { throw HStoreException.invalid("atom " + id + " is not a hyperedge"); @@ -337,7 +375,7 @@ public LongStream lookup(String type, String property, Value value) { } public LongStream range(String type, String property, Optional low, Optional high, boolean lowInclusive, boolean highInclusive) { - TypeDef definition = requireType(type); + TypeDef definition = requireReadableType(type); Predicate accept = candidate -> low.map(bound -> comparable(candidate, bound) && (lowInclusive ? candidate.compareTo(bound) >= 0 : candidate.compareTo(bound) > 0)).orElse(true) && high.map(bound -> comparable(candidate, bound) @@ -355,7 +393,7 @@ public LongStream range(String type, String property, Optional low, Optio return PropertyIndex.candidates(view, tenant(), key.getAsInt(), tag, PropertyIndex.lowerKey(tag, low), PropertyIndex.upperKey(tag, high)) .filter(entry -> accept.test(entry.value())) .mapToLong(Entry::key) - .filter(owner -> view.atom(owner).map(record -> record.type() == definition.id() && record.tenant() == tenant()).orElse(false)) + .filter(owner -> view.atom(owner).map(record -> record.type() == definition.id() && admits(record)).orElse(false)) .sorted() .distinct(); } diff --git a/database/src/main/java/io/hstore/db/Writer.java b/database/src/main/java/io/hstore/db/Writer.java index bc142ba..08dfc6a 100644 --- a/database/src/main/java/io/hstore/db/Writer.java +++ b/database/src/main/java/io/hstore/db/Writer.java @@ -111,6 +111,7 @@ public long node(String type, String key) { public long node(String type, String key, Map properties) { principal().requireWrite(); TypeDef definition = kindOf(type, AtomKind.NODE); + requireWritableType(definition); Security.charge(txn, tenant(), new Usage(1, 0, 0)); long id = txn.createNode(new AtomRecord.NodeRecord(definition.id(), key, 0, 0, 0, tenant(), 0)); assign(id, definition, properties); @@ -128,6 +129,7 @@ public long edge(String type, Map properties) { throw HStoreException.invalid(type + " is a node type"); } principal().requireWrite(); + requireWritableType(definition); Security.charge(txn, tenant(), new Usage(1, 1, 0)); long id = txn.createEdge(new AtomRecord.EdgeRecord(definition.id(), kind.edgeKind().orElseThrow(), Ref.EMPTY, Ref.EMPTY, 0, 0, tenant(), 0)); assign(id, definition, properties); @@ -170,13 +172,13 @@ public void append(long edge, long member, MemberSpec spec) { public void remove(long edge, long member) { principal().requireWrite(); - require(edge); + requireWritable(edge); txn.remove(edge, member); } public void removeAt(long edge, long index) { principal().requireWrite(); - require(edge); + requireWritable(edge); txn.removeAt(edge, index); } @@ -185,6 +187,7 @@ public void load(long edge, List members, MemberSpec spec) { } public void load(long edge, long[] members, MemberSpec spec) { + requireMembership(edge, spec); Incidence[] incidences = new Incidence[members.length]; if (members.length > 0) { Incidence first = incidences[0] = incidence(edge, members[0], spec); @@ -196,6 +199,20 @@ public void load(long edge, long[] members, MemberSpec spec) { txn.load(edge, Arrays.asList(incidences)); } + public void loadIncidences(long edge, List incidences) { + requireMembership(edge, MemberSpec.PLAIN); + Atom container = require(edge); + for (Incidence incidence : incidences) { + requireVisible(incidence.member()); + for (String role : roles(incidence.roleSet())) { + if (!container.type().allowsRole(role)) { + throw HStoreException.invalid(container.type().name() + " does not declare role '" + role + "'"); + } + } + } + txn.load(edge, incidences); + } + public void qualify(long edge, long member, long qualifier) { requireMembership(edge, MemberSpec.PLAIN); requireVisible(member); @@ -218,7 +235,7 @@ private Incidence incidence(long edge, long member, MemberSpec spec) { private void requireMembership(long edge, MemberSpec spec) { principal().requireWrite(); - Atom container = require(edge); + Atom container = requireWritable(edge); if (!container.isEdge()) { throw HStoreException.invalid("atom " + edge + " is not a hyperedge"); } @@ -229,6 +246,18 @@ private void requireMembership(long edge, MemberSpec spec) { } } + public Atom requireWritable(long atom) { + Atom target = require(atom); + requireWritableType(target.type()); + return target; + } + + private void requireWritableType(TypeDef type) { + if (!writable(type.id())) { + throw HStoreException.invalid("user " + principal().user() + " has no WRITE privilege on type " + type.name()); + } + } + private void requireVisible(long member) { if (!visible(member)) { throw HStoreException.invalid("member atom " + member + " does not exist"); @@ -241,7 +270,7 @@ public void set(long atom, String name, Value value) { public void set(long atom, String name, Value value, Validity validity) { principal().requireWrite(); - Atom target = require(atom); + Atom target = requireWritable(atom); Value stored = target.type().property(name).map(def -> Values.coerce(value, def.type())).orElse(value); stored = externalizeLargeText(stored); Property property = new Property(stored, validity.from(), validity.to()); @@ -250,7 +279,7 @@ public void set(long atom, String name, Value value, Validity validity) { public void unset(long atom, String name) { principal().requireWrite(); - require(atom); + requireWritable(atom); OptionalInt key = schema().existingPropertyKey(name); if (key.isPresent()) { updateBag(atom, key.getAsInt(), bag -> bag.without(key.getAsInt())); @@ -288,7 +317,7 @@ long writePayload(byte[] bytes) { public void document(long atom, Json json) { principal().requireWrite(); - require(atom); + requireWritable(atom); byte[] bytes = json.print().getBytes(StandardCharsets.UTF_8); long ref = writePayload(bytes); set(atom, Schema.DOCUMENT, new Value.Payload(ref, bytes.length, "application/json")); @@ -301,7 +330,7 @@ public void embed(long atom, String text) { public void embed(long atom, String model, int version, float[] vector) { principal().requireWrite(); - require(atom); + requireWritable(atom); SemanticPlane semantic = database.semantic(); int id = semantic.existingModel(model).orElseGet(() -> { if (!model.equals(semantic.encoder().model())) { @@ -318,7 +347,15 @@ public void embed(long atom, String model, int version, float[] vector) { public void delete(long atom) { principal().requireWrite(); - Atom target = require(atom); + Atom target = requireWritable(atom); + if (!principal().role().writesEveryType()) { + engineView().incident(atom).forEach(incident -> engineView().atom(incident.edge()) + .filter(record -> record.tenant() == tenant() && !writable(record.type())) + .ifPresent(record -> { + throw HStoreException.invalid("cannot delete atom " + atom + ": it is a member of a hyperedge that user " + + principal().user() + " may not change"); + })); + } Security.charge(txn, tenant(), new Usage(-1, target.isEdge() ? -1 : 0, 0)); txn.delete(PropertySlots.PROPERTIES, atom); txn.delete(Embedding.EMBEDDINGS, atom); diff --git a/database/src/main/java/io/hstore/db/evidence/Provenance.java b/database/src/main/java/io/hstore/db/evidence/Provenance.java index c902271..6a44e7f 100644 --- a/database/src/main/java/io/hstore/db/evidence/Provenance.java +++ b/database/src/main/java/io/hstore/db/evidence/Provenance.java @@ -54,7 +54,7 @@ private Provenance() { } public static long record(Writer writer, Evidence evidence) { - writer.principal().requireWrite(); + requireTenantWrite(writer); requireReferable(writer, evidence.source()); long id = writer.database().engine().transactions().allocateAtom(); writer.transaction().put(EVIDENCE, id, evidence.withTenant(writer.tenant())); @@ -62,7 +62,7 @@ public static long record(Writer writer, Evidence evidence) { } public static long assertion(Writer writer, Qualifier qualifier) { - writer.principal().requireWrite(); + requireTenantWrite(writer); requireReferable(writer, qualifier.source()); qualifier.evidence().forEach(support -> requireReferable(writer, support)); long id = writer.database().engine().transactions().allocateAtom(); @@ -76,6 +76,13 @@ public static long qualify(Writer writer, long edge, long member, Qualifier qual return id; } + private static void requireTenantWrite(Writer writer) { + writer.principal().requireWrite(); + if (!writer.principal().role().writesEveryType()) { + throw HStoreException.invalid("user " + writer.principal().user() + " needs the WRITER or ADMIN role to record evidence and assertions"); + } + } + private static void requireReferable(Reader reader, long id) { if (id != 0 && !reader.visible(id) && evidence(reader, id).isEmpty() && qualifier(reader, id).isEmpty()) { throw HStoreException.invalid("atom " + id + " does not exist"); @@ -83,11 +90,15 @@ private static void requireReferable(Reader reader, long id) { } public static Optional qualifier(Reader reader, long id) { - return id == 0 ? Optional.empty() : reader.engineView().get(QUALIFIERS, id).filter(found -> found.tenant() == reader.tenant()); + return id == 0 || !reader.principal().role().readsEveryType() + ? Optional.empty() + : reader.engineView().get(QUALIFIERS, id).filter(found -> found.tenant() == reader.tenant()); } public static Optional evidence(Reader reader, long id) { - return reader.engineView().get(EVIDENCE, id).filter(found -> found.tenant() == reader.tenant()); + return reader.principal().role().readsEveryType() + ? reader.engineView().get(EVIDENCE, id).filter(found -> found.tenant() == reader.tenant()) + : Optional.empty(); } public static Stream effective(Reader reader, long edge, EvidencePolicy policy) { diff --git a/database/src/main/java/io/hstore/db/hora/PatternMatcher.java b/database/src/main/java/io/hstore/db/hora/PatternMatcher.java index 32a61cf..94085a3 100644 --- a/database/src/main/java/io/hstore/db/hora/PatternMatcher.java +++ b/database/src/main/java/io/hstore/db/hora/PatternMatcher.java @@ -51,6 +51,8 @@ public List plan(Pattern pattern) { } public Stream> match(Pattern pattern, Budget budget) { + pattern.variables().forEach(variable -> variable.type().flatMap(reader::type) + .ifPresent(type -> reader.requireReadableType(type.name()))); Budget.Meter meter = budget.start(); List> bindings = List.of(Map.of()); for (String variable : plan(pattern)) { diff --git a/database/src/main/java/io/hstore/db/query/Ast.java b/database/src/main/java/io/hstore/db/query/Ast.java index 774c704..08b1469 100644 --- a/database/src/main/java/io/hstore/db/query/Ast.java +++ b/database/src/main/java/io/hstore/db/query/Ast.java @@ -10,6 +10,7 @@ import io.hstore.db.hora.Reducer; import io.hstore.db.schema.AtomKind; import io.hstore.db.schema.TypeDef.PropertyDef; +import io.hstore.db.security.Access; import io.hstore.db.security.Role; import io.hstore.db.semantic.SemanticPlane; import io.hstore.db.temporal.BranchMerge; @@ -192,10 +193,21 @@ record CreateTenant(String name, Map quota) implements Statement { record AlterTenant(String name, Map quota) implements Statement { } - record CreateUser(String name, String password, Optional tenant, Role role) implements Statement { + record CreateUser(String name, String password, Optional tenant, Role role, OptionalLong validUntil, + OptionalLong connectionLimit) implements Statement { } - record AlterUser(String name, String password) implements Statement { + record Grant(Access.Privilege privilege, Optional type, String user) implements Statement { + } + + record Revoke(Access.Privilege privilege, Optional type, String user) implements Statement { + } + + record ShowGrants(Optional user) implements Statement { + } + + record AlterUser(String name, Optional password, Optional role, OptionalLong validUntil, + OptionalLong connectionLimit) implements Statement { } record DropUser(String name) implements Statement { diff --git a/database/src/main/java/io/hstore/db/query/Executor.java b/database/src/main/java/io/hstore/db/query/Executor.java index 8cfe2e0..09e35d8 100644 --- a/database/src/main/java/io/hstore/db/query/Executor.java +++ b/database/src/main/java/io/hstore/db/query/Executor.java @@ -27,8 +27,10 @@ import io.hstore.db.retrieve.Ranking; import io.hstore.db.retrieve.RoleTimeScorer; import io.hstore.db.schema.TypeDef; +import io.hstore.db.security.Access; import io.hstore.db.security.Principal; import io.hstore.db.security.Quota; +import io.hstore.db.security.Role; import io.hstore.db.security.Security; import io.hstore.db.semantic.SemanticPlane; import io.hstore.db.signal.Signals; @@ -91,6 +93,7 @@ QueryResult run(Statement statement) { } else if (administrative(statement)) { session.principal().requireAdmin(); } + capability(statement).ifPresent(this::requireCapability); return switch (statement) { case Statement.CreateType type -> write(writer -> { TypeDef created = type.kind().isEdge() @@ -132,41 +135,78 @@ QueryResult run(Statement statement) { + (outcome.conflicts().isEmpty() ? "" : ", " + outcome.conflicts().size() + " conflicts resolved for " + policy)); } case Statement.CreateTenant(String name, Map quota) -> write(writer -> { - Security.Tenant tenant = Security.createTenant(writer.transaction(), name, Quota.UNLIMITED.with(quota)); + Map limits = new LinkedHashMap<>(quota); + Long connections = removeConnections(limits); + Security.Tenant tenant = Security.createTenant(writer.transaction(), name, Quota.UNLIMITED.with(limits)); + if (connections != null) { + Access.setTenantConnectionLimit(writer.transaction(), tenant.id(), connections); + } return QueryResult.message("created tenant " + tenant.name() + " #" + tenant.id()); }); case Statement.AlterTenant(String name, Map quota) -> write(writer -> { Security.Tenant tenant = Security.requireTenant(writer.engineView(), name); - Security.setQuota(writer.transaction(), tenant, tenant.quota().with(quota)); + Map limits = new LinkedHashMap<>(quota); + Long connections = removeConnections(limits); + Security.setQuota(writer.transaction(), tenant, tenant.quota().with(limits)); + if (connections != null) { + Access.setTenantConnectionLimit(writer.transaction(), tenant.id(), connections); + } return QueryResult.message("updated quota of tenant " + name); }); - case Statement.CreateUser(String name, String password, Optional tenant, var role) -> write(writer -> { - Security.createUser(writer.transaction(), name, password, governedTenant(writer, tenant), role); - return QueryResult.message("created user " + name + " with role " + role); - }); - case Statement.AlterUser(String name, String password) -> write(writer -> { - governedUser(writer, name); - Security.setPassword(writer.transaction(), name, password); - return QueryResult.message("changed password of " + name); - }); + case Statement.CreateUser(String name, String password, Optional tenant, var role, var validUntil, var connectionLimit) -> + write(writer -> { + Security.createUser(writer.transaction(), name, password, governedTenant(writer, tenant), role); + Access.forget(writer.transaction(), name); + if (validUntil.isPresent() || connectionLimit.isPresent()) { + Access.setOptions(writer.transaction(), new Access.Options(name, validUntil.orElse(Long.MAX_VALUE), + (int) connectionLimit.orElse(-1))); + } + Security.keepAGlobalAdmin(writer.transaction(), hadGlobalAdmin()); + return QueryResult.message("created user " + name + " with role " + role); + }); + case Statement.AlterUser alter -> write(writer -> alterUser(writer, alter)); case Statement.DropUser(String name) -> write(writer -> { - governedUser(writer, name); + Security.User user = governedUser(writer, name); if (!session.principal().global() && Security.users(writer.engineView()).size() <= 1) { throw HStoreException.invalid("cannot drop the last user: the server would stop asking for credentials"); } Security.dropUser(writer.transaction(), name); + Security.keepAGlobalAdmin(writer.transaction(), hadGlobalAdmin()); return QueryResult.message("dropped user " + name); }); - case Statement.ShowTenants _ -> read(reader -> table(List.of("id", "name", "atoms", "edges", "payload bytes", "max atoms", "max edges"), + case Statement.Grant(Access.Privilege privilege, Optional type, String name) -> write(writer -> { + Security.User user = grantee(writer, privilege, name); + boolean changed = Access.grant(writer.transaction(), name, type.map(found -> writer.requireType(found).id()).orElse(0), privilege); + return QueryResult.message((changed ? "granted " : name + " already holds ") + privilege + type.map(found -> " on type " + found).orElse("") + + (changed ? " to " + user.name() : "")); + }); + case Statement.Revoke(Access.Privilege privilege, Optional type, String name) -> write(writer -> { + grantee(writer, privilege, name); + boolean changed = Access.revoke(writer.transaction(), name, type.map(found -> writer.requireType(found).id()).orElse(0), privilege); + return QueryResult.message((changed ? "revoked " : name + " does not hold ") + privilege + type.map(found -> " on type " + found).orElse("") + + (changed ? " from " + name : "")); + }); + case Statement.ShowGrants(Optional name) -> read(reader -> showGrants(reader, name)); + case Statement.ShowTenants _ -> read(reader -> table(List.of("id", "name", "atoms", "edges", "payload bytes", "max atoms", "max edges", + "connections", "max connections"), Security.tenants(reader.engineView()).stream().filter(tenant -> session.principal().governs(tenant.id())).map(tenant -> { Quota.Usage usage = Security.usage(reader.engineView(), tenant.id()); + int connectionLimit = Access.tenantConnectionLimit(reader.engineView(), tenant.id()); return List.of(QueryResult.number(tenant.id()), QueryResult.text(tenant.name()), QueryResult.number(usage.atoms()), QueryResult.number(usage.edges()), QueryResult.number(usage.payloadBytes()), - limit(tenant.quota().maxAtoms()), limit(tenant.quota().maxEdges())); + limit(tenant.quota().maxAtoms()), limit(tenant.quota().maxEdges()), + QueryResult.number(database().tenantConnections(tenant.id())), + limit(connectionLimit < 0 ? Long.MAX_VALUE : connectionLimit)); }))); - case Statement.ShowUsers _ -> read(reader -> table(List.of("user", "tenant", "role"), Security.users(reader.engineView()).stream() + case Statement.ShowUsers _ -> read(reader -> table(List.of("user", "tenant", "role", "valid until", "connection limit"), + Security.users(reader.engineView()).stream() .filter(user -> session.principal().governs(user.tenant())) - .map(user -> List.of(QueryResult.text(user.name()), QueryResult.number(user.tenant()), QueryResult.text(user.role().name()))))); + .map(user -> { + Access.Options options = Access.options(reader.engineView(), user.name()); + return List.of(QueryResult.text(user.name()), QueryResult.number(user.tenant()), QueryResult.text(user.role().name()), + options.validUntil() == Long.MAX_VALUE ? QueryResult.text("infinity") : QueryResult.text(Instant.ofEpochMilli(options.validUntil()).toString()), + options.connectionLimit() < 0 ? QueryResult.text("unlimited") : QueryResult.number(options.connectionLimit())); + }))); case Statement.DropBranch(String name) -> { database().engine().dropBranch(session.branchId(name)); yield QueryResult.message("dropped branch " + name); @@ -349,8 +389,7 @@ QueryResult run(Statement statement) { private static boolean engineWide(Statement statement) { return switch (statement) { case Statement.CreateBranch _, Statement.DropBranch _, Statement.MergeBranch _, Statement.ShowBranches _, Statement.Sample _, - Statement.DiffGenerations _, Statement.DiffBranches _, Statement.History _, Statement.Stats _, - Statement.Checkpoint _, Statement.CheckpointAt _, Statement.Compact _, Statement.CreateTenant _, Statement.AlterTenant _, + Statement.DiffGenerations _, Statement.DiffBranches _, Statement.History _, Statement.CreateTenant _, Statement.AlterTenant _, Statement.CreateType _, Statement.CreateIndex _, Statement.CreateJsonIndex _ -> true; default -> false; }; @@ -359,6 +398,7 @@ private static boolean engineWide(Statement statement) { private static boolean administrative(Statement statement) { return switch (statement) { case Statement.CreateUser _, Statement.AlterUser _, Statement.DropUser _, Statement.ShowTenants _, Statement.ShowUsers _, + Statement.Grant _, Statement.Revoke _, Statement.CreateView _, Statement.RefreshView _, Statement.DropView _, Statement.ShowView _ -> true; default -> false; }; @@ -377,11 +417,101 @@ private int governedTenant(Reader reader, Optional name) { .orElseThrow(() -> HStoreException.invalid("user " + principal.user() + " may only manage users of its own tenant")).id(); } - private void governedUser(Reader reader, String name) { - Security.user(reader.engineView(), name).filter(user -> session.principal().governs(user.tenant())) + private Security.User governedUser(Reader reader, String name) { + return Security.user(reader.engineView(), name).filter(user -> session.principal().governs(user.tenant())) .orElseThrow(() -> HStoreException.invalid("unknown user '" + name + "'")); } + private boolean hadGlobalAdmin() { + return database().read(reader -> Security.hasGlobalAdmin(reader.engineView())); + } + + private static Long removeConnections(Map limits) { + Long connections = null; + for (String key : List.copyOf(limits.keySet())) { + if (key.equalsIgnoreCase("connections")) { + connections = limits.remove(key); + } + } + return connections; + } + + private QueryResult alterUser(Writer writer, Statement.AlterUser alter) { + Security.User user = governedUser(writer, alter.name()); + boolean attributes = alter.role().isPresent() || alter.validUntil().isPresent() || alter.connectionLimit().isPresent() + || alter.password().isPresent() && !user.name().equals(session.principal().user()); + if (attributes && user.role() == Role.ADMIN && !session.principal().global()) { + throw HStoreException.invalid("only an admin of the default tenant can change the role, password expiry or connection limit " + + "of an admin, or the password of another admin"); + } + Security.keepUnchanged(writer.transaction(), user); + List changes = new ArrayList<>(); + alter.role().filter(role -> role != user.role()).ifPresent(role -> { + Security.setRole(writer.transaction(), user.name(), role); + changes.add("role " + role); + }); + alter.password().ifPresent(password -> { + Security.setPassword(writer.transaction(), user.name(), password); + changes.add("password"); + }); + if (alter.validUntil().isPresent() || alter.connectionLimit().isPresent()) { + Access.Options current = Access.options(writer.engineView(), user.name()); + Access.setOptions(writer.transaction(), new Access.Options(user.name(), alter.validUntil().orElse(current.validUntil()), + (int) alter.connectionLimit().orElse(current.connectionLimit()))); + alter.validUntil().ifPresent(_ -> changes.add("password expiry")); + alter.connectionLimit().ifPresent(_ -> changes.add("connection limit")); + } + Security.keepAGlobalAdmin(writer.transaction(), hadGlobalAdmin()); + return QueryResult.message(changes.isEmpty() ? "nothing changed for " + user.name() : "changed " + String.join(", ", changes) + " of " + user.name()); + } + + private Security.User grantee(Writer writer, Access.Privilege privilege, String name) { + Security.User user = governedUser(writer, name); + if (privilege.onType() && user.role().readsEveryType()) { + throw HStoreException.invalid("user " + name + " has the " + user.role() + " role, which already covers every type; " + + "grants on types apply to users with ROLE none"); + } + if (!privilege.onType()) { + session.principal().requireGlobal(); + if (user.tenant() != Principal.DEFAULT_TENANT) { + throw HStoreException.invalid(privilege + " can only be granted to users of the default tenant"); + } + } + Security.keepUnchanged(writer.transaction(), user); + return user; + } + + private QueryResult showGrants(Reader reader, Optional name) { + Principal principal = session.principal(); + String wanted = name.orElse(principal.user()); + if (!wanted.equals(principal.user())) { + principal.requireAdmin(); + governedUser(reader, wanted); + } + return table(List.of("user", "privilege", "type"), Access.grants(reader.engineView()).stream() + .filter(grant -> name.isPresent() || !principal.role().canAdminister() ? grant.user().equals(wanted) + : Security.user(reader.engineView(), grant.user()).filter(user -> principal.governs(user.tenant())).isPresent()) + .map(grant -> List.of(QueryResult.text(grant.user()), QueryResult.text(grant.privilege().name()), + Access.isCapability(grant.type()) ? QueryResult.scalar(Value.NULL) + : QueryResult.text(reader.schema().type(reader.engineView(), grant.type()).map(TypeDef::name).orElse("#" + grant.type()))))); + } + + private static Optional capability(Statement statement) { + return switch (statement) { + case Statement.Stats _ -> Optional.of(Access.Privilege.MONITOR); + case Statement.Checkpoint _, Statement.CheckpointAt _, Statement.Compact _ -> Optional.of(Access.Privilege.MAINTAIN); + default -> Optional.empty(); + }; + } + + private void requireCapability(Access.Privilege capability) { + Principal principal = session.principal(); + if (!database().holds(principal, capability)) { + throw HStoreException.invalid("user " + principal.user() + " needs the " + capability + " privilege or the ADMIN role in the default " + + "tenant for this statement"); + } + } + private static String describe(CheckpointThreshold threshold) { return "checkpoint after " + threshold.bytes() + " bytes of log, " + switch (threshold.source()) { case AUTOMATIC -> "sized from the store"; @@ -458,7 +588,9 @@ private QueryResult query(Reader reader, Ast.Match match) { } return rows.toList(); }); - database().statistics().observe(reader.tenant(), plan.chosen().path().kind(), plan.chosen().rows(), produced.get()); + if (reader.principal().role().readsEveryType()) { + database().statistics().observe(reader.tenant(), plan.chosen().path().kind(), plan.chosen().rows(), produced.get()); + } boolean aggregate = match.projections().stream().anyMatch(projection -> Evaluator.isAggregate(projection.expr())); List> rows = aggregate ? List.of(match.projections().stream().map(projection -> Evaluator.isAggregate(projection.expr()) @@ -489,10 +621,10 @@ private static Cell component(double value) { } private RankQuery rankQuery(Reader reader, Statement.Rank statement) { - Optional type = statement.type().map(reader::requireType); + Optional type = statement.type().map(reader::requireReadableType); List slots = statement.slots().stream().map(slot -> new RankQuery.Slot(switch (slot.target()) { case Ast.RankTarget.Atom(Ast.Ref ref) -> new RankQuery.Target.Atom(reader.require(session.resolve(reader, ref)).id()); - case Ast.RankTarget.OfType(String name) -> new RankQuery.Target.OfType(reader.requireType(name)); + case Ast.RankTarget.OfType(String name) -> new RankQuery.Target.OfType(reader.requireReadableType(name)); }, Set.copyOf(slot.roles()), slot.weight().orElse(1), slot.required())).toList(); Evaluator evaluator = evaluator(reader); Optional time = statement.time().map(spec -> switch (spec) { @@ -631,7 +763,7 @@ private QueryResult setAlgebra(Writer writer, Statement.SetAlgebra algebra) { default -> throw HStoreException.invalid(algebra.op() + " does not produce a hyperedge"); }; long created = writer.edge(writer.require(leftId).type().name()); - writer.transaction().load(created, result.values().toList()); + writer.loadIncidences(created, result.values().toList()); algebra.into().ifPresent(name -> session.bind(name, created)); return QueryResult.table(List.of("edge", "cardinality"), List.of(List.of(atomCell(writer, created), QueryResult.number(result.size())))); } diff --git a/database/src/main/java/io/hstore/db/query/Parser.java b/database/src/main/java/io/hstore/db/query/Parser.java index c536563..803067b 100644 --- a/database/src/main/java/io/hstore/db/query/Parser.java +++ b/database/src/main/java/io/hstore/db/query/Parser.java @@ -18,6 +18,7 @@ import io.hstore.db.retrieve.RankQuery; import io.hstore.db.schema.AtomKind; import io.hstore.db.schema.TypeDef.PropertyDef; +import io.hstore.db.security.Access; import io.hstore.db.security.Role; import io.hstore.db.semantic.SemanticPlane; import io.hstore.db.temporal.BranchMerge; @@ -162,8 +163,41 @@ private Statement statement() { } expect("USER"); String name = identifierOrString(); - expect("PASSWORD"); - return new Statement.AlterUser(name, string()); + Optional password = Optional.empty(); + Optional role = Optional.empty(); + OptionalLong validUntil = OptionalLong.empty(); + OptionalLong connectionLimit = OptionalLong.empty(); + boolean changed = false; + while (true) { + if (password.isEmpty() && accept("PASSWORD")) { + password = Optional.of(string()); + } else if (role.isEmpty() && accept("ROLE")) { + role = Optional.of(option(Role.class, identifier())); + } else if (validUntil.isEmpty() && peek().is("VALID")) { + validUntil = OptionalLong.of(validUntil()); + } else if (connectionLimit.isEmpty() && peek().is("CONNECTION")) { + connectionLimit = OptionalLong.of(connectionLimit()); + } else { + break; + } + changed = true; + } + if (!changed) { + throw error("expected PASSWORD, ROLE, VALID UNTIL or CONNECTION LIMIT"); + } + return new Statement.AlterUser(name, password, role, validUntil, connectionLimit); + } + if (accept("GRANT")) { + Access.Privilege privilege = option(Access.Privilege.class, identifier()); + Optional type = typeOf(privilege); + expect("TO"); + return new Statement.Grant(privilege, type, identifierOrString()); + } + if (accept("REVOKE")) { + Access.Privilege privilege = option(Access.Privilege.class, identifier()); + Optional type = typeOf(privilege); + expect("FROM"); + return new Statement.Revoke(privilege, type, identifierOrString()); } if (peek().is("FORMAT")) { index++; @@ -420,6 +454,9 @@ private Statement statement() { if (accept("USERS")) { return new Statement.ShowUsers(); } + if (accept("GRANTS")) { + return new Statement.ShowGrants(accept("FOR") ? Optional.of(identifierOrString()) : Optional.empty()); + } expect("VIEWS"); return new Statement.ShowViews(); } @@ -446,7 +483,9 @@ private Statement create() { String password = string(); Optional tenant = accept("TENANT") ? Optional.of(identifier()) : Optional.empty(); Role role = accept("ROLE") ? option(Role.class, identifier()) : Role.WRITER; - return new Statement.CreateUser(name, password, tenant, role); + OptionalLong validUntil = peek().is("VALID") ? OptionalLong.of(validUntil()) : OptionalLong.empty(); + OptionalLong connectionLimit = peek().is("CONNECTION") ? OptionalLong.of(connectionLimit()) : OptionalLong.empty(); + return new Statement.CreateUser(name, password, tenant, role, validUntil, connectionLimit); } if (accept("BRANCH")) { String name = identifier(); @@ -752,6 +791,35 @@ private OptionalDouble preferenceWeight() { return accept("WEIGHT") ? OptionalDouble.of(number().doubleValue()) : OptionalDouble.empty(); } + private Optional typeOf(Access.Privilege privilege) { + if (!privilege.onType()) { + return Optional.empty(); + } + expect("ON"); + expect("TYPE"); + return Optional.of(identifier()); + } + + private long validUntil() { + expect("VALID"); + expect("UNTIL"); + if (peek().kind() == Token.Kind.STRING && peek().text().equalsIgnoreCase("infinity")) { + index++; + return Long.MAX_VALUE; + } + return instant(); + } + + private long connectionLimit() { + expect("CONNECTION"); + expect("LIMIT"); + long limit = longValue(); + if (limit < -1 || limit > 100_000) { + throw error("CONNECTION LIMIT must be -1 (no limit) or between 0 and 100000"); + } + return limit; + } + private Ast.Match match() { boolean edges; if (accept("EDGE") || accept("EDGES")) { diff --git a/database/src/main/java/io/hstore/db/query/Planner.java b/database/src/main/java/io/hstore/db/query/Planner.java index 623f0ba..fa8110e 100644 --- a/database/src/main/java/io/hstore/db/query/Planner.java +++ b/database/src/main/java/io/hstore/db/query/Planner.java @@ -55,6 +55,7 @@ String describe() { private static final double ENTRIES_PER_PAGE = 128; private static final double TREE_DEPTH = 3; private static final int MAX_INTERSECTED = 4; + private static final double BLIND_SELECTIVITY = 0.1; private final Reader reader; private final Statistics statistics; @@ -68,7 +69,7 @@ String describe() { } Plan plan(Ast.Match match) { - Optional type = match.type().map(reader::requireType); + Optional type = match.type().map(reader::requireReadableType); type.ifPresent(found -> { if (found.kind().isEdge() != match.edges()) { throw HStoreException.invalid(found.name() + " is not a " + (match.edges() ? "hyperedge" : "node") + " type"); @@ -144,7 +145,7 @@ private Candidate intersection(List parts, double population, List consumed = parts.stream().flatMap(part -> part.sources().stream()).toList(); AccessPath path = new AccessPath.Intersection(parts.stream().map(Candidate::path).toList()); - double corrected = rows * statistics.correction(reader.tenant(), path.kind()); + double corrected = rows * correction(path); double access = parts.stream().mapToDouble(part -> scanCost(part.path(), part.rows())).sum(); return new Candidate(path, consumed, corrected, access + VERIFY_WEIGHT * corrected * verificationWeight(conjuncts, consumed)); } @@ -157,7 +158,7 @@ private static double verificationWeight(List conjuncts, List consum } private Candidate candidate(AccessPath path, List sources, double rows, List conjuncts) { - double corrected = rows * statistics.correction(reader.tenant(), path.kind()); + double corrected = rows * correction(path); return new Candidate(path, sources, corrected, scanCost(path, corrected) + VERIFY_WEIGHT * corrected * verificationWeight(conjuncts, sources)); } @@ -202,7 +203,14 @@ private double unrecordedEstimate(AccessPath path, double population) { }; } + private double correction(AccessPath path) { + return reader.principal().role().readsEveryType() ? statistics.correction(reader.tenant(), path.kind()) : 1.0; + } + private double indexEstimate(AccessPath.IndexRange range) { + if (!reader.principal().role().readsEveryType()) { + return Math.ceil(reader.countOfType(range.type().id()) * BLIND_SELECTIVITY); + } TypeTag tag = Reader.indexedTag(range.type(), range.property()).orElseThrow(); OptionalInt key = reader.schema().existingPropertyKey(range.property()); if (key.isEmpty()) { diff --git a/database/src/main/java/io/hstore/db/query/Session.java b/database/src/main/java/io/hstore/db/query/Session.java index a5e6fae..fea2737 100644 --- a/database/src/main/java/io/hstore/db/query/Session.java +++ b/database/src/main/java/io/hstore/db/query/Session.java @@ -39,10 +39,13 @@ public final class Session implements AutoCloseable { private boolean authenticated; private Optional credentials = Optional.empty(); private boolean renewing; + private boolean stale; private Optional origin = Optional.empty(); private int failedAuthentications; private Optional scram = Optional.empty(); private Optional scramUser = Optional.empty(); + private Optional counted = Optional.empty(); + private boolean closed; private Optional clearTextRefusal = Optional.empty(); private record ScramAttempt(Scram.Server server, SignIns.Reservation reservation, Optional user) { @@ -114,7 +117,8 @@ public Optional scramFinal(String clientFinal) { throw malformed; } Optional found = outcome instanceof Scram.Outcome.Accepted(String user, String _) - ? database.read(reader -> Security.user(reader.engineView(), user)).filter(current -> attempt.user().equals(Optional.of(current))) + ? database.read(reader -> Security.user(reader.engineView(), user).filter(current -> attempt.user().equals(Optional.of(current))) + .filter(stored -> !Security.expired(reader.engineView(), stored.name()))) : Optional.empty(); if (found.isEmpty()) { attempt.reservation().failed(); @@ -143,10 +147,20 @@ private void requireCanAuthenticate() { } } - private void signIn(Security.User found) { - principal = Security.principal(found); + private void signIn(Security.User user) { + Principal found = Security.principal(user); + counted.ifPresent(database::leave); + try { + database.enter(found); + } catch (RuntimeException refused) { + counted.ifPresent(database::rejoin); + throw refused; + } + counted = Optional.of(found); + stale = false; + principal = found; authenticated = true; - credentials = Optional.of(found); + credentials = Optional.of(user); renewing = false; variables.clear(); branch = Branch.MAIN; @@ -164,6 +178,9 @@ public boolean credentialsCurrent() { if (!authenticated) { return true; } + if (stale) { + return false; + } Optional stored = database.read(reader -> Security.user(reader.engineView(), principal.user())); boolean current = stored.isPresent() && credentials.isPresent() && stored.get().tenant() == credentials.get().tenant() && stored.get().role() == credentials.get().role() @@ -193,12 +210,17 @@ public List executeAll(String source) { } public QueryResult execute(Statement statement) { + if (closed) { + throw HStoreException.invalid("this session is closed"); + } if (!(statement instanceof Statement.Authenticate) && !credentialsCurrent()) { - close(); - throw HStoreException.invalid("user " + principal.user() + " was dropped or its password changed; authenticate again"); + release(); + stale = true; + throw HStoreException.invalid("user " + principal.user() + " was dropped, or its password or role changed; authenticate again"); } QueryResult result = dispatch(statement); - if (statement instanceof Statement.AlterUser(String name, String _) && authenticated && name.equals(principal.user())) { + if (statement instanceof Statement.AlterUser(String name, Optional password, var _, var _, var _) + && password.isPresent() && authenticated && name.equals(principal.user())) { renewing = true; } return result; @@ -352,9 +374,19 @@ public boolean inTransaction() { @Override public void close() { - transaction.ifPresent(Transaction::abort); - transaction = Optional.empty(); - scram.ifPresent(pending -> pending.reservation().failed()); - scram = Optional.empty(); + closed = true; + release(); + } + + private void release() { + try { + transaction.ifPresent(Transaction::abort); + } finally { + transaction = Optional.empty(); + scram.ifPresent(pending -> pending.reservation().failed()); + scram = Optional.empty(); + counted.ifPresent(database::leave); + counted = Optional.empty(); + } } } diff --git a/database/src/main/java/io/hstore/db/security/Access.java b/database/src/main/java/io/hstore/db/security/Access.java new file mode 100644 index 0000000..2138df0 --- /dev/null +++ b/database/src/main/java/io/hstore/db/security/Access.java @@ -0,0 +1,184 @@ +// SPDX-FileCopyrightText: MetaCognition India Pvt Limited +// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 + +package io.hstore.db.security; + +import io.hstore.engine.HStoreException; +import io.hstore.engine.catalog.Slot; +import io.hstore.engine.page.ByteCursor; +import io.hstore.engine.tree.EntryMeasure; +import io.hstore.engine.tree.FingerprintMode; +import io.hstore.engine.tree.Hashing; +import io.hstore.engine.tree.TreeSchema; +import io.hstore.engine.tree.ValueCodec; +import io.hstore.engine.txn.Transaction; +import io.hstore.engine.txn.View; + +import java.util.Comparator; +import java.util.List; +import java.util.Optional; + +public final class Access { + + public enum Privilege { + READ, + WRITE, + MONITOR, + MAINTAIN; + + public boolean onType() { + return this == READ || this == WRITE; + } + } + + public record Grant(String user, int type, Privilege privilege) { + } + + public record TenantOptions(int tenant, int connectionLimit) { + } + + public record Options(String user, long validUntil, int connectionLimit) { + public static Options defaults(String user) { + return new Options(user, Long.MAX_VALUE, -1); + } + + public boolean expired(long now) { + return now >= validUntil; + } + } + + private static final int MONITOR_TARGET = -1; + private static final int MAINTAIN_TARGET = -2; + + private static final ValueCodec GRANT_CODEC = ValueCodec.rows( + grant -> 20 + ByteCursor.stringSize(grant.user()), + (out, grant) -> out.putString(grant.user()).putSignedVarLong(grant.type()).putByte(grant.privilege().ordinal()), + in -> new Grant(in.getString(), (int) in.getSignedVarLong(), Privilege.values()[in.getUnsignedByte()])); + + private static final ValueCodec OPTIONS_CODEC = ValueCodec.rows( + options -> 30 + ByteCursor.stringSize(options.user()), + (out, options) -> out.putString(options.user()).putSignedVarLong(options.validUntil()).putSignedVarLong(options.connectionLimit()), + in -> new Options(in.getString(), in.getSignedVarLong(), (int) in.getSignedVarLong())); + + public static final Slot GRANTS = Slot.primary(75, "grants", new TreeSchema<>(110, "grants", FingerprintMode.SET, + GRANT_CODEC, EntryMeasure.keyed(grant -> Hashing.of(Hashing.of(grant.user()), grant.type(), grant.privilege().ordinal())))); + public static final Slot USER_OPTIONS = Slot.primary(76, "user-options", new TreeSchema<>(111, "user-options", FingerprintMode.SET, + OPTIONS_CODEC, EntryMeasure.keyed(options -> Hashing.of(Hashing.of(options.user()), options.validUntil(), options.connectionLimit())))); + private static final ValueCodec TENANT_OPTIONS_CODEC = ValueCodec.rows( + _ -> 20, + (out, options) -> out.putSignedVarLong(options.tenant()).putSignedVarLong(options.connectionLimit()), + in -> new TenantOptions((int) in.getSignedVarLong(), (int) in.getSignedVarLong())); + + public static final Slot TENANT_OPTIONS = Slot.primary(77, "tenant-options", new TreeSchema<>(112, "tenant-options", + FingerprintMode.SET, TENANT_OPTIONS_CODEC, EntryMeasure.keyed(options -> Hashing.of(options.tenant(), options.connectionLimit())))); + public static final List> SLOTS = List.of(GRANTS, USER_OPTIONS, TENANT_OPTIONS); + + private Access() { + } + + private static int target(int type, Privilege privilege) { + return switch (privilege) { + case READ, WRITE -> type; + case MONITOR -> MONITOR_TARGET; + case MAINTAIN -> MAINTAIN_TARGET; + }; + } + + private static long key(String user, int target) { + return Hashing.of(Hashing.of(user), target); + } + + private static Optional stored(View view, String user, int target) { + return view.get(GRANTS, key(user, target)).filter(grant -> grant.user().equals(user) && grant.type() == target); + } + + public static Optional onType(View view, String user, int type) { + return stored(view, user, type).map(Grant::privilege); + } + + public static boolean holds(View view, String user, Privilege capability) { + if (capability.onType()) { + throw new IllegalArgumentException(capability + " is a privilege on a type"); + } + return stored(view, user, target(0, capability)).isPresent(); + } + + public static boolean grant(Transaction txn, String user, int type, Privilege privilege) { + int target = target(type, privilege); + Optional current = stored(txn, user, target); + if (current.isPresent() && (current.get().privilege() == privilege || current.get().privilege() == Privilege.WRITE)) { + return false; + } + requireFree(txn, user, target); + txn.put(GRANTS, key(user, target), new Grant(user, target, privilege)); + return true; + } + + public static boolean revoke(Transaction txn, String user, int type, Privilege privilege) { + int target = target(type, privilege); + Optional current = stored(txn, user, target); + if (current.isEmpty()) { + return false; + } + if (privilege == Privilege.WRITE) { + if (current.get().privilege() != Privilege.WRITE) { + return false; + } + txn.put(GRANTS, key(user, target), new Grant(user, target, Privilege.READ)); + } else { + txn.delete(GRANTS, key(user, target)); + } + return true; + } + + private static void requireFree(View view, String user, int target) { + view.get(GRANTS, key(user, target)) + .filter(grant -> !grant.user().equals(user) || grant.type() != target) + .ifPresent(_ -> { + throw HStoreException.invalid("cannot store this grant: its key collides with another grant"); + }); + } + + public static List grants(View view) { + return view.scan(GRANTS).values() + .sorted(Comparator.comparing(Grant::user).thenComparingInt(Grant::type)) + .toList(); + } + + public static void forget(Transaction txn, String user) { + grants(txn).stream().filter(grant -> grant.user().equals(user)) + .forEach(grant -> txn.delete(GRANTS, key(user, grant.type()))); + txn.merge(USER_OPTIONS, Hashing.of(user), _ -> true, current -> current.filter(options -> !options.user().equals(user))); + } + + public static Options options(View view, String user) { + return view.get(USER_OPTIONS, Hashing.of(user)).filter(options -> options.user().equals(user)).orElseGet(() -> Options.defaults(user)); + } + + public static void setOptions(Transaction txn, Options options) { + if (options.equals(Options.defaults(options.user()))) { + txn.delete(USER_OPTIONS, Hashing.of(options.user())); + } else { + txn.put(USER_OPTIONS, Hashing.of(options.user()), options); + } + } + + public static int tenantConnectionLimit(View view, int tenant) { + return view.get(TENANT_OPTIONS, tenant).filter(options -> options.tenant() == tenant).map(TenantOptions::connectionLimit).orElse(-1); + } + + public static void setTenantConnectionLimit(Transaction txn, int tenant, long limit) { + if (limit < -1 || limit > Integer.MAX_VALUE) { + throw HStoreException.invalid("a connection limit must be -1 (no limit) or between 0 and " + Integer.MAX_VALUE); + } + if (limit == -1) { + txn.delete(TENANT_OPTIONS, tenant); + } else { + txn.put(TENANT_OPTIONS, tenant, new TenantOptions(tenant, (int) limit)); + } + } + + public static boolean isCapability(int type) { + return type < 0; + } +} diff --git a/database/src/main/java/io/hstore/db/security/Quota.java b/database/src/main/java/io/hstore/db/security/Quota.java index 5c9429e..5e740ad 100644 --- a/database/src/main/java/io/hstore/db/security/Quota.java +++ b/database/src/main/java/io/hstore/db/security/Quota.java @@ -22,7 +22,7 @@ public Quota with(Map limits) { case "transaction_pages" -> new Quota(quota.maxAtoms, quota.maxEdges, quota.maxPayloadBytes, value, quota.maxQueryPages); case "query_pages" -> new Quota(quota.maxAtoms, quota.maxEdges, quota.maxPayloadBytes, quota.maxTransactionPages, value); default -> throw HStoreException.invalid("unknown quota '" + limit.getKey() - + "', expected atoms, edges, payload_bytes, transaction_pages or query_pages"); + + "', expected atoms, edges, payload_bytes, transaction_pages, query_pages or connections"); }; } return quota; diff --git a/database/src/main/java/io/hstore/db/security/Role.java b/database/src/main/java/io/hstore/db/security/Role.java index 3a0b47e..f30271d 100644 --- a/database/src/main/java/io/hstore/db/security/Role.java +++ b/database/src/main/java/io/hstore/db/security/Role.java @@ -6,7 +6,8 @@ public enum Role { ADMIN, WRITER, - READER; + READER, + NONE; public boolean canWrite() { return this != READER; @@ -15,4 +16,12 @@ public boolean canWrite() { public boolean canAdminister() { return this == ADMIN; } + + public boolean readsEveryType() { + return this != NONE; + } + + public boolean writesEveryType() { + return this == ADMIN || this == WRITER; + } } diff --git a/database/src/main/java/io/hstore/db/security/Security.java b/database/src/main/java/io/hstore/db/security/Security.java index 983b5a5..1100fd9 100644 --- a/database/src/main/java/io/hstore/db/security/Security.java +++ b/database/src/main/java/io/hstore/db/security/Security.java @@ -32,6 +32,7 @@ public record User(String name, String salt, String hash, int iterations, int te } public static final int ITERATIONS = 600_000; + private static final System.Logger LOG = System.getLogger("hstore.security"); private static final long SCRAM_SECRET = 0x5343_5241_4D00_0000L; private static final int SCRAM_SECRET_WORDS = 4; public static final int MIN_PASSWORD_LENGTH = 8; @@ -70,7 +71,7 @@ public record User(String name, String salt, String hash, int iterations, int te public static final Slot USAGE = Slot.primary(46, "tenant-usage", new TreeSchema<>(83, "tenant-usage", FingerprintMode.SET, USAGE_CODEC, EntryMeasure.keyed(usage -> Hashing.of(usage.atoms(), usage.edges(), usage.payloadBytes())))); - public static final List> SLOTS = List.of(TENANTS, USERS, USAGE); + public static final List> SLOTS = List.of(TENANTS, USERS, USAGE, Access.GRANTS, Access.USER_OPTIONS, Access.TENANT_OPTIONS); public static final Tenant DEFAULT = new Tenant(Principal.DEFAULT_TENANT, "default", Quota.UNLIMITED); @@ -234,6 +235,58 @@ public static void requirePassword(String password) { public static void dropUser(Transaction txn, String name) { user(txn, name).orElseThrow(() -> HStoreException.invalid("unknown user '" + name + "'")); txn.delete(USERS, userKey(name)); + Access.forget(txn, name); + } + + public static void setRole(Transaction txn, String name, Role role) { + User current = user(txn, name).orElseThrow(() -> HStoreException.invalid("unknown user '" + name + "'")); + txn.put(USERS, userKey(name), new User(current.name(), current.salt(), current.hash(), current.iterations(), current.tenant(), role)); + } + + public static void keepUnchanged(Transaction txn, User user) { + txn.merge(USERS, userKey(user.name()), current -> current.equals(Optional.of(user)), current -> current); + } + + public static long globalAdmins(View view) { + return users(view).stream().filter(Security::globalAdmin).count(); + } + + public static boolean globalAdmin(User user) { + return user.tenant() == Principal.DEFAULT_TENANT && user.role() == Role.ADMIN; + } + + public static boolean hasGlobalAdmin(View view) { + return users(view).stream().anyMatch(Security::globalAdmin); + } + + public static void keepAGlobalAdmin(Transaction txn, boolean hadGlobalAdmin) { + List all = users(txn); + if (all.isEmpty() || !hadGlobalAdmin && all.stream().noneMatch(Security::globalAdmin)) { + return; + } + List admins = all.stream().filter(user -> globalAdmin(user) && neverExpires(Access.options(txn, user.name()))).toList(); + if (admins.isEmpty()) { + throw HStoreException.invalid("this would leave no admin of the default tenant whose password never expires; " + + "create or restore one first"); + } + for (User admin : admins) { + txn.merge(USERS, userKey(admin.name()), current -> current.filter(user -> user.name().equals(admin.name()) && globalAdmin(user)).isPresent(), + current -> current); + txn.merge(Access.USER_OPTIONS, Hashing.of(admin.name()), current -> neverExpires(current.filter(options -> options.user().equals(admin.name())) + .orElseGet(() -> Access.Options.defaults(admin.name()))), current -> current); + } + } + + private static boolean neverExpires(Access.Options options) { + return options.validUntil() == Long.MAX_VALUE; + } + + public static boolean expired(View view, String name) { + boolean expired = Access.options(view, name).expired(System.currentTimeMillis()); + if (expired) { + LOG.log(System.Logger.Level.WARNING, "user \"{0}\" tried to sign in after its password expired", name); + } + return expired; } public static Optional user(View view, String name) { @@ -253,7 +306,7 @@ public static Optional signIn(View view, String name, String password) { if (stored.isEmpty()) { derive(password, UNKNOWN_USER_SALT, ITERATIONS); } - return stored.filter(user -> matches(user, password)); + return stored.filter(user -> matches(user, password)).filter(user -> !expired(view, user.name())); } public static boolean hasUsers(View view) { diff --git a/database/src/main/java/io/hstore/db/signal/Signals.java b/database/src/main/java/io/hstore/db/signal/Signals.java index 08ef451..55320ca 100644 --- a/database/src/main/java/io/hstore/db/signal/Signals.java +++ b/database/src/main/java/io/hstore/db/signal/Signals.java @@ -76,7 +76,7 @@ private Signals() { public static long register(Writer writer, Signal signal) { writer.principal().requireWrite(); - writer.require(signal.atom()); + writer.requireWritable(signal.atom()); long id = writer.database().engine().transactions().allocateAtom(); writer.transaction().put(SIGNALS, id, signal); return id; diff --git a/database/src/main/java/io/hstore/db/temporal/StateBindings.java b/database/src/main/java/io/hstore/db/temporal/StateBindings.java index ee5698f..045359b 100644 --- a/database/src/main/java/io/hstore/db/temporal/StateBindings.java +++ b/database/src/main/java/io/hstore/db/temporal/StateBindings.java @@ -57,7 +57,7 @@ private static Binding read(ByteCursor in) { public static void bind(Writer writer, long atom, String schema, Value value, Validity validity) { writer.principal().requireWrite(); - writer.require(atom); + writer.requireWritable(atom); int schemaId = writer.database().engine().dictionary().name(STATE_SCHEMA_NAMESPACE, Dictionary.requireName("state schema name", schema)); writer.transaction().update(STATE, atom, current -> Optional.of(new Binding(schemaId, value, validity.from(), validity.to(), current.map(Binding::version).orElse(0L) + 1))); diff --git a/database/src/test/java/io/hstore/db/PermissionsTest.java b/database/src/test/java/io/hstore/db/PermissionsTest.java new file mode 100644 index 0000000..45e5e23 --- /dev/null +++ b/database/src/test/java/io/hstore/db/PermissionsTest.java @@ -0,0 +1,559 @@ +// SPDX-FileCopyrightText: MetaCognition India Pvt Limited +// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 + +package io.hstore.db; + +import io.hstore.db.query.QueryResult; +import io.hstore.db.query.Session; +import io.hstore.db.security.Access; +import io.hstore.db.security.Principal; +import io.hstore.db.security.Scram; +import io.hstore.db.security.Security; +import io.hstore.engine.EngineOptions; +import io.hstore.engine.HStoreException; +import io.hstore.engine.txn.TxnOptions; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.nio.file.Path; +import java.time.Instant; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class PermissionsTest { + + @TempDir + Path directory; + + HypergraphDatabase database; + Session admin; + + @BeforeEach + void open() { + database = HypergraphDatabase.open(directory, DatabaseOptions.defaults().withEngine(EngineOptions.defaults().withPageSize(4096))); + admin = new Session(database); + admin.executeAll(""" + CREATE NODE TYPE Patient (mrn STRING INDEXED); + CREATE NODE TYPE Doctor (name STRING); + CREATE NODE TYPE Secret (mrn STRING INDEXED); + CREATE SET EDGE TYPE Encounter ROLES (patient, doctor); + CREATE USER root PASSWORD 'root-password' ROLE admin; + CREATE USER clerk PASSWORD 'clerk-password' ROLE none; + CREATE USER ops PASSWORD 'ops-password' ROLE reader; + INSERT NODE Patient 'p1' {mrn: 'A-1'} AS $p; + INSERT NODE Doctor 'd1' {name: 'Ada'} AS $d; + INSERT EDGE Encounter MEMBERS ($p AS patient, $d AS doctor) AS $e; + """); + } + + @AfterEach + void close() { + admin.close(); + database.close(); + } + + private Session login(String user, String password) { + Session session = new Session(database); + assertTrue(session.authenticate(user, password), user); + return session; + } + + private static String cell(QueryResult result) { + return result.rows().getFirst().getFirst().render(); + } + + private long id(String variable) { + return admin.variables().get(variable); + } + + @Test + void roleNoneSeesOnlyGrantedTypes() { + try (Session clerk = login("clerk", "clerk-password")) { + String denied = assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("MATCH NODE n:Patient RETURN n")).getMessage(); + assertTrue(denied.contains("no READ privilege on type Patient"), denied); + assertEquals("0", cell(clerk.execute("MATCH NODE n RETURN count(*)"))); + assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("DESCRIBE @" + id("p"))); + assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("PATTERN (n: NODE Patient)")); + assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("RANK EDGE e LIKE (ANY Patient) TOP 3")); + + admin.execute("GRANT READ ON TYPE Patient TO clerk"); + assertEquals("1", cell(clerk.execute("MATCH NODE n:Patient RETURN count(*)"))); + assertEquals("1", cell(clerk.execute("MATCH NODE n:Patient WHERE n.mrn = 'A-1' RETURN count(*)"))); + assertEquals("1", cell(clerk.execute("MATCH NODE n RETURN count(*)"))); + assertEquals(0, clerk.execute("INCIDENT TO @" + id("p")).rows().size()); + assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("MEMBERS OF @" + id("e"))); + assertEquals("0", cell(clerk.execute("MATCH NODE n:Patient RETURN degree(n)"))); + + admin.execute("GRANT READ ON TYPE Encounter TO clerk"); + List members = clerk.execute("MEMBERS OF @" + id("e")).rows().stream().map(row -> row.get(1).render()).toList(); + assertEquals(List.of("@" + id("p") + " Patient:'p1'", "@" + id("d")), members); + assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("DESCRIBE @" + id("d"))); + assertEquals(1, clerk.execute("INCIDENT TO @" + id("p")).rows().size()); + assertEquals("1", cell(clerk.execute("MATCH EDGE e:Encounter WHERE e HAS @" + id("p") + " AS patient RETURN count(*)"))); + assertEquals("0", cell(clerk.execute("MATCH EDGE e:Encounter WHERE e HAS @" + id("d") + " RETURN count(*)"))); + } + } + + @Test + void writesNeedTheWritePrivilegeOfTheChangedType() { + admin.executeAll("GRANT READ ON TYPE Patient TO clerk; GRANT READ ON TYPE Encounter TO clerk;"); + try (Session clerk = login("clerk", "clerk-password")) { + String denied = assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("INSERT NODE Patient 'p2'")).getMessage(); + assertTrue(denied.contains("no WRITE privilege on type Patient"), denied); + assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("SET @" + id("p") + ".mrn = 'X'")); + admin.execute("GRANT WRITE ON TYPE Patient TO clerk"); + clerk.execute("INSERT NODE Patient 'p2' AS $p2"); + clerk.execute("SET @" + id("p") + ".mrn = 'A-9'"); + assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("INSERT EDGE Encounter MEMBERS ($p2 AS patient)")); + assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("ADD $p2 TO @" + id("e") + " AS patient")); + assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("SET @" + id("d") + ".name = 'Eve'")); + assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("DELETE @" + id("d"))); + assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("EVIDENCE @" + id("p") + " SOURCE 'x'")); + admin.execute("GRANT WRITE ON TYPE Encounter TO clerk"); + clerk.execute("ADD $p2 TO @" + id("e") + " AS patient"); + assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("ADD @" + id("d") + " TO @" + id("e") + " AS doctor")); + admin.execute("REVOKE WRITE ON TYPE Patient FROM clerk"); + assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("INSERT NODE Patient 'p3'")); + assertEquals("2", cell(clerk.execute("MATCH NODE n:Patient RETURN count(*)"))); + admin.execute("REVOKE READ ON TYPE Patient FROM clerk"); + assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("MATCH NODE n:Patient RETURN count(*)")); + } + assertEquals("A-9", cell(admin.execute("MATCH NODE n:Patient WHERE n = @" + id("p") + " RETURN n.mrn"))); + } + + @Test + void grantsAreCheckedAndListed() { + String covered = assertThrows(HStoreException.InvalidSchema.class, () -> admin.execute("GRANT READ ON TYPE Patient TO ops")).getMessage(); + assertTrue(covered.contains("already covers every type"), covered); + assertThrows(HStoreException.InvalidSchema.class, () -> admin.execute("GRANT READ ON TYPE Missing TO clerk")); + assertThrows(HStoreException.InvalidSchema.class, () -> admin.execute("GRANT READ ON TYPE Patient TO nobody")); + assertThrows(HStoreException.class, () -> admin.execute("GRANT DELETE ON TYPE Patient TO clerk")); + admin.execute("GRANT WRITE ON TYPE Patient TO clerk"); + assertTrue(admin.execute("GRANT READ ON TYPE Patient TO clerk").message().contains("already holds")); + admin.execute("GRANT MONITOR TO ops"); + QueryResult all = admin.execute("SHOW GRANTS"); + assertEquals(List.of(List.of("clerk", "WRITE", "Patient"), List.of("ops", "MONITOR", "NULL")), + all.rows().stream().map(row -> row.stream().map(cell -> cell.render().toUpperCase().equals("NULL") ? "NULL" : cell.render()).toList()).toList()); + try (Session clerk = login("clerk", "clerk-password")) { + assertEquals(1, clerk.execute("SHOW GRANTS").rows().size()); + assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("SHOW GRANTS FOR ops")); + assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("GRANT READ ON TYPE Doctor TO clerk")); + } + admin.execute("DROP USER clerk"); + admin.execute("CREATE USER clerk PASSWORD 'clerk-password' ROLE none"); + assertEquals(1, admin.execute("SHOW GRANTS").rows().size()); + assertEquals(List.of("user", "tenant", "role", "valid until", "connection limit"), admin.execute("SHOW USERS").columns()); + } + + @Test + void tenantAdminsGrantOnlyInsideTheirTenant() { + admin.executeAll(""" + CREATE TENANT acme; + CREATE USER boss PASSWORD 'boss-password' TENANT acme ROLE admin; + CREATE USER temp PASSWORD 'temp-password' TENANT acme ROLE none; + """); + try (Session boss = login("boss", "boss-password")) { + boss.execute("GRANT READ ON TYPE Patient TO temp"); + assertThrows(HStoreException.InvalidSchema.class, () -> boss.execute("GRANT READ ON TYPE Patient TO clerk")); + assertThrows(HStoreException.InvalidSchema.class, () -> boss.execute("GRANT MONITOR TO temp")); + assertEquals(List.of("temp"), boss.execute("SHOW GRANTS").rows().stream().map(row -> row.getFirst().render()).toList()); + } + assertThrows(HStoreException.InvalidSchema.class, () -> admin.execute("GRANT MONITOR TO temp")); + } + + @Test + void capabilitiesOpenEngineStatementsWithoutAdmin() { + try (Session ops = login("ops", "ops-password")) { + assertThrows(HStoreException.InvalidSchema.class, () -> ops.execute("STATS")); + assertThrows(HStoreException.InvalidSchema.class, () -> ops.execute("CHECKPOINT")); + admin.execute("GRANT MONITOR TO ops"); + assertFalse(ops.execute("STATS").rows().isEmpty()); + assertThrows(HStoreException.InvalidSchema.class, () -> ops.execute("CHECKPOINT")); + admin.execute("GRANT MAINTAIN TO ops"); + ops.execute("CHECKPOINT"); + ops.execute("COMPACT"); + assertThrows(HStoreException.InvalidSchema.class, () -> ops.execute("HISTORY")); + assertThrows(HStoreException.InvalidSchema.class, () -> ops.execute("CREATE NODE TYPE Nope")); + admin.execute("REVOKE MONITOR FROM ops"); + assertThrows(HStoreException.InvalidSchema.class, () -> ops.execute("STATS")); + } + } + + @Test + void rolesChangeAndTheLastAdminStays() { + try (Session ops = login("ops", "ops-password")) { + assertThrows(HStoreException.InvalidSchema.class, () -> ops.execute("INSERT NODE Patient 'p9'")); + admin.execute("ALTER USER ops ROLE writer"); + String ended = assertThrows(HStoreException.InvalidSchema.class, () -> ops.execute("WHOAMI")).getMessage(); + assertTrue(ended.contains("authenticate again"), ended); + } + try (Session ops = login("ops", "ops-password")) { + ops.execute("INSERT NODE Patient 'p9'"); + } + try (Session root = login("root", "root-password")) { + String last = assertThrows(HStoreException.InvalidSchema.class, () -> root.execute("ALTER USER root ROLE reader")).getMessage(); + assertTrue(last.contains("no admin of the default tenant whose password never expires"), last); + assertTrue(assertThrows(HStoreException.InvalidSchema.class, () -> root.execute("DROP USER root")).getMessage() + .contains("no admin of the default tenant whose password never expires")); + assertThrows(HStoreException.InvalidSchema.class, () -> root.execute("ALTER USER root VALID UNTIL '2000-01-01'")); + String soon = Instant.now().plusSeconds(1).toString(); + assertTrue(assertThrows(HStoreException.InvalidSchema.class, () -> root.execute("ALTER USER root VALID UNTIL '" + soon + "'")) + .getMessage().contains("whose password never expires")); + assertThrows(HStoreException.InvalidSchema.class, () -> root.execute("ALTER USER root VALID UNTIL '2999-01-01'")); + root.execute("ALTER USER ops ROLE admin"); + root.execute("ALTER USER root VALID UNTIL '2999-01-01'"); + assertThrows(HStoreException.InvalidSchema.class, () -> root.execute("ALTER USER ops VALID UNTIL '2999-01-01'")); + root.execute("ALTER USER root VALID UNTIL 'infinity'"); + root.execute("CREATE USER temp PASSWORD 'temp-password' ROLE admin VALID UNTIL '2999-01-01'"); + root.execute("ALTER USER root ROLE reader"); + } + assertTrue(admin.execute("ALTER USER ops PASSWORD 'ops-password-2' CONNECTION LIMIT 3 VALID UNTIL 'infinity'").message() + .contains("password, password expiry, connection limit")); + assertThrows(HStoreException.class, () -> admin.execute("ALTER USER ops")); + assertThrows(HStoreException.class, () -> admin.execute("ALTER USER ops CONNECTION LIMIT -2")); + } + + @Test + void expiredPasswordsCannotSignIn() { + admin.execute("ALTER USER clerk VALID UNTIL '2000-01-01'"); + assertFalse(new Session(database).authenticate("clerk", "clerk-password")); + try (Session session = new Session(database)) { + Scram.Client client = new Scram.Client("clerk", "clerk-password", Optional.empty()); + assertTrue(session.scramFinal(client.last(session.scramFirst(Scram.MECHANISM, client.first(), Optional.empty()))).isEmpty()); + } + admin.execute("ALTER USER clerk VALID UNTIL '2999-01-01'"); + assertTrue(new Session(database).authenticate("clerk", "clerk-password")); + admin.execute("CREATE USER brief PASSWORD 'brief-password' ROLE reader VALID UNTIL 1"); + assertFalse(new Session(database).authenticate("brief", "brief-password")); + admin.execute("ALTER USER brief VALID UNTIL 'infinity'"); + assertTrue(new Session(database).authenticate("brief", "brief-password")); + assertEquals("infinity", admin.execute("SHOW USERS").rows().stream().filter(row -> row.getFirst().render().equals("brief")) + .findFirst().orElseThrow().get(3).render()); + } + + @Test + void connectionLimitsCountSignedInSessions() { + admin.execute("ALTER USER ops CONNECTION LIMIT 1"); + Session first = login("ops", "ops-password"); + Session second = new Session(database); + String limited = assertThrows(HStoreException.ResourceLimit.class, () -> second.authenticate("ops", "ops-password")).getMessage(); + assertTrue(limited.contains("too many connections for user ops"), limited); + first.close(); + assertTrue(second.authenticate("ops", "ops-password")); + second.close(); + assertEquals(0, database.connections("ops")); + admin.execute("ALTER USER root CONNECTION LIMIT 0"); + try (Session one = login("root", "root-password"); Session two = login("root", "root-password")) { + assertTrue(one.authenticated() && two.authenticated()); + assertEquals(2, database.connections("root")); + } + admin.execute("ALTER USER ops CONNECTION LIMIT -1"); + try (Session one = login("ops", "ops-password"); Session two = login("ops", "ops-password")) { + assertTrue(one.authenticated()); + two.authenticate("clerk", "clerk-password"); + assertEquals(1, database.connections("ops")); + assertEquals(1, database.connections("clerk")); + } + assertEquals(0, database.connections("ops")); + assertEquals(0, database.connections("clerk")); + } + + @Test + void reviewFindingsStayFixed() { + admin.executeAll(""" + INSERT NODE Secret 's1' {mrn: 'S-1'}; + INSERT NODE Patient 'lone' {mrn: 'L-1'} AS $lone; + INSERT EDGE Encounter MEMBERS ($p AS patient, $d AS doctor) AS $e2; + EVIDENCE 'lab report' SOURCE $p AS $ev; + GRANT WRITE ON TYPE Patient TO clerk; + """); + try (Session clerk = login("clerk", "clerk-password")) { + assertTrue(admin.execute("TRACE @" + id("ev")).render().contains("lab report")); + assertFalse(clerk.execute("TRACE @" + id("ev")).render().contains("lab report")); + String present = assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("DESCRIBE @Doctor:'d1'")).getMessage(); + String absent = assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("DESCRIBE @Doctor:'nobody'")).getMessage(); + assertEquals(present, absent); + String cascade = assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("DELETE @" + id("p"))).getMessage(); + assertTrue(cascade.contains("member of a hyperedge"), cascade); + clerk.execute("DELETE @" + id("lone")); + String known = clerk.execute("EXPLAIN MATCH NODE n:Patient WHERE n.mrn = 'S-1'").message(); + String unknown = clerk.execute("EXPLAIN MATCH NODE n:Patient WHERE n.mrn = 'nowhere'").message(); + assertEquals(unknown.replace("nowhere", "?"), known.replace("S-1", "?")); + admin.execute("GRANT WRITE ON TYPE Encounter TO clerk"); + assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("INTERSECT @" + id("e") + ", @" + id("e2") + " INTO $both")); + + clerk.execute("BEGIN"); + assertEquals("1", cell(clerk.execute("MATCH NODE n:Patient RETURN count(*)"))); + long before = database.engine().transactions().current().id(); + admin.execute("REVOKE READ ON TYPE Patient FROM clerk"); + assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("MATCH NODE n:Patient RETURN count(*)")); + clerk.execute("ROLLBACK"); + assertThrows(HStoreException.InvalidSchema.class, () -> clerk.execute("AT GENERATION " + before + " MATCH NODE n:Patient RETURN count(*)")); + } + assertEquals(1, admin.execute("SHOW GRANTS FOR clerk").rows().size()); + } + + @Test + void concurrentAdminRemovalsCannotBothCommit() { + admin.execute("CREATE USER second PASSWORD 'second-password' ROLE admin"); + try (Session root = login("root", "root-password"); Session other = login("second", "second-password")) { + root.execute("BEGIN"); + other.execute("BEGIN"); + root.execute("ALTER USER second ROLE writer"); + other.execute("ALTER USER root ROLE writer"); + root.execute("COMMIT"); + assertThrows(HStoreException.class, () -> other.execute("COMMIT")); + } + assertEquals(1, admin.execute("SHOW USERS").rows().stream().filter(row -> row.get(2).render().equals("ADMIN") + && row.get(1).render().equals("0")).count()); + } + + @Test + void theFirstGlobalAdminCannotExpire(@TempDir Path elsewhere) { + try (HypergraphDatabase fresh = HypergraphDatabase.open(elsewhere); Session system = new Session(fresh)) { + assertTrue(assertThrows(HStoreException.InvalidSchema.class, + () -> system.execute("CREATE USER first PASSWORD 'first-password' ROLE admin VALID UNTIL '2999-01-01'")) + .getMessage().contains("whose password never expires")); + system.execute("CREATE USER first PASSWORD 'first-password' ROLE admin"); + } + } + + @Test + void everyChangeLeavesAGlobalAdmin(@TempDir Path elsewhere) { + try (HypergraphDatabase fresh = HypergraphDatabase.open(elsewhere); Session system = new Session(fresh)) { + system.execute("CREATE USER early PASSWORD 'early-password' ROLE reader"); + system.execute("CREATE USER root PASSWORD 'root-password' ROLE admin"); + system.execute("DROP USER early"); + system.execute("BEGIN"); + system.execute("DROP USER root"); + assertThrows(HStoreException.InvalidSchema.class, () -> system.execute("CREATE USER bob PASSWORD 'bob-password' ROLE reader")); + system.execute("ROLLBACK"); + try (Session dropping = new Session(fresh); Session creating = new Session(fresh)) { + dropping.execute("BEGIN"); + creating.execute("BEGIN"); + dropping.execute("DROP USER root"); + creating.execute("CREATE USER bob PASSWORD 'bob-password' ROLE reader"); + dropping.execute("COMMIT"); + assertThrows(HStoreException.class, () -> creating.execute("COMMIT")); + } + assertEquals(List.of(), fresh.read(reader -> Security.users(reader.engineView()))); + } + } + + @Test + void restatingARoleStillGuardsAgainstARacingDrop() { + try (Session altering = new Session(database)) { + altering.execute("BEGIN"); + altering.execute("ALTER USER ops ROLE reader VALID UNTIL '2000-01-01'"); + admin.execute("DROP USER ops"); + admin.execute("CREATE USER ops PASSWORD 'ops-new-password' ROLE reader"); + assertThrows(HStoreException.class, () -> altering.execute("COMMIT")); + } + assertTrue(new Session(database).authenticate("ops", "ops-new-password")); + } + + @Test + void signingInAgainAsARecreatedUserCountsInItsNewTenant() { + admin.executeAll(""" + CREATE TENANT a; + CREATE TENANT b QUOTA (CONNECTIONS 1); + CREATE USER mover PASSWORD 'mover-password' TENANT a ROLE reader; + CREATE USER filler PASSWORD 'filler-password' TENANT b ROLE reader; + """); + int a = database.read(reader -> Security.requireTenant(reader.engineView(), "a").id()); + int b = database.read(reader -> Security.requireTenant(reader.engineView(), "b").id()); + try (Session mover = login("mover", "mover-password"); Session filler = login("filler", "filler-password")) { + assertTrue(filler.authenticated()); + admin.executeAll("DROP USER mover; CREATE USER mover PASSWORD 'mover-password' TENANT b ROLE reader;"); + assertThrows(HStoreException.ResourceLimit.class, () -> mover.authenticate("mover", "mover-password")); + assertEquals(1, database.tenantConnections(a)); + assertEquals(1, database.tenantConnections(b)); + } + assertEquals(0, database.tenantConnections(a)); + assertEquals(0, database.tenantConnections(b)); + admin.execute("ALTER TENANT b QUOTA (Connections -1)"); + } + + @Test + void aClosedSessionRunsNothing() { + Session ops = login("ops", "ops-password"); + ops.close(); + assertThrows(HStoreException.InvalidSchema.class, () -> ops.execute("BEGIN")); + assertEquals(0, database.connections("ops")); + } + + @Test + void tenantAdminsChangeOnlyTheirOwnAdminPassword() { + admin.executeAll(""" + CREATE TENANT acme; + CREATE USER boss PASSWORD 'boss-password' TENANT acme ROLE admin; + CREATE USER peer PASSWORD 'peer-password' TENANT acme ROLE admin; + """); + try (Session boss = login("boss", "boss-password")) { + assertThrows(HStoreException.InvalidSchema.class, () -> boss.execute("ALTER USER peer PASSWORD 'taken-over-1'")); + boss.execute("ALTER USER boss PASSWORD 'boss-password-2'"); + } + assertTrue(new Session(database).authenticate("peer", "peer-password")); + } + + @Test + void concurrentExpiriesCannotLockEveryAdminOut() { + admin.execute("CREATE USER second PASSWORD 'second-password' ROLE admin"); + try (Session root = login("root", "root-password"); Session other = login("second", "second-password")) { + root.execute("BEGIN"); + other.execute("BEGIN"); + root.execute("ALTER USER second VALID UNTIL '2000-01-01'"); + other.execute("ALTER USER root VALID UNTIL '2000-01-01'"); + root.execute("COMMIT"); + assertThrows(HStoreException.class, () -> other.execute("COMMIT")); + } + assertTrue(new Session(database).authenticate("root", "root-password")); + assertFalse(new Session(database).authenticate("second", "second-password")); + } + + @Test + void futureExpiriesOnBothAdminsCannotBothCommit() { + admin.execute("CREATE USER second PASSWORD 'second-password' ROLE admin"); + try (Session one = new Session(database); Session two = new Session(database)) { + one.execute("BEGIN"); + two.execute("BEGIN"); + one.execute("ALTER USER second VALID UNTIL '2999-01-01'"); + two.execute("ALTER USER root VALID UNTIL '2999-01-01'"); + one.execute("COMMIT"); + assertThrows(HStoreException.class, () -> two.execute("COMMIT")); + } + } + + @Test + void aRoleChangeRacingAnExpiryCannotLockEveryAdminOut() { + admin.execute("CREATE USER second PASSWORD 'second-password' ROLE admin"); + try (Session one = new Session(database); Session two = new Session(database)) { + one.execute("BEGIN"); + two.execute("BEGIN"); + one.execute("ALTER USER second ROLE writer"); + two.execute("ALTER USER root VALID UNTIL '2999-01-01'"); + one.execute("COMMIT"); + assertThrows(HStoreException.class, () -> two.execute("COMMIT")); + } + } + + @Test + void loadingAnEmptyMemberListStillNeedsWriteAccess() { + admin.execute("GRANT WRITE ON TYPE Patient TO clerk; GRANT READ ON TYPE Encounter TO clerk"); + Principal clerk = database.read(reader -> Security.user(reader.engineView(), "clerk")).map(Security::principal).orElseThrow(); + long encounter = id("e"); + assertThrows(HStoreException.InvalidSchema.class, () -> database.write(TxnOptions.defaults(), clerk, writer -> { + writer.load(encounter, new long[0], MemberSpec.PLAIN); + return null; + })); + assertEquals("1", cell(admin.execute("MATCH EDGE e:Encounter WHERE e HAS @Patient:'p1' RETURN count(*)"))); + } + + @Test + void aGrantRacingADropDoesNotOutliveTheUser() { + try (Session granting = new Session(database); Session dropping = new Session(database)) { + granting.execute("BEGIN"); + dropping.execute("BEGIN"); + granting.execute("GRANT READ ON TYPE Secret TO clerk"); + dropping.execute("DROP USER clerk"); + dropping.execute("COMMIT"); + assertThrows(HStoreException.class, () -> granting.execute("COMMIT")); + } + assertEquals(0, admin.execute("SHOW GRANTS").rows().stream().filter(row -> row.getFirst().render().equals("clerk")).count()); + try (Session altering = new Session(database); Session dropping = new Session(database)) { + altering.execute("BEGIN"); + dropping.execute("BEGIN"); + altering.execute("ALTER USER ops CONNECTION LIMIT 1"); + dropping.execute("DROP USER ops"); + dropping.execute("COMMIT"); + assertThrows(HStoreException.class, () -> altering.execute("COMMIT")); + } + database.write(writer -> { + Access.grant(writer.transaction(), "clerk", writer.requireType("Secret").id(), Access.Privilege.READ); + Access.setOptions(writer.transaction(), new Access.Options("clerk", 1, 1)); + return null; + }); + admin.execute("CREATE USER clerk PASSWORD 'clerk-password-2' ROLE none"); + assertEquals(0, admin.execute("SHOW GRANTS").rows().stream().filter(row -> row.getFirst().render().equals("clerk")).count()); + assertTrue(new Session(database).authenticate("clerk", "clerk-password-2")); + } + + @Test + void signingInAgainAsTheSameUserKeepsOneConnection() { + admin.execute("ALTER USER ops CONNECTION LIMIT 1"); + try (Session ops = login("ops", "ops-password")) { + assertTrue(ops.authenticate("ops", "ops-password")); + assertEquals(1, database.connections("ops")); + assertTrue(ops.authenticate("clerk", "clerk-password")); + assertEquals(0, database.connections("ops")); + assertTrue(ops.authenticate("ops", "ops-password")); + } + assertEquals(0, database.connections("ops")); + } + + @Test + void tenantsCanBeLimitedInConnections() { + admin.executeAll(""" + CREATE TENANT acme QUOTA (connections 2); + CREATE USER boss PASSWORD 'boss-password' TENANT acme ROLE admin CONNECTION LIMIT 1; + """); + try (Session boss = login("boss", "boss-password")) { + boss.execute("CREATE USER helper PASSWORD 'helper-password' ROLE admin"); + try (Session helper = login("helper", "helper-password")) { + assertTrue(helper.authenticated()); + String limited = assertThrows(HStoreException.ResourceLimit.class, + () -> new Session(database).authenticate("helper", "helper-password")).getMessage(); + assertTrue(limited.contains("too many connections for tenant"), limited); + assertThrows(HStoreException.InvalidSchema.class, () -> boss.execute("ALTER TENANT acme QUOTA (connections 10)")); + assertTrue(admin.execute("SHOW TENANTS").render().contains("acme")); + } + assertTrue(new Session(database).authenticate("helper", "helper-password")); + } + admin.execute("ALTER TENANT acme QUOTA (connections -1)"); + try (Session one = login("boss", "boss-password")) { + assertTrue(one.authenticated()); + assertTrue(login("helper", "helper-password").authenticated()); + assertTrue(login("helper", "helper-password").authenticated()); + } + assertThrows(HStoreException.InvalidSchema.class, () -> admin.execute("ALTER TENANT acme QUOTA (connections -5)")); + } + + @Test + void tenantAdminsCannotLiftLimitsOnAdmins() { + admin.executeAll(""" + CREATE TENANT acme; + CREATE USER boss PASSWORD 'boss-password' TENANT acme ROLE admin CONNECTION LIMIT 2; + CREATE USER peer PASSWORD 'peer-password' TENANT acme ROLE admin VALID UNTIL '2000-01-01'; + CREATE USER staff PASSWORD 'staff-password' TENANT acme ROLE writer; + """); + try (Session boss = login("boss", "boss-password")) { + assertThrows(HStoreException.InvalidSchema.class, () -> boss.execute("ALTER USER boss CONNECTION LIMIT -1")); + assertThrows(HStoreException.InvalidSchema.class, () -> boss.execute("ALTER USER peer VALID UNTIL 'infinity'")); + assertThrows(HStoreException.InvalidSchema.class, () -> boss.execute("ALTER USER peer ROLE writer")); + boss.execute("ALTER USER boss PASSWORD 'boss-password-2'"); + boss.execute("ALTER USER staff CONNECTION LIMIT 1 VALID UNTIL '2999-01-01' ROLE reader"); + } + } + + @Test + void sessionsEndedByAChangeStayEndedUntilTheySignIn() { + try (Session ops = login("ops", "ops-password")) { + admin.execute("ALTER USER ops ROLE writer"); + assertThrows(HStoreException.InvalidSchema.class, () -> ops.execute("WHOAMI")); + admin.execute("ALTER USER ops ROLE reader"); + assertThrows(HStoreException.InvalidSchema.class, () -> ops.execute("WHOAMI")); + assertEquals(0, database.connections("ops")); + assertTrue(ops.authenticate("ops", "ops-password")); + assertTrue(cell(ops.execute("WHOAMI")).equals("ops")); + assertEquals(1, database.connections("ops")); + } + } +} diff --git a/database/src/test/java/io/hstore/db/TenantBoundaryTest.java b/database/src/test/java/io/hstore/db/TenantBoundaryTest.java index b66f1f2..d6aaaa9 100644 --- a/database/src/test/java/io/hstore/db/TenantBoundaryTest.java +++ b/database/src/test/java/io/hstore/db/TenantBoundaryTest.java @@ -12,6 +12,7 @@ import java.nio.file.Path; import java.util.List; import java.util.Map; +import java.util.regex.Pattern; import java.util.stream.Stream; import static java.util.Map.entry; @@ -20,25 +21,28 @@ class TenantBoundaryTest { static final Map ENGINE_WIDE = Map.ofEntries( - entry("io/hstore/db/Reader.java", "defines engineView and the tenant-checked lookups built on it"), + entry("io/hstore/db/Reader.java", "defines engineView and the tenant-checked lookups built on it; reads grants from the latest main"), entry("io/hstore/db/Writer.java", "index backfills and embedding checks that run for every tenant or on shared structures"), entry("io/hstore/db/HypergraphDatabase.java", "users, tenants, quotas and the SCRAM secret, which are not tenant data"), entry("io/hstore/db/query/Session.java", "the session's own user and tenant records"), - entry("io/hstore/db/query/Executor.java", "user, tenant and quota administration and the shared schema listing"), + entry("io/hstore/db/query/Executor.java", "user, tenant, quota and grant administration and the shared schema listing"), entry("io/hstore/db/query/Planner.java", "property index estimates keyed by the reader's tenant"), - entry("io/hstore/db/evidence/Provenance.java", "qualifier and evidence records filtered by their stamped tenant"), - entry("io/hstore/db/signal/Signals.java", "signals of an atom that Reader.visible accepted"), - entry("io/hstore/db/temporal/StateBindings.java", "state of an atom that Reader.visible accepted"), + entry("io/hstore/db/evidence/Provenance.java", "qualifier and evidence records filtered by their stamped tenant, written after the writer's checks"), + entry("io/hstore/db/signal/Signals.java", "signals of an atom that Reader.visible or Writer.requireWritable accepted"), + entry("io/hstore/db/temporal/StateBindings.java", "state of an atom that Reader.visible or Writer.requireWritable accepted"), + entry("io/hstore/db/hora/SwapSampler.java", "SAMPLE writes a new branch, for global admins only"), entry("io/hstore/db/temporal/Temporal.java", "DIFF over whole generations, for global admins only"), entry("io/hstore/db/temporal/BranchMerge.java", "branch merges, for global admins only"), entry("io/hstore/db/view/MaterializedViews.java", "view maintenance, filtered by the view's tenant")); + static final Pattern RAW = Pattern.compile("engineView\\(\\)|\\.transaction\\(\\)|engine\\(\\)\\.(snapshot|read|write)\\("); + static List rawViewUsers(Path root) throws IOException { try (Stream files = Files.walk(root)) { return files.filter(file -> file.toString().endsWith(".java")) .filter(file -> { try { - return Files.readString(file).contains("engineView()"); + return RAW.matcher(Files.readString(file)).find(); } catch (IOException e) { throw new UncheckedIOException(e); } diff --git a/docs/database/hql.md b/docs/database/hql.md index 002d3e7..08ff459 100644 --- a/docs/database/hql.md +++ b/docs/database/hql.md @@ -732,18 +732,29 @@ DROP VIEW activity; -- dropped view activity create_tenant = "CREATE" "TENANT" identifier [ "QUOTA" quota ] ; (* ADMIN *) alter_tenant = "ALTER" "TENANT" identifier "QUOTA" quota ; (* ADMIN *) quota = "(" quota_key number { "," quota_key number } ")" ; -quota_key = "atoms" | "edges" | "payload_bytes" | "transaction_pages" | "query_pages" ; +quota_key = "atoms" | "edges" | "payload_bytes" | "transaction_pages" | "query_pages" | "connections" ; create_user = "CREATE" "USER" ( identifier | string ) "PASSWORD" string - [ "TENANT" identifier ] [ "ROLE" ( "admin" | "writer" | "reader" ) ] ; (* ADMIN; ROLE default writer *) -alter_user = "ALTER" "USER" ( identifier | string ) "PASSWORD" string ; (* ADMIN *) + [ "TENANT" identifier ] [ "ROLE" role ] [ valid_until ] [ connections ] ; (* ADMIN; ROLE default writer *) +alter_user = "ALTER" "USER" ( identifier | string ) + ( "PASSWORD" string | "ROLE" role | valid_until | connections ) { … } ; (* ADMIN; each clause at most once *) +role = "admin" | "writer" | "reader" | "none" ; +valid_until = "VALID" "UNTIL" ( instant | "'infinity'" ) ; +connections = "CONNECTION" "LIMIT" integer ; (* -1 = no limit, else 0-100000 *) drop_user = "DROP" "USER" ( identifier | string ) ; (* ADMIN *) +grant = "GRANT" ( ( "READ" | "WRITE" ) "ON" "TYPE" identifier | "MONITOR" | "MAINTAIN" ) + "TO" ( identifier | string ) ; (* ADMIN; MONITOR, MAINTAIN: global admin *) +revoke = "REVOKE" ( ( "READ" | "WRITE" ) "ON" "TYPE" identifier | "MONITOR" | "MAINTAIN" ) + "FROM" ( identifier | string ) ; (* ADMIN; MONITOR, MAINTAIN: global admin *) use_tenant = "USE" "TENANT" identifier ; authenticate = "AUTHENTICATE" ( identifier | string ) "PASSWORD" string ; show = "SHOW" ( "TENANTS" | "USERS" ) ; (* ADMIN *) +show_grants = "SHOW" "GRANTS" [ "FOR" ( identifier | string ) ] ; (* FOR another user: ADMIN *) ``` -`SHOW TENANTS` columns: `id, name, atoms, edges, payload bytes, max atoms, max edges`; `SHOW USERS`: `user, -tenant, role` (tenant id). The tenancy model is specified in [security](security.md). +`SHOW TENANTS` columns: `id, name, atoms, edges, payload bytes, max atoms, max edges, connections, max connections`; `SHOW USERS`: `user, +tenant, role, valid until, connection limit` (tenant id); `SHOW GRANTS`: `user, privilege, type`. The model +behind roles, grants, `VALID UNTIL` and `CONNECTION LIMIT` is specified in +[security](security.md#principals-and-roles). A password must be 8 to 1,024 characters long. In its place `CREATE USER` and `ALTER USER` accept a SCRAM-SHA-256 verifier (`hstore verifier` prints one), so the password never reaches the server. `AUTHENTICATE` works in @@ -757,6 +768,11 @@ ALTER TENANT clinic_b QUOTA (payload_bytes 1048576, query_pages 100000); CREATE USER analyst PASSWORD 's3cret-pass' TENANT clinic_b ROLE writer; CREATE USER auditor PASSWORD 'pw-secret' ROLE reader; ALTER USER auditor PASSWORD 'pw2-secret'; +CREATE USER clerk PASSWORD 'clerk-pass' TENANT clinic_b ROLE none VALID UNTIL '2027-01-01' CONNECTION LIMIT 2; +GRANT WRITE ON TYPE Lab TO clerk; +REVOKE WRITE ON TYPE Lab FROM clerk; -- clerk keeps READ +ALTER USER auditor ROLE writer CONNECTION LIMIT 5; +GRANT MONITOR TO auditor; -- STATS without being an admin DROP USER auditor; USE TENANT clinic_b; INSERT NODE Lab 'b1' {name: 'B1'}; INSERT NODE Lab 'b2' {name: 'B2'}; INSERT NODE Lab 'b3' {name: 'B3'}; diff --git a/docs/database/security.md b/docs/database/security.md index ec992e9..e5891d4 100644 --- a/docs/database/security.md +++ b/docs/database/security.md @@ -9,7 +9,7 @@ in [wire protocol](../operations/wire-protocol.md) and [Studio](../operations/st ```java record Principal(String user, int tenant, Role role, int home) -enum Role { ADMIN, WRITER, READER } +enum Role { ADMIN, WRITER, READER, NONE } ``` `tenant` is the tenant a session currently works in and `home` is the tenant the user belongs to. They only @@ -17,16 +17,108 @@ differ after an admin of the default tenant runs `USE TENANT`. An ADMIN whose ho **global admin** (`Principal.global()`); an ADMIN of any other tenant is a **tenant admin** and never leaves its tenant. -| Capability | global admin | tenant admin | WRITER | READER | -|---|:---:|:---:|:---:|:---:| -| read atoms of its tenant | yes | yes | yes | yes | -| insert, update, delete atoms, members, properties, embeddings, evidence, state, signals | yes | yes | yes | no | -| create, alter and drop users of its own tenant; `SHOW USERS`, `SHOW TENANTS` (own tenant only) | yes | yes | no | no | -| create, refresh, drop and read materialized views (`SHOW VIEWS` lists names for every role) | yes | yes | no | no | -| users of other tenants, `CREATE/ALTER TENANT` | yes | no | no | no | -| DDL (`CREATE … TYPE`, `CREATE INDEX`, `CREATE JSON INDEX`); the schema is shared by every tenant | yes | no | no | no | -| branches (`USE BRANCH`, `SHOW BRANCHES`, `CREATE/DROP/MERGE BRANCH`, `SAMPLE`, `DIFF`), `HISTORY`, `STATS`, `CHECKPOINT`, `CHECKPOINT AT`, `COMPACT` | yes | no | no | no | -| switch tenant (`USE TENANT`, `Principal.inTenant`) | yes | no | no | no | +| Capability | global admin | tenant admin | WRITER | READER | NONE | +|---|:---:|:---:|:---:|:---:|:---:| +| read atoms of its tenant | yes | yes | yes | yes | types with a READ grant | +| insert, update, delete atoms, members, properties, embeddings, state, signals | yes | yes | yes | no | types with a WRITE grant | +| record evidence and assertions (`EVIDENCE`, `QUALIFY`) | yes | yes | yes | no | no | +| create, alter and drop users of its own tenant, grant and revoke type privileges to them; `SHOW USERS`, `SHOW TENANTS` (own tenant only), `SHOW GRANTS FOR` | yes | yes | no | no | no | +| create, refresh, drop and read materialized views (`SHOW VIEWS` lists names for every role) | yes | yes | no | no | no | +| users of other tenants, `CREATE/ALTER TENANT`, `GRANT MONITOR`, `GRANT MAINTAIN` | yes | no | no | no | no | +| DDL (`CREATE … TYPE`, `CREATE INDEX`, `CREATE JSON INDEX`); the schema is shared by every tenant | yes | no | no | no | no | +| branches (`USE BRANCH`, `SHOW BRANCHES`, `CREATE/DROP/MERGE BRANCH`, `SAMPLE`, `DIFF`), `HISTORY` | yes | no | no | no | no | +| `STATS` and Studio's dashboard | yes | no | with `MONITOR` (default tenant) | with `MONITOR` (default tenant) | with `MONITOR` (default tenant) | +| `CHECKPOINT`, `CHECKPOINT AT`, `COMPACT` | yes | no | with `MAINTAIN` (default tenant) | with `MAINTAIN` (default tenant) | with `MAINTAIN` (default tenant) | +| switch tenant (`USE TENANT`, `Principal.inTenant`) | yes | no | no | no | no | + +### Privileges on types + +READER, WRITER and ADMIN cover every type of their tenant, like PostgreSQL's `pg_read_all_data` and +`pg_write_all_data`. A user with `ROLE none` starts with no access to data and receives it per type: + +- `GRANT READ ON TYPE T TO u` lets `u` see atoms of `T`; `GRANT WRITE ON TYPE T TO u` also lets it create them and + change their properties, members, documents, embeddings, state and signals. `WRITE` includes `READ`. + `REVOKE WRITE` leaves `READ`; `REVOKE READ` removes both. Grants on types are refused for users whose role + already covers every type. +- An atom of a type the user cannot read is hidden wherever an atom of another tenant is hidden, with the one + exception in the next point: `Reader.visible`, `atom`, `require` and every lookup built on them (see the + isolation model below) check the type as well as the tenant, so `DESCRIBE`, properties, its own incidences, + degrees, matches and semantic hits leave it out. Naming the type + in `MATCH`, `RANK`, `PATTERN` or a HORA statement fails with `no READ privilege on type T`, as `SELECT` on a table + without the privilege fails in PostgreSQL. +- A hyperedge the user can read shows all its members, including members of types it cannot read: their ids and + what the edge itself records about each membership (role, weight, validity, qualifier), the way a foreign-key + column shows the key of a row in a table you cannot select from. An atom of another tenant never appears there. + Those members have no label, properties or incidences of their own for the user, and adding one to an edge + fails as if it did not exist. Keep such data in edge types the user cannot read when even the ids must stay + hidden. +- Writing to a hyperedge needs `WRITE` on the edge's type; the members added must be readable, also for set + algebra `INTO`. Deleting an atom also removes it from every hyperedge that contains it, so a user with + `ROLE none` may only delete an atom when it can write every hyperedge of its tenant that holds it. +- Referring to an atom by key (`@T:'key'`) needs `READ` on `T`; without it the reference fails with the + privilege error whether or not the key exists, so keys of unreadable types cannot be probed. +- Evidence and assertions (`TRACE`, `EVIDENCE`, `QUALIFY` policies) belong to the tenant, not to a type, so a user + with `ROLE none` neither sees nor records them. +- `EXPLAIN` for a user with `ROLE none` estimates property-index ranges as a tenth of the type's readable atoms + instead of counting the index, whose entries span every type, and neither uses nor feeds the tenant's + cardinality feedback. +- Grants are kept in slot `GRANTS` = 75 (schema 110), keyed by user and type. `Reader` reads them from the latest + committed generation of main, not from the statement's snapshot, so a grant or revoke applies to the next + statement of every open session, inside an open transaction too, and `AT GENERATION` or `AS OF` cannot bring + back a revoked privilege. As in PostgreSQL, statements that already ran keep their effect. `DROP USER` + removes the user's grants, so a user later created with the same name starts without them. + +### Capabilities + +`GRANT MONITOR TO u` allows `STATS` and Studio's dashboard; `GRANT MAINTAIN TO u` allows `CHECKPOINT`, +`CHECKPOINT AT` and `COMPACT`. They resemble PostgreSQL's `pg_monitor` and `pg_checkpoint`/`pg_maintain`: an +operator can watch and maintain the engine without being a global admin. Only global admins grant them, and only +to users of the default tenant, since the statements report on or act on every tenant. `HISTORY`, branches and +DDL stay with global admins. + +### Password expiry and connection limits + +- `VALID UNTIL ''` on `CREATE USER` or `ALTER USER` makes the password stop working at that instant; + `VALID UNTIL 'infinity'` removes the limit. As in PostgreSQL it expires the password, not the account: open + sessions continue. A sign-in with an expired password fails with the usual `authentication failed`, over SCRAM + only at the last step, and the server logs `user "u" tried to sign in after its password expired`. +- `CONNECTION LIMIT n` caps the user's signed-in sessions (wire connections, Studio sessions and embedded sessions + that authenticated) at `n`; `-1` means no limit. The next sign-in fails with `ABORTED_RESOURCE_LIMIT: too many + connections for user u`. Global admins are exempt, as PostgreSQL exempts superusers. A session that signs in + again gives back its old count before it takes the new one, so signing in again as the same user works at + `CONNECTION LIMIT 1`, and a user dropped and created in another tenant is counted there. A closed session runs + no further statement. A Studio session gives its count back when it closes, after + `studio_session_minutes` without requests, or after 12 hours, whichever comes first; a background sweep notices + idle sessions even when nobody uses Studio. +- A per-user limit binds that user only. A tenant admin can create users, including other admins of its tenant, + so to cap a whole tenant use the tenant's `connections` quota: `CREATE TENANT t QUOTA (connections 20)` or + `ALTER TENANT t QUOTA (connections 20)` (`-1` removes it; like every quota key, `connections` ignores case). It counts every signed-in session of the tenant's users + and fails the next sign-in with `too many connections for tenant #id`. Only global admins change quotas. +- User options live in slot `USER_OPTIONS` = 76 (schema 111) and tenant limits in `TENANT_OPTIONS` = 77 (schema + 112); a user or tenant without an entry has no expiry and no limit. `CREATE USER` clears any entry left under + its name, and `GRANT`, `REVOKE` and `ALTER USER` place a commit-time precondition on the user's record, so a + grant or option that commits after a concurrent `DROP USER` fails. One that commits just before the `DROP USER` + can be left without a user, since the drop only removes the grants it saw; such a row is never shown by `SHOW + GRANTS` and never reaches a later user of that name, because `CREATE USER` clears it. + +### Changing roles + +`ALTER USER u ROLE r` changes the role; `u`'s open sessions then fail with "authenticate again" until they sign in +again, so the new role applies at once. Only a global admin may change the role, password expiry or connection +limit of an ADMIN, or the password of an ADMIN other than itself, so a tenant admin cannot lift limits put on +itself or take over another admin of its tenant. + +Once the default tenant has an ADMIN, it must keep one whose password never expires while any user exists. Every +`CREATE USER`, `ALTER USER` and `DROP USER` checks this against the state it leaves behind and is refused if none +would remain: dropping or demoting the last one, giving its password any `VALID UNTIL` other than `'infinity'`, +creating the first global admin with an expiry, or dropping the last admin and creating other users in the same +transaction. An embedded application may still create tenant users before any global admin exists. Any expiry date eventually passes, +so checking only that a password still works today would let a date one second ahead lock everyone out. The +statement also places a commit-time precondition on the role record and the options record of every remaining +such admin, so concurrent transactions that each remove or expire a different admin, or that create a user while +another drops the last admin, cannot all commit, whichever records they write. Dropping the last user is allowed +for a global admin and turns authentication off again under `authentication = auto`. Should every admin still be lost (for example to a forgotten password), `hstore shell` on +the data directory runs as the built-in `system` admin, like PostgreSQL's single-user mode. `USE MAIN` is open to everyone. Without `TENANT`, `CREATE USER` puts the new user in the session's current tenant. A tenant admin who names another tenant, or another tenant's user, gets the same error as for a name that @@ -90,8 +182,8 @@ The raw engine `View` is available as `reader.engineView()` for code that works tenant and quota records, the SCRAM secret, the shared schema listing, branch merges and `DIFF` (global admins only), materialized view maintenance (filtered by the view's tenant), and records that carry their own tenant stamp (evidence, qualifiers, state, signals). `TenantBoundaryTest` in the database and server modules lists the -files allowed to call it, each with its reason, and fails when any other file does, or when a listed file no -longer needs it. Adding a raw read therefore means stating why it is safe in that list. +files allowed to call it, or to reach data around `Reader` through `writer.transaction()` or engine snapshots, each +with its reason, and fails when any other file does, or when a listed file no longer needs it. Adding a raw read therefore means stating why it is safe in that list. Writes call `require`, so a principal cannot modify an atom it cannot see. A hyperedge may only gain members that are visible to the writer (`Writer.incidence`, also used by `Writer.qualify`), so a tenant cannot reference another @@ -115,7 +207,7 @@ Shared, non-tenant-scoped structures: | dictionary | interned strings | ids only; property, state schema and model names are limited to 128 characters, and only a global admin can create an embedding model | | HNSW indexes | one per model | results filtered by `Reader.visible` ([semantic](semantic.md#tenant-visibility)); vectors must match the model's length, and a row the index cannot take is logged and skipped ([semantic](semantic.md#embedding-records)) | | payload store | append-only byte store | payloads are reachable only through tenant-checked properties and embeddings | -| branches, history, statistics | engine-level | global admins only (`USE BRANCH`, `SHOW BRANCHES`, `MERGE`, `STATS`, `HISTORY`, `DIFF`; Studio `/api/stats` and `/api/history`) | +| branches, history, statistics | engine-level | global admins only (`USE BRANCH`, `SHOW BRANCHES`, `MERGE`, `HISTORY`, `DIFF`; Studio `/api/history`); `STATS` and Studio `/api/stats` also for users with `MONITOR`, `CHECKPOINT` and `COMPACT` also for users with `MAINTAIN` | ```sql CREATE TENANT clinic_b; diff --git a/docs/operations/logging.md b/docs/operations/logging.md index 378d2a3..0335534 100644 --- a/docs/operations/logging.md +++ b/docs/operations/logging.md @@ -144,6 +144,7 @@ and in Docker it is the last line of `docker logs`. The storage-integrity failur | WARNING | `user "{user}" failed to sign in 50 times within 15 minutes; someone may be guessing its password from several addresses` (logger `hstore.security`). | | WARNING | `user "{user}" has a password stored by HStore 0.1, which cannot be used with SCRAM-SHA-256; it can sign in once through Studio to convert it, or an administrator can set a new one with ALTER USER` (logger `hstore.security`, at most once every 10 minutes per user): the client only sees `authentication failed`. | | INFO | `the password of user "{user}" is now stored as a SCRAM-SHA-256 verifier` (logger `hstore.security`): a user with a password from HStore 0.1 signed in through Studio or an `AUTHENTICATE` statement. | +| WARNING | `user "{user}" tried to sign in after its password expired` (logger `hstore.security`; see `VALID UNTIL` in [security](../database/security.md#password-expiry-and-connection-limits)). | | WARNING | at startup, `this database was written by a version that did not keep hyperedge members inside their tenant; …`: run `hstore check` and `hstore check --repair-tenants` ([cli](cli.md)). | | WARNING | at startup, `authentication = off and the server listens on {address}: …` or `the database has no users, so connections from other hosts are refused until CREATE USER creates one`, when the listen address is not loopback. | | WARNING | at startup, `{n} users have passwords stored by HStore 0.1 and cannot sign in over this port until each signs in once through Studio or gets a new password with ALTER USER: {names}`. | diff --git a/docs/operations/studio.md b/docs/operations/studio.md index db525a7..7ab647d 100644 --- a/docs/operations/studio.md +++ b/docs/operations/studio.md @@ -34,7 +34,7 @@ flowchart LR | **Console** | HQL editor with syntax highlighting, line numbers, completion of keywords, type, property and role names, and persistent history. Results appear as a stream of frames, newest first. Each frame offers *Graph* (when the result contains atoms), *Table*, *JSON* and *Plan* (when the statement produced a trace) tabs. Multi-statement scripts produce one frame per statement. A failed statement gets a red frame with the error code and stops the script. | | **Explore** | Full-screen hypergraph canvas. You can load a sample of every hyperedge type, search (`42`, `@42`, `Type:key`, `Type:'key'` or a type name), expand atoms, hide types from the legend, switch render modes and toggle labels. An inspector panel shows the selected atom. A generation slider re-renders the same atoms at any retained generation. | | **Schema** | Cards for every node and hyperedge type: kind, visible atom count, properties with `indexed`/`required` badges and value type, JSON indexes, and roles. *Explore* and *Query* buttons on each card, plus a branch table. | -| **Dashboard** | Global admins only (ADMIN in the default tenant). Polls `/api/stats` every 2 s. Metric tiles show generation, commits, conflicts, group-commit batch size, cache hit rate, data written, WAL and uptime. Live charts show commits/s, pages written/s, pages read/s and WAL throughput. A segment map is coloured by state (active, sealed, compacting, retired) and filled by bytes used relative to the largest segment size, so the bars also show how segments grew. Hover a segment for its bytes, node images and live images. Configuration and recovery details are listed. | +| **Dashboard** | Global admins (ADMIN in the default tenant) and users granted `MONITOR`. Polls `/api/stats` every 2 s. Metric tiles show generation, commits, conflicts, group-commit batch size, cache hit rate, data written, WAL and uptime. Live charts show commits/s, pages written/s, pages read/s and WAL throughput. A segment map is coloured by state (active, sealed, compacting, retired) and filled by bytes used relative to the largest segment size, so the bars also show how segments grew. Hover a segment for its bytes, node images and live images. Configuration and recovery details are listed. | | **Access** | ADMIN only. Tenants (usage and quotas) and users, forms to create users and tenants, and *Drop* for users. Every action runs ordinary HQL (`CREATE USER …`, `CREATE TENANT … QUOTA (…)`, `DROP USER …`) through the session. | The top bar shows the current branch, user, tenant, role and the latest durable generation (polled every @@ -148,7 +148,7 @@ Errors use one JSON shape: | `GET /api/graph` | yes | Hypergraph neighbourhood for visualisation; see below. | | `GET /api/schema` | yes | Types visible to the session: `id`, `name`, `kind`, `version`, `count`, `roles`, `properties[{name,type,indexed,required}]`, `jsonIndexes[{path,type}]`, plus `branches` (empty unless the session belongs to a global admin). | | `GET /api/history` | yes | `{"current": G, "generations": [{"id","wallTime","txnId"}]}` for the retained generations. Users other than global admins only get `id`, since the wall times and transaction ids describe every tenant's commits. | -| `GET /api/stats` | global ADMIN | Engine counters, segments (`id`, `state`, `pages` = node images, `bytes`, `live`), recovery outcome, configuration (`pageSize`, `segmentBytes` (the largest segment), `durability`, `walMode`, `nodeCacheBytes`, `historyLimit`, `checkpointWalBytes` = the threshold in use, `checkpointSource` = `automatic`, `database` or `configuration`), semantic index generation, Studio session count, branches. | +| `GET /api/stats` | global ADMIN or `MONITOR` | Engine counters, segments (`id`, `state`, `pages` = node images, `bytes`, `live`), recovery outcome, configuration (`pageSize`, `segmentBytes` (the largest segment), `durability`, `walMode`, `nodeCacheBytes`, `historyLimit`, `checkpointWalBytes` = the threshold in use, `checkpointSource` = `automatic`, `database` or `configuration`), semantic index generation, Studio session count, branches. | Example `GET /api/info`: diff --git a/docs/operations/wire-protocol.md b/docs/operations/wire-protocol.md index b8fc975..81b3b98 100644 --- a/docs/operations/wire-protocol.md +++ b/docs/operations/wire-protocol.md @@ -179,7 +179,7 @@ bytes` and the connection is closed. A connection whose first request starts lik without a reply, so a web page cannot use a browser to post HQL to the port. If a user is dropped or its password changes, the next statement on each of its open connections fails with -`user NAME was dropped or its password changed; authenticate again`, and so does every statement after it until the +`user NAME was dropped, or its password or role changed; authenticate again`, and so does every statement after it until the connection signs in again with a SASL exchange. An exchange that started before the change fails at its last step, since the server only accepts it while the user's record is the one it took the verifier from. diff --git a/engine/src/main/java/io/hstore/engine/txn/View.java b/engine/src/main/java/io/hstore/engine/txn/View.java index 6f788b6..275ccae 100644 --- a/engine/src/main/java/io/hstore/engine/txn/View.java +++ b/engine/src/main/java/io/hstore/engine/txn/View.java @@ -108,14 +108,14 @@ public Optional incidence(long edge, long member) { : Optional.empty()); } - public Optional incidence(long edge, long member, int tenant) { - return atom(edge).flatMap(record -> record instanceof EdgeRecord found && found.tenant() == tenant + public Optional incidence(long edge, long member, Predicate admit) { + return atom(edge).flatMap(record -> record instanceof EdgeRecord found && admit.test(found) ? hyperedge(edge, found, false).get(member) : Optional.empty()); } - public boolean contains(long edge, long member, int tenant) { - return incidence(edge, member, tenant).isPresent(); + public boolean contains(long edge, long member, Predicate admit) { + return incidence(edge, member, admit).isPresent(); } public long cardinality(long edge) { diff --git a/server/src/main/java/io/hstore/server/StatementLogger.java b/server/src/main/java/io/hstore/server/StatementLogger.java index d659e73..c96873f 100644 --- a/server/src/main/java/io/hstore/server/StatementLogger.java +++ b/server/src/main/java/io/hstore/server/StatementLogger.java @@ -50,22 +50,24 @@ private static Category category(Statement statement) { return switch (statement) { case Statement.CreateType _, Statement.CreateIndex _, Statement.CreateJsonIndex _, Statement.CreateView _, Statement.CreateBranch _, Statement.DropBranch _, Statement.CreateTenant _, Statement.AlterTenant _, - Statement.CreateUser _, Statement.AlterUser _, Statement.DropUser _, Statement.CheckpointAt _ -> Category.DDL; + Statement.CreateUser _, Statement.AlterUser _, Statement.DropUser _, Statement.Grant _, Statement.Revoke _, Statement.CheckpointAt _ -> Category.DDL; case Statement.Query _, Statement.Explain _, Statement.Rank _, Statement.Members _, Statement.IncidentTo _, Statement.Describe _, Statement.Gather _, Statement.Reduce _, Statement.Scatter _, Statement.Propagate _, Statement.Neighbors _, Statement.OverlapJoin _, Statement.Closure _, Statement.PatternQuery _, Statement.Expand _, Statement.History _, Statement.Stats _, Statement.ShowTypes _, Statement.ShowBranches _, Statement.ShowViews _, Statement.ShowView _, Statement.ShowTenants _, Statement.ShowUsers _, Statement.WhoAmI _, Statement.DiffBranches _, - Statement.DiffGenerations _, Statement.ResolveSignals _, Statement.Trace _ -> Category.READ; + Statement.DiffGenerations _, Statement.ResolveSignals _, Statement.Trace _, Statement.ShowGrants _ -> Category.READ; default -> Category.MOD; }; } static String describe(Statement statement, String script) { return switch (statement) { - case Statement.CreateUser(String name, String _, Optional tenant, Role role) -> + case Statement.CreateUser(String name, String _, Optional tenant, Role role, var _, var _) -> "CREATE USER " + name + " PASSWORD '***'" + tenant.map(found -> " TENANT " + found).orElse("") + " ROLE " + role; - case Statement.AlterUser(String name, String _) -> "ALTER USER " + name + " PASSWORD '***'"; + case Statement.AlterUser(String name, Optional password, var role, var _, var _) -> "ALTER USER " + name + + password.map(_ -> " PASSWORD '***'").orElse("") + role.map(found -> " ROLE " + found).orElse("") + + (password.isEmpty() && role.isEmpty() ? " (options)" : ""); case Statement.Authenticate(String user, String _) -> "AUTHENTICATE " + user + " PASSWORD '***'"; default -> abbreviate(script); }; diff --git a/server/src/main/java/io/hstore/server/studio/Sessions.java b/server/src/main/java/io/hstore/server/studio/Sessions.java index 8c6665f..7fc9347 100644 --- a/server/src/main/java/io/hstore/server/studio/Sessions.java +++ b/server/src/main/java/io/hstore/server/studio/Sessions.java @@ -11,6 +11,9 @@ import java.util.Map; import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantLock; import java.util.function.Function; @@ -47,15 +50,20 @@ T exclusively(Function work) { } } + private static final System.Logger LOG = System.getLogger("hstore.studio"); private static final SecureRandom RANDOM = new SecureRandom(); private static final long LIFETIME_NANOS = Duration.ofHours(12).toNanos(); private static final int MAX_SESSIONS = 4096; private final Map entries = new ConcurrentHashMap<>(); private final long idleNanos; + private final ScheduledExecutorService sweeper = Executors.newSingleThreadScheduledExecutor( + Thread.ofVirtual().name("studio-session-sweeper").factory()); Sessions(Duration idle) { this.idleNanos = idle.toNanos(); + long period = Math.clamp(idle.toMillis() / 4, 10, Duration.ofMinutes(1).toMillis()); + sweeper.scheduleWithFixedDelay(this::sweep, period, period, TimeUnit.MILLISECONDS); } Entry open(Session session) { @@ -80,17 +88,31 @@ Optional find(String token) { } void close(String token) { - Optional.ofNullable(entries.remove(token)).ifPresent(entry -> entry.exclusively(session -> { - session.close(); - return null; - })); + Optional.ofNullable(entries.remove(token)).ifPresent(entry -> { + try { + entry.exclusively(session -> { + session.close(); + return null; + }); + } catch (RuntimeException failure) { + LOG.log(System.Logger.Level.WARNING, "closing a studio session failed", failure); + } + }); } int size() { return entries.size(); } - private void expire() { + private void sweep() { + try { + expire(); + } catch (RuntimeException failure) { + LOG.log(System.Logger.Level.WARNING, "closing idle studio sessions failed; the next sweep will try again", failure); + } + } + + void expire() { long now = System.nanoTime(); entries.values().stream() .filter(entry -> (now - entry.lastUsed > idleNanos || now - entry.opened > LIFETIME_NANOS) && !entry.lock.isLocked()) @@ -101,6 +123,7 @@ private void expire() { @Override public void close() { + sweeper.shutdown(); entries.keySet().stream().toList().forEach(this::close); } } diff --git a/server/src/main/java/io/hstore/server/studio/Studio.java b/server/src/main/java/io/hstore/server/studio/Studio.java index 01b264c..ed087aa 100644 --- a/server/src/main/java/io/hstore/server/studio/Studio.java +++ b/server/src/main/java/io/hstore/server/studio/Studio.java @@ -10,6 +10,7 @@ import io.hstore.db.query.QueryResult; import io.hstore.db.query.Session; import io.hstore.db.schema.TypeDef; +import io.hstore.db.security.Access; import io.hstore.db.security.Principal; import io.hstore.db.security.Security; import io.hstore.db.value.Json; @@ -309,6 +310,7 @@ private Response info(Request request) { private Response login(Request request) { request.cookie(COOKIE).ifPresent(sessions::close); + sessions.expire(); Json body = request.json(); Optional user = field(body, "user"); Session session = new Session(database); @@ -331,7 +333,13 @@ private Response login(Request request) { session.close(); throw HttpFailure.unauthorized("user and password are required"); } - Sessions.Entry entry = sessions.open(session); + Sessions.Entry entry; + try { + entry = sessions.open(session); + } catch (RuntimeException full) { + session.close(); + throw full; + } LOG.log(System.Logger.Level.INFO, "studio session opened for user={0} role={1} from {2}", session.principal().user(), session.principal().role(), request.peer()); boolean secure = options.tls().isPresent() || request.headers().getOrDefault("X-Forwarded-Proto", List.of()).contains("https"); @@ -484,8 +492,8 @@ private Response history(Request request) { private Response stats(Request request) { Session session = session(request).session(); - if (!session.principal().global()) { - throw HttpFailure.forbidden("the dashboard requires the ADMIN role in the default tenant"); + if (!database.holds(session.principal(), Access.Privilege.MONITOR)) { + throw HttpFailure.forbidden("the dashboard requires the MONITOR privilege or the ADMIN role in the default tenant"); } EngineStats engine = database.engine().stats(); TransactionManager.Statistics transactions = database.engine().transactions().statistics(); diff --git a/server/src/test/java/io/hstore/server/ServerTest.java b/server/src/test/java/io/hstore/server/ServerTest.java index fc579a7..d5c8613 100644 --- a/server/src/test/java/io/hstore/server/ServerTest.java +++ b/server/src/test/java/io/hstore/server/ServerTest.java @@ -11,6 +11,7 @@ import io.hstore.db.security.Role; import io.hstore.engine.Dictionary; import io.hstore.engine.EngineOptions; +import io.hstore.engine.catalog.Branch; import io.hstore.engine.StorageEngine; import io.hstore.engine.catalog.Branch; import io.hstore.engine.topology.Incidence; diff --git a/server/src/test/java/io/hstore/server/TenantBoundaryTest.java b/server/src/test/java/io/hstore/server/TenantBoundaryTest.java index 8de6389..d7341ca 100644 --- a/server/src/test/java/io/hstore/server/TenantBoundaryTest.java +++ b/server/src/test/java/io/hstore/server/TenantBoundaryTest.java @@ -12,6 +12,7 @@ import java.nio.file.Path; import java.util.List; import java.util.Map; +import java.util.regex.Pattern; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -20,7 +21,9 @@ class TenantBoundaryTest { private static final Map ENGINE_WIDE = Map.of( "io/hstore/server/studio/Studio.java", "the shared schema listing and the session's tenant name", - "io/hstore/server/studio/Neighborhood.java", "the shared schema listing"); + "io/hstore/server/studio/Neighborhood.java", "the shared schema listing", + "io/hstore/server/Main.java", "hstore init creates the first superuser before any session exists"); + private static final Pattern RAW = Pattern.compile("engineView\\(\\)|\\.transaction\\(\\)|engine\\(\\)\\.(snapshot|read|write)\\("); @Test void onlyEngineWideCodeReadsTheRawView() throws IOException { @@ -30,7 +33,7 @@ void onlyEngineWideCodeReadsTheRawView() throws IOException { users = files.filter(file -> file.toString().endsWith(".java")) .filter(file -> { try { - return Files.readString(file).contains("engineView()"); + return RAW.matcher(Files.readString(file)).find(); } catch (IOException e) { throw new UncheckedIOException(e); } diff --git a/server/src/test/java/io/hstore/server/studio/SessionsTest.java b/server/src/test/java/io/hstore/server/studio/SessionsTest.java new file mode 100644 index 0000000..79b09cc --- /dev/null +++ b/server/src/test/java/io/hstore/server/studio/SessionsTest.java @@ -0,0 +1,49 @@ +// SPDX-FileCopyrightText: MetaCognition India Pvt Limited +// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 + +package io.hstore.server.studio; + +import io.hstore.db.DatabaseOptions; +import io.hstore.db.HypergraphDatabase; +import io.hstore.db.query.Session; +import io.hstore.engine.EngineOptions; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.nio.file.Path; +import java.time.Duration; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; + +class SessionsTest { + + @TempDir + Path directory; + + @Test + void idleSessionsGiveBackTheirConnectionsWithoutAnyRequest() { + try (HypergraphDatabase database = HypergraphDatabase.open(directory, DatabaseOptions.defaults().withEngine(EngineOptions.defaults().withPageSize(4096))); + Sessions sessions = new Sessions(Duration.ofMillis(200))) { + try (Session setup = new Session(database)) { + setup.execute("CREATE USER admin PASSWORD 'admin-password' ROLE admin"); + setup.execute("CREATE USER ops PASSWORD 'ops-password' ROLE reader CONNECTION LIMIT 1"); + } + Session ops = new Session(database); + assertTrue(ops.authenticate("ops", "ops-password")); + sessions.open(ops); + assertEquals(1, database.connections("ops")); + assertTimeoutPreemptively(Duration.ofSeconds(10), () -> { + while (database.connections("ops") > 0) { + Thread.onSpinWait(); + } + }); + assertEquals(0, sessions.size()); + try (Session again = new Session(database)) { + assertTrue(again.authenticate("ops", "ops-password")); + } + } + } +} diff --git a/server/src/test/java/io/hstore/server/studio/StudioTest.java b/server/src/test/java/io/hstore/server/studio/StudioTest.java index e09605a..c12aa7b 100644 --- a/server/src/test/java/io/hstore/server/studio/StudioTest.java +++ b/server/src/test/java/io/hstore/server/studio/StudioTest.java @@ -140,6 +140,22 @@ void repeatedFailedLoginsAreThrottled() throws Exception { assertEquals(200, post("/api/login", "{\"user\":\"bob\",\"password\":\"b0b-pass\"}", Optional.empty(), true).statusCode()); } + @Test + void theDashboardOpensToMonitors() throws Exception { + try (Session session = new Session(database)) { + session.execute("CREATE USER watcher PASSWORD 'watcher-pass' ROLE reader"); + } + HttpResponse signedIn = post("/api/login", "{\"user\":\"watcher\",\"password\":\"watcher-pass\"}", Optional.empty(), true); + Optional cookie = signedIn.headers().firstValue("Set-Cookie").map(header -> header.substring(0, header.indexOf(';'))); + assertEquals(403, get("/api/stats", cookie).statusCode()); + try (Session session = new Session(database)) { + session.execute("GRANT MONITOR TO watcher"); + } + assertEquals(200, get("/api/stats", cookie).statusCode()); + List generations = ((Json.Array) object(get("/api/history", cookie).body()).get("generations")).items(); + assertTrue(generations.stream().allMatch(generation -> ((Json.Obj) generation).fields().keySet().equals(Set.of("id")))); + } + @Test void studioServesHttpsWithSecureCookies() throws Exception { Path certificate = Path.of(StudioTest.class.getResource("/tls/certificate.pem").toURI());