|
1 | | -// Package exec implements a manual DNS provider which runs a program for |
2 | | -// adding/removing the DNS record. |
3 | | -// |
4 | | -// The file name of the external program is specified in the environment |
5 | | -// variable EXEC_PATH. When it is run by lego, three command-line parameters |
6 | | -// are passed to it: The action ("present" or "cleanup"), the fully-qualified domain |
7 | | -// name, the value for the record and the TTL. |
8 | | -// |
9 | | -// For example, requesting a certificate for the domain 'foo.example.com' can |
10 | | -// be achieved by calling lego as follows: |
11 | | -// |
12 | | -// EXEC_PATH=./update-dns.sh \ |
13 | | -// lego --dns exec \ |
14 | | -// --domains foo.example.com \ |
15 | | - |
16 | | -// |
17 | | -// It will then call the program './update-dns.sh' with like this: |
18 | | -// |
19 | | -// ./update-dns.sh "present" "_acme-challenge.foo.example.com." "MsijOYZxqyjGnFGwhjrhfg-Xgbl5r68WPda0J9EgqqI" "120" |
20 | | -// |
21 | | -// The program then needs to make sure the record is inserted. When it returns |
22 | | -// an error via a non-zero exit code, lego aborts. |
23 | | -// |
24 | | -// When the record is to be removed again, the program is called with the first |
25 | | -// command-line parameter set to "cleanup" instead of "present". |
26 | 1 | package exec |
27 | 2 |
|
28 | 3 | import ( |
29 | 4 | "errors" |
| 5 | + "fmt" |
30 | 6 | "os" |
31 | 7 | "os/exec" |
32 | 8 | "strconv" |
33 | 9 |
|
34 | 10 | "github.com/xenolf/lego/acme" |
| 11 | + "github.com/xenolf/lego/log" |
| 12 | + "github.com/xenolf/lego/platform/config/env" |
35 | 13 | ) |
36 | 14 |
|
| 15 | +// Config Provider configuration. |
| 16 | +type Config struct { |
| 17 | + Program string |
| 18 | + Mode string |
| 19 | +} |
| 20 | + |
37 | 21 | // DNSProvider adds and removes the record for the DNS challenge by calling a |
38 | 22 | // program with command-line parameters. |
39 | 23 | type DNSProvider struct { |
40 | | - program string |
| 24 | + config *Config |
41 | 25 | } |
42 | 26 |
|
43 | 27 | // NewDNSProvider returns a new DNS provider which runs the program in the |
44 | 28 | // environment variable EXEC_PATH for adding and removing the DNS record. |
45 | 29 | func NewDNSProvider() (*DNSProvider, error) { |
46 | | - s := os.Getenv("EXEC_PATH") |
47 | | - if s == "" { |
48 | | - return nil, errors.New("environment variable EXEC_PATH not set") |
| 30 | + values, err := env.Get("EXEC_PATH") |
| 31 | + if err != nil { |
| 32 | + return nil, fmt.Errorf("exec: %v", err) |
49 | 33 | } |
50 | 34 |
|
51 | | - return NewDNSProviderProgram(s) |
| 35 | + return NewDNSProviderConfig(&Config{ |
| 36 | + Program: values["EXEC_PATH"], |
| 37 | + Mode: os.Getenv("EXEC_MODE"), |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +// NewDNSProviderConfig returns a new DNS provider which runs the given configuration |
| 42 | +// for adding and removing the DNS record. |
| 43 | +func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { |
| 44 | + if config == nil { |
| 45 | + return nil, errors.New("the configuration is nil") |
| 46 | + } |
| 47 | + |
| 48 | + return &DNSProvider{config: config}, nil |
52 | 49 | } |
53 | 50 |
|
54 | 51 | // NewDNSProviderProgram returns a new DNS provider which runs the given program |
55 | 52 | // for adding and removing the DNS record. |
| 53 | +// Deprecated: use NewDNSProviderConfig instead |
56 | 54 | func NewDNSProviderProgram(program string) (*DNSProvider, error) { |
57 | | - return &DNSProvider{program: program}, nil |
| 55 | + if len(program) == 0 { |
| 56 | + return nil, errors.New("the program is undefined") |
| 57 | + } |
| 58 | + |
| 59 | + return NewDNSProviderConfig(&Config{Program: program}) |
58 | 60 | } |
59 | 61 |
|
60 | 62 | // Present creates a TXT record to fulfil the dns-01 challenge. |
61 | 63 | func (d *DNSProvider) Present(domain, token, keyAuth string) error { |
62 | | - fqdn, value, ttl := acme.DNS01Record(domain, keyAuth) |
63 | | - cmd := exec.Command(d.program, "present", fqdn, value, strconv.Itoa(ttl)) |
64 | | - cmd.Stdout = os.Stdout |
65 | | - cmd.Stderr = os.Stderr |
| 64 | + var args []string |
| 65 | + if d.config.Mode == "RAW" { |
| 66 | + args = []string{"present", "--", domain, token, keyAuth} |
| 67 | + } else { |
| 68 | + fqdn, value, ttl := acme.DNS01Record(domain, keyAuth) |
| 69 | + args = []string{"present", fqdn, value, strconv.Itoa(ttl)} |
| 70 | + } |
| 71 | + |
| 72 | + cmd := exec.Command(d.config.Program, args...) |
| 73 | + |
| 74 | + output, err := cmd.CombinedOutput() |
| 75 | + if len(output) > 0 { |
| 76 | + log.Println(string(output)) |
| 77 | + } |
66 | 78 |
|
67 | | - return cmd.Run() |
| 79 | + return err |
68 | 80 | } |
69 | 81 |
|
70 | 82 | // CleanUp removes the TXT record matching the specified parameters |
71 | 83 | func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { |
72 | | - fqdn, value, ttl := acme.DNS01Record(domain, keyAuth) |
73 | | - cmd := exec.Command(d.program, "cleanup", fqdn, value, strconv.Itoa(ttl)) |
74 | | - cmd.Stdout = os.Stdout |
75 | | - cmd.Stderr = os.Stderr |
| 84 | + var args []string |
| 85 | + if d.config.Mode == "RAW" { |
| 86 | + args = []string{"cleanup", "--", domain, token, keyAuth} |
| 87 | + } else { |
| 88 | + fqdn, value, ttl := acme.DNS01Record(domain, keyAuth) |
| 89 | + args = []string{"cleanup", fqdn, value, strconv.Itoa(ttl)} |
| 90 | + } |
| 91 | + |
| 92 | + cmd := exec.Command(d.config.Program, args...) |
| 93 | + |
| 94 | + output, err := cmd.CombinedOutput() |
| 95 | + if len(output) > 0 { |
| 96 | + log.Println(string(output)) |
| 97 | + } |
76 | 98 |
|
77 | | - return cmd.Run() |
| 99 | + return err |
78 | 100 | } |
0 commit comments