/
kms.go
159 lines (119 loc) · 2.97 KB
/
kms.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
//
// gokms - Go packages to interact with Amazon KMS (Key Management Service)
//
//
// Written by Kiko Hsieh <kiko_hsieh@trend.com.tw>
//
package kms
import (
"bytes"
"encoding/json"
"fmt"
"github.com/AdRoll/goamz/aws"
"io/ioutil"
"net/http"
"time"
)
const (
httpMethod = "POST"
contentType = "application/x-amz-json-1.1"
targetPrefix = "TrentService."
serverName = "kms"
)
type KMS struct {
aws.Auth
aws.Region
}
func New(auth aws.Auth, region aws.Region) *KMS {
return &KMS{auth, region}
}
func (k *KMS) query(requstInfo KMSAction) ([]byte, error) {
b, err := json.Marshal(requstInfo)
if err != nil {
return nil, err
}
hreq, err := http.NewRequest(httpMethod, k.Region.KMSEndpoint+"/", bytes.NewBuffer(b))
if err != nil {
return nil, err
}
hreq.Header.Set("Content-Type", contentType)
hreq.Header.Set("X-Amz-Date", time.Now().UTC().Format(aws.ISO8601BasicFormat))
hreq.Header.Set("X-Amz-Target", targetPrefix+requstInfo.ActionName())
if k.Auth.Token() != "" {
hreq.Header.Set("X-Amz-Security-Token", k.Auth.Token())
}
//All KMS operations require Signature Version 4
//http://docs.aws.amazon.com/kms/latest/APIReference/Welcome.html
signer := aws.NewV4Signer(k.Auth, serverName, k.Region)
signer.Sign(hreq)
r, err := http.DefaultClient.Do(hreq)
if err != nil {
return nil, err
}
body, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if r.StatusCode != 200 {
return nil, buildError(body, r.StatusCode)
}
return body, err
}
type KMSError struct {
StatusCode int `json:",omitempty"`
Type string `json:"__type"`
Message string `json:"message"`
}
func (k *KMSError) Error() string {
return fmt.Sprintf("Type: %s, Code: %d, Message: %s",
k.Type, k.StatusCode, k.Message,
)
}
func buildError(body []byte, statusCode int) error {
err := KMSError{StatusCode: statusCode}
json.Unmarshal(body, &err)
return &err
}
// ================== Action ========================
func (k *KMS) DescribeKey(info DescribeKeyInfo) (DescribeKeyResp, error) {
resp := DescribeKeyResp{}
bResp, err := k.query(&info)
if err != nil {
return resp, err
}
err = json.Unmarshal(bResp, &resp)
return resp, err
}
func (k *KMS) ListAliases(info ListAliasesInfo) (ListAliasesResp, error) {
resp := ListAliasesResp{}
bResp, err := k.query(&info)
if err != nil {
return resp, err
}
err = json.Unmarshal(bResp, &resp)
return resp, err
}
func (k *KMS) Encrypt(info EncryptInfo) (EncryptResp, error) {
resp := EncryptResp{}
bResp, err := k.query(&info)
if err != nil {
return resp, err
}
err = json.Unmarshal(bResp, &resp)
return resp, err
}
func (k *KMS) Decrypt(info DecryptInfo) (DecryptResp, error) {
resp := DecryptResp{}
bResp, err := k.query(&info)
if err != nil {
return resp, err
}
err = json.Unmarshal(bResp, &resp)
return resp, err
}
func (k *KMS) EnableKey(info EnableKeyInfo) error {
_, err := k.query(&info)
return err
}
func (k *KMS) DisableKey(info DisableKeyInfo) error {
_, err := k.query(&info)
return err
}