OrientDB - 如何 - 使用ODocument记录级别安全性

时间:2015-05-28 19:39:03

标签: java orientdb

我正在努力让Record Level Security使用OrientDB的文档数据库(Java)API。我喜欢将数据库中某些记录的访问权限限制为特定角色的想法;但是,我在文档中找不到如何做到这一点。我是OrientDB的新手,所以我确定我只是遗漏了一些东西。

这是我到目前为止所拥有的。

// Create database.
ODatabaseDocumentTx db = new ODatabaseDocumentTx(path);
db.create();

// Create role with no permissions.
db.command(new OCommandSQL("INSERT INTO orole SET name = 'foobar', mode = 0;")).execute();

// Create a user with the new role.
OSecurity sm = db.getMetadata().getSecurity();
OUser user = sm.createUser("user", "user", "foobar");
ORole foobarRole = sm.getRole("foobar");

// Insert 2 records, one restricted, one is not.
OClass restricted = db.getMetadata().getSchema().getClass("ORestricted");
OClass docClass = db.getMetadata().getSchema().getOrCreateClass(TABLE_NAME, restricted);

ODocument doc1 = new ODocument(docClass);
ODocument doc2 = new ODocument(docClass);

// The restricted record...
doc1.field("name", TABLE_NAME);
doc1.field("Id", 1, OType.INTEGER);
doc1.field("Message", "Hello 1", OType.STRING);
doc1.save();

// The unrestricted record...
doc2.field("name", TABLE_NAME);
doc2.field("Id", 2, OType.INTEGER);
doc2.field("Message", "Hello 2", OType.STRING);
doc2.save();

// Allow "user" with "foobar" role to read record doc2.
String sql = String.format(
  "UPDATE %s ADD _allowRead = %s",
  doc2.getIdentity().toString(),
  foobarRole.getDocument().getIdentity().toString());

db.command(new OCommandSQL(sql)).execute();

// Give foobar role permission to read from table.
db.command(new OCommandSQL(String.format("GRANT READ ON database.class.%s TO foobar", TABLE_NAME))).execute();

db.close();

// Open connection for "user".
ODatabaseDocumentTx userDb = new ODatabaseDocumentTx(path);
userDb.open("user", "user");

// Here I would expect to see the message from doc2 but not doc1.
// Nothing gets printed...
for (ODocument odoc : userDb.browseClass(TABLE_NAME))
{
  System.out.println(odoc.field("Message"));
}

文档说明在READ期间会跳过角色无法访问的记录。就我而言,用户无法阅读任何内容。

有关如何使这种行为有效的任何想法?

1 个答案:

答案 0 :(得分:2)

经过几天的摆弄,这就是我开始工作的方式。

// Create database.
ODatabaseDocumentTx db = new ODatabaseDocumentTx(path);
db.create();

// Create users/roles.
OSecurity sm = db.getMetadata().getSecurity();

restrictedRole = sm.createRole("foobar", OSecurityRole.ALLOW_MODES.DENY_ALL_BUT);
restrictedRole.addRule(ORule.ResourceGeneric.CLASS, TABLE_NAME, ORole.PERMISSION_READ);
restrictedRole.addRule(ORule.ResourceGeneric.DATABASE, TABLE_NAME, ORole.PERMISSION_READ);
restrictedRole.addRule(ORule.ResourceGeneric.CLUSTER, TABLE_NAME, ORole.PERMISSION_READ);
restrictedRole.save();
restrictedRole.reload();

admin = sm.getUser("admin");
user2 = sm.createUser("user2", "user2", "foobar");

// Insert 2 records, one restricted, one is not.
OClass restricted = db.getMetadata().getSchema().getClass("ORestricted");
OClass docClass = db.getMetadata().getSchema().getOrCreateClass(TABLE_NAME, restricted);

ODocument doc1 = new ODocument(docClass);
ODocument doc2 = new ODocument(docClass);

// The restricted record...
doc1.field("name", TABLE_NAME);
doc1.field("Id", 1, OType.INTEGER);
doc1.field("Message", "Hello 1", OType.STRING);
doc1.save();

// The unrestricted record...
doc2.field("name", TABLE_NAME);
doc2.field("Id", 2, OType.INTEGER);
doc2.field("Message", "Hello 2", OType.STRING);
db.getMetadata().getSecurity().allowRole(doc2, OSecurityShared.ALLOW_READ_FIELD, "foobar");
doc2.save();

//
// PRINT RESTRICTED
//
db.close();
db = new ODatabaseDocumentTx(path);
db.open("user2", "user2");

// Prints:
// Hello 2
for (ODocument doc : db.browseClass(TABLE_NAME))
{
  System.out.println(doc.field("Message"));
}

//
// PRINT ADMIN
//
db.close();
db = new ODatabaseDocumentTx(path);
db.open("admin", "admin");

// Prints:
// Hello 1
// Hello 2
for (ODocument doc : db.browseClass(TABLE_NAME))
{
  System.out.println(doc.field("Message"));
}

该角色的addRule方法允许具有该角色的用户从该类读取。第二个文档有一个额外的行allowRole,允许具有该角色的用户阅读该特定文档,这是故意排除在第一个文档之外的。结果是具有“foobar”角色的用户只在阅读时获得第二个文档。 admin角色可以读取这两个文档。

另外,请注意“ORestricted”中的文档类继承。

OClass restricted = db.getMetadata().getSchema().getClass("ORestricted");
OClass docClass = db.getMetadata().getSchema().getOrCreateClass(TABLE_NAME, restricted);

文档需要从ORestricted继承才能使记录级安全性起作用。就个人而言,我没有找到任何解释如何在任何地方的代码中执行此操作(可能我没有找到正确的位置)并且Java中没有直接继承自ORestricted类的文档类。因此,我们必须使用模式类元数据告诉驱动程序我们创建的文档需要从ORestricted继承。