-
Notifications
You must be signed in to change notification settings - Fork 123
Add security exception list #1495
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
Draft
nick-benoit
wants to merge
11
commits into
main
Choose a base branch
from
add_security_exception_list
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7b5909a
Add security exception list resource
nick-benoit f29832b
Add exception list examples
nick-benoit e385ba4
Add exceptions client helper
nick-benoit ef69978
Use composite id for internal id
nick-benoit 5ddcf0e
Merge branch 'main' of github.com:elastic/terraform-provider-elastics…
nick-benoit 3980a35
Add plugin to provider
nick-benoit 7b2f1d0
Set space from composite id / add import test
nick-benoit a19c981
Use normalized json type for "meta"
nick-benoit 1f7b81e
Use typeutils for casts
nick-benoit 3e2d841
Return parsed response from exceptions.go
nick-benoit 67d43e1
Extract valid list types into a variable
nick-benoit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
examples/resources/elasticstack_kibana_security_exception_list/resource.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| resource "elasticstack_kibana_security_exception_list" "example" { | ||
| list_id = "my-detection-exception-list" | ||
| name = "My Detection Exception List" | ||
| description = "List of exceptions for security detection rules" | ||
| type = "detection" | ||
| namespace_type = "single" | ||
|
|
||
| tags = ["security", "detections"] | ||
| } |
10 changes: 10 additions & 0 deletions
10
examples/resources/elasticstack_kibana_security_exception_list/resource_endpoint.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| resource "elasticstack_kibana_security_exception_list" "endpoint" { | ||
| list_id = "my-endpoint-exception-list" | ||
| name = "My Endpoint Exception List" | ||
| description = "List of endpoint exceptions" | ||
| type = "endpoint" | ||
| namespace_type = "agnostic" | ||
|
|
||
| os_types = ["linux", "windows", "macos"] | ||
| tags = ["endpoint", "security"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| package kibana_oapi | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
|
|
||
| "github.com/elastic/terraform-provider-elasticstack/generated/kbapi" | ||
| "github.com/elastic/terraform-provider-elasticstack/internal/diagutil" | ||
| "github.com/hashicorp/terraform-plugin-framework/diag" | ||
| ) | ||
|
|
||
| // GetExceptionList reads an exception list from the API by ID or list_id | ||
| func GetExceptionList(ctx context.Context, client *Client, spaceId string, params *kbapi.ReadExceptionListParams) (*kbapi.ReadExceptionListResponse, diag.Diagnostics) { | ||
| resp, err := client.API.ReadExceptionListWithResponse(ctx, kbapi.SpaceId(spaceId), params) | ||
| if err != nil { | ||
| return nil, diagutil.FrameworkDiagFromError(err) | ||
| } | ||
|
|
||
| switch resp.StatusCode() { | ||
| case http.StatusOK: | ||
| return resp, nil | ||
| case http.StatusNotFound: | ||
| return nil, nil | ||
| default: | ||
| return nil, reportUnknownError(resp.StatusCode(), resp.Body) | ||
| } | ||
| } | ||
|
|
||
| // CreateExceptionList creates a new exception list. | ||
| func CreateExceptionList(ctx context.Context, client *Client, spaceId string, body kbapi.CreateExceptionListJSONRequestBody) (*kbapi.CreateExceptionListResponse, diag.Diagnostics) { | ||
| resp, err := client.API.CreateExceptionListWithResponse(ctx, kbapi.SpaceId(spaceId), body) | ||
| if err != nil { | ||
| return nil, diagutil.FrameworkDiagFromError(err) | ||
| } | ||
|
|
||
| switch resp.StatusCode() { | ||
| case http.StatusOK: | ||
| return resp, nil | ||
| default: | ||
| return nil, reportUnknownError(resp.StatusCode(), resp.Body) | ||
| } | ||
| } | ||
|
|
||
| // UpdateExceptionList updates an existing exception list. | ||
| func UpdateExceptionList(ctx context.Context, client *Client, spaceId string, body kbapi.UpdateExceptionListJSONRequestBody) (*kbapi.UpdateExceptionListResponse, diag.Diagnostics) { | ||
| resp, err := client.API.UpdateExceptionListWithResponse(ctx, kbapi.SpaceId(spaceId), body) | ||
| if err != nil { | ||
| return nil, diagutil.FrameworkDiagFromError(err) | ||
| } | ||
|
|
||
| switch resp.StatusCode() { | ||
| case http.StatusOK: | ||
| return resp, nil | ||
| default: | ||
| return nil, reportUnknownError(resp.StatusCode(), resp.Body) | ||
| } | ||
| } | ||
|
|
||
| // DeleteExceptionList deletes an existing exception list. | ||
| func DeleteExceptionList(ctx context.Context, client *Client, spaceId string, params *kbapi.DeleteExceptionListParams) diag.Diagnostics { | ||
| resp, err := client.API.DeleteExceptionListWithResponse(ctx, kbapi.SpaceId(spaceId), params) | ||
| if err != nil { | ||
| return diagutil.FrameworkDiagFromError(err) | ||
| } | ||
|
|
||
| switch resp.StatusCode() { | ||
| case http.StatusOK: | ||
| return nil | ||
| case http.StatusNotFound: | ||
| return nil | ||
| default: | ||
| return reportUnknownError(resp.StatusCode(), resp.Body) | ||
| } | ||
| } | ||
|
|
||
| // GetExceptionListItem reads an exception list item from the API by ID or item_id | ||
| func GetExceptionListItem(ctx context.Context, client *Client, spaceId string, params *kbapi.ReadExceptionListItemParams) (*kbapi.ReadExceptionListItemResponse, diag.Diagnostics) { | ||
| resp, err := client.API.ReadExceptionListItemWithResponse(ctx, kbapi.SpaceId(spaceId), params) | ||
| if err != nil { | ||
| return nil, diagutil.FrameworkDiagFromError(err) | ||
| } | ||
|
|
||
| switch resp.StatusCode() { | ||
| case http.StatusOK: | ||
| return resp, nil | ||
| case http.StatusNotFound: | ||
| return nil, nil | ||
| default: | ||
| return nil, reportUnknownError(resp.StatusCode(), resp.Body) | ||
| } | ||
| } | ||
|
|
||
| // CreateExceptionListItem creates a new exception list item. | ||
| func CreateExceptionListItem(ctx context.Context, client *Client, spaceId string, body kbapi.CreateExceptionListItemJSONRequestBody) (*kbapi.CreateExceptionListItemResponse, diag.Diagnostics) { | ||
| resp, err := client.API.CreateExceptionListItemWithResponse(ctx, kbapi.SpaceId(spaceId), body) | ||
| if err != nil { | ||
| return nil, diagutil.FrameworkDiagFromError(err) | ||
| } | ||
|
|
||
| switch resp.StatusCode() { | ||
| case http.StatusOK: | ||
| return resp, nil | ||
| default: | ||
| return nil, reportUnknownError(resp.StatusCode(), resp.Body) | ||
| } | ||
| } | ||
|
|
||
| // UpdateExceptionListItem updates an existing exception list item. | ||
| func UpdateExceptionListItem(ctx context.Context, client *Client, spaceId string, body kbapi.UpdateExceptionListItemJSONRequestBody) (*kbapi.UpdateExceptionListItemResponse, diag.Diagnostics) { | ||
| resp, err := client.API.UpdateExceptionListItemWithResponse(ctx, kbapi.SpaceId(spaceId), body) | ||
| if err != nil { | ||
| return nil, diagutil.FrameworkDiagFromError(err) | ||
| } | ||
|
|
||
| switch resp.StatusCode() { | ||
| case http.StatusOK: | ||
| return resp, nil | ||
| default: | ||
| return nil, reportUnknownError(resp.StatusCode(), resp.Body) | ||
| } | ||
| } | ||
|
|
||
| // DeleteExceptionListItem deletes an existing exception list item. | ||
| func DeleteExceptionListItem(ctx context.Context, client *Client, spaceId string, params *kbapi.DeleteExceptionListItemParams) diag.Diagnostics { | ||
| resp, err := client.API.DeleteExceptionListItemWithResponse(ctx, kbapi.SpaceId(spaceId), params) | ||
| if err != nil { | ||
| return diagutil.FrameworkDiagFromError(err) | ||
| } | ||
|
|
||
| switch resp.StatusCode() { | ||
| case http.StatusOK: | ||
| return nil | ||
| case http.StatusNotFound: | ||
| return nil | ||
| default: | ||
| return reportUnknownError(resp.StatusCode(), resp.Body) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| package security_exception_list_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/elastic/terraform-provider-elasticstack/internal/acctest" | ||
| "github.com/elastic/terraform-provider-elasticstack/internal/versionutils" | ||
| "github.com/google/uuid" | ||
| "github.com/hashicorp/go-version" | ||
| "github.com/hashicorp/terraform-plugin-testing/config" | ||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| ) | ||
|
|
||
| var minExceptionListAPISupport = version.Must(version.NewVersion("7.9.0")) | ||
|
|
||
| func TestAccResourceExceptionList(t *testing.T) { | ||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { acctest.PreCheck(t) }, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| SkipFunc: versionutils.CheckIfVersionIsUnsupported(minExceptionListAPISupport), | ||
| ProtoV6ProviderFactories: acctest.Providers, | ||
| ConfigDirectory: acctest.NamedTestCaseDirectory("create"), | ||
| ConfigVariables: config.Variables{ | ||
| "list_id": config.StringVariable("test-exception-list"), | ||
| "name": config.StringVariable("Test Exception List"), | ||
| "description": config.StringVariable("Test exception list for acceptance tests"), | ||
| "type": config.StringVariable("detection"), | ||
| "namespace_type": config.StringVariable("single"), | ||
| "tags": config.ListVariable(config.StringVariable("test")), | ||
| }, | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "list_id", "test-exception-list"), | ||
| resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "name", "Test Exception List"), | ||
| resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "description", "Test exception list for acceptance tests"), | ||
| resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "type", "detection"), | ||
| resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "namespace_type", "single"), | ||
| resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "tags.0", "test"), | ||
| resource.TestCheckResourceAttrSet("elasticstack_kibana_security_exception_list.test", "id"), | ||
| resource.TestCheckResourceAttrSet("elasticstack_kibana_security_exception_list.test", "created_at"), | ||
| resource.TestCheckResourceAttrSet("elasticstack_kibana_security_exception_list.test", "created_by"), | ||
| ), | ||
| }, | ||
| { | ||
| SkipFunc: versionutils.CheckIfVersionIsUnsupported(minExceptionListAPISupport), | ||
| ProtoV6ProviderFactories: acctest.Providers, | ||
| ConfigDirectory: acctest.NamedTestCaseDirectory("update"), | ||
| ConfigVariables: config.Variables{ | ||
| "list_id": config.StringVariable("test-exception-list"), | ||
| "name": config.StringVariable("Test Exception List Updated"), | ||
| "description": config.StringVariable("Updated description"), | ||
| "type": config.StringVariable("detection"), | ||
| "namespace_type": config.StringVariable("single"), | ||
| "tags": config.ListVariable(config.StringVariable("test"), config.StringVariable("updated")), | ||
| }, | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "name", "Test Exception List Updated"), | ||
| resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "description", "Updated description"), | ||
| resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "tags.0", "test"), | ||
| resource.TestCheckResourceAttr("elasticstack_kibana_security_exception_list.test", "tags.1", "updated"), | ||
| ), | ||
| }, | ||
| { // Import | ||
| SkipFunc: versionutils.CheckIfVersionIsUnsupported(minExceptionListAPISupport), | ||
| ProtoV6ProviderFactories: acctest.Providers, | ||
| ConfigDirectory: acctest.NamedTestCaseDirectory("update"), | ||
| ConfigVariables: config.Variables{ | ||
| "list_id": config.StringVariable("test-exception-list"), | ||
| "name": config.StringVariable("Test Exception List Updated"), | ||
| "description": config.StringVariable("Updated description"), | ||
| "type": config.StringVariable("detection"), | ||
| "namespace_type": config.StringVariable("single"), | ||
| "tags": config.ListVariable(config.StringVariable("test"), config.StringVariable("updated")), | ||
| }, | ||
| ResourceName: "elasticstack_kibana_security_exception_list.test", | ||
| ImportState: true, | ||
| ImportStateVerify: true, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func TestAccResourceExceptionListWithSpace(t *testing.T) { | ||
| resourceName := "elasticstack_kibana_security_exception_list.test" | ||
| spaceResourceName := "elasticstack_kibana_space.test" | ||
| spaceID := fmt.Sprintf("test-space-%s", uuid.New().String()[:8]) | ||
|
|
||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { acctest.PreCheck(t) }, | ||
| ProtoV6ProviderFactories: acctest.Providers, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| SkipFunc: versionutils.CheckIfVersionIsUnsupported(minExceptionListAPISupport), | ||
| ConfigDirectory: acctest.NamedTestCaseDirectory("create"), | ||
| ConfigVariables: config.Variables{ | ||
| "space_id": config.StringVariable(spaceID), | ||
| "list_id": config.StringVariable("test-exception-list-space"), | ||
| "name": config.StringVariable("Test Exception List in Space"), | ||
| "description": config.StringVariable("Test exception list in custom space"), | ||
| "type": config.StringVariable("detection"), | ||
| "namespace_type": config.StringVariable("single"), | ||
| "tags": config.ListVariable(config.StringVariable("test"), config.StringVariable("space")), | ||
| }, | ||
| Check: resource.ComposeTestCheckFunc( | ||
| // Check space attributes | ||
| resource.TestCheckResourceAttr(spaceResourceName, "space_id", spaceID), | ||
| resource.TestCheckResourceAttr(spaceResourceName, "name", "Test Space for Exception Lists"), | ||
|
|
||
| // Check exception list attributes | ||
| resource.TestCheckResourceAttr(resourceName, "space_id", spaceID), | ||
| resource.TestCheckResourceAttr(resourceName, "list_id", "test-exception-list-space"), | ||
| resource.TestCheckResourceAttr(resourceName, "name", "Test Exception List in Space"), | ||
| resource.TestCheckResourceAttr(resourceName, "description", "Test exception list in custom space"), | ||
| resource.TestCheckResourceAttr(resourceName, "type", "detection"), | ||
| resource.TestCheckResourceAttr(resourceName, "namespace_type", "single"), | ||
| resource.TestCheckResourceAttr(resourceName, "tags.0", "test"), | ||
| resource.TestCheckResourceAttr(resourceName, "tags.1", "space"), | ||
| resource.TestCheckResourceAttrSet(resourceName, "id"), | ||
| resource.TestCheckResourceAttrSet(resourceName, "created_at"), | ||
| resource.TestCheckResourceAttrSet(resourceName, "created_by"), | ||
| ), | ||
| }, | ||
| { | ||
| SkipFunc: versionutils.CheckIfVersionIsUnsupported(minExceptionListAPISupport), | ||
| ConfigDirectory: acctest.NamedTestCaseDirectory("update"), | ||
| ConfigVariables: config.Variables{ | ||
| "space_id": config.StringVariable(spaceID), | ||
| "list_id": config.StringVariable("test-exception-list-space"), | ||
| "name": config.StringVariable("Test Exception List in Space Updated"), | ||
| "description": config.StringVariable("Updated description in space"), | ||
| "type": config.StringVariable("detection"), | ||
| "namespace_type": config.StringVariable("single"), | ||
| "tags": config.ListVariable(config.StringVariable("test"), config.StringVariable("space"), config.StringVariable("updated")), | ||
| }, | ||
| Check: resource.ComposeTestCheckFunc( | ||
| // Check space attributes remain the same | ||
| resource.TestCheckResourceAttr(spaceResourceName, "space_id", spaceID), | ||
| resource.TestCheckResourceAttr(spaceResourceName, "name", "Test Space for Exception Lists"), | ||
|
|
||
| // Check updated exception list attributes | ||
| resource.TestCheckResourceAttr(resourceName, "space_id", spaceID), | ||
| resource.TestCheckResourceAttr(resourceName, "name", "Test Exception List in Space Updated"), | ||
| resource.TestCheckResourceAttr(resourceName, "description", "Updated description in space"), | ||
| resource.TestCheckResourceAttr(resourceName, "tags.0", "test"), | ||
| resource.TestCheckResourceAttr(resourceName, "tags.1", "space"), | ||
| resource.TestCheckResourceAttr(resourceName, "tags.2", "updated"), | ||
| ), | ||
| }, | ||
| { // Import | ||
| SkipFunc: versionutils.CheckIfVersionIsUnsupported(minExceptionListAPISupport), | ||
| ConfigDirectory: acctest.NamedTestCaseDirectory("update"), | ||
| ConfigVariables: config.Variables{ | ||
| "space_id": config.StringVariable(spaceID), | ||
| "list_id": config.StringVariable("test-exception-list-space"), | ||
| "name": config.StringVariable("Test Exception List in Space Updated"), | ||
| "description": config.StringVariable("Updated description in space"), | ||
| "type": config.StringVariable("detection"), | ||
| "namespace_type": config.StringVariable("single"), | ||
| "tags": config.ListVariable(config.StringVariable("test"), config.StringVariable("space"), config.StringVariable("updated")), | ||
| }, | ||
| ResourceName: resourceName, | ||
| ImportState: true, | ||
| ImportStateVerify: true, | ||
| }, | ||
| }, | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.