Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): update seeder #35

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/cli/src/commands/database/ogcio/common-rbac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ export const createScopes = async <
fillScopesMethod: (scopesToSeed: T[]) => O;
}): Promise<O> => {
const scopesToCreate = params.fillScopesMethod(params.scopesToSeed);
if (params.scopesToSeed.length === 0) {
return scopesToCreate;
}
const queries: Array<
Promise<Omit<OrganizationSeedingScope | ResourceSeedingScope, 'id'> & { id: string }>
> = [];
Expand Down
23 changes: 21 additions & 2 deletions packages/cli/src/commands/database/ogcio/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { readFileSync } from 'node:fs';
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable unicorn/import-style */
import { resolve } from 'node:path';

import { getEnv } from '@silverhand/essentials';
import type { CommandModule } from 'yargs';

import { createPoolAndDatabaseIfNeeded } from '../../../database.js';
Expand All @@ -11,9 +14,25 @@ import { seedOgcio } from './ogcio.js';

const DEFAULT_SEEDER_FILE = './src/commands/database/ogcio/ogcio-seeder.json';

const loadSeederData = (path: string): OgcioTenantSeeder =>
const interpolateString = (content: string): string => {
const regExp = /<\w+>/g;

return content.replaceAll(regExp, function (match) {
const variableName = match.slice(1, -1);
if (!getEnv(variableName)) {
return match;
}
return getEnv(variableName);
});
};

const loadSeederData = (path: string): OgcioTenantSeeder => {
const content = readFileSync(new URL(path, import.meta.url), 'utf8');
const interpolatedContent = interpolateString(content);

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
JSON.parse(readFileSync(new URL(path, import.meta.url), 'utf8'));
return JSON.parse(interpolatedContent);
};

const getSeederData = async (seederFilepath: unknown): Promise<OgcioTenantSeeder> => {
if (typeof seederFilepath !== 'string' || seederFilepath.length === 0) {
Expand Down

This file was deleted.

114 changes: 114 additions & 0 deletions packages/cli/src/commands/database/ogcio/ogcio-seeder-dev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{
"default": {
"organizations": [
{
"name": "OGCIO",
"description": "OGCIO Organization",
"id": "ogcio"
}
],
"applications": [
{
"name": "Payments Building Block",
"description": "Payments App of Life Events",
"type": "Traditional",
"redirect_uri": "<SEEDER_PAYMENTS_APP_REDIRECT_URI>",
"logout_redirect_uri": "<SEEDER_PAYMENTS_APP_LOGOUT_REDIRECT_URI>",
"secret": "<SEEDER_PAYMENTS_APP_SECRET>",
"id": "r5f56tpkytpqyyshiutd2"
},
{
"name": "Messaging Building Block",
"description": "Messaging App of Life Events",
"type": "Traditional",
"redirect_uri": "<SEEDER_MESSAGING_APP_REDIRECT_URI>",
"logout_redirect_uri": "<SEEDER_MESSAGING_APP_LOGOUT_REDIRECT_URI>",
"secret": "<SEEDER_MESSAGING_APP_SECRET>",
"id": "1lvmteh2ao3xrswyq7j3e"
}
],
"resources": [
{
"id": "payments-api",
"name": "Payments Building Block API",
"indicator": "<SEEDER_PAYMENTS_API_INDICATOR>"
},
{
"id": "messaging-api",
"name": "Messaging Building Block API",
"indicator": "<SEEDER_MESSAGING_API_INDICATOR>"
}
],
"connectors": [
{
"id": "mygovid",
"sync_profile": false,
"connector_id": "mygovid",
"config": {
"scope": "openid profile email",
"clientId": "<SEEDER_MYGOVID_CONNECTOR_CLIENT_ID>",
"clientSecret": "<SEEDER_MYGOVID_CONNECTOR_CLIENT_SECRET>",
"tokenEndpoint": "<SEEDER_MYGOVID_CONNECTOR_TOKEN_ENDPOINT>",
"authorizationEndpoint": "<SEEDER_MYGOVID_CONNECTOR_AUTHORIZATION_ENDPOINT>",
"tokenEndpointAuthMethod": "client_secret_post",
"idTokenVerificationConfig": {
"jwksUri": "<SEEDER_MYGOVID_CONNECTOR_JWS_URI>"
},
"clientSecretJwtSigningAlgorithm": "HS256"
},
"metadata": {
"logo": "https://mygovidstatic.blob.core.windows.net/assets/images/favicon_196x196.png",
"name": {
"en": "MyGovId"
},
"target": "MyGovId (MyGovId connector)"
}
}
],
"sign_in_experiences": [
{
"id": "default",
"color": {
"primaryColor": "#007DA6",
"darkPrimaryColor": "#007DA6",
"isDarkModeEnabled": false
},
"branding": {
"logoUrl": "https://mygovidstatic.blob.core.windows.net/assets/images/helpchat-logo.png",
"darkLogoUrl": "https://mygovidstatic.blob.core.windows.net/assets/images/helpchat-logo.png"
},
"language_info": {
"autoDetect": true,
"fallbackLanguage": "en"
},
"sign_in": {
"methods": []
},
"sign_up": {
"verify": false,
"password": false,
"identifiers": []
},
"social_sign_in_connector_targets": [
"MyGovId (MyGovId connector)"
],
"sign_in_mode": "SignInAndRegister"
}
],
"webhooks": [
{
"id": "login_webhook",
"name": "User log in",
"events": [
"PostRegister",
"PostSignIn"
],
"config": {
"url": "<SEEDER_WEBHOOK_LOGIN_URL>"
},
"signing_key": "<SEEDER_WEBHOOK_SIGNING_KEY>",
"enabled": true
}
]
}
}
4 changes: 2 additions & 2 deletions packages/cli/src/commands/database/ogcio/ogcio-seeder.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@
{
"id": "default",
"color": {
"primaryColor": "#FFFFFF",
"darkPrimaryColor": "#FFFFFF",
"primaryColor": "#007DA6",
"darkPrimaryColor": "#007DA6",
"isDarkModeEnabled": false
},
"branding": {
Expand Down
91 changes: 55 additions & 36 deletions packages/cli/src/commands/database/ogcio/ogcio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,66 @@ const createDataForTenant = async (
tenantId: string,
tenantData: OgcioSeeder
) => {
const organizations = await createOrganizations({
transaction,
tenantId,
organizations: tenantData.organizations,
});
if (tenantData.organizations.length > 0) {
const organizations = await createOrganizations({
transaction,
tenantId,
organizations: tenantData.organizations,
});
}

const organizationsRbac = await seedOrganizationRbacData({
transaction,
tenantId,
toSeed: tenantData,
});
const applications = await seedApplications({
transaction,
tenantId,
applications: tenantData.applications,
});
const resources = await seedResources({
transaction,
tenantId,
inputResources: tenantData.resources,
});
const resourcesRbac = await seedResourceRbacData({
tenantId,
transaction,
toSeed: tenantData,
seededResources: resources,
});
const connectors = await seedConnectors({
transaction,
tenantId,
connectors: tenantData.connectors,
});
const signInExperiences = await seedSignInExperiences({
transaction,
tenantId,
experiences: tenantData.sign_in_experiences,
});
const webhooks = await seedWebhooks({
transaction,
tenantId,
hooks: tenantData.webhooks
});

if (tenantData.applications.length > 0) {
const applications = await seedApplications({
transaction,
tenantId,
applications: tenantData.applications,
});
}

if (tenantData.resources.length > 0) {
const resources = await seedResources({
transaction,
tenantId,
inputResources: tenantData.resources,
});

const resourcesRbac = await seedResourceRbacData({
tenantId,
transaction,
toSeed: tenantData,
seededResources: resources,
});
}

if (tenantData.connectors.length > 0) {
const connectors = await seedConnectors({
transaction,
tenantId,
connectors: tenantData.connectors,
});
}

if (tenantData.sign_in_experiences.length > 0) {
const signInExperiences = await seedSignInExperiences({
transaction,
tenantId,
experiences: tenantData.sign_in_experiences,
});
}

if (tenantData.webhooks.length > 0) {
const webhooks = await seedWebhooks({
transaction,
tenantId,
hooks: tenantData.webhooks,
});
}
};

const transactionMethod = async (transaction: DatabaseTransactionConnection) => {
Expand Down
43 changes: 28 additions & 15 deletions packages/cli/src/commands/database/ogcio/organizations-rbac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,33 @@ export const seedOrganizationRbacData = async (params: {
roles: Record<string, OrganizationSeedingRole>;
relations: SeedingRelation[];
}> => {
const createdScopes = await createScopes({
transaction: params.transaction,
tenantId: params.tenantId,
scopesToSeed: params.toSeed.organization_permissions,
fillScopesMethod: fillScopes,
});
const createdRoles = await createRoles({
transaction: params.transaction,
tenantId: params.tenantId,
scopesLists: createdScopes,
rolesToSeed: params.toSeed.organization_roles,
fillRolesMethod: fillRoles,
});
const relations = await createRelations(params.transaction, params.tenantId, createdRoles);
if (params.toSeed.organization_permissions.length > 0) {
const createdScopes = await createScopes({
transaction: params.transaction,
tenantId: params.tenantId,
scopesToSeed: params.toSeed.organization_permissions,
fillScopesMethod: fillScopes,
});
const createdRoles = await createRoles({
transaction: params.transaction,
tenantId: params.tenantId,
scopesLists: createdScopes,
rolesToSeed: params.toSeed.organization_roles,
fillRolesMethod: fillRoles,
});
const relations = await createRelations(params.transaction, params.tenantId, createdRoles);

return { scopes: createdScopes, roles: createdRoles, relations };
return { scopes: createdScopes, roles: createdRoles, relations };
}

return {
scopes: {
scopesList: [],
scopesByEntity: {},
scopesByAction: {},
scopesByFullName: {},
},
roles: {},
relations: [],
};
};
Loading