dalet-oss / terraform-provider-opnsense

Terraform Provider for OPNSense DHCP Leases
Apache License 2.0
34 stars 6 forks source link

opnsense provider crashes with segfault #5

Closed Morpheus0x closed 2 years ago

Morpheus0x commented 2 years ago

System Information

Linux distribution

Debian 11

Terraform version

Terraform v1.2.3

Provider versions

0.3.0


Description of Issue/Question

Setup

infra.tf

resource "opnsense_dhcp_static_map" "dhcp1" {
  interface = "LAN"
  mac       = "9e:0d:a6:a7:7e:fb"
  ipaddr    = "10.0.5.234"
  hostname  = "myhostname"
}
resource "opnsense_dns_host_override" "dns1" {
  type   = "A"
  host   = "myhostname"
  domain = "example.com"
  ip     = "10.0.5.234"
}

Steps to Reproduce Issue

Running terraform apply produces the following output and stacktrace

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with
the following symbols:
  + create

Terraform will perform the following actions:

  # opnsense_dhcp_static_map.dhcp1 will be created
  + resource "opnsense_dhcp_static_map" "dhcp1" {
      + hostname  = "myhostname"
      + id        = (known after apply)
      + interface = "LAN"
      + ipaddr    = "10.0.5.234"
      + mac       = "9e:0d:a6:a7:7e:fb"
    }

  # opnsense_dns_host_override.dns1 will be created
  + resource "opnsense_dns_host_override" "dns1" {
      + domain = "example.com"
      + host   = "myhostname"
      + id     = (known after apply)
      + ip     = "10.0.5.234"
      + type   = "A"
    }

Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

opnsense_dns_host_override.dns1: Creating...
opnsense_dhcp_static_map.dhcp1: Creating...
opnsense_dhcp_static_map.dhcp1: Still creating... [10s elapsed]
opnsense_dns_host_override.dns1: Still creating... [10s elapsed]
opnsense_dns_host_override.dns1: Creation complete after 11s [id=A/myhostname/example.com/10.0.5.234/0]
╷
│ Error: Plugin did not respond
│
│   with opnsense_dhcp_static_map.dhcp1,
│   on infra.tf line 3, in resource "opnsense_dhcp_static_map" "dhcp1":
│    3: resource "opnsense_dhcp_static_map" "dhcp1" {
│
│ The plugin encountered an error, and failed to respond to the plugin.(*GRPCProvider).ApplyResourceChange call. The
│ plugin logs may contain more details.
╵

Stack trace from the terraform-provider-opnsense_v0.3.0 plugin:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0xc56096]

goroutine 23 [running]:
github.com/gxben/terraform-provider-opnsense/opnsense.(*DHCPSession).GetStaticFieldNames(0xc00062a180, 0xc0000e9b90, 0x2)
        github.com/gxben/terraform-provider-opnsense/opnsense/dhcp.go:69 +0x116
github.com/gxben/terraform-provider-opnsense/opnsense.(*DHCPSession).GetAllInterfaceStaticMappings(0xc00062a180, 0xc000322549, 0x3, 0xd37a80, 0xfd8d40, 0x0, 0xc0002ae600, 0x0)
        github.com/gxben/terraform-provider-opnsense/opnsense/dhcp.go:146 +0x298
github.com/gxben/terraform-provider-opnsense/opnsense.(*DHCPSession).FindMappingByMAC(0xc00062a180, 0xc000685778, 0xc0002ae650, 0x0, 0x0)
        github.com/gxben/terraform-provider-opnsense/opnsense/dhcp.go:249 +0x46
github.com/gxben/terraform-provider-opnsense/opnsense.(*DHCPSession).CreateStaticMapping(0xc00062a180, 0xc000685778, 0x8, 0xd37a80)
        github.com/gxben/terraform-provider-opnsense/opnsense/dhcp.go:268 +0x39
github.com/gxben/terraform-provider-opnsense/opnsense.resourceDhcpStaticMappingCreate(0xc000189c00, 0xd0ed60, 0xc0000a0b40, 0x2, 0x15ab440)
        github.com/gxben/terraform-provider-opnsense/opnsense/resource_opn_dhcp_static_map.go:89 +0x25f
