Skip to content

Commit

Permalink
feat: add env argument for secret mounts
Browse files Browse the repository at this point in the history
  • Loading branch information
aegypius committed Oct 11, 2024
1 parent dba56a7 commit 74c2936
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
13 changes: 10 additions & 3 deletions src/Language/Docker/Parser/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ data RunFlag
deriving (Show)

data RunMountArg
= MountArgFromImage Text
= MountArgEnv Text
| MountArgFromImage Text
| MountArgId Text
| MountArgMode Text
| MountArgReadOnly Bool
Expand Down Expand Up @@ -161,13 +162,14 @@ secretMount args =
Left e -> customError e
Right as -> return $ foldr secretOpts def as
where
allowed = Set.fromList ["target", "id", "required", "source", "mode", "uid", "gid"]
allowed = Set.fromList ["target", "id", "required", "source", "mode", "uid", "gid", "env"]
required = Set.empty
secretOpts :: RunMountArg -> SecretOpts -> SecretOpts
secretOpts (MountArgTarget path) co = co {sTarget = Just path}
secretOpts (MountArgId i) co = co {sCacheId = Just i}
secretOpts (MountArgRequired r) co = co {sIsRequired = Just r}
secretOpts (MountArgSource path) co = co {sSource = Just path}
secretOpts (MountArgEnv e) co = co {sEnv = Just e}
secretOpts (MountArgMode m) co = co {sMode = Just m}
secretOpts (MountArgUid u) co = co {sUid = Just u}
secretOpts (MountArgGid g) co = co {sGid = Just g}
Expand Down Expand Up @@ -223,7 +225,8 @@ mountChoices mountType =
mountArgSource,
mountArgMode,
mountArgUid,
mountArgGid
mountArgGid,
mountArgEnv
]

stringArg :: (?esc :: Char) => Parser Text
Expand All @@ -239,6 +242,9 @@ cacheSharing :: Parser CacheSharing
cacheSharing =
choice [Private <$ string "private", Shared <$ string "shared", Locked <$ string "locked"]

mountArgEnv :: (?esc :: Char) => Parser RunMountArg
mountArgEnv = MountArgEnv <$> key "env" stringArg

mountArgFromImage :: (?esc :: Char) => Parser RunMountArg
mountArgFromImage = MountArgFromImage <$> key "from" stringArg

Expand Down Expand Up @@ -317,6 +323,7 @@ mountArgUid :: (?esc :: Char) => Parser RunMountArg
mountArgUid = MountArgUid <$> key "uid" stringArg

toArgName :: RunMountArg -> Text
toArgName (MountArgEnv _) = "env"
toArgName (MountArgFromImage _) = "from"
toArgName (MountArgGid _) = "gid"
toArgName (MountArgId _) = "id"
Expand Down
3 changes: 2 additions & 1 deletion src/Language/Docker/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,15 @@ data SecretOpts
sCacheId :: !(Maybe Text),
sIsRequired :: !(Maybe Bool),
sSource :: !(Maybe SourcePath),
sEnv :: !(Maybe Text),
sMode :: !(Maybe Text),
sUid :: !(Maybe Text),
sGid :: !(Maybe Text)
}
deriving (Eq, Show, Ord)

instance Default SecretOpts where
def = SecretOpts Nothing Nothing Nothing Nothing Nothing Nothing Nothing
def = SecretOpts Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing

data CacheSharing
= Shared
Expand Down
6 changes: 4 additions & 2 deletions test/Language/Docker/ParseRunSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,15 @@ spec = do
[ Run $ RunArgs (ArgumentsText "echo foo") flags
]
it "--mount=type=secret all modifiers" $
let file = Text.unlines ["RUN --mount=type=secret,target=/foo,id=a,required,source=/bar,mode=0700,uid=0,gid=0 echo foo"]
let file = Text.unlines ["RUN --mount=type=secret,target=/foo,env=baz,id=a,required,source=/bar,mode=0700,uid=0,gid=0 echo foo"]
flags =
def
{ mount =
Set.singleton $
SecretMount
( def
{ sTarget = Just "/foo",
sEnv = Just "baz",
sCacheId = Just "a",
sIsRequired = Just True,
sSource = Just "/bar",
Expand All @@ -208,14 +209,15 @@ spec = do
[ Run $ RunArgs (ArgumentsText "echo foo") flags
]
it "--mount=type=secret all modifiers, required explicit" $
let file = Text.unlines ["RUN --mount=type=secret,target=/foo,id=a,required=true,source=/bar,mode=0700,uid=0,gid=0 echo foo"]
let file = Text.unlines ["RUN --mount=type=secret,target=/foo,env=baz,id=a,required=true,source=/bar,mode=0700,uid=0,gid=0 echo foo"]
flags =
def
{ mount =
Set.singleton $
SecretMount
( def
{ sTarget = Just "/foo",
sEnv = Just "baz",
sCacheId = Just "a",
sIsRequired = Just True,
sSource = Just "/bar",
Expand Down

0 comments on commit 74c2936

Please sign in to comment.