Skip to content

Commit

Permalink
feat: report authorized SSH keys found on host
Browse files Browse the repository at this point in the history
  • Loading branch information
vst committed Mar 25, 2024
1 parent 76b612f commit 9f204ec
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Lhp/Remote.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ compileReport [email protected] {..} = do
_reportKernel <- _mkKernel _hostName kvs
_reportDistribution <- _mkDistribution _hostName kvs
_reportDockerContainers <- _fetchHostDockerContainers _hostName
_reportSshAuthorizedKeys <- _fetchHostSshAuthorizedKeys _hostName
pure Types.Report {..}


Expand Down Expand Up @@ -104,6 +105,19 @@ _fetchHostDockerContainers h =
Right sv -> pure sv


-- | Attempts to find and return all SSH authorized keys on the remote
-- host.
_fetchHostSshAuthorizedKeys
:: MonadIO m
=> MonadError LhpError m
=> Z.Ssh.Destination
-> m [T.Text]
_fetchHostSshAuthorizedKeys h =
filter (not . T.null . T.strip) . T.lines . Z.Text.unsafeTextFromBL <$> prog
where
prog = _toSshError h (Z.Ssh.runScript h $(embedStringFile "src/scripts/ssh-keys.sh") ["bash"])


-- | Smart constructor for remote host cloud information.
_mkCloud
:: MonadError LhpError m
Expand Down
2 changes: 2 additions & 0 deletions src/Lhp/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ data Report = Report
, _reportKernel :: !Kernel
, _reportDistribution :: !Distribution
, _reportDockerContainers :: !(Maybe [DockerContainer])
, _reportSshAuthorizedKeys :: ![T.Text]
}
deriving (Eq, Generic, Show)
deriving (Aeson.FromJSON, Aeson.ToJSON) via (ADC.Autodocodec Report)
Expand All @@ -68,6 +69,7 @@ instance ADC.HasCodec Report where
<*> ADC.requiredField "kernel" "Kernel information." ADC..= _reportKernel
<*> ADC.requiredField "distribution" "Distribution information." ADC..= _reportDistribution
<*> ADC.requiredField "dockerContainers" "List of Docker containers if the host is a Docker host." ADC..= _reportDockerContainers
<*> ADC.requiredField "sshAuthorizedKeys" "List of SSH authorized keys found on host." ADC..= _reportSshAuthorizedKeys


-- * Cloud Information
Expand Down
24 changes: 24 additions & 0 deletions src/scripts/ssh-keys.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env sh

###################
# SHELL BEHAVIOUR #
###################

# Stop on errors:
set -e

#############
# PROCEDURE #
#############

find \
/etc/ssh/authorized_keys.d/* \
$(cut -f6 -d ':' /etc/passwd | sort | uniq | xargs -I{} echo "{}/.ssh/authorized_keys") \
$(cut -f6 -d ':' /etc/passwd | sort | uniq | xargs -I{} echo "{}/.ssh/authorized_keys2") \
2>/dev/null |
sort -u |
xargs -I{} cat {} |
xargs -L1 echo |
grep -vE "^#" |
sort -u |
tr -s ' '

0 comments on commit 9f204ec

Please sign in to comment.