def forwards(self, orm): "Write your forwards methods here." for credential in orm.Credential.objects.all(): for field in ('ssh_password', 'ssh_key_data', 'ssh_key_unlock', 'sudo_password'): setattr( credential, field, encrypt_field(credential, field, field != 'ssh_key_data')) credential.save() for project in orm.Project.objects.all(): for field in ('scm_password', 'scm_key_data', 'scm_key_unlock'): setattr(project, field, encrypt_field(project, field, field != 'scm_key_data')) project.save()
def test_job_dependency_with_already_updated(controlplane_instance_group, job_template_factory, mocker, inventory_source_factory): objects = job_template_factory('jt', organization='org1', project='proj', inventory='inv', credential='cred') instance = controlplane_instance_group.instances.all()[0] j = create_job(objects.job_template, dependencies_processed=False) i = objects.inventory ii = inventory_source_factory("ec2") ii.source = "ec2" ii.update_on_launch = True ii.update_cache_timeout = 0 ii.save() i.inventory_sources.add(ii) j.start_args = json.dumps(dict(inventory_sources_already_updated=[ii.id])) j.save() j.start_args = encrypt_field(j, field_name="start_args") j.save() with mock.patch("awx.main.scheduler.TaskManager.start_task"): dm = DependencyManager() with mock.patch.object(DependencyManager, "create_inventory_update", wraps=dm.create_inventory_update) as mock_iu: dm.schedule() mock_iu.assert_not_called() with mock.patch("awx.main.scheduler.TaskManager.start_task"): TaskManager().schedule() TaskManager.start_task.assert_called_once_with( j, controlplane_instance_group, [], instance)
def save(self, *args, **kwargs): encrypted = settings_registry.is_setting_encrypted(self.key) new_instance = not bool(self.pk) # If update_fields has been specified, add our field names to it, # if it hasn't been specified, then we're just doing a normal save. update_fields = kwargs.get('update_fields', []) # When first saving to the database, don't store any encrypted field # value, but instead save it until after the instance is created. # Otherwise, store encrypted value to the database. if encrypted: if new_instance: self._saved_value = self.value self.value = '' else: self.value = encrypt_field(self, 'value') if 'value' not in update_fields: update_fields.append('value') super(Setting, self).save(*args, **kwargs) # After saving a new instance for the first time, set the encrypted # field and save again. if encrypted and new_instance: from awx.main.signals import disable_activity_stream with disable_activity_stream(): self.value = self._saved_value self.save(update_fields=['value'])
def save(self, *args, **kwargs): new_instance = not bool(self.pk) update_fields = kwargs.get('update_fields', []) for field in filter( lambda x: self.notification_class.init_parameters[x]['type'] == "password", self.notification_class.init_parameters): if self.notification_configuration[field].startswith( "$encrypted$"): continue if new_instance: value = self.notification_configuration[field] setattr(self, '_saved_{}_{}'.format("config", field), value) self.notification_configuration[field] = '' else: encrypted = encrypt_field(self, 'notification_configuration', subfield=field, skip_utf8=True) self.notification_configuration[field] = encrypted if 'notification_configuration' not in update_fields: update_fields.append('notification_configuration') super(NotificationTemplate, self).save(*args, **kwargs) if new_instance: update_fields = [] for field in filter( lambda x: self.notification_class.init_parameters[x][ 'type'] == "password", self.notification_class.init_parameters): saved_value = getattr(self, '_saved_{}_{}'.format("config", field), '') self.notification_configuration[field] = saved_value if 'notification_configuration' not in update_fields: update_fields.append('notification_configuration') self.save(update_fields=update_fields)
def test_job_dependency_with_already_updated(default_instance_group, job_template_factory, mocker, inventory_source_factory): objects = job_template_factory('jt', organization='org1', project='proj', inventory='inv', credential='cred', jobs=["job_should_start"]) instance = default_instance_group.instances.all()[0] j = objects.jobs["job_should_start"] j.status = 'pending' j.save() i = objects.inventory ii = inventory_source_factory("ec2") ii.source = "ec2" ii.update_on_launch = True ii.update_cache_timeout = 0 ii.save() i.inventory_sources.add(ii) j.start_args = json.dumps(dict(inventory_sources_already_updated=[ii.id])) j.save() j.start_args = encrypt_field(j, field_name="start_args") j.save() with mock.patch("awx.main.scheduler.TaskManager.start_task"): tm = TaskManager() with mock.patch.object(TaskManager, "create_inventory_update", wraps=tm.create_inventory_update) as mock_iu: tm.schedule() mock_iu.assert_not_called() with mock.patch("awx.main.scheduler.TaskManager.start_task"): TaskManager().schedule() TaskManager.start_task.assert_called_once_with(j, default_instance_group, [], instance)
def save(self, *args, **kwargs): new_instance = not bool(self.pk) update_fields = kwargs.get('update_fields', []) # preserve existing notification messages if not overwritten by new messages if not new_instance: old_nt = NotificationTemplate.objects.get(pk=self.id) old_messages = old_nt.messages new_messages = self.messages def merge_messages(local_old_messages, local_new_messages, local_event): if local_new_messages.get(local_event, {}) and local_old_messages.get(local_event, {}): local_old_event_msgs = local_old_messages[local_event] local_new_event_msgs = local_new_messages[local_event] for msg_type in ['message', 'body']: if msg_type not in local_new_event_msgs and local_old_event_msgs.get(msg_type, None): local_new_event_msgs[msg_type] = local_old_event_msgs[msg_type] if old_messages is not None and new_messages is not None: for event in ('started', 'success', 'error', 'workflow_approval'): if not new_messages.get(event, {}) and old_messages.get(event, {}): new_messages[event] = old_messages[event] continue if event == 'workflow_approval' and old_messages.get('workflow_approval', None): new_messages.setdefault('workflow_approval', {}) for subevent in ('running', 'approved', 'timed_out', 'denied'): old_wfa_messages = old_messages['workflow_approval'] new_wfa_messages = new_messages['workflow_approval'] if not new_wfa_messages.get(subevent, {}) and old_wfa_messages.get(subevent, {}): new_wfa_messages[subevent] = old_wfa_messages[subevent] continue if old_wfa_messages: merge_messages(old_wfa_messages, new_wfa_messages, subevent) else: merge_messages(old_messages, new_messages, event) new_messages.setdefault(event, None) for field in filter(lambda x: self.notification_class.init_parameters[x]['type'] == "password", self.notification_class.init_parameters): if self.notification_configuration[field].startswith("$encrypted$"): continue if new_instance: value = self.notification_configuration[field] setattr(self, '_saved_{}_{}'.format("config", field), value) self.notification_configuration[field] = '' else: encrypted = encrypt_field(self, 'notification_configuration', subfield=field) self.notification_configuration[field] = encrypted if 'notification_configuration' not in update_fields: update_fields.append('notification_configuration') super(NotificationTemplate, self).save(*args, **kwargs) if new_instance: update_fields = [] for field in filter(lambda x: self.notification_class.init_parameters[x]['type'] == "password", self.notification_class.init_parameters): saved_value = getattr(self, '_saved_{}_{}'.format("config", field), '') self.notification_configuration[field] = saved_value if 'notification_configuration' not in update_fields: update_fields.append('notification_configuration') self.save(update_fields=update_fields)
def encrypt_field(self, field, ask): if field not in self.inputs: return None encrypted = encrypt_field(self, field, ask=ask) if encrypted: self.inputs[field] = encrypted elif field in self.inputs: del self.inputs[field]
def encrypt_field(self, field, ask): encrypted = encrypt_field(self, field, ask=ask) if encrypted: self.inputs[field] = encrypted elif field in self.inputs: del self.inputs[field]
def encrypt_field(self, field, ask): encrypted = encrypt_field(self, field, ask) setattr(self, field, encrypted)