Skip to content

Commit f561aa4

Browse files
Enforce CFSClean for Network Isolation (#504)
* Enforce CFSClean for Network Isolation * use authenticated npmrc for openapidiff * Update test-step.yml formating * Revert to using autorest v2 * Use microsoft package feed proxy
1 parent aa98dc4 commit f561aa4

4 files changed

Lines changed: 89 additions & 1 deletion

File tree

eng/1es-redirect.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extends:
3636
- 1ES.PT.Tag-refs/tags/canary
3737
settings:
3838
skipBuildTagsForGitHubPullRequests: true
39+
networkIsolationPolicy: Permissive, CFSClean
3940
sdl:
4041
git:
4142
longpaths: true

eng/test-steps.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,33 @@ steps:
88
- task: UseDotNet@2
99
inputs:
1010
version: 6.x
11+
1112
- template: /eng/common/pipelines/templates/steps/create-authenticated-npmrc.yml
13+
parameters:
14+
npmrcPath: $(Agent.TempDirectory)/oad.npmrc
15+
1216
- script: npm ci
1317
displayName: npm ci
18+
env:
19+
NPM_CONFIG_USERCONFIG: $(Agent.TempDirectory)/oad.npmrc
20+
1421
- script: npm run lint
1522
displayName: lint
23+
1624
- script: npm run prettier
1725
displayName: prettier
26+
1827
- script: npm test
1928
displayName: test
29+
env:
30+
NPM_CONFIG_USERCONFIG: $(Agent.TempDirectory)/oad.npmrc
31+
autorest_registry: https://packagefeedproxy.microsoft.io/npm/
32+
2033
- script: npm pack
2134
displayName: pack
35+
env:
36+
NPM_CONFIG_USERCONFIG: $(Agent.TempDirectory)/oad.npmrc
37+
2238
- task: CopyFiles@2
2339
displayName: "Copy Files to Staging"
2440
inputs:

src/lib/validators/openApiDiff.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,31 @@ const _ = require("lodash")
1919

2020
const execFile = util.promisify(child_process.execFile)
2121

22+
const getAutoRestNpmrcPath = (): string | undefined => {
23+
const candidates = [process.env.npm_config_userconfig, process.env.NPM_CONFIG_USERCONFIG]
24+
return candidates.find(value => typeof value === "string" && value.trim().length > 0)
25+
}
26+
27+
export const getAutoRestRegistry = (npmrcPath = getAutoRestNpmrcPath()): string | undefined => {
28+
const configuredRegistry = [process.env.autorest_registry, process.env.npm_config_registry, process.env.NPM_CONFIG_REGISTRY].find(
29+
value => typeof value === "string" && value.trim().length > 0
30+
)
31+
if (configuredRegistry) {
32+
return configuredRegistry.trim()
33+
}
34+
35+
if (!npmrcPath || !fs.existsSync(npmrcPath)) {
36+
return undefined
37+
}
38+
39+
const registryEntry = fs
40+
.readFileSync(npmrcPath, "utf8")
41+
.split(/\r?\n/u)
42+
.map(line => line.match(/^\s*registry\s*=\s*["']?([^"'#;]+)["']?\s*(?:[#;].*)?$/iu)?.[1]?.trim())
43+
.find(value => value)
44+
return registryEntry
45+
}
46+
2247
export type Options = {
2348
readonly consoleLogLevel?: unknown
2449
readonly logFilepath?: unknown
@@ -241,13 +266,28 @@ export class OpenApiDiff {
241266
]
242267

243268
const args = [...autoRestArgs, ...swaggerArgs, ...commonArgs]
269+
const autoRestNpmrcPath = getAutoRestNpmrcPath()
270+
const autoRestRegistry = getAutoRestRegistry(autoRestNpmrcPath)
271+
const env = {
272+
...process.env,
273+
NODE_OPTIONS: "--max-old-space-size=8192",
274+
...(autoRestNpmrcPath ? { npm_config_userconfig: autoRestNpmrcPath, NPM_CONFIG_USERCONFIG: autoRestNpmrcPath } : {}),
275+
...(autoRestRegistry ? { autorest_registry: autoRestRegistry } : {})
276+
}
277+
278+
if (autoRestNpmrcPath) {
279+
log.debug(`Using npm user config for AutoRest: ${autoRestNpmrcPath}`)
280+
}
281+
if (autoRestRegistry) {
282+
log.debug(`Using npm registry for AutoRest core: ${autoRestRegistry}`)
283+
}
244284

245285
log.debug(`Executing: "${autoRestFile} ${args.join(" ")}"`)
246286

247287
const { stderr } = await execFile(autoRestFile, args, {
248288
encoding: "utf8",
249289
maxBuffer: 1024 * 1024 * 64,
250-
env: { ...process.env, NODE_OPTIONS: "--max-old-space-size=8192" }
290+
env
251291
})
252292
if (stderr) {
253293
// autorest 3.8.0 emits deprecation message to stderr with exit code 0

src/test/openApiDiffTest.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as assert from "assert"
2+
import * as fs from "fs"
3+
import * as os from "os"
4+
import * as path from "path"
5+
import { getAutoRestRegistry } from "../lib/validators/openApiDiff"
6+
7+
describe("OpenApiDiff", () => {
8+
it("gets the AutoRest core registry from the npm user config", () => {
9+
const tempFolder = fs.mkdtempSync(path.join(os.tmpdir(), "oad-npmrc-"))
10+
const npmrcPath = path.join(tempFolder, ".npmrc")
11+
fs.writeFileSync(npmrcPath, "@azure:registry=https://example.invalid/scoped/\nregistry=https://packagefeedproxy.microsoft.io/npm/\n")
12+
13+
const environmentVariables = ["autorest_registry", "npm_config_registry", "NPM_CONFIG_REGISTRY"] as const
14+
const originalValues = environmentVariables.map(name => process.env[name])
15+
environmentVariables.forEach(name => delete process.env[name])
16+
17+
try {
18+
assert.equal(getAutoRestRegistry(npmrcPath), "https://packagefeedproxy.microsoft.io/npm/")
19+
} finally {
20+
environmentVariables.forEach((name, index) => {
21+
const originalValue = originalValues[index]
22+
if (originalValue === undefined) {
23+
delete process.env[name]
24+
} else {
25+
process.env[name] = originalValue
26+
}
27+
})
28+
fs.rmSync(tempFolder, { recursive: true, force: true })
29+
}
30+
})
31+
})

0 commit comments

Comments
 (0)