Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 1 addition & 43 deletions lwgenerate/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package azure

import (
"fmt"
"io"
"net"
"net/http"
"strings"

"github.com/hashicorp/hcl/v2/hclwrite"
Expand Down Expand Up @@ -126,39 +124,6 @@ func IsIpv4(ip string) bool {
return parsedIP != nil && parsedIP.To4() != nil
}

// getCurrentPublicIP retrieves the current public IP address of the machine
func getCurrentPublicIP() (string, error) {
// use reliable IP detection services
urls := []string{
"https://api.ipify.org",
"https://ifconfig.me",
"https://ipinfo.io/ip",
}

for _, url := range urls {
resp, err := http.Get(url)
if err != nil {
continue
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
continue
}
ip := strings.TrimSpace(string(body))
// Validate if the IP is a valid IPv4 address
if !IsIpv4(ip) {
continue
}
return ip, nil
}
}

return "", fmt.Errorf("failed to get public IP")
}

// Ensure all combinations of inputs are valid for supported spec
func (args *GenerateAzureTfConfigurationArgs) validate() error {
// Validate one of config, agentless or activity log was enabled; otherwise error out
Expand Down Expand Up @@ -743,14 +708,7 @@ func createActivityLog(args *GenerateAzureTfConfigurationArgs) ([]*hclwrite.Bloc
if args.UseStorageAccountNetworkRules && !args.ExistingStorageAccount {
attributes["use_storage_account_network_rules"] = args.UseStorageAccountNetworkRules

// if no IP rules are provided, automatically detect the current public IP
if len(args.StorageAccountNetworkRuleIpRules) == 0 {
currentIP, err := getCurrentPublicIP()
if err != nil {
return nil, err
}
attributes["storage_account_network_rule_ip_rules"] = []string{currentIP}
} else {
if len(args.StorageAccountNetworkRuleIpRules) > 0 {
attributes["storage_account_network_rule_ip_rules"] = args.StorageAccountNetworkRuleIpRules
}
}
Expand Down
Loading