如何允许访问Hasura中角色的所有查询?



我的Hasura实例被配置为接受JWT令牌并使用它们进行授权。它有40张表

我想要一个"全访问"风格的角色。此角色可以发出任何查询—没有过滤,没有拒绝操作。他们不会是管理员。

我如何告诉Hasura这个角色应该可以访问每个查询?似乎我需要手动(或通过脚本)授予访问每个表。

我看到的问题:

  1. 手动将花费很长时间并且容易出错
  2. 很难确认权限是否完整和准确(审计)
  3. 如果我们添加一个新表,它将默认为不可访问。这是不希望的
  4. 脚本会比较复杂。它可能需要模式自省、聚合处理等。当脚本应该运行时将不清楚

不一样,但我希望从firestore安全规则中摘录如下:

// Allow read/write access on all documents to any user signed in to the application
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}

在出现更好的解决方案之前,下面是我的节点脚本。使用Hasura 1.3.3。它应该将所有表的所有权限授予超级用户角色。假设hasura上还没有启用安全功能,因此您可能需要传递管理密钥作为头来验证是否启用了安全功能。

/**
* Export metadata from hasura
* Loop over all tables
* Grant all actions to these tables to all users
*/
const fetch = require('node-fetch');
const hasuraUrl = 'http://localhost:18080/v1/query';
async function main() {
const metadata = await getMetadata()
.catch( e => console.error(`Unable to fetch schema. Cannot grant permissions.`, e) ); 
metadata.tables.map( table => {
// Grant all actions to a superuser-type role
const fetches = grantAllActions( table );
return fetches;
});
}
async function getMetadata() {
const body = {
type: 'export_metadata',
args: {}
}
return fetch( hasuraUrl, {
method: 'post',
headers: { 
'Content-Type': 'application/json'
},
body: JSON.stringify( body )
})
.then( res => res.json() );
}
async function grantAllActions( tableDef ) {
// Check: {}  means a "TrueExp". It will always pass the check, regardless of row contents
const grantInsert = {
type: 'create_insert_permission',
args: {
table: {
name: tableDef.table.name,
schema: 'myschema'
},
role: 'my-superuser',
permission: {
check: {},
filter: {},
columns: '*'
}
}
}
// Use the above as a template and modify as needed to satisfy the required keys on different grants
const grantUpdate = JSON.parse( JSON.stringify( grantInsert ));
const grantDelete = JSON.parse( JSON.stringify( grantInsert ));
const grantSelect = JSON.parse( JSON.stringify( grantInsert ));
grantUpdate.type = 'create_update_permission';

grantDelete.type = 'create_delete_permission';
delete grantDelete.args.permission.columns;

grantSelect.type = 'create_select_permission';
grantSelect.args.permission.allow_aggregations = true;
const $fetches = [grantSelect, grantUpdate, grantDelete, grantInsert].map( grantObject => {
return fetch( hasuraUrl, {
method: 'post',
headers: {
'Content-Type': 'application/json',
'X-Hasura-Role': 'admin'
},
body: JSON.stringify( grantObject )
})
.then( res => console.log( res ))      
.catch( e => console.warn( `Error during superuser grant. table: ${tableDef.table.name}`, e ) );

});
return Promise.all($fetches);
}
main();

最新更新