Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ANE-2243): use correct Docker Hub registry API endpoint #1500

Merged
merged 6 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# FOSSA CLI Changelog

## Unreleased
- Container scanning: Fix a bug where Docker URLs were being constructed incorrectly, resulting in a 403 error

## 3.9.45
- Preflight: Fix a bug where the preflight checks fail for SBOM team analysis ([#1499](https://github.com/fossas/fossa-cli/pull/1499))

Expand Down
9 changes: 9 additions & 0 deletions src/Container/Docker/SourceParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module Container.Docker.SourceParser (
suggestDockerExport,
showReferenceWithSep,
toRepoNameWithRegistry,
toRegistryApiHost,
) where

import Control.Monad (unless, void)
Expand Down Expand Up @@ -158,6 +159,14 @@ showReferenceWithSep :: RepoReference -> Text
showReferenceWithSep (RepoReferenceTag (RepoTag tag)) = ":" <> tag
showReferenceWithSep (RepoReferenceDigest (RepoDigest digest)) = "@" <> digest

-- | Convert a registry host to its API endpoint.
-- For Docker Hub, we need to use registry-1.docker.io instead of index.docker.io
-- See: https://docs.docker.com/registry/spec/api/
toRegistryApiHost :: Text -> Text
toRegistryApiHost host
| host == defaultRegistry = "registry-1.docker.io"
| otherwise = host

-- | Parses to RegistryImageSource.
--
-- >> parse parseImageUrl "fossa/db" = https://index.docker.io/fossa/db:latest
Expand Down
5 changes: 3 additions & 2 deletions src/Control/Carrier/ContainerRegistryApi.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Container.Docker.SourceParser (
),
RepoDigest,
RepoReference (RepoReferenceDigest),
toRegistryApiHost,
)
import Control.Algebra (Has)
import Control.Carrier.AtomicCounter (runAtomicCounter)
Expand Down Expand Up @@ -147,13 +148,13 @@ reqManager = sendIO $ newManager tlsManagerSettings
-- Refer to: https://github.com/opencontainers/distribution-spec/blob/main/spec.md#pulling-manifests
manifestEndpoint :: Has (Lift IO) sig m => RegistryImageSource -> m Request
manifestEndpoint (RegistryImageSource url scheme _ repo ref _) =
sendIO $ parseRequest (toString $ (toText . show $ scheme) <> url <> "/v2/" <> repo <> "/manifests/" <> (toText . show $ ref))
sendIO $ parseRequest (toString $ (toText . show $ scheme) <> toRegistryApiHost url <> "/v2/" <> repo <> "/manifests/" <> (toText . show $ ref))

-- | Blob Endpoint.
-- Refer to: https://github.com/opencontainers/distribution-spec/blob/main/spec.md#pulling-blobs
blobEndpoint :: Has (Lift IO) sig m => RegistryImageSource -> m Request
blobEndpoint (RegistryImageSource url scheme _ repo ref _) =
sendIO $ parseRequest (toString $ (toText . show $ scheme) <> url <> "/v2/" <> repo <> "/blobs/" <> (toText . show $ ref))
sendIO $ parseRequest (toString $ (toText . show $ scheme) <> toRegistryApiHost url <> "/v2/" <> repo <> "/blobs/" <> (toText . show $ ref))

-- | Retrieve Manifest.
getImageManifest ::
Expand Down
11 changes: 10 additions & 1 deletion test/Container/Docker/SourceParserSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import Container.Docker.SourceParser (
defaultRegistry,
defaultTag,
parseImageUrl,
toRegistryApiHost,
)
import Data.Text (Text)
import Data.Void (Void)
import Test.Hspec (Expectation, Spec, describe, it)
import Test.Hspec (Expectation, Spec, describe, it, shouldBe)
import Test.Hspec.Megaparsec (shouldParse)
import Text.Megaparsec (
Parsec,
Expand Down Expand Up @@ -127,6 +128,14 @@ spec = do
fixtureArch
)

describe "toRegistryApiHost" $ do
it "should convert Docker Hub registry to API endpoint" $
toRegistryApiHost defaultRegistry `shouldBe` "registry-1.docker.io"

it "should not modify other registry hosts" $ do
toRegistryApiHost "ghcr.io" `shouldBe` "ghcr.io"
toRegistryApiHost "quay.io" `shouldBe` "quay.io"

fixtureArch :: Text
fixtureArch = "amd64"

Expand Down
Loading