blobs.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package distribution
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "time"
  8. "github.com/docker/distribution/context"
  9. "github.com/docker/distribution/digest"
  10. "github.com/docker/distribution/reference"
  11. )
  12. var (
  13. // ErrBlobExists returned when blob already exists
  14. ErrBlobExists = errors.New("blob exists")
  15. // ErrBlobDigestUnsupported when blob digest is an unsupported version.
  16. ErrBlobDigestUnsupported = errors.New("unsupported blob digest")
  17. // ErrBlobUnknown when blob is not found.
  18. ErrBlobUnknown = errors.New("unknown blob")
  19. // ErrBlobUploadUnknown returned when upload is not found.
  20. ErrBlobUploadUnknown = errors.New("blob upload unknown")
  21. // ErrBlobInvalidLength returned when the blob has an expected length on
  22. // commit, meaning mismatched with the descriptor or an invalid value.
  23. ErrBlobInvalidLength = errors.New("blob invalid length")
  24. )
  25. // ErrBlobInvalidDigest returned when digest check fails.
  26. type ErrBlobInvalidDigest struct {
  27. Digest digest.Digest
  28. Reason error
  29. }
  30. func (err ErrBlobInvalidDigest) Error() string {
  31. return fmt.Sprintf("invalid digest for referenced layer: %v, %v",
  32. err.Digest, err.Reason)
  33. }
  34. // ErrBlobMounted returned when a blob is mounted from another repository
  35. // instead of initiating an upload session.
  36. type ErrBlobMounted struct {
  37. From reference.Canonical
  38. Descriptor Descriptor
  39. }
  40. func (err ErrBlobMounted) Error() string {
  41. return fmt.Sprintf("blob mounted from: %v to: %v",
  42. err.From, err.Descriptor)
  43. }
  44. // Descriptor describes targeted content. Used in conjunction with a blob
  45. // store, a descriptor can be used to fetch, store and target any kind of
  46. // blob. The struct also describes the wire protocol format. Fields should
  47. // only be added but never changed.
  48. type Descriptor struct {
  49. // MediaType describe the type of the content. All text based formats are
  50. // encoded as utf-8.
  51. MediaType string `json:"mediaType,omitempty"`
  52. // Size in bytes of content.
  53. Size int64 `json:"size,omitempty"`
  54. // Digest uniquely identifies the content. A byte stream can be verified
  55. // against against this digest.
  56. Digest digest.Digest `json:"digest,omitempty"`
  57. // URLs contains the source URLs of this content.
  58. URLs []string `json:"urls,omitempty"`
  59. // NOTE: Before adding a field here, please ensure that all
  60. // other options have been exhausted. Much of the type relationships
  61. // depend on the simplicity of this type.
  62. }
  63. // Descriptor returns the descriptor, to make it satisfy the Describable
  64. // interface. Note that implementations of Describable are generally objects
  65. // which can be described, not simply descriptors; this exception is in place
  66. // to make it more convenient to pass actual descriptors to functions that
  67. // expect Describable objects.
  68. func (d Descriptor) Descriptor() Descriptor {
  69. return d
  70. }
  71. // BlobStatter makes blob descriptors available by digest. The service may
  72. // provide a descriptor of a different digest if the provided digest is not
  73. // canonical.
  74. type BlobStatter interface {
  75. // Stat provides metadata about a blob identified by the digest. If the
  76. // blob is unknown to the describer, ErrBlobUnknown will be returned.
  77. Stat(ctx context.Context, dgst digest.Digest) (Descriptor, error)
  78. }
  79. // BlobDeleter enables deleting blobs from storage.
  80. type BlobDeleter interface {
  81. Delete(ctx context.Context, dgst digest.Digest) error
  82. }
  83. // BlobEnumerator enables iterating over blobs from storage
  84. type BlobEnumerator interface {
  85. Enumerate(ctx context.Context, ingester func(dgst digest.Digest) error) error
  86. }
  87. // BlobDescriptorService manages metadata about a blob by digest. Most
  88. // implementations will not expose such an interface explicitly. Such mappings
  89. // should be maintained by interacting with the BlobIngester. Hence, this is
  90. // left off of BlobService and BlobStore.
  91. type BlobDescriptorService interface {
  92. BlobStatter
  93. // SetDescriptor assigns the descriptor to the digest. The provided digest and
  94. // the digest in the descriptor must map to identical content but they may
  95. // differ on their algorithm. The descriptor must have the canonical
  96. // digest of the content and the digest algorithm must match the
  97. // annotators canonical algorithm.
  98. //
  99. // Such a facility can be used to map blobs between digest domains, with
  100. // the restriction that the algorithm of the descriptor must match the
  101. // canonical algorithm (ie sha256) of the annotator.
  102. SetDescriptor(ctx context.Context, dgst digest.Digest, desc Descriptor) error
  103. // Clear enables descriptors to be unlinked
  104. Clear(ctx context.Context, dgst digest.Digest) error
  105. }
  106. // BlobDescriptorServiceFactory creates middleware for BlobDescriptorService.
  107. type BlobDescriptorServiceFactory interface {
  108. BlobAccessController(svc BlobDescriptorService) BlobDescriptorService
  109. }
  110. // ReadSeekCloser is the primary reader type for blob data, combining
  111. // io.ReadSeeker with io.Closer.
  112. type ReadSeekCloser interface {
  113. io.ReadSeeker
  114. io.Closer
  115. }
  116. // BlobProvider describes operations for getting blob data.
  117. type BlobProvider interface {
  118. // Get returns the entire blob identified by digest along with the descriptor.
  119. Get(ctx context.Context, dgst digest.Digest) ([]byte, error)
  120. // Open provides a ReadSeekCloser to the blob identified by the provided
  121. // descriptor. If the blob is not known to the service, an error will be
  122. // returned.
  123. Open(ctx context.Context, dgst digest.Digest) (ReadSeekCloser, error)
  124. }
  125. // BlobServer can serve blobs via http.
  126. type BlobServer interface {
  127. // ServeBlob attempts to serve the blob, identifed by dgst, via http. The
  128. // service may decide to redirect the client elsewhere or serve the data
  129. // directly.
  130. //
  131. // This handler only issues successful responses, such as 2xx or 3xx,
  132. // meaning it serves data or issues a redirect. If the blob is not
  133. // available, an error will be returned and the caller may still issue a
  134. // response.
  135. //
  136. // The implementation may serve the same blob from a different digest
  137. // domain. The appropriate headers will be set for the blob, unless they
  138. // have already been set by the caller.
  139. ServeBlob(ctx context.Context, w http.ResponseWriter, r *http.Request, dgst digest.Digest) error
  140. }
  141. // BlobIngester ingests blob data.
  142. type BlobIngester interface {
  143. // Put inserts the content p into the blob service, returning a descriptor
  144. // or an error.
  145. Put(ctx context.Context, mediaType string, p []byte) (Descriptor, error)
  146. // Create allocates a new blob writer to add a blob to this service. The
  147. // returned handle can be written to and later resumed using an opaque
  148. // identifier. With this approach, one can Close and Resume a BlobWriter
  149. // multiple times until the BlobWriter is committed or cancelled.
  150. Create(ctx context.Context, options ...BlobCreateOption) (BlobWriter, error)
  151. // Resume attempts to resume a write to a blob, identified by an id.
  152. Resume(ctx context.Context, id string) (BlobWriter, error)
  153. }
  154. // BlobCreateOption is a general extensible function argument for blob creation
  155. // methods. A BlobIngester may choose to honor any or none of the given
  156. // BlobCreateOptions, which can be specific to the implementation of the
  157. // BlobIngester receiving them.
  158. // TODO (brianbland): unify this with ManifestServiceOption in the future
  159. type BlobCreateOption interface {
  160. Apply(interface{}) error
  161. }
  162. // CreateOptions is a collection of blob creation modifiers relevant to general
  163. // blob storage intended to be configured by the BlobCreateOption.Apply method.
  164. type CreateOptions struct {
  165. Mount struct {
  166. ShouldMount bool
  167. From reference.Canonical
  168. // Stat allows to pass precalculated descriptor to link and return.
  169. // Blob access check will be skipped if set.
  170. Stat *Descriptor
  171. }
  172. }
  173. // BlobWriter provides a handle for inserting data into a blob store.
  174. // Instances should be obtained from BlobWriteService.Writer and
  175. // BlobWriteService.Resume. If supported by the store, a writer can be
  176. // recovered with the id.
  177. type BlobWriter interface {
  178. io.WriteCloser
  179. io.ReaderFrom
  180. // Size returns the number of bytes written to this blob.
  181. Size() int64
  182. // ID returns the identifier for this writer. The ID can be used with the
  183. // Blob service to later resume the write.
  184. ID() string
  185. // StartedAt returns the time this blob write was started.
  186. StartedAt() time.Time
  187. // Commit completes the blob writer process. The content is verified
  188. // against the provided provisional descriptor, which may result in an
  189. // error. Depending on the implementation, written data may be validated
  190. // against the provisional descriptor fields. If MediaType is not present,
  191. // the implementation may reject the commit or assign "application/octet-
  192. // stream" to the blob. The returned descriptor may have a different
  193. // digest depending on the blob store, referred to as the canonical
  194. // descriptor.
  195. Commit(ctx context.Context, provisional Descriptor) (canonical Descriptor, err error)
  196. // Cancel ends the blob write without storing any data and frees any
  197. // associated resources. Any data written thus far will be lost. Cancel
  198. // implementations should allow multiple calls even after a commit that
  199. // result in a no-op. This allows use of Cancel in a defer statement,
  200. // increasing the assurance that it is correctly called.
  201. Cancel(ctx context.Context) error
  202. }
  203. // BlobService combines the operations to access, read and write blobs. This
  204. // can be used to describe remote blob services.
  205. type BlobService interface {
  206. BlobStatter
  207. BlobProvider
  208. BlobIngester
  209. }
  210. // BlobStore represent the entire suite of blob related operations. Such an
  211. // implementation can access, read, write, delete and serve blobs.
  212. type BlobStore interface {
  213. BlobService
  214. BlobServer
  215. BlobDeleter
  216. }