From 329e571dc0acc4d156104aa72148bd66d202ffc2 Mon Sep 17 00:00:00 2001 From: Remy Suen Date: Fri, 25 May 2018 12:25:59 -0400 Subject: [PATCH] Fix #39 Consider private registries when validating Signed-off-by: Remy Suen --- CHANGELOG.md | 1 + package-lock.json | 14 +++++++------- package.json | 4 ++-- src/dockerValidator.ts | 12 ++++++------ test/dockerValidator.test.ts | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70e529d..f92d771 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. ## [Unreleased] ### Fixed - fix incorrect validaiton warning in ARG, ENV, and LABEL instructions caused by quotes being used in variable replacements ([#36](https://github.com/rcjsuen/dockerfile-utils/issues/36)) +- fix incorrect validation of tagged images caused by FROM referencing images in a private registry ([#39](https://github.com/rcjsuen/dockerfile-utils/issues/39)) ## [0.0.9] - 2018-04-16 ### Fixed diff --git a/package-lock.json b/package-lock.json index e4dd2f0..c29735d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -69,11 +69,11 @@ "dev": true }, "dockerfile-ast": { - "version": "0.0.6", - "resolved": "https://registry.npmjs.org/dockerfile-ast/-/dockerfile-ast-0.0.6.tgz", - "integrity": "sha512-/8vO4mEniqhsKnqQJGhKHS7NfXVYCCzcclAoZjDg4NLteNiJT7p6EoUQEQG9me850DvIgWbz5axfN1BgrxIHvA==", + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/dockerfile-ast/-/dockerfile-ast-0.0.7.tgz", + "integrity": "sha512-NYWfYrbAvh+fQVweXb8rRYwxOwt1TwR/vI6QcCoBh7Y7V846Emr4tk5fly1f5cXLVkouv41EU2X2w4IbbywZyA==", "requires": { - "vscode-languageserver-types": "3.5.0" + "vscode-languageserver-types": "3.6.0" } }, "escape-string-regexp": { @@ -3068,9 +3068,9 @@ "dev": true }, "vscode-languageserver-types": { - "version": "3.5.0", - "resolved": "https://registry.npmjs.org/vscode-languageserver-types/-/vscode-languageserver-types-3.5.0.tgz", - "integrity": "sha1-5I15li8LjgLelV4/UkkI4rGcA3Q=" + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/vscode-languageserver-types/-/vscode-languageserver-types-3.6.0.tgz", + "integrity": "sha512-GSgQtGmtza4PoNH0+iHWylWg/1sw2DODezqYWRxbN910dPchI3CQaSJN76csKcQGv55wsWgX82T6n74q8mFSpw==" }, "wrappy": { "version": "1.0.2", diff --git a/package.json b/package.json index 39cb924..86d0cdb 100644 --- a/package.json +++ b/package.json @@ -27,8 +27,8 @@ "main": "./lib/main.js", "types": "./lib/main.d.ts", "dependencies": { - "dockerfile-ast": "0.0.6", - "vscode-languageserver-types": "3.5.0" + "dockerfile-ast": "0.0.7", + "vscode-languageserver-types": "3.6.0" }, "devDependencies": { "@types/mocha": "^2.2.33", diff --git a/src/dockerValidator.ts b/src/dockerValidator.ts index f7cf063..c075de1 100644 --- a/src/dockerValidator.ts +++ b/src/dockerValidator.ts @@ -5,7 +5,7 @@ import { TextDocument, Range, Position, Diagnostic, DiagnosticSeverity } from 'vscode-languageserver-types'; -import { Dockerfile, Flag, Instruction, JSONInstruction, Add, Cmd, Copy, Entrypoint, Env, From, Healthcheck, Label, Onbuild, ModifiableInstruction, DockerfileParser, Directive } from 'dockerfile-ast'; +import { Dockerfile, Flag, Instruction, JSONInstruction, Add, Cmd, Copy, Entrypoint, From, Healthcheck, Onbuild, ModifiableInstruction, PropertyInstruction, DockerfileParser, Directive } from 'dockerfile-ast'; import { ValidationCode, ValidationSeverity, ValidatorSettings } from './main'; export const KEYWORDS = [ @@ -90,7 +90,7 @@ export class Validator { */ private checkArguments(instruction: Instruction, problems: Diagnostic[], expectedArgCount: number[], validate: Function, createIncompleteDiagnostic?: Function): void { - let args = instruction.getArguments(); + let args = instruction instanceof PropertyInstruction ? instruction.getPropertyArguments() : instruction.getArguments(); if (args.length === 0) { // all instructions are expected to have at least one argument let range = instruction.getInstructionRange(); @@ -318,7 +318,7 @@ export class Validator { this.checkArguments(instruction, problems, [-1], function (): any { return null; }); - let properties = instruction instanceof Env ? (instruction as Env).getProperties() : (instruction as Label).getProperties(); + let properties = (instruction as PropertyInstruction).getProperties(); if (properties.length === 1 && properties[0].getValue() === null) { let range = properties[0].getNameRange(); problems.push(Validator.createENVRequiresTwoArguments(range.start, range.end)); @@ -366,11 +366,11 @@ export class Validator { let from = instruction as From; let index = argument.indexOf('@'); if (index === -1) { - index = argument.indexOf(':'); - if (index === -1) { + let tagRange = from.getImageTagRange(); + if (tagRange === null) { return null; } - let tag = argument.substring(index + 1); + let tag = document.getText(tagRange); if (tag.indexOf('$') !== -1) { return null; } else if (tag === "") { diff --git a/test/dockerValidator.test.ts b/test/dockerValidator.test.ts index 13a749b..1e3589a 100644 --- a/test/dockerValidator.test.ts +++ b/test/dockerValidator.test.ts @@ -2534,6 +2534,42 @@ describe("Docker Validator Tests", function() { diagnostics = validateDockerfile("FROM node@sha256:613685c22f65d01f2264bdd49b8a336488e14faf29f3ff9b6bf76a4da23c4700"); assert.equal(diagnostics.length, 0); + diagnostics = validateDockerfile("FROM privateregistry.com/image:tag"); + assert.equal(diagnostics.length, 0); + + diagnostics = validateDockerfile("FROM privateregistry.com/image@sha256:613685c22f65d01f2264bdd49b8a336488e14faf29f3ff9b6bf76a4da23c4700"); + assert.equal(diagnostics.length, 0); + + diagnostics = validateDockerfile("FROM privateregistry.com:5000/image:tag"); + assert.equal(diagnostics.length, 0); + + diagnostics = validateDockerfile("FROM privateregistry.com:5000/image@sha256:613685c22f65d01f2264bdd49b8a336488e14faf29f3ff9b6bf76a4da23c4700"); + assert.equal(diagnostics.length, 0); + + diagnostics = validateDockerfile("FROM localhost/node:9"); + assert.equal(diagnostics.length, 0); + + diagnostics = validateDockerfile("FROM localhost/node@sha256:613685c22f65d01f2264bdd49b8a336488e14faf29f3ff9b6bf76a4da23c4700"); + assert.equal(diagnostics.length, 0); + + diagnostics = validateDockerfile("FROM localhost:1234/node:9"); + assert.equal(diagnostics.length, 0); + + diagnostics = validateDockerfile("FROM localhost:1234/node@sha256:613685c22f65d01f2264bdd49b8a336488e14faf29f3ff9b6bf76a4da23c4700"); + assert.equal(diagnostics.length, 0); + + diagnostics = validateDockerfile("FROM 123.22.33.123/user/image:tag2"); + assert.equal(diagnostics.length, 0); + + diagnostics = validateDockerfile("FROM 123.22.33.123/user/image@sha256:613685c22f65d01f2264bdd49b8a336488e14faf29f3ff9b6bf76a4da23c4700"); + assert.equal(diagnostics.length, 0); + + diagnostics = validateDockerfile("FROM 123.22.33.123:2345/user/image:tag2"); + assert.equal(diagnostics.length, 0); + + diagnostics = validateDockerfile("FROM 123.22.33.123:2345/user/image@sha256:613685c22f65d01f2264bdd49b8a336488e14faf29f3ff9b6bf76a4da23c4700"); + assert.equal(diagnostics.length, 0); + diagnostics = validateDockerfile("ARG image=node\nFROM $image"); assert.equal(diagnostics.length, 0);