Skip to content
Merged
49 changes: 49 additions & 0 deletions database/src/main/java/io/hstore/db/HypergraphDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,6 +61,8 @@ public final class HypergraphDatabase implements AutoCloseable {
private final Statistics statistics;
private final MaterializedViews views;
private final SignIns signIns = new SignIns();
private final Map<String, Integer> connections = new ConcurrentHashMap<>();
private final Map<Integer, Integer> tenantConnections = new ConcurrentHashMap<>();
private final byte[] scramSecret;
private final Map<String, Long> legacyWarnings = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -150,6 +153,52 @@ public SignIns signIns() {
return signIns;
}

public void enter(Principal principal) {
Access.Options limits = read(reader -> Access.options(reader.engineView(), principal.user()));
int tenantLimit = read(reader -> Access.tenantConnectionLimit(reader.engineView(), principal.home()));
synchronized (connections) {
if (!principal.global()) {
if (limits.connectionLimit() >= 0 && connections(principal.user()) >= limits.connectionLimit()) {
throw HStoreException.limit("too many connections for user " + principal.user() + " (connection limit "
+ limits.connectionLimit() + ")");
}
if (tenantLimit >= 0 && tenantConnections(principal.home()) >= tenantLimit) {
throw HStoreException.limit("too many connections for tenant #" + principal.home() + " (connection limit "
+ tenantLimit + ")");
}
}
connections.merge(principal.user(), 1, Integer::sum);
tenantConnections.merge(principal.home(), 1, Integer::sum);
}
}

public boolean holds(Principal principal, Access.Privilege capability) {
return principal.global() || principal.home() == Principal.DEFAULT_TENANT
&& read(reader -> Access.holds(reader.engineView(), principal.user(), capability));
}

public void rejoin(Principal principal) {
synchronized (connections) {
connections.merge(principal.user(), 1, Integer::sum);
tenantConnections.merge(principal.home(), 1, Integer::sum);
}
}

public void leave(Principal principal) {
synchronized (connections) {
connections.computeIfPresent(principal.user(), (_, count) -> count <= 1 ? null : count - 1);
tenantConnections.computeIfPresent(principal.home(), (_, count) -> count <= 1 ? null : count - 1);
}
}

public int tenantConnections(int tenant) {
return tenantConnections.getOrDefault(tenant, 0);
}

public int connections(String user) {
return connections.getOrDefault(user, 0);
}

public static List<Slot<?>> extensionSlots() {
return Stream.of(SchemaSlots.ALL, PropertySlots.ALL, List.<Slot<?>>of(Embedding.EMBEDDINGS),
Provenance.SLOTS, StateBindings.SLOTS, Signals.SLOTS, MaterializedViews.SLOTS, Security.SLOTS)
Expand Down
68 changes: 53 additions & 15 deletions database/src/main/java/io/hstore/db/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,8 +28,10 @@
import io.hstore.engine.tree.Entry;
import io.hstore.engine.tree.Tree;
import io.hstore.engine.txn.IncidentEdge;
import io.hstore.engine.txn.Snapshot;
import io.hstore.engine.txn.View;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -48,6 +51,8 @@ public sealed class Reader permits Writer {
private final Principal principal;

private Boolean onlyDefaultTenant;
private final Map<Integer, Optional<Access.Privilege>> grantedTypes = new HashMap<>();
private long grantsGeneration = -1;

Reader(HypergraphDatabase database, View view, Principal principal) {
this.database = database;
Expand All @@ -67,7 +72,7 @@ private boolean onlyDefaultTenant() {
Boolean known = onlyDefaultTenant;
if (known == null) {
try (View.Unrecorded _ = view.unrecorded()) {
known = tenant() == Principal.DEFAULT_TENANT
known = principal.role().readsEveryType() && tenant() == Principal.DEFAULT_TENANT
&& view.scan(EngineSlots.TYPES).last().map(entry -> entry.key() >>> 32 == Principal.DEFAULT_TENANT).orElse(true);
}
onlyDefaultTenant = known;
Expand All @@ -76,7 +81,40 @@ private boolean onlyDefaultTenant() {
}

public boolean visible(long atom) {
return view.atom(atom).map(record -> record.tenant() == tenant()).orElse(false);
return view.atom(atom).map(this::admits).orElse(false);
}

boolean admits(AtomRecord record) {
return record.tenant() == tenant() && readable(record.type());
}

public boolean readable(int type) {
return principal.role().readsEveryType() || privilege(type).isPresent();
}

public boolean writable(int type) {
return principal.role().writesEveryType() || privilege(type).filter(found -> found == Access.Privilege.WRITE).isPresent();
}

private Optional<Access.Privilege> privilege(int type) {
long current = database.engine().transactions().current().id();
if (current != grantsGeneration) {
grantedTypes.clear();
grantsGeneration = current;
}
return grantedTypes.computeIfAbsent(type, found -> {
try (Snapshot latest = database.engine().snapshot()) {
return Access.onType(latest, principal.user(), found);
}
});
}

public TypeDef requireReadableType(String name) {
TypeDef type = requireType(name);
if (!readable(type.id())) {
throw HStoreException.invalid("user " + principal.user() + " has no READ privilege on type " + type.name());
}
return type;
}

public boolean owns(long atom) {
Expand All @@ -92,21 +130,21 @@ public View.Unrecorded unrecorded() {
}

public boolean isEdge(long atom) {
return view.atom(atom).map(record -> record.tenant() == tenant() && record instanceof AtomRecord.EdgeRecord).orElse(false);
return view.atom(atom).map(record -> admits(record) && record instanceof AtomRecord.EdgeRecord).orElse(false);
}

public Optional<Hyperedge> 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> 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<IncidentEdge> incidentEdges(long atom) {
Expand Down Expand Up @@ -148,7 +186,7 @@ public long generation() {

public Optional<Atom> 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));
}

Expand All @@ -165,27 +203,27 @@ 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) {
return find(type, key).orElseThrow(() -> HStoreException.invalid("no " + type + " named '" + 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) {
Expand Down Expand Up @@ -250,7 +288,7 @@ public Optional<Json> 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");
Expand Down Expand Up @@ -337,7 +375,7 @@ public LongStream lookup(String type, String property, Value value) {
}

public LongStream range(String type, String property, Optional<Value> low, Optional<Value> high, boolean lowInclusive, boolean highInclusive) {
TypeDef definition = requireType(type);
TypeDef definition = requireReadableType(type);
Predicate<Value> accept = candidate -> low.map(bound -> comparable(candidate, bound)
&& (lowInclusive ? candidate.compareTo(bound) >= 0 : candidate.compareTo(bound) > 0)).orElse(true)
&& high.map(bound -> comparable(candidate, bound)
Expand All @@ -355,7 +393,7 @@ public LongStream range(String type, String property, Optional<Value> 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();
}
Expand Down
53 changes: 45 additions & 8 deletions database/src/main/java/io/hstore/db/Writer.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public long node(String type, String key) {
public long node(String type, String key, Map<String, ?> 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);
Expand All @@ -128,6 +129,7 @@ public long edge(String type, Map<String, ?> 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);
Expand Down Expand Up @@ -170,13 +172,13 @@ public void append(long edge, long member, MemberSpec spec) {

public void remove(long edge, long member) {
principal().requireWrite();
require(edge);
requireWritable(edge);
txn.remove(edge, member);
}

public void removeAt(long edge, long index) {
principal().requireWrite();
require(edge);
requireWritable(edge);
txn.removeAt(edge, index);
}

Expand All @@ -185,6 +187,7 @@ public void load(long edge, List<Long> 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);
Expand All @@ -196,6 +199,20 @@ public void load(long edge, long[] members, MemberSpec spec) {
txn.load(edge, Arrays.asList(incidences));
}

public void loadIncidences(long edge, List<Incidence> 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);
Expand All @@ -218,7 +235,7 @@ private Incidence incidence(long edge, long member, MemberSpec spec) {

private void requireMembership(long edge, MemberSpec spec) {
principal().requireWrite();
Atom container = require(edge);
Atom container = requireWritable(edge);
if (!container.isEdge()) {
throw HStoreException.invalid("atom " + edge + " is not a hyperedge");
}
Expand All @@ -229,6 +246,18 @@ private void requireMembership(long edge, MemberSpec spec) {
}
}

public Atom requireWritable(long atom) {
Atom target = require(atom);
requireWritableType(target.type());
return target;
}

private void requireWritableType(TypeDef type) {
if (!writable(type.id())) {
throw HStoreException.invalid("user " + principal().user() + " has no WRITE privilege on type " + type.name());
}
}

private void requireVisible(long member) {
if (!visible(member)) {
throw HStoreException.invalid("member atom " + member + " does not exist");
Expand All @@ -241,7 +270,7 @@ public void set(long atom, String name, Value value) {

public void set(long atom, String name, Value value, Validity validity) {
principal().requireWrite();
Atom target = require(atom);
Atom target = requireWritable(atom);
Value stored = target.type().property(name).map(def -> Values.coerce(value, def.type())).orElse(value);
stored = externalizeLargeText(stored);
Property property = new Property(stored, validity.from(), validity.to());
Expand All @@ -250,7 +279,7 @@ public void set(long atom, String name, Value value, Validity validity) {

public void unset(long atom, String name) {
principal().requireWrite();
require(atom);
requireWritable(atom);
OptionalInt key = schema().existingPropertyKey(name);
if (key.isPresent()) {
updateBag(atom, key.getAsInt(), bag -> bag.without(key.getAsInt()));
Expand Down Expand Up @@ -288,7 +317,7 @@ long writePayload(byte[] bytes) {

public void document(long atom, Json json) {
principal().requireWrite();
require(atom);
requireWritable(atom);
byte[] bytes = json.print().getBytes(StandardCharsets.UTF_8);
long ref = writePayload(bytes);
set(atom, Schema.DOCUMENT, new Value.Payload(ref, bytes.length, "application/json"));
Expand All @@ -301,7 +330,7 @@ public void embed(long atom, String text) {

public void embed(long atom, String model, int version, float[] vector) {
principal().requireWrite();
require(atom);
requireWritable(atom);
SemanticPlane semantic = database.semantic();
int id = semantic.existingModel(model).orElseGet(() -> {
if (!model.equals(semantic.encoder().model())) {
Expand All @@ -318,7 +347,15 @@ public void embed(long atom, String model, int version, float[] vector) {

public void delete(long atom) {
principal().requireWrite();
Atom target = require(atom);
Atom target = requireWritable(atom);
if (!principal().role().writesEveryType()) {
engineView().incident(atom).forEach(incident -> engineView().atom(incident.edge())
.filter(record -> record.tenant() == tenant() && !writable(record.type()))
.ifPresent(record -> {
throw HStoreException.invalid("cannot delete atom " + atom + ": it is a member of a hyperedge that user "
+ principal().user() + " may not change");
}));
}
Security.charge(txn, tenant(), new Usage(-1, target.isEdge() ? -1 : 0, 0));
txn.delete(PropertySlots.PROPERTIES, atom);
txn.delete(Embedding.EMBEDDINGS, atom);
Expand Down
Loading
Loading