Skip to content

Commit

Permalink
added sso support to config object
Browse files Browse the repository at this point in the history
  • Loading branch information
karmaniverous committed Jul 17, 2024
1 parent 3bf7b5a commit 035220e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 36 deletions.
105 changes: 71 additions & 34 deletions src/Config.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import _ from 'lodash';
import { z } from 'zod';

const actionEnum = z.enum(['destroy', 'import', 'remove']).optional();
const actionEnum = z.enum(['destroy', 'import', 'remove']);
type Action = z.infer<typeof actionEnum>;

const actionableSchema = z.object({
action: actionEnum,
action: actionEnum.optional(),
id: z.string().optional(),
});

export type Actionable = z.infer<typeof actionableSchema>;

const filterValid = <T extends Actionable = Actionable>(
collection: Record<string, T>,
collection: Record<string, T> = {},
) =>
_.entries(collection)
.filter(([, v]) => !v.action || !['destroy', 'remove'].includes(v.action))
.map(([k]) => k);

const actionErrorModifier = (action: Action) =>
const actionErrorModifier = (action?: Action) =>
action === 'destroy'
? 'destroyed'
: action === 'remove'
Expand All @@ -27,15 +27,17 @@ const actionErrorModifier = (action: Action) =>

export const configSchema = z
.object({
accounts: z.record(
actionableSchema
.extend({
email: z.string(),
name: z.string(),
organizational_unit: z.string().optional(),
})
.strict(),
),
accounts: z
.record(
actionableSchema
.extend({
email: z.string(),
name: z.string(),
organizational_unit: z.string().optional(),
})
.strict(),
)
.optional(),
batches: z
.record(
z
Expand All @@ -47,15 +49,17 @@ export const configSchema = z
)
.optional(),
configPath: z.string().optional(),
environments: z.record(
z
.object({
account: z.string(),
cognito_user_pool_name: z.string(),
gha_on_push_branches: z.string().optional(),
})
.strict(),
),
environments: z
.record(
z
.object({
account: z.string(),
cognito_user_pool_name: z.string(),
gha_on_push_branches: z.string().optional(),
})
.strict(),
)
.optional(),
organization: z
.object({
aws_region: z.string(),
Expand All @@ -65,14 +69,41 @@ export const configSchema = z
namespace: z.string().optional(),
})
.strict(),
organizational_units: z.record(
actionableSchema
.extend({
name: z.string(),
parent: z.string().optional(),
})
.strict(),
),
organizational_units: z
.record(
actionableSchema
.extend({
name: z.string(),
parent: z.string().optional(),
})
.strict(),
)
.optional(),
sso: z
.object({
groups: z
.record(
z.object({
description: z.string().optional(),
account_permission_sets: z
.string()
.or(z.string().array())
.or(z.record(z.string().or(z.string().array())))
.optional(),
}),
)
.optional(),
permission_sets: z
.record(
z.object({
description: z.string().optional(),
policies: z.string().or(z.string().array()).optional(),
}),
)
.optional(),
policy_documents: z.record(z.string()).optional(),
})
.optional(),
terraform: z
.object({
aws_profile: z.string().optional(),
Expand Down Expand Up @@ -113,7 +144,7 @@ export const configSchema = z
const ou = data.accounts[account].organizational_unit;

if (ou && !validOus.includes(ou)) {
const action = data.organizational_units[ou]?.action; // eslint-disable-line @typescript-eslint/no-unnecessary-condition
const action = data.organizational_units?.[ou]?.action;

ctx.addIssue({
code: z.ZodIssueCode.invalid_enum_value,
Expand All @@ -129,7 +160,7 @@ export const configSchema = z
for (const environment in data.environments) {
// validate account
const account = data.environments[environment].account;
if (!validAccounts.includes(account)) {
if (data.accounts && !validAccounts.includes(account)) {
const action = data.accounts[account]?.action; // eslint-disable-line @typescript-eslint/no-unnecessary-condition

ctx.addIssue({
Expand All @@ -143,7 +174,10 @@ export const configSchema = z
}

// validate organization.master_account
if (!validAccounts.includes(data.organization.master_account)) {
if (
data.accounts &&
!validAccounts.includes(data.organization.master_account)
) {
const action = data.accounts[data.organization.master_account]?.action; // eslint-disable-line @typescript-eslint/no-unnecessary-condition

ctx.addIssue({
Expand Down Expand Up @@ -185,7 +219,10 @@ export const configSchema = z
}

// validate terraform.state_account
if (!validAccounts.includes(data.terraform.state_account)) {
if (
data.accounts &&
!validAccounts.includes(data.terraform.state_account)
) {
const action = data.accounts[data.terraform.state_account]?.action; // eslint-disable-line @typescript-eslint/no-unnecessary-condition

ctx.addIssue({
Expand Down
4 changes: 2 additions & 2 deletions src/updateConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { readConfig, writeConfig } from './configFile';
import { getErrorMessage } from './getErrorMessage';

type Update = {
[K in keyof Config]-?: Config[K] extends Record<string, object>
? Config[K][string] extends Actionable
[K in keyof Config]-?: NonNullable<Config[K]> extends Record<string, object>
? NonNullable<Config[K]>[string] extends Actionable
? { value: Record<string, string> }
: never
: never;
Expand Down

0 comments on commit 035220e

Please sign in to comment.