[209] | 1 | #!/usr/bin/python |
---|
| 2 | |
---|
[411] | 3 | import cache_acls |
---|
[209] | 4 | import getafsgroups |
---|
| 5 | import re |
---|
| 6 | import string |
---|
[1473] | 7 | import dns.resolver |
---|
[865] | 8 | from invirt.database import Machine, NIC, Type, Disk, CDROM, Autoinstall |
---|
[879] | 9 | from invirt.config import structs as config |
---|
[1612] | 10 | from invirt.common import InvalidInput |
---|
[209] | 11 | |
---|
| 12 | MAX_MEMORY_TOTAL = 512 |
---|
| 13 | MAX_MEMORY_SINGLE = 256 |
---|
| 14 | MIN_MEMORY_SINGLE = 16 |
---|
| 15 | MAX_DISK_TOTAL = 50 |
---|
| 16 | MAX_DISK_SINGLE = 50 |
---|
| 17 | MIN_DISK_SINGLE = 0.1 |
---|
| 18 | MAX_VMS_TOTAL = 10 |
---|
| 19 | MAX_VMS_ACTIVE = 4 |
---|
| 20 | |
---|
[572] | 21 | class Validate: |
---|
[609] | 22 | def __init__(self, username, state, machine_id=None, name=None, description=None, owner=None, |
---|
[572] | 23 | admin=None, contact=None, memory=None, disksize=None, |
---|
[629] | 24 | vmtype=None, cdrom=None, autoinstall=None, strict=False): |
---|
[572] | 25 | # XXX Successive quota checks aren't a good idea, since you |
---|
| 26 | # can't necessarily change the locker and disk size at the |
---|
| 27 | # same time. |
---|
| 28 | created_new = (machine_id is None) |
---|
| 29 | |
---|
[577] | 30 | if strict: |
---|
| 31 | if name is None: |
---|
| 32 | raise InvalidInput('name', name, "You must provide a machine name.") |
---|
[609] | 33 | if description is None: |
---|
| 34 | raise InvalidInput('description', description, "You must provide a description.") |
---|
[577] | 35 | if memory is None: |
---|
| 36 | raise InvalidInput('memory', memory, "You must provide a memory size.") |
---|
| 37 | if disksize is None: |
---|
| 38 | raise InvalidInput('disk', disksize, "You must provide a disk size.") |
---|
| 39 | |
---|
[572] | 40 | if machine_id is not None: |
---|
[632] | 41 | self.machine = testMachineId(username, state, machine_id) |
---|
[572] | 42 | machine = getattr(self, 'machine', None) |
---|
| 43 | |
---|
| 44 | owner = testOwner(username, owner, machine) |
---|
| 45 | if owner is not None: |
---|
| 46 | self.owner = owner |
---|
[1709] | 47 | self.admin = testAdmin(username, admin, machine) |
---|
[572] | 48 | contact = testContact(username, contact, machine) |
---|
| 49 | if contact is not None: |
---|
| 50 | self.contact = contact |
---|
| 51 | name = testName(username, name, machine) |
---|
| 52 | if name is not None: |
---|
| 53 | self.name = name |
---|
[609] | 54 | description = testDescription(username, description, machine) |
---|
| 55 | if description is not None: |
---|
| 56 | self.description = description |
---|
[572] | 57 | if memory is not None: |
---|
| 58 | self.memory = validMemory(self.owner, state, memory, machine, |
---|
| 59 | on=not created_new) |
---|
| 60 | if disksize is not None: |
---|
[632] | 61 | self.disksize = validDisk(self.owner, state, disksize, machine) |
---|
[572] | 62 | if vmtype is not None: |
---|
| 63 | self.vmtype = validVmType(vmtype) |
---|
| 64 | if cdrom is not None: |
---|
[1013] | 65 | if not CDROM.query().get(cdrom): |
---|
[572] | 66 | raise CodeError("Invalid cdrom type '%s'" % cdrom) |
---|
| 67 | self.cdrom = cdrom |
---|
[629] | 68 | if autoinstall is not None: |
---|
[1013] | 69 | self.autoinstall = Autoinstall.query().get(autoinstall) |
---|
[572] | 70 | |
---|
| 71 | |
---|
| 72 | def getMachinesByOwner(owner, machine=None): |
---|
[209] | 73 | """Return the machines owned by the same as a machine. |
---|
[440] | 74 | |
---|
[209] | 75 | If the machine is None, return the machines owned by the same |
---|
| 76 | user. |
---|
| 77 | """ |
---|
| 78 | if machine: |
---|
| 79 | owner = machine.owner |
---|
[1001] | 80 | return Machine.query().filter_by(owner=owner) |
---|
[209] | 81 | |
---|
[572] | 82 | def maxMemory(owner, g, machine=None, on=True): |
---|
[209] | 83 | """Return the maximum memory for a machine or a user. |
---|
| 84 | |
---|
[440] | 85 | If machine is None, return the memory available for a new |
---|
[209] | 86 | machine. Else, return the maximum that machine can have. |
---|
| 87 | |
---|
| 88 | on is whether the machine should be turned on. If false, the max |
---|
| 89 | memory for the machine to change to, if it is left off, is |
---|
| 90 | returned. |
---|
| 91 | """ |
---|
[251] | 92 | if machine is not None and machine.memory > MAX_MEMORY_SINGLE: |
---|
[250] | 93 | # If they've been blessed, let them have it |
---|
| 94 | return machine.memory |
---|
[253] | 95 | if not on: |
---|
| 96 | return MAX_MEMORY_SINGLE |
---|
[572] | 97 | machines = getMachinesByOwner(owner, machine) |
---|
[575] | 98 | active_machines = [m for m in machines if m.name in g.xmlist_raw] |
---|
[209] | 99 | mem_usage = sum([x.memory for x in active_machines if x != machine]) |
---|
| 100 | return min(MAX_MEMORY_SINGLE, MAX_MEMORY_TOTAL-mem_usage) |
---|
| 101 | |
---|
[572] | 102 | def maxDisk(owner, machine=None): |
---|
[440] | 103 | """Return the maximum disk that a machine can reach. |
---|
| 104 | |
---|
| 105 | If machine is None, the maximum disk for a new machine. Otherwise, |
---|
| 106 | return the maximum that a given machine can be changed to. |
---|
| 107 | """ |
---|
[535] | 108 | if machine is not None: |
---|
| 109 | machine_id = machine.machine_id |
---|
| 110 | else: |
---|
| 111 | machine_id = None |
---|
[1001] | 112 | disk_usage = Disk.query().filter(Disk.c.machine_id != machine_id).\ |
---|
| 113 | join('machine').\ |
---|
| 114 | filter_by(owner=owner).sum(Disk.c.size) or 0 |
---|
[209] | 115 | return min(MAX_DISK_SINGLE, MAX_DISK_TOTAL-disk_usage/1024.) |
---|
| 116 | |
---|
[572] | 117 | def cantAddVm(owner, g): |
---|
| 118 | machines = getMachinesByOwner(owner) |
---|
[575] | 119 | active_machines = [m for m in machines if m.name in g.xmlist_raw] |
---|
[1001] | 120 | if machines.count() >= MAX_VMS_TOTAL: |
---|
[209] | 121 | return 'You have too many VMs to create a new one.' |
---|
| 122 | if len(active_machines) >= MAX_VMS_ACTIVE: |
---|
| 123 | return ('You already have the maximum number of VMs turned on. ' |
---|
| 124 | 'To create more, turn one off.') |
---|
| 125 | return False |
---|
| 126 | |
---|
[632] | 127 | def haveAccess(user, state, machine): |
---|
[235] | 128 | """Return whether a user has administrative access to a machine""" |
---|
[874] | 129 | return (user in cache_acls.accessList(machine) |
---|
| 130 | or (machine.adminable and state.isadmin)) |
---|
[209] | 131 | |
---|
| 132 | def owns(user, machine): |
---|
| 133 | """Return whether a user owns a machine""" |
---|
[411] | 134 | return user in expandLocker(machine.owner) |
---|
[209] | 135 | |
---|
| 136 | def validMachineName(name): |
---|
| 137 | """Check that name is valid for a machine name""" |
---|
| 138 | if not name: |
---|
| 139 | return False |
---|
[648] | 140 | charset = string.lowercase + string.digits + '-' |
---|
| 141 | if '-' in (name[0], name[-1]) or len(name) > 63: |
---|
[209] | 142 | return False |
---|
| 143 | for x in name: |
---|
| 144 | if x not in charset: |
---|
| 145 | return False |
---|
| 146 | return True |
---|
| 147 | |
---|
[572] | 148 | def validMemory(owner, g, memory, machine=None, on=True): |
---|
| 149 | """Parse and validate limits for memory for a given owner and machine. |
---|
[209] | 150 | |
---|
| 151 | on is whether the memory must be valid after the machine is |
---|
| 152 | switched on. |
---|
| 153 | """ |
---|
| 154 | try: |
---|
| 155 | memory = int(memory) |
---|
| 156 | if memory < MIN_MEMORY_SINGLE: |
---|
| 157 | raise ValueError |
---|
| 158 | except ValueError: |
---|
[440] | 159 | raise InvalidInput('memory', memory, |
---|
[211] | 160 | "Minimum %s MiB" % MIN_MEMORY_SINGLE) |
---|
[572] | 161 | max_val = maxMemory(owner, g, machine, on) |
---|
[867] | 162 | if not g.isadmin and memory > max_val: |
---|
[209] | 163 | raise InvalidInput('memory', memory, |
---|
[572] | 164 | 'Maximum %s MiB for %s' % (max_val, owner)) |
---|
[209] | 165 | return memory |
---|
| 166 | |
---|
[632] | 167 | def validDisk(owner, g, disk, machine=None): |
---|
[572] | 168 | """Parse and validate limits for disk for a given owner and machine.""" |
---|
[209] | 169 | try: |
---|
| 170 | disk = float(disk) |
---|
[867] | 171 | if not g.isadmin and disk > maxDisk(owner, machine): |
---|
[209] | 172 | raise InvalidInput('disk', disk, |
---|
[572] | 173 | "Maximum %s G" % maxDisk(owner, machine)) |
---|
[209] | 174 | disk = int(disk * 1024) |
---|
| 175 | if disk < MIN_DISK_SINGLE * 1024: |
---|
| 176 | raise ValueError |
---|
| 177 | except ValueError: |
---|
| 178 | raise InvalidInput('disk', disk, |
---|
[211] | 179 | "Minimum %s GiB" % MIN_DISK_SINGLE) |
---|
[209] | 180 | return disk |
---|
[437] | 181 | |
---|
| 182 | def validVmType(vm_type): |
---|
[440] | 183 | if vm_type is None: |
---|
| 184 | return None |
---|
[1013] | 185 | t = Type.query().get(vm_type) |
---|
[440] | 186 | if t is None: |
---|
[437] | 187 | raise CodeError("Invalid vm type '%s'" % vm_type) |
---|
[440] | 188 | return t |
---|
[437] | 189 | |
---|
[632] | 190 | def testMachineId(user, state, machine_id, exists=True): |
---|
[209] | 191 | """Parse, validate and check authorization for a given user and machine. |
---|
| 192 | |
---|
| 193 | If exists is False, don't check that it exists. |
---|
| 194 | """ |
---|
| 195 | if machine_id is None: |
---|
[440] | 196 | raise InvalidInput('machine_id', machine_id, |
---|
[209] | 197 | "Must specify a machine ID.") |
---|
| 198 | try: |
---|
| 199 | machine_id = int(machine_id) |
---|
| 200 | except ValueError: |
---|
| 201 | raise InvalidInput('machine_id', machine_id, "Must be an integer.") |
---|
[1013] | 202 | machine = Machine.query().get(machine_id) |
---|
[209] | 203 | if exists and machine is None: |
---|
| 204 | raise InvalidInput('machine_id', machine_id, "Does not exist.") |
---|
[632] | 205 | if machine is not None and not haveAccess(user, state, machine): |
---|
[209] | 206 | raise InvalidInput('machine_id', machine_id, |
---|
| 207 | "You do not have access to this machine.") |
---|
| 208 | return machine |
---|
| 209 | |
---|
| 210 | def testAdmin(user, admin, machine): |
---|
[411] | 211 | """Determine whether a user can set the admin of a machine to this value. |
---|
| 212 | |
---|
| 213 | Return the value to set the admin field to (possibly 'system:' + |
---|
| 214 | admin). XXX is modifying this a good idea? |
---|
| 215 | """ |
---|
[575] | 216 | if admin is None: |
---|
[209] | 217 | return None |
---|
[575] | 218 | if machine is not None and admin == machine.administrator: |
---|
[1709] | 219 | return admin |
---|
[228] | 220 | if admin == user: |
---|
[209] | 221 | return admin |
---|
[411] | 222 | if ':' not in admin: |
---|
| 223 | if cache_acls.isUser(admin): |
---|
| 224 | return admin |
---|
| 225 | admin = 'system:' + admin |
---|
[413] | 226 | try: |
---|
[879] | 227 | if user in getafsgroups.getAfsGroupMembers(admin, config.authz[0].cell): |
---|
[413] | 228 | return admin |
---|
| 229 | except getafsgroups.AfsProcessError, e: |
---|
[431] | 230 | errmsg = str(e) |
---|
| 231 | if errmsg.startswith("pts: User or group doesn't exist"): |
---|
| 232 | errmsg = 'The group "%s" does not exist.' % admin |
---|
| 233 | raise InvalidInput('administrator', admin, errmsg) |
---|
[413] | 234 | #XXX Should we require that user is in the admin group? |
---|
[209] | 235 | return admin |
---|
[440] | 236 | |
---|
[228] | 237 | def testOwner(user, owner, machine=None): |
---|
[411] | 238 | """Determine whether a user can set the owner of a machine to this value. |
---|
| 239 | |
---|
| 240 | If machine is None, this is the owner of a new machine. |
---|
| 241 | """ |
---|
[572] | 242 | if owner == user: |
---|
[228] | 243 | return owner |
---|
[572] | 244 | if machine is not None and owner in (machine.owner, None): |
---|
[573] | 245 | return machine.owner |
---|
[228] | 246 | if owner is None: |
---|
| 247 | raise InvalidInput('owner', owner, "Owner must be specified") |
---|
[411] | 248 | try: |
---|
| 249 | if user not in cache_acls.expandLocker(owner): |
---|
| 250 | raise InvalidInput('owner', owner, 'You do not have access to the ' |
---|
| 251 | + owner + ' locker') |
---|
| 252 | except getafsgroups.AfsProcessError, e: |
---|
| 253 | raise InvalidInput('owner', owner, str(e)) |
---|
| 254 | return owner |
---|
[209] | 255 | |
---|
| 256 | def testContact(user, contact, machine=None): |
---|
[576] | 257 | if contact is None or (machine is not None and contact == machine.contact): |
---|
[209] | 258 | return None |
---|
| 259 | if not re.match("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$", contact, re.I): |
---|
| 260 | raise InvalidInput('contact', contact, "Not a valid email.") |
---|
| 261 | return contact |
---|
| 262 | |
---|
| 263 | def testDisk(user, disksize, machine=None): |
---|
| 264 | return disksize |
---|
| 265 | |
---|
| 266 | def testName(user, name, machine=None): |
---|
[572] | 267 | if name is None: |
---|
[209] | 268 | return None |
---|
[647] | 269 | name = name.lower() |
---|
[572] | 270 | if machine is not None and name == machine.name: |
---|
| 271 | return None |
---|
[1473] | 272 | try: |
---|
[1492] | 273 | hostname = '%s.%s.' % (name, config.dns.domains[0]) |
---|
[1542] | 274 | resolver = dns.resolver.Resolver() |
---|
| 275 | resolver.nameservers = ['127.0.0.1'] |
---|
[1492] | 276 | try: |
---|
[1542] | 277 | resolver.query(hostname, 'A') |
---|
[1492] | 278 | except dns.resolver.NoAnswer, e: |
---|
| 279 | # If we can get the TXT record, then we can verify it's |
---|
| 280 | # reserved. If this lookup fails, let it bubble up and be |
---|
| 281 | # dealt with |
---|
[1542] | 282 | answer = resolver.query(hostname, 'TXT') |
---|
[1492] | 283 | txt = answer[0].strings[0] |
---|
[1495] | 284 | if txt.startswith('reserved'): |
---|
[1492] | 285 | raise InvalidInput('name', name, 'The name you have requested has been %s. For more information, contact us at %s' % (txt, config.dns.contact)) |
---|
| 286 | |
---|
[1473] | 287 | # If the hostname didn't exist, it would have thrown an |
---|
| 288 | # exception by now - error out |
---|
| 289 | raise InvalidInput('name', name, 'Name is already taken.') |
---|
| 290 | except dns.resolver.NXDOMAIN, e: |
---|
[572] | 291 | if not validMachineName(name): |
---|
[649] | 292 | raise InvalidInput('name', name, 'You must provide a machine name. Max 63 chars, alnum plus \'-\', does not begin or end with \'-\'.') |
---|
[209] | 293 | return name |
---|
[1473] | 294 | except InvalidInput: |
---|
| 295 | raise |
---|
| 296 | except: |
---|
| 297 | # Any other error is a validation failure |
---|
| 298 | raise InvalidInput('name', name, 'We were unable to verify that this name is available. If you believe this is in error, please contact us at %s' % config.dns.contact) |
---|
[209] | 299 | |
---|
[609] | 300 | def testDescription(user, description, machine=None): |
---|
| 301 | if description is None or description.strip() == '': |
---|
| 302 | return None |
---|
| 303 | return description.strip() |
---|
| 304 | |
---|
[209] | 305 | def testHostname(user, hostname, machine): |
---|
| 306 | for nic in machine.nics: |
---|
| 307 | if hostname == nic.hostname: |
---|
| 308 | return hostname |
---|
| 309 | # check if doesn't already exist |
---|
| 310 | if NIC.select_by(hostname=hostname): |
---|
| 311 | raise InvalidInput('hostname', hostname, |
---|
| 312 | "Already exists") |
---|
| 313 | if not re.match("^[A-Z0-9-]{1,22}$", hostname, re.I): |
---|
| 314 | raise InvalidInput('hostname', hostname, "Not a valid hostname; " |
---|
| 315 | "must only use number, letters, and dashes.") |
---|
| 316 | return hostname |
---|