@mouryaamit/nx-polystack 1.12.1 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/migrations.json +5 -0
- package/dist/src/generators/infra/files/ci/github-actions/.github/workflows/ci.yml__template__ +3 -0
- package/dist/src/generators/infra/files/ci/jenkins/Jenkinsfile__template__ +4 -1
- package/dist/src/migrations/ci-nx-hardening/ci-nx-hardening.d.ts +13 -0
- package/dist/src/migrations/ci-nx-hardening/ci-nx-hardening.js +109 -0
- package/dist/src/migrations/ci-nx-hardening/ci-nx-hardening.js.map +1 -0
- package/package.json +1 -1
package/dist/migrations.json
CHANGED
|
@@ -127,6 +127,11 @@
|
|
|
127
127
|
"version": "1.12.0",
|
|
128
128
|
"description": "Add the root npm scripts assets:strip-exif and assets:check-exif (pstack assets strip-exif) for the source images apps ship",
|
|
129
129
|
"implementation": "./src/migrations/assets-exif-scripts/assets-exif-scripts"
|
|
130
|
+
},
|
|
131
|
+
"ci-nx-hardening": {
|
|
132
|
+
"version": "1.13.0",
|
|
133
|
+
"description": "Generated CI files: NX_DAEMON=false, and the Jenkinsfile passes commit SHAs to nx affected (Nx advisories before 22.7.10)",
|
|
134
|
+
"implementation": "./src/migrations/ci-nx-hardening/ci-nx-hardening"
|
|
130
135
|
}
|
|
131
136
|
}
|
|
132
137
|
}
|
|
@@ -17,6 +17,8 @@ pipeline {
|
|
|
17
17
|
environment {
|
|
18
18
|
BASE_BRANCH = "${env.CHANGE_TARGET ?: '<%= baseBranch %>'}"
|
|
19
19
|
NX_BASE = "${env.CHANGE_TARGET ? 'origin/' + env.CHANGE_TARGET : (env.BRANCH_NAME == '<%= baseBranch %>' ? 'HEAD~1' : 'origin/<%= baseBranch %>')}"
|
|
20
|
+
// No Nx daemon: its sockets are readable by other users of a shared agent (Nx before 22.7.10).
|
|
21
|
+
NX_DAEMON = 'false'
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
stages {
|
|
@@ -32,7 +34,8 @@ pipeline {
|
|
|
32
34
|
stage('Lint, test, build (affected)') {
|
|
33
35
|
steps {
|
|
34
36
|
sh 'git fetch --no-tags origin "+refs/heads/${BASE_BRANCH}:refs/remotes/origin/${BASE_BRANCH}"'
|
|
35
|
-
|
|
37
|
+
// Nx gets commit SHAs, never ref names (git-ref command injection in Nx before 22.7.10).
|
|
38
|
+
sh 'BASE_SHA=$(git rev-parse --verify "${NX_BASE}^{commit}") && npx nx affected -t lint typecheck test build --parallel=3 --base="$BASE_SHA" --head="$(git rev-parse --verify HEAD)"'
|
|
36
39
|
}
|
|
37
40
|
}
|
|
38
41
|
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Tree } from '@nx/devkit';
|
|
2
|
+
export interface HardenResult {
|
|
3
|
+
text: string;
|
|
4
|
+
/** What was added, for the log. */
|
|
5
|
+
done: string[];
|
|
6
|
+
/** What could not be done automatically, for the log. */
|
|
7
|
+
manual: string[];
|
|
8
|
+
}
|
|
9
|
+
/** ci.yml: `NX_DAEMON: 'false'` in the `main` job's env. */
|
|
10
|
+
export declare function hardenGithubCi(text: string): HardenResult;
|
|
11
|
+
/** Jenkinsfile: `NX_DAEMON = 'false'` in environment, and the affected step on commit SHAs. */
|
|
12
|
+
export declare function hardenJenkinsfile(text: string): HardenResult;
|
|
13
|
+
export default function ciNxHardening(tree: Tree): Promise<void>;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hardenJenkinsfile = exports.hardenGithubCi = void 0;
|
|
4
|
+
const devkit_1 = require("@nx/devkit");
|
|
5
|
+
/**
|
|
6
|
+
* v1.13.0 (NX-114): Nx before 22.7.10 has advisories that matter on CI agents (daemon sockets
|
|
7
|
+
* readable by other local users; command injection through git refs given to `nx affected`).
|
|
8
|
+
* CI files pstack generated (their first line names nx-polystack) get `NX_DAEMON=false`, and the
|
|
9
|
+
* generated Jenkinsfile resolves its base to a commit SHA before Nx sees it. Other CI files, and
|
|
10
|
+
* generated ones whose affected step was edited, are reported, not changed.
|
|
11
|
+
*/
|
|
12
|
+
const GENERATED = /^(#|\/\/) Generated by nx-polystack/;
|
|
13
|
+
const CI_YML = '.github/workflows/ci.yml';
|
|
14
|
+
const JENKINSFILE = 'Jenkinsfile';
|
|
15
|
+
/** The affected step `pstack infra` generated before 1.13.0 (any targets). */
|
|
16
|
+
const OLD_JENKINS_AFFECTED = /^(\s*)sh 'npx nx affected (.*?) --base="\$\{NX_BASE\}" --head=HEAD'\s*$/;
|
|
17
|
+
/** ci.yml: `NX_DAEMON: 'false'` in the `main` job's env. */
|
|
18
|
+
function hardenGithubCi(text) {
|
|
19
|
+
const res = { text, done: [], manual: [] };
|
|
20
|
+
if (/\bNX_DAEMON\b/.test(text))
|
|
21
|
+
return res;
|
|
22
|
+
const lines = text.split('\n');
|
|
23
|
+
const main = lines.findIndex((l) => /^ {2}main:\s*$/.test(l));
|
|
24
|
+
const end = (() => {
|
|
25
|
+
for (let i = main + 1; i < lines.length; i++)
|
|
26
|
+
if (/^ {0,2}\S/.test(lines[i]))
|
|
27
|
+
return i;
|
|
28
|
+
return lines.length;
|
|
29
|
+
})();
|
|
30
|
+
if (main < 0) {
|
|
31
|
+
res.manual.push(`${CI_YML}: no "main" job; add NX_DAEMON: 'false' to the job env by hand`);
|
|
32
|
+
return res;
|
|
33
|
+
}
|
|
34
|
+
const env = lines.findIndex((l, i) => i > main && i < end && /^ {4}env:\s*$/.test(l));
|
|
35
|
+
const entry = " NX_DAEMON: 'false'";
|
|
36
|
+
if (env >= 0) {
|
|
37
|
+
lines.splice(env + 1, 0, entry);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
const runsOn = lines.findIndex((l, i) => i > main && i < end && /^ {4}runs-on:/.test(l));
|
|
41
|
+
const at = runsOn >= 0 ? runsOn + 1 : main + 1;
|
|
42
|
+
lines.splice(at, 0, ' env:', entry);
|
|
43
|
+
}
|
|
44
|
+
res.text = lines.join('\n');
|
|
45
|
+
res.done.push(`${CI_YML}: NX_DAEMON=false`);
|
|
46
|
+
return res;
|
|
47
|
+
}
|
|
48
|
+
exports.hardenGithubCi = hardenGithubCi;
|
|
49
|
+
/** Jenkinsfile: `NX_DAEMON = 'false'` in environment, and the affected step on commit SHAs. */
|
|
50
|
+
function hardenJenkinsfile(text) {
|
|
51
|
+
const res = { text, done: [], manual: [] };
|
|
52
|
+
const lines = text.split('\n');
|
|
53
|
+
if (!/\bNX_DAEMON\b/.test(text)) {
|
|
54
|
+
const env = lines.findIndex((l) => /^\s*environment\s*\{\s*$/.test(l));
|
|
55
|
+
const indent = env >= 0 ? /^\s*/.exec(lines[env])?.[0] ?? '' : '';
|
|
56
|
+
const close = env >= 0 ? lines.findIndex((l, i) => i > env && l.replace(/\s+$/, '') === `${indent}}`) : -1;
|
|
57
|
+
if (close >= 0) {
|
|
58
|
+
lines.splice(close, 0, `${indent} NX_DAEMON = 'false'`);
|
|
59
|
+
res.done.push(`${JENKINSFILE}: NX_DAEMON=false`);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
res.manual.push(`${JENKINSFILE}: no environment block; set NX_DAEMON = 'false' by hand`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (!/BASE_SHA=\$\(git rev-parse/.test(text)) {
|
|
66
|
+
const at = lines.findIndex((l) => OLD_JENKINS_AFFECTED.test(l));
|
|
67
|
+
if (at >= 0) {
|
|
68
|
+
const [, indent, args] = OLD_JENKINS_AFFECTED.exec(lines[at]) ?? [];
|
|
69
|
+
lines[at] =
|
|
70
|
+
`${indent}sh 'BASE_SHA=$(git rev-parse --verify "\${NX_BASE}^{commit}") && ` +
|
|
71
|
+
`npx nx affected ${args} --base="$BASE_SHA" --head="$(git rev-parse --verify HEAD)"'`;
|
|
72
|
+
res.done.push(`${JENKINSFILE}: nx affected runs on commit SHAs`);
|
|
73
|
+
}
|
|
74
|
+
else if (/nx affected\b/.test(text)) {
|
|
75
|
+
res.manual.push(`${JENKINSFILE}: the nx affected step was edited; resolve --base/--head to commit SHAs ` +
|
|
76
|
+
'(git rev-parse --verify "<ref>^{commit}") by hand');
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
res.text = lines.join('\n');
|
|
80
|
+
return res;
|
|
81
|
+
}
|
|
82
|
+
exports.hardenJenkinsfile = hardenJenkinsfile;
|
|
83
|
+
async function ciNxHardening(tree) {
|
|
84
|
+
const say = (s) => devkit_1.logger.info(`ci-nx-hardening: ${s}`);
|
|
85
|
+
for (const [file, harden] of [
|
|
86
|
+
[CI_YML, hardenGithubCi],
|
|
87
|
+
[JENKINSFILE, hardenJenkinsfile],
|
|
88
|
+
]) {
|
|
89
|
+
const text = tree.read(file, 'utf8');
|
|
90
|
+
if (text === null)
|
|
91
|
+
continue;
|
|
92
|
+
if (!GENERATED.test(text)) {
|
|
93
|
+
if (!/\bNX_DAEMON\b/.test(text) && /nx affected\b/.test(text)) {
|
|
94
|
+
say(`${file} was not generated by pstack and is left as it is; for Nx before 22.7.10 set ` +
|
|
95
|
+
'NX_DAEMON=false and pass commit SHAs to nx affected (see SECURITY.md).');
|
|
96
|
+
}
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const res = harden(text);
|
|
100
|
+
if (res.text !== text)
|
|
101
|
+
tree.write(file, res.text);
|
|
102
|
+
for (const d of res.done)
|
|
103
|
+
say(d);
|
|
104
|
+
for (const m of res.manual)
|
|
105
|
+
say(m);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
exports.default = ciNxHardening;
|
|
109
|
+
//# sourceMappingURL=ci-nx-hardening.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ci-nx-hardening.js","sourceRoot":"","sources":["../../../../src/migrations/ci-nx-hardening/ci-nx-hardening.ts"],"names":[],"mappings":";;;AAAA,uCAA0C;AAE1C;;;;;;GAMG;AAEH,MAAM,SAAS,GAAG,qCA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C"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mouryaamit/nx-polystack",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.13.0",
|
|
4
4
|
"description": "Nx plugin and pstack CLI that scaffold and build multi-tech monorepos (Angular, Ionic, Flutter) with a hardened release pipeline.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "commonjs",
|