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

Add 'ADD --checksum' support #92

Merged
merged 6 commits into from
Apr 24, 2024
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
1 change: 1 addition & 0 deletions integration-tests/parse_files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function clone_repos() {
git_clone https://github.com/ArchiveTeam/warrior-dockerfile.git &
git_clone https://github.com/wckr/wocker-dockerfile.git &
git_clone https://github.com/kartoza/docker-postgis.git &
git_clone https://github.com/andrericardo/docker-subtitleedit &

# colliding names
git_named_clone https://github.com/yaronr/dockerfile.git yaronr-dockerfile &
Expand Down
6 changes: 3 additions & 3 deletions language-docker.cabal
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.35.0.
-- This file has been generated from package.yaml by hpack version 0.36.0.
--
-- see: https://github.com/sol/hpack
--
-- hash: 37c989b46891ef750c66b64a1dcbdfc30c1ee22a0ed4821aeb27c7c63c02d2a9
-- hash: 528820a99687ed9372038487df42e487d63b867ed312d39e8e51b6d1bf4cf7d9

name: language-docker
version: 12.1.0
version: 12.2.0
synopsis: Dockerfile parser, pretty-printer and embedded DSL
description: All functions for parsing and pretty-printing Dockerfiles are exported through @Language.Docker@. For more fine-grained operations look for specific modules that implement a certain functionality.
See the <https://github.com/hadolint/language-docker GitHub project> for the source-code and examples.
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: language-docker
version: '12.1.0'
version: '12.2.0'
synopsis: Dockerfile parser, pretty-printer and embedded DSL
description: 'All functions for parsing and pretty-printing Dockerfiles are
exported through @Language.Docker@. For more fine-grained operations look for
Expand Down
31 changes: 22 additions & 9 deletions src/Language/Docker/Parser/Copy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import Language.Docker.Syntax

data Flag
= FlagChown Chown
= FlagChecksum Checksum
| FlagChown Chown
| FlagChmod Chmod
| FlagLink Link
| FlagSource CopySource
Expand All @@ -20,7 +21,7 @@
parseCopy = do
reserved "COPY"
flags <- copyFlag `sepEndBy` requiredWhitespace
let chownFlags = [c | FlagChown c <- flags]

Check warning on line 24 in src/Language/Docker/Parser/Copy.hs

View workflow job for this annotation

GitHub Actions / hlint

Suggestion in parseCopy, parseAdd in module Language.Docker.Parser.Copy: Reduce duplication ▫︎ Found: "let chownFlags = [c | FlagChown c <- flags]\nlet chmodFlags = [c | FlagChmod c <- flags]\nlet linkFlags = [l | FlagLink l <- flags]\n" ▫︎ Perhaps: "Combine with src/Language/Docker/Parser/Copy.hs:61:3-45"
let chmodFlags = [c | FlagChmod c <- flags]
let linkFlags = [l | FlagLink l <- flags]
let sourceFlags = [f | FlagSource f <- flags]
Expand All @@ -33,7 +34,7 @@
(_, _, _, _ : _ : _, _) -> customError $ DuplicateFlagError "--link"
(_, _, _, _, _ : _ : _) -> customError $ DuplicateFlagError "--from"
_ -> do
let cho =

Check warning on line 37 in src/Language/Docker/Parser/Copy.hs

View workflow job for this annotation

GitHub Actions / hlint

Suggestion in parseCopy, parseAdd in module Language.Docker.Parser.Copy: Reduce duplication ▫︎ Found: "let cho\n = case chownFlags of\n [] -> NoChown\n c : _ -> c\nlet chm\n = case chmodFlags of\n [] -> NoChmod\n c : _ -> c\nlet lnk\n = case linkFlags of\n [] -> NoLink\n l : _ -> l\n" ▫︎ Perhaps: "Combine with src/Language/Docker/Parser/Copy.hs:(77,7)-(79,28)"
case chownFlags of
[] -> NoChown
c : _ -> c
Expand All @@ -56,18 +57,23 @@
parseAdd = do
reserved "ADD"
flags <- addFlag `sepEndBy` requiredWhitespace
let checksumFlags = [c | FlagChecksum c <- flags]
let chownFlags = [c | FlagChown c <- flags]
let chmodFlags = [c | FlagChmod c <- flags]
let linkFlags = [l | FlagLink l <- flags]
let invalidFlags = [i | FlagInvalid i <- flags]
notFollowedBy (string "--") <?>
"only the --chown flag, the --chmod flag or the src and dest paths"
case (invalidFlags, chownFlags, linkFlags, chmodFlags) of
((k, v) : _, _, _, _) -> unexpectedFlag k v
(_, _ : _ : _, _, _) -> customError $ DuplicateFlagError "--chown"
(_, _, _ : _ : _, _) -> customError $ DuplicateFlagError "--chmod"
(_, _, _, _ : _ : _) -> customError $ DuplicateFlagError "--link"
"only the --checksum, --chown, --chmod, --link flags or the src and dest paths"
case (invalidFlags, checksumFlags, chownFlags, linkFlags, chmodFlags) of
((k, v) : _, _, _, _, _) -> unexpectedFlag k v
(_, _ : _ : _, _, _, _) -> customError $ DuplicateFlagError "--checksum"
(_, _, _ : _ : _, _, _) -> customError $ DuplicateFlagError "--chown"
(_, _, _, _ : _ : _, _) -> customError $ DuplicateFlagError "--chmod"
(_, _, _, _, _ : _ : _) -> customError $ DuplicateFlagError "--link"
_ -> do
let chk = case checksumFlags of
[] -> NoChecksum
c : _ -> c
let cho = case chownFlags of
[] -> NoChown
c : _ -> c
Expand All @@ -78,7 +84,7 @@
case linkFlags of
[] -> NoLink
l : _ -> l
fileList "ADD" (\src dest -> Add (AddArgs src dest) (AddFlags cho chm lnk))
fileList "ADD" (\src dest -> Add (AddArgs src dest) (AddFlags chk cho chm lnk))

heredocList :: (?esc :: Char) =>
(NonEmpty SourcePath -> TargetPath -> Instruction Text) ->
Expand Down Expand Up @@ -114,11 +120,18 @@
copyFlag = (FlagSource <$> try copySource <?> "only one --from") <|> addFlag

addFlag :: (?esc :: Char) => Parser Flag
addFlag = (FlagChown <$> try chown <?> "--chown")
addFlag = (FlagChecksum <$> try checksum <?> "--checksum")
<|> (FlagChown <$> try chown <?> "--chown")
<|> (FlagChmod <$> try chmod <?> "--chmod")
<|> (FlagLink <$> try link <?> "--link")
<|> (FlagInvalid <$> try anyFlag <?> "other flag")

checksum :: (?esc :: Char) => Parser Checksum
checksum = do
void $ string "--checksum="
chk <- someUnless "the remote file checksum" (== ' ')
return $ Checksum chk

chown :: (?esc :: Char) => Parser Chown
chown = do
void $ string "--chown="
Expand Down
9 changes: 8 additions & 1 deletion src/Language/Docker/PrettyPrint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@
_ -> ""
in hsep $ [pretty s | SourcePath s <- toList sources] ++ [pretty dest <> ending]

prettyPrintChecksum :: Checksum -> Doc ann
prettyPrintChecksum checksum =
case checksum of
Checksum c -> "--checksum=" <> pretty c
NoChecksum -> mempty

prettyPrintChown :: Chown -> Doc ann
prettyPrintChown chown =
case chown of
Expand Down Expand Up @@ -284,7 +290,7 @@
CopyArgs {sourcePaths, targetPath}
CopyFlags {chmodFlag, chownFlag, linkFlag, sourceFlag} -> do
"COPY"
prettyPrintChown chownFlag

Check warning on line 293 in src/Language/Docker/PrettyPrint.hs

View workflow job for this annotation

GitHub Actions / hlint

Suggestion in prettyPrintInstruction in module Language.Docker.PrettyPrint: Reduce duplication ▫︎ Found: "prettyPrintChown chownFlag\nprettyPrintChmod chmodFlag\nprettyPrintLink linkFlag\n" ▫︎ Perhaps: "Combine with src/Language/Docker/PrettyPrint.hs:327:9-34"
prettyPrintChmod chmodFlag
prettyPrintLink linkFlag
prettyPrintCopySource sourceFlag
Expand Down Expand Up @@ -315,8 +321,9 @@
prettyPrintBaseImage b
Add
AddArgs {sourcePaths, targetPath}
AddFlags {chownFlag, chmodFlag, linkFlag} -> do
AddFlags {checksumFlag, chownFlag, chmodFlag, linkFlag} -> do
"ADD"
prettyPrintChecksum checksumFlag
prettyPrintChown chownFlag
prettyPrintChmod chmodFlag
prettyPrintLink linkFlag
Expand Down
16 changes: 14 additions & 2 deletions src/Language/Docker/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ newtype TargetPath
}
deriving (Show, Eq, Ord, IsString)

