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 next-id command #232

Merged
merged 2 commits into from
Aug 2, 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
2 changes: 2 additions & 0 deletions EXAMPLE_ADVISORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
```toml

[advisory]
# Submit PRs with HSEC-0000-0000, or run `hsec-tools next-id` to
# print the next available ID.
id = "HSEC-0000-0000"
cwe = []

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ to remove the explanatory comments for each field.
[advisory]
# Identifier for the advisory (mandatory). Will be assigned a "HSEC-YYYY-NNNN"
# identifier e.g. HSEC-2022-0001. Please use "HSEC-0000-0000" in PRs.
# Or run `hsec-tools next-id` to print the next available ID.
id = "HSEC-0000-0000"

# Publication date of the advisory as an RFC 3339 date.
Expand Down
10 changes: 10 additions & 0 deletions code/hsec-tools/app/Command/NextID.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Command.NextID where

import Security.Advisories.Core.HsecId (printHsecId, getNextHsecId)
import Security.Advisories.Filesystem (getGreatestId)

import Util (ensureRepo)

runNextIDCommand :: Maybe FilePath -> IO ()
runNextIDCommand mPath =
ensureRepo mPath >>= getGreatestId >>= getNextHsecId >>= putStrLn . printHsecId
16 changes: 4 additions & 12 deletions code/hsec-tools/app/Command/Reserve.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

module Command.Reserve where

import Control.Monad (unless, when)
import Data.Maybe (fromMaybe)
import Control.Monad (when)
import System.Exit (die)
import System.FilePath ((</>), (<.>))

import Security.Advisories.Git
( add
, commit
, explainGitError
, getRepoRoot
)
import Security.Advisories.Core.HsecId
( placeholder
Expand All @@ -21,10 +19,11 @@ import Security.Advisories.Core.HsecId
import Security.Advisories.Filesystem
( dirNameAdvisories
, dirNameReserved
, isSecurityAdvisoriesRepo
, getGreatestId
)

import Util (ensureRepo)

-- | How to choose IDs when creating advisories or
-- reservations.
data IdMode
Expand All @@ -40,14 +39,7 @@ data CommitFlag = Commit | DoNotCommit

runReserveCommand :: Maybe FilePath -> IdMode -> CommitFlag -> IO ()
runReserveCommand mPath idMode commitFlag = do
let
path = fromMaybe "." mPath
repoPath <- getRepoRoot path >>= \case
Left _ -> die "Not a git repo"
Right a -> pure a
isRepo <- isSecurityAdvisoriesRepo repoPath
unless isRepo $
die "Not a security-advisories repo"
repoPath <- ensureRepo mPath

hsid <- case idMode of
IdModePlaceholder -> pure placeholder
Expand Down
10 changes: 9 additions & 1 deletion code/hsec-tools/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

module Main where

import qualified Command.Reserve
import Control.Monad (forM_, join, void, when)
import Control.Monad.Trans.Except (runExceptT, ExceptT (ExceptT), withExceptT, throwE)
import Control.Monad.IO.Class (liftIO)
Expand All @@ -29,6 +28,9 @@ import System.FilePath (takeBaseName)
import System.IO (hPrint, hPutStrLn, stderr)
import Validation (Validation (..))

import qualified Command.Reserve
import qualified Command.NextID

main :: IO ()
main =
join $
Expand All @@ -43,6 +45,7 @@ cliOpts = info (commandsParser <**> helper) (fullDesc <> header "Haskell Advisor
commandsParser =
hsubparser
( command "check" (info commandCheck (progDesc "Syntax check a single advisory"))
<> command "next-id" (info commandNextID (progDesc "Print the next available HSEC ID"))
<> command "reserve" (info commandReserve (progDesc "Reserve an HSEC ID"))
<> command "osv" (info commandOsv (progDesc "Convert a single advisory to OSV"))
<> command "render" (info commandRender (progDesc "Render a single advisory as HTML"))
Expand Down Expand Up @@ -76,6 +79,11 @@ commandReserve =
<> help "Commit the reservation file"
)

commandNextID :: Parser (IO ())
commandNextID =
Command.NextID.runNextIDCommand
<$> optional (argument str (metavar "REPO"))

commandCheck :: Parser (IO ())
commandCheck =
withAdvisory go
Expand Down
21 changes: 21 additions & 0 deletions code/hsec-tools/app/Util.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{-# LANGUAGE LambdaCase #-}

module Util where

import Data.Maybe (fromMaybe)
import System.Exit (die)

import Security.Advisories.Filesystem (isSecurityAdvisoriesRepo)
import Security.Advisories.Git (getRepoRoot)

-- | Ensure the given path (or current directory "." if @Nothing@)
-- is an advisory Git repo. Return the (valid) repo root, or die
-- with an error message.
--
ensureRepo :: Maybe FilePath -> IO FilePath
ensureRepo mPath =
getRepoRoot (fromMaybe "." mPath) >>= \case
Left _ -> die "Not a git repo"
Right repoPath -> isSecurityAdvisoriesRepo repoPath >>= \case
False -> die "Not a security-advisories repo"
True -> pure repoPath
2 changes: 2 additions & 0 deletions code/hsec-tools/hsec-tools.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ library
executable hsec-tools
main-is: Main.hs
other-modules: Command.Reserve
, Command.NextID
, Util

-- Modules included in this executable, other than Main.
-- other-modules:
Expand Down
Loading