terraform-hcloud-kube-hetzner icon indicating copy to clipboard operation
terraform-hcloud-kube-hetzner copied to clipboard

Add `myipv4` as possible firewall setting input

Open vsalomaki opened this issue 2 months ago • 35 comments

Description

Add ability to use the string "myipv4" as a source_ip or destination_ip in the firewall rules. Idea is that the current external IP of the TF-user is retrieved using dig -4 +short myip.opendns.com @resolver1.opendns.com and if the "myipv4" is used in any of the firewall ip-ranges, the string is replaced with the IP-address retrieve from opendns.

Gemini

Summary of Changes

This pull request significantly enhances firewall configuration by allowing users to specify a dynamic placeholder, "myipv4", in their firewall rules. This placeholder is automatically resolved to the user's current public IPv4 address using an external dig command, eliminating the need for manual updates when their IP changes. The change integrates a robust IP detection script and refactors firewall rule processing to ensure flexible and convenient management of network access.

Highlights

  • Dynamic Public IPv4 Resolution: Introduced the ability to use a configurable placeholder string (default "myipv4") in firewall rules, which is dynamically resolved to the user's current public IPv4 address using the dig command.
  • Firewall Rule Integration: The placeholder can be used in source_ips or destination_ips arrays within firewall_kube_api_source, firewall_ssh_source, and extra_firewall_rules, and is replaced with the detected IP in CIDR/32 format.
  • Robust IP Detection Script: A bash script executed via the external Terraform provider fetches the IPv4, includes fallbacks (OpenDNS, Google DNS), and performs strict validation to ensure a valid IP is returned. The script is only executed if the placeholder is present in firewall rules.
  • New Terraform Provider Dependency: The hashicorp/external Terraform provider (~> 2.0) is now a required dependency to enable external command execution for IP detection.
  • Refactored Firewall Rule Processing: The locals.tf file now includes logic to iterate through merged firewall rules, replacing the placeholder with the resolved public IP and handling cases where rules are null or the placeholder is not used.

Changelog

  • data.tf

    • Added data "external" "my_ip" block to fetch the current public IPv4 address.
    • Implemented a bash script within the external data source that uses dig with OpenDNS and Google DNS as fallbacks to retrieve the IPv4 address.
    • The script includes robust validation for dig command existence, empty results, and strict IPv4 address format, exiting with an error if validation fails.
    • The execution of this data source is conditional, only running if var.my_ipv4_ref is found in any firewall rule definitions.
  • kube.tf.example

    • Updated comments for firewall_kube_api_source, firewall_ssh_source, and extra_firewall_rules to document the usage of the "myipv4" placeholder and its dependency on the dig command.
    • Added an example demonstrating the use of "myipv4" in firewall_ssh_source.
  • locals.tf

    • Introduced a new local variable my_public_ipv4_cidr which constructs the CIDR block (e.g., 1.2.3.4/32) from the fetched IP, using try for robustness.
    • Refactored the firewall_rules_list logic to iterate through merged firewall rules and dynamically replace var.my_ipv4_ref in source_ips and destination_ips arrays with local.my_public_ipv4_cidr.
    • The replacement logic uses try and compact to ensure robustness, handling cases where source_ips or destination_ips might be null or where the placeholder should be filtered out if the IP is not fetched.
  • variables.tf

    • Updated the extra_firewall_rules variable type to a more specific object structure, including description, direction, protocol, port, source_ips, and destination_ips.
    • Added a validation block for extra_firewall_rules to ensure the direction is either 'in' or 'out'.
    • Added a new string variable my_ipv4_ref with a default value of "myipv4", allowing users to customize the placeholder string for their IPv4 address in firewall rules.
  • versions.tf

    • Added the hashicorp/external Terraform provider as a required dependency with a version constraint of ~> 2.0.

vsalomaki avatar Oct 11 '25 20:10 vsalomaki

Summary of Changes

