Skip to content

Commit 6d62cfd

Browse files
Merge pull request #173 from iExecBlockchainComputing/feature/add-mailgun-check-in-dapp
Add Mailgun API check in dapp
2 parents 9578306 + 647fa59 commit 6d62cfd

File tree

11 files changed

+425
-201
lines changed

11 files changed

+425
-201
lines changed

.drone.yml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,11 +575,20 @@ steps:
575575
- cd dapp
576576
- npm run lint
577577

578-
- name: unit-test
578+
- name: test
579579
image: node:14-alpine3.11
580+
environment:
581+
MJ_APIKEY_PUBLIC:
582+
from_secret: mj_apikey_public
583+
MJ_APIKEY_PRIVATE:
584+
from_secret: mj_apikey_private
585+
MJ_SENDER:
586+
from_secret: mj_sender
587+
MAILGUN_APIKEY:
588+
from_secret: mailgun_apikey
580589
commands:
581590
- cd dapp
582-
- npm run ctest tests/unit/
591+
- npm run ctest
583592

584593
- name: docker-publish-dry-run
585594
# plugin doc https://plugins.drone.io/plugins/docker and repo https://github.com/drone-plugins/drone-docker
@@ -643,6 +652,8 @@ steps:
643652
from_secret: mj_apikey_private
644653
MJ_SENDER:
645654
from_secret: mj_sender
655+
MAILGUN_APIKEY:
656+
from_secret: mailgun_apikey
646657
commands:
647658
- cd dapp
648659
- npm run ctest
@@ -711,6 +722,8 @@ steps:
711722
from_secret: mj_apikey_private
712723
MJ_SENDER:
713724
from_secret: mj_sender
725+
MAILGUN_APIKEY:
726+
from_secret: mailgun_apikey
714727
commands:
715728
- cd dapp
716729
- npm run ctest
@@ -844,6 +857,8 @@ steps:
844857
from_secret: mj_apikey_private
845858
MJ_SENDER:
846859
from_secret: mj_sender
860+
MAILGUN_APIKEY:
861+
from_secret: mailgun_apikey
847862
commands:
848863
- cd deployment-dapp
849864
- npm run push-dapp-secret
@@ -1026,6 +1041,8 @@ steps:
10261041
from_secret: mj_apikey_private
10271042
MJ_SENDER:
10281043
from_secret: mj_sender
1044+
MAILGUN_APIKEY:
1045+
from_secret: mailgun_apikey
10291046
commands:
10301047
- cd deployment-dapp
10311048
- npm run push-dapp-secret

dapp/.env.override

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
IEXEC_IN=/path/to/input/directory
22
IEXEC_OUT=/path/to/output/directory
33
IEXEC_DATASET_FILENAME="protectedData.zip"
4-
IEXEC_APP_DEVELOPER_SECRET='{"MJ_APIKEY_PUBLIC":"dev_mailjet_public_key","MJ_APIKEY_PRIVATE":"dev_mailjet_private_key","MJ_SENDER":"dev_sender_email"}'
5-
IEXEC_REQUESTER_SECRET_1='{"emailSubject":"mail_object_to_send","emailContentMultiAddr":"encrypted_email_content_multiAddr","senderName":"sender_name","emailContentEncryptionKey":"email_content_encryption_key","contentType":"text/plain"}'
4+
IEXEC_APP_DEVELOPER_SECRET='{"MJ_APIKEY_PUBLIC":"dev_mailjet_public_key","MJ_APIKEY_PRIVATE":"dev_mailjet_private_key","MJ_SENDER":"dev_sender_email","MAILGUN_APIKEY":"dev_mailgun_api_key"}'
5+
IEXEC_REQUESTER_SECRET_1='{"emailSubject":"mail_object_to_send","emailContentMultiAddr":"encrypted_email_content_multiAddr","senderName":"sender_name","emailContentEncryptionKey":"email_content_encryption_key","contentType":"text/plain"}'

dapp/local-test.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Set the name of the Docker image
1+
# Set the name of the Docker image
22
IMG_NAME=web3mail:non-tee
33

44
# Build the regular non-TEE image
@@ -14,14 +14,16 @@ if [ ! -d "/tmp/iexec_out" ]; then
1414
fi
1515

1616
# Place your .zip file in the /tmp/iexec_in directory and replace DATA_FILENAME with the name of the file you just placed in the directory
17-
# The .zip file should contain a file with the email content you want to protect
17+
# The .zip file should contain a file with the email content you want to protect
1818
DATA_FILENAME="your_file_name.zip"
1919

2020

2121
# Replace the values of the variables MJ_APIKEY_PUBLIC, MJ_APIKEY_PRIVATE, and MJ_SENDER with your own Mailjet API key and sender email address
2222
MJ_APIKEY_PUBLIC="your_mail_jet_api_key_public"
2323
MJ_APIKEY_PRIVATE="your_mail_jet_api_private"
2424
MJ_SENDER="your_mail_jet_sender"
25+
# Replace the value of the variable MAILGUN_APIKEY with your own Mailgun API key
26+
MAILGUN_APIKEY="your_mailgun_api_key"
2527

2628

2729
# Replace the following variables with your own values:
@@ -32,7 +34,7 @@ SENDER_NAME="the_sender_name"
3234
CONTENT_TYPE="text/plain"
3335

3436
IEXEC_REQUESTER_SECRET_1='{"emailSubject":"'${EMAIL_SUBJECT}'","emailContentEncryptionKey":"'${EMAIL_CONTENT_ENCRYPTION_KEY}'","emailContentMultiAddr":"'${EMAIL_CONTENT_MULTIADDR}'","senderName":"'${SENDER_NAME}'","contentType":"'${CONTENT_TYPE}'"}'
35-
IEXEC_APP_DEVELOPER_SECRET='{"MJ_APIKEY_PUBLIC":"'$MJ_APIKEY_PUBLIC'","MJ_APIKEY_PRIVATE":"'$MJ_APIKEY_PRIVATE'","MJ_SENDER":"'$MJ_SENDER'"}'
37+
IEXEC_APP_DEVELOPER_SECRET='{"MJ_APIKEY_PUBLIC":"'$MJ_APIKEY_PUBLIC'","MJ_APIKEY_PRIVATE":"'$MJ_APIKEY_PRIVATE'","MJ_SENDER":"'$MJ_SENDER'","MAILGUN_APIKEY":"'$MAILGUN_APIKEY'"}'
3638

3739
docker run -it --rm \
3840
-v /tmp/iexec_in:/iexec_in \
@@ -42,4 +44,4 @@ docker run -it --rm \
4244
-e IEXEC_DATASET_FILENAME=${DATA_FILENAME} \
4345
-e IEXEC_APP_DEVELOPER_SECRET=${IEXEC_APP_DEVELOPER_SECRET} \
4446
-e IEXEC_REQUESTER_SECRET_1=${IEXEC_REQUESTER_SECRET_1} \
45-
${IMG_NAME}
47+
${IMG_NAME}

dapp/src/sendEmail.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
downloadEncryptedContent,
1414
decryptContent,
1515
} = require('./decryptEmailContent');
16+
const { validateEmailAddress } = require('./validateEmailAddress');
1617

1718
async function writeTaskOutput(path, message) {
1819
try {
@@ -61,8 +62,16 @@ async function start() {
6162
} catch (e) {
6263
throw Error(`Failed to parse ProtectedData: ${e.message}`);
6364
}
65+
66+
// 1- Validate email address syntax (Joi regexp)
6467
validateProtectedData(protectedData);
6568

69+
// 2- Third-party validation service (Mailgun)
70+
await validateEmailAddress({
71+
emailAddress: protectedData.email,
72+
mailgunApiKey: appDeveloperSecret.MAILGUN_APIKEY,
73+
});
74+
6675
const encryptedEmailContent = await downloadEncryptedContent(
6776
requesterSecret.emailContentMultiAddr
6877
);
@@ -78,6 +87,7 @@ async function start() {
7887
mailJetApiKeyPublic: appDeveloperSecret.MJ_APIKEY_PUBLIC,
7988
mailJetApiKeyPrivate: appDeveloperSecret.MJ_APIKEY_PRIVATE,
8089
mailJetSender: appDeveloperSecret.MJ_SENDER,
90+
mailgunApiKey: appDeveloperSecret.MAILGUN_APIKEY,
8191
// from requester secret
8292
emailContent: requesterEmailContent,
8393
emailSubject: requesterSecret.emailSubject,

dapp/src/validateEmailAddress.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// fetch is not included in node 14
2+
const fetch = require('node-fetch');
3+
4+
async function validateEmailAddress({ emailAddress, mailgunApiKey }) {
5+
// https://documentation.mailgun.com/docs/inboxready/mailgun-validate/single-valid-ir/#single-validation
6+
const basicAuth = Buffer.from(`api:${mailgunApiKey}`).toString('base64');
7+
const response = await fetch(
8+
`https://api.mailgun.net/v4/address/validate?address=${encodeURIComponent(
9+
emailAddress
10+
)}`,
11+
{
12+
method: 'GET',
13+
headers: {
14+
Authorization: `Basic ${basicAuth}`,
15+
},
16+
}
17+
);
18+
19+
if (!response.ok) {
20+
// Mailgun API down? Prefer not to throw
21+
return;
22+
}
23+
24+
const emailAddressSeemsLegit = await response.json();
25+
26+
if (emailAddressSeemsLegit.result !== 'deliverable') {
27+
throw new Error('The protected email address seems to be invalid.');
28+
}
29+
}
30+
31+
module.exports = {
32+
validateEmailAddress,
33+
};

dapp/src/validation.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const appSecretSchema = Joi.object({
1919
MJ_APIKEY_PUBLIC: Joi.string().required(),
2020
MJ_APIKEY_PRIVATE: Joi.string().required(),
2121
MJ_SENDER: Joi.string().email().required(),
22+
MAILGUN_APIKEY: Joi.string().required(),
2223
});
2324

2425
function validateAppSecret(obj) {
220 Bytes
Binary file not shown.
216 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)