Revision 30e0ed74 snf-cyclades-app/synnefo/db/models.py

b/snf-cyclades-app/synnefo/db/models.py
37 37
from synnefo.api.faults import ServiceUnavailable
38 38
from synnefo.util.rapi import GanetiRapiClient
39 39
from synnefo import settings as snf_settings
40
from aes_encrypt import encrypt_db_charfield, decrypt_db_charfield
40 41

  
42
BACKEND_CLIENTS = {}  # {hash:Backend client}
43
BACKEND_HASHES = {}   # {Backend.id:hash}
41 44

  
42
BACKEND_CLIENTS = {}    #{hash:Backend client}
43
BACKEND_HASHES = {}     #{Backend.id:hash}
44 45

  
45 46
def get_client(hash, backend):
46 47
    """Get a cached backend client or create a new one.
......
59 60
    # Always get a new instance to ensure latest credentials
60 61
    if isinstance(backend, Backend):
61 62
        backend = backend.id
62
    (credentials,) = Backend.objects.filter(id=backend).values_list('hash',
63
                                'clustername', 'port', 'username', 'password')
64 63

  
65
    hash, clustername, port, user, password = credentials
64
    backend = Backend.objects.get(id=backend)
65
    hash = backend.hash
66
    clustername = backend.clustername
67
    port = backend.port
68
    user = backend.username
69
    password = backend.password
66 70

  
67 71
    # Check client for updated hash
68 72
    if hash in BACKEND_CLIENTS:
......
113 117
        for backend in self._clone():
114 118
            backend.delete()
115 119

  
120

  
116 121
class ProtectDeleteManager(models.Manager):
117 122
    def get_query_set(self):
118 123
        return BackendQuerySet(self.model, using=self._db)
......
123 128
    port = models.PositiveIntegerField('Port', default=5080)
124 129
    username = models.CharField('Username', max_length=64, blank=True,
125 130
                                null=True)
126
    password = models.CharField('Password', max_length=64, blank=True,
131
    password_hash = models.CharField('Password', max_length=64, blank=True,
127 132
                                null=True)
128 133
    # Sha1 is up to 40 characters long
129 134
    hash = models.CharField('Hash', max_length=40, editable=False, null=False)
......
168 173
                (self.clustername, self.port, self.username, self.password)) \
169 174
                .hexdigest()
170 175

  
176
    @property
177
    def password(self):
178
        return decrypt_db_charfield(self.password_hash)
179

  
180
    @password.setter
181
    def password(self, value):
182
        self.password_hash = encrypt_db_charfield(value)
183

  
171 184
    def save(self, *args, **kwargs):
172 185
        # Create a new hash each time a Backend is saved
173 186
        old_hash = self.hash

Also available in: Unified diff