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

Feature: add policy to resource definition #36

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
36cf420
convert resource object
vmanilo Oct 12, 2023
1e7d3d7
added feature branch
vmanilo Oct 12, 2023
3149028
Merge remote-tracking branch 'upstream/main' into feature/convert-res…
vmanilo Oct 12, 2023
f95f959
Update go mod file
vmanilo Oct 12, 2023
a838b06
fix test
vmanilo Oct 12, 2023
2beeca9
remove feature branch
vmanilo Oct 12, 2023
97c1aff
Merge branch 'main' into feature/convert-resource-object
vmanilo Oct 19, 2023
e3b74f7
Merge remote-tracking branch 'upstream/main' into feature/convert-res…
vmanilo Oct 20, 2023
9e29506
Merge branch 'feature/convert-resource-object' of github.com:vmanilo/…
vmanilo Oct 20, 2023
7db6408
Merge branch 'main' into feature/convert-resource-object
vmanilo Oct 24, 2023
cf39585
wip
vmanilo Oct 25, 2023
fa52cd2
Merge branch 'feature/convert-resource-object' into feature/add-polic…
vmanilo Oct 25, 2023
6de2221
Merge remote-tracking branch 'upstream/main' into feature/convert-res…
vmanilo Oct 29, 2023
36c231a
breaking change: set IsBrowserShortcutEnabled=false by default
vmanilo Oct 29, 2023
e7063e1
remove feature branch
vmanilo Oct 29, 2023
05faab1
Merge branch 'feature/convert-resource-object' into feature/add-polic…
vmanilo Nov 1, 2023
faa8f52
added security_policy to resource definition
vmanilo Nov 7, 2023
ec402a0
fix tests
vmanilo Nov 7, 2023
489c394
added security_policy_id to resource definition
vmanilo Nov 10, 2023
aaa61e6
remove feature branch from CI
vmanilo Nov 10, 2023
4c3088d
Merge branch 'feature/add-security_policy-to-resource' into feature/c…
vmanilo Nov 10, 2023
a57cfb1
added feature branch to CI
vmanilo Nov 10, 2023
ff04d2a
remove feature branch
vmanilo Nov 10, 2023
a5b91d5
Merge branch 'feature/convert-resource-object' into feature/add-polic…
vmanilo Nov 12, 2023
127d510
added schema upgrader
vmanilo Nov 16, 2023
39f4ba8
fix updating resource without protocols
vmanilo Nov 22, 2023
798ddaf
fix setting default ports value
vmanilo Nov 22, 2023
2e3eb7e
fix diff supress for empty ports array
vmanilo Nov 27, 2023
f7d9389
fix default ports value
vmanilo Nov 30, 2023
87d20fe
fix test TestAccTwingateResourceImport
vmanilo Nov 30, 2023
78a3c52
Merge remote-tracking branch 'upstream/main' into feature/convert-res…
vmanilo Dec 2, 2023
5bf80fa
fix test
vmanilo Dec 2, 2023
6dfdc7d
restore ci.yml
vmanilo Dec 3, 2023
c42baa8
Merge branch 'feature/convert-resource-object' into feature/add-polic…
vmanilo Dec 3, 2023
6e33622
fix docs
vmanilo Dec 3, 2023
2f377b8
fix test tf config
vmanilo Dec 3, 2023
724c7f2
added schema upgrader
vmanilo Dec 4, 2023
9db9c37
fix equalProtocol func
vmanilo Dec 6, 2023
77fe526
fix handling default policy for access groups
vmanilo Dec 11, 2023
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
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
pull_request:
branches:
- main
- feature/add-policy-to-resource-definition
paths-ignore:
- 'README.md'

Expand All @@ -15,6 +16,7 @@ on:
- 'README.md'
branches:
- main
- feature/add-policy-to-resource-definition

# Ensures only 1 action runs per PR and previous is canceled on new trigger
concurrency:
Expand Down Expand Up @@ -118,7 +120,7 @@ jobs:
name: Matrix Acceptance Tests
needs: build
runs-on: ubuntu-latest
if: "!github.event.pull_request.head.repo.fork"
# if: "!github.event.pull_request.head.repo.fork"
timeout-minutes: 15
strategy:
fail-fast: false
Expand Down Expand Up @@ -169,7 +171,7 @@ jobs:

