-
Notifications
You must be signed in to change notification settings - Fork 18
/
director_ui.go
300 lines (281 loc) · 10.5 KB
/
director_ui.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/***************************************************************
*
* Copyright (C) 2024, Pelican Project, Morgridge Institute for Research
*
* 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 director
import (
"fmt"
"net/http"
"net/url"
"path"
"strings"
"github.com/gin-gonic/gin"
"github.com/pelicanplatform/pelican/param"
"github.com/pelicanplatform/pelican/server_structs"
"github.com/pelicanplatform/pelican/web_ui"
log "github.com/sirupsen/logrus"
)
type (
listServerRequest struct {
ServerType string `form:"server_type"` // "cache" or "origin"
}
listServerResponse struct {
Name string `json:"name"`
AuthURL string `json:"authUrl"`
BrokerURL string `json:"brokerUrl"`
URL string `json:"url"` // This is server's XRootD URL for file transfer
WebURL string `json:"webUrl"` // This is server's Web interface and API
Type server_structs.ServerType `json:"type"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Writes bool `json:"enableWrite"`
DirectReads bool `json:"enableFallbackRead"`
Listings bool `json:"enableListing"`
Filtered bool `json:"filtered"`
FilteredType string `json:"filteredType"`
Status HealthTestStatus `json:"status"`
NamespacePrefixes []string `json:"namespacePrefixes"`
}
statResponse struct {
OK bool `json:"ok"`
Message string `json:"message"`
Metadata []*objectMetadata `json:"metadata"`
}
statRequest struct {
MinResponses int `form:"min_responses"`
MaxResponses int `form:"max_responses"`
}
supportContactRes struct {
Email string `json:"email"`
Url string `json:"url"`
}
)
func (req listServerRequest) ToInternalServerType() server_structs.ServerType {
if req.ServerType == "cache" {
return server_structs.CacheType
}
if req.ServerType == "origin" {
return server_structs.OriginType
}
return ""
}
func listServers(ctx *gin.Context) {
queryParams := listServerRequest{}
if ctx.ShouldBindQuery(&queryParams) != nil {
ctx.JSON(http.StatusBadRequest, server_structs.SimpleApiResp{
Status: server_structs.RespFailed,
Msg: "Invalid query parameters",
})
return
}
var servers []server_structs.Advertisement
if queryParams.ServerType != "" {
if !strings.EqualFold(queryParams.ServerType, string(server_structs.OriginType)) && !strings.EqualFold(queryParams.ServerType, string(server_structs.CacheType)) {
ctx.JSON(http.StatusBadRequest, server_structs.SimpleApiResp{
Status: server_structs.RespFailed,
Msg: "Invalid server type",
})
return
}
servers = listAdvertisement([]server_structs.ServerType{server_structs.ServerType(queryParams.ToInternalServerType())})
} else {
servers = listAdvertisement([]server_structs.ServerType{server_structs.OriginType, server_structs.CacheType})
}
healthTestUtilsMutex.RLock()
defer healthTestUtilsMutex.RUnlock()
resList := make([]listServerResponse, 0)
for _, server := range servers {
healthStatus := HealthStatusUnknown
healthUtil, ok := healthTestUtils[server.ServerAd]
if ok {
healthStatus = healthUtil.Status
}
filtered, ft := checkFilter(server.Name)
var auth_url string
if server.AuthURL == (url.URL{}) {
auth_url = server.URL.String()
} else {
auth_url = server.AuthURL.String()
}
res := listServerResponse{
Name: server.Name,
BrokerURL: server.BrokerURL.String(),
AuthURL: auth_url,
URL: server.URL.String(),
WebURL: server.WebURL.String(),
Type: server.Type,
Latitude: server.Latitude,
Longitude: server.Longitude,
Writes: server.Writes,
DirectReads: server.DirectReads,
Listings: server.Listings,
Filtered: filtered,
FilteredType: ft.String(),
Status: healthStatus,
}
for _, ns := range server.NamespaceAds {
res.NamespacePrefixes = append(res.NamespacePrefixes, ns.Path)
}
resList = append(resList, res)
}
ctx.JSON(http.StatusOK, resList)
}
func queryOrigins(ctx *gin.Context) {
pathParam := ctx.Param("path")
path := path.Clean(pathParam)
if path == "" || strings.HasSuffix(path, "/") {
ctx.JSON(http.StatusBadRequest, server_structs.SimpleApiResp{
Status: server_structs.RespFailed,
Msg: "Path should not be empty or ended with slash '/'",
})
return
}
queryParams := statRequest{}
if ctx.ShouldBindQuery(&queryParams) != nil {
ctx.JSON(http.StatusBadRequest, server_structs.SimpleApiResp{
Status: server_structs.RespFailed,
Msg: "Invalid query parameters",
})
return
}
meta, msg, err := NewObjectStat().Query(path, ctx, queryParams.MinResponses, queryParams.MaxResponses)
if err != nil {
if err == NoPrefixMatchError {
ctx.JSON(http.StatusNotFound, server_structs.SimpleApiResp{
Status: server_structs.RespFailed,
Msg: err.Error(),
})
return
} else if err == ParameterError {
ctx.JSON(http.StatusBadRequest, server_structs.SimpleApiResp{
Status: server_structs.RespFailed,
Msg: err.Error(),
})
return
} else if err == InsufficientResError {
// Insufficient response does not cause a 500 error, but OK field in response is false
if len(meta) < 1 {
ctx.JSON(http.StatusNotFound, server_structs.SimpleApiResp{
Status: server_structs.RespFailed,
Msg: msg + " If no object is available, please check if the object is in a public namespace.",
})
return
}
res := statResponse{Message: msg, Metadata: meta, OK: false}
ctx.JSON(http.StatusOK, res)
} else {
log.Errorf("Error in NewObjectStat with path: %s, min responses: %d, max responses: %d. %v", path, queryParams.MinResponses, queryParams.MaxResponses, err)
ctx.JSON(http.StatusInternalServerError, server_structs.SimpleApiResp{
Status: server_structs.RespFailed,
Msg: err.Error(),
})
return
}
}
if len(meta) < 1 {
ctx.JSON(http.StatusNotFound, server_structs.SimpleApiResp{
Status: server_structs.RespFailed,
Msg: err.Error() + " If no object is available, please check if the object is in a public namespace.",
})
}
res := statResponse{Message: msg, Metadata: meta, OK: true}
ctx.JSON(http.StatusOK, res)
}
// A gin route handler that given a server hostname through path variable `name`,
// checks and adds the server to a list of servers to be bypassed when the director redirects
// object requests from the client
func handleFilterServer(ctx *gin.Context) {
sn := strings.TrimPrefix(ctx.Param("name"), "/")
if sn == "" {
ctx.JSON(http.StatusBadRequest, server_structs.SimpleApiResp{
Status: server_structs.RespFailed,
Msg: "'name' is a required path parameter",
})
return
}
filtered, filterType := checkFilter(sn)
if filtered {
ctx.JSON(http.StatusBadRequest, server_structs.SimpleApiResp{
Status: server_structs.RespFailed,
Msg: fmt.Sprint("Can't filter a server that already has been fitlered with type ", filterType),
})
return
}
filteredServersMutex.Lock()
defer filteredServersMutex.Unlock()
// If we previously temporarily allowed a server, we switch to permFiltered (reset)
if filterType == tempAllowed {
filteredServers[sn] = permFiltered
} else {
filteredServers[sn] = tempFiltered
}
ctx.JSON(http.StatusOK, server_structs.SimpleApiResp{Status: server_structs.RespOK, Msg: "success"})
}
// A gin route handler that given a server hostname through path variable `name`,
// checks and removes the server from a list of servers to be bypassed when the director redirects
// object requests from the client
func handleAllowServer(ctx *gin.Context) {
sn := strings.TrimPrefix(ctx.Param("name"), "/")
if sn == "" {
ctx.JSON(http.StatusBadRequest, server_structs.SimpleApiResp{
Status: server_structs.RespFailed,
Msg: "'name' is a required path parameter",
})
return
}
filtered, ft := checkFilter(sn)
if !filtered {
ctx.JSON(http.StatusBadRequest, server_structs.SimpleApiResp{
Status: server_structs.RespFailed,
Msg: fmt.Sprintf("Can't allow server %s that is not being filtered", sn),
})
return
}
filteredServersMutex.Lock()
defer filteredServersMutex.Unlock()
if ft == tempFiltered {
// For temporarily filtered server, allowing them by removing the server from the map
delete(filteredServers, sn)
} else if ft == permFiltered {
// For servers to filter from the config, temporarily allow the server
filteredServers[sn] = tempAllowed
} else if ft == topoFiltered {
ctx.JSON(http.StatusBadRequest, server_structs.SimpleApiResp{
Status: server_structs.RespFailed,
Msg: fmt.Sprintf("Can't allow server %s that is disabled by the OSG Topology. Contact OSG admin at support@osg-htc.org to enable the server.", sn),
})
return
}
ctx.JSON(http.StatusOK, server_structs.SimpleApiResp{Status: server_structs.RespOK, Msg: "success"})
}
// Endpoint for director support contact information
func handleDirectorContact(ctx *gin.Context) {
email := param.Director_SupportContactEmail.GetString()
url := param.Director_SupportContactUrl.GetString()
ctx.JSON(http.StatusOK, supportContactRes{Email: email, Url: url})
}
func RegisterDirectorWebAPI(router *gin.RouterGroup) {
directorWebAPI := router.Group("/api/v1.0/director_ui")
// Follow RESTful schema
{
directorWebAPI.GET("/servers", listServers)
directorWebAPI.PATCH("/servers/filter/*name", web_ui.AuthHandler, web_ui.AdminAuthHandler, handleFilterServer)
directorWebAPI.PATCH("/servers/allow/*name", web_ui.AuthHandler, web_ui.AdminAuthHandler, handleAllowServer)
directorWebAPI.GET("/servers/origins/stat/*path", web_ui.AuthHandler, queryOrigins)
directorWebAPI.HEAD("/servers/origins/stat/*path", web_ui.AuthHandler, queryOrigins)
directorWebAPI.GET("/contact", handleDirectorContact)
}
}