crans_ticket/client.py
2014-09-20 15:15:16 +02:00

66 lines
1.9 KiB
Python

# -*- coding: utf-8 -*-
from __future__ import print_function
from gestion import secrets_new as secrets
import pika
import json
import random
import datetime
import string
from lc_ldap import crans_utils
CREDS = pika.credentials.PlainCredentials('oie', secrets.get('rabbitmq_oie'), True)
PARAMS = pika.ConnectionParameters(host='rabbitmq.crans.org',
port=5671, credentials=CREDS, ssl=True)
rabbit_c = pika.BlockingConnection(PARAMS)
ch = rabbit_c.channel()
ch.queue_declare('CransTicket')
def gen_password():
"""Génère un mot de passe aléatoire"""
random.seed(datetime.datetime.now().microsecond)
chars = string.letters + string.digits + '/=+*'
length = 10
return ''.join([random.choice(chars) for _ in xrange(length)])
class Ticket(object):
data = []
def add_entry(self, x):
self.data.append(x)
def add_account(self, login, mdp):
self.add_entry({'login': login, 'pass': mdp})
def add_machine(self, machine):
todo = {
'host': machine['host'][0].split('.', 1)[0],
'macAddress': machine['macAddress'][0].value,
'type': 'fil',
}
if machine.has_key('ipsec'):
todo['secret'] = machine['ipsec'][0].value
todo['type'] = 'wifi'
self.add_entry(todo)
def reset_password(self, adh):
login = adh['uid'][0].value
try:
value = gen_password()
adh['userPassword'] = [crans_utils.hash_password(value).decode('ascii')]
except EnvironmentError:
print("Impossible de changer le mot de passe de %s" % login)
exit(2)
self.add_account(login, value)
def print(self):
if not self.data:
print("Nothing to print !")
return
ch.basic_publish(exchange='', routing_key='CransTicket',
body=json.dumps(self.data))
print("Un nouveau ticket est en cours d'impression ...")