This repository has been archived by the owner on Aug 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
auth.go
67 lines (62 loc) · 1.55 KB
/
auth.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
package testing
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
. "github.com/onsi/gomega"
)
func NewServerRequest(method, uri string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, uri, body)
if err != nil {
return nil, err
}
req.Host = req.URL.Host
// set req.TLS if request is https
if req.URL.Scheme == "https" {
req.TLS = &tls.ConnectionState{}
}
// zero out fields that are not available to the server
req.URL = &url.URL{
Path: req.URL.Path,
RawQuery: req.URL.RawQuery,
}
return req, nil
}
func BuildRequest(method, url, remoteAddr, requestId, forwardedFor string) *http.Request {
req, err := NewServerRequest(method, url, nil)
Expect(err).ToNot(HaveOccurred())
req.Header.Add("X-Vcap-Request-ID", requestId)
req.Header.Add("X-Forwarded-For", forwardedFor)
req.RemoteAddr = remoteAddr
return req
}
func BuildExpectedLog(timestamp time.Time, requestId, method, path, sourceHost, sourcePort, dstHost, dstPort string) string {
extensions := []string{
fmt.Sprintf("rt=%d", timestamp.UnixNano()/int64(time.Millisecond)),
"cs1Label=userAuthenticationMechanism",
"cs1=oauth-access-token",
"cs2Label=vcapRequestId",
"cs2=" + requestId,
"request=" + path,
"requestMethod=" + method,
"src=" + sourceHost,
"spt=" + sourcePort,
"dst=" + dstHost,
"dpt=" + dstPort,
}
fields := []string{
"0",
"cloud_foundry",
"log_cache",
"1.0",
method + " " + path,
method + " " + path,
"0",
strings.Join(extensions, " "),
}
return "CEF:" + strings.Join(fields, "|")
}