nettoie apikaz

This commit is contained in:
HPL 2024-06-06 10:39:18 +02:00
parent f501d519af
commit acf800f7f6

View File

@ -45,9 +45,9 @@ swagger = Swagger(app, template={
#************************************************* #*************************************************
#Filtrer les IP qui peuvent accéder à l'api #Filtrer les IP qui peuvent accéder à l'api
#TODO: au lieu d'avoir les IP en dur, prendre le fichier allow_ip'
trusted_ips = [ trusted_ips = [
"217.108.155.85",
"82.64.20.246", "82.64.20.246",
"31.39.14.228", "31.39.14.228",
"51.75.112.172", "51.75.112.172",
@ -120,6 +120,7 @@ cloud_url=os.environ.get('cloud_url')
sympa_ident=os.environ.get('sympa_SOAP_USER') sympa_ident=os.environ.get('sympa_SOAP_USER')
sympa_pass=os.environ.get('sympa_SOAP_PASSWORD') sympa_pass=os.environ.get('sympa_SOAP_PASSWORD')
sympa_url=os.environ.get('sympa_url') sympa_url=os.environ.get('sympa_url')
sympa_liste_info=os.environ.get('sympa_liste_info')
gandi_key=os.environ.get('gandi_GANDI_KEY') gandi_key=os.environ.get('gandi_GANDI_KEY')
gandi_url_api=os.environ.get('gandi_GANDI_API') gandi_url_api=os.environ.get('gandi_GANDI_API')
@ -127,16 +128,15 @@ gandi_url_api=os.environ.get('gandi_GANDI_API')
site_url=os.environ.get('site_url') site_url=os.environ.get('site_url')
#pour webmail_url et mdp_url, ça renvoie des tuples et non des str, bizarre, du coup, je mets en dur #pour webmail_url et mdp_url, ça renvoie des tuples et non des str, bizarre, du coup, je mets en dur
#webmail_url=os.environ.get('webmail_url'), webmail_url=os.environ.get('webmail_url'),
#mdp_url=os.environ.get('mdp_url'), mdp_url=os.environ.get('mdp_url'),
webmail_url="https://webmail.kazkouil.fr"
mdp_url="https://mdp.kazkouil.fr"
#pour le mail #pour le mail
app.config['MAIL_SERVER']= os.environ.get('apikaz_MAIL_SERVER') app.config['MAIL_SERVER']= os.environ.get('apikaz_MAIL_SERVER')
app.config['MAIL_PORT'] = 587 app.config['MAIL_PORT'] = 587
app.config['MAIL_USERNAME'] = os.environ.get('apikaz_MAIL_USERNAME') app.config['MAIL_USERNAME'] = os.environ.get('apikaz_MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('apikaz_MAIL_PASSWORD') app.config['MAIL_PASSWORD'] = os.environ.get('apikaz_MAIL_PASSWORD')
app.config['MAIL_REPLY_TO'] = os.environ.get('apikaz_MAIL_REPLY_TO')
app.config['MAIL_USE_TLS'] = True app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USE_SSL'] = False app.config['MAIL_USE_SSL'] = False
mail = Mail(app) mail = Mail(app)
@ -1084,9 +1084,9 @@ class Ldap_user(Resource):
('mail', email.encode('utf-8')), ('mail', email.encode('utf-8')),
('mailEnabled', b'TRUE'), ('mailEnabled', b'TRUE'),
('mailGidNumber', b'5000'), ('mailGidNumber', b'5000'),
('mailHomeDirectory', f"/var/mail/kazkouil.fr/{email.split('@')[0]}/".encode('utf-8')), ('mailHomeDirectory', f"/var/mail/{email.split('@')[1]}/{email.split('@')[0]}/".encode('utf-8')),
('mailQuota', f'{quota}G'.encode('utf-8')), ('mailQuota', f'{quota}G'.encode('utf-8')),
('mailStorageDirectory', f"maildir:/var/mail/kazkouil.fr/{email.split('@')[0]}/".encode('utf-8')), ('mailStorageDirectory', f"maildir:/var/mail/{email.split('@')[1]}/{email.split('@')[0]}/".encode('utf-8')),
('mailUidNumber', b'5000'), ('mailUidNumber', b'5000'),
('mailDeSecours', email_secours.encode('utf-8')), ('mailDeSecours', email_secours.encode('utf-8')),
('identifiantKaz', f'{prenom.lower()}.{nom.lower()}'.encode('utf-8')), ('identifiantKaz', f'{prenom.lower()}.{nom.lower()}'.encode('utf-8')),
@ -1260,7 +1260,7 @@ api.add_resource(Cloud_user_delete, '/cloud/user/delete/<string:email>')
class Sympa_user(Resource): class Sympa_user(Resource):
def __init__(self): def __init__(self):
global sympa_ident, sympa_pass, sympa_url global sympa_ident, sympa_pass, sympa_url, app.config['MAIL_USERNAME']
self.sympa_ident = sympa_ident self.sympa_ident = sympa_ident
self.sympa_pass = sympa_pass self.sympa_pass = sympa_pass
self.sympa_url = sympa_url self.sympa_url = sympa_url
@ -1268,7 +1268,7 @@ class Sympa_user(Resource):
def _execute_sympa_command(self, email, liste, service): def _execute_sympa_command(self, email, liste, service):
try: try:
if validate_email(email) and validate_email(liste): if validate_email(email) and validate_email(liste):
cmd = f'export PERL5LIB=/usr/src/app/:$PERL5LIB && /usr/src/app/Sympa/sympa_soap_client.pl --soap_url={self.sympa_url}/sympasoap --trusted_application={self.sympa_ident} --trusted_application_password={self.sympa_pass} --proxy_vars=USER_EMAIL=admin@kaz.bzh --service={service} --service_parameters="{liste},{email}" && echo $?' cmd = f'export PERL5LIB=/usr/src/app/:$PERL5LIB && /usr/src/app/Sympa/sympa_soap_client.pl --soap_url={self.sympa_url}/sympasoap --trusted_application={self.sympa_ident} --trusted_application_password={self.sympa_pass} --proxy_vars=USER_EMAIL={app.config['MAIL_USERNAME']} --service={service} --service_parameters="{liste},{email}" && echo $?'
output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT) output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
return output.decode("utf-8"), 200 # Retourne la sortie et un code de succès return output.decode("utf-8"), 200 # Retourne la sortie et un code de succès
except EmailNotValidError as e: except EmailNotValidError as e:
@ -1547,6 +1547,8 @@ api.add_resource(Dns, '/dns/<string:sdomaine>/<string:serveur>', endpoint='dns_p
class Kaz_user(Resource): class Kaz_user(Resource):
def __init__(self): def __init__(self):
global sympa_liste_info
self.paheko_users_action_resource = Paheko_users_action() self.paheko_users_action_resource = Paheko_users_action()
self.paheko_user_resource=Paheko_user() self.paheko_user_resource=Paheko_user()
self.ldap_user_resource = Ldap_user() self.ldap_user_resource = Ldap_user()
@ -1557,6 +1559,7 @@ class Kaz_user(Resource):
self.mattermost_user_channel_resource=Mattermost_user_channel() self.mattermost_user_channel_resource=Mattermost_user_channel()
self.mattermost_team_resource=Mattermost_team() self.mattermost_team_resource=Mattermost_team()
self.sympa_user_resource=Sympa_user() self.sympa_user_resource=Sympa_user()
#******************************************************************************************** #********************************************************************************************
def delete(self): def delete(self):
@ -1578,16 +1581,20 @@ class Kaz_user(Resource):
if glob(f"{tempfile.gettempdir()}/{prefixe}*"): raise ValueError("ERREUR 0 : api déjà en cours d'utilisation !") if glob(f"{tempfile.gettempdir()}/{prefixe}*"): raise ValueError("ERREUR 0 : api déjà en cours d'utilisation !")
lock_file = tempfile.NamedTemporaryFile(prefix=prefixe,delete=True) lock_file = tempfile.NamedTemporaryFile(prefix=prefixe,delete=True)
liste_emails=["0a@kazkouil.fr","0b@kazkouil.fr"] #TODO à remplir à la main
liste_emails=["",""]
email_secours=""
liste_sympa=""
for email in liste_emails: for email in liste_emails:
res, status_code = self.ldap_user_resource.delete(email) res, status_code = self.ldap_user_resource.delete(email)
res, status_code = self.mattermost_user_resource.delete(email) res, status_code = self.mattermost_user_resource.delete(email)
nom_orga=''.join(random.choice(string.ascii_lowercase) for _ in range(8)) nom_orga=''.join(random.choice(string.ascii_lowercase) for _ in range(8))
res, status_code = self.paheko_user_resource.put(email,"nom_orga",nom_orga) res, status_code = self.paheko_user_resource.put(email,"nom_orga",nom_orga)
res, status_code = self.paheko_user_resource.put(email,"action_auto","A créer") res, status_code = self.paheko_user_resource.put(email,"action_auto","A créer")
res, status_code = self.paheko_user_resource.put(email,"email_secours","fab@kazkouil.fr") res, status_code = self.paheko_user_resource.put(email,"email_secours",email_secours)
res, status_code = self.sympa_user_resource.delete(email,"infos@listes.kaz.bzh") res, status_code = self.sympa_user_resource.delete(email,liste_sympa)
res, status_code = self.sympa_user_resource.delete("fab@kazkouil.fr","infos@listes.kaz.bzh") res, status_code = self.sympa_user_resource.delete(email_secours,liste_sympa)
msg=f"**POST AUTO** suppression de {email} ok" msg=f"**POST AUTO** suppression de {email} ok"
self.mattermost_message_resource.post(message=msg) self.mattermost_message_resource.post(message=msg)
return "OK", 200 return "OK", 200
@ -1698,10 +1705,10 @@ class Kaz_user(Resource):
#on créé dans le cloud genéral #on créé dans le cloud genéral
#inutile car tous les user du ldap sont user du cloud général. #inutile car tous les user du ldap sont user du cloud général.
#on inscrit email et email_secours à la nl infos@listes.kaz.bzh #on inscrit email et email_secours à la nl sympa_liste_info
res, status_code = self.sympa_user_resource.post(email,"infos@listes.kaz.bzh") res, status_code = self.sympa_user_resource.post(email,sympa_liste_info)
if status_code != 200: raise ValueError(f"ERREUR 9 sur Sympa: {email} : {res}, on arrête tout ") if status_code != 200: raise ValueError(f"ERREUR 9 sur Sympa: {email} : {res}, on arrête tout ")
res, status_code = self.sympa_user_resource.post(email_secours,"infos@listes.kaz.bzh") res, status_code = self.sympa_user_resource.post(email_secours,sympa_liste_info)
if status_code != 200: raise ValueError(f"ERREUR 10 sur Sympa: {email_secours} : {res}, on arrête tout ") if status_code != 200: raise ValueError(f"ERREUR 10 sur Sympa: {email_secours} : {res}, on arrête tout ")
#on construit/envoie le mail #on construit/envoie le mail
@ -1720,7 +1727,7 @@ class Kaz_user(Resource):
} }
subject="KAZ: confirmation d'inscription !" subject="KAZ: confirmation d'inscription !"
sender=app.config['MAIL_USERNAME'] sender=app.config['MAIL_USERNAME']
reply_to = "contact@kaz.bzh" reply_to = app.config['MAIL_REPLY_TO']
msg = Message(subject=subject, sender=sender, reply_to=reply_to, recipients=[email,email_secours]) msg = Message(subject=subject, sender=sender, reply_to=reply_to, recipients=[email,email_secours])
msg.html = render_template('email_inscription.html', **context) msg.html = render_template('email_inscription.html', **context)
mail.send(msg) mail.send(msg)
@ -1785,10 +1792,8 @@ class Test(Resource):
#**** test messagerie #**** test messagerie
NOM="fab" NOM="toto"
#EMAIL_SOUHAITE='fab@kazkouil.fr' EMAIL_SOUHAITE=''
#EMAIL_SOUHAITE='fab@kaz.bzh'
EMAIL_SOUHAITE='sysadmin@listes.kaz.bzh'
PASSWORD="toto" PASSWORD="toto"
QUOTA="1" QUOTA="1"
ADMIN_ORGA="0" ADMIN_ORGA="0"
@ -1809,7 +1814,7 @@ class Test(Resource):
subject = "KAZ: confirmation d'inscription !" subject = "KAZ: confirmation d'inscription !"
sender=app.config['MAIL_USERNAME'] sender=app.config['MAIL_USERNAME']
reply_to = "contact@kaz.bzh" reply_to = app.config['MAIL_REPLY_TO']
msg = Message(subject=subject, sender=sender, reply_to=reply_to, recipients=[EMAIL_SOUHAITE]) msg = Message(subject=subject, sender=sender, reply_to=reply_to, recipients=[EMAIL_SOUHAITE])
msg.html = render_template('email_inscription.html', **context) msg.html = render_template('email_inscription.html', **context)