openssh-github-keys 0.1.0.0 → 0.1.1.0
raw patch · 2 files changed
+40/−95 lines, 2 filesdep +keyword-argsdep +parsecdep ~basenew-uploader
Dependencies added: keyword-args, parsec
Dependency ranges changed: base
Files
- openssh-github-keys.cabal +17/−7
- src/Main.hs +23/−88
openssh-github-keys.cabal view
@@ -1,13 +1,18 @@ name: openssh-github-keys-version: 0.1.0.0-synopsis: Fetch OpenSSH keys from a Github team+version: 0.1.1.0+synopsis: Fetch OpenSSH keys from a GitHub team description: . This package fetches the OpenSSH public keys for all users from a- Github team. It is intended to be executed from the AuthorizedKeys+ GitHub team. It is intended to be executed from the AuthorizedKeys command in the sshd_config file, which then allows users to log in using keys that they have in their Github accounts. .++ Please see the+ <https://github.com/stackbuilders/openssh-github-keys GitHub page>+ for detailed usage information.+ . homepage: https://github.com/stackbuilders/openssh-github-keys license: MIT license-file: LICENSE@@ -26,7 +31,7 @@ main-is: Main.hs -- other-modules: -- other-extensions:- build-depends: base >=4.5 && <4.8+ build-depends: base >=4.5 && <4.9 , octohat >=0.1.2 , dotenv , text@@ -34,6 +39,8 @@ , directory , filepath , unix+ , keyword-args+ , parsec hs-source-dirs: src ghc-options: -Wall@@ -43,7 +50,7 @@ library exposed-modules: System.OpensshGithubKeys - build-depends: base >=4.5 && <4.8+ build-depends: base >=4.5 && <4.9 , octohat >=0.1.2 , text @@ -56,11 +63,14 @@ type: exitcode-stdio-1.0 hs-source-dirs: spec, src main-is: Spec.hs- build-depends: base >=4.5 && <4.8+ build-depends: base >=4.5 && <4.9 , octohat >=0.1.2 , text- , hspec+ , keyword-args+ , optparse-applicative+ , parsec+ , keyword-args default-language: Haskell2010 ghc-options: -Wall
src/Main.hs view
@@ -2,84 +2,34 @@ module Main where -import Options.Applicative--import qualified Data.Text as T--import Network.Octohat (keysOfTeamInOrganization)-import Network.Octohat.Types (runGitHub, OrganizationName(..),- TeamName(..))--import System.OpensshGithubKeys (formatKey)--import System.Timeout (timeout)--import qualified Configuration.Dotenv as Dotenv--data Options = Options- { localUser :: String- , organization :: String- , team :: String- , users :: [String]- , dotfile :: Maybe FilePath-- } deriving (Show)-+import qualified Configuration.Dotenv as Dotenv+import Control.Monad (when)+import qualified Data.Text as T+import Network.Octohat (keysOfTeamInOrganization)+import Network.Octohat.Types (OrganizationName (..),+ TeamName (..), runGitHub)+import Options.Applicative+import System.OpensshGithubKeys (formatKey)+import System.OpensshGithubKeys.Config (options, readFileOptions)+import System.OpensshGithubKeys.Types (Options (..))+import System.Timeout (timeout) fetchKeys :: Options -> IO ()-fetchKeys opts = do- if any (\u -> localUser opts == u) (users opts)- then do- res <- runGitHub $ keysOfTeamInOrganization- (OrganizationName $ T.pack $ organization opts)- (TeamName $ T.pack $ team opts)-- case res of- Right membersWithKeys ->- putStrLn $ unlines $ concatMap formatKey membersWithKeys-- Left e -> error $ "Error retrieving keys for organization '" ++ show e-- else return ()--config :: Parser Options-config = Options- <$> argument str (metavar "AUTHENTICATE"- <> help "Local user trying to authenticate currently")-- <*> strOption (- long "organization"- <> short 'o'- <> metavar "ORGANIZATION"- <> help "GitHub organization from which to select teams")-- <*> strOption (- long "team"- <> short 't'- <> metavar "TEAM"- <> help "GitHub team from which to select members' keys" )-- <*> some (strOption (- long "user"- <> short 'u'- <> metavar "USER"- <> help "A local user that we should try to authenticate using Github" ))-- <*> strOptional (- long "dotfile"- <> short 'f'- <> metavar "DOTFILE"- <> help "File in 'dotfile' format specifying GITHUB_TOKEN" )+fetchKeys os = when (any (\u -> localUser os == u) (users os)) $ do+ res <- runGitHub $ keysOfTeamInOrganization+ (OrganizationName $ T.pack $ organization os)+ (TeamName $ T.pack $ team os) -strOptional :: Mod OptionFields String -> Parser (Maybe String)-strOptional flags = Just <$> strOption flags <|> pure Nothing+ case res of+ Right membersWithKeys -> putStrLn $ unlines $ concatMap formatKey membersWithKeys+ Left e -> error $ "Error retrieving keys for organization '" ++ show e main :: IO () main = do- options <- execParser opts- readDotenvFile options-- res <- timeout allowedTime (fetchKeys options)+ d <- readFileOptions "/etc/openssh-github-keys/login.conf"+ opts <- execParser $ options d+ readDotenvFile opts+ res <- timeout allowedTime (fetchKeys opts) case res of Nothing -> error $ "Allowed time of " ++ show allowedTime ++ " microseconds exceeded while fetching keys from GitHub."@@ -87,22 +37,7 @@ where allowedTime = 5 * 1000000 -- Allowed time in microseconds for fetching keys- opts = info (helper <*> config)- ( fullDesc - <> progDesc (unlines- [ "Fetches ssh public keys from TEAMS under ORGANIZATION on GitHub."- , "The output format is suitable for AuthorizedKeysCommand in"- , "sshd_config. Requires a github token, which can be specified"- , "in an environment variable GITHUB_TOKEN or in a dotenv file which"- , "can be specified with the -f option."])-- <> header "openssh-github-keys - fetches team member keys from GitHub"- )- -- | Conditionally reads the ~/.github_token file if it exists. readDotenvFile :: Options -> IO ()-readDotenvFile opts =- case dotfile opts of- Just f -> Dotenv.loadFile False f- Nothing -> return ()+readDotenvFile = Dotenv.loadFile False . dotfile