-
Notifications
You must be signed in to change notification settings - Fork 267
/
common.ts
342 lines (293 loc) · 10.9 KB
/
common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Copyright (C) 2018 Garden Technologies, Inc. <info@garden.io>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import Joi from "@hapi/joi"
import { JoiObject } from "@hapi/joi"
import { splitLast } from "../util/util"
import isGitUrl from "is-git-url"
import { deline, dedent } from "../util/string"
export type Primitive = string | number | boolean | null
export interface PrimitiveMap {
[key: string]: Primitive
}
export interface DeepPrimitiveMap {
[key: string]: Primitive | DeepPrimitiveMap | Primitive[] | DeepPrimitiveMap[]
}
// export type ConfigWithSpec<S extends object> = <T extends S>{
// spec: Omit<T, keyof S> & Partial<S>
// }
export const includeGuideLink =
"https://docs.garden.io/guides/configuration-files#including-excluding-files-and-directories"
export const enumToArray = (Enum) => Object.values(Enum).filter((k) => typeof k === "string") as string[]
interface JoiGitUrlParams {
requireHash?: boolean
}
interface JoiPosixPathParams {
absoluteOnly?: boolean
allowGlobs?: boolean
relativeOnly?: boolean
subPathOnly?: boolean
filenameOnly?: boolean
}
// Extend the Joi module with our custom rules
interface MetadataKeys {
internal?: boolean
deprecated?: boolean
extendable?: boolean
experimental?: boolean
}
// Unfortunately we need to explicitly extend each type (just extending the AnySchema doesn't work).
declare module "@hapi/joi" {
export interface AnySchema {
meta(keys: MetadataKeys): this
}
export interface ArraySchema {
meta(keys: MetadataKeys): this
}
export interface AlternativesSchema {
meta(keys: MetadataKeys): this
}
export interface BinarySchema {
meta(keys: MetadataKeys): this
}
export interface BooleanSchema {
meta(keys: MetadataKeys): this
}
export interface DateSchema {
meta(keys: MetadataKeys): this
}
export interface FunctionSchema {
meta(keys: MetadataKeys): this
}
export interface NumberSchema {
meta(keys: MetadataKeys): this
}
export interface ObjectSchema {
meta(keys: MetadataKeys): this
}
export interface StringSchema {
meta(keys: MetadataKeys): this
gitUrl: (params: JoiGitUrlParams) => this
posixPath: (params?: JoiPosixPathParams) => this
}
export interface LazySchema {
meta(keys: MetadataKeys): this
}
}
export const joi: Joi.Root = Joi.extend({
base: Joi.string(),
name: "string",
language: {
gitUrl: "must be a valid Git repository URL",
requireHash: "must specify a branch/tag hash",
posixPath: "must be a POSIX-style path", // Used below as 'string.posixPath'
absoluteOnly: "must be a an absolute path",
allowGlobs: "must not include globs (wildcards)",
relativeOnly: "must be a relative path (may not be an absolute path)",
subPathOnly: "must be a relative sub-path (may not contain '..' segments or be an absolute path)",
filenameOnly: "must be a filename (may not contain slashes)",
},
rules: [
{
name: "gitUrl",
params: {
options: Joi.object().keys({
requireHash: Joi.boolean().description("Only allow Git URLs with a branch/tag hash."),
}),
},
validate(params: { options?: JoiGitUrlParams }, value: string, state, prefs) {
// Make sure it's a string
const baseSchema = Joi.string()
const result = baseSchema.validate(value)
if (result.error) {
return result.error
}
if (!isGitUrl(value)) {
// tslint:disable-next-line:no-invalid-this
return this.createError("string.gitUrl", { v: value }, state, prefs)
}
if (params.options && params.options.requireHash === true) {
const url = splitLast(value, "#")[0]
if (!url) {
// tslint:disable-next-line:no-invalid-this
return this.createError("string.requireHash", { v: value }, state, prefs)
}
}
return value
},
},
{
name: "posixPath",
params: {
options: Joi.object()
.keys({
absoluteOnly: Joi.boolean().description("Only allow absolute paths (starting with /)."),
allowGlobs: Joi.boolean().description("Allow globs (wildcards) in path."),
relativeOnly: Joi.boolean().description("Disallow absolute paths (starting with /)."),
subPathOnly: Joi.boolean().description(
"Only allow sub-paths. That is, disallow '..' path segments and absolute paths."
),
filenameOnly: Joi.boolean().description("Only allow filenames. That is, disallow '/' in the path."),
})
.oxor("absoluteOnly", "relativeOnly")
.oxor("absoluteOnly", "filenameOnly")
.oxor("absoluteOnly", "subPathOnly"),
},
validate(params: { options?: JoiPosixPathParams }, value: string, state, prefs) {
// Note: This relativeOnly param is in the context of URLs.
// Our own relativeOnly param is in the context of file paths.
const baseSchema = Joi.string().uri({ relativeOnly: true })
const result = baseSchema.validate(value)
if (result.error) {
// tslint:disable-next-line:no-invalid-this
return this.createError("string.posixPath", { v: value }, state, prefs)
}
const options = params.options || {}
if (options.absoluteOnly) {
if (!value.startsWith("/")) {
// tslint:disable-next-line:no-invalid-this
return this.createError("string.absoluteOnly", { v: value }, state, prefs)
}
} else if (options.subPathOnly) {
if (value.startsWith("/") || value.split("/").includes("..")) {
// tslint:disable-next-line:no-invalid-this
return this.createError("string.subPathOnly", { v: value }, state, prefs)
}
} else if (options.relativeOnly) {
if (value.startsWith("/")) {
// tslint:disable-next-line:no-invalid-this
return this.createError("string.relativeOnly", { v: value }, state, prefs)
}
}
if (options.filenameOnly && value.includes("/")) {
// tslint:disable-next-line:no-invalid-this
return this.createError("string.filenameOnly", { v: value }, state, prefs)
}
if (!options.allowGlobs && (value.includes("*") || value.includes("?"))) {
// tslint:disable-next-line:no-invalid-this
return this.createError("string.allowGlobs", { v: value }, state, prefs)
}
return value // Everything is OK
},
},
],
})
export const joiPrimitive = () =>
joi
.alternatives()
.try(
joi.number(),
joi
.string()
.allow("")
.allow(null),
joi.boolean()
)
.description("Number, string or boolean")
export const absolutePathRegex = /^\/.*/ // Note: Only checks for the leading slash
// from https://stackoverflow.com/a/12311250/3290965
export const identifierRegex = /^(?![0-9]+$)(?!.*-$)(?!-)[a-z0-9-]{1,63}$/
export const userIdentifierRegex = /^(?!garden)(?=.{1,63}$)[a-z][a-z0-9]*(-[a-z0-9]+)*$/
export const envVarRegex = /^(?!garden)[a-z_][a-z0-9_\.]*$/i
export const joiIdentifierDescription =
"valid RFC1035/RFC1123 (DNS) label (may contain lowercase letters, numbers and dashes, must start with a letter, " +
"and cannot end with a dash) and must not be longer than 63 characters."
const moduleIncludeDescription = (extraDescription?: string) => {
const desc = dedent`
Specify a list of POSIX-style paths or globs that should be regarded as the source files for this
module. Files that do *not* match these paths or globs are excluded when computing the version of the module,
when responding to filesystem watch events, and when staging builds.
Note that you can also _exclude_ files using the \`exclude\` field or by placing \`.gardenignore\` files in your
source tree, which use the same format as \`.gitignore\` files. See the
[Configuration Files guide](${includeGuideLink}) for details.
Also note that specifying an empty list here means _no sources_ should be included.
`
if (extraDescription) {
return desc + "\n\n" + extraDescription
}
return desc
}
export const joiModuleIncludeDirective = (extraDescription?: string) =>
joi
.array()
.items(joi.string().posixPath({ allowGlobs: true, subPathOnly: true }))
.description(moduleIncludeDescription(extraDescription))
export const joiIdentifier = () =>
joi
.string()
.regex(identifierRegex)
.description(joiIdentifierDescription[0].toUpperCase() + joiIdentifierDescription.slice(1))
export const joiProviderName = (name: string) =>
joiIdentifier()
.required()
.description("The name of the provider plugin to use.")
.default(name)
.example(name)
export const joiStringMap = (valueSchema: JoiObject) => joi.object().pattern(/.+/, valueSchema)
export const joiUserIdentifier = () =>
joi
.string()
.regex(userIdentifierRegex)
.description(
deline`
Valid RFC1035/RFC1123 (DNS) label (may contain lowercase letters, numbers and dashes, must start
with a letter, and cannot end with a dash), cannot contain consecutive dashes or start with \`garden\`,
or be longer than 63 characters.
`
)
export const joiIdentifierMap = (valueSchema: JoiObject) =>
joi
.object()
.pattern(identifierRegex, valueSchema)
.default(() => ({}), "{}")
.description("Key/value map. Keys must be valid identifiers.")
export const joiVariables = () =>
joi
.object()
.pattern(/[a-zA-Z][a-zA-Z0-9_\-]+/i, joiPrimitive())
.default(() => ({}), "{}")
.unknown(false)
.description("Key/value map. Keys may contain letters and numbers, and values must be primitives.")
export const joiEnvVars = () =>
joi
.object()
.pattern(envVarRegex, joiPrimitive())
.default(() => ({}), "{}")
.unknown(false)
.description(
"Key/value map of environment variables. Keys must be valid POSIX environment variable names " +
"(must not start with `GARDEN`) and values must be primitives."
)
export const joiArray = (schema) =>
joi
.array()
.items(schema)
.default(() => [], "[]")
export const joiRepositoryUrl = () =>
joi
.alternatives(
joi.string().gitUrl({ requireHash: true }),
// Allow file URLs as well
joi.string().uri({ scheme: ["file"] })
)
.description(
"A remote repository URL. Currently only supports git servers. Must contain a hash suffix" +
" pointing to a specific branch or tag, with the format: <git remote url>#<branch|tag>"
)
.example("git+https://github.com/org/repo.git#v2.0")
export const joiSchema = () =>
joi
.object({
isJoi: joi
.boolean()
.only(true)
.required(),
})
.unknown(true)
export function isPrimitive(value: any) {
return typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null
}