Skip to content

Commit 1f5a839

Browse files
mtrezzadavimacedo
authored andcommitted
Get ParseConfig parameters with Master Key (parse-community#5954)
* added saving, retrieving * added tests * fixed typo * added masterKeyOnly to schema controller
1 parent 8581c1f commit 1f5a839

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

spec/ParseGlobalConfig.spec.js

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,17 @@ describe('a GlobalConfig', () => {
2020
.upsertOneObject(
2121
'_GlobalConfig',
2222
{
23-
fields: { objectId: { type: 'Number' }, params: { type: 'Object' } },
23+
fields: {
24+
objectId: { type: 'Number' },
25+
params: { type: 'Object' },
26+
masterKeyOnly: { type: 'Object' },
27+
},
2428
},
2529
query,
26-
{ params: { companies: ['US', 'DK'] } }
30+
{
31+
params: { companies: ['US', 'DK'], internalParam: 'internal' },
32+
masterKeyOnly: { internalParam: true },
33+
}
2734
)
2835
.then(done, err => {
2936
jfail(err);
@@ -54,6 +61,44 @@ describe('a GlobalConfig', () => {
5461
});
5562
});
5663

64+
it('internal parameter can be retrieved with master key', done => {
65+
request({
66+
url: 'http://localhost:8378/1/config',
67+
json: true,
68+
headers,
69+
}).then(response => {
70+
const body = response.data;
71+
try {
72+
expect(response.status).toEqual(200);
73+
expect(body.params.internalParam).toEqual('internal');
74+
} catch (e) {
75+
jfail(e);
76+
}
77+
done();
78+
});
79+
});
80+
81+
it('internal parameter cannot be retrieved without master key', done => {
82+
request({
83+
url: 'http://localhost:8378/1/config',
84+
json: true,
85+
headers: {
86+
'X-Parse-Application-Id': 'test',
87+
'X-Parse-REST-API-Key': 'rest',
88+
'Content-Type': 'application/json',
89+
},
90+
}).then(response => {
91+
const body = response.data;
92+
try {
93+
expect(response.status).toEqual(200);
94+
expect(body.params.internalParam).toBeUndefined();
95+
} catch (e) {
96+
jfail(e);
97+
}
98+
done();
99+
});
100+
});
101+
57102
it('can be updated when a master key exists', done => {
58103
request({
59104
method: 'PUT',
@@ -117,7 +162,13 @@ describe('a GlobalConfig', () => {
117162
method: 'PUT',
118163
url: 'http://localhost:8378/1/config',
119164
json: true,
120-
body: { params: { companies: { __op: 'Delete' }, foo: 'bar' } },
165+
body: {
166+
params: {
167+
companies: { __op: 'Delete' },
168+
internalParam: { __op: 'Delete' },
169+
foo: 'bar',
170+
},
171+
},
121172
headers,
122173
}).then(response => {
123174
const body = response.data;

src/Controllers/SchemaController.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ const defaultColumns: { [string]: SchemaFields } = Object.freeze({
131131
_GlobalConfig: {
132132
objectId: { type: 'String' },
133133
params: { type: 'Object' },
134+
masterKeyOnly: { type: 'Object' },
134135
},
135136
_GraphQLConfig: {
136137
objectId: { type: 'String' },

src/Routers/GlobalConfigRouter.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,20 @@ export class GlobalConfigRouter extends PromiseRouter {
1313
return { response: { params: {} } };
1414
}
1515
const globalConfig = results[0];
16-
return { response: { params: globalConfig.params } };
16+
if (!req.auth.isMaster && globalConfig.masterKeyOnly !== undefined) {
17+
for (const param in globalConfig.params) {
18+
if (globalConfig.masterKeyOnly[param]) {
19+
delete globalConfig.params[param];
20+
delete globalConfig.masterKeyOnly[param];
21+
}
22+
}
23+
}
24+
return {
25+
response: {
26+
params: globalConfig.params,
27+
masterKeyOnly: globalConfig.masterKeyOnly,
28+
},
29+
};
1730
});
1831
}
1932

@@ -25,9 +38,11 @@ export class GlobalConfigRouter extends PromiseRouter {
2538
);
2639
}
2740
const params = req.body.params;
41+
const masterKeyOnly = req.body.masterKeyOnly || {};
2842
// Transform in dot notation to make sure it works
2943
const update = Object.keys(params).reduce((acc, key) => {
3044
acc[`params.${key}`] = params[key];
45+
acc[`masterKeyOnly.${key}`] = masterKeyOnly[key] || false;
3146
return acc;
3247
}, {});
3348
return req.config.database

0 commit comments

Comments
 (0)