forked from bokysan/docker-postfix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add XOAuth2 support for GMail (bokysan#42)
* Add XOAuth2 support for GMail * Attempt to support integration tests
- Loading branch information
1 parent
4ba3145
commit 16771d4
Showing
14 changed files
with
577 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.idea | ||
helm/fixtures | ||
public | ||
gh-pages | ||
gh-pages | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
version: '3.7' | ||
|
||
volumes: | ||
logs-volume: | ||
|
||
services: | ||
postfix_test_587: | ||
hostname: "postfix" | ||
image: "boky/postfix" | ||
build: | ||
context: ../.. | ||
restart: always | ||
healthcheck: | ||
test: [ "CMD", "sh", "-c", "netstat -an | fgrep 587 | fgrep -q LISTEN" ] | ||
interval: 10s | ||
timeout: 5s | ||
start_period: 10s | ||
retries: 2 | ||
environment: | ||
FORCE_COLOR: "1" | ||
ALLOW_EMPTY_SENDER_DOMAINS: "true" | ||
RELAYHOST: "[smtp.gmail.com]:587" | ||
RELAYHOST_USERNAME: "${RELAYHOST_USERNAME}" | ||
RELAYHOST_TLS_LEVEL: "encrypt" | ||
XOAUTH2_CLIENT_ID: "${XOAUTH2_CLIENT_ID}" | ||
XOAUTH2_SECRET: "${XOAUTH2_SECRET}" | ||
XOAUTH2_INITIAL_ACCESS_TOKEN: "${XOAUTH2_INITIAL_ACCESS_TOKEN}" | ||
XOAUTH2_INITIAL_REFRESH_TOKEN: "${XOAUTH2_INITIAL_REFRESH_TOKEN}" | ||
XOAUTH2_SYSLOG_ON_FAILURE: "no" | ||
XOAUTH2_FULL_TRACE: "no" | ||
LOG_FORMAT: "json" | ||
POSTFIX_maillog_file: "/logs/postfix.log" | ||
POSTFIX_maillog_file_prefixes: "/var,/dev/sdout,/logs" | ||
volumes: | ||
- "logs-volume:/logs" | ||
tests: | ||
image: "boky/postfix-integration-test" | ||
restart: "no" | ||
volumes: | ||
- "../xoauth2/common:/common" | ||
- "./it:/code" | ||
- "logs-volume:/logs" | ||
build: | ||
context: ../tester | ||
command: "/" | ||
environment: | ||
FROM: "${FROM}" | ||
TO: "${TO}" | ||
SKIP_INVALID_DOMAIN_SEND: "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env bats | ||
|
||
if [ -z "$FROM" ]; then | ||
FROM=$1 | ||
shift | ||
fi | ||
|
||
if [ -z "$TO" ]; then | ||
TO=$1 | ||
shift | ||
fi | ||
|
||
# Wait for postfix to startup | ||
wait-for-service -q tcp://postfix_test_587:587 | ||
|
||
SMTP_DATA="-smtp postfix_test_587 -port 587" | ||
|
||
load /common/common-xoauth2.sh | ||
|
||
@test "Relay email with proper XOAuth2 credentials" { | ||
local message_id="12345.test@example.com" | ||
local postfix_message_id='' | ||
local smtp_result='' | ||
local status='' | ||
|
||
mailsend \ | ||
-sub "Test email 1" $SMTP_DATA \ | ||
-from $FROM \ | ||
-to $TO \ | ||
header \ | ||
-name "Message-ID" \ | ||
-value "${message_id}" \ | ||
body \ | ||
-msg "Hello world!\nThis is a simple test message!" | ||
|
||
postfix_message_id=$(get_postfix_message_id '/logs/postfix.log' ${message_id}) | ||
smtp_result=$(get_smtp_result '/logs/postfix.log' "${postfix_message_id}") | ||
status=$(get_param_value "${smtp_result}" 'status') | ||
|
||
[ -n "$status" ] | ||
echo "$status" | grep -q -E "^deferred" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#!/usr/bin/env bash | ||
|
||
get_postfix_message_id() { | ||
local log_file=$1 | ||
local message_id=$2 | ||
local postfix_message_id='' | ||
local result='ko' | ||
local count=0; | ||
local max_wait=15 | ||
while [[ $result == 'ko' ]] && [[ $count -lt $max_wait ]]; do | ||
postfix_message_id=$(cat ${log_file} | grep -E -e 'postfix/cleanup' | cut -d':' -f4- | sed -r -e "s/\s*([^:]+)\s*:\s*message-id=${message_id}/\1/") | ||
if [[ -n "$postfix_message_id" ]]; then | ||
result='ok' | ||
else | ||
sleep 1 | ||
count=$((count+1)) | ||
fi | ||
done | ||
|
||
if [[ $count -eq $max_wait ]]; then | ||
echo >&2 "Message with message-id='${message_id}' not found" | ||
return 1 | ||
fi | ||
|
||
echo ${postfix_message_id} | ||
return 0 | ||
} | ||
|
||
get_smtp_result() { | ||
local log_file=$1 | ||
local postfix_message_id=$2 | ||
local smtp_result='' | ||
local result='ko' | ||
local count=0 | ||
local max_wait=15 | ||
|
||
while [[ $result == 'ko' ]] && [[ $count -lt $max_wait ]]; do | ||
smtp_result=$(cat ${log_file} | grep -E -e 'postfix/smtp\[' | cut -d':' -f4- | sed -r -e "s/${postfix_message_id}:(.*)/\1/g") | ||
if [[ -n "$smtp_result" ]]; then | ||
result='ok' | ||
else | ||
sleep 1 | ||
count=$((count+1)) | ||
fi | ||
done | ||
|
||
if [[ $count -eq $max_wait ]]; then | ||
echo >&2 "Message with postfix id='${postfix_message_id}' not found" | ||
return 1 | ||
fi | ||
|
||
echo ${smtp_result} | ||
return 0 | ||
} | ||
|
||
get_param_value() { | ||
local smtp_result=$1 | ||
local param_name_to_search=$2 | ||
local params='' | ||
local param_name='' | ||
local param_value='' | ||
local status='' | ||
|
||
local old_ifs=$IFS | ||
IFS=',' | ||
read -ra params <<< "${smtp_result}" | ||
IFS=$old_ifs | ||
|
||
for i in "${params[@]}"; do | ||
param_name=$(echo $i | cut -d'=' -f1) | ||
param_value=$(echo $i | cut -d'=' -f2) | ||
if [[ "${param_name}" == "${param_name_to_search}" ]]; then | ||
status="${param_value}" | ||
break; | ||
fi | ||
echo $i; | ||
done | ||
|
||
if [[ -z "${status}" ]]; then | ||
echo "${param_name_to_search} not found!." | ||
return 1 | ||
fi | ||
|
||
echo "${status}" | ||
return 0 | ||
} |
Oops, something went wrong.