-
Notifications
You must be signed in to change notification settings - Fork 105
Add security role data source #177
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
29c9b71
Add data-source for security role
k-yomo d07ce1b
Generate docs for security role data-source
k-yomo 49e0b1b
Update CHANGELOG
k-yomo 60731e1
Fix docs to include subcategory
6ec61a4
Fix typo in docs
a128ab5
Merge branch 'main' into add-role-data-source
d8f385d
Fix conflicting constant name
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
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
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,91 @@ | ||
--- | ||
subcategory: "Security" | ||
layout: "" | ||
page_title: "Elasticstack: elasticstack_elasticsearch_security_role Data Source" | ||
description: |- | ||
Retrieves roles in the native realm. | ||
--- | ||
|
||
# Data Source: elasticstack_elasticsearch_security_role | ||
|
||
Use this data source to information about existing Elasticsearch role. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-get-role.html | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
provider "elasticstack" { | ||
elasticsearch {} | ||
} | ||
|
||
data "elasticstack_elasticsearch_security_role" "role" { | ||
name = "testrole" | ||
} | ||
|
||
output "role" { | ||
value = data.elasticstack_elasticsearch_security_role.role.name | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) The name of the role. | ||
|
||
### Optional | ||
|
||
- `elasticsearch_connection` (Block List, Max: 1) Used to establish connection to Elasticsearch server. Overrides environment variables if present. (see [below for nested schema](#nestedblock--elasticsearch_connection)) | ||
- `run_as` (Set of String) A list of users that the owners of this role can impersonate. | ||
|
||
### Read-Only | ||
|
||
- `applications` (Set of Object) A list of application privilege entries. (see [below for nested schema](#nestedatt--applications)) | ||
- `cluster` (Set of String) A list of cluster privileges. These privileges define the cluster level actions that users with this role are able to execute. | ||
- `global` (String) An object defining global privileges. | ||
- `id` (String) Internal identifier of the resource | ||
- `indices` (Set of Object) A list of indices permissions entries. (see [below for nested schema](#nestedatt--indices)) | ||
- `metadata` (String) Optional meta-data. | ||
|
||
<a id="nestedblock--elasticsearch_connection"></a> | ||
### Nested Schema for `elasticsearch_connection` | ||
|
||
Optional: | ||
|
||
- `api_key` (String, Sensitive) API Key to use for authentication to Elasticsearch | ||
- `ca_data` (String) PEM-encoded custom Certificate Authority certificate | ||
- `ca_file` (String) Path to a custom Certificate Authority certificate | ||
- `endpoints` (List of String, Sensitive) A list of endpoints the Terraform provider will point to. They must include the http(s) schema and port number. | ||
- `insecure` (Boolean) Disable TLS certificate validation | ||
- `password` (String, Sensitive) A password to use for API authentication to Elasticsearch. | ||
- `username` (String) A username to use for API authentication to Elasticsearch. | ||
|
||
|
||
<a id="nestedatt--applications"></a> | ||
### Nested Schema for `applications` | ||
|
||
Read-Only: | ||
|
||
- `application` (String) | ||
- `privileges` (Set of String) | ||
- `resources` (Set of String) | ||
|
||
|
||
<a id="nestedatt--indices"></a> | ||
### Nested Schema for `indices` | ||
|
||
Read-Only: | ||
|
||
- `allow_restricted_indices` (Boolean) | ||
- `field_security` (List of Object) (see [below for nested schema](#nestedobjatt--indices--field_security)) | ||
- `names` (Set of String) | ||
- `privileges` (Set of String) | ||
- `query` (String) | ||
|
||
<a id="nestedobjatt--indices--field_security"></a> | ||
### Nested Schema for `indices.field_security` | ||
|
||
Read-Only: | ||
|
||
- `except` (Set of String) | ||
- `grant` (Set of String) |
11 changes: 11 additions & 0 deletions
11
examples/data-sources/elasticstack_elasticsearch_security_role/data-source.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,11 @@ | ||
provider "elasticstack" { | ||
elasticsearch {} | ||
} | ||
|
||
data "elasticstack_elasticsearch_security_role" "role" { | ||
name = "testrole" | ||
} | ||
|
||
output "role" { | ||
value = data.elasticstack_elasticsearch_security_role.role.name | ||
} |
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,165 @@ | ||
package security | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com./elastic/terraform-provider-elasticstack/internal/clients" | ||
"github.com./elastic/terraform-provider-elasticstack/internal/utils" | ||
"github.com./hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com./hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func DataSourceRole() *schema.Resource { | ||
roleSchema := map[string]*schema.Schema{ | ||
"id": { | ||
Description: "Internal identifier of the resource", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"name": { | ||
Description: "The name of the role.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"applications": { | ||
Description: "A list of application privilege entries.", | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"application": { | ||
Description: "The name of the application to which this entry applies.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"privileges": { | ||
Description: "A list of strings, where each element is the name of an application privilege or action.", | ||
Type: schema.TypeSet, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
Computed: true, | ||
}, | ||
"resources": { | ||
Description: "A list resources to which the privileges are applied.", | ||
Type: schema.TypeSet, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"global": { | ||
Description: "An object defining global privileges.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"cluster": { | ||
Description: "A list of cluster privileges. These privileges define the cluster level actions that users with this role are able to execute.", | ||
Type: schema.TypeSet, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
Computed: true, | ||
}, | ||
"indices": { | ||
Description: "A list of indices permissions entries.", | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"field_security": { | ||
Description: "The document fields that the owners of the role have read access to.", | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"grant": { | ||
Description: "List of the fields to grant the access to.", | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"except": { | ||
Description: "List of the fields to which the grants will not be applied.", | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"names": { | ||
Description: "A list of indices (or index name patterns) to which the permissions in this entry apply.", | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"privileges": { | ||
Description: "The index level privileges that the owners of the role have on the specified indices.", | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"query": { | ||
Description: "A search query that defines the documents the owners of the role have read access to.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"allow_restricted_indices": { | ||
Description: "Include matching restricted indices in names parameter. Usage is strongly discouraged as it can grant unrestricted operations on critical data, make the entire system unstable or leak sensitive information.", | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"metadata": { | ||
Description: "Optional meta-data.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"run_as": { | ||
Description: "A list of users that the owners of this role can impersonate.", | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
} | ||
|
||
utils.AddConnectionSchema(roleSchema) | ||
|
||
return &schema.Resource{ | ||
Description: "Retrieves roles in the native realm. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-get-role.html", | ||
ReadContext: dataSourceSecurityRoleRead, | ||
Schema: roleSchema, | ||
} | ||
} | ||
|
||
func dataSourceSecurityRoleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client, err := clients.NewApiClient(d, meta) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
roleId := d.Get("name").(string) | ||
id, diags := client.ID(ctx, roleId) | ||
if diags.HasError() { | ||
return diags | ||
} | ||
d.SetId(id.String()) | ||
|
||
return resourceSecurityRoleRead(ctx, d, meta) | ||
} |
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,66 @@ | ||
package security_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com./elastic/terraform-provider-elasticstack/internal/acctest" | ||
"github.com./elastic/terraform-provider-elasticstack/internal/utils" | ||
"github.com./hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceSecurityRole(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { acctest.PreCheck(t) }, | ||
ProviderFactories: acctest.Providers, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceSecurityRole, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_security_role.test", "name", "data_source_test"), | ||
resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_security_role.test", "cluster.*", "all"), | ||
utils.TestCheckResourceListAttr("data.elasticstack_elasticsearch_security_role.test", "indices.0.names", []string{"index1", "index2"}), | ||
resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_security_role.test", "indices.0.privileges.*", "all"), | ||
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_security_role.test", "indices.0.allow_restricted_indices", "true"), | ||
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_security_role.test", "applications.0.application", "myapp"), | ||
utils.TestCheckResourceListAttr("data.elasticstack_elasticsearch_security_role.test", "applications.0.privileges", []string{"admin", "read"}), | ||
resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_security_role.test", "applications.0.resources.*", "*"), | ||
resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_security_role.test", "run_as.*", "other_user"), | ||
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_security_role.test", "metadata", `{"version":1}`), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccDataSourceSecurityRole = ` | ||
provider "elasticstack" { | ||
elasticsearch {} | ||
} | ||
|
||
resource "elasticstack_elasticsearch_security_role" "test" { | ||
name = "data_source_test" | ||
cluster = ["all"] | ||
|
||
indices { | ||
names = ["index1", "index2"] | ||
privileges = ["all"] | ||
allow_restricted_indices = true | ||
} | ||
|
||
applications { | ||
application = "myapp" | ||
privileges = ["admin", "read"] | ||
resources = ["*"] | ||
} | ||
|
||
run_as = ["other_user"] | ||
|
||
metadata = jsonencode({ | ||
version = 1 | ||
}) | ||
} | ||
|
||
data "elasticstack_elasticsearch_security_role" "test" { | ||
name = elasticstack_elasticsearch_security_role.test.name | ||
} | ||
` |
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
17 changes: 17 additions & 0 deletions
17
templates/data-sources/elasticsearch_security_role.md.tmpl
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,17 @@ | ||
--- | ||
subcategory: "Security" | ||
layout: "" | ||
page_title: "Elasticstack: elasticstack_elasticsearch_security_role Data Source" | ||
description: |- | ||
Retrieves roles in the native realm. | ||
--- | ||
|
||
# Data Source: elasticstack_elasticsearch_security_role | ||
|
||
Use this data source to information about existing Elasticsearch role. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-get-role.html | ||
|
||
## Example Usage | ||
|
||
{{ tffile "examples/data-sources/elasticstack_elasticsearch_security_role/data-source.tf" }} | ||
|
||
{{ .SchemaMarkdown | trimspace }} |
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.
Maybe there's other words you intended to have here, happy to use those instead too :)
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.
Whoops, I guess I just deleted it by mistake, fixed🙏
6ec61a4