-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
artifact_server.go
245 lines (206 loc) · 6.7 KB
/
artifact_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package artifacts
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/argoproj/argo-workflows/v3/persist/sqldb"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
"github.com/argoproj/argo-workflows/v3/server/auth"
"github.com/argoproj/argo-workflows/v3/util/instanceid"
"github.com/argoproj/argo-workflows/v3/workflow/artifactrepositories"
artifact "github.com/argoproj/argo-workflows/v3/workflow/artifacts"
"github.com/argoproj/argo-workflows/v3/workflow/hydrator"
)
type ArtifactServer struct {
gatekeeper auth.Gatekeeper
hydrator hydrator.Interface
wfArchive sqldb.WorkflowArchive
instanceIDService instanceid.Service
artDriverFactory artifact.NewDriverFunc
artifactRepositories artifactrepositories.Interface
}
func NewArtifactServer(authN auth.Gatekeeper, hydrator hydrator.Interface, wfArchive sqldb.WorkflowArchive, instanceIDService instanceid.Service, artifactRepositories artifactrepositories.Interface) *ArtifactServer {
return newArtifactServer(authN, hydrator, wfArchive, instanceIDService, artifact.NewDriver, artifactRepositories)
}
func newArtifactServer(authN auth.Gatekeeper, hydrator hydrator.Interface, wfArchive sqldb.WorkflowArchive, instanceIDService instanceid.Service, artDriverFactory artifact.NewDriverFunc, artifactRepositories artifactrepositories.Interface) *ArtifactServer {
return &ArtifactServer{authN, hydrator, wfArchive, instanceIDService, artDriverFactory, artifactRepositories}
}
func (a *ArtifactServer) GetOutputArtifact(w http.ResponseWriter, r *http.Request) {
a.getArtifact(w, r, false)
}
func (a *ArtifactServer) GetInputArtifact(w http.ResponseWriter, r *http.Request) {
a.getArtifact(w, r, true)
}
func (a *ArtifactServer) getArtifact(w http.ResponseWriter, r *http.Request, isInput bool) {
ctx, err := a.gateKeeping(r)
if err != nil {
w.WriteHeader(401)
_, _ = w.Write([]byte(err.Error()))
return
}
path := strings.SplitN(r.URL.Path, "/", 6)
namespace := path[2]
workflowName := path[3]
nodeId := path[4]
artifactName := path[5]
log.WithFields(log.Fields{"namespace": namespace, "workflowName": workflowName, "nodeId": nodeId, "artifactName": artifactName, "isInput": isInput}).Info("Download artifact")
wf, err := a.getWorkflowAndValidate(ctx, namespace, workflowName)
if err != nil {
a.serverInternalError(err, w)
return
}
err = a.returnArtifact(ctx, w, r, wf, nodeId, artifactName, isInput)
if err != nil {
a.serverInternalError(err, w)
return
}
}
func (a *ArtifactServer) GetOutputArtifactByUID(w http.ResponseWriter, r *http.Request) {
a.getArtifactByUID(w, r, false)
}
func (a *ArtifactServer) GetInputArtifactByUID(w http.ResponseWriter, r *http.Request) {
a.getArtifactByUID(w, r, true)
}
func (a *ArtifactServer) getArtifactByUID(w http.ResponseWriter, r *http.Request, isInput bool) {
ctx, err := a.gateKeeping(r)
if err != nil {
w.WriteHeader(401)
_, _ = w.Write([]byte(err.Error()))
return
}
path := strings.SplitN(r.URL.Path, "/", 6)
uid := path[2]
nodeId := path[3]
artifactName := path[4]
log.WithFields(log.Fields{"uid": uid, "nodeId": nodeId, "artifactName": artifactName, "isInput": isInput}).Info("Download artifact")
wf, err := a.getWorkflowByUID(ctx, uid)
if err != nil {
a.serverInternalError(err, w)
return
}
err = a.returnArtifact(ctx, w, r, wf, nodeId, artifactName, isInput)
if err != nil {
a.serverInternalError(err, w)
return
}
}
func (a *ArtifactServer) gateKeeping(r *http.Request) (context.Context, error) {
token := r.Header.Get("Authorization")
if token == "" {
cookie, err := r.Cookie("authorization")
if err != nil {
if err != http.ErrNoCookie {
return nil, err
}
} else {
token = cookie.Value
}
}
ctx := metadata.NewIncomingContext(r.Context(), metadata.MD{"authorization": []string{token}})
return a.gatekeeper.Context(ctx)
}
func (a *ArtifactServer) serverInternalError(err error, w http.ResponseWriter) {
w.WriteHeader(500)
_, _ = w.Write([]byte(err.Error()))
}
func (a *ArtifactServer) returnArtifact(ctx context.Context, w http.ResponseWriter, r *http.Request, wf *wfv1.Workflow, nodeId, artifactName string, isInput bool) error {
kubeClient := auth.GetKubeClient(ctx)
var art *wfv1.Artifact
if isInput {
art = wf.Status.Nodes[nodeId].Inputs.GetArtifactByName(artifactName)
} else {
art = wf.Status.Nodes[nodeId].Outputs.GetArtifactByName(artifactName)
}
if art == nil {
return fmt.Errorf("artifact not found")
}
ref, err := a.artifactRepositories.Resolve(ctx, wf.Spec.ArtifactRepositoryRef, wf.Namespace)
if err != nil {
return err
}
ar, err := a.artifactRepositories.Get(ctx, ref)
if err != nil {
return err
}
l := ar.ToArtifactLocation()
err = art.Relocate(l)
if err != nil {
return err
}
driver, err := a.artDriverFactory(ctx, art, resources{kubeClient, wf.Namespace})
if err != nil {
return err
}
tmp, err := ioutil.TempFile("/tmp", "artifact")
if err != nil {
return err
}
tmpPath := tmp.Name()
defer func() { _ = os.Remove(tmpPath) }()
err = driver.Load(art, tmpPath)
if err != nil {
return err
}
file, err := os.Open(filepath.Clean(tmpPath))
if err != nil {
return err
}
defer func() {
if err := file.Close(); err != nil {
log.Fatalf("Error closing file[%s]: %v", tmpPath, err)
}
}()
stats, err := file.Stat()
if err != nil {
return err
}
contentLength := strconv.FormatInt(stats.Size(), 10)
log.WithFields(log.Fields{"size": contentLength}).Debug("Artifact file size")
key, _ := art.GetKey()
w.Header().Add("Content-Disposition", fmt.Sprintf(`filename="%s"`, path.Base(key)))
w.WriteHeader(200)
http.ServeContent(w, r, "", time.Time{}, file)
return nil
}
func (a *ArtifactServer) getWorkflowAndValidate(ctx context.Context, namespace string, workflowName string) (*wfv1.Workflow, error) {
wfClient := auth.GetWfClient(ctx)
wf, err := wfClient.ArgoprojV1alpha1().Workflows(namespace).Get(ctx, workflowName, metav1.GetOptions{})
if err != nil {
return nil, err
}
err = a.instanceIDService.Validate(wf)
if err != nil {
return nil, err
}
err = a.hydrator.Hydrate(wf)
if err != nil {
return nil, err
}
return wf, nil
}
func (a *ArtifactServer) getWorkflowByUID(ctx context.Context, uid string) (*wfv1.Workflow, error) {
wf, err := a.wfArchive.GetWorkflow(uid)
if err != nil {
return nil, err
}
allowed, err := auth.CanI(ctx, "get", "workflows", wf.Namespace, wf.Name)
if err != nil {
return nil, err
}
if !allowed {
return nil, status.Error(codes.PermissionDenied, "permission denied")
}
return wf, nil
}