data Checksum
= Checksum !Text
| NoChecksum
deriving (Show, Eq, Ord)

instance IsString Checksum where
fromString ch =
case ch of
"" -> NoChecksum
_ -> Checksum (Text.pack ch)

data Chown
= Chown !Text
| NoChown
Expand Down Expand Up @@ -197,14 +208,15 @@ data AddArgs

data AddFlags
= AddFlags
{ chownFlag :: !Chown,
{ checksumFlag :: !Checksum,
chownFlag :: !Chown,
chmodFlag :: !Chmod,
linkFlag :: !Link
}
deriving (Show, Eq, Ord)

instance Default AddFlags where
def = AddFlags NoChown NoChmod NoLink
def = AddFlags NoChecksum NoChown NoChmod NoLink

data Check args
= Check !(CheckArgs args)
Expand Down
24 changes: 16 additions & 8 deletions test/Language/Docker/ParseAddSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,54 +41,62 @@ spec = do
)
def
]
it "with checksum flag" $
let file = Text.unlines ["ADD --checksum=sha256:24454f830cdd http://www.example.com/foo bar"]
in assertAst
file
[ Add
( AddArgs (fmap SourcePath ["http://www.example.com/foo"]) (TargetPath "bar") )
( AddFlags (Checksum "sha256:24454f830cdd") NoChown NoChmod NoLink )
]
it "with chown flag" $
let file = Text.unlines ["ADD --chown=root:root foo bar"]
in assertAst
file
[ Add
( AddArgs (fmap SourcePath ["foo"]) (TargetPath "bar") )
( AddFlags (Chown "root:root") NoChmod NoLink )
( AddFlags NoChecksum (Chown "root:root") NoChmod NoLink )
]
it "with chmod flag" $
let file = Text.unlines ["ADD --chmod=640 foo bar"]
in assertAst
file
[ Add
( AddArgs (fmap SourcePath ["foo"]) (TargetPath "bar") )
( AddFlags NoChown (Chmod "640") NoLink )
( AddFlags NoChecksum NoChown (Chmod "640") NoLink )
]
it "with link flag" $
let file = Text.unlines ["ADD --link foo bar"]
in assertAst
file
[ Add
( AddArgs (fmap SourcePath ["foo"]) (TargetPath "bar") )
( AddFlags NoChown NoChmod Link )
( AddFlags NoChecksum NoChown NoChmod Link )
]
it "with chown and chmod flag" $
let file = Text.unlines ["ADD --chown=root:root --chmod=640 foo bar"]
in assertAst
file
[ Add
( AddArgs (fmap SourcePath ["foo"]) (TargetPath "bar") )
( AddFlags (Chown "root:root") (Chmod "640") NoLink )
( AddFlags NoChecksum (Chown "root:root") (Chmod "640") NoLink )
]
it "with chown and chmod flag other order" $
let file = Text.unlines ["ADD --chmod=640 --chown=root:root foo bar"]
in assertAst
file
[ Add
( AddArgs (fmap SourcePath ["foo"]) (TargetPath "bar") )
( AddFlags (Chown "root:root") (Chmod "640") NoLink )
( AddFlags NoChecksum (Chown "root:root") (Chmod "640") NoLink )
]
it "with all flags" $
let file =
Text.unlines ["ADD --chmod=640 --chown=root:root --link foo bar"]
Text.unlines ["ADD --chmod=640 --chown=root:root --checksum=sha256:24454f830cdd --link foo bar"]
in assertAst
file
[ Add
( AddArgs (fmap SourcePath ["foo"]) (TargetPath "bar") )
( AddFlags (Chown "root:root") (Chmod "640") Link )
( AddFlags (Checksum "sha256:24454f830cdd") (Chown "root:root") (Chmod "640") Link )
]
it "list of quoted files and chown" $
let file =
Expand All @@ -101,5 +109,5 @@ spec = do
(fmap SourcePath ["foo", "bar", "baz"])
(TargetPath "/app")
)
( AddFlags (Chown "user:group") NoChmod NoLink )
( AddFlags NoChecksum (Chown "user:group") NoChmod NoLink )
]
13 changes: 9 additions & 4 deletions test/Language/Docker/PrettyPrintSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,30 @@ spec = do
( AddArgs [SourcePath "foo"] (TargetPath "bar") )
( def :: AddFlags )
in assertPretty "ADD foo bar" add
it "with just checksum" $ do
let add = Add
( AddArgs [SourcePath "http://www.example.com/foo"] (TargetPath "bar") )
( AddFlags ( Checksum "sha256:24454f830cdd" ) NoChown NoChmod NoLink )
in assertPretty "ADD --checksum=sha256:24454f830cdd http://www.example.com/foo bar" add
it "with just chown" $ do
let add = Add
( AddArgs [SourcePath "foo"] (TargetPath "bar") )
( AddFlags ( Chown "root:root" ) NoChmod NoLink )
( AddFlags NoChecksum ( Chown "root:root" ) NoChmod NoLink )
in assertPretty "ADD --chown=root:root foo bar" add
it "with just chmod" $ do
let add = Add
( AddArgs [SourcePath "foo"] (TargetPath "bar") )
( AddFlags NoChown ( Chmod "751" ) NoLink )
( AddFlags NoChecksum NoChown ( Chmod "751" ) NoLink )
in assertPretty "ADD --chmod=751 foo bar" add
it "with just link" $ do
let add = Add
( AddArgs [SourcePath "foo"] (TargetPath "bar") )
( AddFlags NoChown NoChmod Link )
( AddFlags NoChecksum NoChown NoChmod Link )
in assertPretty "ADD --link foo bar" add
it "with chown, chmod and link" $ do
let add = Add
( AddArgs [SourcePath "foo"] (TargetPath "bar") )
( AddFlags ( Chown "root:root" ) ( Chmod "751" ) Link )
( AddFlags NoChecksum ( Chown "root:root" ) ( Chmod "751" ) Link )
in assertPretty "ADD --chown=root:root --chmod=751 --link foo bar" add

describe "pretty print COPY" $ do
Expand Down
Loading