Skip to content

Commit

Permalink
Make ip_address read only
Browse files Browse the repository at this point in the history
Setting a different value for ip_address
and hostname does not work with the current
way we create receptor certs.
  • Loading branch information
fosterseth committed Aug 17, 2023
1 parent e84c02b commit a352ed5
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 36 deletions.
11 changes: 1 addition & 10 deletions awx/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -5551,15 +5551,6 @@ def validate_hostname(self, value):

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
Expand Down
1 change: 1 addition & 0 deletions awx/api/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ def update_raw_data(self, data):
# 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):
Expand Down
3 changes: 1 addition & 2 deletions awx/api/views/instance_install_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions awx/main/tasks/receptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 1 addition & 22 deletions awx/main/tests/functional/api/test_instance_peers.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,6 @@ def test_disallow_changing_hostname(self, admin_user, patch):
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
Expand Down Expand Up @@ -214,7 +205,7 @@ def test_control_node_retains_other_peers(self, node_type):
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)

Expand Down Expand Up @@ -265,18 +256,6 @@ def test_group_vars(self, get, admin_user):
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
Expand Down

0 comments on commit a352ed5

Please sign in to comment.