Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Health Monitor email messages and TLS #2513

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/bosh-monitor/lib/bosh/monitor/plugins/email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ def send_email_async(subject, body, date = Time.now)
end

Async do
smtp = Net::SMTP.new(smtp_options['host'], smtp_options['port'])
smtp.enable_starttls if smtp_options['tls']

starttls_mode = smtp_options['tls'] ? :always : false
smtp = Net::SMTP.new(smtp_options['host'], smtp_options['port'], starttls: starttls_mode)
smtp.start(*smtp_start_params) do |smtp|
smtp.send_message(formatted_message(headers, body), smtp_options['from'], recipients)
logger.debug("Email sent (took #{Time.now - started} seconds)")
Expand All @@ -128,9 +127,9 @@ def create_headers(subject, date)
end

def formatted_message(headers_hash, body_text)
headers_text = headers_hash.map { |key, value| "#{key}: #{value}" }.join('\n')
headers_text = headers_hash.map { |key, value| "#{key}: #{value}" }.join("\r\n")

"#{headers_text}\n\n#{body_text}"
"#{headers_text}\r\n\r\n#{body_text}"
end
end
end
Expand Down
25 changes: 23 additions & 2 deletions src/bosh-monitor/spec/unit/bosh/monitor/plugins/email_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}

@options = {
'recipients' => ['dude@vmware.com', 'dude2@vmware.com'],
'recipients' => ['recipient@example.com', 'recipient2@example.com'],
'smtp' => @smtp_options,
'interval' => 0.1,
}
Expand Down Expand Up @@ -51,7 +51,7 @@
end

it 'has a list of recipients and smtp options' do
expect(@plugin.recipients).to eq(['dude@vmware.com', 'dude2@vmware.com'])
expect(@plugin.recipients).to eq(['recipient@example.com', 'recipient2@example.com'])
expect(@plugin.smtp_options).to eq(@smtp_options)
end

Expand Down Expand Up @@ -117,11 +117,32 @@
expect(@plugin.queue_size(:heartbeat)).to eq(0)
end

it 'correctly formats the email message' do
date = Time.utc(2024, 1, 2, 3, 4, 5)
headers = @plugin.create_headers('Some subject', date)
message = @plugin.formatted_message(headers, "This is the body text")


expected_message = "From: hm@example.com\r\nTo: recipient@example.com, recipient2@example.com\r\nSubject: Some subject\r\nDate: Tue, 2 Jan 2024 03:04:05 +0000\r\nContent-Type: text/plain; charset=\"iso-8859-1\"\r\n\r\nThis is the body text"

expect(message).to eq(expected_message)
end

it 'writes datetime headers compliant with rfc5322' do
headers = @plugin.create_headers('Some subject', Time.now)
expect(headers).to be_truthy
date_value = headers['Date']
expect(date_value).to be_truthy
expect(/[[:alpha:]]{3}, \d{1,2} [[:alpha:]]{3} \d{4} \d{2}:\d{2}:\d{2} [\+,\-].+/).to match(date_value)
end

context 'Net::SMTP canary' do
# Currently the Health Monitor email plugin does not support direct TLS connections, only
# STARTTLS commands during an existing SMTP session. We are relying on TLS being false by
# default in the Net::SMTP class.
it 'defaults tls to false' do
smtp = Net::SMTP.new('example.com', 25)
expect(smtp.tls?).to eq(false)
end
end
end