summaryrefslogtreecommitdiffstats
path: root/ansible_collections/ibm/qradar/plugins/modules/qradar_offense_note.py
blob: 2cda42c6952bd6655cee526c1b6dd69f9c231c0c (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2019, Adam Miller (admiller@redhat.com)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function


__metaclass__ = type

DOCUMENTATION = """
---
module: offense_note
short_description: Create or update a QRadar Offense Note
description:
  - This module allows to create a QRadar Offense note
version_added: "1.0.0"
options:
  id:
    description:
      - Offense ID to operate on
    required: true
    type: int
  note_text:
    description: The note's text contents
    required: true
    type: str

author: Ansible Security Automation Team (@maxamillion) <https://github.com/ansible-security>
"""

"""
# FIXME - WOULD LIKE TO QUERY BY NAME BUT HOW TO ACCOMPLISH THAT IS NON-OBVIOUS
# offense_name:
#   description:
#    - Name of Offense
#   required: true
#   type: str

# FIXME - WOULD LIKE TO MANAGE STATE
# state:
#   description: Define state of the note: present or absent
#   required: false
#   choices: ["present", "absent"]
#   default: "present"
"""

EXAMPLES = """
- name: Add a note to QRadar Offense ID 1
  ibm.qradar.offense_note:
    id: 1
    note_text: This an example note entry that should be made on offense id 1
"""

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six.moves.urllib.parse import quote

from ansible_collections.ibm.qradar.plugins.module_utils.qradar import QRadarRequest


def set_offense_values(module, qradar_request):
    if module.params["closing_reason"]:
        code, found_closing_reason = qradar_request.get(
            "/api/siem/offense_closing_reasons?filter={0}".format(
                quote('text="{0}"'.format(module.params["closing_reason"])),
            ),
        )
        if found_closing_reason:
            module.params["closing_reason_id"] = found_closing_reason[0]["id"]
        else:
            module.fail_json(
                "Unable to find closing_reason text: {0}".format(
                    module.params["closing_reason"],
                ),
            )

    if module.params["status"]:
        module.params["status"] = module.params["status"].upper()


def main():
    argspec = dict(
        #        state=dict(required=False, choices=["present", "absent"], type='str', default="present"),
        id=dict(required=True, type="int"),
        note_text=dict(required=True, type="str"),
    )

    module = AnsibleModule(argument_spec=argspec, supports_check_mode=True)

    qradar_request = QRadarRequest(
        module,
        not_rest_data_keys=["state", "id"],
    )

    # if module.params['name']:
    #    # FIXME - QUERY HERE BY NAME
    #    found_offense = qradar_request.get('/api/siem/offenses?filter={0}'.format(module.params['name']))
    # FIXME - once this is sorted, add it to module_utils

    code, found_notes = qradar_request.get(
        "/api/siem/offenses/{0}/notes?filter={1}".format(
            module.params["id"],
            quote('note_text="{0}"'.format(module.params["note_text"])),
        ),
    )

    # if module.params['state'] == 'present':

    if found_notes:
        # The note we want exists either by ID or by text name, verify

        note = found_notes[0]
        if note["note_text"] == module.params["note_text"]:
            module.exit_json(
                msg="No changes necessary. Nothing to do.",
                changed=False,
            )
        else:
            if module.check_mode:
                module.exit_json(
                    msg="A change would have occured but did not because Check Mode",
                    changed=True,
                )

            qradar_return_data = qradar_request.post_by_path(
                "api/siem/offenses/{0}/notes?note_text={1}".format(
                    module.params["id"],
                    quote("{0}".format(module.params["note_text"])),
                ),
                data=False,
            )
            module.exit_json(
                msg="Successfully created Offense Note ID: {0}".format(
                    qradar_return_data["id"],
                ),
                qradar_return_data=qradar_return_data,
                changed=False,
            )

    else:
        if module.check_mode:
            module.exit_json(
                msg="A change would have occured but did not because Check Mode",
                changed=True,
            )

        qradar_return_data = qradar_request.post_by_path(
            "api/siem/offenses/{0}/notes?note_text={1}".format(
                module.params["id"],
                quote("{0}".format(module.params["note_text"])),
            ),
            data=False,
        )
        module.exit_json(
            msg="Successfully created Offense Note ID: {0}".format(
                qradar_return_data["id"],
            ),
            qradar_return_data=qradar_return_data,
            changed=True,
        )

    module.exit_json(msg="No changes necessary. Nothing to do.", changed=False)

    # FIXME FIXME FIXME - can we actually delete these via the REST API?
    # if module.params['state'] == 'absent':
    #    if not found_notes:
    #        module.exit_json(msg="No changes necessary. Nothing to do.", changed=False)
    #    else:
    #        if module.check_mode:
    #            module.exit_json(msg="A change would have occured but did not because Check Mode", changed=True)
    #        # FIXME: fix the POST here to actually delete
    #        qradar_return_data = qradar_request.post_by_path(
    #            'api/siem/offenses/{0}/notes?note_text={1}'.format(
    #                module.params['id'],
    #                quote("{0}".format(module.params['note_text'])),
    #            ),
    #            data=False
    #        )
    #        module.exit_json(
    #            msg="Successfully created Offense Note ID: {0}".format(qradar_return_data['id']),
    #            qradar_return_data=qradar_return_data,
    #            changed=True
    #        )


if __name__ == "__main__":
    main()