Hello @vsalomaki, I'm Gemini Code Assist[^1]! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the flexibility of firewall configurations by allowing users to specify 'myipv4' as a source or destination IP. This placeholder is automatically resolved to the user's current public IPv4 address, eliminating the need for manual updates when their IP changes. The change integrates an external data source to fetch the IP and updates the firewall rule processing to perform the necessary substitution.

Highlights

  • Dynamic IP Detection: Introduced a mechanism to automatically detect the user's public IPv4 address using the dig command and the external Terraform provider.
  • Firewall Rule Placeholder: Added support for a new 'myipv4' placeholder string in firewall source/destination IP arrays, which will be dynamically replaced with the detected public IPv4 address in CIDR/32 format.
  • Enhanced Firewall Configuration: Modified the firewall rule processing logic to identify and substitute the 'myipv4' placeholder, simplifying the configuration for allowing access from the user's current IP.
  • New Provider Dependency: Included the hashicorp/external Terraform provider as a required dependency to enable the execution of external commands for IP detection.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

[^1]: Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

gemini-code-assist[bot] avatar Oct 11 '25 20:10 gemini-code-assist[bot]

/gemini review

vsalomaki avatar Oct 11 '25 20:10 vsalomaki

/gemini review

vsalomaki avatar Oct 11 '25 20:10 vsalomaki

/gemini review

vsalomaki avatar Oct 11 '25 20:10 vsalomaki

/gemini review

vsalomaki avatar Oct 11 '25 21:10 vsalomaki

/gemini summary

vsalomaki avatar Oct 11 '25 21:10 vsalomaki

Summary of Changes

This pull request significantly enhances the flexibility of firewall configurations by enabling the use of a dynamic 'myipv4' placeholder. This placeholder automatically resolves to the user's current public IPv4 address, eliminating the need for manual updates when their IP changes. The implementation integrates an external data source to fetch the IP and updates the firewall rule processing to perform the necessary substitution, making firewall management more convenient and robust.

Highlights

  • Dynamic Public IP Detection: Introduced a mechanism to automatically detect the user's public IPv4 address using the dig command and the external Terraform provider. This IP is then used in firewall rules.
  • Firewall Rule Placeholder: Added support for a new 'myipv4' placeholder string in firewall source_ips or destination_ips arrays. This placeholder is dynamically replaced with the detected public IPv4 address in CIDR/32 format.
  • Enhanced Firewall Configuration: Modified the firewall rule processing logic to identify and substitute the 'myipv4' placeholder, simplifying the configuration for allowing access from the user's current IP without manual updates.
  • New Provider Dependency: Included the hashicorp/external Terraform provider as a required dependency to enable the execution of external commands for IP detection.
  • Configurable IP Fetching: Added a new fetch_myip boolean variable (defaulting to true) to control whether the public IP address should be fetched and used in firewall rules.
Changelog
  • data.tf
    • Added a new data "external" "my_ipv4" block to fetch the current public IPv4 address using a bash script that executes dig against myip.opendns.com.
    • Implemented robust validation within the bash script to ensure the fetched value is a valid IPv4 address and handles potential errors gracefully.
    • The execution of this data source is conditional on the new var.fetch_myip.
  • kube.tf.example
    • Added a new commented variable fetch_myip with a default value of true.
    • Updated comments for firewall_kube_api_source, firewall_ssh_source, and extra_firewall_rules to document the usage of the 'myipv4' placeholder and its dependencies (dig and fetch_myip = true).
    • Included an example of using 'myipv4' in firewall_ssh_source.
  • locals.tf
    • Introduced a new local variable my_public_ipv4_cidr which constructs the CIDR block (e.g., 1.2.3.4/32) from the fetched IP, conditional on var.fetch_myip.
    • Refactored the firewall_rules_list logic to iterate through merged firewall rules and dynamically replace the 'myipv4' string in source_ips and destination_ips arrays.
    • The replacement logic now uses concat and try to ensure robustness, handling cases where source_ips or destination_ips might be null or where fetch_myip is disabled (in which case 'myipv4' entries are simply removed).
  • variables.tf
    • Added a new boolean variable fetch_myip with a default value of true and a descriptive comment.
  • versions.tf
    • Added the hashicorp/external Terraform provider as a required dependency with a version constraint of ~> 2.0.
