Skip to content

Commit

Permalink
Merge pull request #63 from SocialGouv/feat/mailer-to-nodemailer
Browse files Browse the repository at this point in the history
feat: change aws email to nodemailer
  • Loading branch information
ClementNumericite authored Sep 13, 2023
2 parents 15a1f84 + 4d38759 commit 512dafc
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 198 deletions.
14 changes: 7 additions & 7 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ CLUSTER_NAME=docker-cluster
# ElasticApi key name
NEXT_PUBLIC_ELASTIC_API_KEY_NAME=cm2d_api_key

# AWS SES credentials
AWS_ACCESS_KEY_ID=your_access_key_id_here
AWS_SECRET_ACCESS_KEY=your_secret_access_key_here
AWS_REGION=your_aws_region_here

# The email address that sends the email
EMAIL_SOURCE=your_email_source_here
# Mailer
NODEMAILER_HOST=ex4.mail.ovh.net
NODEMAILER_PORT=587
NODEMAILER_USER=user
NODEMAILER_PASSWORD=password
NODEMAILER_FROM=example@email.com
NODEMAILER_BASEURL=http://localhost:3000
40 changes: 23 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,16 @@ Il est maintenant possible de se connecter en utilisant l'adresse email et le mo

## Les variables d'environnement NextJS

| Nom de la variable | Description |
| --------------------- | ----------------------------------------------------------------------- |
| ELASTIC_HOST | L'URL du serveur Elasticsearch, ici configuré pour une instance locale. |
| ELASTIC_PASSWORD | Le mot de passe à utiliser pour se connecter à Elasticsearch. |
| AWS_ACCESS_KEY_ID | Votre ID de clé d'accès AWS pour AWS SES. |
| AWS_SECRET_ACCESS_KEY | Votre clé d'accès secrète AWS pour AWS SES. |
| AWS_REGION | La région AWS dans laquelle AWS SES est configuré. |
| EMAIL_SOURCE | L'adresse e-mail utilisée pour envoyer les e-mails. |
| Nom de la variable | Description |
| ------------------- | ---------------------------------------------------------------------------- |
| ELASTIC_HOST | L'URL du serveur Elasticsearch, ici configuré pour une instance locale. |
| ELASTIC_PASSWORD | Le mot de passe à utiliser pour se connecter à Elasticsearch. |
| NODEMAILER_HOST | Le host domain pour se connecter au SMTP. |
| NODEMAILER_PORT | Le port pour se connecter au SMTP. |
| NODEMAILER_USER | Identifiant pour l'authentification au SMTP. |
| NODEMAILER_PASSWORD | Mot de passe pour l'authentification au SMTP. |
| NODEMAILER_FROM | L'adresse e-mail utilisée pour envoyer les e-mails. |
| NODEMAILER_BASEURL | L'URL courante de l'application pour construire les liens envoyés par email. |

## Initialisation de l'environnement ELK

Expand Down Expand Up @@ -180,39 +182,43 @@ Continous mode
Date field for continous mode : `@timestamp`
Delay : `60s`

## Docker production

### Créer les images docker

### Docker production

## Créer les images docker
```
docker build -t cm2d-elasticsearch docker/elasticsearch
docker build -t cm2d-kibana docker/kibana
docker build --build-arg NEXT_PUBLIC_ELASTIC_API_KEY_NAME=${NEXT_PUBLIC_ELASTIC_API_KEY_NAME} -t cm2d-webapp webapp-next
```

## Créer les réseaux docker
### Créer les réseaux docker

```
docker network create elastic
docker network create webapp
```

## Elasticsearch
### Elasticsearch

```
docker run -d -p 9200:9200 -p 9300:9300 --net elastic -v es_data:/usr/share/elasticsearch/data -v certs:/usr/share/elasticsearch/config/certs -e ELASTIC_PASSWORD=${ELASTIC_PASSWORD} --name elasticsearch cm2d-elasticsearch
```

## Attacher le réseau webapp à Elasticsearch
### Attacher le réseau webapp à Elasticsearch

```
docker network connect webapp elasticsearch
```

## Kibana
### Kibana

```
docker run -d -p 5601:5601 --net elastic -v kibana_data:/usr/share/kibana/data -v certs:/usr/share/kibana/config/certs -e ELASTICSEARCH_PASSWORD=${KIBANA_PASSWORD} --name kibana cm2d-kibana
```

## Webapp
### Webapp

