-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathadvertise.go
160 lines (139 loc) · 5.04 KB
/
advertise.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
/***************************************************************
*
* Copyright (C) 2023, 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 origin_ui
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/pelicanplatform/pelican/client"
"github.com/pelicanplatform/pelican/config"
"github.com/pelicanplatform/pelican/director"
"github.com/pelicanplatform/pelican/metrics"
"github.com/pelicanplatform/pelican/param"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
type directorResponse struct {
Error string `json:"error"`
}
func PeriodicAdvertiseOrigin() error {
ticker := time.NewTicker(1 * time.Minute)
go func() {
err := AdvertiseOrigin()
if err != nil {
log.Warningln("Origin advertise failed:", err)
metrics.SetComponentHealthStatus(metrics.OriginCache_Federation, metrics.StatusCritical, "Error advertising origin to federation")
} else {
metrics.SetComponentHealthStatus(metrics.OriginCache_Federation, metrics.StatusOK, "")
}
for {
<-ticker.C
err := AdvertiseOrigin()
if err != nil {
log.Warningln("Origin advertise failed:", err)
metrics.SetComponentHealthStatus(metrics.OriginCache_Federation, metrics.StatusCritical, "Error advertising origin to federation")
} else {
metrics.SetComponentHealthStatus(metrics.OriginCache_Federation, metrics.StatusOK, "")
}
}
}()
return nil
}
func AdvertiseOrigin() error {
name := param.Xrootd_Sitename.GetString()
if name == "" {
return errors.New("Origin name isn't set")
}
// TODO: waiting on a different branch to merge origin URL generation
// The checkdefaults func that runs before the origin is served checks for and
// parses the originUrl, so it should be safe to just grab it as a string here.
originUrl := param.Origin_Url.GetString()
originWebUrl := fmt.Sprintf("https://%s", config.ComputeExternalAddress())
// Here we instantiate the namespaceAd slice, but we still need to define the namespace
namespaceUrl, err := url.Parse(param.Federation_NamespaceUrl.GetString())
if err != nil {
return errors.Wrap(err, "Bad NamespaceUrl")
}
if namespaceUrl.String() == "" {
return errors.New("No NamespaceUrl is set")
}
prefix := param.Origin_NamespacePrefix.GetString()
// TODO: Need to figure out where to get some of these values
// so that they aren't hardcoded...
nsAd := director.NamespaceAd{
RequireToken: true,
Path: prefix,
Issuer: *namespaceUrl,
MaxScopeDepth: 3,
Strategy: "OAuth2",
BasePath: "/",
}
ad := director.OriginAdvertise{
Name: name,
URL: originUrl,
WebURL: originWebUrl,
Namespaces: []director.NamespaceAd{nsAd},
}
body, err := json.Marshal(ad)
if err != nil {
return errors.Wrap(err, "Failed to generate JSON description of origin")
}
directorUrlStr := param.Federation_DirectorUrl.GetString()
if directorUrlStr == "" {
return errors.New("Director endpoint URL is not known")
}
directorUrl, err := url.Parse(directorUrlStr)
if err != nil {
return errors.Wrap(err, "Failed to parse Federation.DirectorURL")
}
directorUrl.Path = "/api/v1.0/director/registerOrigin"
token, err := director.CreateAdvertiseToken(prefix)
if err != nil {
return errors.Wrap(err, "Failed to generate advertise token")
}
req, err := http.NewRequest("POST", directorUrl.String(), bytes.NewBuffer(body))
if err != nil {
return errors.Wrap(err, "Failed to create POST request for director registration")
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
userAgent := "pelican-origin/" + client.ObjectClientOptions.Version
req.Header.Set("User-Agent", userAgent)
// We should switch this over to use the common transport, but for that to happen
// that function needs to be exported from pelican
tr := config.GetTransport()
client := http.Client{Transport: tr}
resp, err := client.Do(req)
if err != nil {
return errors.Wrap(err, "Failed to start request for director registration")
}
defer resp.Body.Close()
body, _ = io.ReadAll(resp.Body)
if resp.StatusCode > 299 {
var respErr directorResponse
if unmarshalErr := json.Unmarshal(body, &respErr); unmarshalErr != nil { // Error creating json
return errors.Wrapf(unmarshalErr, "Could not unmarshall the director's response, which responded %v from director registration: %v", resp.StatusCode, resp.Status)
}
return errors.Errorf("Error during director registration: %v\n", respErr.Error)
}
return nil
}