Activity
  • gemini-code-assist[bot] provided an initial summary of changes.
  • vsalomaki made multiple requests for /gemini review and /gemini summary.
  • gemini-code-assist[bot] provided several review comments, initially suggesting improvements for the robustness of the dig command's output handling (e.g., checking for empty strings, non-IP strings, multiple IPs, and using a stricter regex).
  • gemini-code-assist[bot] also suggested simplifying the source_ips and destination_ips logic using lookup() and try() functions.
  • A critical issue was identified by gemini-code-assist[bot] regarding the handling of 'myipv4' when fetch_myip is false, leading to the current robust concat and try implementation to filter out the placeholder in such cases.
  • A minor suggestion was made to complete an incomplete comment in kube.tf.example.

gemini-code-assist[bot] avatar Oct 11 '25 21:10 gemini-code-assist[bot]

/gemini review

vsalomaki avatar Oct 11 '25 22:10 vsalomaki

/gemini review

vsalomaki avatar Oct 11 '25 22:10 vsalomaki

/gemini review

vsalomaki avatar Oct 11 '25 22:10 vsalomaki

/gemini summary

vsalomaki avatar Oct 11 '25 22:10 vsalomaki

Summary of Changes

This pull request introduces a significant enhancement to firewall configurations by allowing the use of a dynamic 'myipv4' placeholder. This placeholder automatically resolves to the user's current public IPv4 address, eliminating the need for manual updates when their IP changes. The implementation integrates an external data source to fetch the IP and updates the firewall rule processing to perform the necessary substitution, making firewall management more convenient and robust.

Highlights

  • Dynamic Public IP Detection: Introduced a mechanism to automatically detect the user's public IPv4 address using the dig command and the external Terraform provider. This IP is then used in firewall rules.
  • Firewall Rule Placeholder: Added support for a new 'myipv4' placeholder string in firewall source_ips or destination_ips arrays. This placeholder is dynamically replaced with the detected public IPv4 address in CIDR/32 format.
  • Enhanced Firewall Configuration: Modified the firewall rule processing logic to identify and substitute the 'myipv4' placeholder, simplifying the configuration for allowing access from the user's current IP without manual updates.
  • New Provider Dependency: Included the hashicorp/external Terraform provider as a required dependency to enable the execution of external commands for IP detection.
  • Configurable IP Fetching: Added a new fetch_myip boolean variable (defaulting to true) to control whether the public IP address should be fetched and used in firewall rules.
Changelog
  • data.tf
    • Added a new data "external" "my_ip" block to fetch the current public IPv4 address using a bash script that executes dig against myip.opendns.com and o-o.myaddr.l.google.com as a fallback.
    • Implemented robust validation within the bash script to ensure the fetched value is a valid IPv4 address, handles dig command existence, empty results, and includes a 5-second timeout.
    • The execution of this data source is conditional on the new var.fetch_myip.
  • kube.tf.example
    • Added a new commented variable fetch_myip with a default value of true.
    • Updated comments for firewall_kube_api_source, firewall_ssh_source, and extra_firewall_rules to document the usage of the 'myipv4' placeholder and its dependencies (dig and fetch_myip = true).
    • Included an example of using 'myipv4' in firewall_ssh_source.
  • locals.tf
    • Introduced a new local variable my_public_ipv4_cidr which constructs the CIDR block (e.g., 1.2.3.4/32) from the fetched IP, conditional on var.fetch_myip.
    • Refactored the firewall_rules_list logic to iterate through merged firewall rules and dynamically replace the 'myipv4' string in source_ips and destination_ips arrays.
    • The replacement logic now uses try and compact to ensure robustness, handling cases where source_ips or destination_ips might be null or where fetch_myip is disabled (in which case 'myipv4' entries are simply removed).
  • variables.tf
    • Added a new boolean variable fetch_myip with a default value of true and a descriptive comment.
  • versions.tf
    • Added the hashicorp/external Terraform provider as a required dependency with a version constraint of ~> 2.0.
