module Ecluse.Config.Mirror (validateMirrorSettings) where
import Data.Map.Strict qualified as Map
import Ecluse.Config.Target (resolveStoreBackend, resolveUploadBackend, vetPublicationRepository)
import Ecluse.Config.Types
import Ecluse.Core.Ecosystem (Ecosystem)
import Ecluse.Core.Registry.Adapter (uploadEndpointFor)
import Ecluse.Core.Registry.Adapter.Capability (UploadEndpointRequirement (ReadRootUpload))
import Ecluse.Runtime.Credential.CodeArtifact.Presentation (codeArtifactUsername)
validateMirrorSettings :: AppConfig -> Either [ConfigError] ()
validateMirrorSettings :: AppConfig -> Either [ConfigError] ()
validateMirrorSettings AppConfig
app = case ((Ecosystem, MountConfig) -> [ConfigError])
-> [(Ecosystem, MountConfig)] -> [ConfigError]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Ecosystem, MountConfig) -> [ConfigError]
refusedUploadUrls [(Ecosystem, MountConfig)]
mounts of
[] -> ((Ecosystem, MountConfig) -> Either [ConfigError] ())
-> [(Ecosystem, MountConfig)] -> Either [ConfigError] ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ (Ecosystem, MountConfig) -> Either [ConfigError] ()
validate [(Ecosystem, MountConfig)]
mounts
[ConfigError]
refused -> [ConfigError] -> Either [ConfigError] ()
forall a b. a -> Either a b
Left [ConfigError]
refused
where
mounts :: [(Ecosystem, MountConfig)]
mounts = Map Ecosystem MountConfig -> [(Ecosystem, MountConfig)]
forall k a. Map k a -> [(k, a)]
Map.toList (AppConfig -> Map Ecosystem MountConfig
cfgMounts AppConfig
app)
validate :: (Ecosystem, MountConfig) -> Either [ConfigError] ()
validate (Ecosystem
eco, MountConfig
mount) = do
Maybe MirrorEndpoint
-> (MirrorEndpoint -> Either [ConfigError] ())
-> Either [ConfigError] ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ (MountConfig -> Maybe MirrorEndpoint
mntMirrorTarget MountConfig
mount) ((MirrorEndpoint -> Either [ConfigError] ())
-> Either [ConfigError] ())
-> (MirrorEndpoint -> Either [ConfigError] ())
-> Either [ConfigError] ()
forall a b. (a -> b) -> a -> b
$ \MirrorEndpoint
target -> do
case (MirrorEndpoint -> MirrorWrite
meWrite MirrorEndpoint
target, MirrorEndpoint -> Maybe RegistryUrl
meUploadUrl MirrorEndpoint
target) of
(WriteCodeArtifact{}, Just RegistryUrl
upload) -> do
original <- (ConfigError -> [ConfigError])
-> Either ConfigError StoreBackend
-> Either [ConfigError] StoreBackend
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ConfigError -> [ConfigError]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Ecosystem -> MirrorEndpoint -> Either ConfigError StoreBackend
resolveStoreBackend Ecosystem
eco MirrorEndpoint
target)
uploaded <- first pure (resolveUploadBackend eco target upload)
unless (original == uploaded) (refuse "CodeArtifact uploadUrl must address the same mirror repository")
(MirrorWrite, Maybe RegistryUrl)
_ -> () -> Either [ConfigError] ()
forall a. a -> Either [ConfigError] a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
case MirrorEndpoint -> MirrorWrite
meWrite MirrorEndpoint
target of
WriteCodeArtifact{} -> Bool -> Either [ConfigError] () -> Either [ConfigError] ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Bool -> (Text -> Bool) -> Maybe Text -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
True (Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
codeArtifactUsername) (MirrorEndpoint -> Maybe Text
meUsername MirrorEndpoint
target)) (Text -> Either [ConfigError] ()
forall {b}. Text -> Either [ConfigError] b
refuse Text
codeArtifactUsernameRequired)
MirrorWrite
_ -> () -> Either [ConfigError] ()
forall a. a -> Either [ConfigError] a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Maybe PublicationEndpoint
-> (PublicationEndpoint -> Either [ConfigError] ())
-> Either [ConfigError] ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ (MountConfig -> Maybe PublicationEndpoint
mntPublicationTarget MountConfig
mount) ((PublicationEndpoint -> Either [ConfigError] ())
-> Either [ConfigError] ())
-> (PublicationEndpoint -> Either [ConfigError] ())
-> Either [ConfigError] ()
forall a b. (a -> b) -> a -> b
$ \PublicationEndpoint
target -> do
(ConfigError -> [ConfigError])
-> Either ConfigError () -> Either [ConfigError] ()
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ConfigError -> [ConfigError]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Ecosystem -> PublicationEndpoint -> Either ConfigError ()
vetPublicationRepository Ecosystem
eco PublicationEndpoint
target)
Bool -> Either [ConfigError] () -> Either [ConfigError] ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Maybe Text -> Bool
forall a. Maybe a -> Bool
isJust (PublicationEndpoint -> Maybe Text
peUsername PublicationEndpoint
target) Bool -> Bool -> Bool
&& Maybe Secret -> Bool
forall a. Maybe a -> Bool
isNothing (PublicationEndpoint -> Maybe Secret
peToken PublicationEndpoint
target)) (Either [ConfigError] () -> Either [ConfigError] ())
-> Either [ConfigError] () -> Either [ConfigError] ()
forall a b. (a -> b) -> a -> b
$
Text -> Either [ConfigError] ()
forall {b}. Text -> Either [ConfigError] b
refuse Text
"publicationTarget.username requires a static token"
Bool -> Either [ConfigError] () -> Either [ConfigError] ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Target -> StoreTag
tgtTag (PublicationEndpoint -> Target
peTarget PublicationEndpoint
target) StoreTag -> StoreTag -> Bool
forall a. Eq a => a -> a -> Bool
== StoreTag
TagCodeArtifact Bool -> Bool -> Bool
&& Bool -> (Text -> Bool) -> Maybe Text -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
codeArtifactUsername) (PublicationEndpoint -> Maybe Text
peUsername PublicationEndpoint
target)) (Either [ConfigError] () -> Either [ConfigError] ())
-> Either [ConfigError] () -> Either [ConfigError] ()
forall a b. (a -> b) -> a -> b
$
Text -> Either [ConfigError] ()
forall {b}. Text -> Either [ConfigError] b
refuse Text
codeArtifactUsernameRequired
refuse :: Text -> Either [ConfigError] b
refuse = [ConfigError] -> Either [ConfigError] b
forall a b. a -> Either a b
Left ([ConfigError] -> Either [ConfigError] b)
-> (Text -> [ConfigError]) -> Text -> Either [ConfigError] b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConfigError -> [ConfigError]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ConfigError -> [ConfigError])
-> (Text -> ConfigError) -> Text -> [ConfigError]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ConfigError
ParseError
codeArtifactUsernameRequired :: Text
codeArtifactUsernameRequired = Text
"CodeArtifact requires the " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
codeArtifactUsername Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" credential username"
refusedUploadUrls :: (Ecosystem, MountConfig) -> [ConfigError]
refusedUploadUrls :: (Ecosystem, MountConfig) -> [ConfigError]
refusedUploadUrls (Ecosystem
eco, MountConfig
mount) =
[ Ecosystem -> Text -> ConfigError
UploadUrlRefused Ecosystem
eco (Text -> StoreTag -> Text -> Text
taggedKeyPath Text
key (Target -> StoreTag
tgtTag Target
target) Text
"uploadUrl")
| Bool
derivesUploadAddress
, (Text
key, Target
target, Just RegistryUrl
_) <- [(Text, Target, Maybe RegistryUrl)]
mirrorTarget [(Text, Target, Maybe RegistryUrl)]
-> [(Text, Target, Maybe RegistryUrl)]
-> [(Text, Target, Maybe RegistryUrl)]
forall a. Semigroup a => a -> a -> a
<> [(Text, Target, Maybe RegistryUrl)]
publicationTarget
]
where
derivesUploadAddress :: Bool
derivesUploadAddress = Ecosystem -> Maybe UploadEndpointRequirement
uploadEndpointFor Ecosystem
eco Maybe UploadEndpointRequirement
-> Maybe UploadEndpointRequirement -> Bool
forall a. Eq a => a -> a -> Bool
== UploadEndpointRequirement -> Maybe UploadEndpointRequirement
forall a. a -> Maybe a
Just UploadEndpointRequirement
ReadRootUpload
mirrorTarget :: [(Text, Target, Maybe RegistryUrl)]
mirrorTarget = [(Text
"mirrorTarget", MirrorEndpoint -> Target
meTarget MirrorEndpoint
endpoint, MirrorEndpoint -> Maybe RegistryUrl
meUploadUrl MirrorEndpoint
endpoint) | MirrorEndpoint
endpoint <- Maybe MirrorEndpoint -> [MirrorEndpoint]
forall a. Maybe a -> [a]
maybeToList (MountConfig -> Maybe MirrorEndpoint
mntMirrorTarget MountConfig
mount)]
publicationTarget :: [(Text, Target, Maybe RegistryUrl)]
publicationTarget = [(Text
"publicationTarget", PublicationEndpoint -> Target
peTarget PublicationEndpoint
endpoint, PublicationEndpoint -> Maybe RegistryUrl
peUploadUrl PublicationEndpoint
endpoint) | PublicationEndpoint
endpoint <- Maybe PublicationEndpoint -> [PublicationEndpoint]
forall a. Maybe a -> [a]
maybeToList (MountConfig -> Maybe PublicationEndpoint
mntPublicationTarget MountConfig
mount)]