Нет описания

iocs.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # IRIS Source Code
  2. # Copyright (C) 2024 - DFIR-IRIS
  3. # contact@dfir-iris.org
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 3 of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program; if not, write to the Free Software Foundation,
  17. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. from graphene_sqlalchemy import SQLAlchemyObjectType
  19. from graphene import Field
  20. from graphene import Mutation
  21. from graphene import NonNull
  22. from graphene import Int
  23. from graphene import Float
  24. from graphene import String
  25. from app.blueprints.graphql.permissions import permissions_check_current_user_has_some_case_access
  26. from app.blueprints.graphql.permissions import permissions_check_current_user_has_some_case_access_stricter
  27. from app.models.authorization import CaseAccessLevel
  28. from app.models.models import Ioc
  29. from app.business.iocs import iocs_create
  30. from app.business.iocs import iocs_get
  31. from app.business.iocs import iocs_update
  32. from app.business.iocs import iocs_delete
  33. from graphene.relay import Connection
  34. class IOCObject(SQLAlchemyObjectType):
  35. class Meta:
  36. model = Ioc
  37. class IOCConnection(Connection):
  38. class Meta:
  39. node = IOCObject
  40. total_count = Int()
  41. @staticmethod
  42. def resolve_total_count(root, info, **kwargs):
  43. return root.length
  44. class IOCCreate(Mutation):
  45. class Arguments:
  46. # note: it seems really too difficult to work with IDs.
  47. # I don't understand why graphql_relay.from_global_id does not seem to work...
  48. # note: I prefer NonNull rather than the syntax required=True
  49. case_id = NonNull(Float)
  50. type_id = NonNull(Int)
  51. tlp_id = NonNull(Int)
  52. value = NonNull(String)
  53. description = String()
  54. tags = String()
  55. ioc = Field(IOCObject)
  56. @staticmethod
  57. def mutate(root, info, case_id, type_id, tlp_id, value, description=None, tags=None):
  58. request = {
  59. 'ioc_type_id': type_id,
  60. 'ioc_tlp_id': tlp_id,
  61. 'ioc_value': value,
  62. 'ioc_description': description,
  63. 'ioc_tags': tags
  64. }
  65. permissions_check_current_user_has_some_case_access(case_id, [CaseAccessLevel.full_access])
  66. ioc, _ = iocs_create(request, case_id)
  67. return IOCCreate(ioc=ioc)
  68. class IOCUpdate(Mutation):
  69. class Arguments:
  70. ioc_id = NonNull(Float)
  71. type_id = Int()
  72. tlp_id = Int()
  73. value = String()
  74. description = String()
  75. tags = String()
  76. ioc_misp = String()
  77. user_id = Float()
  78. ioc_enrichment = String()
  79. custom_attributes = String()
  80. modification_history = String()
  81. ioc = Field(IOCObject)
  82. @staticmethod
  83. def mutate(root, info, ioc_id, type_id=None, tlp_id=None, value=None, description=None, tags=None,
  84. ioc_misp=None, user_id=None, ioc_enrichment=None, modification_history=None):
  85. permissions_check_current_user_has_some_case_access_stricter([CaseAccessLevel.full_access])
  86. request = {}
  87. if type_id:
  88. request['ioc_type_id'] = type_id
  89. if tlp_id:
  90. request['ioc_tlp_id'] = tlp_id
  91. if value:
  92. request['ioc_value'] = value
  93. if description:
  94. request['ioc_description'] = description
  95. if tags:
  96. request['ioc_tags'] = tags
  97. if ioc_misp:
  98. request['ioc_misp'] = ioc_misp
  99. if user_id:
  100. request['user_id'] = user_id
  101. if ioc_enrichment:
  102. request['ioc_enrichment'] = ioc_enrichment
  103. if modification_history:
  104. request['modification_history'] = modification_history
  105. ioc = iocs_get(ioc_id)
  106. ioc, _ = iocs_update(ioc, request)
  107. return IOCCreate(ioc=ioc)
  108. class IOCDelete(Mutation):
  109. class Arguments:
  110. ioc_id = NonNull(Float)
  111. message = String()
  112. @staticmethod
  113. def mutate(root, info, ioc_id):
  114. ioc = iocs_get(ioc_id)
  115. permissions_check_current_user_has_some_case_access(ioc.case_id, [CaseAccessLevel.full_access])
  116. message = iocs_delete(ioc)
  117. return IOCDelete(message=message)