This repository has been archived by the owner on Oct 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
capi_client.go
187 lines (157 loc) · 4.64 KB
/
capi_client.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
package auth
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
)
// CAPIClient defines a client for accessing the CC API
type CAPIClient struct {
client HTTPClient
capi string
externalCapi string
storeAppsLatency func(float64)
storeListServiceInstancesLatency func(float64)
storeLogAccessLatency func(float64)
storeServiceInstancesLatency func(float64)
}
// NewCAPIClient returns a new CAPIClient
func NewCAPIClient(
capiAddr string,
externalCapiAddr string,
client HTTPClient,
m Metrics,
log *log.Logger,
) *CAPIClient {
_, err := url.Parse(capiAddr)
if err != nil {
log.Fatalf("failed to parse internal CAPI addr: %s", err)
}
_, err = url.Parse(externalCapiAddr)
if err != nil {
log.Fatalf("failed to parse external CAPI addr: %s", err)
}
return &CAPIClient{
client: client,
capi: capiAddr,
externalCapi: externalCapiAddr,
storeAppsLatency: m.NewGauge("LastCAPIV3AppsLatency"),
storeListServiceInstancesLatency: m.NewGauge("LastCAPIV2ListServiceInstancesLatency"),
storeLogAccessLatency: m.NewGauge("LastCAPIV4LogAccessLatency"),
storeServiceInstancesLatency: m.NewGauge("LastCAPIV2ServiceInstancesLatency"),
}
}
// IsAuthorized determines autorization to a given sourceID is valid
func (c *CAPIClient) IsAuthorized(sourceID, token string) bool {
uri := fmt.Sprintf("%s/internal/v4/log_access/%s", c.capi, sourceID)
req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
log.Printf("failed to build authorize log access request: %s", err)
return false
}
req.Header.Set("Authorization", token)
start := time.Now()
resp, err := c.client.Do(req)
c.storeLogAccessLatency(float64(time.Since(start)))
if err != nil {
log.Printf("CAPI request failed: %s", err)
return false
}
defer func(r *http.Response) {
cleanup(r)
}(resp)
if resp.StatusCode == http.StatusOK {
return true
}
uri = fmt.Sprintf("%s/v2/service_instances/%s", c.externalCapi, sourceID)
req, err = http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
log.Printf("failed to build authorize service instance access request: %s", err)
return false
}
req.Header.Set("Authorization", token)
start = time.Now()
resp, err = c.client.Do(req)
c.storeServiceInstancesLatency(float64(time.Since(start)))
if err != nil {
log.Printf("External CAPI request failed: %s", err)
return false
}
defer func(r *http.Response) {
cleanup(r)
}(resp)
return resp.StatusCode == http.StatusOK
}
// AvailableSourceIDs returns all the available source ids a client has access to
func (c *CAPIClient) AvailableSourceIDs(token string) []string {
var sourceIDs []string
req, err := http.NewRequest(http.MethodGet, c.externalCapi+"/v3/apps", nil)
if err != nil {
log.Printf("failed to build authorize log access request: %s", err)
return nil
}
req.Header.Set("Authorization", token)
start := time.Now()
resp, err := c.client.Do(req)
c.storeAppsLatency(float64(time.Since(start)))
if err != nil {
log.Printf("CAPI request failed: %s", err)
return nil
}
defer func(r *http.Response) {
cleanup(r)
}(resp)
if resp.StatusCode != http.StatusOK {
log.Printf("CAPI request failed (/v3/apps): %d", resp.StatusCode)
return nil
}
var appSources struct {
Resources []struct {
Guid string `json:"guid"`
} `json:"resources"`
}
json.NewDecoder(resp.Body).Decode(&appSources)
for _, v := range appSources.Resources {
sourceIDs = append(sourceIDs, v.Guid)
}
req, err = http.NewRequest(http.MethodGet, c.externalCapi+"/v2/service_instances", nil)
if err != nil {
log.Printf("failed to build authorize service instance access request: %s", err)
return nil
}
req.Header.Set("Authorization", token)
start = time.Now()
resp, err = c.client.Do(req)
c.storeListServiceInstancesLatency(float64(time.Since(start)))
if err != nil {
log.Printf("External CAPI request failed: %s", err)
return nil
}
defer func(r *http.Response) {
cleanup(r)
}(resp)
if resp.StatusCode != http.StatusOK {
log.Printf("CAPI request failed (/v2/service_instances): %d", resp.StatusCode)
return nil
}
var serviceSources struct {
Resources []struct {
Metadata struct {
Guid string `json:"guid"`
} `json:"metadata"`
} `json:"resources"`
}
json.NewDecoder(resp.Body).Decode(&serviceSources)
for _, v := range serviceSources.Resources {
sourceIDs = append(sourceIDs, v.Metadata.Guid)
}
return sourceIDs
}
func cleanup(resp *http.Response) {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}