From 51696c56ce5f5826a2c941cdb11b687bc3533bab Mon Sep 17 00:00:00 2001 From: venkat1701 Date: Sun, 25 Oct 2026 22:20:57 +0530 Subject: [PATCH 1/5] feat(security): grants on types, capabilities, password expiry and connection limits - ROLE none: no data access until GRANT READ|WRITE ON TYPE. An atom of a type the user cannot read is invisible exactly like another tenant's, through the same Reader checks; naming such a type in MATCH, RANK, PATTERN or HORA fails with no READ privilege. Writes need WRITE on the changed atom's type. - GRANT MONITOR (STATS, Studio dashboard) and GRANT MAINTAIN (CHECKPOINT, CHECKPOINT AT, COMPACT) let default-tenant users operate the engine without being global admins. - ALTER USER ... ROLE, VALID UNTIL and CONNECTION LIMIT, in any combination with PASSWORD. Expired passwords fail like wrong ones; connection limits count signed-in sessions and spare global admins. - The last admin of the default tenant cannot change its role or be dropped while other users remain. - SHOW GRANTS, and SHOW USERS lists expiry and connection limit. - Grants and user options live in their own slots (75, 76), so existing user records are untouched. --- .../java/io/hstore/db/HypergraphDatabase.java | 31 ++- .../src/main/java/io/hstore/db/Reader.java | 55 +++- .../src/main/java/io/hstore/db/Writer.java | 30 ++- .../io/hstore/db/evidence/Provenance.java | 11 +- .../io/hstore/db/hora/PatternMatcher.java | 2 + .../src/main/java/io/hstore/db/query/Ast.java | 16 +- .../java/io/hstore/db/query/Executor.java | 133 +++++++-- .../main/java/io/hstore/db/query/Parser.java | 74 ++++- .../main/java/io/hstore/db/query/Planner.java | 2 +- .../main/java/io/hstore/db/query/Session.java | 9 +- .../java/io/hstore/db/security/Access.java | 159 +++++++++++ .../main/java/io/hstore/db/security/Role.java | 11 +- .../java/io/hstore/db/security/Security.java | 23 +- .../java/io/hstore/db/signal/Signals.java | 2 +- .../io/hstore/db/temporal/StateBindings.java | 2 +- .../java/io/hstore/db/PermissionsTest.java | 252 ++++++++++++++++++ docs/database/hql.md | 22 +- docs/database/security.md | 71 ++++- docs/operations/logging.md | 1 + docs/operations/studio.md | 4 +- .../main/java/io/hstore/engine/txn/View.java | 8 +- .../io/hstore/server/StatementLogger.java | 10 +- .../java/io/hstore/server/studio/Studio.java | 5 +- .../io/hstore/server/studio/StudioTest.java | 16 ++ 24 files changed, 868 insertions(+), 81 deletions(-) create mode 100644 database/src/main/java/io/hstore/db/security/Access.java create mode 100644 database/src/test/java/io/hstore/db/PermissionsTest.java diff --git a/database/src/main/java/io/hstore/db/HypergraphDatabase.java b/database/src/main/java/io/hstore/db/HypergraphDatabase.java index f803454..0d66831 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; @@ -33,7 +34,9 @@ import java.nio.file.Path; import java.time.Duration; import java.util.List; +import java.util.Map; import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Function; @@ -51,6 +54,7 @@ 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 byte[] scramSecret; private HypergraphDatabase(Path directory, DatabaseOptions options) { @@ -95,7 +99,8 @@ public List repairTenantMemberships() { } public Scram.Server scram(Optional channelBinding) { - return new Scram.Server(name -> read(reader -> Security.user(reader.engineView(), name)).map(Security::verifier), scramSecret, + return new Scram.Server(name -> read(reader -> Security.user(reader.engineView(), name) + .filter(user -> !Security.expired(reader.engineView(), user.name()))).map(Security::verifier), scramSecret, Security.ITERATIONS, channelBinding); } @@ -103,6 +108,30 @@ public SignIns signIns() { return signIns; } + public void enter(Principal principal) { + Access.Options limits = read(reader -> Access.options(reader.engineView(), principal.user())); + connections.compute(principal.user(), (user, count) -> { + int current = count == null ? 0 : count; + if (limits.connectionLimit() >= 0 && !principal.global() && current >= limits.connectionLimit()) { + throw HStoreException.limit("too many connections for user " + user + " (connection limit " + limits.connectionLimit() + ")"); + } + return current + 1; + }); + } + + 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 leave(String user) { + connections.computeIfPresent(user, (_, count) -> count <= 1 ? null : count - 1); + } + + 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 944929f..c8132a4 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; @@ -29,6 +30,7 @@ import io.hstore.engine.txn.IncidentEdge; import io.hstore.engine.txn.View; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -48,6 +50,7 @@ public sealed class Reader permits Writer { private final Principal principal; private Boolean onlyDefaultTenant; + private final Map> grantedTypes = new HashMap<>(); Reader(HypergraphDatabase database, View view, Principal principal) { this.database = database; @@ -67,7 +70,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 +79,31 @@ 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) { + return grantedTypes.computeIfAbsent(type, found -> Access.onType(view, 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 +119,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 +175,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)); } @@ -173,19 +200,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) { @@ -243,7 +270,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"); @@ -330,7 +357,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 -> lowInclusive ? candidate.compareTo(bound) >= 0 : candidate.compareTo(bound) > 0).orElse(true) && high.map(bound -> highInclusive ? candidate.compareTo(bound) <= 0 : candidate.compareTo(bound) < 0).orElse(true); OptionalInt key = schema().existingPropertyKey(property); @@ -343,7 +370,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 71d7185..6140208 100644 --- a/database/src/main/java/io/hstore/db/Writer.java +++ b/database/src/main/java/io/hstore/db/Writer.java @@ -110,6 +110,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); @@ -127,6 +128,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); @@ -169,13 +171,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); } @@ -217,7 +219,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"); } @@ -228,6 +230,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"); @@ -240,7 +254,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()); @@ -249,7 +263,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); schema().existingPropertyKey(name).ifPresent(key -> updateBag(atom, key, bag -> bag.without(key))); } @@ -278,7 +292,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")); @@ -291,7 +305,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())) { @@ -308,7 +322,7 @@ 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); 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..fb06059 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"); 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..8ea1e0e 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() @@ -140,23 +143,41 @@ QueryResult run(Statement statement) { Security.setQuota(writer.transaction(), tenant, tenant.quota().with(quota)); 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); + if (validUntil.isPresent() || connectionLimit.isPresent()) { + Access.setOptions(writer.transaction(), new Access.Options(name, validUntil.orElse(Long.MAX_VALUE), + (int) connectionLimit.orElse(-1))); + } + 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"); } + if (user.tenant() == Principal.DEFAULT_TENANT && user.role() == Role.ADMIN && Security.globalAdmins(writer.engineView()) == 1 + && Security.users(writer.engineView()).size() > 1) { + throw HStoreException.invalid("cannot drop the last admin of the default tenant while other users remain"); + } Security.dropUser(writer.transaction(), name); return QueryResult.message("dropped user " + name); }); + 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"), Security.tenants(reader.engineView()).stream().filter(tenant -> session.principal().governs(tenant.id())).map(tenant -> { Quota.Usage usage = Security.usage(reader.engineView(), tenant.id()); @@ -164,9 +185,15 @@ QueryResult run(Statement statement) { QueryResult.number(usage.edges()), QueryResult.number(usage.payloadBytes()), limit(tenant.quota().maxAtoms()), limit(tenant.quota().maxEdges())); }))); - 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 +376,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 +385,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 +404,81 @@ 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 QueryResult alterUser(Writer writer, Statement.AlterUser alter) { + Security.User user = governedUser(writer, alter.name()); + List changes = new ArrayList<>(); + alter.role().filter(role -> role != user.role()).ifPresent(role -> { + if (user.tenant() == Principal.DEFAULT_TENANT && user.role() == Role.ADMIN && Security.globalAdmins(writer.engineView()) == 1) { + throw HStoreException.invalid("cannot change the role of the last admin of the default tenant"); + } + 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")); + } + 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"); + } + } + 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"; @@ -489,10 +586,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) { 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 a710b51..b3f48c3 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; @@ -147,8 +148,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++; @@ -405,6 +439,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(); } @@ -431,7 +468,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(); @@ -737,6 +776,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..6b44a04 100644 --- a/database/src/main/java/io/hstore/db/query/Planner.java +++ b/database/src/main/java/io/hstore/db/query/Planner.java @@ -68,7 +68,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"); 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 4b8e1d9..410c587 100644 --- a/database/src/main/java/io/hstore/db/query/Session.java +++ b/database/src/main/java/io/hstore/db/query/Session.java @@ -42,6 +42,7 @@ public final class Session implements AutoCloseable { private int failedAuthentications; private Optional scram = Optional.empty(); private Optional scramUser = Optional.empty(); + private Optional counted = Optional.empty(); private record ScramAttempt(Scram.Server server, SignIns.Reservation reservation) { } @@ -137,6 +138,9 @@ private void requireCanAuthenticate() { } private void signIn(Principal found) { + database.enter(found); + counted.ifPresent(database::leave); + counted = Optional.of(found.user()); principal = found; authenticated = true; credentials = database.read(reader -> Security.user(reader.engineView(), found.user())); @@ -191,7 +195,8 @@ public QueryResult execute(Statement statement) { throw HStoreException.invalid("user " + principal.user() + " was dropped or its password 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; @@ -346,5 +351,7 @@ public void close() { 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..132a04e --- /dev/null +++ b/database/src/main/java/io/hstore/db/security/Access.java @@ -0,0 +1,159 @@ +// 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 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())))); + public static final List> SLOTS = List.of(GRANTS, USER_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.delete(USER_OPTIONS, Hashing.of(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 boolean isCapability(int type) { + return type < 0; + } +} 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 f5ac054..ea19319 100644 --- a/database/src/main/java/io/hstore/db/security/Security.java +++ b/database/src/main/java/io/hstore/db/security/Security.java @@ -31,6 +31,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 int MIN_VERIFIER_ITERATIONS = 4096; private static final long SCRAM_SECRET = 0x5343_5241_4D00_0000L; private static final int SCRAM_SECRET_WORDS = 4; @@ -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); public static final Tenant DEFAULT = new Tenant(Principal.DEFAULT_TENANT, "default", Quota.UNLIMITED); @@ -227,6 +228,24 @@ 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 long globalAdmins(View view) { + return users(view).stream().filter(user -> user.tenant() == Principal.DEFAULT_TENANT && user.role() == Role.ADMIN).count(); + } + + 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) { @@ -242,7 +261,7 @@ public static Optional authenticate(View view, String name, String pa if (stored.isEmpty()) { derive(password, UNKNOWN_USER_SALT, ITERATIONS); } - return stored.filter(user -> verifier(user).matches(password)).map(Security::principal); + return stored.filter(user -> verifier(user).matches(password)).filter(user -> !expired(view, user.name())).map(Security::principal); } 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..f9049a4 --- /dev/null +++ b/database/src/test/java/io/hstore/db/PermissionsTest.java @@ -0,0 +1,252 @@ +// 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.Scram; +import io.hstore.engine.EngineOptions; +import io.hstore.engine.HStoreException; + +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.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 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("last admin"), last); + assertTrue(assertThrows(HStoreException.InvalidSchema.class, () -> root.execute("DROP USER root")).getMessage().contains("last admin")); + root.execute("ALTER USER ops ROLE admin"); + 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")); + } +} diff --git a/docs/database/hql.md b/docs/database/hql.md index 002d3e7..0d9ee6f 100644 --- a/docs/database/hql.md +++ b/docs/database/hql.md @@ -734,16 +734,27 @@ 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" ; 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). +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 ec3c03e..9d285c9 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,65 @@ 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 | with `MONITOR` | with `MONITOR` | with `MONITOR` | with `MONITOR` | +| `CHECKPOINT`, `CHECKPOINT AT`, `COMPACT` | yes | with `MAINTAIN` | with `MAINTAIN` | with `MAINTAIN` | with `MAINTAIN` | +| 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 invisible exactly like an atom of another tenant: `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, 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 the ids of all its members, including members of types it cannot read, + the way a foreign-key column shows the key of a row in a table you cannot select from. Those members have no + label, properties or incidences for the user, and adding one to an edge fails as if it did not exist. +- Writing to a hyperedge needs `WRITE` on the edge's type; the members added must be readable. +- Grants are kept in slot `GRANTS` = 75 (schema 110), keyed by user and type, and read per statement, so a grant or + revoke applies to the next statement of every open session. `DROP USER` removes the user's grants. + +### 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. +- Both live in slot `USER_OPTIONS` = 76 (schema 111); a user without an entry has no expiry and no limit. + +### Changing roles + +`ALTER USER u ROLE r` changes the role; `u`'s open sessions then fail with "authenticate again", so the new role +applies at once. The last ADMIN of the default tenant cannot change its role, and cannot be dropped while other +users exist, so the database never loses its last global admin. `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 diff --git a/docs/operations/logging.md b/docs/operations/logging.md index 157043b..2c5532b 100644 --- a/docs/operations/logging.md +++ b/docs/operations/logging.md @@ -142,6 +142,7 @@ and in Docker it is the last line of `docker logs`. The storage-integrity failur | WARNING | `connection from /{ip}:{port} refused: {reason} (max_connections={n})`: the server is full, or 16 connections from this address are still waiting to authenticate. | | WARNING | `connection {n} from /{ip}:{port} refused: no users exist and the client is not on this host` (Studio: `studio request from … refused: …`). | | 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}" 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. | | LOG | `studio session opened for user={user} role={ROLE} from /{ip}:{port}` | 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/engine/src/main/java/io/hstore/engine/txn/View.java b/engine/src/main/java/io/hstore/engine/txn/View.java index 1dac53b..83df6af 100644 --- a/engine/src/main/java/io/hstore/engine/txn/View.java +++ b/engine/src/main/java/io/hstore/engine/txn/View.java @@ -104,14 +104,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/Studio.java b/server/src/main/java/io/hstore/server/studio/Studio.java index 01b264c..bc373e8 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; @@ -484,8 +485,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/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()); From df9b42ea514e8be81158914570006ab14db11286 Mon Sep 17 00:00:00 2001 From: venkat1701 Date: Sun, 25 Oct 2026 22:41:38 +0530 Subject: [PATCH 2/5] Close privilege gaps found in review - ROLE none no longer sees evidence or assertions (TRACE), which belong to the tenant rather than to a type. - A key reference to an unreadable type fails with the privilege error whether or not the key exists. - DELETE by a ROLE none user needs WRITE on every hyperedge of its tenant that holds the atom, since deleting removes those memberships. - Grants are read from the latest committed main, so a revoke applies inside open transactions and AT GENERATION cannot restore it. - EXPLAIN for ROLE none estimates index ranges blindly and skips the tenant's cardinality feedback. - Set algebra INTO checks the members it copies. - A Studio login that cannot open a session releases its connection. - Removing, demoting or expiring an admin of the default tenant needs another admin who can sign in, and places a commit-time precondition on those admins, so concurrent removals cannot both commit. - Only global admins change the role, expiry or connection limit of an admin. - Password expiry is checked after the SCRAM proof; sessions ended by a credential change stay ended until they sign in again. - hstore check and --repair-tenants cover every active branch, and the boundary tests also cover raw transactions and engine snapshots. --- .../java/io/hstore/db/HypergraphDatabase.java | 17 +++- .../src/main/java/io/hstore/db/Reader.java | 15 +++- .../src/main/java/io/hstore/db/Writer.java | 22 +++++ .../io/hstore/db/evidence/Provenance.java | 8 +- .../java/io/hstore/db/query/Executor.java | 24 +++-- .../main/java/io/hstore/db/query/Planner.java | 12 ++- .../main/java/io/hstore/db/query/Session.java | 11 ++- .../java/io/hstore/db/security/Security.java | 19 +++- .../hstore/db/security/TenantMemberships.java | 26 ++++-- .../java/io/hstore/db/PermissionsTest.java | 89 ++++++++++++++++++- .../java/io/hstore/db/TenantBoundaryTest.java | 16 ++-- docs/database/security.md | 40 ++++++--- docs/operations/cli.md | 4 +- docs/operations/wire-protocol.md | 2 +- .../src/main/java/io/hstore/server/Check.java | 8 +- .../java/io/hstore/server/studio/Studio.java | 8 +- .../java/io/hstore/server/ServerTest.java | 14 +-- .../io/hstore/server/TenantBoundaryTest.java | 7 +- 18 files changed, 278 insertions(+), 64 deletions(-) diff --git a/database/src/main/java/io/hstore/db/HypergraphDatabase.java b/database/src/main/java/io/hstore/db/HypergraphDatabase.java index 0d66831..0438273 100644 --- a/database/src/main/java/io/hstore/db/HypergraphDatabase.java +++ b/database/src/main/java/io/hstore/db/HypergraphDatabase.java @@ -33,6 +33,7 @@ import java.nio.file.Path; import java.time.Duration; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; @@ -95,12 +96,22 @@ public boolean tenantMembershipsChecked() { } public List repairTenantMemberships() { - return write(writer -> TenantMemberships.repair(writer.transaction())); + List removed = new ArrayList<>(); + for (Branch branch : engine.branches().stream().filter(Branch::isActive).toList()) { + removed.addAll(write(options.transaction().onBranch(branch.id()), Principal.SYSTEM, + writer -> TenantMemberships.repair(writer.transaction(), branch.name()))); + } + write(writer -> { + if (!TenantMemberships.checked(writer.transaction())) { + TenantMemberships.markChecked(writer.transaction()); + } + return null; + }); + return removed; } public Scram.Server scram(Optional channelBinding) { - return new Scram.Server(name -> read(reader -> Security.user(reader.engineView(), name) - .filter(user -> !Security.expired(reader.engineView(), user.name()))).map(Security::verifier), scramSecret, + return new Scram.Server(name -> read(reader -> Security.user(reader.engineView(), name)).map(Security::verifier), scramSecret, Security.ITERATIONS, channelBinding); } diff --git a/database/src/main/java/io/hstore/db/Reader.java b/database/src/main/java/io/hstore/db/Reader.java index c8132a4..0fa4ccf 100644 --- a/database/src/main/java/io/hstore/db/Reader.java +++ b/database/src/main/java/io/hstore/db/Reader.java @@ -28,6 +28,7 @@ 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; @@ -51,6 +52,7 @@ public sealed class Reader permits Writer { private Boolean onlyDefaultTenant; private final Map> grantedTypes = new HashMap<>(); + private long grantsGeneration = -1; Reader(HypergraphDatabase database, View view, Principal principal) { this.database = database; @@ -95,7 +97,16 @@ public boolean writable(int type) { } private Optional privilege(int type) { - return grantedTypes.computeIfAbsent(type, found -> Access.onType(view, principal.user(), found)); + 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) { @@ -192,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) { diff --git a/database/src/main/java/io/hstore/db/Writer.java b/database/src/main/java/io/hstore/db/Writer.java index 6140208..a6ec442 100644 --- a/database/src/main/java/io/hstore/db/Writer.java +++ b/database/src/main/java/io/hstore/db/Writer.java @@ -197,6 +197,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); @@ -323,6 +337,14 @@ public void embed(long atom, String model, int version, float[] vector) { public void delete(long atom) { principal().requireWrite(); 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 fb06059..6a44e7f 100644 --- a/database/src/main/java/io/hstore/db/evidence/Provenance.java +++ b/database/src/main/java/io/hstore/db/evidence/Provenance.java @@ -90,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/query/Executor.java b/database/src/main/java/io/hstore/db/query/Executor.java index 8ea1e0e..0be6563 100644 --- a/database/src/main/java/io/hstore/db/query/Executor.java +++ b/database/src/main/java/io/hstore/db/query/Executor.java @@ -158,9 +158,8 @@ QueryResult run(Statement statement) { 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"); } - if (user.tenant() == Principal.DEFAULT_TENANT && user.role() == Role.ADMIN && Security.globalAdmins(writer.engineView()) == 1 - && Security.users(writer.engineView()).size() > 1) { - throw HStoreException.invalid("cannot drop the last admin of the default tenant while other users remain"); + if (Security.globalAdmin(user) && Security.users(writer.engineView()).size() > 1) { + Security.keepAnotherGlobalAdmin(writer.transaction(), name); } Security.dropUser(writer.transaction(), name); return QueryResult.message("dropped user " + name); @@ -411,11 +410,18 @@ private Security.User governedUser(Reader reader, String name) { 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(); + 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"); + } + boolean demoted = alter.role().filter(role -> role != Role.ADMIN).isPresent(); + boolean expires = alter.validUntil().isPresent() && alter.validUntil().getAsLong() <= System.currentTimeMillis(); + if (Security.globalAdmin(user) && (demoted || expires)) { + Security.keepAnotherGlobalAdmin(writer.transaction(), user.name()); + } List changes = new ArrayList<>(); alter.role().filter(role -> role != user.role()).ifPresent(role -> { - if (user.tenant() == Principal.DEFAULT_TENANT && user.role() == Role.ADMIN && Security.globalAdmins(writer.engineView()) == 1) { - throw HStoreException.invalid("cannot change the role of the last admin of the default tenant"); - } Security.setRole(writer.transaction(), user.name(), role); changes.add("role " + role); }); @@ -555,7 +561,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()) @@ -728,7 +736,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/Planner.java b/database/src/main/java/io/hstore/db/query/Planner.java index 6b44a04..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; @@ -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 410c587..e9d0175 100644 --- a/database/src/main/java/io/hstore/db/query/Session.java +++ b/database/src/main/java/io/hstore/db/query/Session.java @@ -38,6 +38,7 @@ 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(); @@ -112,7 +113,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)).map(Security::principal) + ? database.read(reader -> Security.user(reader.engineView(), user) + .filter(stored -> !Security.expired(reader.engineView(), stored.name()))).map(Security::principal) : Optional.empty(); if (found.isEmpty()) { attempt.reservation().failed(); @@ -139,6 +141,7 @@ private void requireCanAuthenticate() { private void signIn(Principal found) { database.enter(found); + stale = false; counted.ifPresent(database::leave); counted = Optional.of(found.user()); principal = found; @@ -161,6 +164,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() @@ -192,7 +198,8 @@ public List executeAll(String source) { public QueryResult execute(Statement statement) { if (!(statement instanceof Statement.Authenticate) && !credentialsCurrent()) { close(); - throw HStoreException.invalid("user " + principal.user() + " was dropped or its password changed; authenticate again"); + 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, Optional password, var _, var _, var _) 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 ea19319..d92f4f2 100644 --- a/database/src/main/java/io/hstore/db/security/Security.java +++ b/database/src/main/java/io/hstore/db/security/Security.java @@ -237,7 +237,24 @@ public static void setRole(Transaction txn, String name, Role role) { } public static long globalAdmins(View view) { - return users(view).stream().filter(user -> user.tenant() == Principal.DEFAULT_TENANT && user.role() == Role.ADMIN).count(); + 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 void keepAnotherGlobalAdmin(Transaction txn, String leaving) { + long now = System.currentTimeMillis(); + List others = users(txn).stream() + .filter(user -> globalAdmin(user) && !user.name().equals(leaving) && !Access.options(txn, user.name()).expired(now)) + .toList(); + if (others.isEmpty()) { + throw HStoreException.invalid("this would leave no admin of the default tenant who can sign in; " + + "create or restore another one first"); + } + others.forEach(user -> txn.merge(USERS, userKey(user.name()), + current -> current.filter(Security::globalAdmin).isPresent(), current -> current)); } public static boolean expired(View view, String name) { diff --git a/database/src/main/java/io/hstore/db/security/TenantMemberships.java b/database/src/main/java/io/hstore/db/security/TenantMemberships.java index b1a2149..a230f1a 100644 --- a/database/src/main/java/io/hstore/db/security/TenantMemberships.java +++ b/database/src/main/java/io/hstore/db/security/TenantMemberships.java @@ -3,8 +3,11 @@ package io.hstore.db.security; +import io.hstore.engine.StorageEngine; import io.hstore.engine.catalog.AtomRecord; +import io.hstore.engine.catalog.Branch; import io.hstore.engine.catalog.EngineSlots; +import io.hstore.engine.txn.Snapshot; import io.hstore.engine.txn.Transaction; import io.hstore.engine.txn.View; @@ -16,22 +19,30 @@ public final class TenantMemberships { private static final long CHECKED = 0x4D45_4D42_4552_5301L; - public record Crossing(long edge, long member, int edgeTenant, int memberTenant) { + public record Crossing(String branch, long edge, long member, int edgeTenant, int memberTenant) { public String repair() { - return "REMOVE @" + member + " FROM @" + edge; + return (branch.equals("main") ? "" : "USE BRANCH " + branch + "; ") + "REMOVE @" + member + " FROM @" + edge; } } + public static List find(StorageEngine engine) { + return engine.branches().stream().filter(Branch::isActive).flatMap(branch -> { + try (Snapshot snapshot = engine.snapshot(branch.id())) { + return find(snapshot, branch.name()).stream(); + } + }).toList(); + } + private TenantMemberships() { } - public static List find(View view) { + public static List find(View view, String branch) { return view.atoms() .flatMap(entry -> entry.value() instanceof AtomRecord.EdgeRecord edge ? view.edge(entry.key(), edge).memberIds().boxed() .flatMap(member -> view.atom(member) .filter(record -> record.tenant() != edge.tenant()) - .map(record -> new Crossing(entry.key(), member, edge.tenant(), record.tenant())) + .map(record -> new Crossing(branch, entry.key(), member, edge.tenant(), record.tenant())) .stream()) : Stream.empty()) .toList(); @@ -45,12 +56,9 @@ public static void markChecked(Transaction txn) { txn.merge(EngineSlots.SETTINGS, CHECKED, Optional::isEmpty, _ -> Optional.of(1L)); } - public static List repair(Transaction txn) { - List crossings = find(txn); + public static List repair(Transaction txn, String branch) { + List crossings = find(txn, branch); crossings.forEach(crossing -> txn.remove(crossing.edge(), crossing.member())); - if (!checked(txn)) { - markChecked(txn); - } return crossings; } } diff --git a/database/src/test/java/io/hstore/db/PermissionsTest.java b/database/src/test/java/io/hstore/db/PermissionsTest.java index f9049a4..4062f64 100644 --- a/database/src/test/java/io/hstore/db/PermissionsTest.java +++ b/database/src/test/java/io/hstore/db/PermissionsTest.java @@ -38,6 +38,7 @@ void open() { 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; @@ -194,8 +195,11 @@ void rolesChangeAndTheLastAdminStays() { } try (Session root = login("root", "root-password")) { String last = assertThrows(HStoreException.InvalidSchema.class, () -> root.execute("ALTER USER root ROLE reader")).getMessage(); - assertTrue(last.contains("last admin"), last); - assertTrue(assertThrows(HStoreException.InvalidSchema.class, () -> root.execute("DROP USER root")).getMessage().contains("last admin")); + assertTrue(last.contains("no admin of the default tenant who can sign in"), last); + assertTrue(assertThrows(HStoreException.InvalidSchema.class, () -> root.execute("DROP USER root")).getMessage() + .contains("no admin of the default tenant who can sign in")); + assertThrows(HStoreException.InvalidSchema.class, () -> root.execute("ALTER USER root VALID UNTIL '2000-01-01'")); + root.execute("ALTER USER root VALID UNTIL '2999-01-01'"); root.execute("ALTER USER ops ROLE admin"); root.execute("ALTER USER root ROLE reader"); } @@ -249,4 +253,85 @@ void connectionLimitsCountSignedInSessions() { 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 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/security.md b/docs/database/security.md index 9d285c9..5865c4a 100644 --- a/docs/database/security.md +++ b/docs/database/security.md @@ -27,8 +27,8 @@ tenant. | 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 | with `MONITOR` | with `MONITOR` | with `MONITOR` | with `MONITOR` | -| `CHECKPOINT`, `CHECKPOINT AT`, `COMPACT` | yes | with `MAINTAIN` | with `MAINTAIN` | with `MAINTAIN` | with `MAINTAIN` | +| `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 @@ -48,9 +48,21 @@ READER, WRITER and ADMIN cover every type of their tenant, like PostgreSQL's `pg - A hyperedge the user can read shows the ids of all its members, including members of types it cannot read, the way a foreign-key column shows the key of a row in a table you cannot select from. Those members have no label, properties or incidences for the user, and adding one to an edge fails as if it did not exist. -- Writing to a hyperedge needs `WRITE` on the edge's type; the members added must be readable. -- Grants are kept in slot `GRANTS` = 75 (schema 110), keyed by user and type, and read per statement, so a grant or - revoke applies to the next statement of every open session. `DROP USER` removes the user's grants. +- 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 @@ -73,9 +85,15 @@ DDL stay with global admins. ### Changing roles -`ALTER USER u ROLE r` changes the role; `u`'s open sessions then fail with "authenticate again", so the new role -applies at once. The last ADMIN of the default tenant cannot change its role, and cannot be dropped while other -users exist, so the database never loses its last global admin. +`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, so a tenant admin cannot lift limits put on itself or on another admin of its tenant. + +A change that would leave the default tenant without an ADMIN whose password still works (dropping one while other +users exist, giving it another role, or expiring its password now) is refused. The statement also places a +commit-time precondition on every other such admin, so two concurrent transactions that each remove a different +admin cannot both commit. 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 @@ -139,13 +157,13 @@ 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 tenant's atoms. Evidence sources, assertion sources and the evidence list of `QUALIFY` must be atoms, evidence or -assertions of the writer's tenant (`Provenance.record/assertion`). `hstore check` lists any membership on main +assertions of the writer's tenant (`Provenance.record/assertion`). `hstore check` lists any membership on an active branch whose member belongs to a different tenant than its edge, with the `REMOVE` statement that undoes it, and `hstore check --repair-tenants` removes them in one transaction. Databases written before these checks existed could have such memberships from `QUALIFY`. A new database, and one that `hstore check` found clean or diff --git a/docs/operations/cli.md b/docs/operations/cli.md index 63b6f7a..391e3ba 100644 --- a/docs/operations/cli.md +++ b/docs/operations/cli.md @@ -214,7 +214,7 @@ A violated invariant aborts the check with the offending page and a reason such `leaf keys out of order in ` or `unbalanced tree in `. The report ends with a tenant isolation line. Older versions could leave a hyperedge with a member from another -tenant; the check lists each such membership with the `REMOVE` statement that undoes it. With `--repair-tenants` +tenant; the check lists each such membership, on every active branch, with the statement that undoes it. With `--repair-tenants` (after the directory), `check` first removes all of them in one transaction and lists what it removed: ``` @@ -223,7 +223,7 @@ tenant isolation: removed 1 memberships that crossed tenants: REMOVE @14 FROM @12 -- member of tenant 1 in an edge of tenant 2 generation 160, recovered 0 commits from the log … -tenant isolation: no hyperedge on main has a member from another tenant +tenant isolation: no hyperedge on any branch has a member from another tenant ``` A check that finds no such membership, and every repair, records that the database is clean, which silences the diff --git a/docs/operations/wire-protocol.md b/docs/operations/wire-protocol.md index 3248eb8..3307ba1 100644 --- a/docs/operations/wire-protocol.md +++ b/docs/operations/wire-protocol.md @@ -163,7 +163,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 sends a successful `AUTHENTICATE`. Without `tls = on` the password travels in clear text. With TLS the server completes the handshake before it diff --git a/server/src/main/java/io/hstore/server/Check.java b/server/src/main/java/io/hstore/server/Check.java index 5caeb42..ef06d20 100644 --- a/server/src/main/java/io/hstore/server/Check.java +++ b/server/src/main/java/io/hstore/server/Check.java @@ -29,7 +29,7 @@ static String run(StorageEngine engine) { boolean clean; boolean checked; try (Snapshot pinned = engine.snapshot()) { - List crossings = TenantMemberships.find(pinned); + List crossings = TenantMemberships.find(engine); report = run(engine, pinned.catalog()) + "\n" + isolation(crossings); clean = crossings.isEmpty(); checked = TenantMemberships.checked(pinned); @@ -46,7 +46,7 @@ static String run(StorageEngine engine) { static String repair(HypergraphDatabase database) { List removed = database.repairTenantMemberships(); if (removed.isEmpty()) { - return "tenant isolation: no hyperedge on main had a member from another tenant; nothing to repair"; + return "tenant isolation: no hyperedge on any branch had a member from another tenant; nothing to repair"; } return "tenant isolation: removed " + removed.size() + " memberships that crossed tenants:\n" + removed.stream().map(crossing -> " " + crossing.repair() + " -- member of tenant " + crossing.memberTenant() @@ -83,9 +83,9 @@ private static String isolation(List found) { List crossings = found.stream().map(crossing -> " " + crossing.repair() + " -- member of tenant " + crossing.memberTenant() + " in an edge of tenant " + crossing.edgeTenant()).toList(); if (crossings.isEmpty()) { - return "tenant isolation: no hyperedge on main has a member from another tenant"; + return "tenant isolation: no hyperedge on any branch has a member from another tenant"; } - return "tenant isolation: " + crossings.size() + " memberships on main cross tenants; remove them with " + return "tenant isolation: " + crossings.size() + " memberships cross tenants; remove them with " + "hstore check --repair-tenants, or run these as the edge's tenant:\n" + String.join("\n", crossings.subList(0, Math.min(MAX_LISTED, crossings.size()))) + (crossings.size() > MAX_LISTED ? "\n ... " + (crossings.size() - MAX_LISTED) + " more" : ""); 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 bc373e8..71eb0a1 100644 --- a/server/src/main/java/io/hstore/server/studio/Studio.java +++ b/server/src/main/java/io/hstore/server/studio/Studio.java @@ -332,7 +332,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"); diff --git a/server/src/test/java/io/hstore/server/ServerTest.java b/server/src/test/java/io/hstore/server/ServerTest.java index b2e8a4f..57b64d5 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.topology.Incidence; import io.hstore.engine.topology.Validity; @@ -480,22 +481,23 @@ void checkFindsAndRepairsMembershipsAcrossTenants() throws Exception { writer.transaction().upsert(g, p, _ -> Incidence.of(p)); return null; }); + database.engine().createBranch("side", Branch.MAIN); ids = new long[]{p, g}; } try (StorageEngine engine = StorageEngine.open(directory, options.engine().withExtensions(HypergraphDatabase.extensionSlots(), List.of()))) { String report = Check.run(engine); - assertTrue(report.contains("1 memberships on main cross tenants"), report); - assertTrue(report.contains("REMOVE @" + ids[0] + " FROM @" + ids[1]), report); + assertTrue(report.contains("2 memberships cross tenants"), report); + assertTrue(report.contains("USE BRANCH side; REMOVE @" + ids[0] + " FROM @" + ids[1]), report); } try (HypergraphDatabase database = HypergraphDatabase.open(directory, options)) { String repaired = Check.repair(database); - assertTrue(repaired.contains("removed 1 memberships") && repaired.contains("REMOVE @" + ids[0] + " FROM @" + ids[1]), repaired); + assertTrue(repaired.contains("removed 2 memberships") && repaired.contains("REMOVE @" + ids[0] + " FROM @" + ids[1]), repaired); assertTrue(database.tenantMembershipsChecked()); assertTrue(Check.repair(database).contains("nothing to repair")); assertEquals(1L, (long) database.read(reader -> reader.engineView().cardinality(ids[1]))); } try (StorageEngine engine = StorageEngine.open(directory, options.engine().withExtensions(HypergraphDatabase.extensionSlots(), List.of()))) { - assertTrue(Check.run(engine).contains("no hyperedge on main has a member from another tenant")); + assertTrue(Check.run(engine).contains("no hyperedge on any branch has a member from another tenant")); } } @@ -543,14 +545,14 @@ void checkListsMembershipsThatCrossTenants() { """); long patient = admin.variables().get("a"); long edge = admin.variables().get("e"); - assertTrue(Check.run(database.engine()).contains("no hyperedge on main has a member from another tenant")); + assertTrue(Check.run(database.engine()).contains("no hyperedge on any branch has a member from another tenant")); database.write(writer -> { writer.transaction().insert(edge, new Incidence(patient, Dictionary.NO_ROLES, Weight.ONE, Validity.ALWAYS.from(), Validity.ALWAYS.to(), 0, 0)); return null; }); String report = Check.run(database.engine()); - assertTrue(report.contains("1 memberships on main cross tenants"), report); + assertTrue(report.contains("1 memberships cross tenants"), report); assertTrue(report.contains("REMOVE @" + patient + " FROM @" + edge), report); } } 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); } From 3ae0d461e28ad56fe97663d9ec35e3b8a363ce0f Mon Sep 17 00:00:00 2001 From: venkat1701 Date: Tue, 27 Oct 2026 15:43:25 +0530 Subject: [PATCH 3/5] Close the remaining gaps in admin, grant and connection checks - A merge used only as a guard changed nothing, so its slot did not count as touched and the fast path skipped the precondition. Every slot a transaction writes to now counts. - The default tenant keeps an admin whose password never expires, and the check also guards the other admins' options records. - CREATE USER clears leftover grants and options; GRANT, REVOKE and ALTER USER guard the user's record against a racing DROP USER. - Signing in again as the same user keeps one connection count, idle Studio sessions are swept in the background, and tenants get a connections quota. - Writer.load checks write access before loading no members. --- .../java/io/hstore/db/HypergraphDatabase.java | 32 +++- .../src/main/java/io/hstore/db/Writer.java | 1 + .../java/io/hstore/db/query/Executor.java | 35 +++- .../main/java/io/hstore/db/query/Session.java | 10 +- .../java/io/hstore/db/security/Access.java | 29 +++- .../java/io/hstore/db/security/Quota.java | 2 +- .../java/io/hstore/db/security/Security.java | 23 ++- .../java/io/hstore/db/PermissionsTest.java | 150 +++++++++++++++++- docs/database/hql.md | 4 +- docs/database/security.md | 41 +++-- docs/transactions/transactions.md | 2 +- .../main/java/io/hstore/engine/txn/Op.java | 1 + .../java/io/hstore/engine/txn/Workspace.java | 9 +- .../hstore/engine/txn/PreconditionTest.java | 87 ++++++++++ .../io/hstore/server/studio/Sessions.java | 10 +- .../java/io/hstore/server/studio/Studio.java | 1 + .../io/hstore/server/studio/SessionsTest.java | 49 ++++++ 17 files changed, 439 insertions(+), 47 deletions(-) create mode 100644 engine/src/test/java/io/hstore/engine/txn/PreconditionTest.java create mode 100644 server/src/test/java/io/hstore/server/studio/SessionsTest.java diff --git a/database/src/main/java/io/hstore/db/HypergraphDatabase.java b/database/src/main/java/io/hstore/db/HypergraphDatabase.java index 66989bd..6f5f9f6 100644 --- a/database/src/main/java/io/hstore/db/HypergraphDatabase.java +++ b/database/src/main/java/io/hstore/db/HypergraphDatabase.java @@ -60,6 +60,7 @@ public final class HypergraphDatabase implements AutoCloseable { 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 HypergraphDatabase(Path directory, DatabaseOptions options) { @@ -130,13 +131,21 @@ public SignIns signIns() { public void enter(Principal principal) { Access.Options limits = read(reader -> Access.options(reader.engineView(), principal.user())); - connections.compute(principal.user(), (user, count) -> { - int current = count == null ? 0 : count; - if (limits.connectionLimit() >= 0 && !principal.global() && current >= limits.connectionLimit()) { - throw HStoreException.limit("too many connections for user " + user + " (connection limit " + limits.connectionLimit() + ")"); + 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 + ")"); + } } - return current + 1; - }); + connections.merge(principal.user(), 1, Integer::sum); + tenantConnections.merge(principal.home(), 1, Integer::sum); + } } public boolean holds(Principal principal, Access.Privilege capability) { @@ -144,8 +153,15 @@ public boolean holds(Principal principal, Access.Privilege capability) { && read(reader -> Access.holds(reader.engineView(), principal.user(), capability)); } - public void leave(String user) { - connections.computeIfPresent(user, (_, count) -> count <= 1 ? null : count - 1); + 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) { diff --git a/database/src/main/java/io/hstore/db/Writer.java b/database/src/main/java/io/hstore/db/Writer.java index a6ec442..49223e1 100644 --- a/database/src/main/java/io/hstore/db/Writer.java +++ b/database/src/main/java/io/hstore/db/Writer.java @@ -186,6 +186,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); 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 0be6563..b33e449 100644 --- a/database/src/main/java/io/hstore/db/query/Executor.java +++ b/database/src/main/java/io/hstore/db/query/Executor.java @@ -135,17 +135,32 @@ 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 = limits.remove("connections"); + 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 = limits.remove("connections"); + 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, var validUntil, var connectionLimit) -> write(writer -> { - Security.createUser(writer.transaction(), name, password, governedTenant(writer, tenant), role); + int home = governedTenant(writer, tenant); + if (home == Principal.DEFAULT_TENANT && role == Role.ADMIN && validUntil.isPresent() && validUntil.getAsLong() != Long.MAX_VALUE) { + Security.keepAnotherGlobalAdmin(writer.transaction(), name); + } + Security.createUser(writer.transaction(), name, password, home, 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))); @@ -177,12 +192,16 @@ QueryResult run(Statement statement) { + (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"), + 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", "valid until", "connection limit"), Security.users(reader.engineView()).stream() @@ -416,7 +435,7 @@ private QueryResult alterUser(Writer writer, Statement.AlterUser alter) { + "of an admin"); } boolean demoted = alter.role().filter(role -> role != Role.ADMIN).isPresent(); - boolean expires = alter.validUntil().isPresent() && alter.validUntil().getAsLong() <= System.currentTimeMillis(); + boolean expires = alter.validUntil().isPresent() && alter.validUntil().getAsLong() != Long.MAX_VALUE; if (Security.globalAdmin(user) && (demoted || expires)) { Security.keepAnotherGlobalAdmin(writer.transaction(), user.name()); } @@ -430,6 +449,9 @@ private QueryResult alterUser(Writer writer, Statement.AlterUser alter) { changes.add("password"); }); if (alter.validUntil().isPresent() || alter.connectionLimit().isPresent()) { + if (alter.role().isEmpty() && alter.password().isEmpty()) { + Security.keepUnchanged(writer.transaction(), user); + } 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()))); @@ -451,6 +473,7 @@ private Security.User grantee(Writer writer, Access.Privilege privilege, String throw HStoreException.invalid(privilege + " can only be granted to users of the default tenant"); } } + Security.keepUnchanged(writer.transaction(), user); return user; } 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 65198e2..ef25858 100644 --- a/database/src/main/java/io/hstore/db/query/Session.java +++ b/database/src/main/java/io/hstore/db/query/Session.java @@ -43,7 +43,7 @@ public final class Session implements AutoCloseable { private int failedAuthentications; private Optional scram = Optional.empty(); private Optional scramUser = Optional.empty(); - private Optional counted = Optional.empty(); + private Optional counted = Optional.empty(); private Optional clearTextRefusal = Optional.empty(); private record ScramAttempt(Scram.Server server, SignIns.Reservation reservation) { @@ -145,10 +145,12 @@ private void requireCanAuthenticate() { } private void signIn(Principal found) { - database.enter(found); + if (counted.filter(principal -> principal.user().equals(found.user())).isEmpty()) { + database.enter(found); + counted.ifPresent(database::leave); + counted = Optional.of(found); + } stale = false; - counted.ifPresent(database::leave); - counted = Optional.of(found.user()); principal = found; authenticated = true; credentials = database.read(reader -> Security.user(reader.engineView(), found.user())); diff --git a/database/src/main/java/io/hstore/db/security/Access.java b/database/src/main/java/io/hstore/db/security/Access.java index 132a04e..2138df0 100644 --- a/database/src/main/java/io/hstore/db/security/Access.java +++ b/database/src/main/java/io/hstore/db/security/Access.java @@ -34,6 +34,9 @@ public boolean onType() { 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); @@ -61,7 +64,14 @@ public boolean expired(long now) { 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())))); - public static final List> SLOTS = List.of(GRANTS, USER_OPTIONS); + 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() { } @@ -138,7 +148,7 @@ public static List grants(View view) { 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.delete(USER_OPTIONS, Hashing.of(user)); + txn.merge(USER_OPTIONS, Hashing.of(user), _ -> true, current -> current.filter(options -> !options.user().equals(user))); } public static Options options(View view, String user) { @@ -153,6 +163,21 @@ public static void setOptions(Transaction txn, Options 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/Security.java b/database/src/main/java/io/hstore/db/security/Security.java index d66bbb5..e79e403 100644 --- a/database/src/main/java/io/hstore/db/security/Security.java +++ b/database/src/main/java/io/hstore/db/security/Security.java @@ -71,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, Access.GRANTS, Access.USER_OPTIONS); + 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); @@ -242,6 +242,10 @@ public static void setRole(Transaction txn, String name, Role role) { 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(); } @@ -251,16 +255,23 @@ public static boolean globalAdmin(User user) { } public static void keepAnotherGlobalAdmin(Transaction txn, String leaving) { - long now = System.currentTimeMillis(); List others = users(txn).stream() - .filter(user -> globalAdmin(user) && !user.name().equals(leaving) && !Access.options(txn, user.name()).expired(now)) + .filter(user -> globalAdmin(user) && !user.name().equals(leaving) && neverExpires(Access.options(txn, user.name()))) .toList(); if (others.isEmpty()) { - throw HStoreException.invalid("this would leave no admin of the default tenant who can sign in; " + throw HStoreException.invalid("this would leave no admin of the default tenant whose password never expires; " + "create or restore another one first"); } - others.forEach(user -> txn.merge(USERS, userKey(user.name()), - current -> current.filter(Security::globalAdmin).isPresent(), current -> current)); + for (User other : others) { + txn.merge(USERS, userKey(other.name()), current -> current.filter(user -> user.name().equals(other.name()) && globalAdmin(user)).isPresent(), + current -> current); + txn.merge(Access.USER_OPTIONS, Hashing.of(other.name()), current -> neverExpires(current.filter(options -> options.user().equals(other.name())) + .orElseGet(() -> Access.Options.defaults(other.name()))), current -> current); + } + } + + private static boolean neverExpires(Access.Options options) { + return options.validUntil() == Long.MAX_VALUE; } public static boolean expired(View view, String name) { diff --git a/database/src/test/java/io/hstore/db/PermissionsTest.java b/database/src/test/java/io/hstore/db/PermissionsTest.java index 4062f64..d89d8cd 100644 --- a/database/src/test/java/io/hstore/db/PermissionsTest.java +++ b/database/src/test/java/io/hstore/db/PermissionsTest.java @@ -5,9 +5,13 @@ 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; @@ -15,6 +19,7 @@ import org.junit.jupiter.api.io.TempDir; import java.nio.file.Path; +import java.time.Instant; import java.util.List; import java.util.Optional; @@ -195,12 +200,19 @@ void rolesChangeAndTheLastAdminStays() { } 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 who can sign in"), last); + 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 who can sign in")); + .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'")); - root.execute("ALTER USER root VALID UNTIL '2999-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() @@ -304,6 +316,138 @@ void concurrentAdminRemovalsCannotBothCommit() { && 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 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(""" diff --git a/docs/database/hql.md b/docs/database/hql.md index 0d9ee6f..08ff459 100644 --- a/docs/database/hql.md +++ b/docs/database/hql.md @@ -732,7 +732,7 @@ 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" role ] [ valid_until ] [ connections ] ; (* ADMIN; ROLE default writer *) alter_user = "ALTER" "USER" ( identifier | string ) @@ -751,7 +751,7 @@ show = "SHOW" ( "TENANTS" | "USERS" ) ; 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, +`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). diff --git a/docs/database/security.md b/docs/database/security.md index 33ed40c..ebfb157 100644 --- a/docs/database/security.md +++ b/docs/database/security.md @@ -40,14 +40,18 @@ READER, WRITER and ADMIN cover every type of their tenant, like PostgreSQL's `pg 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 invisible exactly like an atom of another tenant: `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, incidences, degrees, matches and semantic hits leave it out. Naming the 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 the ids of all its members, including members of types it cannot read, - the way a foreign-key column shows the key of a row in a table you cannot select from. Those members have no - label, properties or incidences for the user, and adding one to an edge fails as if it did not exist. +- 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. @@ -80,8 +84,18 @@ DDL stay with global admins. 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. -- Both live in slot `USER_OPTIONS` = 76 (schema 111); a user without an entry has no expiry and no limit. + connections for user u`. Global admins are exempt, as PostgreSQL exempts superusers. A session that signs in + again as the same user keeps its one count. 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). 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 races `DROP USER` fails instead of passing to the next user created with that name. ### Changing roles @@ -89,10 +103,13 @@ DDL stay with global admins. again, so the new role applies at once. Only a global admin may change the role, password expiry or connection limit of an ADMIN, so a tenant admin cannot lift limits put on itself or on another admin of its tenant. -A change that would leave the default tenant without an ADMIN whose password still works (dropping one while other -users exist, giving it another role, or expiring its password now) is refused. The statement also places a -commit-time precondition on every other such admin, so two concurrent transactions that each remove a different -admin cannot both commit. Should every admin still be lost (for example to a forgotten password), `hstore shell` on +The default tenant must keep an ADMIN whose password never expires. A change that would leave none (dropping one +while other users exist, giving it another role, giving its password any `VALID UNTIL` other than `'infinity'`, +or creating the first global admin with one) is refused: any expiry date eventually passes, so checking only +that the 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 other such admin, so two +concurrent transactions that each remove or expire a different admin cannot both commit, whichever records they +write. 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 diff --git a/docs/transactions/transactions.md b/docs/transactions/transactions.md index 29994c2..3e2190d 100644 --- a/docs/transactions/transactions.md +++ b/docs/transactions/transactions.md @@ -134,7 +134,7 @@ commit(txn): ### 5.1 Fast path: per-slot merge -`Workspace.untouchedSince(latest, includingReads)` is true when every slot the transaction **changed** still has the same root in `latest` as in its base (`Ref.same`). A slot counts as changed when its tree root differs from the base root. Slots that were only read are ignored for `SNAPSHOT` transactions. For `SERIALIZABLE` transactions they are included, because the fast path skips `validateReads`. When the check passes, the commit takes the latest root vector and overlays only the changed slots (`rebasedOnto`). A concurrent commit to a slot this transaction merely read is therefore kept, and commits that write disjoint slots never replay, however many other commits landed in between. +`Workspace.untouchedSince(latest, includingReads)` is true when every slot the transaction **changed** still has the same root in `latest` as in its base (`Ref.same`). A slot counts as changed when its tree root differs from the base root, or when the transaction wrote to it at all: every keyed write (`put`, `delete`, `update`, `merge`) carries a precondition (for `put`, `delete` and `update`, that the key still holds what the transaction saw), and a write that leaves the value as it was does not change the root. Such a slot is recorded by `Workspace.guard`, so when it moved the commit replays and checks the precondition against the latest state. Without that, a `merge` used only as a guard (`current -> current` with a precondition on another user's record, as the last-admin check does) would be skipped whenever the transaction's other writes went to slots that had not moved. Slots that were only read are ignored for `SNAPSHOT` transactions. For `SERIALIZABLE` transactions they are included, because the fast path skips `validateReads`. When the check passes, the commit takes the latest root vector and overlays only the changed slots (`rebasedOnto`). A concurrent commit to a slot this transaction merely read is therefore kept, and commits that write disjoint slots never replay, however many other commits landed in between. `requestsStable` handles idempotency keys: if the transaction carries a request id, the `REQUESTS` slot is written inside the commit lock, so the fast path additionally requires that `REQUESTS` itself has not moved since the base. diff --git a/engine/src/main/java/io/hstore/engine/txn/Op.java b/engine/src/main/java/io/hstore/engine/txn/Op.java index df99859..f30f38a 100644 --- a/engine/src/main/java/io/hstore/engine/txn/Op.java +++ b/engine/src/main/java/io/hstore/engine/txn/Op.java @@ -104,6 +104,7 @@ record SlotWrite(Slot slot, long key, Predicate> precondition, @Override public void apply(Workspace workspace) { + workspace.guard(slot); Optional current = workspace.get(slot, key); if (!precondition.test(current)) { String message = "precondition failed for " + slot + " key " + key; diff --git a/engine/src/main/java/io/hstore/engine/txn/Workspace.java b/engine/src/main/java/io/hstore/engine/txn/Workspace.java index f09d88b..9c3dfbf 100644 --- a/engine/src/main/java/io/hstore/engine/txn/Workspace.java +++ b/engine/src/main/java/io/hstore/engine/txn/Workspace.java @@ -19,9 +19,11 @@ import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.function.UnaryOperator; public final class Workspace { @@ -41,6 +43,7 @@ interface EdgeGuard { private final EdgeGuard guard; private final boolean replaying; private final Map> trees = new HashMap<>(); + private final Set guarded = new HashSet<>(); private final List memberChanges = new ArrayList<>(); private final List> slotChanges = new ArrayList<>(); @@ -142,9 +145,13 @@ public RootVector rebasedOnto(RootVector latest) { return roots; } + void guard(Slot slot) { + guarded.add(slot.id()); + } + boolean untouchedSince(RootVector latest, boolean includingReads) { return trees.entrySet().stream() - .filter(entry -> includingReads || changed(entry)) + .filter(entry -> includingReads || guarded.contains(entry.getKey()) || changed(entry)) .allMatch(entry -> Ref.same(base.get(entry.getKey()), latest.get(entry.getKey()))); } diff --git a/engine/src/test/java/io/hstore/engine/txn/PreconditionTest.java b/engine/src/test/java/io/hstore/engine/txn/PreconditionTest.java new file mode 100644 index 0000000..ca2a088 --- /dev/null +++ b/engine/src/test/java/io/hstore/engine/txn/PreconditionTest.java @@ -0,0 +1,87 @@ +// SPDX-FileCopyrightText: MetaCognition India Pvt Limited +// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 + +package io.hstore.engine.txn; + +import io.hstore.engine.EngineOptions; +import io.hstore.engine.HStoreException; +import io.hstore.engine.StorageEngine; +import io.hstore.engine.catalog.Slot; +import io.hstore.engine.page.ByteCursor; +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 org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.nio.file.Path; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class PreconditionTest { + + private static final Slot GUARDED = Slot.primary(120, "guarded", counters(240)); + private static final Slot WRITTEN = Slot.primary(121, "written", counters(241)); + + @TempDir + Path directory; + + private static TreeSchema counters(int id) { + return new TreeSchema<>(id, "counters-" + id, FingerprintMode.SET, + ValueCodec.rows(ByteCursor::signedVarLongSize, ByteCursor::putSignedVarLong, ByteCursor::getSignedVarLong), + (key, value, into) -> into.entry(key, Hashing.of(key, value))); + } + + private StorageEngine open() { + return StorageEngine.open(directory, EngineOptions.defaults().withPageSize(1024).withPagesPerSegment(256) + .withExtensions(List.of(GUARDED, WRITTEN), List.of())); + } + + private static void put(StorageEngine engine, Slot slot, long key, long value) { + engine.write(txn -> { + txn.put(slot, key, value); + return null; + }); + } + + @Test + void aPreconditionThatChangesNothingIsStillCheckedAtCommit() { + try (StorageEngine engine = open()) { + put(engine, GUARDED, 1, 1); + Transaction guarded = engine.begin(); + guarded.merge(GUARDED, 1, current -> current.equals(Optional.of(1L)), current -> current); + guarded.put(WRITTEN, 1, 1L); + put(engine, GUARDED, 1, 2); + assertThrows(HStoreException.Conflict.class, guarded::commit); + + Transaction unaffected = engine.begin(); + unaffected.merge(GUARDED, 1, current -> current.equals(Optional.of(2L)), current -> current); + unaffected.put(WRITTEN, 2, 1L); + put(engine, GUARDED, 5, 5); + assertEquals(CommitResult.Outcome.REBASED, unaffected.commit().outcome()); + + Transaction untouched = engine.begin(); + untouched.merge(GUARDED, 1, current -> current.equals(Optional.of(2L)), current -> current); + untouched.put(WRITTEN, 3, 1L); + put(engine, WRITTEN, 9, 9); + assertEquals(CommitResult.Outcome.REBASED, untouched.commit().outcome()); + } + } + + @Test + void aWriteOfTheSameValueStillPreventsALostUpdate() { + try (StorageEngine engine = open()) { + put(engine, GUARDED, 1, 1); + Transaction same = engine.begin(); + same.put(GUARDED, 1, 1L); + same.put(WRITTEN, 1, 1L); + put(engine, GUARDED, 1, 7); + assertThrows(HStoreException.Conflict.class, same::commit); + } + } +} 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..1e5003f 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; @@ -53,9 +56,13 @@ T exclusively(Function work) { 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::expire, period, period, TimeUnit.MILLISECONDS); } Entry open(Session session) { @@ -90,7 +97,7 @@ int size() { return entries.size(); } - private void expire() { + 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 +108,7 @@ private void expire() { @Override public void close() { + sweeper.shutdownNow(); 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 71eb0a1..ed087aa 100644 --- a/server/src/main/java/io/hstore/server/studio/Studio.java +++ b/server/src/main/java/io/hstore/server/studio/Studio.java @@ -310,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); 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")); + } + } + } +} From 157da6d02595586f29230bd69342010eca7906d2 Mon Sep 17 00:00:00 2001 From: venkat1701 Date: Tue, 27 Oct 2026 16:32:41 +0530 Subject: [PATCH 4/5] Check the admin rule after every user change and tighten session counts - CREATE, ALTER and DROP USER all check that a never-expiring global admin remains, and guard the ones that do, so a create racing the drop of the last admin cannot commit. - ALTER USER always guards the user's record, even when it restates the current role. - Signing in again gives back the old count before taking the new one, so a user recreated in another tenant is counted there. - A closed session runs nothing; closing always releases its count. - The connections quota key ignores case. - A tenant admin cannot set another admin's password. - The Studio sweeper survives failures and does not interrupt writes. --- .../java/io/hstore/db/HypergraphDatabase.java | 7 ++ .../java/io/hstore/db/query/Executor.java | 40 +++++----- .../main/java/io/hstore/db/query/Session.java | 35 ++++++--- .../java/io/hstore/db/security/Security.java | 22 +++--- .../java/io/hstore/db/PermissionsTest.java | 77 +++++++++++++++++++ docs/database/security.md | 33 ++++---- .../io/hstore/server/studio/Sessions.java | 27 +++++-- 7 files changed, 182 insertions(+), 59 deletions(-) diff --git a/database/src/main/java/io/hstore/db/HypergraphDatabase.java b/database/src/main/java/io/hstore/db/HypergraphDatabase.java index 6f5f9f6..dea898b 100644 --- a/database/src/main/java/io/hstore/db/HypergraphDatabase.java +++ b/database/src/main/java/io/hstore/db/HypergraphDatabase.java @@ -153,6 +153,13 @@ public boolean holds(Principal principal, Access.Privilege capability) { && 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); 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 b33e449..733fdc2 100644 --- a/database/src/main/java/io/hstore/db/query/Executor.java +++ b/database/src/main/java/io/hstore/db/query/Executor.java @@ -136,7 +136,7 @@ QueryResult run(Statement statement) { } case Statement.CreateTenant(String name, Map quota) -> write(writer -> { Map limits = new LinkedHashMap<>(quota); - Long connections = limits.remove("connections"); + 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); @@ -146,7 +146,7 @@ QueryResult run(Statement statement) { case Statement.AlterTenant(String name, Map quota) -> write(writer -> { Security.Tenant tenant = Security.requireTenant(writer.engineView(), name); Map limits = new LinkedHashMap<>(quota); - Long connections = limits.remove("connections"); + Long connections = removeConnections(limits); Security.setQuota(writer.transaction(), tenant, tenant.quota().with(limits)); if (connections != null) { Access.setTenantConnectionLimit(writer.transaction(), tenant.id(), connections); @@ -155,16 +155,13 @@ QueryResult run(Statement statement) { }); case Statement.CreateUser(String name, String password, Optional tenant, var role, var validUntil, var connectionLimit) -> write(writer -> { - int home = governedTenant(writer, tenant); - if (home == Principal.DEFAULT_TENANT && role == Role.ADMIN && validUntil.isPresent() && validUntil.getAsLong() != Long.MAX_VALUE) { - Security.keepAnotherGlobalAdmin(writer.transaction(), name); - } - Security.createUser(writer.transaction(), name, password, home, role); + 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()); return QueryResult.message("created user " + name + " with role " + role); }); case Statement.AlterUser alter -> write(writer -> alterUser(writer, alter)); @@ -173,10 +170,8 @@ QueryResult run(Statement statement) { 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"); } - if (Security.globalAdmin(user) && Security.users(writer.engineView()).size() > 1) { - Security.keepAnotherGlobalAdmin(writer.transaction(), name); - } Security.dropUser(writer.transaction(), name); + Security.keepAGlobalAdmin(writer.transaction()); return QueryResult.message("dropped user " + name); }); case Statement.Grant(Access.Privilege privilege, Optional type, String name) -> write(writer -> { @@ -427,18 +422,25 @@ private Security.User governedUser(Reader reader, String name) { .orElseThrow(() -> HStoreException.invalid("unknown user '" + name + "'")); } + 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(); + 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"); - } - boolean demoted = alter.role().filter(role -> role != Role.ADMIN).isPresent(); - boolean expires = alter.validUntil().isPresent() && alter.validUntil().getAsLong() != Long.MAX_VALUE; - if (Security.globalAdmin(user) && (demoted || expires)) { - Security.keepAnotherGlobalAdmin(writer.transaction(), user.name()); + + "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); @@ -449,15 +451,13 @@ private QueryResult alterUser(Writer writer, Statement.AlterUser alter) { changes.add("password"); }); if (alter.validUntil().isPresent() || alter.connectionLimit().isPresent()) { - if (alter.role().isEmpty() && alter.password().isEmpty()) { - Security.keepUnchanged(writer.transaction(), user); - } 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()); return QueryResult.message(changes.isEmpty() ? "nothing changed for " + user.name() : "changed " + String.join(", ", changes) + " of " + user.name()); } 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 ef25858..dbc582c 100644 --- a/database/src/main/java/io/hstore/db/query/Session.java +++ b/database/src/main/java/io/hstore/db/query/Session.java @@ -44,6 +44,7 @@ public final class Session implements AutoCloseable { 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) { @@ -145,11 +146,14 @@ private void requireCanAuthenticate() { } private void signIn(Principal found) { - if (counted.filter(principal -> principal.user().equals(found.user())).isEmpty()) { + counted.ifPresent(database::leave); + try { database.enter(found); - counted.ifPresent(database::leave); - counted = Optional.of(found); + } catch (RuntimeException refused) { + counted.ifPresent(database::rejoin); + throw refused; } + counted = Optional.of(found); stale = false; principal = found; authenticated = true; @@ -203,8 +207,11 @@ 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(); + release(); stale = true; throw HStoreException.invalid("user " + principal.user() + " was dropped, or its password or role changed; authenticate again"); } @@ -364,11 +371,19 @@ public boolean inTransaction() { @Override public void close() { - transaction.ifPresent(Transaction::abort); - transaction = Optional.empty(); - scram.ifPresent(pending -> pending.reservation().failed()); - scram = Optional.empty(); - counted.ifPresent(database::leave); - counted = 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/Security.java b/database/src/main/java/io/hstore/db/security/Security.java index e79e403..b950046 100644 --- a/database/src/main/java/io/hstore/db/security/Security.java +++ b/database/src/main/java/io/hstore/db/security/Security.java @@ -254,19 +254,21 @@ public static boolean globalAdmin(User user) { return user.tenant() == Principal.DEFAULT_TENANT && user.role() == Role.ADMIN; } - public static void keepAnotherGlobalAdmin(Transaction txn, String leaving) { - List others = users(txn).stream() - .filter(user -> globalAdmin(user) && !user.name().equals(leaving) && neverExpires(Access.options(txn, user.name()))) - .toList(); - if (others.isEmpty()) { + public static void keepAGlobalAdmin(Transaction txn) { + List all = users(txn); + if (all.isEmpty()) { + 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 another one first"); + + "create or restore one first"); } - for (User other : others) { - txn.merge(USERS, userKey(other.name()), current -> current.filter(user -> user.name().equals(other.name()) && globalAdmin(user)).isPresent(), + 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(other.name()), current -> neverExpires(current.filter(options -> options.user().equals(other.name())) - .orElseGet(() -> Access.Options.defaults(other.name()))), 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); } } diff --git a/database/src/test/java/io/hstore/db/PermissionsTest.java b/database/src/test/java/io/hstore/db/PermissionsTest.java index d89d8cd..1c98780 100644 --- a/database/src/test/java/io/hstore/db/PermissionsTest.java +++ b/database/src/test/java/io/hstore/db/PermissionsTest.java @@ -326,6 +326,83 @@ void theFirstGlobalAdminCannotExpire(@TempDir Path elsewhere) { } } + @Test + void everyChangeLeavesAGlobalAdmin(@TempDir Path elsewhere) { + try (HypergraphDatabase fresh = HypergraphDatabase.open(elsewhere); Session system = new Session(fresh)) { + assertThrows(HStoreException.InvalidSchema.class, () -> system.execute("CREATE USER bob PASSWORD 'bob-password' ROLE reader")); + system.execute("CREATE USER root PASSWORD 'root-password' ROLE admin"); + 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"); diff --git a/docs/database/security.md b/docs/database/security.md index ebfb157..9276987 100644 --- a/docs/database/security.md +++ b/docs/database/security.md @@ -85,31 +85,38 @@ DDL stay with global admins. - `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 as the same user keeps its one count. A Studio session gives its count back when it closes, after + 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). It counts every signed-in session of the tenant's users + `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 races `DROP USER` fails instead of passing to the next user created with that name. + 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, so a tenant admin cannot lift limits put on itself or on another admin of its tenant. - -The default tenant must keep an ADMIN whose password never expires. A change that would leave none (dropping one -while other users exist, giving it another role, giving its password any `VALID UNTIL` other than `'infinity'`, -or creating the first global admin with one) is refused: any expiry date eventually passes, so checking only -that the 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 other such admin, so two -concurrent transactions that each remove or expire a different admin cannot both commit, whichever records they -write. Should every admin still be lost (for example to a forgotten password), `hstore shell` on +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. + +While any user exists, the default tenant must keep an ADMIN whose password never expires. 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'`, or creating +users in a database that has no such admin (so the first user must be one). 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 @@ -199,7 +206,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/server/src/main/java/io/hstore/server/studio/Sessions.java b/server/src/main/java/io/hstore/server/studio/Sessions.java index 1e5003f..7fc9347 100644 --- a/server/src/main/java/io/hstore/server/studio/Sessions.java +++ b/server/src/main/java/io/hstore/server/studio/Sessions.java @@ -50,6 +50,7 @@ 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; @@ -62,7 +63,7 @@ T exclusively(Function work) { Sessions(Duration idle) { this.idleNanos = idle.toNanos(); long period = Math.clamp(idle.toMillis() / 4, 10, Duration.ofMinutes(1).toMillis()); - sweeper.scheduleWithFixedDelay(this::expire, period, period, TimeUnit.MILLISECONDS); + sweeper.scheduleWithFixedDelay(this::sweep, period, period, TimeUnit.MILLISECONDS); } Entry open(Session session) { @@ -87,16 +88,30 @@ 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 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() @@ -108,7 +123,7 @@ void expire() { @Override public void close() { - sweeper.shutdownNow(); + sweeper.shutdown(); entries.keySet().stream().toList().forEach(this::close); } } From 4ce0e117b7097a8f70c501b5bbb2b6f5041c61d2 Mon Sep 17 00:00:00 2001 From: venkat1701 Date: Tue, 27 Oct 2026 16:41:12 +0530 Subject: [PATCH 5/5] Apply the admin rule once the database has a global admin Requiring every database to start with a global admin refused embedded setups that create tenant users first. The rule now applies as soon as a global admin exists, which still refuses dropping the last one and creating other users in the same or a concurrent transaction. --- .../src/main/java/io/hstore/db/query/Executor.java | 10 +++++++--- .../src/main/java/io/hstore/db/security/Security.java | 8 ++++++-- .../src/test/java/io/hstore/db/PermissionsTest.java | 3 ++- docs/database/security.md | 9 +++++---- 4 files changed, 20 insertions(+), 10 deletions(-) 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 733fdc2..09e35d8 100644 --- a/database/src/main/java/io/hstore/db/query/Executor.java +++ b/database/src/main/java/io/hstore/db/query/Executor.java @@ -161,7 +161,7 @@ QueryResult run(Statement statement) { Access.setOptions(writer.transaction(), new Access.Options(name, validUntil.orElse(Long.MAX_VALUE), (int) connectionLimit.orElse(-1))); } - Security.keepAGlobalAdmin(writer.transaction()); + Security.keepAGlobalAdmin(writer.transaction(), hadGlobalAdmin()); return QueryResult.message("created user " + name + " with role " + role); }); case Statement.AlterUser alter -> write(writer -> alterUser(writer, alter)); @@ -171,7 +171,7 @@ QueryResult run(Statement statement) { throw HStoreException.invalid("cannot drop the last user: the server would stop asking for credentials"); } Security.dropUser(writer.transaction(), name); - Security.keepAGlobalAdmin(writer.transaction()); + Security.keepAGlobalAdmin(writer.transaction(), hadGlobalAdmin()); return QueryResult.message("dropped user " + name); }); case Statement.Grant(Access.Privilege privilege, Optional type, String name) -> write(writer -> { @@ -422,6 +422,10 @@ private Security.User governedUser(Reader reader, String name) { .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())) { @@ -457,7 +461,7 @@ private QueryResult alterUser(Writer writer, Statement.AlterUser alter) { alter.validUntil().ifPresent(_ -> changes.add("password expiry")); alter.connectionLimit().ifPresent(_ -> changes.add("connection limit")); } - Security.keepAGlobalAdmin(writer.transaction()); + Security.keepAGlobalAdmin(writer.transaction(), hadGlobalAdmin()); return QueryResult.message(changes.isEmpty() ? "nothing changed for " + user.name() : "changed " + String.join(", ", changes) + " of " + user.name()); } 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 b950046..3662075 100644 --- a/database/src/main/java/io/hstore/db/security/Security.java +++ b/database/src/main/java/io/hstore/db/security/Security.java @@ -254,9 +254,13 @@ public static boolean globalAdmin(User user) { return user.tenant() == Principal.DEFAULT_TENANT && user.role() == Role.ADMIN; } - public static void keepAGlobalAdmin(Transaction txn) { + 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()) { + 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(); diff --git a/database/src/test/java/io/hstore/db/PermissionsTest.java b/database/src/test/java/io/hstore/db/PermissionsTest.java index 1c98780..45e5e23 100644 --- a/database/src/test/java/io/hstore/db/PermissionsTest.java +++ b/database/src/test/java/io/hstore/db/PermissionsTest.java @@ -329,8 +329,9 @@ void theFirstGlobalAdminCannotExpire(@TempDir Path elsewhere) { @Test void everyChangeLeavesAGlobalAdmin(@TempDir Path elsewhere) { try (HypergraphDatabase fresh = HypergraphDatabase.open(elsewhere); Session system = new Session(fresh)) { - assertThrows(HStoreException.InvalidSchema.class, () -> system.execute("CREATE USER bob PASSWORD 'bob-password' ROLE reader")); + 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")); diff --git a/docs/database/security.md b/docs/database/security.md index 9276987..125fb1a 100644 --- a/docs/database/security.md +++ b/docs/database/security.md @@ -108,10 +108,11 @@ again, so the new role applies at once. Only a global admin may change the role, 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. -While any user exists, the default tenant must keep an ADMIN whose password never expires. 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'`, or creating -users in a database that has no such admin (so the first user must be one). Any expiry date eventually passes, +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