github.com/hashicorp/terraform-plugin-sdk/helper/schema.(*Resource).Apply(0xc000364120, 0xc000607310, 0xc0006005c0, 0xd0ed60, 0xc0000a0b40, 0xd73601, 0xc0000a3968, 0xc0006ff470)
        github.com/hashicorp/terraform-plugin-sdk@v1.9.0/helper/schema/resource.go:310 +0x375
github.com/hashicorp/terraform-plugin-sdk/helper/schema.(*Provider).Apply(0xc000370100, 0xc00020da18, 0xc000607310, 0xc0006005c0, 0xc0000a4ce8, 0xc0000be3d0, 0xd752a0)
        github.com/hashicorp/terraform-plugin-sdk@v1.9.0/helper/schema/provider.go:294 +0x99
github.com/hashicorp/terraform-plugin-sdk/internal/helper/plugin.(*GRPCProviderServer).ApplyResourceChange(0xc00036e038, 0xffbf30, 0xc0006fec60, 0xc0001896c0, 0xc00036e038, 0xc0006fec60, 0xc000624b80)
        github.com/hashicorp/terraform-plugin-sdk@v1.9.0/internal/helper/plugin/grpc_provider.go:885 +0x8a5
github.com/hashicorp/terraform-plugin-sdk/internal/tfplugin5._Provider_ApplyResourceChange_Handler(0xe49ec0, 0xc00036e038, 0xffbf30, 0xc0006fec60, 0xc000075500, 0x0, 0xffbf30, 0xc0006fec60, 0xc00013e540, 0xdf)
        github.com/hashicorp/terraform-plugin-sdk@v1.9.0/internal/tfplugin5/tfplugin5.pb.go:3305 +0x214
google.golang.org/grpc.(*Server).processUnaryRPC(0xc000082a80, 0x1003718, 0xc000083500, 0xc000588500, 0xc0004b8f60, 0x1555be0, 0x0, 0x0, 0x0)
        google.golang.org/grpc@v1.27.1/server.go:1024 +0x522
google.golang.org/grpc.(*Server).handleStream(0xc000082a80, 0x1003718, 0xc000083500, 0xc000588500, 0x0)
        google.golang.org/grpc@v1.27.1/server.go:1313 +0xd2c
google.golang.org/grpc.(*Server).serveStreams.func1.1(0xc0000b8170, 0xc000082a80, 0x1003718, 0xc000083500, 0xc000588500)
        google.golang.org/grpc@v1.27.1/server.go:722 +0xab
created by google.golang.org/grpc.(*Server).serveStreams.func1
        google.golang.org/grpc@v1.27.1/server.go:720 +0xa5

Error: The terraform-provider-opnsense_v0.3.0 plugin crashed!

This is always indicative of a bug within the plugin. It would be immensely
helpful if you could report the crash with the plugin's maintainers so that it
can be fixed. The output above should help diagnose the issue.

I logged in to opnsense after running terraform apply and the Reporter caught an error:

[06-Jul-2022 11:56:52] PHP Warning:  Invalid argument supplied for foreach() in /usr/local/www/services_unbound_host_edit.php on line 121

This would mean that there was an issue with configuring the DNS Override via API, however the override was successfully added: firefox_7mt4BQX1CN

The issue is, that the DHCP Static Lease isn't configured.


Additional information:

I made sure that my opnsense firewall has a valid ssl certificate and that username and password are correct

Morpheus0x commented 2 years ago

I found the problem: The interface name wasn't correct. I expected it to be the same as shown in the GUI, but it wasn't. I looked at the source code for this provider and found out that it basically just simulates a user configuring the firewall using the browser, there I found out that I need to look for the if value in the request form data. By using dev tools in the browser, creating a static lease and looking at the request form data, I found out that I have to write lowercase lan as interface name. Also if you are using vlans, the interface name isn't at all intuitive(e.g. my 2nd vlan interface is called opt7) and the only way to get the interface name that is needed by the provider is by looking at the request in dev tools.

The good thing is, that this doesn't change, so you only have to look that up once. A good idea would be to define a terraform data source that matches the name of your interface in opnsense and contains the string for the interface.

dedene commented 2 years ago

Just to add to this conversation, I believe you can also see the interface name to use in the _opnsense_dhcp_staticmap resource from the url in OPNSense. I.e. when going to Services » DHCPv4 » LAN, you can see the interface name in the url: https://<opnsense_url>/services_dhcp.php?if=lan