|
| 1 | +package application |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "io/ioutil" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/docker/docker/api/types" |
| 15 | + "github.com/docker/docker/client" |
| 16 | + "github.com/go-chi/chi/v5" |
| 17 | + "go.uber.org/zap" |
| 18 | +) |
| 19 | + |
| 20 | +// ImageParams describe body data |
| 21 | +type ImageParams struct { |
| 22 | + Name string `json:"name"` |
| 23 | + AuthToken string `json:"auth_token"` |
| 24 | +} |
| 25 | + |
| 26 | +// PostImageHandler is used to pull the request image, see https://docs.docker.com/engine/api/sdk/examples/#pull-an-image-with-authentication |
| 27 | +func (a *Application) PostImageHandler(w http.ResponseWriter, r *http.Request) { |
| 28 | + var imageParams ImageParams |
| 29 | + |
| 30 | + serviceID := chi.URLParam(r, "serviceID") |
| 31 | + project := chi.URLParam(r, "project") |
| 32 | + commit := chi.URLParam(r, "commit") |
| 33 | + l := a.logger.With( |
| 34 | + zap.String("service", serviceID), |
| 35 | + zap.String("project", project), |
| 36 | + zap.String("commit", commit), |
| 37 | + ) |
| 38 | + |
| 39 | + if !a.Services[serviceID].Meta().UserDockerCompose { |
| 40 | + l.Warn("service does not accept custom image") |
| 41 | + http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + d := json.NewDecoder(r.Body) |
| 46 | + err := d.Decode(&imageParams) |
| 47 | + if err != nil { |
| 48 | + l.Warn("decoding post image handler params error", zap.Error(err)) |
| 49 | + http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + if err := verifyImageName(imageParams.Name, project, commit); err != nil { |
| 54 | + l.Warn("verifying image name error", zap.Error(err)) |
| 55 | + http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + authConfig := types.AuthConfig{ |
| 60 | + Username: "gitlab-ci-token", |
| 61 | + Password: imageParams.AuthToken, |
| 62 | + } |
| 63 | + encoded, err := json.Marshal(authConfig) |
| 64 | + if err != nil { |
| 65 | + l.Warn("encoding auth config", zap.Error(err)) |
| 66 | + http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + auth := base64.URLEncoding.EncodeToString(encoded) |
| 71 | + cli, err := client.NewClientWithOpts(client.FromEnv) |
| 72 | + if err != nil { |
| 73 | + l.Warn("new client error", zap.Error(err)) |
| 74 | + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + ended := make(chan bool) |
| 79 | + f, flushable := w.(http.Flusher) |
| 80 | + if flushable { |
| 81 | + go func() { |
| 82 | + ticker := time.NewTicker(1 * time.Second) |
| 83 | + for { |
| 84 | + select { |
| 85 | + case <-ticker.C: |
| 86 | + io.WriteString(w, "#") |
| 87 | + if flushable { |
| 88 | + f.Flush() |
| 89 | + } |
| 90 | + case <-ended: |
| 91 | + return |
| 92 | + case <-r.Context().Done(): |
| 93 | + return |
| 94 | + } |
| 95 | + } |
| 96 | + }() |
| 97 | + } |
| 98 | + |
| 99 | + out, err := cli.ImagePull(r.Context(), imageParams.Name, types.ImagePullOptions{RegistryAuth: auth}) |
| 100 | + if err != nil { |
| 101 | + l.Warn("error when downloading image", zap.Error(err)) |
| 102 | + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) |
| 103 | + return |
| 104 | + } |
| 105 | + defer out.Close() |
| 106 | + |
| 107 | + // ⚠ consume the reader or nothing happens |
| 108 | + _, err = ioutil.ReadAll(out) |
| 109 | + if flushable { |
| 110 | + ended <- true |
| 111 | + } |
| 112 | + if err != nil { |
| 113 | + l.Warn("error when downloading image", zap.Error(err)) |
| 114 | + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + w.Write([]byte("|> Image pulled successfully\n")) |
| 119 | + w.WriteHeader(http.StatusOK) |
| 120 | +} |
| 121 | + |
| 122 | +func verifyImageName(name string, project string, commit string) error { |
| 123 | + parts := strings.Split(name, ":") |
| 124 | + |
| 125 | + if len(parts) < 2 { |
| 126 | + return fmt.Errorf("invalid image name %s", name) |
| 127 | + } |
| 128 | + |
| 129 | + if parts[len(parts)-1] != commit { |
| 130 | + return fmt.Errorf("image label name is not equal to commit sha : %s != %s", parts[len(parts)-1], commit) |
| 131 | + } |
| 132 | + |
| 133 | + unescaped, err := url.PathUnescape(project) |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + |
| 138 | + if !strings.Contains(name, unescaped) { |
| 139 | + return fmt.Errorf("image %s do not match project %s", name, unescaped) |
| 140 | + } |
| 141 | + |
| 142 | + return nil |
| 143 | +} |
0 commit comments