-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmail-tester.go
166 lines (137 loc) · 4.04 KB
/
mail-tester.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
package notmain
import (
"context"
"crypto/x509"
"flag"
"fmt"
netmail "net/mail"
"os"
"time"
"github.com/letsencrypt/boulder/bdns"
"github.com/letsencrypt/boulder/cmd"
bconfig "github.com/letsencrypt/boulder/config"
"github.com/letsencrypt/boulder/features"
bmail "github.com/letsencrypt/boulder/mail"
)
const usageString = `
usage:
mail-tester --config <path> <recipient>
args:
config File path to the configuration file for this service
recipient Email address to send an email to
`
type config struct {
Mailer struct {
DebugAddr string
DB cmd.DBConfig
cmd.SMTPConfig
From string
Subject string
CertLimit int
NagTimes []string
// How much earlier (than configured nag intervals) to
// send reminders, to account for the expected delay
// before the next expiration-mailer invocation.
NagCheckInterval string
// Path to a text/template email template
EmailTemplate string
Frequency bconfig.Duration
TLS cmd.TLSConfig
SAService *cmd.GRPCClientConfig
DNSTries int
DNSStaticResolvers []string
DNSTimeout string
DNSAllowLoopbackAddresses bool
// Path to a file containing a list of trusted root certificates for use
// during the SMTP connection (as opposed to the gRPC connections).
SMTPTrustedRootFile string
Features features.Config
}
Syslog cmd.SyslogConfig
OpenTelemetry cmd.OpenTelemetryConfig
}
func main() {
usage := func() {
fmt.Fprintf(os.Stderr, usageString)
os.Exit(1)
}
configFile := flag.String("config", "", "File path to the configuration file for this service")
flag.Parse()
if len(os.Args) <= 3 || *configFile == "" {
usage()
}
args := flag.Args()
recipient := args[0]
var c config
err := cmd.ReadConfigFile(*configFile, &c)
cmd.FailOnError(err, "Reading JSON config file into config structure")
features.Set(c.Mailer.Features)
scope, logger, oTelShutdown := cmd.StatsAndLogging(c.Syslog, c.OpenTelemetry, c.Mailer.DebugAddr)
defer oTelShutdown(context.Background())
logger.Info(cmd.VersionString())
tlsConfig, err := c.Mailer.TLS.Load(scope)
cmd.FailOnError(err, "TLS config")
clk := cmd.Clock()
dnsTimeout, err := time.ParseDuration(c.Mailer.DNSTimeout)
cmd.FailOnError(err, "Couldn't parse DNS timeout")
dnsTries := c.Mailer.DNSTries
if dnsTries < 1 {
dnsTries = 1
}
var resolver bdns.Client
servers, err := bdns.NewStaticProvider(c.Mailer.DNSStaticResolvers)
cmd.FailOnError(err, "Couldn't start static DNS server resolver")
if !c.Mailer.DNSAllowLoopbackAddresses {
r := bdns.New(
dnsTimeout,
servers,
scope,
clk,
dnsTries,
logger,
tlsConfig)
resolver = r
} else {
r := bdns.NewTest(dnsTimeout, servers, scope, clk, dnsTries, logger, tlsConfig)
resolver = r
}
var smtpRoots *x509.CertPool
smtpSkipVerify := false
if c.Mailer.SMTPTrustedRootFile == "InsecureSkipVerify" {
smtpSkipVerify = true
} else if c.Mailer.SMTPTrustedRootFile != "" {
pem, err := os.ReadFile(c.Mailer.SMTPTrustedRootFile)
cmd.FailOnError(err, "Loading trusted roots file")
smtpRoots = x509.NewCertPool()
if !smtpRoots.AppendCertsFromPEM(pem) {
cmd.FailOnError(nil, "Failed to parse root certs PEM")
}
}
fromAddress, err := netmail.ParseAddress(c.Mailer.From)
cmd.FailOnError(err, fmt.Sprintf("Could not parse from address: %s", c.Mailer.From))
smtpPassword, err := c.Mailer.PasswordConfig.Pass()
cmd.FailOnError(err, "Failed to load SMTP password")
mailClient := bmail.New(
c.Mailer.Server,
c.Mailer.Port,
c.Mailer.Username,
smtpPassword,
smtpRoots,
smtpSkipVerify,
resolver,
*fromAddress,
logger,
scope,
1*time.Second,
5*60*time.Second)
conn, err := mailClient.Connect()
cmd.FailOnError(err, "mail-tester failed to connect")
defer conn.Close()
recipients := []string{}
recipients = append(recipients, recipient)
err = conn.SendMail(recipients, "Test Email from LabCA", "Test sending email from the LabCA server")
cmd.FailOnError(err, "mail-tester has failed")
}
func init() {
cmd.RegisterCommand("mail-tester", main, &cmd.ConfigValidator{Config: &config{}})
}