From 965127637ba241183ce10e6f9ab7b213aa5dcd47 Mon Sep 17 00:00:00 2001 From: Seth Foster Date: Thu, 17 Aug 2023 15:41:04 -0400 Subject: [PATCH] Make ip_address read only Setting a different value for ip_address and hostname does not work with the current way we create receptor certs. --- awx/api/serializers.py | 11 +-------- awx/api/views/__init__.py | 1 + awx/api/views/instance_install_bundle.py | 3 +-- awx/main/tasks/receptor.py | 3 +-- .../functional/api/test_instance_peers.py | 23 +------------------ 5 files changed, 5 insertions(+), 36 deletions(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 3e4611f743..a373d59c51 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -5386,7 +5386,7 @@ class InstanceSerializer(BaseSerializer): class Meta: model = Instance - read_only_fields = ('uuid', 'version') + read_only_fields = ('ip_address', 'uuid', 'version') fields = ( 'id', 'hostname', @@ -5551,15 +5551,6 @@ class InstanceSerializer(BaseSerializer): return value - def validate_ip_address(self, value): - """ - Cannot change ip address - """ - if self.instance and self.instance.ip_address != value: - raise serializers.ValidationError(_("Cannot change ip_address.")) - - return value - def validate_listener_port(self, value): """ Cannot change listener port, unless going from none to integer, and vice versa diff --git a/awx/api/views/__init__.py b/awx/api/views/__init__.py index 3189bde1e3..0511b69664 100644 --- a/awx/api/views/__init__.py +++ b/awx/api/views/__init__.py @@ -343,6 +343,7 @@ class InstanceDetail(RetrieveUpdateAPIView): # these fields are only valid on creation of an instance, so they unwanted on detail view data.pop('node_type', None) data.pop('hostname', None) + data.pop('ip_address', None) return super(InstanceDetail, self).update_raw_data(data) def update(self, request, *args, **kwargs): diff --git a/awx/api/views/instance_install_bundle.py b/awx/api/views/instance_install_bundle.py index 518b43e49e..9ae7f7c460 100644 --- a/awx/api/views/instance_install_bundle.py +++ b/awx/api/views/instance_install_bundle.py @@ -126,8 +126,7 @@ def generate_inventory_yml(instance_obj): def generate_group_vars_all_yml(instance_obj): peers = [] for instance in instance_obj.peers.all(): - host_or_ip = instance.ip_address or instance.hostname - peers.append(dict(host=host_or_ip, port=instance.listener_port)) + peers.append(dict(host=instance.hostname, port=instance.listener_port)) all_yaml = render_to_string("instance_install_bundle/group_vars/all.yml", context=dict(instance=instance_obj, peers=peers)) # convert consecutive newlines with a single newline return re.sub(r'\n+', '\n', all_yaml) diff --git a/awx/main/tasks/receptor.py b/awx/main/tasks/receptor.py index 073d47bd81..32c8e325ad 100644 --- a/awx/main/tasks/receptor.py +++ b/awx/main/tasks/receptor.py @@ -703,8 +703,7 @@ def generate_config_data(): receptor_config = list(RECEPTOR_CONFIG_STARTER) for instance in instances: - host_or_ip = instance.ip_address or instance.hostname - peer = {'tcp-peer': {'address': f'{host_or_ip}:{instance.listener_port}', 'tls': 'tlsclient'}} + peer = {'tcp-peer': {'address': f'{instance.hostname}:{instance.listener_port}', 'tls': 'tlsclient'}} receptor_config.append(peer) should_update = should_update_config(instances) return receptor_config, should_update diff --git a/awx/main/tests/functional/api/test_instance_peers.py b/awx/main/tests/functional/api/test_instance_peers.py index ce15d9f2d9..21eabb9176 100644 --- a/awx/main/tests/functional/api/test_instance_peers.py +++ b/awx/main/tests/functional/api/test_instance_peers.py @@ -166,15 +166,6 @@ class TestPeers: expect=400, ) - def test_disallow_changing_ip_address(self, admin_user, patch): - hop = Instance.objects.create(hostname='hop', ip_address='10.10.10.10', node_type='hop') - patch( - url=reverse('api:instance_detail', kwargs={'pk': hop.pk}), - data={"ip_address": "12.12.12.12"}, - user=admin_user, - expect=400, - ) - def test_disallow_changing_node_state(self, admin_user, patch): ''' only allow setting to deprovisioning @@ -214,7 +205,7 @@ class TestPeers: if a new node comes online, other peer relationships should remain intact ''' - hop1 = Instance.objects.create(hostname='hop1', ip_address="10.10.10.10", node_type='hop', listener_port=6789, peers_from_control_nodes=True) + hop1 = Instance.objects.create(hostname='hop1', node_type='hop', listener_port=6789, peers_from_control_nodes=True) hop2 = Instance.objects.create(hostname='hop2', node_type='hop', listener_port=6789, peers_from_control_nodes=False) hop1.peers.add(hop2) @@ -265,18 +256,6 @@ class TestPeers: assert not has_peer(execution_vars, 'hop1:6789') assert execution_vars.get('receptor_listener', False) - def test_group_vars_ip_address_over_hostname(self): - ''' - test that ip_address has precedence over hostname in group_vars all.yml - ''' - hop1 = Instance.objects.create(hostname='hop1', node_type='hop', listener_port=6789, peers_from_control_nodes=True) - hop2 = Instance.objects.create(hostname='hop2', ip_address="10.10.10.10", node_type='hop', listener_port=6789, peers_from_control_nodes=False) - hop1.peers.add(hop2) - - hop1_vars = yaml.safe_load(generate_group_vars_all_yml(hop1)) - - assert has_peer(hop1_vars, "10.10.10.10:6789") - def test_write_receptor_config_called(self): ''' Assert that write_receptor_config is called