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

handleFileForWritingWithOwnerPermissionImpl: Fix permisions #141

Merged
merged 3 commits into from
Jul 27, 2023
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
15 changes: 9 additions & 6 deletions cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cabal-version: 3.4

name: cardano-api
version: 8.10.0.0
version: 8.10.1.0
synopsis: The cardano api
description: The cardano api.
category: Cardano,
Expand Down Expand Up @@ -196,11 +196,11 @@ library
Cardano.Api.ChainSync.ClientPipelined
Cardano.Api.Crypto.Ed25519Bip32
Cardano.Api.Shelley
-- TODO: Eliminate Cardano.Api.Ledger when
-- cardano-api only depends on modules
-- exposed by cardano-api-ledger
Cardano.Api.Ledger
-- TODO: Eliminate Cardano.Api.Ledger when
-- cardano-api only depends on modules
-- exposed by cardano-api-ledger
Cardano.Api.Ledger


build-depends: bytestring
, cardano-api:internal
Expand Down Expand Up @@ -268,7 +268,9 @@ test-suite cardano-api-test
, cardano-ledger-api >= 1.3
, cardano-ledger-core:{cardano-ledger-core, testlib} >= 1.4
, containers
, directory
, hedgehog >= 1.1
, hedgehog-extras
, hedgehog-quickcheck
, mtl
, QuickCheck
Expand All @@ -278,6 +280,7 @@ test-suite cardano-api-test

other-modules: Test.Cardano.Api.Crypto
Test.Cardano.Api.Eras
Test.Cardano.Api.IO
Test.Cardano.Api.Json
Test.Cardano.Api.KeysByron
Test.Cardano.Api.Ledger
Expand Down
14 changes: 7 additions & 7 deletions cardano-api/internal/Cardano/Api/IO/Compat/Posix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ module Cardano.Api.IO.Compat.Posix

#ifdef UNIX

import Cardano.Api.Error (FileError (..), fileIOExceptT)
import Cardano.Api.Error (FileError (..))
import Cardano.Api.IO.Base

import Control.Exception (IOException, bracket, bracketOnError, try)
import Control.Monad (forM_, when)
import Control.Monad.Except (ExceptT, runExceptT)
import Control.Monad.IO.Class
import Control.Monad.Trans.Except.Extra (left)
import Control.Monad.Trans.Except.Extra (handleIOExceptT, left)
import qualified Data.ByteString as BS
import System.Directory ()
import System.FilePath ((</>))
import qualified System.IO as IO
import System.IO (Handle)
import System.Posix.Files (fileMode, getFileStatus, groupModes, intersectFileModes,
nullFileMode, otherModes, ownerReadMode, setFdOwnerAndGroup, setFileMode,
stdFileMode)
nullFileMode, otherModes, ownerModes, ownerReadMode, setFdOwnerAndGroup,
setFileMode, stdFileMode)
# if MIN_VERSION_unix(2,8,0)
import System.Posix.IO (OpenFileFlags (..), OpenMode (..), closeFd, defaultFileFlags,
fdToHandle, openFd)
Expand Down Expand Up @@ -67,7 +67,7 @@ handleFileForWritingWithOwnerPermissionImpl path f = do
bracket
(fdToHandle fd)
IO.hClose
(runExceptT . fileIOExceptT path . const . f)
(runExceptT . handleIOExceptT (FileIOError path) . f)

writeSecretsImpl :: FilePath -> [Char] -> [Char] -> (a -> BS.ByteString) -> [a] -> IO ()
writeSecretsImpl outDir prefix suffix secretOp xs =
Expand Down Expand Up @@ -112,7 +112,7 @@ openFileDescriptor fp openMode =
ReadWrite ->
defaultFileFlags { creat = Just stdFileMode }
WriteOnly ->
defaultFileFlags { creat = Just stdFileMode }
defaultFileFlags { creat = Just ownerModes }

# else
openFileDescriptor fp openMode =
Expand All @@ -129,7 +129,7 @@ openFileDescriptor fp openMode =
, defaultFileFlags
)
WriteOnly ->
( Just stdFileMode
( Just ownerModes
, defaultFileFlags
)
# endif
Expand Down
40 changes: 40 additions & 0 deletions cardano-api/test/cardano-api-test/Test/Cardano/Api/IO.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{-# LANGUAGE TypeApplications #-}

module Test.Cardano.Api.IO
( tests
) where

import Cardano.Api
import Cardano.Api.IO

import Control.Monad.Except (runExceptT)
import Control.Monad.IO.Class (liftIO)
import System.Directory (removeFile)

import Hedgehog
import qualified Hedgehog.Extras as H
import Hedgehog.Internal.Property
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.Hedgehog

prop_createVrfFileWithOwnerPermissions :: Property
prop_createVrfFileWithOwnerPermissions =
H.propertyOnce . H.moduleWorkspace "help" $ \ws -> do
file <- H.noteTempFile ws "file"

result <- liftIO $ writeLazyByteStringFileWithOwnerPermissions (File file) ""

case result of
Left err -> failWith Nothing $ displayError @(FileError ()) err
Right () -> return ()

fResult <- liftIO . runExceptT $ checkVrfFilePermissions (File file)

case fResult of
Left err -> failWith Nothing $ show err
Right () -> liftIO (removeFile file) >> success

tests :: TestTree
tests = testGroup "Test.Cardano.Api.IO"
[ testProperty "Create VRF File with Owner Permissions" prop_createVrfFileWithOwnerPermissions
]
2 changes: 2 additions & 0 deletions cardano-api/test/cardano-api-test/cardano-api-test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import System.IO (BufferMode (LineBuffering), hSetBuffering, hSetEncod

import qualified Test.Cardano.Api.Crypto
import qualified Test.Cardano.Api.Eras
import qualified Test.Cardano.Api.IO
import qualified Test.Cardano.Api.Json
import qualified Test.Cardano.Api.KeysByron
import qualified Test.Cardano.Api.Ledger
Expand Down Expand Up @@ -35,6 +36,7 @@ tests =
testGroup "Cardano.Api"
[ Test.Cardano.Api.Crypto.tests
, Test.Cardano.Api.Eras.tests
, Test.Cardano.Api.IO.tests
, Test.Cardano.Api.Json.tests
, Test.Cardano.Api.KeysByron.tests
, Test.Cardano.Api.Ledger.tests
Expand Down