-
Notifications
You must be signed in to change notification settings - Fork 1
/
certbot-govh.go
176 lines (144 loc) · 4.38 KB
/
certbot-govh.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
package main
import (
"errors"
"fmt"
"log"
"os"
"strings"
"github.com/ovh/go-ovh/ovh"
"golang.org/x/net/publicsuffix"
"gopkg.in/ini.v1"
)
type DnsEntry struct {
FieldType string `json:"fieldType"`
SubDomain string `json:"subDomain"`
Target string `json:"target"`
Ttl int `json:"ttl"`
}
type DnsReturn struct {
Id int `json:"id"`
}
type OvhCredential struct {
Endpoint string
ApplicationKey string
ApplicationSecret string
ConsumerKey string
}
func checkOvhCredential(ovhCredential *OvhCredential, domain string, primaryDomain string) error {
homeDirname, err := os.UserHomeDir()
if err != nil {
return err
}
cfg, err := ini.Load(homeDirname + "/.ovh/" + primaryDomain + ".ini")
if err != nil {
return err
}
ovhCredential.Endpoint = cfg.Section("default").Key("endpoint").String()
ovhCredential.ApplicationKey = cfg.Section(ovhCredential.Endpoint).Key("application_key").String()
ovhCredential.ApplicationSecret = cfg.Section(ovhCredential.Endpoint).Key("application_secret").String()
ovhCredential.ConsumerKey = cfg.Section(ovhCredential.Endpoint).Key("consumer_key").String()
if ovhCredential.Endpoint == "" {
return errors.New("endpoint is not defined")
}
if ovhCredential.ApplicationKey == "" {
return errors.New("ApplicationKey is not defined")
}
if ovhCredential.ApplicationSecret == "" {
return errors.New("ApplicationSecret is not defined")
}
if ovhCredential.ConsumerKey == "" {
return errors.New("ConsumerKey is not defined")
}
return nil
}
// Create the validation DNS entry
func addTokenToDns(client *ovh.Client, domain string, primaryDomain string) {
validation := os.Getenv("CERTBOT_VALIDATION")
dnsEntry := &DnsEntry{FieldType: "TXT", SubDomain: "_acme-challenge." + domain + ".", Target: validation, Ttl: 60}
var dnsReturn DnsReturn
// Create the entry
err := client.Post("/domain/zone/"+primaryDomain+"/record", dnsEntry, &dnsReturn)
if err != nil {
log.Fatalf("Error: %q\n", err)
}
// Refresh DNS Zone
err = client.Post("/domain/zone/"+primaryDomain+"/refresh", nil, nil)
if err != nil {
log.Fatalf("Error: %q\n", err)
}
// Print the zone ID : certbot set this value via CERTBOT_AUTH_OUTPUT environment variable
fmt.Printf("%d", dnsReturn.Id)
}
// Remove the created entry from DNS Zone
func removeTokenToDns(client *ovh.Client, domain string, primaryDomain string) {
zoneId := strings.TrimSpace(os.Getenv("CERTBOT_AUTH_OUTPUT"))
if zoneId == "" {
log.Fatal("No zone ID in CERTBOT_AUTH_OUTPUT")
}
// Delete the DNS entry
err := client.Delete("/domain/zone/"+primaryDomain+"/record/"+zoneId, nil)
if err != nil {
log.Fatalf("Error: %q\n", err)
}
// Refresh DNS Zone
err = client.Post("/domain/zone/"+primaryDomain+"/refresh", nil, nil)
if err != nil {
log.Fatalf("Error: %q\n", err)
}
}
func help(optionalExitCode ...int) {
fmt.Printf("%s\n", os.Args[0])
exitCode := 0
if len(optionalExitCode) > 0 {
exitCode = optionalExitCode[0]
}
os.Exit(exitCode)
}
func main() {
if len(os.Args) != 2 {
log.Fatalf("This program require one parameter exactly")
}
action := os.Args[1]
if action == "help" || action == "-h" || action == "--help" {
help()
}
/*
CERTBOT_DOMAIN: The domain being authenticated
CERTBOT_VALIDATION: The validation string
CERTBOT_TOKEN: Resource name part of the HTTP-01 challenge (HTTP-01 only)
CERTBOT_REMAINING_CHALLENGES: Number of challenges remaining after the current challenge
CERTBOT_ALL_DOMAINS: A comma-separated list of all domains challenged for the current certificate
*/
// Get the domain and primary domain from certbot
domain := os.Getenv("CERTBOT_DOMAIN")
primaryDomain, err := publicsuffix.EffectiveTLDPlusOne(domain)
if err != nil {
log.Fatalf("%s : no primary domain found", domain)
}
// Get OVH credential for primary domain
var ovhCredential OvhCredential
err = checkOvhCredential(&ovhCredential, domain, primaryDomain)
if err != nil {
log.Fatalf("Error: %q\n", err)
}
// Init the OVH HTTP Client
client, err := ovh.NewClient(
ovhCredential.Endpoint,
ovhCredential.ApplicationKey,
ovhCredential.ApplicationSecret,
ovhCredential.ConsumerKey,
)
if err != nil {
log.Fatalf("Error: %q\n", err)
}
// Process action
switch action {
case "create":
addTokenToDns(client, domain, primaryDomain)
case "delete":
removeTokenToDns(client, domain, primaryDomain)
default:
fmt.Print("Action is not found\n")
help(1)
}
}