This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
Directory storage engine; Switch to cryptonite base conversion functions #38
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2744f94
Directory storage engine; Switch to cryptonite base conversion functions
patrickmn 2e7a517
CHANGELOG.md: Add information about settable storage engine and the a…
patrickmn ab37823
CHANGELOG.md: Clarify that the strong read-after-create consistency r…
patrickmn 0223b88
Node.Storage...: Replace 'Key not found' with 'Payload not found'
patrickmn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
{-# LANGUAGE StrictData #-} | ||
module Constellation.Node.Storage.Directory where | ||
|
||
import ClassyPrelude hiding (delete, hash) | ||
import Crypto.Hash (Digest, SHA3_512, hash) | ||
import Data.Binary (encode, decode) | ||
import Data.ByteArray.Encoding | ||
(Base(Base32, Base64), convertToBase, convertFromBase) | ||
import System.Directory (createDirectoryIfMissing, removeFile, listDirectory) | ||
import qualified Data.ByteString.Char8 as BC | ||
import qualified Data.ByteString.Lazy as BL | ||
import qualified Data.Text.Encoding as TE | ||
|
||
import Constellation.Enclave.Payload | ||
(EncryptedPayload(EncryptedPayload, eplCt)) | ||
import Constellation.Enclave.Types (PublicKey) | ||
import Constellation.Node.Types | ||
(Storage(Storage, savePayload, loadPayload, deletePayload, | ||
traverseStorage, closeStorage)) | ||
import Constellation.Util.Exception (trys) | ||
import Constellation.Util.Logging (warnf) | ||
|
||
directoryStorage :: FilePath -> IO Storage | ||
directoryStorage dir = do | ||
createDirectoryIfMissing True (dir </> "payloads") | ||
return Storage | ||
{ savePayload = save dir | ||
, loadPayload = load dir | ||
, deletePayload = delete dir | ||
, traverseStorage = trav dir | ||
, closeStorage = return () | ||
} | ||
|
||
save :: FilePath -> (EncryptedPayload, [PublicKey]) -> IO (Either String Text) | ||
save dir x@(EncryptedPayload{..}, _) = trys $ do | ||
let fname = BC.unpack $ convertToBase Base32 dig | ||
k = TE.decodeUtf8 $ convertToBase Base64 dig | ||
-- TODO: Error out when the key already exists (collisions) | ||
BL.writeFile (dir </> "payloads" </> fname) (encode x) | ||
return k | ||
where | ||
dig = hash eplCt :: Digest SHA3_512 | ||
|
||
load :: FilePath -> Text -> IO (Either String (EncryptedPayload, [PublicKey])) | ||
load dir k = case convertFromBase Base64 $ TE.encodeUtf8 k of | ||
Left err -> return $ Left err | ||
Right dig -> load' dir (BC.unpack $ convertToBase Base32 (dig :: ByteString)) | ||
|
||
load' :: FilePath | ||
-> FilePath | ||
-> IO (Either String (EncryptedPayload, [PublicKey])) | ||
load' dir fname = do | ||
ex <- trys $ decode <$> BL.readFile (dir </> "payloads" </> fname) | ||
return $ case ex of | ||
Left err -> Left $ "Payload not found in directory " ++ dir ++ ": " ++ err | ||
Right x -> Right x | ||
|
||
delete :: FilePath -> Text -> IO () | ||
delete dir k = case convertFromBase Base64 $ TE.encodeUtf8 k of | ||
Left err -> warnf "Invalid/non-Base64 key '{}' given to delete: {}" (k, err) | ||
Right dig -> do | ||
let fname = BC.unpack (convertToBase Base32 (dig :: ByteString)) | ||
removeFile (dir </> "payloads" </> fname) | ||
|
||
trav :: FilePath -> (Text -> (EncryptedPayload, [PublicKey]) -> IO Bool) -> IO () | ||
trav dir f = listDirectory dir >>= loop | ||
where | ||
loop [] = return () | ||
loop (fname:xs) = case convertFromBase Base32 (BC.pack fname) of | ||
Left err -> do | ||
warnf "Invalid/non-Base32 file '{}': {}" (fname, err) | ||
loop xs | ||
Right dig -> do | ||
let k = TE.decodeUtf8 $ convertToBase Base64 (dig :: ByteString) | ||
load' dir fname >>= \case | ||
Left err -> do | ||
warnf "Failed to load payload {} during directory traversal: {}" (k, err) | ||
loop xs | ||
Right x -> f k x >>= \cont -> when cont $ loop xs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason not to include this as an option for users with the new command line options?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Command line options are parsed to create the cfg, so --storage "dir:..." would trigger this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I was referring to the memoryStorage option, not directoryStorage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have another PR coming which will enable that and others, just trying to decide if making leveldb a base dependency is worth it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's worth having for now, until a final decision is made wrt storage to use - that way you don't have to have the commented out tests and dependencies in the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah was leaning the same way. Will open another PR soon, including some benchmarking code as well...