Esempio n. 1
0
class User(UserMixin, SurrogatePK, Model):
    username = Column(db.String(80), unique=True, nullable=False)
    email = Column(db.String)

    password = db.Column(db.String, nullable=False)
    created_at = Column(db.DateTime,
                        nullable=False,
                        default=dt.datetime.utcnow)

    # oauth related stuff
    social_id = Column(db.String)
    first_name = Column(db.String)
    last_name = Column(db.String)

    def __init__(self,
                 username,
                 password=None,
                 email=None,
                 social_id=None,
                 first_name=None,
                 last_name=None):
        self.username = username
        self.email = email
        if password:
            self.set_password(password)
        else:
            self.password = None

        self.social_id = social_id
        self.first_name = first_name
        self.last_name = last_name

    def set_password(self, password):
        self.update(
            password=bcrypt.generate_password_hash(password).decode('utf-8'))
        return

    def check_password(self, value):
        if not self.password:
            # still do the computation
            return bcrypt.generate_password_hash(value) and False
        return bcrypt.check_password_hash(self.password, value)

    def generate_auth_token(self):
        s = Serializer(current_app.config['SECRET_KEY'])
        return s.dumps({'id': self.id})

    @staticmethod
    def verify_auth_token(token):
        if not token:
            return None
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except SignatureExpired:
            return None  # valid token, but expired
        except BadSignature:
            return None  # invalid token
        user = User.query.get(data['id'])
        return user
Esempio n. 2
0
class AlertEmail(SurrogatePK, Model):
    alert_id = db.Column(db.Integer,
                         db.ForeignKey('alerts.id', ondelete='CASCADE'))
    alert = relationship(
        'Alerts',
        backref=db.backref('alert_email', lazy='dynamic',
                           passive_deletes=True),
    )

    status = Column(db.String, nullable=True)

    node_id = db.Column(db.Integer, db.ForeignKey('node.id',
                                                  ondelete='CASCADE'))
    node = relationship('Node',
                        backref=db.backref('alert_email',
                                           passive_deletes=True,
                                           lazy='dynamic'))

    body = Column(db.String, nullable=False)

    created_at = Column(db.DateTime,
                        nullable=False,
                        default=dt.datetime.utcnow)
    updated_at = Column(db.DateTime, nullable=False)

    def __init__(self,
                 node=None,
                 node_id=None,
                 alert=None,
                 alert_id=None,
                 body=None,
                 status=None,
                 updated_at=dt.datetime.utcnow()):

        if node:
            self.node = node
        elif node_id:
            self.node_id = node_id
        if alert:
            self.alert = alert
        elif alert_id:
            self.alert_id = alert_id
        self.updated_at = updated_at
        self.body = body
        self.status = status
Esempio n. 3
0
class AlertLog(SurrogatePK, Model):
    name = Column(db.String, nullable=False)
    timestamp = Column(db.DateTime, default=dt.datetime.utcnow)
    action = Column(db.String)
    columns = Column(JSONB)
    alert_id = db.Column(db.Integer,
                         db.ForeignKey('alerts.id', ondelete='CASCADE'))
    result_log_uuid = Column(db.String, nullable=True)

    def __init__(self,
                 name=None,
                 action=None,
                 columns=None,
                 timestamp=None,
                 alert_id=None,
                 result_log_uuid=None):
        self.name = name
        self.action = action
        self.columns = columns or {}
        self.timestamp = timestamp
        self.alert_id = alert_id
        self.result_log_uuid = result_log_uuid

    def to_dict(self):
        dictionary = {}
        for c in self.__table__.columns:
            if not c.name == "timestamp":
                dictionary[c.name] = getattr(self, c.name)
            else:
                dictionary[c.name] = getattr(
                    self, c.name).strftime('%d-%m-%Y %H:%M:%S.%f')
        return dictionary

    @declared_attr
    def __table_args__(cls):
        return (
            Index('idx_%s_name' % cls.__tablename__, 'name'),
            Index('idx_%s_result_log_uuid' % cls.__tablename__,
                  'result_log_uuid'),
        )
