OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Package info exposes /auth/api/v1/server/info URL and allows to query it. |
| 6 // |
| 7 // This endpoint is used by luci services to advertise service account names |
| 8 // they use (among other things). |
| 9 package info |
| 10 |
| 11 import ( |
| 12 "encoding/json" |
| 13 "net/http" |
| 14 "time" |
| 15 |
| 16 "github.com/julienschmidt/httprouter" |
| 17 "golang.org/x/net/context" |
| 18 |
| 19 "github.com/luci/luci-go/server/auth/internal" |
| 20 "github.com/luci/luci-go/server/middleware" |
| 21 "github.com/luci/luci-go/server/proccache" |
| 22 ) |
| 23 |
| 24 // ServiceInfo describes JSON format of /auth/api/v1/server/info response. |
| 25 type ServiceInfo struct { |
| 26 AppID string `json:"app_id,omitempty"` |
| 27 AppRuntime string `json:"app_runtime,omitempty"` |
| 28 AppRuntimeVersion string `json:"app_runtime_version,omitempty"` |
| 29 AppVersion string `json:"app_version,omitempty"` |
| 30 ServiceAccountName string `json:"service_account_name,omitempty"` |
| 31 } |
| 32 |
| 33 type proccacheKey string |
| 34 |
| 35 // FetchServiceInfo fetches information about the service at given URL. |
| 36 // |
| 37 // Uses proccache to cache it locally for 1 hour. |
| 38 func FetchServiceInfo(c context.Context, serviceURL string) (*ServiceInfo, error
) { |
| 39 info, err := proccache.GetOrMake(c, proccacheKey(serviceURL), func() (in
terface{}, time.Duration, error) { |
| 40 info := &ServiceInfo{} |
| 41 err := internal.FetchJSON(c, info, func() (*http.Request, error)
{ |
| 42 return http.NewRequest("GET", serviceURL+"/auth/api/v1/s
erver/info", nil) |
| 43 }) |
| 44 if err != nil { |
| 45 return nil, 0, err |
| 46 } |
| 47 return info, time.Hour, nil |
| 48 }) |
| 49 if err != nil { |
| 50 return nil, err |
| 51 } |
| 52 return info.(*ServiceInfo), nil |
| 53 } |
| 54 |
| 55 // ServiceInfoCallback is called by /auth/api/v1/server/info handler. |
| 56 type ServiceInfoCallback func(context.Context) (ServiceInfo, error) |
| 57 |
| 58 // InstallHandlers installs handler that serves info provided by the callback. |
| 59 func InstallHandlers(r *httprouter.Router, base middleware.Base, cb ServiceInfoC
allback) { |
| 60 handler := func(c context.Context, w http.ResponseWriter, r *http.Reques
t, p httprouter.Params) { |
| 61 var response struct { |
| 62 ServiceInfo |
| 63 Error string `json:"error,omitempty"` |
| 64 } |
| 65 info, err := cb(c) |
| 66 w.Header().Set("Content-Type", "application/json; charset=utf-8"
) |
| 67 if err != nil { |
| 68 w.WriteHeader(http.StatusInternalServerError) |
| 69 response.Error = err.Error() |
| 70 } else { |
| 71 w.WriteHeader(http.StatusOK) |
| 72 response.ServiceInfo = info |
| 73 } |
| 74 json.NewEncoder(w).Encode(response) |
| 75 } |
| 76 r.GET("/auth/api/v1/server/info", base(handler)) |
| 77 } |
OLD | NEW |