distribution_routes.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package distribution
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "net/http"
  6. "strings"
  7. "github.com/docker/distribution/manifest/manifestlist"
  8. "github.com/docker/distribution/manifest/schema1"
  9. "github.com/docker/distribution/manifest/schema2"
  10. "github.com/docker/distribution/reference"
  11. "github.com/docker/docker/api/server/httputils"
  12. "github.com/docker/docker/api/types"
  13. registrytypes "github.com/docker/docker/api/types/registry"
  14. "github.com/opencontainers/image-spec/specs-go/v1"
  15. "github.com/pkg/errors"
  16. "golang.org/x/net/context"
  17. )
  18. func (s *distributionRouter) getDistributionInfo(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  19. if err := httputils.ParseForm(r); err != nil {
  20. return err
  21. }
  22. w.Header().Set("Content-Type", "application/json")
  23. var (
  24. config = &types.AuthConfig{}
  25. authEncoded = r.Header.Get("X-Registry-Auth")
  26. distributionInspect registrytypes.DistributionInspect
  27. )
  28. if authEncoded != "" {
  29. authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
  30. if err := json.NewDecoder(authJSON).Decode(&config); err != nil {
  31. // for a search it is not an error if no auth was given
  32. // to increase compatibility with the existing api it is defaulting to be empty
  33. config = &types.AuthConfig{}
  34. }
  35. }
  36. image := vars["name"]
  37. ref, err := reference.ParseAnyReference(image)
  38. if err != nil {
  39. return err
  40. }
  41. namedRef, ok := ref.(reference.Named)
  42. if !ok {
  43. if _, ok := ref.(reference.Digested); ok {
  44. // full image ID
  45. return errors.Errorf("no manifest found for full image ID")
  46. }
  47. return errors.Errorf("unknown image reference format: %s", image)
  48. }
  49. distrepo, _, err := s.backend.GetRepository(ctx, namedRef, config)
  50. if err != nil {
  51. return err
  52. }
  53. blobsrvc := distrepo.Blobs(ctx)
  54. if canonicalRef, ok := namedRef.(reference.Canonical); !ok {
  55. namedRef = reference.TagNameOnly(namedRef)
  56. taggedRef, ok := namedRef.(reference.NamedTagged)
  57. if !ok {
  58. return errors.Errorf("image reference not tagged: %s", image)
  59. }
  60. descriptor, err := distrepo.Tags(ctx).Get(ctx, taggedRef.Tag())
  61. if err != nil {
  62. return err
  63. }
  64. distributionInspect.Descriptor = v1.Descriptor{
  65. MediaType: descriptor.MediaType,
  66. Digest: descriptor.Digest,
  67. Size: descriptor.Size,
  68. }
  69. } else {
  70. // TODO(nishanttotla): Once manifests can be looked up as a blob, the
  71. // descriptor should be set using blobsrvc.Stat(ctx, canonicalRef.Digest())
  72. // instead of having to manually fill in the fields
  73. distributionInspect.Descriptor.Digest = canonicalRef.Digest()
  74. }
  75. // we have a digest, so we can retrieve the manifest
  76. mnfstsrvc, err := distrepo.Manifests(ctx)
  77. if err != nil {
  78. return err
  79. }
  80. mnfst, err := mnfstsrvc.Get(ctx, distributionInspect.Descriptor.Digest)
  81. if err != nil {
  82. return err
  83. }
  84. mediaType, payload, err := mnfst.Payload()
  85. if err != nil {
  86. return err
  87. }
  88. // update MediaType because registry might return something incorrect
  89. distributionInspect.Descriptor.MediaType = mediaType
  90. if distributionInspect.Descriptor.Size == 0 {
  91. distributionInspect.Descriptor.Size = int64(len(payload))
  92. }
  93. // retrieve platform information depending on the type of manifest
  94. switch mnfstObj := mnfst.(type) {
  95. case *manifestlist.DeserializedManifestList:
  96. for _, m := range mnfstObj.Manifests {
  97. distributionInspect.Platforms = append(distributionInspect.Platforms, v1.Platform{
  98. Architecture: m.Platform.Architecture,
  99. OS: m.Platform.OS,
  100. OSVersion: m.Platform.OSVersion,
  101. OSFeatures: m.Platform.OSFeatures,
  102. Variant: m.Platform.Variant,
  103. })
  104. }
  105. case *schema2.DeserializedManifest:
  106. configJSON, err := blobsrvc.Get(ctx, mnfstObj.Config.Digest)
  107. var platform v1.Platform
  108. if err == nil {
  109. err := json.Unmarshal(configJSON, &platform)
  110. if err == nil && (platform.OS != "" || platform.Architecture != "") {
  111. distributionInspect.Platforms = append(distributionInspect.Platforms, platform)
  112. }
  113. }
  114. case *schema1.SignedManifest:
  115. platform := v1.Platform{
  116. Architecture: mnfstObj.Architecture,
  117. OS: "linux",
  118. }
  119. distributionInspect.Platforms = append(distributionInspect.Platforms, platform)
  120. }
  121. return httputils.WriteJSON(w, http.StatusOK, distributionInspect)
  122. }