Debian Codegraph @

ansible_collections/purestorage/flasharray/plugins/modules/purefa_logging.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2021, Simon Dodsley ([email protected])
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {
12 "metadata_version": "1.1",
13 "status": ["deprecated"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 ---
19 module: purefa_logging
20 version_added: '1.19.0'
21 short_description: Manage Pure Storage FlashArray Audit and Session logs
22 description:
23 - view the FlashArray audit trail oe session logs, newest to oldest based on (start) time
24 author:
25 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
26 options:
27 log_type:
28 description:
29 - The type of logs to be viewed
30 type: str
31 default: audit
32 choices: [audit, session]
33 limit:
34 description:
35 - The maximum number of audit events returned
36 default: 1000
37 type: int
38 extends_documentation_fragment:
39 - purestorage.flasharray.purestorage.fa
40 """
41
42 EXAMPLES = r"""
43 - name: List last 100 audit events
44 purestorage.flasharray.purefa_logging:
45 limit: 100
46 fa_url: 10.10.10.2
47 api_token: e31060a7-21fc-e277-6240-25983c6c4592
48
49 - name: List last 24 session events
50 purestorage.flasharray.purefa_logging:
51 limit: 24
52 log_type: session
53 fa_url: 10.10.10.2
54 api_token: e31060a7-21fc-e277-6240-25983c6c4592
55 """
56
57 RETURN = r"""
58 """
59
60 import time
61
62 from ansible.module_utils.basic import AnsibleModule
63 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
64 get_array,
65 purefa_argument_spec,
66 )
67 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
68 LooseVersion,
69 )
70
71 AUDIT_API_VERSION = "2.2"
72
73
74 def main():
75 argument_spec = purefa_argument_spec()
76 argument_spec.update(
77 dict(
78 limit=dict(type="int", default=1000),
79 log_type=dict(type="str", default="audit", choices=["audit", "session"]),
80 )
81 )
82 module = AnsibleModule(argument_spec, supports_check_mode=True)
83
84 array = get_array(module)
85 api_version = array.get_rest_version()
86 audits = []
87 changed = False
88 if LooseVersion(AUDIT_API_VERSION) <= LooseVersion(api_version):
89 changed = True
90 if not module.check_mode:
91 if module.params["log_type"] == "audit":
92 all_audits = list(
93 array.get_audits(
94 limit=module.params["limit"],
95 sort=["time-"],
96 ).items
97 )
98 else:
99 all_audits = list(
100 array.get_sessions(
101 limit=module.params["limit"],
102 sort=["start_time-"],
103 ).items
104 )
105 for audit in all_audits:
106 if module.params["log_type"] == "session":
107 start_time = getattr(audit, "start_time", None)
108 end_time = getattr(audit, "end_time", None)
109 if start_time:
110 human_start_time = time.strftime(
111 "%Y-%m-%d %H:%M:%S", time.localtime(start_time / 1000)
112 )
113 else:
114 human_start_time = None
115 if end_time:
116 human_end_time = time.strftime(
117 "%Y-%m-%d %H:%M:%S", time.localtime(end_time / 1000)
118 )
119 else:
120 human_end_time = None
121
122 data = {
123 "start_time": human_start_time,
124 "end_time": human_end_time,
125 "location": getattr(audit, "location", None),
126 "user": getattr(audit, "user", None),
127 "event": audit.event,
128 "event_count": getattr(audit, "event_count", None),
129 "user_interface": getattr(audit, "user_interface", None),
130 }
131 else:
132 event_time = getattr(audit, "time", None)
133 if event_time:
134 human_event_time = time.strftime(
135 "%Y-%m-%d %H:%M:%S", time.localtime(event_time / 1000)
136 )
137 else:
138 human_event_time = None
139 data = {
140 "time": human_event_time,
141 "arguments": audit.arguments,
142 "command": audit.command,
143 "subcommand": audit.subcommand,
144 "user": audit.user,
145 "origin": getattr(audit.origin, "name", None),
146 }
147 audits.append(data)
148 else:
149 module.fail_json(msg="Purity version does not support audit log return")
150 if module.params["log_type"] == "audit":
151 module.exit_json(changed=changed, audits=audits)
152 else:
153 module.exit_json(changed=changed, sessions=audits)
154
155
156 if __name__ == "__main__":
157 main()