-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrequest.go
executable file
·159 lines (135 loc) · 4.28 KB
/
request.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
package main
import (
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)
// The below functions fetch data from the URL
func fetch(method string, url string, headers map[string]string) ([]byte, error) {
var contents []byte
// Create new http request
request, err := http.NewRequest(method, url, nil)
if err != nil {
return contents, fmt.Errorf("encountered error when sending new request: %v", err)
}
// copy headers
if headers != nil {
for k, v := range headers {
request.Header.Set(k, v)
}
}
var tlsConfig *tls.Config
if cmdOptions.SkipSsl { // Skip SSL stuffs
tlsConfig = &tls.Config{
InsecureSkipVerify: true,
}
} else { // add CA if provided
cert, err := ioutil.ReadFile(cmdOptions.CACertFile)
if err != nil {
Fatalf("Couldn't load certificate file: %v", err)
}
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(cert)
tlsConfig = &tls.Config{
RootCAs: certPool,
}
}
transport := &http.Transport{TLSClientConfig: tlsConfig}
// perform request
client := &http.Client{
Transport: transport,
Timeout: 30 * time.Second,
}
resp, err := client.Do(request)
if err != nil {
return contents, fmt.Errorf("encountered error when requesting the data from http: %v", err)
}
// read response
defer resp.Body.Close()
contents, err = ioutil.ReadAll(resp.Body)
if err != nil {
return contents, fmt.Errorf("encountered error when reading the data from http: %v", err)
}
// Check if the response is 200
if resp.StatusCode != http.StatusOK {
return contents, fmt.Errorf("encountered invalid status code from http: %v", resp.StatusCode)
}
return contents, nil
}
// the below function does the get from the URL
func get(url string, headers map[string]string) ([]byte, error) {
return fetch("GET", url, headers)
}
// the below function does the POST from the URL
func post(url string, headers map[string]string) ([]byte, error) {
return fetch("POST", url, headers)
}
// Extract the access token from ops man
func (m *MetricsCollector) authenticate() error {
Debugf("Extracting the access token for the ops manager: %s", cmdOptions.OpsManHostname)
// Parse the authentication URL
URL, err := url.Parse("https://" + cmdOptions.OpsManHostname + "/uaa/oauth/token")
if err != nil {
return IncrementErrorCounter(fmt.Sprintf("error in parsing authentication url: %v", err))
}
// parameters
parameters := url.Values{}
headers := make(map[string]string)
headers["Accept"] = "application/json;charset=utf-8"
headers["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8"
// Add Parameter / header values for URL for authentication
if isClientIDBeingUsed() {
parameters.Add("grant_type", "client_credentials")
headers["Authorization"] = "Basic " + basicAuth()
} else {
parameters.Add("grant_type", "password")
parameters.Add("username", cmdOptions.OpsManUsername)
parameters.Add("password", cmdOptions.OpsManPassword)
headers["Authorization"] = "Basic b3BzbWFuOg=="
}
URL.RawQuery = parameters.Encode()
// Fetch the authentication data
var content []byte
if isClientIDBeingUsed() {
content, err = post(URL.String(), headers)
} else {
content, err = get(URL.String(), headers)
}
if err != nil {
return IncrementErrorCounter(fmt.Sprintf("error during token extraction: %v", err))
}
// Extract the auth token from the content received
err = json.Unmarshal(content, &m)
if err != nil {
return IncrementErrorCounter(fmt.Sprintf("error in json unmarshal of uaa response: %v", err))
}
return nil
}
// Build the header and send request to the ops manager for the data
func (m *MetricsCollector) opsmanRequestHandler() ([]byte, error) {
Debugf("Extracting the certificate list from the URL: %s", m.OpsManCertificateListUrl)
// Headers.
headers := make(map[string]string)
headers["Accept"] = "application/json;charset=utf-8"
headers["Authorization"] = "Bearer " + m.AccessToken
// Fetch the data
return get(m.OpsManCertificateListUrl, headers)
}
// Is this client id and client secret request
func isClientIDBeingUsed() bool {
if cmdOptions.OpsManUsername == "" {
return true
}
return false
}
// Basic Auth Encoder
func basicAuth() string {
auth := cmdOptions.OpsManClientID + ":" + cmdOptions.OpsManClientSecret
return base64.StdEncoding.EncodeToString([]byte(auth))
}