docs: Ajouter guide de déploiement et fichier .env.example

- DEPLOYMENT.md : guide complet déploiement local et Docker Compose
- .env.example : template variables d'environnement
- Instructions development, production et troubleshooting
main
zen6 2025-12-06 23:12:22 +01:00
parent ddece00272
commit f06ac35910
2 changed files with 244 additions and 0 deletions

17
.env.example 100644
View File

@ -0,0 +1,17 @@
# Configuration PKI API
# Mode de stockage: 'memory' (développement) ou 'mongodb' (production)
STORAGE_TYPE=mongodb
# Port du serveur
PORT=8080
# MongoDB
MONGO_URI=mongodb://admin:password@mongo:27017
MONGO_DB=pkiapi
# Sécurité JWT
JWT_SECRET_KEY=super-secret-key-change-in-production
# Gin Framework Mode
GIN_MODE=release

227
DEPLOYMENT.md 100644
View File

@ -0,0 +1,227 @@
# Guide de Déploiement - PKI API
## Déploiement Local (Développement)
### Mode MemoryStore (en mémoire)
```bash
export STORAGE_TYPE=memory
export PORT=8080
export JWT_SECRET_KEY=dev-key-change-in-prod
go build -o pkiapi ./cmd/main.go
./pkiapi
```
L'API démarre sur `http://localhost:8080`
### Mode MongoDB Local
1. **Démarrer MongoDB** (via Docker):
```bash
docker run -d -p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=password \
--name pkiapi-mongo \
mongo:latest
```
2. **Configurer les variables d'environnement**:
```bash
export STORAGE_TYPE=mongodb
export MONGO_URI=mongodb://admin:password@localhost:27017
export MONGO_DB=pkiapi
export JWT_SECRET_KEY=super-secret-key
```
3. **Lancer l'API**:
```bash
go build -o pkiapi ./cmd/main.go
./pkiapi
```
## Déploiement Docker Compose (Recommandé)
### Prérequis
- Docker et Docker Compose installés
- Port 8080 et 27017 libres
### Lancer le déploiement complet
```bash
# Copier la configuration d'exemple
cp .env.example .env
# Éditer .env si nécessaire
nano .env
# Lancer les services
docker compose up -d
# Vérifier l'état
docker compose ps
# Voir les logs de l'API
docker compose logs api -f
# Voir les logs de MongoDB
docker compose logs mongo -f
```
### Arrêter les services
```bash
docker compose down
# Avec suppression du volume MongoDB
docker compose down -v
```
## Vérifier le déploiement
### 1. Vérifier que l'API est accessible
```bash
curl http://localhost:8080/api/v1/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"admin"}'
```
Réponse attendue:
```json
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"expires_in": 86400
}
```
### 2. Obtenir un token et tester un endpoint protégé
```bash
# Obtenir le token
TOKEN=$(curl -s -X POST http://localhost:8080/api/v1/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"admin"}' | jq -r '.token')
# Lister les certificats (endpoint protégé)
curl -H "Authorization: Bearer $TOKEN" \
http://localhost:8080/api/v1/certificates
```
### 3. Créer une CA et un certificat signé
```bash
# Obtenir le token
TOKEN=$(curl -s -X POST http://localhost:8080/api/v1/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"admin"}' | jq -r '.token')
# Créer une CA root
CA_ID=$(curl -s -X POST http://localhost:8080/api/v1/ca \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{"subject":"CN=Root CA","validityDays":3650}' | jq -r '.id')
echo "CA créée: $CA_ID"
# Créer un certificat signé par la CA
curl -X POST http://localhost:8080/api/v1/certificates/sign \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d "{\"caId\":\"$CA_ID\",\"subject\":\"CN=test.example.com\",\"validityDays\":365}" | jq .
```
## Configuration de Production
### Variables d'environnement recommandées
```bash
# Sécurité
JWT_SECRET_KEY=<générer-une-clé-forte-aléatoire>
# MongoDB
MONGO_URI=mongodb://<user>:<password>@<host>:27017
MONGO_DB=pkiapi-prod
# API
PORT=8080
STORAGE_TYPE=mongodb
# Gin
GIN_MODE=release
```
### Recommandations de sécurité
1. **MongoDB**:
- Utiliser MongoDB Atlas ou un cluster MongoDB géré
- Activer l'authentification avec credentials forts
- Utiliser SSL/TLS pour les connexions
- Restreindre les accès par IP
2. **API**:
- Générer une clé JWT forte et aléatoire
- Utiliser HTTPS en production
- Placer derrière un reverse proxy (nginx, Apache)
- Configurer les CORS si nécessaire
- Utiliser un WAF (Web Application Firewall)
3. **Infrastructure**:
- Exécuter dans un conteneur avec ressources limitées
- Utiliser des secrets manager (Vault, AWS Secrets Manager)
- Configurer les logs centralisés
- Mettre en place la surveillance et alertes
## Troubleshooting
### Docker Compose ne démarre pas
```bash
# Vérifier les erreurs de build
docker compose build --no-cache
# Voir les logs détaillés
docker compose up --abort-on-container-exit
```
### Erreur de connexion MongoDB
```bash
# Vérifier que MongoDB est accessible
docker compose exec mongo mongosh --authenticationDatabase admin -u admin -p password
# Vérifier la connectivité réseau
docker compose exec api ping mongo
```
### API retourne erreur d'authentification
```bash
# Vérifier le token JWT
curl -X POST http://localhost:8080/api/v1/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"admin"}'
# Ajouter le token dans les headers
curl -H "Authorization: Bearer <token>" http://localhost:8080/api/v1/certificates
```
## Performance et Scaling
- **MemoryStore**: Idéal pour développement et tests (single instance)
- **MongoStore**: Adapté pour production avec replica set MongoDB
- **Scaling horizontal**: Lancer plusieurs instances de l'API derrière un load balancer
## Maintenance
### Sauvegarde MongoDB
```bash
docker compose exec mongo mongodump --authenticationDatabase admin -u admin -p password -o /dump
```
### Restauration MongoDB
```bash
docker compose exec mongo mongorestore --authenticationDatabase admin -u admin -p password /dump
```
### Nettoyage des volumes
```bash
docker compose down -v # ⚠️ Supprime TOUS les données !
```