Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 53 additions & 3 deletions f5_cccl/utils/mgmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,59 @@
#
"""Wrapper functions for the f5-sdk"""

import logging
import os
import tempfile

from f5.bigip import ManagementRoot

LOGGER = logging.getLogger(__name__)


def mgmt_root(host, username, password, port, token, trusted_certs=''):
"""Create a BIG-IP Management Root object.

Args:
host: BIG-IP hostname or IP address
username: BIG-IP admin username
password: BIG-IP admin password
port: BIG-IP management port (default: 443)
token: Token type for authentication (e.g., "tmos")
trusted_certs: Optional PEM-encoded CA certificate bundle for TLS
verification. If provided, SSL verification is enabled
using these certificates. If empty, SSL verification is
disabled (insecure, for backward compatibility).

def mgmt_root(host, username, password, port, token):
"""Create a BIG-IP Management Root object"""
return ManagementRoot(host, username, password, port=port, token=token)
Returns:
ManagementRoot: A connected BIG-IP management object
"""
if trusted_certs:
# Write trusted certs to a temporary file for use with ManagementRoot.
# The temp file must persist for the lifetime of the ManagementRoot
# session so delete=False is used.
cert_file = tempfile.NamedTemporaryFile(
mode='w', suffix='.pem', delete=False)
try:
cert_file.write(trusted_certs)
cert_file.flush()
cert_file.close()
LOGGER.info(
"SSL verification enabled with trusted certificate(s) "
"from Secret")
return ManagementRoot(
host, username, password, port=port, token=token,
verify=cert_file.name)
except Exception as e:
LOGGER.error(
"Failed to configure SSL verification with trusted "
"certs: %s", e)
# Clean up temp file on error
try:
os.unlink(cert_file.name)
except OSError:
pass
raise
else:
# Backward compatibility: SSL verification disabled (insecure)
return ManagementRoot(
host, username, password, port=port, token=token)