Skip to content

Commit

Permalink
base64-encode the schedule hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Sydney Kerckhove committed Dec 18, 2023
1 parent 1ae9806 commit 677b849
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
10 changes: 5 additions & 5 deletions smos-scheduler/default.nix
@@ -1,6 +1,6 @@
{ mkDerivation, aeson, autodocodec, autodocodec-yaml, autoexporter
, base, bytestring, conduit, containers, cron, cryptohash-sha256
, dirforest, envparse, fuzzy-time, genvalidity
, base, base64-bytestring, bytestring, conduit, containers, cron
, cryptohash-sha256, dirforest, envparse, fuzzy-time, genvalidity
, genvalidity-bytestring, genvalidity-containers, genvalidity-path
, genvalidity-sydtest, genvalidity-sydtest-aeson, genvalidity-text
, genvalidity-time, lib, megaparsec, mtl, optparse-applicative
Expand All @@ -17,9 +17,9 @@ mkDerivation {
isLibrary = true;
isExecutable = true;
libraryHaskellDepends = [
aeson autodocodec base bytestring conduit containers cron
cryptohash-sha256 envparse fuzzy-time megaparsec mtl
optparse-applicative path path-io pretty-relative-time safe
aeson autodocodec base base64-bytestring bytestring conduit
containers cron cryptohash-sha256 envparse fuzzy-time megaparsec
mtl optparse-applicative path path-io pretty-relative-time safe
safe-coloured-text safe-coloured-text-terminfo smos-cli smos-data
smos-directory smos-report text time tz unliftio validity
validity-path validity-text yaml
Expand Down
1 change: 1 addition & 0 deletions smos-scheduler/package.yaml
Expand Up @@ -18,6 +18,7 @@ library:
dependencies:
- aeson
- autodocodec
- base64-bytestring
- bytestring
- conduit
- containers
Expand Down
1 change: 1 addition & 0 deletions smos-scheduler/smos-scheduler.cabal
Expand Up @@ -38,6 +38,7 @@ library
aeson
, autodocodec
, base
, base64-bytestring
, bytestring
, conduit
, containers
Expand Down
8 changes: 5 additions & 3 deletions smos-scheduler/src/Smos/Scheduler/OptParse/Types.hs
Expand Up @@ -16,6 +16,7 @@ import Crypto.Hash.SHA256 as SHA256
import Data.Aeson (FromJSON, ToJSON)
import Data.ByteString (ByteString)
import qualified Data.ByteString as SB
import qualified Data.ByteString.Base64 as Base64
import qualified Data.ByteString.Lazy as LB
import Data.Map (Map)
import qualified Data.Map as M
Expand All @@ -39,7 +40,6 @@ import Smos.Data
import Smos.Directory.OptParse.Types
import Smos.Report.Time
import System.Cron (CronSchedule, parseCronSchedule, serializeCronSchedule)
import Text.Read
import UnliftIO.IO.File

data Arguments = Arguments Command (FlagsWithConfigFile Flags)
Expand Down Expand Up @@ -181,10 +181,12 @@ hashScheduleItem :: ScheduleItem -> ScheduleItemHash
hashScheduleItem = ScheduleItemHash . SHA256.hashlazy . serialiseScheduleItemConsistently

renderScheduleItemHash :: ScheduleItemHash -> Text
renderScheduleItemHash = T.pack . show . unScheduleItemHash
renderScheduleItemHash = TE.decodeUtf8 . Base64.encode . unScheduleItemHash

parseScheduleItemHash :: Text -> Maybe ScheduleItemHash
parseScheduleItemHash = fmap ScheduleItemHash . readMaybe . T.unpack
parseScheduleItemHash t = case Base64.decode (TE.encodeUtf8 t) of
Left _ -> Nothing
Right sb -> pure $ ScheduleItemHash sb

writeScheduleTemplate :: Path Abs File -> ScheduleTemplate -> IO ()
writeScheduleTemplate p a = do
Expand Down
24 changes: 20 additions & 4 deletions smos-scheduler/test/Smos/Scheduler/OptParseSpec.hs
@@ -1,10 +1,11 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TypeApplications #-}

module Smos.Scheduler.OptParseSpec
( spec,
)
where
module Smos.Scheduler.OptParseSpec (spec) where

import Path
import Smos.Report.Time
import Smos.Scheduler.OptParse.Types
import Smos.Scheduler.Render.Gen ()
import Test.Syd
Expand All @@ -21,3 +22,18 @@ spec = do
jsonSpec @EntryTemplate
genValidSpec @ScheduleTemplate
jsonSpec @ScheduleTemplate
genValidSpec @ScheduleItem
describe "hashScheduleItem" $ do
it "produces valid hashes" $
producesValid hashScheduleItem
it "produces the exact same hash, consistently" $
renderScheduleItemHash
( hashScheduleItem
ScheduleItem
{ scheduleItemDescription = Just "Foobar",
scheduleItemTemplate = "template.smos.template",
scheduleItemDestination = DestinationPathTemplate [relfile|result.smos.template|],
scheduleItemRecurrence = HaircutRecurrence $ Months 1
}
)
`shouldBe` "sARhcXIVaaVp94P3nKt4HkR8nkM6HgxrwpY5kb3Lvf4="

0 comments on commit 677b849

Please sign in to comment.