Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: LABEL instruction compiled by Docker cannot be parsed #22

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions pkg/command/analyzer_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
24 changes: 23 additions & 1 deletion pkg/decompiler/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -26,7 +27,12 @@ var CONTAINERFILE_INSTRUCTIONS = []string{
utils.SHELL_INSTRUCTION,
}

var LABEL_PATTERN = regexp.MustCompile("^LABEL\\s+(.*)=(.*)$")

func Line2Node(line string, root *parser.Node) error {
if strings.HasPrefix(line, utils.LABEL_INSTRUCTION) {
return parseLabel(line, root)
}
result, err := parser.Parse(strings.NewReader(line))
if err != nil {
return err
Expand All @@ -37,10 +43,26 @@ func Line2Node(line string, root *parser.Node) error {
return nil
}

func parseLabel(line string, root *parser.Node) error {
elements := LABEL_PATTERN.FindStringSubmatch(line)
parent := &parser.Node{
Value: "LABEL",
}
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 {
Expand Down