cleanup:
name: Cleanup
if: "!github.event.pull_request.head.repo.fork"
# if: "!github.event.pull_request.head.repo.fork"
needs: tests-acceptance
runs-on: ubuntu-latest
timeout-minutes: 15
Expand Down
63 changes: 35 additions & 28 deletions docs/resources/resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@ resource "twingate_group" "aws" {
name = "aws_group"
}

resource "twingate_group" "devops" {
name = "DevOps"
}

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

data "twingate_security_policy" "mfa" {
name = "Default Policy"
}

data "twingate_security_policy" "test_policy" {
name = "Test Policy"
}
Expand All @@ -41,19 +49,26 @@ resource "twingate_resource" "resource" {

security_policy_id = data.twingate_security_policy.test_policy.id

protocols {
protocols = {
allow_icmp = true
tcp {
tcp = {
policy = "RESTRICTED"
ports = ["80", "82-83"]
}
udp {
udp = {
policy = "ALLOW_ALL"
}
}

dynamic "access" {
for_each = [twingate_group.devops.id, twingate_group.aws.id]
content {
security_policy_id = data.twingate_security_policy.mfa.id
group_id = access.value
}
}

access {
group_ids = [twingate_group.aws.id]
service_account_ids = [twingate_service_account.github_actions_prod.id]
}
}
Expand All @@ -70,13 +85,13 @@ resource "twingate_resource" "resource" {

### Optional

- `access` (Block List, Max: 1) Restrict access to certain groups or service accounts (see [below for nested schema](#nestedblock--access))
- `access` (Block Set) Restrict access to certain groups or service accounts (see [below for nested schema](#nestedblock--access))
- `alias` (String) Set a DNS alias address for the Resource. Must be a DNS-valid name string.
- `is_authoritative` (Boolean) Determines whether assignments in the access block will override any existing assignments. Default is `true`. If set to `false`, assignments made outside of Terraform will be ignored.
- `is_browser_shortcut_enabled` (Boolean) Controls whether an "Open in Browser" shortcut will be shown for this Resource in the Twingate Client.
- `is_visible` (Boolean) Controls whether this Resource will be visible in the main Resource list in the Twingate Client.
- `protocols` (Block List, Max: 1) Restrict access to certain protocols and ports. By default or when this argument is not defined, there is no restriction, and all protocols and ports are allowed. (see [below for nested schema](#nestedblock--protocols))
- `security_policy_id` (String) The ID of a `twingate_security_policy` to set as this Resource's Security Policy. Default is `Default Policy`
- `is_browser_shortcut_enabled` (Boolean) Controls whether an "Open in Browser" shortcut will be shown for this Resource in the Twingate Client. Default is false.
- `is_visible` (Boolean) Controls whether this Resource will be visible in the main Resource list in the Twingate Client. Default is true.
- `protocols` (Attributes) Restrict access to certain protocols and ports. By default or when this argument is not defined, there is no restriction, and all protocols and ports are allowed. (see [below for nested schema](#nestedatt--protocols))
- `security_policy_id` (String) The ID of a `twingate_security_policy` to set as this Resource's Security Policy. Default is `Default Policy`.

### Read-Only

Expand All @@ -87,44 +102,36 @@ resource "twingate_resource" "resource" {

Optional:

- `group_ids` (Set of String) List of Group IDs that will have permission to access the Resource.
- `group_id` (String) Group ID that will have permission to access the Resource.
- `security_policy_id` (String) The ID of a twingate_security_policy to use as the access policy for the group IDs in the access block
- `service_account_ids` (Set of String) List of Service Account IDs that will have permission to access the Resource.


<a id="nestedblock--protocols"></a>
<a id="nestedatt--protocols"></a>
### Nested Schema for `protocols`

Required:

- `tcp` (Block List, Min: 1, Max: 1) (see [below for nested schema](#nestedblock--protocols--tcp))
- `udp` (Block List, Min: 1, Max: 1) (see [below for nested schema](#nestedblock--protocols--udp))

Optional:

- `allow_icmp` (Boolean) Whether to allow ICMP (ping) traffic
- `tcp` (Attributes) (see [below for nested schema](#nestedatt--protocols--tcp))
- `udp` (Attributes) (see [below for nested schema](#nestedatt--protocols--udp))

<a id="nestedblock--protocols--tcp"></a>
<a id="nestedatt--protocols--tcp"></a>
### Nested Schema for `protocols.tcp`

Required:

- `policy` (String) Whether to allow or deny all ports, or restrict protocol access within certain port ranges: Can be `RESTRICTED` (only listed ports are allowed), `ALLOW_ALL`, or `DENY_ALL`

Optional:

- `ports` (List of String) List of port ranges between 1 and 65535 inclusive, in the format `100-200` for a range, or `8080` for a single port
- `policy` (String) Whether to allow or deny all ports, or restrict protocol access within certain port ranges: Can be `RESTRICTED` (only listed ports are allowed), `ALLOW_ALL`, or `DENY_ALL`
- `ports` (Set of String) List of port ranges between 1 and 65535 inclusive, in the format `100-200` for a range, or `8080` for a single port


<a id="nestedblock--protocols--udp"></a>
<a id="nestedatt--protocols--udp"></a>
### Nested Schema for `protocols.udp`

Required:

- `policy` (String) Whether to allow or deny all ports, or restrict protocol access within certain port ranges: Can be `RESTRICTED` (only listed ports are allowed), `ALLOW_ALL`, or `DENY_ALL`

Optional:

- `ports` (List of String) List of port ranges between 1 and 65535 inclusive, in the format `100-200` for a range, or `8080` for a single port
- `policy` (String) Whether to allow or deny all ports, or restrict protocol access within certain port ranges: Can be `RESTRICTED` (only listed ports are allowed), `ALLOW_ALL`, or `DENY_ALL`
- `ports` (Set of String) List of port ranges between 1 and 65535 inclusive, in the format `100-200` for a range, or `8080` for a single port

## Import

Expand Down
23 changes: 19 additions & 4 deletions examples/resources/twingate_resource/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ resource "twingate_group" "aws" {
name = "aws_group"
}

resource "twingate_group" "devops" {
name = "DevOps"
}

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

data "twingate_security_policy" "mfa" {
name = "Default Policy"
}

data "twingate_security_policy" "test_policy" {
name = "Test Policy"
}
Expand All @@ -26,19 +34,26 @@ resource "twingate_resource" "resource" {

security_policy_id = data.twingate_security_policy.test_policy.id

protocols {
protocols = {
allow_icmp = true
tcp {
tcp = {
policy = "RESTRICTED"
ports = ["80", "82-83"]
}
udp {
udp = {
policy = "ALLOW_ALL"
}
}

dynamic "access" {
for_each = [twingate_group.devops.id, twingate_group.aws.id]
content {
security_policy_id = data.twingate_security_policy.mfa.id
group_id = access.value
}
}

access {
group_ids = [twingate_group.aws.id]
service_account_ids = [twingate_service_account.github_actions_prod.id]
}
}
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ require (
github.com/hashicorp/terraform-plugin-framework v1.4.2
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
github.com/hashicorp/terraform-plugin-go v0.19.1
github.com/hashicorp/terraform-plugin-mux v0.12.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.30.0
github.com/hashicorp/terraform-plugin-testing v1.5.1
github.com/hasura/go-graphql-client v0.10.0
github.com/iancoleman/strcase v0.3.0
Expand Down Expand Up @@ -57,6 +55,7 @@ require (
github.com/hashicorp/terraform-exec v0.19.0 // indirect
github.com/hashicorp/terraform-json v0.17.1 // indirect
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.30.0 // indirect
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ github.com/hashicorp/terraform-plugin-go v0.19.1 h1:lf/jTGTeELcz5IIbn/94mJdmnTjR
github.com/hashicorp/terraform-plugin-go v0.19.1/go.mod h1:5NMIS+DXkfacX6o5HCpswda5yjkSYfKzn1Nfl9l+qRs=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow=
github.com/hashicorp/terraform-plugin-mux v0.12.0 h1:TJlmeslQ11WlQtIFAfth0vXx+gSNgvMEng2Rn9z3WZY=
github.com/hashicorp/terraform-plugin-mux v0.12.0/go.mod h1:8MR0AgmV+Q03DIjyrAKxXyYlq2EUnYBQP8gxAAA0zeM=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.30.0 h1:X7vB6vn5tON2b49ILa4W7mFAsndeqJ7bZFOGbVO+0Cc=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.30.0/go.mod h1:ydFcxbdj6klCqYEPkPvdvFKiNGKZLUs+896ODUXCyao=
github.com/hashicorp/terraform-plugin-testing v1.5.1 h1:T4aQh9JAhmWo4+t1A7x+rnxAJHCDIYW9kXyo4sVO92c=
Expand Down
2 changes: 2 additions & 0 deletions golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ linters-settings:
- github.com/hashicorp/terraform-plugin-framework/datasource.DataSource
- github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier.Set
- github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier.Bool
- github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier.String
- github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier.Object
errcheck:
check-type-assertions: false
check-blank: false
Expand Down
39 changes: 7 additions & 32 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,27 @@ import (
"log"

"github.com/Twingate/terraform-provider-twingate/twingate"
twingateV2 "github.com/Twingate/terraform-provider-twingate/twingate/v2"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-mux/tf5to6server"
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"
)

var (
version = "dev"
)

const registry = "registry.terraform.io/Twingate/twingate"

func main() {
var debug bool

flag.BoolVar(&debug, "debug", false, "set to true to run the provider with support for debuggers")
flag.Parse()

ctx := context.Background()
upgradedSdkProvider, err := tf5to6server.UpgradeServer(ctx, twingate.Provider(version).GRPCProvider)
if err != nil {
log.Fatal(err)
}
providers := []func() tfprotov6.ProviderServer{
func() tfprotov6.ProviderServer {
return upgradedSdkProvider
err := providerserver.Serve(context.Background(), twingate.New(version),
providerserver.ServeOpts{
Debug: debug,
Address: registry,
ProtocolVersion: 6,
},
providerserver.NewProtocol6(twingateV2.New(version)()),
}

muxServer, err := tf6muxserver.NewMuxServer(ctx, providers...)

if err != nil {
log.Fatal(err)
}

var serveOpts []tf6server.ServeOpt
if debug {
serveOpts = append(serveOpts, tf6server.WithManagedDebug())
}

err = tf6server.Serve(
"registry.terraform.io/Twingate/twingate",
muxServer.ProviderServer,
serveOpts...,
)

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions twingate/internal/attr/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ const (
RemoteNetworkID = "remote_network_id"
Type = "type"
IsActive = "is_active"
GroupIDs = "group_ids"
)
25 changes: 25 additions & 0 deletions twingate/internal/attr/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "strings"
const (
attrFirstElement = ".0"
attrPathSeparator = ".0."
attrSeparator = "."
attrLenSymbol = ".#"
)

Expand All @@ -18,10 +19,24 @@ func First(attributes ...string) string {
return attr + attrFirstElement
}

func FirstAttr(attributes ...string) string {
attr := PathAttr(attributes...)

if attr == "" {
return ""
}

return attr + attrFirstElement
}

func Path(attributes ...string) string {
return strings.Join(attributes, attrPathSeparator)
}

func PathAttr(attributes ...string) string {
return strings.Join(attributes, attrSeparator)
}

func Len(attributes ...string) string {
attr := Path(attributes...)

Expand All @@ -31,3 +46,13 @@ func Len(attributes ...string) string {

return attr + attrLenSymbol
}

func LenAttr(attributes ...string) string {
attr := PathAttr(attributes...)

if attr == "" {
return ""
}

return attr + attrLenSymbol
}
2 changes: 1 addition & 1 deletion twingate/internal/attr/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package attr

const (
Access = "access"
GroupIDs = "group_ids"
GroupID = "group_id"
ServiceAccountIDs = "service_account_ids"
IsAuthoritative = "is_authoritative"
Policy = "policy"
Expand Down
2 changes: 1 addition & 1 deletion twingate/internal/client/query/resource-create.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package query

type CreateResource struct {
ResourceEntityResponse `graphql:"resourceCreate(name: $name, address: $address, remoteNetworkId: $remoteNetworkId, groupIds: $groupIds, protocols: $protocols, isVisible: $isVisible, isBrowserShortcutEnabled: $isBrowserShortcutEnabled, alias: $alias, securityPolicyId: $securityPolicyId)"`
ResourceEntityResponse `graphql:"resourceCreate(name: $name, address: $address, remoteNetworkId: $remoteNetworkId, protocols: $protocols, isVisible: $isVisible, isBrowserShortcutEnabled: $isBrowserShortcutEnabled, alias: $alias, securityPolicyId: $securityPolicyId)"`
}

func (q CreateResource) IsEmpty() bool {
Expand Down
Loading
Loading