image.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. package local
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "net/url"
  10. "strings"
  11. "github.com/docker/distribution/digest"
  12. "github.com/docker/distribution/registry/api/errcode"
  13. "github.com/docker/docker/api/server/httputils"
  14. "github.com/docker/docker/builder/dockerfile"
  15. derr "github.com/docker/docker/errors"
  16. "github.com/docker/docker/pkg/ioutils"
  17. "github.com/docker/docker/pkg/streamformatter"
  18. "github.com/docker/docker/reference"
  19. "github.com/docker/docker/runconfig"
  20. "github.com/docker/engine-api/types"
  21. "github.com/docker/engine-api/types/container"
  22. "golang.org/x/net/context"
  23. )
  24. func (s *router) postCommit(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  25. if err := httputils.ParseForm(r); err != nil {
  26. return err
  27. }
  28. if err := httputils.CheckForJSON(r); err != nil {
  29. return err
  30. }
  31. cname := r.Form.Get("container")
  32. pause := httputils.BoolValue(r, "pause")
  33. version := httputils.VersionFromContext(ctx)
  34. if r.FormValue("pause") == "" && version.GreaterThanOrEqualTo("1.13") {
  35. pause = true
  36. }
  37. c, _, _, err := runconfig.DecodeContainerConfig(r.Body)
  38. if err != nil && err != io.EOF { //Do not fail if body is empty.
  39. return err
  40. }
  41. if c == nil {
  42. c = &container.Config{}
  43. }
  44. if !s.daemon.Exists(cname) {
  45. return derr.ErrorCodeNoSuchContainer.WithArgs(cname)
  46. }
  47. newConfig, err := dockerfile.BuildFromConfig(c, r.Form["changes"])
  48. if err != nil {
  49. return err
  50. }
  51. commitCfg := &types.ContainerCommitConfig{
  52. Pause: pause,
  53. Repo: r.Form.Get("repo"),
  54. Tag: r.Form.Get("tag"),
  55. Author: r.Form.Get("author"),
  56. Comment: r.Form.Get("comment"),
  57. Config: newConfig,
  58. MergeConfigs: true,
  59. }
  60. imgID, err := s.daemon.Commit(cname, commitCfg)
  61. if err != nil {
  62. return err
  63. }
  64. return httputils.WriteJSON(w, http.StatusCreated, &types.ContainerCommitResponse{
  65. ID: string(imgID),
  66. })
  67. }
  68. // Creates an image from Pull or from Import
  69. func (s *router) postImagesCreate(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  70. if err := httputils.ParseForm(r); err != nil {
  71. return err
  72. }
  73. var (
  74. image = r.Form.Get("fromImage")
  75. repo = r.Form.Get("repo")
  76. tag = r.Form.Get("tag")
  77. message = r.Form.Get("message")
  78. err error
  79. output = ioutils.NewWriteFlusher(w)
  80. )
  81. defer output.Close()
  82. w.Header().Set("Content-Type", "application/json")
  83. if image != "" { //pull
  84. // Special case: "pull -a" may send an image name with a
  85. // trailing :. This is ugly, but let's not break API
  86. // compatibility.
  87. image = strings.TrimSuffix(image, ":")
  88. var ref reference.Named
  89. ref, err = reference.ParseNamed(image)
  90. if err == nil {
  91. if tag != "" {
  92. // The "tag" could actually be a digest.
  93. var dgst digest.Digest
  94. dgst, err = digest.ParseDigest(tag)
  95. if err == nil {
  96. ref, err = reference.WithDigest(ref, dgst)
  97. } else {
  98. ref, err = reference.WithTag(ref, tag)
  99. }
  100. }
  101. if err == nil {
  102. metaHeaders := map[string][]string{}
  103. for k, v := range r.Header {
  104. if strings.HasPrefix(k, "X-Meta-") {
  105. metaHeaders[k] = v
  106. }
  107. }
  108. authEncoded := r.Header.Get("X-Registry-Auth")
  109. authConfig := &types.AuthConfig{}
  110. if authEncoded != "" {
  111. authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
  112. if err := json.NewDecoder(authJSON).Decode(authConfig); err != nil {
  113. // for a pull it is not an error if no auth was given
  114. // to increase compatibility with the existing api it is defaulting to be empty
  115. authConfig = &types.AuthConfig{}
  116. }
  117. }
  118. err = s.daemon.PullImage(ref, metaHeaders, authConfig, output)
  119. }
  120. }
  121. // Check the error from pulling an image to make sure the request
  122. // was authorized. Modify the status if the request was
  123. // unauthorized to respond with 401 rather than 500.
  124. if err != nil && isAuthorizedError(err) {
  125. err = errcode.ErrorCodeUnauthorized.WithMessage(fmt.Sprintf("Authentication is required: %s", err))
  126. }
  127. } else { //import
  128. var newRef reference.Named
  129. if repo != "" {
  130. var err error
  131. newRef, err = reference.ParseNamed(repo)
  132. if err != nil {
  133. return err
  134. }
  135. if _, isCanonical := newRef.(reference.Canonical); isCanonical {
  136. return errors.New("cannot import digest reference")
  137. }
  138. if tag != "" {
  139. newRef, err = reference.WithTag(newRef, tag)
  140. if err != nil {
  141. return err
  142. }
  143. }
  144. }
  145. src := r.Form.Get("fromSrc")
  146. // 'err' MUST NOT be defined within this block, we need any error
  147. // generated from the download to be available to the output
  148. // stream processing below
  149. var newConfig *container.Config
  150. newConfig, err = dockerfile.BuildFromConfig(&container.Config{}, r.Form["changes"])
  151. if err != nil {
  152. return err
  153. }
  154. err = s.daemon.ImportImage(src, newRef, message, r.Body, output, newConfig)
  155. }
  156. if err != nil {
  157. if !output.Flushed() {
  158. return err
  159. }
  160. sf := streamformatter.NewJSONStreamFormatter()
  161. output.Write(sf.FormatError(err))
  162. }
  163. return nil
  164. }
  165. func (s *router) postImagesPush(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  166. metaHeaders := map[string][]string{}
  167. for k, v := range r.Header {
  168. if strings.HasPrefix(k, "X-Meta-") {
  169. metaHeaders[k] = v
  170. }
  171. }
  172. if err := httputils.ParseForm(r); err != nil {
  173. return err
  174. }
  175. authConfig := &types.AuthConfig{}
  176. authEncoded := r.Header.Get("X-Registry-Auth")
  177. if authEncoded != "" {
  178. // the new format is to handle the authConfig as a header
  179. authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
  180. if err := json.NewDecoder(authJSON).Decode(authConfig); err != nil {
  181. // to increase compatibility to existing api it is defaulting to be empty
  182. authConfig = &types.AuthConfig{}
  183. }
  184. } else {
  185. // the old format is supported for compatibility if there was no authConfig header
  186. if err := json.NewDecoder(r.Body).Decode(authConfig); err != nil {
  187. return fmt.Errorf("Bad parameters and missing X-Registry-Auth: %v", err)
  188. }
  189. }
  190. ref, err := reference.ParseNamed(vars["name"])
  191. if err != nil {
  192. return err
  193. }
  194. tag := r.Form.Get("tag")
  195. if tag != "" {
  196. // Push by digest is not supported, so only tags are supported.
  197. ref, err = reference.WithTag(ref, tag)
  198. if err != nil {
  199. return err
  200. }
  201. }
  202. output := ioutils.NewWriteFlusher(w)
  203. defer output.Close()
  204. w.Header().Set("Content-Type", "application/json")
  205. if err := s.daemon.PushImage(ref, metaHeaders, authConfig, output); err != nil {
  206. if !output.Flushed() {
  207. return err
  208. }
  209. sf := streamformatter.NewJSONStreamFormatter()
  210. output.Write(sf.FormatError(err))
  211. }
  212. return nil
  213. }
  214. func (s *router) getImagesGet(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  215. if err := httputils.ParseForm(r); err != nil {
  216. return err
  217. }
  218. w.Header().Set("Content-Type", "application/x-tar")
  219. output := ioutils.NewWriteFlusher(w)
  220. defer output.Close()
  221. var names []string
  222. if name, ok := vars["name"]; ok {
  223. names = []string{name}
  224. } else {
  225. names = r.Form["names"]
  226. }
  227. if err := s.daemon.ExportImage(names, output); err != nil {
  228. if !output.Flushed() {
  229. return err
  230. }
  231. sf := streamformatter.NewJSONStreamFormatter()
  232. output.Write(sf.FormatError(err))
  233. }
  234. return nil
  235. }
  236. func (s *router) postImagesLoad(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  237. return s.daemon.LoadImage(r.Body, w)
  238. }
  239. func (s *router) deleteImages(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  240. if err := httputils.ParseForm(r); err != nil {
  241. return err
  242. }
  243. name := vars["name"]
  244. if strings.TrimSpace(name) == "" {
  245. return fmt.Errorf("image name cannot be blank")
  246. }
  247. force := httputils.BoolValue(r, "force")
  248. prune := !httputils.BoolValue(r, "noprune")
  249. list, err := s.daemon.ImageDelete(name, force, prune)
  250. if err != nil {
  251. return err
  252. }
  253. return httputils.WriteJSON(w, http.StatusOK, list)
  254. }
  255. func (s *router) getImagesByName(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  256. imageInspect, err := s.daemon.LookupImage(vars["name"])
  257. if err != nil {
  258. return err
  259. }
  260. return httputils.WriteJSON(w, http.StatusOK, imageInspect)
  261. }
  262. func (s *router) getImagesJSON(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  263. if err := httputils.ParseForm(r); err != nil {
  264. return err
  265. }
  266. // FIXME: The filter parameter could just be a match filter
  267. images, err := s.daemon.Images(r.Form.Get("filters"), r.Form.Get("filter"), httputils.BoolValue(r, "all"))
  268. if err != nil {
  269. return err
  270. }
  271. return httputils.WriteJSON(w, http.StatusOK, images)
  272. }
  273. func (s *router) getImagesHistory(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  274. name := vars["name"]
  275. history, err := s.daemon.ImageHistory(name)
  276. if err != nil {
  277. return err
  278. }
  279. return httputils.WriteJSON(w, http.StatusOK, history)
  280. }
  281. func (s *router) postImagesTag(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  282. if err := httputils.ParseForm(r); err != nil {
  283. return err
  284. }
  285. repo := r.Form.Get("repo")
  286. tag := r.Form.Get("tag")
  287. newTag, err := reference.WithName(repo)
  288. if err != nil {
  289. return err
  290. }
  291. if tag != "" {
  292. if newTag, err = reference.WithTag(newTag, tag); err != nil {
  293. return err
  294. }
  295. }
  296. if err := s.daemon.TagImage(newTag, vars["name"]); err != nil {
  297. return err
  298. }
  299. w.WriteHeader(http.StatusCreated)
  300. return nil
  301. }
  302. func (s *router) getImagesSearch(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
  303. if err := httputils.ParseForm(r); err != nil {
  304. return err
  305. }
  306. var (
  307. config *types.AuthConfig
  308. authEncoded = r.Header.Get("X-Registry-Auth")
  309. headers = map[string][]string{}
  310. )
  311. if authEncoded != "" {
  312. authJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
  313. if err := json.NewDecoder(authJSON).Decode(&config); err != nil {
  314. // for a search it is not an error if no auth was given
  315. // to increase compatibility with the existing api it is defaulting to be empty
  316. config = &types.AuthConfig{}
  317. }
  318. }
  319. for k, v := range r.Header {
  320. if strings.HasPrefix(k, "X-Meta-") {
  321. headers[k] = v
  322. }
  323. }
  324. query, err := s.daemon.SearchRegistryForImages(r.Form.Get("term"), config, headers)
  325. if err != nil {
  326. return err
  327. }
  328. return httputils.WriteJSON(w, http.StatusOK, query.Results)
  329. }
  330. func isAuthorizedError(err error) bool {
  331. if urlError, ok := err.(*url.Error); ok {
  332. err = urlError.Err
  333. }
  334. if dError, ok := err.(errcode.Error); ok {
  335. if dError.ErrorCode() == errcode.ErrorCodeUnauthorized {
  336. return true
  337. }
  338. }
  339. return false
  340. }