-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
207 lines (163 loc) · 4.93 KB
/
main.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
package main
import (
"bytes"
"context"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"os"
"crypto/rsa"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"time"
"github.com/golang-jwt/jwt/v4"
"strings"
)
var KEY_FILE string = os.Getenv("KEY_FILE")
var KEY_ID string = os.Getenv("KEY_ID")
var SERVICE_ACCOUNT_ID string = os.Getenv("SERVICE_ACCOUNT_ID")
var CLOUD_ID string = os.Getenv("CLOUD_ID")
var API_MAIN_DOMAIN string = "api.cloud.yandex.net/"
var DNS_ZONE_ID string = os.Getenv("DNS_ZONE_ID")
var DNS_RECORD_NAME string = os.Getenv("DNS_RECORD_NAME")
var DNS_RECORD_TYPE string = getEnv("DNS_RECORD_TYPE", "TXT")
var DNS_RECORD_TTL string = getEnv("DNS_RECORD_TTL", "60")
var DNS_RECORD_DATA string = os.Getenv("CERTBOT_VALIDATION")
func getEnv(key string, defaultVal string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return defaultVal
}
func checkEnvVars() error {
if KEY_ID == "" {
return errors.New("variable KEY_ID not set or empty")
}
if SERVICE_ACCOUNT_ID == "" {
return errors.New("variable SERVICE_ACCOUNT_ID not set or empty")
}
if CLOUD_ID == "" {
return errors.New("variable CLOUD_ID not set or empty")
}
if DNS_RECORD_DATA == "" {
return errors.New("variable CERTBOT_VALIDATION not set or empty")
}
return nil
}
func checkError(err error) {
if err != nil {
log.Fatalln(err)
}
}
func makeHttpRequest(requestType, requestUrl string, requestPayload io.Reader, iamToken string) []byte {
client := &http.Client{}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, requestType, requestUrl, requestPayload)
checkError(err)
req.Header.Add("Authorization", "Bearer "+iamToken)
resp, err := client.Do(req)
checkError(err)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
checkError(err)
return body
}
func signedToken() string {
claims := jwt.RegisteredClaims{
Issuer: SERVICE_ACCOUNT_ID,
ExpiresAt: jwt.NewNumericDate(time.Now().Add(1 * time.Hour)),
IssuedAt: jwt.NewNumericDate(time.Now()),
NotBefore: jwt.NewNumericDate(time.Now()),
Audience: []string{"https://iam.api.cloud.yandex.net/iam/v1/tokens"},
}
token := jwt.NewWithClaims(jwt.SigningMethodPS256, claims)
token.Header["kid"] = KEY_ID
privateKey := loadPrivateKey()
signed, err := token.SignedString(privateKey)
if err != nil {
panic(err)
}
return signed
}
func loadPrivateKey() *rsa.PrivateKey {
data, err := ioutil.ReadFile(KEY_FILE)
if err != nil {
panic(err)
}
rsaPrivateKey, err := jwt.ParseRSAPrivateKeyFromPEM(data)
if err != nil {
panic(err)
}
return rsaPrivateKey
}
func getIAMToken() string {
jot := signedToken()
resp, err := http.Post(
"https://iam.api.cloud.yandex.net/iam/v1/tokens",
"application/json",
strings.NewReader(fmt.Sprintf(`{"jwt":"%s"}`, jot)),
)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
panic(fmt.Sprintf("%s: %s", resp.Status, body))
}
var data struct {
IAMToken string `json:"iamToken"`
}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
panic(err)
}
return data.IAMToken
}
func createDnsRecordInYandex(zoneId string, dnsRecordName string, dnsRecodType string, dnsRecordTtl string, dnsRecordData []string, iamToken string) error {
type Replacements struct {
Name string `json:"name"`
Type string `json:"type"`
TTL string `json:"ttl"`
Data []string `json:"data"`
}
type Payload struct {
Replacements []Replacements `json:"replacements"`
}
type YandexResponseRecordAction struct {
Done bool `json:"done"`
}
replacements := Replacements{dnsRecordName, dnsRecodType, dnsRecordTtl, dnsRecordData}
replacementsArray := []Replacements{replacements}
payload := Payload{replacementsArray}
payloadBytes, err := json.Marshal(payload)
checkError(err)
var yandexResponse = YandexResponseRecordAction{}
var apiEndpoint string = "https://dns." + API_MAIN_DOMAIN + "dns/v1/zones/"
var body = makeHttpRequest("POST", apiEndpoint+zoneId+":upsertRecordSets", bytes.NewBuffer(payloadBytes), iamToken)
err = json.Unmarshal(body, &yandexResponse)
checkError(err)
if !yandexResponse.Done {
log.Println(string(body))
return errors.New("Record " + dnsRecordName + dnsRecodType + dnsRecordTtl + strings.Join(dnsRecordData, " ") + " in dns zone " + zoneId + " not created. Or bad created")
}
return nil
}
func main() {
// Check env variabels
var err error = checkEnvVars()
checkError(err)
// Configure logger to JSON format
log.SetFormatter(&log.JSONFormatter{})
// Get IAM token via service account private key
iamToken := getIAMToken()
DNS_RECORD_DATA := []string{"\"" + DNS_RECORD_DATA + "\""}
// Try create DNS record
err = createDnsRecordInYandex(DNS_ZONE_ID, DNS_RECORD_NAME, DNS_RECORD_TYPE, DNS_RECORD_TTL, DNS_RECORD_DATA, iamToken)
checkError(err)
// Sleep for renewing DNS record for certbot
time.Sleep(time.Second * 15)
}