Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Add support for managing cross_cluster API keys in `elasticstack_elasticsearch_security_api_key` ([#1252](https://github.com/elastic/terraform-provider-elasticstack/pull/1252))
- Allow version changes without a destroy/create cycle with `elasticstack_fleet_integration` ([#1255](https://github.com/elastic/terraform-provider-elasticstack/pull/1255)). This fixes an issue where it was impossible to upgrade integrations which are used by an integration policy.
- Add `namespace` attribute to `elasticstack_kibana_synthetics_monitor` resource to support setting data stream namespace independently from `space_id` ([#1247](https://github.com/elastic/terraform-provider-elasticstack/pull/1247))
- Migrate `elasticstack_elasticsearch_security_role_mapping` resource and data source to Terraform Plugin Framework ([#1279](https://github.com/elastic/terraform-provider-elasticstack/pull/1279))

## [0.11.17] - 2025-07-21

Expand Down
2 changes: 1 addition & 1 deletion docs/data-sources/elasticsearch_security_role_mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ output "user" {

### Optional

- `elasticsearch_connection` (Block List, Max: 1, Deprecated) Elasticsearch connection configuration block. This property will be removed in a future provider version. Configure the Elasticsearch connection via the provider configuration instead. (see [below for nested schema](#nestedblock--elasticsearch_connection))
- `elasticsearch_connection` (Block List, Deprecated) Elasticsearch connection configuration block. (see [below for nested schema](#nestedblock--elasticsearch_connection))

### Read-Only

Expand Down
2 changes: 1 addition & 1 deletion docs/resources/elasticsearch_security_role_mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ output "role" {

### Optional

- `elasticsearch_connection` (Block List, Max: 1, Deprecated) Elasticsearch connection configuration block. This property will be removed in a future provider version. Configure the Elasticsearch connection via the provider configuration instead. (see [below for nested schema](#nestedblock--elasticsearch_connection))
- `elasticsearch_connection` (Block List, Deprecated) Elasticsearch connection configuration block. (see [below for nested schema](#nestedblock--elasticsearch_connection))
- `enabled` (Boolean) Mappings that have `enabled` set to `false` are ignored when role mapping is performed.
- `metadata` (String) Additional metadata that helps define which roles are assigned to each user. Keys beginning with `_` are reserved for system usage.
- `role_templates` (String) A list of mustache templates that will be evaluated to determine the roles names that should granted to the users that match the role mapping rules.
Expand Down
53 changes: 34 additions & 19 deletions internal/clients/elasticsearch/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,73 +252,88 @@ func DeleteRole(ctx context.Context, apiClient *clients.ApiClient, rolename stri
return diags
}

func PutRoleMapping(ctx context.Context, apiClient *clients.ApiClient, roleMapping *models.RoleMapping) diag.Diagnostics {
func PutRoleMapping(ctx context.Context, apiClient *clients.ApiClient, roleMapping *models.RoleMapping) fwdiag.Diagnostics {
var diags fwdiag.Diagnostics
roleMappingBytes, err := json.Marshal(roleMapping)
if err != nil {
return diag.FromErr(err)
diags.AddError("Unable to marshal role mapping", err.Error())
return diags
}
esClient, err := apiClient.GetESClient()
if err != nil {
return diag.FromErr(err)
diags.AddError("Unable to get Elasticsearch client", err.Error())
return diags
}
res, err := esClient.Security.PutRoleMapping(roleMapping.Name, bytes.NewReader(roleMappingBytes), esClient.Security.PutRoleMapping.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
diags.AddError("Unable to put role mapping", err.Error())
return diags
}
defer res.Body.Close()
if diags := utils.CheckError(res, "Unable to put role mapping"); diags.HasError() {
if sdkDiags := utils.CheckError(res, "Unable to put role mapping"); sdkDiags.HasError() {
diags.Append(utils.FrameworkDiagsFromSDK(sdkDiags)...)
return diags
}

return nil
return diags
}

func GetRoleMapping(ctx context.Context, apiClient *clients.ApiClient, roleMappingName string) (*models.RoleMapping, diag.Diagnostics) {
func GetRoleMapping(ctx context.Context, apiClient *clients.ApiClient, roleMappingName string) (*models.RoleMapping, fwdiag.Diagnostics) {
var diags fwdiag.Diagnostics
esClient, err := apiClient.GetESClient()
if err != nil {
return nil, diag.FromErr(err)
diags.AddError("Unable to get Elasticsearch client", err.Error())
return nil, diags
}
req := esClient.Security.GetRoleMapping.WithName(roleMappingName)
res, err := esClient.Security.GetRoleMapping(req, esClient.Security.GetRoleMapping.WithContext(ctx))
if err != nil {
return nil, diag.FromErr(err)
diags.AddError("Unable to get role mapping", err.Error())
return nil, diags
}
defer res.Body.Close()

if res.StatusCode == http.StatusNotFound {
return nil, nil
return nil, diags
}
if diags := utils.CheckError(res, "Unable to get a role mapping."); diags.HasError() {
if sdkDiags := utils.CheckError(res, "Unable to get a role mapping."); sdkDiags.HasError() {
diags.Append(utils.FrameworkDiagsFromSDK(sdkDiags)...)
return nil, diags
}
roleMappings := make(map[string]models.RoleMapping)
if err := json.NewDecoder(res.Body).Decode(&roleMappings); err != nil {
return nil, diag.FromErr(err)
diags.AddError("Unable to decode role mapping response", err.Error())
return nil, diags

}
if roleMapping, ok := roleMappings[roleMappingName]; ok {
roleMapping.Name = roleMappingName
return &roleMapping, nil
return &roleMapping, diags
}

return nil, diag.Errorf("unable to find role mapping '%s' in the cluster", roleMappingName)
diags.AddError("Role mapping not found", fmt.Sprintf("unable to find role mapping '%s' in the cluster", roleMappingName))
return nil, diags
}

func DeleteRoleMapping(ctx context.Context, apiClient *clients.ApiClient, roleMappingName string) diag.Diagnostics {
func DeleteRoleMapping(ctx context.Context, apiClient *clients.ApiClient, roleMappingName string) fwdiag.Diagnostics {
var diags fwdiag.Diagnostics
esClient, err := apiClient.GetESClient()
if err != nil {
return diag.FromErr(err)
diags.AddError("Unable to get Elasticsearch client", err.Error())
return diags
}
res, err := esClient.Security.DeleteRoleMapping(roleMappingName, esClient.Security.DeleteRoleMapping.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
diags.AddError("Unable to delete role mapping", err.Error())
return diags
}
defer res.Body.Close()
if diags := utils.CheckError(res, "Unable to delete role mapping"); diags.HasError() {
if sdkDiags := utils.CheckError(res, "Unable to delete role mapping"); sdkDiags.HasError() {
diags.Append(utils.FrameworkDiagsFromSDK(sdkDiags)...)
return diags
}

return nil
return diags
}

func CreateApiKey(apiClient *clients.ApiClient, apikey *models.ApiKey) (*models.ApiKeyCreateResponse, fwdiag.Diagnostics) {
Expand Down
198 changes: 0 additions & 198 deletions internal/elasticsearch/security/role_mapping.go

This file was deleted.

Loading
Loading