Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tofu/terraform fails to apply multiple actions in restricted user mode (unix socket) #200

Closed
gdonval opened this issue Feb 19, 2025 · 4 comments · Fixed by #203
Closed
Milestone

Comments

@gdonval
Copy link

gdonval commented Feb 19, 2025

Minmal non-working example:

terraform {
  required_providers {
    incus = {
      source = "lxc/incus"
      version = "0.2.0"
    }
  }
}

provider incus {
  # Configuration options
}


resource "incus_instance" "instance-user-1000-1"{
  project = "user-1000"
  name    = "gene-instance1"
  image   = "images:alpine/edge"
  wait_for_network = false
}

resource "incus_instance" "instance-user-1000-2"{
  project = "user-1000"
  name    = "gene-instance2"
  image   = "images:alpine/edge"
  wait_for_network = false
}

$ tofu apply
... # plan

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

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

  Enter a value: yes

incus_instance.instance-user-1000-1: Creating...
incus_instance.instance-user-1000-2: Creating...
incus_instance.instance-user-1000-1: Creation complete after 3s [name=gene-instance1]
╷
│ Error: Failed to retrieve Incus InstanceServer
│ 
│   with incus_instance.instance-user-1000-2,
│   on test.tf line 22, in resource "incus_instance" "instance-user-1000-2":
│   22: resource "incus_instance" "instance-user-1000-2"{
│ 
│ Environment variable INCUS_DIR points to an Incus directory that does not contain a writable unix socket
╵

It looks like tofu reverts to try using the privileged unix socket instead of the user one when more than 1 action is queued. I guess that this is not a problem if using the remote API.

@gdonval gdonval changed the title Tofu/terraform fails to apply multiple actions in restricted user mode Tofu/terraform fails to apply multiple actions in restricted user mode (unix socket) Feb 19, 2025
@stgraber stgraber added this to the 1.0 milestone Feb 19, 2025
@stgraber
Copy link
Member

@maveonair any idea what's going on here?

@maveonair
Copy link
Member

maveonair commented Feb 19, 2025

Do you run Opentofu and Terraform on the same computer as the restricted project user-1000?


I tested it on my Macbook with OpenTofu against a Debian Bookworm server with a restricted project, and it worked fine:

Client Setup:

Server:

$ sudo incus config trust add --projects user-1000 --restricted mbpro

Client:

$ incus remote add incus-dev https://incus-dev:8443

OpenTofu / Terraform Config:

terraform {
  required_providers {
    incus = {
      source  = "lxc/incus"
      version = "0.2.0"
    }
  }
}

provider "incus" {
  accept_remote_certificate    = true
  generate_client_certificates = false

  remote {
    name    = "incus-dev" # my remote server
    default = true
  }
}

resource "incus_instance" "instance-user-1000-1" {
  project          = "user-1000"
  name             = "gene-instance1"
  image            = "images:alpine/edge"
  wait_for_network = false
}

resource "incus_instance" "instance-user-1000-2" {
  project          = "user-1000"
  name             = "gene-instance2"
  image            = "images:alpine/edge"
  wait_for_network = false
}

Running Tofu:

$ tofu apply

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

OpenTofu will perform the following actions:

  # incus_instance.instance-user-1000-1 will be created
  + resource "incus_instance" "instance-user-1000-1" {
      + config           = {}
      + ephemeral        = false
      + image            = "images:alpine/edge"
      + ipv4_address     = (known after apply)
      + ipv6_address     = (known after apply)
      + mac_address      = (known after apply)
      + name             = "gene-instance1"
      + profiles         = [
          + "default",
        ]
      + project          = "user-1000"
      + running          = true
      + status           = (known after apply)
      + target           = (known after apply)
      + type             = "container"
      + wait_for_network = false
    }

  # incus_instance.instance-user-1000-2 will be created
  + resource "incus_instance" "instance-user-1000-2" {
      + config           = {}
      + ephemeral        = false
      + image            = "images:alpine/edge"
      + ipv4_address     = (known after apply)
      + ipv6_address     = (known after apply)
      + mac_address      = (known after apply)
      + name             = "gene-instance2"
      + profiles         = [
          + "default",
        ]
      + project          = "user-1000"
      + running          = true
      + status           = (known after apply)
      + target           = (known after apply)
      + type             = "container"
      + wait_for_network = false
    }

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

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

  Enter a value: yes

incus_instance.instance-user-1000-1: Creating...
incus_instance.instance-user-1000-2: Creating...
incus_instance.instance-user-1000-1: Creation complete after 7s [name=gene-instance1]
incus_instance.instance-user-1000-2: Creation complete after 7s [name=gene-instance2]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

$ incus ls
+----------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
|      NAME      |  STATE  |         IPV4         |                     IPV6                      |   TYPE    | SNAPSHOTS |
+----------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| gene-instance1 | RUNNING | 10.28.248.116 (eth0) | fd42:95a8:a939:c62e:216:3eff:fe8e:1223 (eth0) | CONTAINER | 0         |
+----------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| gene-instance2 | RUNNING | 10.28.248.120 (eth0) | fd42:95a8:a939:c62e:216:3eff:fed4:29b4 (eth0) | CONTAINER | 0         |
+----------------+---------+----------------------+-----------------------------------------------+-----------+-----------+

@gdonval
Copy link
Author

gdonval commented Feb 19, 2025

Do you run Opentofu and Terraform on the same computer as the restricted project user-1000?

Yes indeed. I'm sorry if I did not make that obvious. I fully expect explicit remotes to work well. :)

It looks like the first time tofu tries to perform an action, it tries the privileged socket and then the user one. Then, on subsequent actions, it fails to fallback to the user one.

@maveonair
Copy link
Member

I fixed your issue in the following PR: #203

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants