-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
topic/create script for collecting threats data #517
topic/create script for collecting threats data #517
Conversation
service_id=service["service_id"], | ||
service_name=service["service_name"], | ||
service_description=service["description"], | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
service_id が見つかったら、他のserviceに用がないのでbreakした方が良い
tag_id=dependency["tag_id"], tag_version=dependency["version"], tag_name=tag["tag_name"] | ||
) | ||
del threat["dependency_id"] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
引数のthreatsの値が変わる、という挙動は推奨しない。
新しいlistを作って返す方が分かりやすい。
〇理由
・1つの変数が場所によって状態が変わる、というコードは読みにくい。状態が変わっている箇所を全部追いかけないと理解できないため。定義時から値が変わらなければ、定義箇所を見れば変数の中身が分かって読みやすい。
・getXXX関数は戻り値を返す、その他状態を更新しない、というケースが多く、この慣習に従い、適切な関数名を付けていると、呼び出し元を見るだけでコードの意味が分かる。
_threats = get_misp_tag(tc_client, threats) | ||
output_data.update(threats=_threats) | ||
output_json_file(output_data, args.pteam_id, args.service_id) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
threats、_threats は変数名を見ても違いが分からない。
多少長くなっても良いので、違いが分かるような名前が良い。
threats_with_tag_data, threats_with_topic_data とか
エラー時の挙動を設計してください |
scripts/collect_threats_data.py
Outdated
return _threats | ||
|
||
|
||
def output_json_file(threats: dict, pteam_id: str, service_id: str): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
引数pteam_idは使ってないので消すべき
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
関数設計が統一されてないので意図が伝わりにくいです。output_dataを受け取って加工しする関数があったり、データを返してmain側でoutput_data を加工したりと挙動がバラバラです。
最終的なデータ構築の責務はmain側に持たせた方がいいと思います
またthreats という辞書の中身がどんどん変化するので関数の中まで見ないと混乱します。関数の中で辞書を書き換えるのは意図せぬ挙動の元なので極力避けた方がいいと思います
scripts/collect_threats_data.py
Outdated
def get_misp_tag(tc_client: ThreatconnectomeClient, threats: list): | ||
""" | ||
Remove threat that did not have cve_id in missp_tag. | ||
""" | ||
_threats: list = [] | ||
for threat in threats: | ||
topic = tc_client.get_topic(threat["topic_id"]) | ||
cve_id: list = [] | ||
for misp_tag in topic["misp_tags"]: | ||
if misp_tag["tag_name"] and misp_tag["tag_name"].startswith("CVE"): | ||
cve_id.append(misp_tag["tag_name"]) | ||
|
||
if len(cve_id) == 0: | ||
continue | ||
else: | ||
threat.update(cve_id=cve_id) | ||
_threats.append(threat) | ||
|
||
return _threats |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
misp_tag の取得と cve_id という値をデータに付与する責務を持っています。関数名がじつ挙動を表してません。責務を分けた方がいいと思います
@mshim03 全体的な修正
部分的な修正
上記のPRでの記載忘れ
|
scripts/collect_threats_data.py
Outdated
try: | ||
response = self._retry_call(requests.get, f"{self.api_url}/threats", params=params) | ||
except APIError as error: | ||
sys.exit("Is the service_id correct?\n" + str(error)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
service_id は正しいが、そのserviceにthreatが1件も無い場合もあるので、
ここではservice_idに紐づくthreatが取得できなかった、とのメッセージが良いと思います。
エラーメッセージを変更しました |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
PR の目的
経緯・意図・意思決定