|
@ -19,7 +19,6 @@ package dockerfile |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"errors" |
|
|
"errors" |
|
|
"fmt" |
|
|
|
|
|
"os" |
|
|
"os" |
|
|
"strings" |
|
|
"strings" |
|
|
|
|
|
|
|
@ -33,63 +32,118 @@ import ( |
|
|
|
|
|
|
|
|
// Parse reads a Dockerfile, interprets the AST and returns information about the image(s) and package(s) defined
|
|
|
// Parse reads a Dockerfile, interprets the AST and returns information about the image(s) and package(s) defined
|
|
|
func Parse(dockerfilePath string) (parser.ParseResult, error) { |
|
|
func Parse(dockerfilePath string) (parser.ParseResult, error) { |
|
|
dockerfile, dockerfileOpenErr := os.Open(dockerfilePath) |
|
|
|
|
|
if dockerfileOpenErr != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error reading file %s: %s", dockerfilePath, dockerfileOpenErr) |
|
|
|
|
|
return parser.ParseResult{}, dockerfileOpenErr |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parseLogger := log.WithFields(log.Fields{ |
|
|
|
|
|
"filePath": dockerfilePath, |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
dockerfile, err := os.Open(dockerfilePath) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
log.WithFields(log.Fields{ |
|
|
|
|
|
"sourceError": err, |
|
|
|
|
|
}).Fatal("unexpected error reading file") |
|
|
|
|
|
return parser.ParseResult{}, err |
|
|
} |
|
|
} |
|
|
defer func(dockerfile *os.File) { |
|
|
defer func(dockerfile *os.File) { |
|
|
dockerfileCloseErr := dockerfile.Close() |
|
|
|
|
|
if dockerfileCloseErr != nil { |
|
|
|
|
|
log.Error("Unexpected error closing file %s: %s", dockerfilePath, dockerfileCloseErr) |
|
|
|
|
|
|
|
|
err := dockerfile.Close() |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
parseLogger.WithFields(log.Fields{ |
|
|
|
|
|
"sourceError": err, |
|
|
|
|
|
}).Error("unexpected error closing file") |
|
|
} |
|
|
} |
|
|
}(dockerfile) |
|
|
}(dockerfile) |
|
|
|
|
|
|
|
|
parsedASTResult, ASTParseErr := buildkitParser.Parse(dockerfile) |
|
|
|
|
|
if ASTParseErr != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error parsing Dockerfile %s: %s", dockerfilePath, ASTParseErr) |
|
|
|
|
|
return parser.ParseResult{}, ASTParseErr |
|
|
|
|
|
|
|
|
parsedASTResult, err := buildkitParser.Parse(dockerfile) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
parseLogger.WithFields(log.Fields{ |
|
|
|
|
|
"sourceError": err, |
|
|
|
|
|
}).Fatal("unexpected error parsing Dockerfile") |
|
|
|
|
|
return parser.ParseResult{}, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
parseResult, parseErr := parseDockerfileAST(parsedASTResult.AST) |
|
|
|
|
|
if parseErr != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error while parsing AST for %s: %s", dockerfilePath, parseErr) |
|
|
|
|
|
return parser.ParseResult{}, parseErr |
|
|
|
|
|
|
|
|
parseResult, err := parseDockerfileAST(parseLogger, dockerfilePath, parsedASTResult.AST) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
parseLogger.WithFields(log.Fields{ |
|
|
|
|
|
"sourceError": err, |
|
|
|
|
|
}).Fatal("unexpected error while parsing AST") |
|
|
|
|
|
return parser.ParseResult{}, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return parseResult, nil |
|
|
return parseResult, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// parseDockerfileAST Read the parsed AST and extract the required information from each stage
|
|
|
|
|
|
func parseDockerfileAST(dockerfileAST *buildkitParser.Node) (parser.ParseResult, error) { |
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
General Helpers |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
// getCommandLineLocation parse commandLineLocation information into the structure we will persist
|
|
|
|
|
|
func getContentLocation(location []buildkitParser.Range) (parser.ContentLocation, error) { |
|
|
|
|
|
|
|
|
|
|
|
commandLocationStart := location[0].Start |
|
|
|
|
|
commandLocationEnd := location[len(location)-1].End |
|
|
|
|
|
|
|
|
|
|
|
return parser.ContentLocation{ |
|
|
|
|
|
StartLine: commandLocationStart.Line, |
|
|
|
|
|
StartCharacter: commandLocationStart.Character, |
|
|
|
|
|
EndLine: commandLocationEnd.Line, |
|
|
|
|
|
EndCharacter: commandLocationEnd.Character, |
|
|
|
|
|
}, nil |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
Parsing Helper |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
dockerfileStages, _, stageParsingErr := buildkitInstructions.Parse(dockerfileAST) |
|
|
|
|
|
if stageParsingErr != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error while parsing Dockerfile Instructions: %s", stageParsingErr) |
|
|
|
|
|
return parser.ParseResult{}, stageParsingErr |
|
|
|
|
|
|
|
|
// parseDockerfileAST Read the parsed AST and extract the required information from each stage
|
|
|
|
|
|
func parseDockerfileAST(logger *log.Entry, dockerFilePath string, dockerfileAST *buildkitParser.Node) (parser.ParseResult, error) { |
|
|
|
|
|
|
|
|
|
|
|
dockerfileStages, _, err := buildkitInstructions.Parse(dockerfileAST) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
logger.WithFields(log.Fields{ |
|
|
|
|
|
"sourceError": err, |
|
|
|
|
|
}).Fatal("unexpected error while parsing Dockerfile instructions") |
|
|
|
|
|
return parser.ParseResult{}, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var stages []*parser.Stage |
|
|
var stages []*parser.Stage |
|
|
for stageIndex, stage := range dockerfileStages { |
|
|
for stageIndex, stage := range dockerfileStages { |
|
|
stage, stageErr := parseDockerfileStage(stage) |
|
|
|
|
|
if stageErr != nil { |
|
|
|
|
|
log.Fatal("Unexpected error while parsing stage %d", stageIndex) |
|
|
|
|
|
return parser.ParseResult{}, stageErr |
|
|
|
|
|
|
|
|
stageLogger := logger.WithFields(log.Fields{ |
|
|
|
|
|
"stageIndex": stageIndex, |
|
|
|
|
|
"stageName": stage.Name, |
|
|
|
|
|
}) |
|
|
|
|
|
stage, err := parseDockerfileStage(stageLogger, stageIndex, stage) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
stageLogger.WithFields(log.Fields{ |
|
|
|
|
|
"sourceError": err, |
|
|
|
|
|
}).Fatal("unexpected error while parsing stage") |
|
|
|
|
|
return parser.ParseResult{}, err |
|
|
} |
|
|
} |
|
|
stages = append(stages, &stage) |
|
|
stages = append(stages, &stage) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return parser.ParseResult{Stages: stages}, nil |
|
|
|
|
|
|
|
|
return parser.ParseResult{FilePath: dockerFilePath, Stages: stages}, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func parseDockerfileStage(dockerfileStage buildkitInstructions.Stage) (parser.Stage, error) { |
|
|
|
|
|
|
|
|
func parseDockerfileStage(logger *log.Entry, dockerfileStageIndex int, dockerfileStage buildkitInstructions.Stage) (parser.Stage, error) { |
|
|
|
|
|
|
|
|
|
|
|
stageLocation, err := getContentLocation(dockerfileStage.Location) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
logger.WithFields(log.Fields{ |
|
|
|
|
|
"rawStageLocation": dockerfileStage.Location, |
|
|
|
|
|
}).Fatal("unexpected failure parsing stage location information") |
|
|
|
|
|
return parser.Stage{}, err |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
stageLogger := logger.WithFields(log.Fields{ |
|
|
|
|
|
"stageImage": dockerfileStage.BaseName, |
|
|
|
|
|
"stageLocation": stageLocation, |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
imageParts := strings.Split(dockerfileStage.BaseName, ":") |
|
|
imageParts := strings.Split(dockerfileStage.BaseName, ":") |
|
|
|
|
|
|
|
|
if len(imageParts) < 2 { |
|
|
if len(imageParts) < 2 { |
|
|
message := fmt.Sprintf("Not enough information in image (%s) to determine base", imageParts) |
|
|
|
|
|
log.Fatal(message) |
|
|
|
|
|
return parser.Stage{}, errors.New(message) |
|
|
|
|
|
|
|
|
stageLogger.Fatal("not enough information in image to determine base") |
|
|
|
|
|
return parser.Stage{}, errors.New("not enough information in image to determine base") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
name := dockerfileStage.Name |
|
|
name := dockerfileStage.Name |
|
@ -98,80 +152,125 @@ func parseDockerfileStage(dockerfileStage buildkitInstructions.Stage) (parser.St |
|
|
Tag: imageParts[1], |
|
|
Tag: imageParts[1], |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
commandParser, commandParserRetrievalErr := parser.GetCommandParser(image) |
|
|
|
|
|
if commandParserRetrievalErr != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error while determining command parser for image \"%s\": %s", image, commandParserRetrievalErr) |
|
|
|
|
|
return parser.Stage{}, commandParserRetrievalErr |
|
|
|
|
|
|
|
|
commandParser, err := parser.GetCommandParser(image) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
stageLogger.WithFields(log.Fields{ |
|
|
|
|
|
"sourceError": err, |
|
|
|
|
|
}).Fatal("unexpected error while determining command parser for image") |
|
|
|
|
|
return parser.Stage{}, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
repositories, repositoryCommandParseErr := parseRepositoriesFromDockerfileStage(dockerfileStage, commandParser) |
|
|
|
|
|
if repositoryCommandParseErr != nil { |
|
|
|
|
|
message := fmt.Sprintf("Unexpected error while parsing repositories from stage: %s", repositoryCommandParseErr) |
|
|
|
|
|
log.Fatal(message) |
|
|
|
|
|
return parser.Stage{}, errors.New(message) |
|
|
|
|
|
|
|
|
repositories, repositoryCommandLocations, err := parseRepositoriesFromDockerfileStage(stageLogger, dockerfileStage, commandParser) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
stageLogger.WithFields(log.Fields{ |
|
|
|
|
|
"sourceError": err, |
|
|
|
|
|
}).Fatal("unexpected error while parsing repositories from stage") |
|
|
|
|
|
return parser.Stage{}, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
packages, packageCommandParseErr := parsePackagesFromDockerfileStage(dockerfileStage, commandParser) |
|
|
|
|
|
if packageCommandParseErr != nil { |
|
|
|
|
|
message := fmt.Sprintf("Unexpected error while parsing packages from stage: %s", packageCommandParseErr) |
|
|
|
|
|
log.Fatal(message) |
|
|
|
|
|
return parser.Stage{}, errors.New(message) |
|
|
|
|
|
|
|
|
packages, packageCommandLocations, err := parsePackagesFromDockerfileStage(stageLogger, dockerfileStage, commandParser) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
stageLogger.WithFields(log.Fields{ |
|
|
|
|
|
"sourceError": err, |
|
|
|
|
|
}).Fatal("unexpected error while parsing packages from stage") |
|
|
|
|
|
return parser.Stage{}, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var commandLocations []*parser.ContentLocation |
|
|
|
|
|
commandLocations = append(commandLocations, repositoryCommandLocations...) |
|
|
|
|
|
commandLocations = append(commandLocations, packageCommandLocations...) |
|
|
|
|
|
|
|
|
return parser.Stage{ |
|
|
return parser.Stage{ |
|
|
Name: name, |
|
|
|
|
|
Image: &image, |
|
|
|
|
|
Repositories: repositories, |
|
|
|
|
|
Packages: packages, |
|
|
|
|
|
|
|
|
Index: dockerfileStageIndex, |
|
|
|
|
|
Name: name, |
|
|
|
|
|
Image: &image, |
|
|
|
|
|
StageLocation: stageLocation, |
|
|
|
|
|
Repositories: repositories, |
|
|
|
|
|
Packages: packages, |
|
|
|
|
|
CommandLocations: commandLocations, |
|
|
}, nil |
|
|
}, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func parseRepositoriesFromDockerfileStage(dockerfileStage buildkitInstructions.Stage, commandParser *parser.CommandParser) ([]*parser.Repository, error) { |
|
|
|
|
|
|
|
|
func parseRepositoriesFromDockerfileStage(logger *log.Entry, dockerfileStage buildkitInstructions.Stage, commandParser *parser.CommandParser) ([]*parser.Repository, []*parser.ContentLocation, error) { |
|
|
var repositories []*parser.Repository |
|
|
var repositories []*parser.Repository |
|
|
|
|
|
var commandLocations []*parser.ContentLocation |
|
|
|
|
|
|
|
|
typedCommandParser := *commandParser |
|
|
typedCommandParser := *commandParser |
|
|
|
|
|
|
|
|
for _, command := range dockerfileStage.Commands { |
|
|
for _, command := range dockerfileStage.Commands { |
|
|
switch command.(type) { |
|
|
|
|
|
|
|
|
switch typedCommand := command.(type) { |
|
|
case *buildkitInstructions.RunCommand: |
|
|
case *buildkitInstructions.RunCommand: |
|
|
runCommand := command.(*buildkitInstructions.RunCommand) |
|
|
|
|
|
|
|
|
|
|
|
log.Tracef("Parsing RunCommand \"%s\" for repositories", runCommand.CmdLine) |
|
|
|
|
|
|
|
|
|
|
|
parsedRepositories, repositoryParseErr := typedCommandParser.GetRepositories(runCommand.CmdLine[0]) |
|
|
|
|
|
if repositoryParseErr != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error while parsing repositories: %s", repositoryParseErr) |
|
|
|
|
|
return nil, repositoryParseErr |
|
|
|
|
|
|
|
|
commandLocation, err := getContentLocation(typedCommand.Location()) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
logger.WithFields(log.Fields{ |
|
|
|
|
|
"rawCommandLineLocation": typedCommand.Location(), |
|
|
|
|
|
}).Fatal("unexpected failure parsing script location information") |
|
|
|
|
|
return repositories, commandLocations, err |
|
|
} |
|
|
} |
|
|
for _, parsedRepository := range parsedRepositories { |
|
|
|
|
|
repositories = append(repositories, parsedRepository) |
|
|
|
|
|
|
|
|
commandLocations = append(commandLocations, &commandLocation) |
|
|
|
|
|
|
|
|
|
|
|
for _, line := range typedCommand.CmdLine { |
|
|
|
|
|
logger.WithFields(log.Fields{ |
|
|
|
|
|
"commandLine": line, |
|
|
|
|
|
"commandLocation": commandLocation, |
|
|
|
|
|
}).Trace("parsing RunCommand for repositories") |
|
|
|
|
|
parsedRepositories, err := typedCommandParser.GetRepositories(logger, line) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
logger.WithFields(log.Fields{ |
|
|
|
|
|
"commandLine": line, |
|
|
|
|
|
"commandLocation": commandLocation, |
|
|
|
|
|
"sourceError": err, |
|
|
|
|
|
}).Fatal("unexpected error while parsing repositories") |
|
|
|
|
|
return repositories, commandLocations, err |
|
|
|
|
|
} |
|
|
|
|
|
for _, parsedRepository := range parsedRepositories { |
|
|
|
|
|
repositories = append(repositories, parsedRepository) |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return repositories, nil |
|
|
|
|
|
|
|
|
return repositories, commandLocations, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func parsePackagesFromDockerfileStage(dockerfileStage buildkitInstructions.Stage, commandParser *parser.CommandParser) ([]*parser.Package, error) { |
|
|
|
|
|
|
|
|
func parsePackagesFromDockerfileStage(logger *log.Entry, dockerfileStage buildkitInstructions.Stage, commandParser *parser.CommandParser) ([]*parser.Package, []*parser.ContentLocation, error) { |
|
|
var packages []*parser.Package |
|
|
var packages []*parser.Package |
|
|
|
|
|
var commandLocations []*parser.ContentLocation |
|
|
|
|
|
|
|
|
typedCommandParser := *commandParser |
|
|
typedCommandParser := *commandParser |
|
|
|
|
|
|
|
|
for _, command := range dockerfileStage.Commands { |
|
|
for _, command := range dockerfileStage.Commands { |
|
|
switch command.(type) { |
|
|
|
|
|
|
|
|
switch typedCommand := command.(type) { |
|
|
case *buildkitInstructions.RunCommand: |
|
|
case *buildkitInstructions.RunCommand: |
|
|
runCommand := command.(*buildkitInstructions.RunCommand) |
|
|
|
|
|
|
|
|
|
|
|
log.Tracef("Parsing RunCommand \"%s\" for packages", runCommand.CmdLine) |
|
|
|
|
|
|
|
|
|
|
|
parsedPinnedPackages, packageParseErr := typedCommandParser.GetPinnedPackages(runCommand.CmdLine[0]) |
|
|
|
|
|
if packageParseErr != nil { |
|
|
|
|
|
log.Fatalf("Unexpected error while parsing pinned packages: %s", packageParseErr) |
|
|
|
|
|
return nil, packageParseErr |
|
|
|
|
|
|
|
|
commandLocation, err := getContentLocation(typedCommand.Location()) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
logger.WithFields(log.Fields{ |
|
|
|
|
|
"rawCommandLineLocation": typedCommand.Location(), |
|
|
|
|
|
}).Fatal("unexpected failure parsing script location information") |
|
|
|
|
|
return packages, commandLocations, err |
|
|
} |
|
|
} |
|
|
for _, parsedPinnedPackage := range parsedPinnedPackages { |
|
|
|
|
|
packages = append(packages, parsedPinnedPackage) |
|
|
|
|
|
|
|
|
commandLocations = append(commandLocations, &commandLocation) |
|
|
|
|
|
|
|
|
|
|
|
for _, line := range typedCommand.CmdLine { |
|
|
|
|
|
logger.WithFields(log.Fields{ |
|
|
|
|
|
"commandLine": line, |
|
|
|
|
|
"commandLocation": commandLocation, |
|
|
|
|
|
}).Trace("parsing RunCommand for packages") |
|
|
|
|
|
parsedPinnedPackages, err := typedCommandParser.GetPinnedPackages(logger, line) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
logger.WithFields(log.Fields{ |
|
|
|
|
|
"commandLine": line, |
|
|
|
|
|
"commandLocation": commandLocation, |
|
|
|
|
|
"sourceError": err, |
|
|
|
|
|
}).Fatal("unexpected error while parsing pinned packages") |
|
|
|
|
|
return packages, commandLocations, err |
|
|
|
|
|
} |
|
|
|
|
|
for _, parsedPinnedPackage := range parsedPinnedPackages { |
|
|
|
|
|
packages = append(packages, parsedPinnedPackage) |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return packages, nil |
|
|
|
|
|
|
|
|
return packages, commandLocations, nil |
|
|
} |
|
|
} |