blobs.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. )
  11. var (
  12. // ErrBlobExists returned when blob already exists
  13. ErrBlobExists = errors.New("blob exists")
  14. // ErrBlobDigestUnsupported when blob digest is an unsupported version.
  15. ErrBlobDigestUnsupported = errors.New("unsupported blob digest")
  16. // ErrBlobUnknown when blob is not found.
  17. ErrBlobUnknown = errors.New("unknown blob")
  18. // ErrBlobUploadUnknown returned when upload is not found.
  19. ErrBlobUploadUnknown = errors.New("blob upload unknown")
  20. // ErrBlobInvalidLength returned when the blob has an expected length on
  21. // commit, meaning mismatched with the descriptor or an invalid value.
  22. ErrBlobInvalidLength = errors.New("blob invalid length")
  23. // ErrUnsupported returned when an unsupported operation is attempted
  24. ErrUnsupported = errors.New("unsupported operation")
  25. )
  26. // ErrBlobInvalidDigest returned when digest check fails.
  27. type ErrBlobInvalidDigest struct {
  28. Digest digest.Digest
  29. Reason error
  30. }
  31. func (err ErrBlobInvalidDigest) Error() string {
  32. return fmt.Sprintf("invalid digest for referenced layer: %v, %v",
  33. err.Digest, err.Reason)
  34. }
  35. // Descriptor describes targeted content. Used in conjunction with a blob
  36. // store, a descriptor can be used to fetch, store and target any kind of
  37. // blob. The struct also describes the wire protocol format. Fields should
  38. // only be added but never changed.
  39. type Descriptor struct {
  40. // MediaType describe the type of the content. All text based formats are
  41. // encoded as utf-8.
  42. MediaType string `json:"mediaType,omitempty"`
  43. // Size in bytes of content.
  44. Size int64 `json:"size,omitempty"`
  45. // Digest uniquely identifies the content. A byte stream can be verified
  46. // against against this digest.
  47. Digest digest.Digest `json:"digest,omitempty"`
  48. // NOTE: Before adding a field here, please ensure that all
  49. // other options have been exhausted. Much of the type relationships
  50. // depend on the simplicity of this type.
  51. }
  52. // BlobStatter makes blob descriptors available by digest. The service may
  53. // provide a descriptor of a different digest if the provided digest is not
  54. // canonical.
  55. type BlobStatter interface {
  56. // Stat provides metadata about a blob identified by the digest. If the
  57. // blob is unknown to the describer, ErrBlobUnknown will be returned.
  58. Stat(ctx context.Context, dgst digest.Digest) (Descriptor, error)
  59. }
  60. // BlobDeleter enables deleting blobs from storage.
  61. type BlobDeleter interface {
  62. Delete(ctx context.Context, dgst digest.Digest) error
  63. }
  64. // BlobDescriptorService manages metadata about a blob by digest. Most
  65. // implementations will not expose such an interface explicitly. Such mappings
  66. // should be maintained by interacting with the BlobIngester. Hence, this is
  67. // left off of BlobService and BlobStore.
  68. type BlobDescriptorService interface {
  69. BlobStatter
  70. // SetDescriptor assigns the descriptor to the digest. The provided digest and
  71. // the digest in the descriptor must map to identical content but they may
  72. // differ on their algorithm. The descriptor must have the canonical
  73. // digest of the content and the digest algorithm must match the
  74. // annotators canonical algorithm.
  75. //
  76. // Such a facility can be used to map blobs between digest domains, with
  77. // the restriction that the algorithm of the descriptor must match the
  78. // canonical algorithm (ie sha256) of the annotator.
  79. SetDescriptor(ctx context.Context, dgst digest.Digest, desc Descriptor) error
  80. // Clear enables descriptors to be unlinked
  81. Clear(ctx context.Context, dgst digest.Digest) error
  82. }
  83. // ReadSeekCloser is the primary reader type for blob data, combining
  84. // io.ReadSeeker with io.Closer.
  85. type ReadSeekCloser interface {
  86. io.ReadSeeker
  87. io.Closer
  88. }
  89. // BlobProvider describes operations for getting blob data.
  90. type BlobProvider interface {
  91. // Get returns the entire blob identified by digest along with the descriptor.
  92. Get(ctx context.Context, dgst digest.Digest) ([]byte, error)
  93. // Open provides a ReadSeekCloser to the blob identified by the provided
  94. // descriptor. If the blob is not known to the service, an error will be
  95. // returned.
  96. Open(ctx context.Context, dgst digest.Digest) (ReadSeekCloser, error)
  97. }
  98. // BlobServer can serve blobs via http.
  99. type BlobServer interface {
  100. // ServeBlob attempts to serve the blob, identifed by dgst, via http. The
  101. // service may decide to redirect the client elsewhere or serve the data
  102. // directly.
  103. //
  104. // This handler only issues successful responses, such as 2xx or 3xx,
  105. // meaning it serves data or issues a redirect. If the blob is not
  106. // available, an error will be returned and the caller may still issue a
  107. // response.
  108. //
  109. // The implementation may serve the same blob from a different digest
  110. // domain. The appropriate headers will be set for the blob, unless they
  111. // have already been set by the caller.
  112. ServeBlob(ctx context.Context, w http.ResponseWriter, r *http.Request, dgst digest.Digest) error
  113. }
  114. // BlobIngester ingests blob data.
  115. type BlobIngester interface {
  116. // Put inserts the content p into the blob service, returning a descriptor
  117. // or an error.
  118. Put(ctx context.Context, mediaType string, p []byte) (Descriptor, error)
  119. // Create allocates a new blob writer to add a blob to this service. The
  120. // returned handle can be written to and later resumed using an opaque
  121. // identifier. With this approach, one can Close and Resume a BlobWriter
  122. // multiple times until the BlobWriter is committed or cancelled.
  123. Create(ctx context.Context) (BlobWriter, error)
  124. // Resume attempts to resume a write to a blob, identified by an id.
  125. Resume(ctx context.Context, id string) (BlobWriter, error)
  126. }
  127. // BlobWriter provides a handle for inserting data into a blob store.
  128. // Instances should be obtained from BlobWriteService.Writer and
  129. // BlobWriteService.Resume. If supported by the store, a writer can be
  130. // recovered with the id.
  131. type BlobWriter interface {
  132. io.WriteSeeker
  133. io.ReaderFrom
  134. io.Closer
  135. // ID returns the identifier for this writer. The ID can be used with the
  136. // Blob service to later resume the write.
  137. ID() string
  138. // StartedAt returns the time this blob write was started.
  139. StartedAt() time.Time
  140. // Commit completes the blob writer process. The content is verified
  141. // against the provided provisional descriptor, which may result in an
  142. // error. Depending on the implementation, written data may be validated
  143. // against the provisional descriptor fields. If MediaType is not present,
  144. // the implementation may reject the commit or assign "application/octet-
  145. // stream" to the blob. The returned descriptor may have a different
  146. // digest depending on the blob store, referred to as the canonical
  147. // descriptor.
  148. Commit(ctx context.Context, provisional Descriptor) (canonical Descriptor, err error)
  149. // Cancel ends the blob write without storing any data and frees any
  150. // associated resources. Any data written thus far will be lost. Cancel
  151. // implementations should allow multiple calls even after a commit that
  152. // result in a no-op. This allows use of Cancel in a defer statement,
  153. // increasing the assurance that it is correctly called.
  154. Cancel(ctx context.Context) error
  155. }
  156. // BlobService combines the operations to access, read and write blobs. This
  157. // can be used to describe remote blob services.
  158. type BlobService interface {
  159. BlobStatter
  160. BlobProvider
  161. BlobIngester
  162. }
  163. // BlobStore represent the entire suite of blob related operations. Such an
  164. // implementation can access, read, write, delete and serve blobs.
  165. type BlobStore interface {
  166. BlobService
  167. BlobServer
  168. BlobDeleter
  169. }