Twingate / terraform-provider-twingate

Mozilla Public License 2.0
42 stars 11 forks source link

Create a new `twingate_resource_access` resource #241

Closed alexmensch closed 1 year ago

alexmensch commented 1 year ago

New functionality

Arguments

Behavior

Example usage

# Assigns access to `twingate_resource.internal_resources.id`
#   to both a service account and group. Does not override any existing assignments.
resource "twingate_resource_access" "internal" {
    resource_ids = toset([twingate_resource.internal_resources.id])
    service_account_ids = toset([twingate_service_account.github_actions_prod.id])
        group_ids = toset([data.twingate_groups.devops.id])
        authoritative = false
}

data "twingate_groups" "devops" {
        name = "DevOps"
}

resource "twingate_service_account" "github_actions_prod" {
    name = "Github Actions PROD"
}

resource "twingate_remote_network" "aws" {
        name = "AWS"
}

resource "twingate_resource" "internal_resources" {
        name = "All internal resources"
        address = "*.internal.int"
        remote_network_id = twingate_remote_network.aws.id
        protocols {
          allow_icmp = true
          tcp  {
            policy = "RESTRICTED"
            ports = ["80", "82-83"]
          }
          udp {
            policy = "ALLOW_ALL"
          }
       }
}
alexmensch commented 1 year ago

Notes

tjstansell commented 1 year ago

I'm not sure I understand how the authoritative flag on this will work. Does it enforce at the resource side, the group side, or both? As in, if I have

resource "twingate_resource_access" "one" {
    authoritative = true
    resource_ids = [resource.twingate_resource.a.id, resource.twingate_resource.b.id]
    group_ids = [resource.twingate_group.eng.id, resource.twingate_group.sec.id]
}
resource "twingate_resource_access" "two" {
    authoritative = true
    resource_ids = [resource.twingate_resource.c.id, resource.twingate_resource.d.id]
    group_ids = [resource.twingate_group.hr.id, resource.twingate_group.sec.id]
}

what's expected to happen? i would expect:

But would the authoritative flag end up causing the two to fight over which resources the sec group has access to? Would there be a way to specify authoritatively which resources a specific group has access to? or does the authoritative flag only apply to the resources?

Curious here if you're trying to make this flexible enough for folks to choose if they want absolute control at the resource side (who has access to that resource), the group side (which resources that group has access to), or neither (individual permissions managed by terraform, but extras manually done via the UI are also allowed).

tjstansell commented 1 year ago

IMO, I think you could solve this by adding support for access {} blocks in the resource and group resources to maintain exclusive control from that perspective:

resource "twingate_resource" "a" {
  address = ...
  name = ...
  access {
    group_ids = [...]
    policy = ...
  }
  access {
    group_ids = [...]
    policy = ...
  }
}

which would enforce this from the resource perspective. The same thing could be done from the group side. If I'm missing something with how the authoritative flag will work, I'm all ears. I think you had mentioned you were modeling this after a different provider resource that worked this way. Perhaps if I saw that, it would more sense?

Personally, I also think it's much clearer to read the terraform code and understand who has access to a resource if you supported this type of thing (where it's all defined within the twingate_resource itself).

tjstansell commented 1 year ago

I'm used to the aws iam role model so it would work similar to that.

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role#example-of-exclusive-inline-policies

alexmensch commented 1 year ago

After some more debate on this, we've finalized on a new approach that will make access assignments directly on the twingate_resource resource. For now, we are going to table the approach of having a separate Terraform resource for access assignments until we have additional complexity in the policy engine that will justify this.

You can continue to follow this work in issue #245