Activity
  • gemini-code-assist[bot] provided an initial summary of changes.
  • vsalomaki made multiple requests for /gemini review and /gemini summary.
  • gemini-code-assist[bot] provided several review comments, initially suggesting improvements for the robustness of the dig command's output handling (e.g., checking for empty strings, non-IP strings, multiple IPs, and using a stricter regex).
  • gemini-code-assist[bot] also suggested simplifying the source_ips and destination_ips logic using lookup() and try() functions.
  • A critical issue was identified by gemini-code-assist[bot] regarding the handling of 'myipv4' when fetch_myip is false, leading to the current robust concat and try implementation to filter out the placeholder in such cases.
  • Further suggestions from gemini-code-assist[bot] included adding timeouts to the dig command, improving error messages for dig failures, and implementing fallback DNS servers for IP detection.
  • vsalomaki responded to a suggestion by adding dig -4 +short TXT o-o.myaddr.l.google.com @ns1.google.com as a fallback for IP fetching.
  • There was a discussion regarding the naming of the data "external" resource and the fetch_myip variable, with vsalomaki providing rationale for the current naming.

gemini-code-assist[bot] avatar Oct 11 '25 22:10 gemini-code-assist[bot]

/gemini review

vsalomaki avatar Oct 11 '25 22:10 vsalomaki

/gemini review

vsalomaki avatar Oct 11 '25 22:10 vsalomaki

/gemini review

vsalomaki avatar Oct 11 '25 23:10 vsalomaki

/gemini summary

vsalomaki avatar Oct 11 '25 23:10 vsalomaki

Summary of Changes

This pull request significantly enhances firewall configuration by allowing users to specify a dynamic placeholder, "myipv4", in their firewall rules. This placeholder is automatically resolved to the user's current public IPv4 address using an external dig command, eliminating the need for manual updates when their IP changes. The change integrates a robust IP detection script and refactors firewall rule processing to ensure flexible and convenient management of network access.

Highlights

  • Dynamic Public IPv4 Resolution: Introduced the ability to use a configurable placeholder string (default "myipv4") in firewall rules, which is dynamically resolved to the user's current public IPv4 address using the dig command.
  • Firewall Rule Integration: The placeholder can be used in source_ips or destination_ips arrays within firewall_kube_api_source, firewall_ssh_source, and extra_firewall_rules, and is replaced with the detected IP in CIDR/32 format.
  • Robust IP Detection Script: A bash script executed via the external Terraform provider fetches the IPv4, includes fallbacks (OpenDNS, Google DNS), and performs strict validation to ensure a valid IP is returned. The script is only executed if the placeholder is present in firewall rules.
  • New Terraform Provider Dependency: The hashicorp/external Terraform provider (~> 2.0) is now a required dependency to enable external command execution for IP detection.
  • Refactored Firewall Rule Processing: The locals.tf file now includes logic to iterate through merged firewall rules, replacing the placeholder with the resolved public IP and handling cases where rules are null or the placeholder is not used.
