push.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. Copyright 2023 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package ocipush
  14. import (
  15. "context"
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "net/http"
  20. "path/filepath"
  21. "slices"
  22. "time"
  23. pusherrors "github.com/containerd/containerd/v2/core/remotes/errors"
  24. "github.com/distribution/reference"
  25. "github.com/docker/buildx/util/imagetools"
  26. "github.com/docker/compose/v2/pkg/api"
  27. "github.com/opencontainers/go-digest"
  28. "github.com/opencontainers/image-spec/specs-go"
  29. v1 "github.com/opencontainers/image-spec/specs-go/v1"
  30. )
  31. const (
  32. // ComposeProjectArtifactType is the OCI 1.1-compliant artifact type value
  33. // for the generated image manifest.
  34. ComposeProjectArtifactType = "application/vnd.docker.compose.project"
  35. // ComposeYAMLMediaType is the media type for each layer (Compose file)
  36. // in the image manifest.
  37. ComposeYAMLMediaType = "application/vnd.docker.compose.file+yaml"
  38. // ComposeEmptyConfigMediaType is a media type used for the config descriptor
  39. // when doing OCI 1.0-style pushes.
  40. //
  41. // The content is always `{}`, the same as a normal empty descriptor, but
  42. // the specific media type allows clients to fall back to the config media
  43. // type to recognize the manifest as a Compose project since the artifact
  44. // type field is not available in OCI 1.0.
  45. //
  46. // This is based on guidance from the OCI 1.1 spec:
  47. // > Implementers note: artifacts have historically been created without
  48. // > an artifactType field, and tooling to work with artifacts should
  49. // > fallback to the config.mediaType value.
  50. ComposeEmptyConfigMediaType = "application/vnd.docker.compose.config.empty.v1+json"
  51. // ComposeEnvFileMediaType is the media type for each Env File layer in the image manifest.
  52. ComposeEnvFileMediaType = "application/vnd.docker.compose.envfile"
  53. )
  54. // clientAuthStatusCodes are client (4xx) errors that are authentication
  55. // related.
  56. var clientAuthStatusCodes = []int{
  57. http.StatusUnauthorized,
  58. http.StatusForbidden,
  59. http.StatusProxyAuthRequired,
  60. }
  61. type Pushable struct {
  62. Descriptor v1.Descriptor
  63. Data []byte
  64. }
  65. func DescriptorForComposeFile(path string, content []byte) v1.Descriptor {
  66. return v1.Descriptor{
  67. MediaType: ComposeYAMLMediaType,
  68. Digest: digest.FromString(string(content)),
  69. Size: int64(len(content)),
  70. Annotations: map[string]string{
  71. "com.docker.compose.version": api.ComposeVersion,
  72. "com.docker.compose.file": filepath.Base(path),
  73. },
  74. }
  75. }
  76. func DescriptorForEnvFile(path string, content []byte) v1.Descriptor {
  77. return v1.Descriptor{
  78. MediaType: ComposeEnvFileMediaType,
  79. Digest: digest.FromString(string(content)),
  80. Size: int64(len(content)),
  81. Annotations: map[string]string{
  82. "com.docker.compose.version": api.ComposeVersion,
  83. "com.docker.compose.envfile": filepath.Base(path),
  84. },
  85. }
  86. }
  87. func PushManifest(
  88. ctx context.Context,
  89. resolver *imagetools.Resolver,
  90. named reference.Named,
  91. layers []Pushable,
  92. ociVersion api.OCIVersion,
  93. ) error {
  94. // Check if we need an extra empty layer for the manifest config
  95. if ociVersion == api.OCIVersion1_1 || ociVersion == "" {
  96. if err := resolver.Push(ctx, named, v1.DescriptorEmptyJSON, v1.DescriptorEmptyJSON.Data); err != nil {
  97. return err
  98. }
  99. }
  100. // prepare to push the manifest by pushing the layers
  101. layerDescriptors := make([]v1.Descriptor, len(layers))
  102. for i := range layers {
  103. layerDescriptors[i] = layers[i].Descriptor
  104. if err := resolver.Push(ctx, named, layers[i].Descriptor, layers[i].Data); err != nil {
  105. return err
  106. }
  107. }
  108. if ociVersion != "" {
  109. // if a version was explicitly specified, use it
  110. return createAndPushManifest(ctx, resolver, named, layerDescriptors, ociVersion)
  111. }
  112. // try to push in the OCI 1.1 format but fallback to OCI 1.0 on 4xx errors
  113. // (other than auth) since it's most likely the result of the registry not
  114. // having support
  115. err := createAndPushManifest(ctx, resolver, named, layerDescriptors, api.OCIVersion1_1)
  116. var pushErr pusherrors.ErrUnexpectedStatus
  117. if errors.As(err, &pushErr) && isNonAuthClientError(pushErr.StatusCode) {
  118. // TODO(milas): show a warning here (won't work with logrus)
  119. return createAndPushManifest(ctx, resolver, named, layerDescriptors, api.OCIVersion1_0)
  120. }
  121. return err
  122. }
  123. func createAndPushManifest(
  124. ctx context.Context,
  125. resolver *imagetools.Resolver,
  126. named reference.Named,
  127. layers []v1.Descriptor,
  128. ociVersion api.OCIVersion,
  129. ) error {
  130. toPush, err := generateManifest(layers, ociVersion)
  131. if err != nil {
  132. return err
  133. }
  134. for _, p := range toPush {
  135. err = resolver.Push(ctx, named, p.Descriptor, p.Data)
  136. if err != nil {
  137. return err
  138. }
  139. }
  140. return nil
  141. }
  142. func isNonAuthClientError(statusCode int) bool {
  143. if statusCode < 400 || statusCode >= 500 {
  144. // not a client error
  145. return false
  146. }
  147. return !slices.Contains(clientAuthStatusCodes, statusCode)
  148. }
  149. func generateManifest(layers []v1.Descriptor, ociCompat api.OCIVersion) ([]Pushable, error) {
  150. var toPush []Pushable
  151. var config v1.Descriptor
  152. var artifactType string
  153. switch ociCompat {
  154. case api.OCIVersion1_0:
  155. // "Content other than OCI container images MAY be packaged using the image manifest.
  156. // When this is done, the config.mediaType value MUST be set to a value specific to
  157. // the artifact type or the empty value."
  158. // Source: https://github.com/opencontainers/image-spec/blob/main/manifest.md#guidelines-for-artifact-usage
  159. //
  160. // The `ComposeEmptyConfigMediaType` is used specifically for this purpose:
  161. // there is no config, and an empty descriptor is used for OCI 1.1 in
  162. // conjunction with the `ArtifactType`, but for OCI 1.0 compatibility,
  163. // tooling falls back to the config media type, so this is used to
  164. // indicate that it's not a container image but custom content.
  165. configData := []byte("{}")
  166. config = v1.Descriptor{
  167. MediaType: ComposeEmptyConfigMediaType,
  168. Digest: digest.FromBytes(configData),
  169. Size: int64(len(configData)),
  170. }
  171. // N.B. OCI 1.0 does NOT support specifying the artifact type, so it's
  172. // left as an empty string to omit it from the marshaled JSON
  173. artifactType = ""
  174. toPush = append(toPush, Pushable{Descriptor: config, Data: configData})
  175. case api.OCIVersion1_1:
  176. config = v1.DescriptorEmptyJSON
  177. artifactType = ComposeProjectArtifactType
  178. // N.B. the descriptor has the data embedded in it
  179. toPush = append(toPush, Pushable{Descriptor: config, Data: make([]byte, len(config.Data))})
  180. default:
  181. return nil, fmt.Errorf("unsupported OCI version: %s", ociCompat)
  182. }
  183. manifest, err := json.Marshal(v1.Manifest{
  184. Versioned: specs.Versioned{SchemaVersion: 2},
  185. MediaType: v1.MediaTypeImageManifest,
  186. ArtifactType: artifactType,
  187. Config: config,
  188. Layers: layers,
  189. Annotations: map[string]string{
  190. "org.opencontainers.image.created": time.Now().Format(time.RFC3339),
  191. },
  192. })
  193. if err != nil {
  194. return nil, err
  195. }
  196. manifestDescriptor := v1.Descriptor{
  197. MediaType: v1.MediaTypeImageManifest,
  198. Digest: digest.FromString(string(manifest)),
  199. Size: int64(len(manifest)),
  200. Annotations: map[string]string{
  201. "com.docker.compose.version": api.ComposeVersion,
  202. },
  203. ArtifactType: artifactType,
  204. }
  205. toPush = append(toPush, Pushable{Descriptor: manifestDescriptor, Data: manifest})
  206. return toPush, nil
  207. }