Debian Codegraph @

ansible_collections/cisco/meraki/plugins/modules/meraki_mx_malware.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2019, Kevin Breit (@kbreit) <[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 __metaclass__ = type
9
10 ANSIBLE_METADATA = {
11 'metadata_version': '1.1',
12 "status": ['deprecated'],
13 'supported_by': 'community'
14 }
15
16 DOCUMENTATION = r'''
17 ---
18 module: meraki_mx_malware
19 short_description: Manage Malware Protection in the Meraki cloud
20 description:
21 - Fully configure malware protection in a Meraki environment.
22 notes:
23 - Some of the options are likely only used for developers within Meraki.
24 deprecated:
25 removed_in: '3.0.0'
26 why: Updated modules released with increased functionality
27 alternative: cisco.meraki.networks_appliance_security_malware
28 options:
29 state:
30 description:
31 - Specifies whether object should be queried, created/modified, or removed.
32 choices: [absent, present, query]
33 default: query
34 type: str
35 net_name:
36 description:
37 - Name of network which configuration is applied to.
38 aliases: [network]
39 type: str
40 net_id:
41 description:
42 - ID of network which configuration is applied to.
43 type: str
44 allowed_urls:
45 description:
46 - List of URLs to whitelist.
47 type: list
48 elements: dict
49 suboptions:
50 url:
51 description:
52 - URL string to allow.
53 type: str
54 comment:
55 description:
56 - Human readable information about URL.
57 type: str
58 allowed_files:
59 description:
60 - List of files to whitelist.
61 type: list
62 elements: dict
63 suboptions:
64 sha256:
65 description:
66 - 256-bit hash of file.
67 type: str
68 aliases: [ hash ]
69 comment:
70 description:
71 - Human readable information about file.
72 type: str
73 mode:
74 description:
75 - Enabled or disabled state of malware protection.
76 choices: [disabled, enabled]
77 type: str
78
79 author:
80 - Kevin Breit (@kbreit)
81 extends_documentation_fragment: cisco.meraki.meraki
82 '''
83
84 EXAMPLES = r'''
85 - name: Enable malware protection
86 meraki_malware:
87 auth_key: abc123
88 state: present
89 org_name: YourOrg
90 net_name: YourNet
91 mode: enabled
92 delegate_to: localhost
93
94 - name: Set whitelisted url
95 meraki_malware:
96 auth_key: abc123
97 state: present
98 org_name: YourOrg
99 net_name: YourNet
100 mode: enabled
101 allowed_urls:
102 - url: www.ansible.com
103 comment: Ansible
104 - url: www.google.com
105 comment: Google
106 delegate_to: localhost
107
108 - name: Set whitelisted file
109 meraki_malware:
110 auth_key: abc123
111 state: present
112 org_name: YourOrg
113 net_name: YourNet
114 mode: enabled
115 allowed_files:
116 - sha256: e82c5f7d75004727e1f3b94426b9a11c8bc4c312a9170ac9a73abace40aef503
117 comment: random zip
118 delegate_to: localhost
119
120 - name: Get malware settings
121 meraki_malware:
122 auth_key: abc123
123 state: query
124 org_name: YourNet
125 net_name: YourOrg
126 delegate_to: localhost
127 '''
128
129 RETURN = r'''
130 data:
131 description: List of administrators.
132 returned: success
133 type: complex
134 contains:
135 mode:
136 description: Mode to enable or disable malware scanning.
137 returned: success
138 type: str
139 sample: enabled
140 allowed_files:
141 description: List of files which are whitelisted.
142 returned: success
143 type: complex
144 contains:
145 sha256:
146 description: sha256 hash of whitelisted file.
147 returned: success
148 type: str
149 sample: e82c5f7d75004727e1f3b94426b9a11c8bc4c312a9170ac9a73abace40aef503
150 comment:
151 description: Comment about the whitelisted entity
152 returned: success
153 type: str
154 sample: TPS report
155 allowed_urls:
156 description: List of URLs which are whitelisted.
157 returned: success
158 type: complex
159 contains:
160 url:
161 description: URL of whitelisted site.
162 returned: success
163 type: str
164 sample: site.com
165 comment:
166 description: Comment about the whitelisted entity
167 returned: success
168 type: str
169 sample: Corporate HQ
170 '''
171
172 from ansible.module_utils.basic import AnsibleModule, json
173 from ansible_collections.cisco.meraki.plugins.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec
174
175
176 def main():
177 # define the available arguments/parameters that a user can pass to
178 # the module
179
180 urls_arg_spec = dict(url=dict(type='str'),
181 comment=dict(type='str'),
182 )
183
184 files_arg_spec = dict(sha256=dict(type='str', aliases=['hash']),
185 comment=dict(type='str'),
186 )
187
188 argument_spec = meraki_argument_spec()
189 argument_spec.update(state=dict(type='str', choices=['absent', 'present', 'query'], default='query'),
190 net_name=dict(type='str', aliases=['network']),
191 net_id=dict(type='str'),
192 mode=dict(type='str', choices=['enabled', 'disabled']),
193 allowed_urls=dict(type='list', default=None, elements='dict', options=urls_arg_spec),
194 allowed_files=dict(type='list', default=None, elements='dict', options=files_arg_spec),
195 )
196
197 # the AnsibleModule object will be our abstraction working with Ansible
198 # this includes instantiation, a couple of common attr would be the
199 # args/params passed to the execution, as well as if the module
200 # supports check mode
201 module = AnsibleModule(argument_spec=argument_spec,
202 supports_check_mode=True,
203 )
204 meraki = MerakiModule(module, function='malware')
205
206 meraki.params['follow_redirects'] = 'all'
207
208 query_url = {'malware': '/networks/{net_id}/appliance/security/malware'}
209 update_url = {'malware': '/networks/{net_id}/appliance/security/malware'}
210
211 meraki.url_catalog['get_one'].update(query_url)
212 meraki.url_catalog['update'] = update_url
213
214 org_id = meraki.params['org_id']
215 if org_id is None:
216 org_id = meraki.get_org_id(meraki.params['org_name'])
217 net_id = meraki.params['net_id']
218 if net_id is None:
219 nets = meraki.get_nets(org_id=org_id)
220 net_id = meraki.get_net_id(net_name=meraki.params['net_name'], data=nets)
221
222 # Check for argument completeness
223 if meraki.params['state'] == 'present':
224 if meraki.params['allowed_files'] is not None or meraki.params['allowed_urls'] is not None:
225 if meraki.params['mode'] is None:
226 meraki.fail_json(msg="mode must be set when allowed_files or allowed_urls is set.")
227
228 # Assemble payload
229 if meraki.params['state'] == 'present':
230 payload = dict()
231 if meraki.params['mode'] is not None:
232 payload['mode'] = meraki.params['mode']
233 if meraki.params['allowed_urls'] is not None:
234 payload['allowedUrls'] = meraki.params['allowed_urls']
235 if meraki.params['allowed_files'] is not None:
236 payload['allowedFiles'] = meraki.params['allowed_files']
237
238 if meraki.params['state'] == 'query':
239 path = meraki.construct_path('get_one', net_id=net_id)
240 data = meraki.request(path, method='GET')
241 if meraki.status == 200:
242 meraki.result['data'] = data
243 elif meraki.params['state'] == 'present':
244 path = meraki.construct_path('get_one', net_id=net_id)
245 original = meraki.request(path, method='GET')
246 if meraki.is_update_required(original, payload):
247 if meraki.module.check_mode is True:
248 meraki.generate_diff(original, payload)
249 original.update(payload)
250 meraki.result['data'] = original
251 meraki.result['changed'] = True
252 meraki.exit_json(**meraki.result)
253 path = meraki.construct_path('update', net_id=net_id)
254 data = meraki.request(path, method='PUT', payload=json.dumps(payload))
255 if meraki.status == 200:
256 meraki.generate_diff(original, data)
257 meraki.result['data'] = data
258 meraki.result['changed'] = True
259 else:
260 meraki.result['data'] = original
261
262 # in the event of a successful module execution, you will want to
263 # simple AnsibleModule.exit_json(), passing the key/value results
264 meraki.exit_json(**meraki.result)
265
266
267 if __name__ == '__main__':
268 main()