Changelog
  • data.tf
    • Added data "external" "my_ip" block to fetch the current public IPv4 address.
    • Implemented a bash script within the external data source that uses dig with OpenDNS and Google DNS as fallbacks to retrieve the IPv4 address.
    • The script includes robust validation for dig command existence, empty results, and strict IPv4 address format, exiting with an error if validation fails.
    • The execution of this data source is conditional, only running if var.my_ipv4_ref is found in any firewall rule definitions.
  • kube.tf.example
    • Updated comments for firewall_kube_api_source, firewall_ssh_source, and extra_firewall_rules to document the usage of the "myipv4" placeholder and its dependency on the dig command.
    • Added an example demonstrating the use of "myipv4" in firewall_ssh_source.
  • locals.tf
    • Introduced a new local variable my_public_ipv4_cidr which constructs the CIDR block (e.g., 1.2.3.4/32) from the fetched IP, using try for robustness.
    • Refactored the firewall_rules_list logic to iterate through merged firewall rules and dynamically replace var.my_ipv4_ref in source_ips and destination_ips arrays with local.my_public_ipv4_cidr.
    • The replacement logic uses try and compact to ensure robustness, handling cases where source_ips or destination_ips might be null or where the placeholder should be filtered out if the IP is not fetched.
  • variables.tf
    • Updated the extra_firewall_rules variable type to a more specific object structure, including description, direction, protocol, port, source_ips, and destination_ips.
    • Added a validation block for extra_firewall_rules to ensure the direction is either 'in' or 'out'.
    • Added a new string variable my_ipv4_ref with a default value of "myipv4", allowing users to customize the placeholder string for their IPv4 address in firewall rules.
  • versions.tf
    • Added the hashicorp/external Terraform provider as a required dependency with a version constraint of ~> 2.0.
Activity
  • The gemini-code-assist[bot] provided an initial summary of changes.
  • The author, vsalomaki, made multiple requests for /gemini review and /gemini summary throughout the development process.
  • gemini-code-assist[bot] provided several review comments focusing on improving the robustness of the dig command's output handling, including checks for empty strings, non-IP strings, multiple IPs, and suggesting a stricter regex.
  • Suggestions were made by gemini-code-assist[bot] to simplify the source_ips and destination_ips logic using lookup() and try() functions.
  • A critical issue was identified by gemini-code-assist[bot] regarding the handling of 'myipv4' when IP fetching is disabled, leading to the current robust concat and try with compact implementation to filter out the placeholder in such cases.
  • Further suggestions from gemini-code-assist[bot] included adding timeouts to the dig command, improving error messages for dig failures, and implementing fallback DNS servers for IP detection.
  • vsalomaki responded to a suggestion by adding dig -4 +short TXT o-o.myaddr.l.google.com @ns1.google.com as a fallback for IP fetching.
  • There was a discussion regarding the naming of the data "external" resource (my_ip vs my_ipv4) and a variable (fetch_myip vs fetch_myipv4), with vsalomaki providing rationale for the chosen naming.
  • gemini-code-assist[bot] suggested using dig's built-in +time option instead of the timeout command for portability, to which vsalomaki opted to remove the timeout definition completely, relying on dig's default.
  • A final high-priority suggestion from gemini-code-assist[bot] recommended using coalesce() for null firewall source variables in the count condition of the external data source to prevent errors.

gemini-code-assist[bot] avatar Oct 11 '25 23:10 gemini-code-assist[bot]

/gemini review

vsalomaki avatar Oct 11 '25 23:10 vsalomaki

/gemini review

vsalomaki avatar Oct 11 '25 23:10 vsalomaki

/gemini review

vsalomaki avatar Oct 12 '25 00:10 vsalomaki

/gemini review

vsalomaki avatar Oct 13 '25 17:10 vsalomaki

/gemini review

vsalomaki avatar Oct 14 '25 15:10 vsalomaki

@codex review please, and let me know if backward compatible or not.

mysticaltech avatar Oct 23 '25 12:10 mysticaltech

@codex review

vsalomaki avatar Oct 23 '25 18:10 vsalomaki

/gemini review

vsalomaki avatar Oct 23 '25 18:10 vsalomaki

/gemini review

vsalomaki avatar Oct 29 '25 19:10 vsalomaki

/gemini review

vsalomaki avatar Oct 29 '25 21:10 vsalomaki

/gemini review

vsalomaki avatar Oct 29 '25 21:10 vsalomaki

/gemini review

vsalomaki avatar Oct 29 '25 22:10 vsalomaki