Commits

Stefan Saasen  committed c788339

Include SSH based ref advertisements in addition to the HTTP based ref advertisements

  • Participants
  • Parent commits 1c1ace5

Comments (0)

Files changed (4)

File logparser/src/Stash/Log/Analyser.hs

 import Data.Function (on)
 import Text.Printf (printf)
 import Stash.Log.Parser
-import Stash.Log.Common (logDateEqHour, logDateEqMin)
+import Stash.Log.Common (logDateEqHour, logDateEqMin, isSsh, isHttp)
 
 data DateValuePair = DateValuePair {
      getLogDate     :: !LogDate
 maxConcurrent = countLinesWith (\x acc -> let conn = getConcurrentRequests $ getRequestId x
                                           in if conn >= acc then conn else acc)
 
-isSsh :: LogLine -> Bool
-isSsh logLine = getProtocol logLine == "ssh"
-
-isHttp :: LogLine -> Bool
-isHttp logLine = proto == "http" || proto == "https"
-                where proto = getProtocol logLine
-
 -- The concurrent connection data needs to be aggregated.
 -- Example
 --

File logparser/src/Stash/Log/Common.hs

+{-# LANGUAGE OverloadedStrings #-}
+
 module Stash.Log.Common
 ( logDateEqHour
 , logDateEqMin
 , sortLogFiles
+, isSsh
+, isHttp
 ) where
 
-import Stash.Log.Parser (LogDate(..))
+import Stash.Log.Parser
 import Data.List (sortBy)
 import Data.Monoid (mappend)
 import Data.String.Utils (split)
 logDateEqMin a b = logDateEqHour a b &&
                 getMinute a == getMinute b
 
+isSsh :: LogLine -> Bool
+isSsh logLine = getProtocol logLine == "ssh"
+
+isHttp :: LogLine -> Bool
+isHttp logLine = proto == "http" || proto == "https"
+                where proto = getProtocol logLine
+
+
 -- | Sort the logfiles by date and log file sequence number
 -- The logfile naming scheme is: "atlassian-stash-access-2012-11-29.0.log(.bz2)"
 sortLogFiles :: [FilePath] -> [FilePath]

File logparser/src/Stash/Log/GitOpsAnalyser.hs

 import Data.Function (on)
 import Text.Printf (printf)
 import Stash.Log.Parser
-import Stash.Log.Common (logDateEqHour)
+import Stash.Log.Common (logDateEqHour, isSsh, isHttp)
 
 data GitOperationStats = GitOperationStats {
      getOpStatDate              :: String
 -- =================================================================================
 
 isGitOperation :: LogLine -> Bool
-isGitOperation line = (or $ map (\g -> g line) ops)
+isGitOperation line = any (\g -> g line) ops
             where ops = [isClone, isFetch, isShallowClone, isPush, isRefAdvertisement]
 
 -- As of 1.1.2 of the clone cache plugin, refs are explicitly listed in the
 -- labels field, most of the data we have does _not_ have that information though
 isRefAdvertisement :: LogLine -> Bool
-isRefAdvertisement logLine = ".git/info/refs" `S.isSuffixOf` path && "GET" == method && isJust username
+isRefAdvertisement logLine = authenticatedGitOp logLine && isOutgoingLogLine logLine && refAdvertisement logLine
             where
                 action      = getAction logLine
                 path        = getPath action
                 method      = getMethod action
-                username    = getUsername logLine
+                refAdvertisement line
+                            | isSsh line        = isRefs line || not (any (inLabel line) ["clone", "fetch", "shallow clone"])
+                            | isHttp line       = ".git/info/refs" `S.isSuffixOf` path && "GET" == method
+                            | otherwise         = False
 
 isCacheHit :: LogLine -> Bool
 isCacheHit logLine = inLabel logLine "cache:hit"
 isPush :: LogLine -> Bool
 isPush logLine = inLabel logLine "push"
 
+isRefs :: LogLine -> Bool
+isRefs logLine = inLabel logLine "refs"
+
 inLabel :: LogLine -> String -> Bool
 inLabel logLine name =  let labels = getLabels logLine
                         in name `elem` labels

File logparser/tests/Properties.hs

   ( parseLogLine "" )
 
 parsedLogLine = parseLogLine inputLine
-    where inputLine = "63.246.22.196,172.16.3.45 | https | i1112x6x32 | ssaasen | 2012-08-22 18:32:08,505 | \"GET /git/ATLASSIAN/jira.git/info/refs HTTP/1.1\" | \"\" \"git/1.7.4.1\" | shallow clone | - | - | "
+    where inputLine = "63.246.22.196,172.16.3.45 | https | o1112x6x32 | ssaasen | 2012-08-22 18:32:08,505 | \"GET /git/ATLASSIAN/jira.git/info/refs HTTP/1.1\" | \"\" \"git/1.7.4.1\" | shallow clone | - | - | "
 
 parsedLogLine2 = parseLogLine inputLine
     where inputLine = "63.246.22.196,172.16.3.45 | https | i1112x6x32 | - | 2012-08-22 18:32:08,505 | \"GET /git/ATLASSIAN/jira.git/info/refs HTTP/1.1\" | \"\" \"git/1.7.4.1\" | fetch | - | - | "
 
 test_logLineParseRequestId = H.assertEqual
     "Should parse the request id"
-    'i'
+    'o'
     (getInOrOut $ getRequestId $ fromJust parsedLogLine)
 
 test_logLineParseRequestIdCounter = H.assertEqual
     [("https", 3), ("ssh", 1)]
     (protocolCount dataLogLines)
 
+test_identifyRefAdvertisement_SSH = H.assertEqual
+    "Should identify an SSH based ref advertisement correctly"
+    True
+    (isRefAdvertisement $ fromJust $ parseLogLine "172.16.1.187 | ssh | o912x7392530x2 | ssaasen | 2012-12-14 15:12:20,602 | git-upload-pack '/CONF/confluence.git' | - | - | 4284 | jfvu89 |  ")
+
+test_identifyRefAdvertisement_SSHClone = H.assertEqual
+    "Should not identify clone response as ref advertisement"
+    False
+    (isRefAdvertisement $ fromJust $ parseLogLine "172.16.1.187 | ssh | o912x7392530x2 | ssaasen | 2012-12-14 15:12:20,602 | git-upload-pack '/CONF/confluence.git' | - | clone | 4284 | jfvu89 |  ")
+
+test_identifyRefAdvertisement_SSHIncoming = H.assertEqual
+    "Should not identify incoming request as ref advertisement"
+    False
+    (isRefAdvertisement $ fromJust $ parseLogLine "172.16.1.187 | ssh | i912x7392530x2 | ssaasen | 2012-12-14 15:12:20,602 | git-upload-pack '/CONF/confluence.git' | - | - | 4284 | jfvu89 |  ")
+
+test_identifyRefAdvertisement_SSHAuthenticated = H.assertEqual
+    "Should not identify unauthenticated request as ref advertisement"
+    False
+    (isRefAdvertisement $ fromJust $ parseLogLine "172.16.1.187 | ssh | o912x7392530x2 | - | 2012-12-14 15:12:20,602 | git-upload-pack '/CONF/confluence.git' | - | - | 4284 | jfvu89 |  ")
+
+test_identifyRefAdvertisement_HttpAction = H.assertEqual
+    "Should identify an HTTP based ref advertisement correctly"
+    True
+    (isRefAdvertisement $ fromJust $ parseLogLine "63.246.22.222,172.16.3.45 | https | o2112x2x4 | ssaasen | 2012-08-23 17:44:20,123 | \"GET /git/ATLASSIAN/jira.git/info/refs HTTP/1.1\" | \"\" \"JGit/unknown\" | - | - | - | ")
+
+test_identifyRefAdvertisement_HttpLabel = H.assertEqual
+    "Should identify an HTTP based ref advertisement correctly"
+    True
+    (isRefAdvertisement $ fromJust $ parseLogLine "63.246.22.222,172.16.3.45 | https | o2112x2x4 | ssaasen | 2012-08-23 17:44:20,123 | \"GET /git/ATLASSIAN/jira.git/info/refs HTTP/1.1\" | \"\" \"JGit/unknown\" | refs | - | - | ")
+
 ------------------------------------------------------------------------
 test_sortFilesAsc = H.assertEqual
     "Should sort the given files correctly"
         ,testCase "analyser/protocolCount" test_protocolCount
         ,testCase "analyser/dataConcurrentConn logDateEqMin" test_plotDataConcurrentConn
         ,testCase "analyser/dataConcurrentConn logDateEqHour" test_plotDataConcurrentConnHour
+        ,testCase "analyser/isRefAdvertisement ssh" test_identifyRefAdvertisement_SSH
+        ,testCase "analyser/isRefAdvertisement ignore incoming ssh" test_identifyRefAdvertisement_SSHIncoming
+        ,testCase "analyser/isRefAdvertisement require authenticated ssh" test_identifyRefAdvertisement_SSHAuthenticated
+        ,testCase "analyser/isRefAdvertisement ignore clones ssh" test_identifyRefAdvertisement_SSHClone
+        ,testCase "analyser/isRefAdvertisement http action" test_identifyRefAdvertisement_HttpAction
+        ,testCase "analyser/isRefAdvertisement http label" test_identifyRefAdvertisement_HttpLabel
       ],
       testGroup "Common"
       [