From e6fb2fd135e604dee4672c484df87c68c68f2757 Mon Sep 17 00:00:00 2001 From: Jeff MAURY Date: Thu, 16 Nov 2023 16:22:29 +0100 Subject: [PATCH] fix: LABEL instruction compiled by Docker cannot be parsed Fixes #16 Signed-off-by: Jeff MAURY --- pkg/command/analyzer_test.go | 21 +++++++++++++++++++++ pkg/decompiler/utils/utils.go | 35 +++++++++++++++++++++++++++++------ 2 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 pkg/command/analyzer_test.go diff --git a/pkg/command/analyzer_test.go b/pkg/command/analyzer_test.go new file mode 100644 index 0000000..85f5015 --- /dev/null +++ b/pkg/command/analyzer_test.go @@ -0,0 +1,21 @@ +/******************************************************************************* + * Copyright (c) 2022 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. + ******************************************************************************/ +package command + +import "testing" + +func TestCheckNginx(t *testing.T) { + for _, tag := range []string{"1.25.0", "1.25.1", "1.25.2", "1.25.3"} { + t.Run(tag, func(t *testing.T) { + AnalyzeImage("docker.io/nginx:" + tag) + }) + } +} diff --git a/pkg/decompiler/utils/utils.go b/pkg/decompiler/utils/utils.go index a5a8df3..c88f1cc 100644 --- a/pkg/decompiler/utils/utils.go +++ b/pkg/decompiler/utils/utils.go @@ -3,6 +3,7 @@ package utils import ( "github.com/moby/buildkit/frontend/dockerfile/parser" "github.com/redhat-developer/docker-openshift-analyzer/pkg/utils" + "regexp" "strings" ) @@ -26,21 +27,43 @@ var CONTAINERFILE_INSTRUCTIONS = []string{ utils.SHELL_INSTRUCTION, } +var LABEL_PATTERN = regexp.MustCompile("^LABEL\\s+(.*)=(.*)$") + func Line2Node(line string, root *parser.Node) error { - result, err := parser.Parse(strings.NewReader(line)) - if err != nil { - return err + if strings.HasPrefix(line, utils.LABEL_INSTRUCTION) { + return parseLabel(line, root) + } else { + result, err := parser.Parse(strings.NewReader(line)) + if err != nil { + return err + } + for _, node := range result.AST.Children { + root.AddChild(node, -1, -1) + } + } + return nil +} + +func parseLabel(line string, root *parser.Node) error { + elements := LABEL_PATTERN.FindStringSubmatch(line) + parent := &parser.Node{ + Value: "LABEL", } - for _, node := range result.AST.Children { - root.AddChild(node, -1, -1) + node := parent + for _, element := range elements[1:] { + node.Next = &parser.Node{ + Value: element, + } + node = node.Next } + root.AddChild(parent, 0, 0) return nil } func ExtractCmd(str string) string { index := strings.Index(str, utils.NOP) if index > 0 { - return str[index+len(utils.NOP):] + return strings.TrimSpace(str[index+len(utils.NOP):]) } index = strings.Index(str, utils.RUN_PREFIX) if index >= 0 {