Repository navigation
Expand file tree
/
Copy pathdockerfile-runner.test.ts
More file actions
105 lines (87 loc) · 3.14 KB
/
Copy pathdockerfile-runner.test.ts
File metadata and controls
105 lines (87 loc) · 3.14 KB
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
import { readFileSync } from "node:fs"
import path from "node:path"
import { describe, expect, it } from "vitest"
type Instruction = { commande: string; arguments: string }
type Etape = { nom: string; instructions: Instruction[] }
const DOCKERFILE = path.join(import.meta.dirname, "Dockerfile")
function instructionsDuFichier(): Instruction[] {
return readFileSync(DOCKERFILE, "utf8")
.split("\n")
.map((ligne) => ligne.trim())
.filter((ligne) => ligne !== "" && !ligne.startsWith("#"))
.map((ligne) => {
const [commande = "", ...reste] = ligne.split(/\s+/)
return { commande: commande.toUpperCase(), arguments: reste.join(" ") }
})
}
function etapesDuFichier(): Etape[] {
const etapes: Etape[] = []
for (const instruction of instructionsDuFichier()) {
if (instruction.commande !== "FROM") {
etapes.at(-1)?.instructions.push(instruction)
continue
}
const nom = /\bAS\s+(\S+)$/i.exec(instruction.arguments)?.[1]
if (nom === undefined) {
throw new Error(`FROM sans nom d'étape : ${instruction.arguments}`)
}
etapes.push({ nom, instructions: [] })
}
return etapes
}
function etape(nom: string): Etape {
const trouvee = etapesDuFichier().find((etape) => etape.nom === nom)
if (trouvee === undefined) {
throw new Error(`le Dockerfile ne définit aucune étape « ${nom} »`)
}
return trouvee
}
function indexDe(etape: Etape, commande: string): number {
return etape.instructions.findIndex(
(instruction) => instruction.commande === commande
)
}
describe("l'image de l'application tourne sous un utilisateur non-root", () => {
it("garde l'étape runner en dernier, celle que la composition exécute", () => {
const etapes = etapesDuFichier()
expect(etapes.at(-1)?.nom).toBe("runner")
})
it("bascule sur l'utilisateur `node` avant de lancer le serveur", () => {
const runner = etape("runner")
const bascule = indexDe(runner, "USER")
const demarrage = indexDe(runner, "CMD")
expect(bascule).toBeGreaterThan(-1)
expect(demarrage).toBeGreaterThan(-1)
expect(bascule).toBeLessThan(demarrage)
expect(runner.instructions[bascule]?.arguments).toBe("node")
})
it("ne repasse jamais root après la bascule", () => {
const bascules = etape("runner").instructions.filter(
(instruction) => instruction.commande === "USER"
)
expect(bascules.map((instruction) => instruction.arguments)).toEqual([
"node",
])
})
it("laisse le serveur standalone se lancer sans élévation de privilèges", () => {
const runner = etape("runner")
expect(runner.instructions[indexDe(runner, "CMD")]?.arguments).toBe(
'["node", "server.js"]'
)
})
it("ne corrige pas les droits par une couche supplémentaire", () => {
const runner = etape("runner")
const copies = runner.instructions.filter(
(instruction) => instruction.commande === "COPY"
)
expect(copies.length).toBeGreaterThan(0)
for (const copie of copies) {
expect(copie.arguments).not.toContain("--chown")
}
expect(
runner.instructions.filter(
(instruction) => instruction.commande === "RUN"
)
).toEqual([])
})
})