| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = r''' |
| 11 |
--- |
| 12 |
module: bigip_device_trust |
| 13 |
short_description: Manage the trust relationships between BIG-IPs |
| 14 |
description: |
| 15 |
- Manage the trust relationships between BIG-IP systems. Devices, once peered, cannot |
| 16 |
be updated. If updating is needed, the peer must first be removed before it |
| 17 |
can be re-added to the trust. |
| 18 |
version_added: "1.0.0" |
| 19 |
options: |
| 20 |
peer_server: |
| 21 |
description: |
| 22 |
- The peer address to connect to and trust for synchronizing the configuration. |
| 23 |
This is typically the management address of the remote device, but may |
| 24 |
also be a Self IP address. |
| 25 |
type: str |
| 26 |
required: True |
| 27 |
peer_hostname: |
| 28 |
description: |
| 29 |
- The hostname you want to associate with the device. This value is |
| 30 |
used to easily distinguish this device in BIG-IP configuration. |
| 31 |
- When trusting a new device, if this parameter is not specified, the value |
| 32 |
of C(peer_server) is used as a default. |
| 33 |
type: str |
| 34 |
peer_user: |
| 35 |
description: |
| 36 |
- The API username of the remote peer device you are trusting. Note |
| 37 |
that the CLI user cannot be used unless it too has an API account. If this |
| 38 |
value is not specified, then the value of C(user), or the environment |
| 39 |
variable C(F5_USER) is used. |
| 40 |
type: str |
| 41 |
peer_password: |
| 42 |
description: |
| 43 |
- The password of the API username of the remote peer device you are |
| 44 |
trusting. If this value is not specified, then the value of C(password), |
| 45 |
or the environment variable C(F5_PASSWORD) is used. |
| 46 |
type: str |
| 47 |
type: |
| 48 |
description: |
| 49 |
- Specifies whether the device you are adding is a Peer or a Subordinate. |
| 50 |
The default is C(peer). |
| 51 |
- The difference between the two is a matter of mitigating risk of |
| 52 |
compromise. |
| 53 |
- A subordinate device cannot sign a certificate for another device. |
| 54 |
- In the case where the security of an authority device in a trust domain |
| 55 |
is compromised, the risk of compromise is minimized for any subordinate |
| 56 |
device. |
| 57 |
- Designating devices as subordinate devices is recommended for device |
| 58 |
groups with a large number of member devices, where the risk of compromise |
| 59 |
is high. |
| 60 |
type: str |
| 61 |
choices: |
| 62 |
- peer |
| 63 |
- subordinate |
| 64 |
default: peer |
| 65 |
state: |
| 66 |
description: |
| 67 |
- When C(present), ensures the specified devices are trusted. |
| 68 |
- When C(absent), removes the device trusts. |
| 69 |
type: str |
| 70 |
choices: |
| 71 |
- absent |
| 72 |
- present |
| 73 |
default: present |
| 74 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 75 |
author: |
| 76 |
- Tim Rupp (@caphrim007) |
| 77 |
''' |
| 78 |
|
| 79 |
EXAMPLES = r''' |
| 80 |
- name: Add trusts for all peer devices to Active device |
| 81 |
bigip_device_trust: |
| 82 |
peer_server: "{{ item.ansible_host }}" |
| 83 |
peer_hostname: "{{ item.inventory_hostname }}" |
| 84 |
peer_user: "{{ item.bigip_username }}" |
| 85 |
peer_password: "{{ item.bigip_password }}" |
| 86 |
provider: |
| 87 |
server: lb.mydomain.com |
| 88 |
user: admin |
| 89 |
password: secret |
| 90 |
loop: hostvars |
| 91 |
when: inventory_hostname in groups['master'] |
| 92 |
delegate_to: localhost |
| 93 |
''' |
| 94 |
|
| 95 |
RETURN = r''' |
| 96 |
peer_server: |
| 97 |
description: The remote IP address of the trusted peer. |
| 98 |
returned: changed |
| 99 |
type: str |
| 100 |
sample: 10.0.2.15 |
| 101 |
peer_hostname: |
| 102 |
description: The remote hostname used to identify the trusted peer. |
| 103 |
returned: changed |
| 104 |
type: str |
| 105 |
sample: test-bigip-02.localhost.localdomain |
| 106 |
''' |
| 107 |
|
| 108 |
import re |
| 109 |
from datetime import datetime |
| 110 |
|
| 111 |
from ansible.module_utils.basic import AnsibleModule |
| 112 |
|
| 113 |
from ..module_utils.bigip import F5RestClient |
| 114 |
from ..module_utils.common import ( |
| 115 |
F5ModuleError, AnsibleF5Parameters, f5_argument_spec |
| 116 |
) |
| 117 |
from ..module_utils.ipaddress import is_valid_ip |
| 118 |
from ..module_utils.icontrol import tmos_version |
| 119 |
from ..module_utils.teem import send_teem |
| 120 |
|
| 121 |
|
| 122 |
class Parameters(AnsibleF5Parameters): |
| 123 |
api_map = { |
| 124 |
'deviceName': 'peer_hostname', |
| 125 |
'caDevice': 'type', |
| 126 |
'device': 'peer_server', |
| 127 |
'username': 'peer_user', |
| 128 |
'password': 'peer_password' |
| 129 |
} |
| 130 |
|
| 131 |
api_attributes = [ |
| 132 |
'name', |
| 133 |
'caDevice', |
| 134 |
'device', |
| 135 |
'deviceName', |
| 136 |
'username', |
| 137 |
'password' |
| 138 |
] |
| 139 |
|
| 140 |
returnables = [ |
| 141 |
'peer_server', 'peer_hostname' |
| 142 |
] |
| 143 |
|
| 144 |
updatables = [] |
| 145 |
|
| 146 |
def to_return(self): |
| 147 |
result = {} |
| 148 |
try: |
| 149 |
for returnable in self.returnables: |
| 150 |
result[returnable] = getattr(self, returnable) |
| 151 |
result = self._filter_params(result) |
| 152 |
except Exception: |
| 153 |
raise |
| 154 |
return result |
| 155 |
|
| 156 |
@property |
| 157 |
def peer_server(self): |
| 158 |
if self._values['peer_server'] is None: |
| 159 |
return None |
| 160 |
if is_valid_ip(self._values['peer_server']): |
| 161 |
return self._values['peer_server'] |
| 162 |
raise F5ModuleError( |
| 163 |
"The provided 'peer_server' parameter is not an IP address." |
| 164 |
) |
| 165 |
|
| 166 |
@property |
| 167 |
def peer_hostname(self): |
| 168 |
if self._values['peer_hostname'] is None: |
| 169 |
return self.peer_server |
| 170 |
regex = re.compile(r'[^a-zA-Z0-9.\-_]') |
| 171 |
result = regex.sub('_', self._values['peer_hostname']) |
| 172 |
return result |
| 173 |
|
| 174 |
@property |
| 175 |
def type(self): |
| 176 |
if self._values['type'] == 'peer': |
| 177 |
return True |
| 178 |
return False |
| 179 |
|
| 180 |
|
| 181 |
class ModuleManager(object): |
| 182 |
def __init__(self, *args, **kwargs): |
| 183 |
self.module = kwargs.get('module', None) |
| 184 |
self.client = F5RestClient(**self.module.params) |
| 185 |
self.have = None |
| 186 |
self.want = Parameters(params=self.module.params) |
| 187 |
self.changes = Parameters() |
| 188 |
|
| 189 |
def _set_changed_options(self): |
| 190 |
changed = {} |
| 191 |
for key in Parameters.returnables: |
| 192 |
if getattr(self.want, key) is not None: |
| 193 |
changed[key] = getattr(self.want, key) |
| 194 |
if changed: |
| 195 |
self.changes = Parameters(params=changed) |
| 196 |
|
| 197 |
def exec_module(self): |
| 198 |
start = datetime.now().isoformat() |
| 199 |
version = tmos_version(self.client) |
| 200 |
changed = False |
| 201 |
result = dict() |
| 202 |
state = self.want.state |
| 203 |
|
| 204 |
if state == "present": |
| 205 |
changed = self.present() |
| 206 |
elif state == "absent": |
| 207 |
changed = self.absent() |
| 208 |
|
| 209 |
changes = self.changes.to_return() |
| 210 |
result.update(**changes) |
| 211 |
result.update(dict(changed=changed)) |
| 212 |
send_teem(start, self.client, self.module, version) |
| 213 |
return result |
| 214 |
|
| 215 |
def provided_password(self): |
| 216 |
if self.want.password: |
| 217 |
return self.password |
| 218 |
if self.want.provider.get('password', None): |
| 219 |
return self.want.provider.get('password') |
| 220 |
if self.module.params.get('password', None): |
| 221 |
return self.module.params.get('password') |
| 222 |
|
| 223 |
def provided_username(self): |
| 224 |
if self.want.username: |
| 225 |
return self.username |
| 226 |
if self.want.provider.get('user', None): |
| 227 |
return self.provider.get('user') |
| 228 |
if self.module.params.get('user', None): |
| 229 |
return self.module.params.get('user') |
| 230 |
|
| 231 |
def present(self): |
| 232 |
if self.exists(): |
| 233 |
return False |
| 234 |
else: |
| 235 |
return self.create() |
| 236 |
|
| 237 |
def create(self): |
| 238 |
self._set_changed_options() |
| 239 |
if self.want.peer_user is None: |
| 240 |
self.want.update({'peer_user': self.provided_username()}) |
| 241 |
if self.want.peer_password is None: |
| 242 |
self.want.update({'peer_password': self.provided_password()}) |
| 243 |
if self.want.peer_hostname is None: |
| 244 |
self.want.update({'peer_hostname': self.want.peer_server}) |
| 245 |
if self.module.check_mode: |
| 246 |
return True |
| 247 |
|
| 248 |
self.create_on_device() |
| 249 |
return True |
| 250 |
|
| 251 |
def absent(self): |
| 252 |
if self.exists(): |
| 253 |
return self.remove() |
| 254 |
return False |
| 255 |
|
| 256 |
def remove(self): |
| 257 |
if self.module.check_mode: |
| 258 |
return True |
| 259 |
if self.want.peer_hostname is None: |
| 260 |
self.want.update({'peer_hostname': self.want.peer_server}) |
| 261 |
self.remove_from_device() |
| 262 |
if self.exists(): |
| 263 |
raise F5ModuleError("Failed to remove the trusted peer.") |
| 264 |
return True |
| 265 |
|
| 266 |
def exists(self): |
| 267 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 268 |
uri = "https://{0}:{1}/mgmt/tm/cm/device".format( |
| 269 |
self.client.provider['server'], |
| 270 |
self.client.provider['server_port'], |
| 271 |
) |
| 272 |
resp = self.client.api.get(uri) |
| 273 |
try: |
| 274 |
response = resp.json() |
| 275 |
except ValueError as ex: |
| 276 |
raise F5ModuleError(str(ex)) |
| 277 |
|
| 278 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 279 |
if 'message' in response: |
| 280 |
raise F5ModuleError(response['message']) |
| 281 |
else: |
| 282 |
raise F5ModuleError(resp.content) |
| 283 |
|
| 284 |
for device in response['items']: |
| 285 |
try: |
| 286 |
if device['managementIp'] == self.want.peer_server: |
| 287 |
return True |
| 288 |
except KeyError: |
| 289 |
pass |
| 290 |
return False |
| 291 |
|
| 292 |
def create_on_device(self): |
| 293 |
params = self.want.api_params() |
| 294 |
params.update({ |
| 295 |
"command": "run", |
| 296 |
"name": 'Root', |
| 297 |
}) |
| 298 |
uri = "https://{0}:{1}/mgmt/tm/cm/add-to-trust/".format( |
| 299 |
self.client.provider['server'], |
| 300 |
self.client.provider['server_port'] |
| 301 |
) |
| 302 |
resp = self.client.api.post(uri, json=params) |
| 303 |
try: |
| 304 |
response = resp.json() |
| 305 |
except ValueError as ex: |
| 306 |
raise F5ModuleError(str(ex)) |
| 307 |
|
| 308 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 309 |
return True |
| 310 |
raise F5ModuleError(resp.content) |
| 311 |
|
| 312 |
def remove_from_device(self): |
| 313 |
params = self.want.api_params() |
| 314 |
params.update({ |
| 315 |
"command": "run", |
| 316 |
"deviceName": self.want.peer_hostname, |
| 317 |
"name": self.want.peer_hostname, |
| 318 |
}) |
| 319 |
uri = "https://{0}:{1}/mgmt/tm/cm/remove-from-trust/".format( |
| 320 |
self.client.provider['server'], |
| 321 |
self.client.provider['server_port'] |
| 322 |
) |
| 323 |
resp = self.client.api.post(uri, json=params) |
| 324 |
try: |
| 325 |
response = resp.json() |
| 326 |
except ValueError as ex: |
| 327 |
raise F5ModuleError(str(ex)) |
| 328 |
|
| 329 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 330 |
return True |
| 331 |
raise F5ModuleError(resp.content) |
| 332 |
|
| 333 |
|
| 334 |
class ArgumentSpec(object): |
| 335 |
def __init__(self): |
| 336 |
self.supports_check_mode = True |
| 337 |
argument_spec = dict( |
| 338 |
peer_server=dict(required=True), |
| 339 |
peer_hostname=dict(), |
| 340 |
peer_user=dict(), |
| 341 |
peer_password=dict(no_log=True), |
| 342 |
type=dict( |
| 343 |
choices=['peer', 'subordinate'], |
| 344 |
default='peer' |
| 345 |
), |
| 346 |
state=dict( |
| 347 |
default='present', |
| 348 |
choices=['present', 'absent'] |
| 349 |
) |
| 350 |
) |
| 351 |
self.argument_spec = {} |
| 352 |
self.argument_spec.update(f5_argument_spec) |
| 353 |
self.argument_spec.update(argument_spec) |
| 354 |
|
| 355 |
|
| 356 |
def main(): |
| 357 |
spec = ArgumentSpec() |
| 358 |
|
| 359 |
module = AnsibleModule( |
| 360 |
argument_spec=spec.argument_spec, |
| 361 |
supports_check_mode=spec.supports_check_mode |
| 362 |
) |
| 363 |
|
| 364 |
try: |
| 365 |
mm = ModuleManager(module=module) |
| 366 |
results = mm.exec_module() |
| 367 |
module.exit_json(**results) |
| 368 |
except F5ModuleError as ex: |
| 369 |
module.fail_json(msg=str(ex)) |
| 370 |
|
| 371 |
|
| 372 |
if __name__ == '__main__': |
| 373 |
main() |