2024-06-04 18:50:38 +00:00
|
|
|
{- git-annex proxy log
|
|
|
|
-
|
|
|
|
- Copyright 2024 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module Logs.Proxy (
|
|
|
|
Proxy(..),
|
|
|
|
getProxies,
|
|
|
|
recordProxies,
|
|
|
|
) where
|
|
|
|
|
|
|
|
import qualified Annex
|
|
|
|
import Annex.Common
|
|
|
|
import qualified Annex.Branch
|
2024-06-06 16:51:30 +00:00
|
|
|
import qualified Git.Remote
|
2024-06-04 18:50:38 +00:00
|
|
|
import Git.Types
|
|
|
|
import Logs
|
|
|
|
import Logs.UUIDBased
|
|
|
|
import Logs.MapLog
|
|
|
|
import Annex.UUID
|
|
|
|
|
|
|
|
import qualified Data.Set as S
|
2024-06-13 20:00:58 +00:00
|
|
|
import qualified Data.Map as M
|
2024-06-04 18:50:38 +00:00
|
|
|
import Data.ByteString.Builder
|
|
|
|
import qualified Data.Attoparsec.ByteString as A
|
|
|
|
import qualified Data.Attoparsec.ByteString.Char8 as A8
|
|
|
|
import qualified Data.ByteString.Lazy as L
|
|
|
|
|
|
|
|
data Proxy = Proxy
|
|
|
|
{ proxyRemoteUUID :: UUID
|
|
|
|
, proxyRemoteName :: RemoteName
|
|
|
|
} deriving (Show, Eq, Ord)
|
|
|
|
|
|
|
|
getProxies :: Annex (M.Map UUID (S.Set Proxy))
|
2024-06-06 16:51:30 +00:00
|
|
|
getProxies = M.map (validateProxies . value) . fromMapLog . parseProxyLog
|
2024-06-04 18:50:38 +00:00
|
|
|
<$> Annex.Branch.get proxyLog
|
|
|
|
|
|
|
|
recordProxies :: S.Set Proxy -> Annex ()
|
|
|
|
recordProxies proxies = do
|
|
|
|
-- If a private UUID has been configured as a proxy, avoid leaking
|
|
|
|
-- it into the git-annex log.
|
|
|
|
privateuuids <- annexPrivateRepos <$> Annex.getGitConfig
|
|
|
|
let proxies' = S.filter
|
|
|
|
(\p -> S.notMember (proxyRemoteUUID p) privateuuids) proxies
|
|
|
|
|
|
|
|
c <- currentVectorClock
|
|
|
|
u <- getUUID
|
|
|
|
Annex.Branch.change (Annex.Branch.RegardingUUID [u]) proxyLog $
|
|
|
|
(buildLogNew buildProxyList)
|
|
|
|
. changeLog c u proxies'
|
|
|
|
. parseProxyLog
|
|
|
|
|
|
|
|
buildProxyList :: S.Set Proxy -> Builder
|
2024-06-12 17:06:14 +00:00
|
|
|
buildProxyList = assemble . map fmt . S.toList
|
2024-06-04 18:50:38 +00:00
|
|
|
where
|
|
|
|
fmt p = buildUUID (proxyRemoteUUID p)
|
|
|
|
<> colon
|
|
|
|
<> byteString (encodeBS (proxyRemoteName p))
|
|
|
|
colon = charUtf8 ':'
|
2024-06-12 17:06:14 +00:00
|
|
|
|
|
|
|
assemble [] = mempty
|
|
|
|
assemble (x:[]) = x
|
|
|
|
assemble (x:y:l) = x <> " " <> assemble (y:l)
|
2024-06-04 18:50:38 +00:00
|
|
|
|
|
|
|
parseProxyLog :: L.ByteString -> Log (S.Set Proxy)
|
|
|
|
parseProxyLog = parseLogNew parseProxyList
|
|
|
|
|
|
|
|
parseProxyList :: A.Parser (S.Set Proxy)
|
|
|
|
parseProxyList = S.fromList <$> many parseword
|
|
|
|
where
|
|
|
|
parseword = parseproxy
|
|
|
|
<* ((const () <$> A8.char ' ') <|> A.endOfInput)
|
|
|
|
parseproxy = Proxy
|
|
|
|
<$> (toUUID <$> A8.takeWhile1 (/= colon))
|
|
|
|
<* (const () <$> A8.char colon)
|
|
|
|
<*> (decodeBS <$> A8.takeWhile1 (/= ' '))
|
|
|
|
colon = ':'
|
2024-06-06 16:51:30 +00:00
|
|
|
|
|
|
|
-- Filter out any proxies that have a name that is not allowed as a git
|
|
|
|
-- remote name. This avoids any security problems with eg escape
|
|
|
|
-- characters in names, and ensures the name can be used anywhere a usual
|
|
|
|
-- git remote name can be used without causing issues.
|
|
|
|
validateProxies :: S.Set Proxy -> S.Set Proxy
|
|
|
|
validateProxies = S.filter $ \p ->
|
|
|
|
Git.Remote.makeLegalName (proxyRemoteName p) == proxyRemoteName p
|