github.com/m-lab/locate@v0.17.6/handler/monitoring.go (about) 1 package handler 2 3 import ( 4 "net/http" 5 6 "github.com/m-lab/access/controller" 7 "github.com/m-lab/go/host" 8 v2 "github.com/m-lab/locate/api/v2" 9 "github.com/m-lab/locate/static" 10 ) 11 12 // Monitoring issues access tokens for end to end monitoring requests. 13 func (c *Client) Monitoring(rw http.ResponseWriter, req *http.Request) { 14 result := v2.MonitoringResult{} 15 16 // Validate request. 17 cl := controller.GetClaim(req.Context()) 18 if cl == nil { 19 result.Error = v2.NewError("claim", "Must provide access_token", http.StatusBadRequest) 20 writeResult(rw, result.Error.Status, &result) 21 return 22 } 23 24 // Check that the given subject appears to be an M-Lab machine name. 25 m, err := host.Parse(cl.Subject) 26 if err != nil { 27 result.Error = v2.NewError("subject", "Subject must be specified", http.StatusBadRequest) 28 writeResult(rw, result.Error.Status, &result) 29 return 30 } 31 32 // Lookup service configuration. 33 experiment, service := getExperimentAndService(req.URL.Path) 34 ports, ok := static.Configs[service] 35 if !ok { 36 result.Error = v2.NewError("config", "Unknown service: "+service, http.StatusBadRequest) 37 writeResult(rw, result.Error.Status, &result) 38 return 39 } 40 41 // Preserve other, given request parameters. 42 values := req.URL.Query() 43 values.Del("access_token") 44 45 // Get monitoring subject access tokens for the given machine. 46 machine := cl.Subject 47 token := c.getAccessToken(cl.Subject, static.SubjectMonitoring) 48 // NOTE: v2 vs v3 naming 49 // v2 monitoring uses the non-service, machine name as the subject. 50 // v3 monitoring uses the service name as the subject, so this should be a noop. 51 m.Service = experiment 52 hostname := m.StringWithService() 53 urls := c.getURLs(ports, hostname, token, values) 54 result.AccessToken = token 55 result.Target = &v2.Target{ 56 // Monitoring results only include one target. 57 Machine: machine, 58 Hostname: hostname, 59 URLs: urls, 60 } 61 result.Results = append(result.Results, v2.Target{ 62 Machine: machine, 63 Hostname: hostname, 64 URLs: urls, 65 }) 66 writeResult(rw, http.StatusOK, &result) 67 }