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.
This commit is contained in:
Seth Foster 2023-08-17 15:41:04 -04:00 committed by Seth Foster
parent eba130cf41
commit 965127637b
5 changed files with 5 additions and 36 deletions

View File

@ -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

View File

@ -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):

View File

@ -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)

View File

@ -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

View File

@ -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