def _set_duration(self, cert): """Set certificate duration; return certificate object.""" notBefore = m2.x509_get_not_before(cert.x509) notAfter = m2.x509_get_not_after(cert.x509) days = self.signca.ca_def_duration common.is_number(days) m2.x509_gmtime_adj(notBefore, 0) m2.x509_gmtime_adj(notAfter, 60*60*24*int(days)) return cert
def _set_duration(self, cert): """Set certificate duration; return certificate object.""" notBefore = m2.x509_get_not_before(cert.x509) notAfter = m2.x509_get_not_after(cert.x509) days = self.signca.ca_def_duration common.is_number(days) m2.x509_gmtime_adj(notBefore, 0) m2.x509_gmtime_adj(notAfter, 60 * 60 * 24 * int(days)) return cert
def _reserve_ip(self, number): """Reserve an IP from the pool of free IPs""" common.is_number(number) number = int(number) if number > self.KV.scard(self.kv_free): msg = 'Fewer than %s free IP addresses in the subnet store %s' % \ (number, self.kv_free) raise error.InsufficientResource(msg) offer = [] for i in range(number): ip = self.KV.spop(self.kv_free) offer.append(ip) self.KV.sadd(self.kv_aloc, ip) return offer
def create(self, subnet, mask, start_ip=None, stop_ip=None): """Create a DHCP subnet; return DHCP subnet objects.""" subnet = common.validate_ip_address(subnet) filter = 'cn=%s' % subnet if not common.is_number(mask): msg = 'Subnet mask must be an integer, dotted decimal (or any other\ notation) is not allowed' self.log.error(msg) raise error.InputError(msg) mask = str(mask) if start_ip and not stop_ip: msg = 'A range must include a start and stop IP address' self.log.error(msg) raise error.InputError(msg) if start_ip is not None: start_ip = common.validate_ip_address(start_ip) stop_ip = common.validate_ip_address(stop_ip) start_ip, stop_ip = self._is_greater_ip_than(start_ip, stop_ip) dn = 'cn=%s,%s' % (subnet, self.dhcp_service_dn) dn_attr = {'objectClass': ['top', self.dhcp_subnet_class], 'cn': [subnet], 'dhcpNetMask': [mask]} if start_ip is not None: dn_attr['dhcpRange'] = start_ip + ' ' + stop_ip dn_info = [(k, v) for (k, v) in dn_attr.items()] result = self._create_object(dn, dn_info) self.log.debug('Result: %s' % result) return result
def modify(self, increment=1, get_mac=False): """Increment initial UUID object; return list of UUIDs.""" if not common.is_number(increment): msg = 'increment input must be an Integer' raise error.InputError, msg dn = self.next_uuid_dn filter = '%s=*' % self.next_uuid_attr #MMB changed attrs_only to unique result = self._get_object(dn, 0, filter, self.retrieve_attr, unique=True) if result['data'] == []: msg = "Cannot locate a UUID; maybe you need to run create?" raise error.NotFound(msg) uuid = int(result['data'][0][1][self.next_uuid_attr][0]) new_uuid = int(uuid) + int(increment) old_attrs = result['data'][0][1] new_attrs = old_attrs.copy() new_attrs[self.next_uuid_attr] = [str(new_uuid)] self.log.debug('Modifying %s from %s to %s' % (dn, old_attrs, new_attrs)) try: result = self._modify_attributes(dn, new_attrs, old_attrs) except: msg = 'Update of next free UUID to %d failed' % new_uuid raise error.IncrementError, msg mac = [] for item in range(uuid, new_uuid): mac.append(common.mac_from_uuid(item,0)) if get_mac: result['data'] = (range(uuid, (new_uuid)), mac) else: result['data'] = range(uuid, (new_uuid)) result['msg'] = "Reserved UUIDs: " + str(result['data']) self.log.debug('Result: %s' % result) return result
def create(self, uuid_start=None, get_mac=False): """Create initial UUID object; return True.""" if uuid_start: self.next_uuid_start = uuid_start if not common.is_number(self.next_uuid_start): msg = 'next_uuid_start must be an integer' raise error.InputError(msg) # Now we know it's an integer, make it a string for LDAP's sake self.next_uuid_start = str(self.next_uuid_start) dn = self.next_uuid_dn dn_info = [] if not self.next_uuid_class in self.next_uuid_classes: dn_info.append((0, 'objectClass', self.next_uuid_class)) if not self.next_uuid_attr in self.next_uuid_attrs: dn_info.append((0, self.next_uuid_attr, self.next_uuid_start)) if dn_info == []: msg = 'UUID entry already set on %s.' % dn raise error.AlreadyExists(msg) self.log.debug('Adding %s to %s ' % (dn_info, dn)) result = self._create_object(dn, dn_info) uuid = int(result['data'][0][1][self.next_uuid_attr][0]) mac = common.mac_from_uuid(uuid, 0) if get_mac: result['data'] = (uuid, mac) else: result['data'] = [uuid] result['msg'] = 'Created UUID:' self.log.debug('Result: %s' % result) return result