```
docker run -d -p 3000:3000 --net webapp -v certs:/app/certs --env-file ${path_fichier_environnement} --name webapp cm2d-webapp
```
```
16 changes: 9 additions & 7 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ services:
resources:
limits:
memory: 4G

kibana:
build: docker/kibana
container_name: kibana
ports:
- 5601:5601
depends_on:
depends_on:
- elasticsearch
volumes:
- kibana_data:/usr/share/kibana/data
Expand All @@ -47,18 +47,20 @@ services:
depends_on:
- elasticsearch
ports:
- "3000:3000"
- '3000:3000'
volumes:
- certs:/app/certs
networks:
- webapp
environment:
- ELASTIC_HOST=${ELASTIC_HOST}
- ELASTIC_PASSWORD=${ELASTIC_PASSWORD}
- AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
- AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
- AWS_REGION=${AWS_REGION}
- EMAIL_SOURCE=${EMAIL_SOURCE}
- NODEMAILER_HOST=${NODEMAILER_HOST}
- NODEMAILER_PORT=${NODEMAILER_PORT}
- NODEMAILER_USER=${NODEMAILER_USER}
- NODEMAILER_PASSWORD=${NODEMAILER_PASSWORD}
- NODEMAILER_FROM=${NODEMAILER_FROM}
- NODEMAILER_BASEURL=${NODEMAILER_BASEURL}

volumes:
certs:
Expand Down
14 changes: 7 additions & 7 deletions webapp-next/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ OPENAI_API_KEY=xxx
# ElasticApi key name
NEXT_PUBLIC_ELASTIC_API_KEY_NAME=cm2d_api_key

# AWS SES credentials
AWS_ACCESS_KEY_ID=your_access_key_id_here
AWS_SECRET_ACCESS_KEY=your_secret_access_key_here
AWS_REGION=your_aws_region_here

# The email address that sends the email
EMAIL_SOURCE=your_email_source_here
# Mailer
NODEMAILER_HOST=ex4.mail.ovh.net
NODEMAILER_PORT=587
NODEMAILER_USER=user
NODEMAILER_PASSWORD=password
NODEMAILER_FROM=example@email.com
NODEMAILER_BASEURL=http://localhost:3000
10 changes: 6 additions & 4 deletions webapp-next/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ FROM node:16-alpine3.17 AS builder
ARG ELASTIC_HOST
ARG ELASTIC_PASSWORD
ARG NEXT_PUBLIC_ELASTIC_API_KEY_NAME
ARG AWS_ACCESS_KEY_ID
ARG AWS_SECRET_ACCESS_KEY
ARG AWS_REGION
ARG EMAIL_SOURCE
ARG NODEMAILER_HOST
ARG NODEMAILER_PORT
ARG NODEMAILER_USER
ARG NODEMAILER_PASSWORD
ARG NODEMAILER_FROM
ARG NODEMAILER_BASEURL

WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
Expand Down
2 changes: 1 addition & 1 deletion webapp-next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"@types/react": "18.2.6",
"@types/react-dom": "18.2.4",
"@uidotdev/usehooks": "^2.1.1",
"aws-sdk": "^2.1392.0",
"chart.js": "^4.3.0",
"chartjs-adapter-moment": "^1.0.1",
"date-fns": "^2.30.0",
Expand All @@ -32,6 +31,7 @@
"js-cookie": "^3.0.5",
"moment": "^2.29.4",
"next": "13.4.1",
"nodemailer": "^6.9.5",
"openai": "^3.2.1",
"react": "18.2.0",
"react-chartjs-2": "^5.2.0",
Expand Down
45 changes: 13 additions & 32 deletions webapp-next/pages/api/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { generateCode, getCodeEmailHtml, ELASTIC_API_KEY_NAME } from '@/utils/tools';
import { sendMail } from '@/utils/mailter';
import {
generateCode,
getCodeEmailHtml,
ELASTIC_API_KEY_NAME
} from '@/utils/tools';
import { Client } from '@elastic/elasticsearch';
import fs from 'fs';
import type { NextApiRequest, NextApiResponse } from 'next';
import path from 'path';
const tmpCodes = require('../../../utils/codes');
import AWS from 'aws-sdk';

AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: process.env.AWS_REGION
});

const ses = new AWS.SES({ apiVersion: '2012-10-17' });

export default async function handler(
req: NextApiRequest,
Expand All @@ -28,9 +24,7 @@ export default async function handler(
password
},
tls: {
ca: fs.readFileSync(
path.resolve(process.cwd(), './certs/ca/ca.crt')
),
ca: fs.readFileSync(path.resolve(process.cwd(), './certs/ca/ca.crt')),
rejectUnauthorized: false
}
});
Expand All @@ -53,25 +47,12 @@ export default async function handler(
} else {
tmpCodes[username] = { code: generateCode(), apiKey: securityToken };

await ses
.sendEmail({
Destination: {
ToAddresses: [username]
},
Message: {
Body: {
Text: {
Data: `Code de vérification : ${tmpCodes[username].code}`
},
Html: { Data: getCodeEmailHtml(tmpCodes[username].code) }
},
Subject: {
Data: `Votre code d\'authentification`
}
},
Source: process.env.EMAIL_SOURCE as string
})
.promise();
await sendMail(
"Votre code d'authentification",
username,
getCodeEmailHtml(tmpCodes[username].code),
`Code de vérification : ${tmpCodes[username].code}`
);

res.status(200).send({ response: 'ok' });
}
Expand Down
33 changes: 33 additions & 0 deletions webapp-next/utils/mailter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var nodemailer = require('nodemailer');

export async function sendMail(
subject: string,
toEmail: string,
html: string,
text: string
) {
var transporter = nodemailer.createTransport({
host: process.env.NODEMAILER_HOST,
port: process.env.NODEMAILER_PORT,
auth: {
user: process.env.NODEMAILER_USER,
pass: process.env.NODEMAILER_PASSWORD
}
});

var mailOptions = {
from: process.env.NODEMAILER_FROM,
to: toEmail,
subject: subject,
html,
text
};

transporter.sendMail(mailOptions, function (error: any) {
if (error) {
throw new Error(error);
} else {
return true;
}
});
}
Loading

0 comments on commit 512dafc

Please sign in to comment.