/
mocks.go
264 lines (220 loc) · 5.84 KB
/
mocks.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package test
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"text/template"
"time"
"github.com/golang-jwt/jwt/v4"
log "github.com/sirupsen/logrus"
)
func MockjwtAPI() (*httptest.Server, string) {
type JwtAPIMock struct {
UAAUrl string
}
var JwtApi JwtAPIMock
mux := http.NewServeMux()
ts := httptest.NewServer(mux)
jku := ts.URL + "/token_keys"
publicpemkey, token := verifyJWTTokenMock(jku)
json := fmt.Sprintf(`{
"kty": "RSA",
"e": "AQAB",
"use": "sig",
"kid": "uaa-jwt-key-1",
"alg": "RS256",
"value": "%s",
"n": "",
}`, publicpemkey)
mux.HandleFunc("/token_keys", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
writer.Write([]byte(json))
})
JwtApi.UAAUrl = ts.URL
return ts, token
}
func MockBoshDirectorAPI(responses map[string]string, url string) *httptest.Server {
json := `{
"name": "bosh-azure-cfn01",
"uuid": "f0ceb485-e188-4b9f-b3d5-fb2067aad3c2",
"version": "273.1.0 (00000000)",
"user": null,
"cpi": "azure_cpi",
"stemcell_os": "-",
"stemcell_version": "1.8",
"user_authentication": {
"type": "uaa",
"options": {
"url": "{{.URL}}",
"urls": [
"{{.URL}}"
]
}
}
}`
type BoshApiMock struct {
URL string
}
var boshapi BoshApiMock
responseTemplate := template.Must(template.New("boshapi").Parse(json))
mux := http.NewServeMux()
mux.HandleFunc("/info", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
err := responseTemplate.Execute(writer, boshapi)
if err != nil {
panic(err)
}
})
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
response, ok := responses[request.URL.Path]
if !ok {
writer.WriteHeader(http.StatusNotFound)
return
}
writer.Write([]byte(response))
})
ts := httptest.NewServer(mux)
boshapi.URL = url
return ts
}
func verifyJWTTokenMock(jku string) (string, string) {
type payload struct {
Scope []string `json:"scope"`
ClientId string `json:"client_id"`
Cid string `json:"cid"`
Azp string `json:"azp"`
GrantType string `json:"grant_type"`
UserId string `json:"user_id"`
Origin string `json:"origin"`
User string `json:"user_name"`
Email string `json:"email"`
AuthTime time.Time `json:"auth_time"`
RevSig string `json:"rev_sig"`
Zid string `json:"zid"`
jwt.RegisteredClaims
}
// Create the claims
claims := payload{
Scope: []string{"openid",
"bosh.admin"},
ClientId: "bosh_cli",
Cid: "bosh_cli",
Azp: "bosh_cli",
GrantType: "password",
UserId: "f62c94ae-2552-411b-9f8e-9ad181c50b40",
Origin: "uaa",
User: "h.example",
Email: "h.example@192.168.1.11:8443",
AuthTime: time.Now(),
RevSig: "14832ae7",
Zid: "uaa",
RegisteredClaims: jwt.RegisteredClaims{
// A usual scenario is to set the expiration time relative to the current time
ExpiresAt: jwt.NewNumericDate(time.Now().Add(5 * time.Minute)),
IssuedAt: jwt.NewNumericDate(time.Now()),
NotBefore: jwt.NewNumericDate(time.Now()),
Issuer: "https://192.168.1.11:8443/oauth/token",
Subject: "f62c94ae-2552-411b-9f8e-9ad181c50b40",
Audience: []string{"openid", "bosh_cli", "bosh"},
},
}
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
publicKey := privateKey.PublicKey
publickeybytes := x509.MarshalPKCS1PublicKey(&publicKey)
block := &pem.Block{
Type: "PUBLIC KEY",
Bytes: publickeybytes,
}
publicPem := pem.EncodeToMemory(block)
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
token.Header["jku"] = jku
token.Header["kid"] = "uaa-jwt-key-1"
ss, err := token.SignedString(privateKey)
fmt.Printf("%v %v", ss, err)
return string(publicPem), ss
}
func MockCfAPI(responses map[string]string) *httptest.Server {
json := `
{
"links": {
"self": {
"href": "{{.BaseURL}}"
},
"cloud_controller_v3": {
"href": "{{.CCV3URL}}",
"meta": {
"version": "3.115.0"
}
},
"uaa": {
"href": "{{.UaaURL}}"
}
}
}`
type CfApiMock struct {
BaseURL, CCV3URL, UaaURL string
}
var cfapi CfApiMock
responseTemplate := template.Must(template.New("cfapi").Parse(json))
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
err := responseTemplate.Execute(writer, cfapi)
if err != nil {
panic(err)
}
})
mux.HandleFunc("/v3/", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "application/json")
response := responses[request.URL.Path]
if response == "" {
writer.WriteHeader(http.StatusNotFound)
return
}
writer.Write([]byte(response))
})
ts := httptest.NewServer(mux)
cfapi = CfApiMock{
BaseURL: ts.URL,
CCV3URL: ts.URL + "/v3",
UaaURL: ts.URL + "/uaa",
}
return ts
}
type MockPcapAgent struct {
*httptest.Server
Host string
Port string
}
func NewMockPcapAgent(responses map[string]string) *MockPcapAgent {
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
response := responses[request.URL.Path+"?"+request.URL.RawQuery]
if response == "" {
writer.WriteHeader(http.StatusNotFound)
return
}
file, err := os.Open(response)
if err != nil {
panic(err)
}
nbytes, err := io.Copy(writer, file)
if err != nil {
panic(err)
}
log.Infof("wrote %s with %d bytes", response, nbytes)
})
mockup := MockPcapAgent{Server: httptest.NewTLSServer(mux)}
pcapAgentUrl, _ := url.Parse(mockup.URL)
mockup.Host = pcapAgentUrl.Hostname()
mockup.Port = pcapAgentUrl.Port()
return &mockup
}