Esempio n. 4
0
class DistributedQueryTask(SurrogatePK, Model):
    NEW = 0
    PENDING = 1
    COMPLETE = 2
    FAILED = 3
    NOT_SENT = 4
    HIGH = 0

    LOW = 1

    save_results_in_db = Column(db.Boolean, nullable=False, default=False)
    guid = Column(db.String, nullable=False, unique=True)
    status = Column(db.Integer, default=0, nullable=False)
    timestamp = Column(db.DateTime)
    data = Column(JSONB)
    updated_at = Column(db.DateTime, nullable=True, default=None)
    viewed_at = Column(db.DateTime, nullable=True, default=None)
    priority = Column(db.Integer, default=0, nullable=False)
    sql = Column(db.String, nullable=True)

    distributed_query_id = reference_col('distributed_query', nullable=False)
    distributed_query = relationship(
        'DistributedQuery',
        backref=db.backref('tasks',
                           cascade='all, delete-orphan',
                           lazy='dynamic'),
    )

    node_id = db.Column(db.Integer, db.ForeignKey('node.id',
                                                  ondelete='CASCADE'))
    node = relationship('Node',
                        backref=db.backref('distributed_queries',
                                           passive_deletes=True,
                                           lazy='dynamic'))

    def __init__(self,
                 node=None,
                 node_id=None,
                 distributed_query=None,
                 save_results_in_db=False,
                 distributed_query_id=None,
                 updated_at=None,
                 priority=0,
                 viewed_at=None,
                 data=None):
        self.guid = str(uuid.uuid4())
        self.updated_at = updated_at
        self.viewed_at = viewed_at
        self.save_results_in_db = save_results_in_db
        self.data = data
        self.priority = priority
        if node:
            self.node = node
        elif node_id:
            self.node_id = node_id
        if distributed_query:
            self.distributed_query = distributed_query
        elif distributed_query_id:
            self.distributed_query_id = distributed_query_id

    @declared_attr
    def __table_args__(cls):
        return (Index('idx_%s_node_id_status' % cls.__tablename__, 'node_id',
                      'status'), )

    def to_dict_obj(self):
        return {
            'id': self.id,
            'distributed_query': {
                'description': self.distributed_query.description,
                'sql': self.distributed_query.sql
            },
            'results': self.data,
        }
Esempio n. 5
0
class CarveSession(SurrogatePK, Model):
    # StatusQueried for queried carves that did not hit nodes yet
    StatusQueried = "QUERIED"
    # StatusInitialized for initialized carves
    StatusInitialized = "INITIALIZED"
    # StatusInProgress for carves that are on-going
    StatusInProgress = "IN PROGRESS"
    #  StatusCompleted for carves that finalized
    StatusBuilding = "BUILDING"
    #  StatusCompleted for carves that finalized
    StatusCompleted = "COMPLETED"

    session_id = Column(db.String, nullable=False)
    carve_guid = Column(db.String, nullable=False)

    carve_size = Column(db.Integer)
    block_size = Column(db.Integer)
    block_count = Column(db.Integer)
    completed_blocks = Column(db.Integer, default=0)
    archive = Column(db.String())

    request_id = Column(db.String, nullable=False)
    status = Column(db.String, nullable=False)

    node_id = db.Column(db.Integer, db.ForeignKey('node.id',
                                                  ondelete='CASCADE'))
    node = relationship('Node',
                        backref=db.backref('carve_session',
                                           passive_deletes=True,
                                           lazy='dynamic'))

    created_at = Column(db.DateTime,
                        nullable=False,
                        default=dt.datetime.utcnow)
    updated_at = Column(db.DateTime, nullable=False)

    def __init___(self,
                  node_id,
                  session_id=None,
                  carve_guid=None,
                  carve_size=0,
                  block_size=0,
                  block_count=0,
                  archive=None,
                  request_id=None):
        self.node_id = node_id
        self.session_id = session_id
        self.carve_guid = carve_guid
        self.carve_size = carve_size
        self.block_size = block_size
        self.block_count = block_count
        self.archive = archive
        self.request_id = request_id

    def to_dict(self):
        """Return object data in easily serializeable format"""
        return {
            'id': self.id,
            'node_id': self.node_id,
            'session_id': self.session_id,
            'carve_guid': self.carve_guid,
            'carve_size': self.carve_size,
            'block_count': self.block_count,
            'archive': self.archive,
            'created_at': dump_datetime(self.created_at),
        }