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

[ANE-2237] make the preflight check filename unique #1498

Merged
merged 3 commits into from
Jan 16, 2025
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
5 changes: 4 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# FOSSA CLI Changelog

## 3.9.44
- Preflight: Fix a bug where the preflight check could fail if you ran fossa multiple times simultaneously ([#1498](https://github.com/fossas/fossa-cli/pull/1498))

## 3.9.43
- Discovery: Fix a bug where directories in paths.exclude may still be accessed during discovery which causes an error when users don't have permission to read those directories.
- Discovery: Fix a bug where directories in paths.exclude may still be accessed during discovery which causes an error when users don't have permission to read those directories ([#1493](https://github.com/fossas/fossa-cli/pull/1493))

## 3.9.42
- Licensing: Adds support for the Text-Tabs+Wrap License
Expand Down
20 changes: 14 additions & 6 deletions src/App/Fossa/PreflightChecks.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}

module App.Fossa.PreflightChecks (
PreflightCommandChecks (..),
Expand All @@ -21,6 +20,8 @@ import Control.Monad (void, when)
import Data.Error (createErrataWithHeaderOnly)
import Data.Text
import Data.Text.IO qualified as TIO
import Data.UUID qualified as UUID (toString)
import Data.UUID.V4 qualified as UUID (nextRandom)
import Diag.Diagnostic (ToDiagnostic (..))
import Effect.Logger (renderIt)
import Errata (Errata (..))
Expand All @@ -30,7 +31,7 @@ import Path (
Path,
Rel,
fromAbsFile,
mkRelFile,
parseRelFile,
(</>),
)
import Path.IO (getTempDir, removeFile)
Expand Down Expand Up @@ -62,8 +63,10 @@ preflightChecks ::
preflightChecks cmd = context "preflight-checks" $ do
-- Check for writing to temp dir
tmpDir <- sendIO getTempDir
fatalOnIOException "Failed to write to temp directory" . sendIO $ TIO.writeFile (fromAbsFile $ tmpDir </> preflightCheckFileName) "Writing to temp dir"
sendIO $ removeFile (tmpDir </> preflightCheckFileName)
fileName <- preflightCheckFileName
let file = tmpDir </> fileName
fatalOnIOException "Failed to write to temp directory" . sendIO $ TIO.writeFile (fromAbsFile file) "Writing to temp dir"
sendIO $ removeFile file

-- Check for valid API Key and if user can connect to fossa app
org <- errHelp InvalidApiKeyErr $ errDoc apiKeyUrl getOrganization
Expand Down Expand Up @@ -127,8 +130,13 @@ fullAccessTokenCheck TokenTypeResponse{..} = case tokenType of
$ fatal TokenTypeErr
_ -> pure ()

preflightCheckFileName :: Path Rel File
preflightCheckFileName = $(mkRelFile "preflight-check.txt")
-- | Generate a unique filename for a preflight check file.
--
-- The filename needs to be unique so that we don't clobber the file when multiple instances of fossa run on the same machine.
preflightCheckFileName :: Has (Lift IO) sig m => m (Path Rel File)
preflightCheckFileName = do
suffix <- sendIO $ UUID.toString <$> UUID.nextRandom
sendIO $ parseRelFile $ "preflight-check-" <> suffix <> ".txt"

data InvalidApiKeyErr = InvalidApiKeyErr
instance ToDiagnostic InvalidApiKeyErr where
Expand Down
Loading