def receiver_kafka(tenant, message): message = json.loads(message) try: event = message.get("event") if event == "create" or event == "update": message['username'] = message['data']['id'] uc.createOrEditUser(message) elif event == "remove": device_id = message['data']['id'] uc.deleteUser(device_id) except Exception as e: print(e)
def deleteUser(username): # default values deleteAfter = False reason = 'UNSPECIFIED' # URL param if len(request.args) > 0: if 'reason' in request.args: reason = request.args['reason'] elif 'delete' in request.args: deleteAfter = request.args['delete'] in ['True', 'true'] try: uc.deleteUser(username, reason, deleteAfter) except RequestError as err: return formatResponse(err.errorCode, err.message) return formatResponse(200)
def run(self): while True: LOGGER.debug("waiting for new messages") # To consume latest messages and auto-commit offsets while True: try: consumer = (kafka. KafkaConsumer( get_topic(conf.service, conf.subject), group_id='ejbca', bootstrap_servers=[conf.kafkaHost] ) ) break except kafka.errors.NoBrokersAvailable: LOGGER.error('Could not connect to Kafka at %s.' ' Chances are the server is not ready yet.' ' Will retry in 30sec' % conf.kafkaHost) sleep(30) for message in consumer: try: requestData = json.loads(message.value) except ValueError: LOGGER.error('Could not decode message as JSON. ' + dumpKafkaMessage(message)) continue if 'event' not in requestData.keys(): LOGGER.error('Event not specified. ' + dumpKafkaMessage(message)) continue if requestData['event'] in ['create', 'update']: try: if 'data' not in requestData.keys(): LOGGER.error("data segment not found. " + dumpKafkaMessage(message)) continue if 'id' not in requestData['data'].keys(): LOGGER.error("device id not specified. " + dumpKafkaMessage(message)) continue requestData['username'] = requestData['data']['id'] uc.createOrEditUser(requestData) LOGGER.info('user %s created' % requestData['username']) except RequestError as err: LOGGER.error(err.message + " " + dumpKafkaMessage(message)) elif requestData['event'] == 'delete': try: if 'data' not in requestData.keys(): LOGGER.error("data segment not found. " + dumpKafkaMessage(message)) continue if 'id' not in requestData['data'].keys(): LOGGER.error("device id not specified. " + dumpKafkaMessage(message)) continue uc.deleteUser(requestData['data']['id']) LOGGER.info("Device %s revocated" % requestData['data']['id']) except RequestError as err: LOGGER.error(err.message + " " + dumpKafkaMessage(message)) else: LOGGER.error("'event' " + requestData['event'] + " not implemented" + dumpKafkaMessage(message))