Skip to content

Commit 0538472

Browse files
author
david
committed
set versions of agile-idm-core to use pdp.
and also set version of agile-idm-entity
1 parent 9e68786 commit 0538472

File tree

4 files changed

+227
-59
lines changed

4 files changed

+227
-59
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
*policies.json
12
*node_modules*
23
*.svn*
34
*~

conf/agile-idm-core-conf.js

Lines changed: 112 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,122 @@
11
//{"target":{"type":"user"},"locks":[{"path":"hasId","args":["$owner"]}]
22
module.exports = {
33
"storage": {
4-
"dbName": "./database_"
4+
"dbName": "database_"
55
},
6-
"top_level_policy": {
7-
"policy": [
8-
/*
9-
always I have entity_id and entity_type available (both are the pk)
10-
user:{
11-
entity_id,
12-
entity_type,
13-
user_name,
14-
auth_type,
15-
role,
16-
*may have password
6+
"policies": {
7+
"dbName": "./policies.json",
8+
"create_entity_policy": [
9+
// actions of an actor are not restricted a priori
10+
{
11+
target: {
12+
type: "any"
13+
}
14+
}, {
15+
source: {
16+
type: "any"
17+
}
1718
}
18-
for all attributes:
19-
only owner can write
20-
everyone can read
21-
*/
22-
]
23-
},
24-
"attribute_level_policies": {
25-
"user": {
26-
"password": {
27-
"policy": [
28-
// only user with id === $owner can read
29-
// users with attribute role === admin can write, and user with id === $owner can write too.
30-
]
19+
],
20+
"top_level_policy": [
21+
// all properties can be read by everyone
22+
{
23+
target: {
24+
type: "any"
25+
}
3126
},
32-
"role": {
33-
"policy": [
34-
// only users with attribute role === admin can write to role
35-
// everyone can read
36-
],
27+
// all properties can only be changed by the owner of the entity
28+
{
29+
source: {
30+
type: "user"
31+
},
32+
locks: [{
33+
lock: "isOwner"
34+
}]
35+
}, {
36+
source: {
37+
type: "user"
38+
},
39+
locks: [{
40+
lock: "attrEq",
41+
args: ["role", "admin"]
42+
}]
3743
}
38-
},
39-
"sensor": {
40-
"credentials": {
41-
"policy": [
42-
// only user with id === $owner can read
43-
// only user with id === $owner can write
44+
],
45+
"attribute_level_policies": {
46+
"user": {
47+
"password": [
48+
// the property can only be read by the user itself
49+
{
50+
target: {
51+
type: "user"
52+
},
53+
locks: [{
54+
lock: "isOwner"
55+
}]
56+
},
57+
// the property can be set by the user itself and
58+
{
59+
source: {
60+
type: "user"
61+
},
62+
locks: [{
63+
lock: "isOwner"
64+
}]
65+
},
66+
// by all users with role admin
67+
{
68+
source: {
69+
type: "user"
70+
},
71+
locks: [{
72+
lock: "attrEq",
73+
args: ["role", "admin"]
74+
}]
75+
}
4476
],
45-
"rule": {
46-
"replace": "owner"
47-
}
77+
"role": [
78+
// can be read by everyone
79+
{
80+
target: {
81+
type: "any"
82+
}
83+
},
84+
// can only be changed by users with role admin
85+
{
86+
source: {
87+
type: "user"
88+
},
89+
locks: [{
90+
lock: "attrEq",
91+
args: ["role", "admin"]
92+
}]
93+
}
94+
]
95+
},
96+
"sensor": {
97+
"credentials": [
98+
// the property can only be read by the user itself
99+
{
100+
target: {
101+
type: "user"
102+
},
103+
locks: [{
104+
lock: "isOwner"
105+
}]
106+
},
107+
// the property can be set by the user itself and
108+
{
109+
source: {
110+
type: "user"
111+
},
112+
locks: [{
113+
lock: "isOwner"
114+
}]
115+
}
116+
]
48117
}
49-
}
50118

119+
}
51120
},
52121
"schema-validation": [{
53122
"id": "/sensor",
@@ -84,6 +153,9 @@ module.exports = {
84153
},
85154
"password": {
86155
"type": "string"
156+
},
157+
"role":{
158+
"type":"string"
87159
}
88160
},
89161
"required": ["user_name", "auth_type"]

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"test": "tests"
99
},
1010
"dependencies": {
11-
"agile-idm-core": "git+https://github.com/Agile-IoT/agile-idm-core",
12-
"agile-idm-entity-storage": "git+https://github.com/Agile-IoT/agile-idm-entity-storage",
11+
"agile-idm-core": "git+https://github.com/Agile-IoT/agile-idm-core#v1.0.0",
12+
"agile-idm-entity-storage": "git+https://github.com/Agile-IoT/agile-idm-entity-storage#v1.0.0",
1313
"body-parser": "1.x",
1414
"clone": "1.0.2",
1515
"command-line-args": "^3.0.3",

scripts/createUser.js

Lines changed: 112 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
var commandLineArgs = require('command-line-args');
22
var getUsage = require('command-line-usage');
3-
var Storage = require('agile-idm-entity-storage').Storage;
43
var ids = require('../lib/util/id');
4+
var IdmCore = require('agile-idm-core');
55

66
/*
77
Examples of usage:
88
To create a local user wit username bob and password secret:
9-
node createUser.js --username=bob --password=secret --auth=agile-local
9+
node createUser.js --username=bob --password=secret --auth=agile-local --role=admin
1010
To create a user wit username abc that will authenticate using github:
1111
node createUser.js --username=abc --auth=github
1212
node createUser.js --username=dp --auth=pam
@@ -15,8 +15,8 @@ Examples of usage:
1515
1616
*/
1717
var sections = [{
18-
header: 'AGILE IDM User Setup Script',
19-
content: 'Creates [italic]{users} which are administrators for IDM. This script is meant to be used during bootstrap of AGILE IDM'
18+
header: 'AGILE IDM User Setup Script (FOR ADMIN USERS MAINLY!!)',
19+
content: ' Creates [italic]{users} which are administrators for IDM. This script is meant to be used during bootstrap of AGILE IDM [undeline]{Be careful when using this script. It bypasses security checks (to enable the creation of the first user) It is meant to create only admin users. Afterwards admin users can create other users}'
2020
}, {
2121
header: 'User info',
2222
optionList: [{
@@ -34,15 +34,22 @@ var sections = [{
3434
alias: 'p',
3535
typeLabel: '[underline]{String}',
3636
description: 'This argument is the password used for the user, and it MUST be passed when auth is "agile-local".'
37+
},
38+
{
39+
name: 'role (optional)',
40+
alias: 'r',
41+
typeLabel: '[underline]{String}',
42+
description: 'This argument specifies the role of the user, by default this is set to admin".'
3743
}
3844

45+
3946
]
4047
}, {
4148
header: "Configuration info (optional)",
4249
optionList: [{
4350
name: "config",
4451
alias: "c",
45-
description: "location of the database. By default '../database_' executed relative to this script's path is used"
52+
description: "location of the agile-idm-core configuration file. By default it takes the value from the configuration file in ../conf/agile-idm-core-conf.js, which should be '../database_' normally. This path is interpreted as relative to the parent folder"
4653
}]
4754
}, {
4855
header: "Help",
@@ -73,35 +80,111 @@ var optionDefinitions = [{
7380
name: 'help',
7481
alias: 'h',
7582
type: String
76-
}, ];
83+
},
84+
{
85+
name: 'role',
86+
alias: 'r',
87+
type: String
88+
} ];
89+
90+
91+
var pepMockOk = {
92+
declassify: function (userInfo, entityInfo) {
93+
return new Promise(function (resolve, reject) {
94+
resolve(entityInfo);
95+
});
96+
},
97+
declassifyArray: function (userInfo, array) {
98+
return new Promise(function (resolve, reject) {
99+
resolve(array);
100+
});
101+
}
102+
};
103+
104+
var PdpMockOk = {
105+
canRead: function (userInfo, entityInfo) {
106+
return new Promise(function (resolve, reject) {
107+
resolve(entityInfo);
108+
});
109+
},
110+
canDelete: function (userInfo, entityInfo) {
111+
return new Promise(function (resolve, reject) {
112+
resolve(entityInfo);
113+
});
114+
},
115+
canReadArray: function (userInfo, entities) {
116+
return new Promise(function (resolve, reject) {
117+
//console.log('resolving with entities '+JSON.stringify(entities));
118+
resolve(entities);
119+
});
120+
},
121+
canWriteToAttribute: function (userInfo, entities, attributeName, attributeValue) {
122+
return new Promise(function (resolve, reject) {
123+
//console.log('resolving with entities '+JSON.stringify(entities));
124+
resolve();
125+
});
126+
},
127+
canUpdate: function (userInfo, entityInfo) {
128+
return new Promise(function (resolve, reject) {
129+
//console.log('resolving with entities '+JSON.stringify(entities));
130+
resolve(entityInfo);
131+
});
132+
},
133+
canWriteToAllAttributes: function (userInfo, entityInfo) {
134+
return new Promise(function (resolve, reject) {
135+
//console.log('resolving with entities '+JSON.stringify(entities));
136+
resolve();
137+
});
138+
}
139+
140+
};
141+
142+
var admin = {
143+
"id":"root!@!agile-local",
144+
"type":"user",
145+
"user_name": "root",
146+
"auth_type": "agile-local",
147+
"password": "secret",
148+
"role": "admin",
149+
"owner": "root!@!agile-local"
150+
};
77151

78152
function help(err) {
79153
if (err)
80154
console.log(err);
81155
console.log(getUsage(sections));
82156
}
83157
var args;
84-
var config;
85-
158+
var conf;
86159
var entity_type = "/user";
87-
var db_location = {
88-
"storage": {
89-
"dbName": "../database_"
90-
}
91-
};
160+
92161
try {
93162
args = commandLineArgs(optionDefinitions);
94163
if (args.hasOwnProperty("help"))
95164
help();
96165
else {
166+
if(args.config){
167+
conf = require(args.config);
168+
}
169+
else{
170+
conf = require('../conf/agile-idm-core-conf');
171+
172+
}
173+
//hack to execute relative to the upper directory
174+
if(conf.storage.dbName.indexOf("/")!=0){
175+
conf.storage.dbName = "../"+conf.storage.dbName;
176+
}
177+
178+
if(conf.policies.dbName.indexOf("/")!=0){
179+
conf.policies.dbName = "../"+conf.policies.dbName;
180+
}
181+
97182
if (args.auth === "agile-local" && !args.password)
98183
return help(new Error("When local authentication is used a password is required!"));
99184

100185
if (args.username && args.auth) {
101-
if (args.config)
102-
db_location.storage.dbName = args.config;
103186

104-
var storage = new Storage(db_location);
187+
105188
var user_id = ids.buildId(args.username, args.auth);
106189
var storage_id = {
107190
id: user_id,
@@ -113,10 +196,22 @@ try {
113196
auth_type: args.auth
114197

115198
}
199+
if(args.role){
200+
user.role =args.role;
201+
}
116202
if (args.password) {
117203
user.password = args.password;
118204
}
119-
storage.createEntity(user_id, entity_type, user_id, user).then(function (result) {
205+
var idmcore = new IdmCore(conf);
206+
idmcore.setMocks(null, null, PdpMockOk, null, pepMockOk);
207+
console.log(JSON.stringify(admin))
208+
console.log(JSON.stringify(user_id))
209+
console.log(JSON.stringify(entity_type))
210+
console.log(JSON.stringify(user_id))
211+
console.log(JSON.stringify(admin))
212+
console.log(JSON.stringify(admin))
213+
idmcore.createEntityAndSetOwner(admin, user_id, entity_type, user, user_id).then(function(result){
214+
120215
console.log("SUCCESS: User created " + JSON.stringify(result));
121216
}, function fail(err) {
122217
console.warn("FAILURE: User cannot be created " + err);

0 commit comments

Comments
 (0)