|
| 1 | +import { z } from 'zod'; |
| 2 | + |
| 3 | +const TempPasswordSchema = z.object({ |
| 4 | + sendEmail: z.boolean().default(false), |
| 5 | + emailConfig: z.record(z.unknown()).default({}), |
| 6 | +}); |
| 7 | + |
| 8 | +const AuthorisedItemSchema = z.object({ |
| 9 | + project: z.string(), |
| 10 | + name: z.string(), |
| 11 | + url: z |
| 12 | + .string() |
| 13 | + .regex(/^(?:https?:\/\/.+\.git|git@[^:]+:[^/]+\/.+\.git)$/, { |
| 14 | + message: 'Must be a Git HTTPS URL (https://... .git) or SSH URL (git@...:... .git)', |
| 15 | + }), |
| 16 | +}); |
| 17 | + |
| 18 | +const FsSinkSchema = z.object({ |
| 19 | + type: z.literal('fs'), |
| 20 | + params: z.object({ filepath: z.string() }), |
| 21 | + enabled: z.boolean().default(true), |
| 22 | +}); |
| 23 | + |
| 24 | +const MongoSinkSchema = z.object({ |
| 25 | + type: z.literal('mongo'), |
| 26 | + connectionString: z.string(), |
| 27 | + options: z.object({ |
| 28 | + useNewUrlParser: z.boolean().default(true), |
| 29 | + useUnifiedTopology: z.boolean().default(true), |
| 30 | + tlsAllowInvalidCertificates: z.boolean().default(false), |
| 31 | + ssl: z.boolean().default(false), |
| 32 | + }), |
| 33 | + enabled: z.boolean().default(false), |
| 34 | +}); |
| 35 | + |
| 36 | +const SinkSchema = z.discriminatedUnion('type', [FsSinkSchema, MongoSinkSchema]); |
| 37 | + |
| 38 | +const ActiveDirectoryConfigSchema = z.object({ |
| 39 | + url: z.string(), |
| 40 | + baseDN: z.string(), |
| 41 | + searchBase: z.string(), |
| 42 | +}); |
| 43 | + |
| 44 | +const LocalAuthSchema = z.object({ |
| 45 | + type: z.literal('local'), |
| 46 | + enabled: z.boolean().default(true), |
| 47 | +}); |
| 48 | + |
| 49 | +const ADAuthSchema = z.object({ |
| 50 | + type: z.literal('ActiveDirectory'), |
| 51 | + enabled: z.boolean().default(false), |
| 52 | + adminGroup: z.string().default(''), |
| 53 | + userGroup: z.string().default(''), |
| 54 | + domain: z.string().default(''), |
| 55 | + adConfig: ActiveDirectoryConfigSchema, |
| 56 | +}); |
| 57 | + |
| 58 | +const AuthenticationSchema = z.discriminatedUnion('type', [LocalAuthSchema, ADAuthSchema]); |
| 59 | + |
| 60 | +const GithubApiSchema = z.object({ |
| 61 | + baseUrl: z.string().url(), |
| 62 | +}); |
| 63 | + |
| 64 | +const CommitEmailSchema = z.object({ |
| 65 | + local: z.object({ block: z.string().default('') }), |
| 66 | + domain: z.object({ allow: z.string().default('.*') }), |
| 67 | +}); |
| 68 | + |
| 69 | +const CommitBlockSchema = z.object({ |
| 70 | + literals: z.array(z.string()).default([]), |
| 71 | + patterns: z.array(z.string()).default([]), |
| 72 | +}); |
| 73 | + |
| 74 | +const CommitDiffSchema = z.object({ |
| 75 | + block: z.object({ |
| 76 | + literals: z.array(z.string()).default([]), |
| 77 | + patterns: z.array(z.string()).default([]), |
| 78 | + providers: z.record(z.unknown()).default({}), |
| 79 | + }), |
| 80 | +}); |
| 81 | + |
| 82 | +const AttestationQuestionSchema = z.object({ |
| 83 | + label: z.string(), |
| 84 | + tooltip: z.object({ |
| 85 | + text: z.string(), |
| 86 | + links: z.array(z.string()).default([]), |
| 87 | + }), |
| 88 | +}); |
| 89 | + |
| 90 | +export const ConfigSchema = z |
| 91 | + .object({ |
| 92 | + proxyUrl: z.string().url().default('https://github.com'), |
| 93 | + cookieSecret: z.string().default(''), |
| 94 | + sessionMaxAgeHours: z.number().int().positive().default(12), |
| 95 | + tempPassword: TempPasswordSchema.default({}), |
| 96 | + authorisedList: z.array(AuthorisedItemSchema).default([]), |
| 97 | + sink: z.array(SinkSchema).default([]), |
| 98 | + authentication: z.array(AuthenticationSchema).default([{ type: 'local', enabled: true }]), |
| 99 | + api: z |
| 100 | + .object({ |
| 101 | + github: GithubApiSchema, |
| 102 | + }) |
| 103 | + .default({ github: { baseUrl: 'https://api.github.com' } }), |
| 104 | + commitConfig: z |
| 105 | + .object({ |
| 106 | + author: z.object({ email: CommitEmailSchema }), |
| 107 | + message: z.object({ block: CommitBlockSchema }), |
| 108 | + diff: CommitDiffSchema, |
| 109 | + }) |
| 110 | + .default({ |
| 111 | + author: { email: { local: { block: '' }, domain: { allow: '.*' } } }, |
| 112 | + message: { block: { literals: [], patterns: [] } }, |
| 113 | + diff: { block: { literals: [], patterns: [], providers: {} } }, |
| 114 | + }), |
| 115 | + attestationConfig: z |
| 116 | + .object({ |
| 117 | + questions: z.array(AttestationQuestionSchema).default([]), |
| 118 | + }) |
| 119 | + .default({ questions: [] }), |
| 120 | + domains: z.record(z.string(), z.string()).default({}), |
| 121 | + privateOrganizations: z.array(z.string()).default([]), |
| 122 | + urlShortener: z.string().default(''), |
| 123 | + contactEmail: z.string().default(''), |
| 124 | + csrfProtection: z.boolean().default(true), |
| 125 | + plugins: z.array(z.unknown()).default([]), |
| 126 | + tls: z |
| 127 | + .object({ |
| 128 | + enabled: z.boolean().default(false), |
| 129 | + key: z.string().default(''), |
| 130 | + cert: z.string().default(''), |
| 131 | + }) |
| 132 | + .default({}), |
| 133 | + }) |
| 134 | + .strict(); |
| 135 | + |
| 136 | +export type Config = z.infer<typeof ConfigSchema>; |
0 commit comments