/
info.go
96 lines (88 loc) · 3.3 KB
/
info.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
// Copyright 2015 The LUCI Authors.
//
// 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 signing
import (
"context"
"fmt"
"strings"
"time"
"github.com/TriggerMail/luci-go/auth/identity"
"github.com/TriggerMail/luci-go/server/auth/internal"
"github.com/TriggerMail/luci-go/server/caching"
)
// URL string => *ServiceInfo.
var infoCache = caching.RegisterLRUCache(256)
// ServiceInfo describes identity of some service.
//
// It matches JSON format of /auth/api/v1/server/info endpoint.
type ServiceInfo struct {
AppID string `json:"app_id,omitempty"`
AppRuntime string `json:"app_runtime,omitempty"`
AppRuntimeVersion string `json:"app_runtime_version,omitempty"`
AppVersion string `json:"app_version,omitempty"`
ServiceAccountName string `json:"service_account_name,omitempty"`
}
// FetchServiceInfo fetches information about the service from the given URL.
//
// The server is expected to reply with JSON described by ServiceInfo struct
// (like LUCI services do). Uses process cache to cache the response for 1h.
//
// LUCI services serve the service info at /auth/api/v1/server/info.
func FetchServiceInfo(c context.Context, url string) (*ServiceInfo, error) {
info, err := infoCache.LRU(c).GetOrCreate(c, url, func() (interface{}, time.Duration, error) {
info := &ServiceInfo{}
req := internal.Request{
Method: "GET",
URL: url,
Out: info,
}
if err := req.Do(c); err != nil {
return nil, 0, err
}
return info, time.Hour, nil
})
if err != nil {
return nil, err
}
return info.(*ServiceInfo), nil
}
// FetchServiceInfoFromLUCIService is shortcut for FetchServiceInfo that uses
// LUCI-specific endpoint.
//
// 'serviceURL' is root URL of the service (e.g. 'https://example.com').
func FetchServiceInfoFromLUCIService(c context.Context, serviceURL string) (*ServiceInfo, error) {
serviceURL = strings.ToLower(serviceURL)
if !strings.HasPrefix(serviceURL, "https://") {
return nil, fmt.Errorf("not an https:// URL - %q", serviceURL)
}
domain := strings.TrimPrefix(serviceURL, "https://")
if domain == "" || strings.ContainsRune(domain, '/') {
return nil, fmt.Errorf("not a root URL - %q", serviceURL)
}
return FetchServiceInfo(c, serviceURL+"/auth/api/v1/server/info")
}
// FetchLUCIServiceIdentity returns "service:<app-id>" of a LUCI service.
//
// It is the same thing as inf.AppID returned by FetchServiceInfoFromLUCIService
// except it is cached more aggressively because service ID is static (unlike
// some other ServiceInfo fields).
//
// 'serviceURL' is root URL of the service (e.g. 'https://example.com').
func FetchLUCIServiceIdentity(c context.Context, serviceURL string) (identity.Identity, error) {
info, err := FetchServiceInfoFromLUCIService(c, serviceURL)
if err != nil {
return "", err
}
return identity.MakeIdentity("service:" + info.AppID)
}