Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def hosts_by_filter(ldap_filter):
"""Return a list of hosts satisfying the LDAP filter.
The list returned contains a dictionary of LDAP attributes for each host.
"""
with ldap.ldap_ocf() as c:
c.search(
OCF_LDAP_HOSTS,
ldap_filter,
attributes=ldap3.ALL_ATTRIBUTES,
)
return [entry['attributes'] for entry in c.response]
def _get_first_available_uid(known_uid=_KNOWN_UID):
"""Return the first available UID number.
Searches our entire People ou in order to find it. It seems like there
should be a better way to do this, but quick searches don't show any.
We hard-code a value we know has already been reached and only select
entries greater than that for performance. This value can then be cached
and passed back in to make subsequent calls faster.
"""
with ldap_ocf() as c:
c.search(
OCF_LDAP_PEOPLE,
'(uidNumber>={KNOWN_MIN})'.format(KNOWN_MIN=known_uid),
attributes=['uidNumber'],
)
uids = [int(entry['attributes']['uidNumber']) for entry in c.response]
if uids:
max_uid = max(uids)
else:
# If cached UID is later deleted, LDAP response will be empty.
max_uid = known_uid
assert all(start <= end for start, end in RESERVED_UID_RANGES)
next_uid = max_uid + 1
for start, end in sorted(RESERVED_UID_RANGES):
if start <= next_uid <= end: