/
route53.go
80 lines (69 loc) · 2.34 KB
/
route53.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
// http://docs.aws.amazon.com/AWSMechTurk/latest/AWSMechanicalTurkRequester/MakingRequests_RequestAuthenticationArticle.html
// 1 The sender constructs a request to AWS.
// 2 The sender calculates a Keyed-Hashing for Message Authentication code (HMAC), the request signature using the his or her Secret Access Key and the values of the Service, Operation, and Timestamp parameters as input.
// 3 The sender of the request sends the request data, the signature, and Access Key ID (the key-identifier of the Secret Access Key used) to AWS.
// 4 AWS uses the Access Key ID to look up the Secret Access Key.
// 5 AWS generates a signature from the request data and the Secret Access Key using the same algorithm used to calculate the signature in the request.
// 6 If the signature generated by AWS matches the one sent in the request, the request is considered to be authentic. If the comparison fails, the request is discarded, and AWS returns an error response.
package route53
import (
"bytes"
"encoding/xml"
"io/ioutil"
"net/http"
)
const awsURL = `https://route53.amazonaws.com/2012-12-12/hostedzone`
func generateZones(data []byte) HostedZones {
zones := HostedZones{}
xml.Unmarshal(data, &zones)
return zones
}
func generateResourceRecordSet(data []byte) ResourceRecordSets {
recordSets := ResourceRecordSets{}
xml.Unmarshal(data, &recordSets)
return recordSets
}
func remoteTime(url string) (time string, err error) {
headers, err := getHeaders(url)
if err != nil {
return time, err
}
time = headers.Get("Date")
return
}
func getHeaders(url string) (http.Header, error) {
resp, err := http.Head(url)
if err != nil {
return nil, err
}
return resp.Header, nil
}
func post(url string, postData string, headers http.Header) (*http.Response, error) {
req, err := http.NewRequest("POST", url, bytes.NewReader([]byte(postData)))
if err != nil {
return nil, err
}
req.Header = headers
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, err
}
return res, err
}
func getBody(url string, headers http.Header) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err == nil {
req.Header = headers
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err == nil {
return ioutil.ReadAll(resp.Body)
}
}
return nil, err
}