Commit 7c1bc974 authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

CLI: Replace DB obfuscation executable

parent f789b2e4
...@@ -28,8 +28,10 @@ https://stackoverflow.com/questions/876522/creating-a-copy-of-a-database-in-post ...@@ -28,8 +28,10 @@ https://stackoverflow.com/questions/876522/creating-a-copy-of-a-database-in-post
{-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE Strict #-} {-# LANGUAGE Strict #-}
module Main where module CLI.ObfuscateDB (
obfuscateDB
, obfuscateDBCmd
) where
import Data.Text qualified as T import Data.Text qualified as T
import Database.PostgreSQL.Simple qualified as PSQL import Database.PostgreSQL.Simple qualified as PSQL
...@@ -37,23 +39,16 @@ import Database.PostgreSQL.Simple.SqlQQ (sql) ...@@ -37,23 +39,16 @@ import Database.PostgreSQL.Simple.SqlQQ (sql)
import Gargantext.Core (toDBid) import Gargantext.Core (toDBid)
import Gargantext.Database.Admin.Config () import Gargantext.Database.Admin.Config ()
import Gargantext.Database.Admin.Types.Node (NodeType(..)) import Gargantext.Database.Admin.Types.Node (NodeType(..))
import Gargantext.Prelude hiding (option) import Gargantext.Prelude
import Gargantext.Prelude.Database (runPGSExecute, runPGSQuery) import Gargantext.Prelude.Database (runPGSExecute, runPGSQuery)
import Options.Applicative.Simple import CLI.Types
import Options.Applicative
data Args = Args { obfuscateDBCmd :: HasCallStack => Mod CommandFields CLI
dbHost :: Text obfuscateDBCmd = command "obfuscate-db" (info (helper <*> fmap CLISub obfuscateDB_p) (progDesc "Obfuscate a cloned Gargantext DB."))
, dbPort :: Int
, dbName :: Text
, dbUser :: Text
, dbPassword :: Text
} deriving (Show, Eq)
obfuscateDB_p :: Parser CLICmd
args :: Parser Args obfuscateDB_p = fmap CCMD_obfuscate_db $ ObfuscateDBArgs
args = Args
<$> ( strOption ( long "db-host" <$> ( strOption ( long "db-host"
<> metavar "db-host" <> metavar "db-host"
<> help "Location of the DB server" <> help "Location of the DB server"
...@@ -71,17 +66,9 @@ args = Args ...@@ -71,17 +66,9 @@ args = Args
<*> ( strOption ( long "db-password" <*> ( strOption ( long "db-password"
<> metavar "db-password" <> metavar "db-password"
<> value "" )) <> value "" ))
main :: IO ()
main = do
(opts, ()) <-
simpleOptions "0.0.1"
"gargantext DB obfuscation"
"Obfuscates a cloned Gargantext DB"
args
empty
obfuscateDB :: ObfuscateDBArgs -> IO ()
obfuscateDB opts = do
putText $ show opts putText $ show opts
let ci = PSQL.ConnectInfo { connectHost = T.unpack $ dbHost opts let ci = PSQL.ConnectInfo { connectHost = T.unpack $ dbHost opts
...@@ -101,7 +88,7 @@ main = do ...@@ -101,7 +88,7 @@ main = do
obfuscateNotes :: PSQL.Connection -> IO () obfuscateNotes :: PSQL.Connection -> IO ()
obfuscateNotes c = do obfuscateNotes c = do
let nt = toDBid Notes let nt = toDBid Notes
_ <- runPGSExecute c [sql|UPDATE nodes SET name = concat('notes-', id) WHERE typename = ?;|] (PSQL.Only nt) _ <- runPGSExecute c [sql|UPDATE nodes SET name = concat('notes-', id) WHERE typename = ?;|] (PSQL.Only nt)
nsNew <- runPGSQuery c [sql|SELECT id, name FROM nodes WHERE typename = ?|] (PSQL.Only nt) :: IO [(Int, Text)] nsNew <- runPGSQuery c [sql|SELECT id, name FROM nodes WHERE typename = ?|] (PSQL.Only nt) :: IO [(Int, Text)]
......
...@@ -3,6 +3,7 @@ module CLI.Types where ...@@ -3,6 +3,7 @@ module CLI.Types where
import Prelude import Prelude
import Data.String import Data.String
import Data.Text (Text)
newtype CorpusFile = CorpusFile { _CorpusFile :: FilePath } newtype CorpusFile = CorpusFile { _CorpusFile :: FilePath }
deriving (Show, Eq, IsString) deriving (Show, Eq, IsString)
...@@ -13,9 +14,19 @@ newtype TermListFile = TermListFile { _TermsListFile :: FilePath } ...@@ -13,9 +14,19 @@ newtype TermListFile = TermListFile { _TermsListFile :: FilePath }
newtype OutputFile = OutputFile { _OutputFile :: FilePath } newtype OutputFile = OutputFile { _OutputFile :: FilePath }
deriving (Show, Eq, IsString) deriving (Show, Eq, IsString)
data ObfuscateDBArgs = ObfuscateDBArgs {
dbHost :: !Text
, dbPort :: !Int
, dbName :: !Text
, dbUser :: !Text
, dbPassword :: !Text
} deriving (Show, Eq)
data CLICmd data CLICmd
= CCMD_clean_csv_corpus = CCMD_clean_csv_corpus
| CCMD_filter_terms_and_cooc !CorpusFile !TermListFile !OutputFile | CCMD_filter_terms_and_cooc !CorpusFile !TermListFile !OutputFile
| CCMD_obfuscate_db !ObfuscateDBArgs
deriving (Show, Eq) deriving (Show, Eq)
data CLI = data CLI =
......
...@@ -19,8 +19,9 @@ module Main where ...@@ -19,8 +19,9 @@ module Main where
import Prelude import Prelude
import CLI.Types
import CLI.FilterTermsAndCooc import CLI.FilterTermsAndCooc
import CLI.ObfuscateDB (obfuscateDB, obfuscateDBCmd)
import CLI.Types
import Options.Applicative import Options.Applicative
runCLI :: CLI -> IO () runCLI :: CLI -> IO ()
...@@ -29,6 +30,8 @@ runCLI = \case ...@@ -29,6 +30,8 @@ runCLI = \case
-> putStrLn "TODO." -> putStrLn "TODO."
CLISub (CCMD_filter_terms_and_cooc corpusFile termListFile outputFile) CLISub (CCMD_filter_terms_and_cooc corpusFile termListFile outputFile)
-> filterTermsAndCoocCLI corpusFile termListFile outputFile -> filterTermsAndCoocCLI corpusFile termListFile outputFile
CLISub (CCMD_obfuscate_db args)
-> obfuscateDB args
main :: IO () main :: IO ()
main = runCLI =<< execParser opts main = runCLI =<< execParser opts
...@@ -40,5 +43,6 @@ main = runCLI =<< execParser opts ...@@ -40,5 +43,6 @@ main = runCLI =<< execParser opts
allOptions :: Parser CLI allOptions :: Parser CLI
allOptions = subparser ( allOptions = subparser (
filterTermsAndCoocCmd filterTermsAndCoocCmd <>
obfuscateDBCmd
) )
...@@ -81,7 +81,7 @@ common optimized ...@@ -81,7 +81,7 @@ common optimized
-rtsopts -rtsopts
-with-rtsopts=-N -with-rtsopts=-N
-Wmissing-signatures -Wmissing-signatures
-- When enabled, it swaps the hashing algorithm -- When enabled, it swaps the hashing algorithm
-- with a quicker (and less secure) version, which -- with a quicker (and less secure) version, which
-- runs faster in tests. -- runs faster in tests.
...@@ -89,10 +89,6 @@ flag test-crypto ...@@ -89,10 +89,6 @@ flag test-crypto
default: False default: False
manual: True manual: True
flag disable-db-obfuscation-executable
default: False
manual: True
-- When enabled, it suppresses at compile time the -- When enabled, it suppresses at compile time the
-- debug output for the phylo code, so that it doesn't -- debug output for the phylo code, so that it doesn't
-- hinder its performance. -- hinder its performance.
...@@ -711,8 +707,9 @@ executable gargantext-cli ...@@ -711,8 +707,9 @@ executable gargantext-cli
main-is: Main.hs main-is: Main.hs
other-modules: other-modules:
CLI.CleanCsvCorpus CLI.CleanCsvCorpus
CLI.Types
CLI.FilterTermsAndCooc CLI.FilterTermsAndCooc
CLI.ObfuscateDB
CLI.Types
CLI.Utils CLI.Utils
Paths_gargantext Paths_gargantext
hs-source-dirs: hs-source-dirs:
...@@ -723,39 +720,21 @@ executable gargantext-cli ...@@ -723,39 +720,21 @@ executable gargantext-cli
, bytestring ^>= 0.10.12.0 , bytestring ^>= 0.10.12.0
, cassava ^>= 0.5.2.0 , cassava ^>= 0.5.2.0
, containers ^>= 0.6.5.1 , containers ^>= 0.6.5.1
, extra
, extra ^>= 1.7.9 , extra ^>= 1.7.9
, full-text-search ^>= 0.2.1.4 , full-text-search ^>= 0.2.1.4
, gargantext , gargantext
, gargantext-prelude , gargantext-prelude
, ini ^>= 0.4.1 , ini ^>= 0.4.1
, optparse-generic ^>= 1.4.7
, optparse-applicative , optparse-applicative
, optparse-generic ^>= 1.4.7
, postgresql-simple ^>= 0.6.4
, protolude ^>= 0.3.3 , protolude ^>= 0.3.3
, split ^>= 0.2.3.4 , split ^>= 0.2.3.4
, text ^>= 1.2.4.1 , text ^>= 1.2.4.1
, unordered-containers ^>= 0.2.16.0 , unordered-containers ^>= 0.2.16.0
, vector ^>= 0.12.3.0 , vector ^>= 0.12.3.0
executable gargantext-db-obfuscation
import:
defaults
, optimized
main-is: Main.hs
other-modules:
Paths_gargantext
hs-source-dirs:
bin/gargantext-db-obfuscation
if flag(disable-db-obfuscation-executable)
buildable: False
else
build-depends:
extra
, gargantext
, gargantext-prelude
, optparse-simple
, postgresql-simple ^>= 0.6.4
, text
executable gargantext-import executable gargantext-import
import: import:
defaults defaults
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment