def create_token(self, database_client: DatabaseClient, user: str, description: str, expiration: Optional[datetime.timedelta]) -> dict: now = self.date_time_provider.now() token = { "identifier": str(uuid.uuid4()), "user": user, "type": "token", "description": description, "hash_function": self.token_hash_function, "hash_function_parameters": self.token_hash_function_parameters, "hash_function_salt": None, "expiration_date": None, "creation_date": self.date_time_provider.serialize(now), "update_date": self.date_time_provider.serialize(now), } if expiration is not None: token["expiration_date"] = self.date_time_provider.serialize(now + expiration) secret = secrets.token_hex(self.token_size) token["secret"] = self.hash_token(secret, token["hash_function"], token["hash_function_parameters"]) database_client.insert_one(self.table, token) result = self.convert_to_public(token) result["secret"] = secret return result
def set_password(self, database_client: DatabaseClient, user: str, password: str) -> dict: now = self.date_time_provider.now() authentication = database_client.find_one(self.table, { "user": user, "type": "password" }) if authentication is None: authentication = { "identifier": str(uuid.uuid4()), "user": user, "type": "password", "creation_date": self.date_time_provider.serialize(now), "update_date": self.date_time_provider.serialize(now), } database_client.insert_one(self.table, authentication) authentication.update({ "hash_function": self.password_hash_function, "hash_function_parameters": self.password_hash_function_parameters, "hash_function_salt": secrets.token_hex(self.password_salt_size), "update_date": self.date_time_provider.serialize(now), }) authentication["secret"] = self.hash_password(password, authentication["hash_function_salt"], authentication["hash_function"], authentication["hash_function_parameters"]) database_client.update_one(self.table, { "identifier": authentication["identifier"] }, authentication) return self.convert_to_public(authentication)
def create( self, database_client: DatabaseClient, # pylint: disable = too-many-arguments worker_identifier: str, owner: str, version: str, display_name: str) -> dict: now = self.date_time_provider.now() worker = { "identifier": worker_identifier, "owner": owner, "version": version, "display_name": display_name, "properties": {}, "is_enabled": True, "is_active": False, "should_disconnect": False, "creation_date": self.date_time_provider.serialize(now), "update_date": self.date_time_provider.serialize(now), } database_client.insert_one(self.table, worker) return worker
def create_or_update(self, database_client: DatabaseClient, # pylint: disable = too-many-arguments schedule_identifier: str, project: str, display_name: str, job: str, parameters: dict, expression: str) -> dict: now = self.date_time_provider.now() schedule = self.get(database_client, project, schedule_identifier) if schedule is None: schedule = { "project": project, "identifier": schedule_identifier, "display_name": display_name, "job": job, "parameters": parameters, "expression": expression, "is_enabled": False, "last_run": None, "creation_date": self.date_time_provider.serialize(now), "update_date": self.date_time_provider.serialize(now), } database_client.insert_one(self.table, schedule) else: update_data = { "display_name": display_name, "job": job, "parameters": parameters, "expression": expression, "update_date": self.date_time_provider.serialize(now), } schedule.update(update_data) database_client.update_one(self.table, { "project": project, "identifier": schedule_identifier }, update_data) return schedule
def create(self, database_client: DatabaseClient, # pylint: disable = too-many-arguments project: str, job: str, parameters: dict, source: dict) -> dict: identifier = self.create_identifier(database_client) now = self.date_time_provider.now() run = { "identifier": identifier, "project": project, "job": job, "parameters": parameters, "source": source, "worker": None, "status": "pending", "start_date": None, "completion_date": None, "results": None, "should_cancel": False, "should_abort": False, "creation_date": self.date_time_provider.serialize(now), "update_date": self.date_time_provider.serialize(now), } database_client.insert_one(self.table, run) return run
def create_or_update( self, database_client: DatabaseClient, # pylint: disable = too-many-arguments job_identifier: str, project: str, display_name: str, description: str, definition: dict, parameters: list, properties: dict) -> dict: now = self.date_time_provider.now() job = self.get(database_client, project, job_identifier) if job is None: job = { "project": project, "identifier": job_identifier, "display_name": display_name, "description": description, "definition": definition, "parameters": parameters, "properties": properties, "is_enabled": True, "creation_date": self.date_time_provider.serialize(now), "update_date": self.date_time_provider.serialize(now), } database_client.insert_one(self.table, job) else: update_data = { "display_name": display_name, "description": description, "definition": definition, "parameters": parameters, "properties": properties, "update_date": self.date_time_provider.serialize(now), } job.update(update_data) database_client.update_one(self.table, { "project": project, "identifier": job_identifier }, update_data) return job
def create(self, database_client: DatabaseClient, user_identifier: str, display_name: str) -> dict: if self.user_identifier_regex.search(user_identifier) is None: raise ValueError("User identifier is invalid: '%s'" % user_identifier) now = self.date_time_provider.now() user = { "identifier": user_identifier, "display_name": display_name, "roles": [], "is_enabled": True, "creation_date": self.date_time_provider.serialize(now), "update_date": self.date_time_provider.serialize(now), } database_client.insert_one(self.table, user) return user