89 lines
2.6 KiB
JavaScript
89 lines
2.6 KiB
JavaScript
// Adapted from Sendico's canonical Product policy seed; see PROVENANCE.md.
|
|
// Run only against the SXCHNGE-owned database. This seeds descriptions only:
|
|
// no users, organization, role grants or provider resources are created.
|
|
const databaseName = process.env.SXCHNGE_MONGO_DATABASE || 'sxchnge';
|
|
const targetDb = db.getSiblingDB(databaseName);
|
|
const now = new Date();
|
|
|
|
const canonicalPolicies = [
|
|
{
|
|
_id: ObjectId('677a61569cf14638b34423e0'),
|
|
name: 'Organization Settings',
|
|
description: 'Access to organization settings',
|
|
resourceTypes: ['organizations'],
|
|
},
|
|
{
|
|
_id: ObjectId('67bb6ec0da91191ffeb47012'),
|
|
name: 'Accounts',
|
|
description: 'Access to accounts settings',
|
|
resourceTypes: ['accounts'],
|
|
},
|
|
{
|
|
_id: ObjectId('67b3d7bd452192e77a26b22d'),
|
|
name: 'Roles',
|
|
description: 'Access to roles management',
|
|
resourceTypes: ['roles', 'policies'],
|
|
},
|
|
];
|
|
|
|
upsertCanonicalPolicies();
|
|
|
|
function builtInFilter(extra) {
|
|
return Object.assign(
|
|
{
|
|
$or: [
|
|
{ organizationRef: { $exists: false } },
|
|
{ organizationRef: null },
|
|
],
|
|
},
|
|
extra,
|
|
);
|
|
}
|
|
|
|
function upsertCanonicalPolicies() {
|
|
const result = [];
|
|
for (const entry of canonicalPolicies) {
|
|
let existing = targetDb.policies.findOne({ _id: entry._id });
|
|
if (!existing) {
|
|
// Migration-created policies carry environment-specific ids.
|
|
existing = targetDb.policies.findOne(
|
|
builtInFilter({ resourceTypes: { $all: entry.resourceTypes } }),
|
|
);
|
|
}
|
|
if (existing) {
|
|
const patch = {};
|
|
if (existing.name !== entry.name) patch.name = entry.name;
|
|
if (existing.description !== entry.description) {
|
|
patch.description = entry.description;
|
|
}
|
|
if (
|
|
JSON.stringify(existing.resourceTypes || []) !==
|
|
JSON.stringify(entry.resourceTypes)
|
|
) {
|
|
patch.resourceTypes = entry.resourceTypes;
|
|
}
|
|
if (Object.keys(patch).length > 0) {
|
|
patch.updatedAt = now;
|
|
targetDb.policies.updateOne({ _id: existing._id }, { $set: patch });
|
|
print(`Patched built-in policy "${entry.name}" (${existing._id.toHexString()})`);
|
|
} else {
|
|
print(`Built-in policy "${entry.name}" is up to date`);
|
|
}
|
|
result.push({ entry, id: existing._id, inserted: false });
|
|
continue;
|
|
}
|
|
|
|
targetDb.policies.insertOne({
|
|
_id: entry._id,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
name: entry.name,
|
|
description: entry.description,
|
|
resourceTypes: entry.resourceTypes,
|
|
});
|
|
print(`Seeded built-in policy "${entry.name}" (${entry._id.toHexString()})`);
|
|
result.push({ entry, id: entry._id, inserted: true });
|
|
}
|
|
return result;
|
|
}
|