-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
files.go
175 lines (144 loc) · 4.27 KB
/
files.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
/*
Copyright 2018 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package web
import (
"context"
"net/http"
"github.com/gravitational/teleport/lib/auth"
"github.com/gravitational/teleport/lib/client"
"github.com/gravitational/teleport/lib/reversetunnel"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/sshutils/scp"
"github.com/gravitational/trace"
"github.com/julienschmidt/httprouter"
)
// fileTransferRequest describes HTTP file transfer request
type fileTransferRequest struct {
// Server describes a server to connect to (serverId|hostname[:port]).
server string
// Login is Linux username to connect as.
login string
// Namespace is node namespace.
namespace string
// Cluster is the name of the remote cluster to connect to.
cluster string
// remoteLocation is file remote location
remoteLocation string
// filename is a file name
filename string
}
func (h *Handler) transferFile(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *SessionContext, site reversetunnel.RemoteSite) (interface{}, error) {
query := r.URL.Query()
req := fileTransferRequest{
login: p.ByName("login"),
namespace: p.ByName("namespace"),
server: p.ByName("server"),
remoteLocation: query.Get("location"),
filename: query.Get("filename"),
}
clt, err := ctx.GetUserClient(site)
if err != nil {
return nil, trace.Wrap(err)
}
ft := fileTransfer{
ctx: ctx,
authClient: clt,
proxyHostPort: h.ProxyHostPort(),
}
isUpload := r.Method == http.MethodPost
if isUpload {
err = ft.upload(req, r)
} else {
err = ft.download(req, w)
}
if err != nil {
return nil, trace.Wrap(err)
}
return ok(), nil
}
type fileTransfer struct {
// ctx is a web session context for the currently logged in user.
ctx *SessionContext
authClient auth.ClientI
proxyHostPort string
}
func (f *fileTransfer) download(req fileTransferRequest, w http.ResponseWriter) error {
cmd, err := scp.CreateHTTPDownload(scp.HTTPTransferRequest{
RemoteLocation: req.remoteLocation,
HTTPResponse: w,
User: f.ctx.GetUser(),
})
if err != nil {
return trace.Wrap(err)
}
tc, err := f.createClient(req)
if err != nil {
return trace.Wrap(err)
}
err = tc.ExecuteSCP(context.TODO(), cmd)
if err != nil {
return trace.Wrap(err)
}
return nil
}
func (f *fileTransfer) upload(req fileTransferRequest, httpReq *http.Request) error {
cmd, err := scp.CreateHTTPUpload(scp.HTTPTransferRequest{
RemoteLocation: req.remoteLocation,
FileName: req.filename,
HTTPRequest: httpReq,
User: f.ctx.GetUser(),
})
if err != nil {
return trace.Wrap(err)
}
tc, err := f.createClient(req)
if err != nil {
return trace.Wrap(err)
}
err = tc.ExecuteSCP(context.TODO(), cmd)
if err != nil {
return trace.Wrap(err)
}
return nil
}
func (f *fileTransfer) createClient(req fileTransferRequest) (*client.TeleportClient, error) {
if !services.IsValidNamespace(req.namespace) {
return nil, trace.BadParameter("invalid namespace %q", req.namespace)
}
if req.login == "" {
return nil, trace.BadParameter("missing login")
}
servers, err := f.authClient.GetNodes(req.namespace)
if err != nil {
return nil, trace.Wrap(err)
}
hostName, hostPort, err := resolveServerHostPort(req.server, servers)
if err != nil {
return nil, trace.BadParameter("invalid server name %q: %v", req.server, err)
}
cfg, err := makeTeleportClientConfig(f.ctx)
if err != nil {
return nil, trace.Wrap(err)
}
cfg.HostLogin = req.login
cfg.SiteName = req.cluster
cfg.Namespace = req.namespace
cfg.ProxyHostPort = f.proxyHostPort
cfg.Host = hostName
cfg.HostPort = hostPort
tc, err := client.NewClient(cfg)
if err != nil {
return nil, trace.BadParameter("failed to create client: %v", err)
}
return tc, nil
}