-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjvn_model.py
134 lines (109 loc) · 3.97 KB
/
jvn_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# JVN Vulnerability Infomation Managed System
#
#
from sqlalchemy import orm
from sqlalchemy import create_engine, Column, Integer, String, SmallInteger, DateTime
from sqlalchemy.ext.declarative import declarative_base
import lepl.apps.rfc3696
Base = declarative_base()
class Account(Base):
"""アカウント情報(Model)"""
__tablename__ = "jvn_account"
user_id = Column(String, nullable=False, primary_key=True)
passwd = Column(String)
user_name = Column(String)
email = Column(String)
department = Column(String)
privs = Column(String)
def __init__(self, row, passwd):
self.user_id = row["user_id"]
self.passwd = passwd
self.user_name = row["user_name"]
self.email = row["email"]
self.department = row["department"]
self.privs = row["privs"]
def validate(self, db, method):
error_message = "入力されていない項目があります。(全て必須項目です。)"
if not self.user_id:
return (False, error_message)
if not self.passwd:
return (False, error_message)
if not self.user_name:
return (False, error_message)
if not self.email:
return (False, error_message)
if not self.department:
return (False, error_message)
if not self.privs:
return (False, error_message)
error_message = "最大桁数をこえている項目があります。"
if 32 < len(self.user_id):
return (False, error_message)
if 64 < len(self.passwd):
return (False, error_message)
if 255 < len(self.user_name):
return (False, error_message)
if 255 < len(self.email):
return (False, error_message)
if 32 < len(self.department):
return (False, error_message)
if 8 < len(self.privs):
return (False, error_message)
if method == "regist" and db.query(Account).filter_by(user_id=self.user_id).first():
return (False, "既にアカウントが存在してます。")
email_validator = lepl.apps.rfc3696.Email()
if not email_validator(self.email):
return (False, "メールアドレスの形式が正しくありません。")
return True, ""
class Product(Base):
"""製品情報(Model)"""
__tablename__ = "jvn_product"
pid = Column(Integer, nullable=False)
pname = Column(String, nullable=False)
cpe = Column(String, nullable=False, primary_key=True)
vid = Column(Integer, nullable=False)
fs_manage = Column(String, nullable=False)
edit = Column(SmallInteger)
class Vulnerability(Base):
"""脆弱性情報(Model)"""
__tablename__ = "jvn_vulnerability"
identifier = Column(String, nullable=False, primary_key=True)
title = Column(String, nullable=False)
link = Column(String, nullable=False)
description = Column(String, nullable=False)
issued_date = Column(String, nullable=False)
modified_date = Column(DateTime, nullable=False)
public_date = Column(DateTime, nullable=False)
cweid = Column(String)
cwetitle = Column(String)
ticket_modified_date = Column(DateTime)
def do_transaction(func, app):
"""トランザクション処理"""
ret = None
engine = None
try:
dburl = "postgres://%s:%s@%s:%s/%s" % (
app.config.get("db", "user"),
app.config.get("db", "password"),
app.config.get("db", "host"),
app.config.get("db", "port"),
app.config.get("db", "database"),
)
engine = create_engine(dburl)
Session = orm.scoped_session(
orm.sessionmaker(bind=engine, expire_on_commit=False)
)
db = Session()
ret = func(db)
db.commit()
except Exception as e:
db.rollback()
raise e
finally:
if engine:
engine.dispose()
return ret