Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
18 changes: 16 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"pastel": "^3.0.0",
"permitio": "2.6.1",
"react": "^18.2.0",
"uuid": "^11.1.0",
"zod": "^3.21.3"
},
"devDependencies": {
Expand Down
11 changes: 10 additions & 1 deletion source/commands/test/generate/e2e.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,22 @@ export const options = zod.object({
'Test code sample that iterates the config file and asserts the results.',
}),
),
snippetPath: zod
Comment thread
35C4n0r marked this conversation as resolved.
.string()
.optional()
.describe(
option({
description: 'Optional: Path to save the test file',
}),
),
});

type Props = {
readonly options: zInfer<typeof options>;
};

export default function E2e({
options: { dryRun, models, path, apiKey, snippet },
options: { dryRun, models, path, apiKey, snippet, snippetPath },
}: Props) {
return (
<AuthProvider scope={'environment'} permit_key={apiKey}>
Expand All @@ -69,6 +77,7 @@ export default function E2e({
models={models}
path={path}
snippet={snippet}
snippetPath={snippetPath}
/>
</AuthProvider>
);
Expand Down
4 changes: 2 additions & 2 deletions source/components/init/GenerateUsersComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useRef, useMemo, useState } from 'react';
import { useGeneratePolicySnapshot } from '../test/hooks/usePolicySnapshot.js';
import { useGeneratePolicyRBACSnapshot } from '../test/hooks/usePolicyRBACSnapshot.js';
import { Text, Box } from 'ink';
import Spinner from 'ink-spinner';
import SelectInput from 'ink-select-input';
Expand Down Expand Up @@ -38,7 +38,7 @@ export default function GeneratedUsersComponent({
);

const { state, error, createdUsers, tenantId } =
useGeneratePolicySnapshot(snapshotOptions);
useGeneratePolicyRBACSnapshot(snapshotOptions);

// Handle errors
useEffect(() => {
Expand Down
134 changes: 107 additions & 27 deletions source/components/test/GeneratePolicySnapshot.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import React, { useEffect } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import { Newline, Text } from 'ink';
import Spinner from 'ink-spinner';
import { useGeneratePolicySnapshot } from './hooks/usePolicySnapshot.js';
import {
RBACConfig,
useGeneratePolicyRBACSnapshot,
} from './hooks/usePolicyRBACSnapshot.js';
import { CodeSampleComponent } from './code-samples/CodeSampleComponent.js';
import {
ABACConfig,
useGeneratePolicyABACSnapshot,
} from './hooks/usePolicyABACSnapshot.js';
import { saveFile } from '../../utils/fileSaver.js';
import Spinner from 'ink-spinner';

export type GeneratePolicySnapshotProps = {
dryRun: boolean;
Expand All @@ -13,46 +21,115 @@ export type GeneratePolicySnapshotProps = {
snippetPath?: string;
};

export type DryUser = {
key: string;
email: string;
firstName: string;
lastName: string;
roles: string[];
};

export type AccessControlConfig = {
config: (RBACConfig | ABACConfig)[];
users?: DryUser[];
};

export function GeneratePolicySnapshot({
dryRun,
models,
path,
snippet,
snippetPath,
}: GeneratePolicySnapshotProps) {
const filePath = snippet && !path ? 'authz-test.json' : path;
const { state, error, roles, tenantId, finalConfig, dryUsers } =
useGeneratePolicySnapshot({ dryRun, models, path: filePath });
const [state, setState] = useState<'building' | 'done'>('building');
const [error, setError] = useState<string | undefined | null>(undefined);
const [finalConfig, setFinalConfig] = useState<AccessControlConfig>({
config: [],
});
// const [finalDryUsers, setFinalDryUsers] = useState<DryUser[]>([]);
const {
state: RBACState,
error: RBACError,
finalConfig: RBACConfig,
dryUsers: dryRBACUsers,
} = useGeneratePolicyRBACSnapshot({
dryRun,
models,
path: filePath,
});

// Handle Error and lifecycle completion.
useEffect(() => {
if (error || (state === 'done' && !snippet)) {
const {
state: ABACState,
error: ABACError,
finalConfig: ABACConfig,
dryUsers: dryABACUsers,
} = useGeneratePolicyABACSnapshot({
dryRun,
models,
path,
});

const saveConfigToPath = useCallback(
async (finalConfig: AccessControlConfig) => {
// Write config as pretty JSON
const json = JSON.stringify(finalConfig, null, 2);
const { error } = await saveFile(path ?? '', json);
if (error) {
setError(error);
return;
}
setTimeout(() => {
process.exit(1);
setState('done');
}, 1000);
},
[path],
);

useEffect(() => {
// console.log('IM MAIN', [RBACState, ABACState], models);
Comment thread
35C4n0r marked this conversation as resolved.
Outdated
const configsGenerated = [RBACState, ABACState].filter(
Comment thread
35C4n0r marked this conversation as resolved.
state => state === 'done',
);
if (configsGenerated.length === models.length) {
const combinedConfigs: AccessControlConfig = {
config: [...ABACConfig, ...RBACConfig],
users: [...dryABACUsers, ...dryRBACUsers],
};
setFinalConfig(prev => ({
...prev,
...combinedConfigs,
}));
if (path) {
saveConfigToPath(combinedConfigs);
} else {
setTimeout(() => {
setState('done');
}, 1000);
}
}
}, [error, snippet, state]);
}, [
ABACConfig,
ABACState,
RBACConfig,
RBACState,
dryABACUsers,
dryRBACUsers,
models,
path,
saveConfigToPath,
]);

return (
<>
{state === 'roles' && <Text>Getting all roles</Text>}
{roles.length > 0 && <Text>Roles found: {roles.length}</Text>}
{state === 'rbac-tenant' && <Text>Crating a new Tenant</Text>}
{tenantId && <Text>Created a new test tenant: {tenantId}</Text>}
{state === 'rbac-generate' && (
{state === 'building' && (
<Text>
Generating test data for you <Spinner type={'dots3'} />{' '}
Building Config <Spinner type={'dots'} />{' '}
</Text>
)}
{dryRun && <Text>Dry run mode!</Text>}
{state === 'done' && filePath && <Text>Config saved to {filePath}</Text>}
{state === 'done' && !filePath && (
<Text>
{' '}
{JSON.stringify(
dryRun
? { users: dryUsers, config: finalConfig }
: { config: finalConfig },
)}{' '}
</Text>
{state === 'done' && path && <Text>Config saved to {path}!</Text>}
{state === 'done' && !path && (
<Text> {JSON.stringify(finalConfig)} </Text>
)}
{state === 'done' && snippet && (
<>
Expand All @@ -61,10 +138,13 @@ export function GeneratePolicySnapshot({
framework={snippet}
configPath={filePath}
pdpUrl={'http://localhost:7766'}
path={snippetPath}
/>
</>
)}
{error && <Text>{error}</Text>}
{ABACError && <Text>{ABACError}</Text>}
{RBACError && <Text>{RBACError}</Text>}
</>
);
}
6 changes: 3 additions & 3 deletions source/components/test/code-samples/CodeSampleComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function CodeSampleComponent({
}
}, [auth, framework, configPath, path, pdpUrl, state]);

const saveCodeTOPath = useCallback(async () => {
const saveCodeToPath = useCallback(async () => {
const { error } = await saveFile(path ?? '', code ?? '');
if (error) {
setError(error);
Expand All @@ -57,11 +57,11 @@ export function CodeSampleComponent({

useEffect(() => {
if (code && path) {
saveCodeTOPath();
saveCodeToPath();
} else if (code) {
setState('done');
}
}, [code, path, saveCodeTOPath]);
}, [code, path, saveCodeToPath]);

return (
<>
Expand Down
Loading