Skip to content

Commit 12e5203

Browse files
committed
Merge branch 'copilot/add-elastic-security-exceptions' of github.com:elastic/terraform-provider-elasticstack into copilot/add-elastic-security-exceptions
2 parents 70ca678 + 06bcdaf commit 12e5203

File tree

6 files changed

+33
-58
lines changed

6 files changed

+33
-58
lines changed

docs/resources/kibana_security_exception_item.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ resource "elasticstack_kibana_security_exception_item" "complex_entry" {
103103
### Optional
104104

105105
- `comments` (Attributes List) Array of comments about the exception item. (see [below for nested schema](#nestedatt--comments))
106-
- `expire_time` (String) The exception item's expiration date in ISO format. This field is only available for regular exception items, not endpoint exceptions.
106+
- `expire_time` (String) The exception item's expiration date in RFC3339 format. This field is only available for regular exception items, not endpoint exceptions.
107107
- `item_id` (String) The exception item's human readable string identifier.
108108
- `meta` (String) Placeholder for metadata about the exception item as JSON string.
109109
- `namespace_type` (String) Determines whether the exception item is available in all Kibana spaces or just the space in which it is created. Can be `single` (default) or `agnostic`.

internal/kibana/security/exception_item/create.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
99
"github.com/elastic/terraform-provider-elasticstack/internal/clients/kibana_oapi"
1010
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
11+
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
1112
"github.com/hashicorp/terraform-plugin-framework/attr"
1213
"github.com/hashicorp/terraform-plugin-framework/diag"
1314
"github.com/hashicorp/terraform-plugin-framework/resource"
@@ -140,24 +141,8 @@ func (r *ExceptionItemResource) Create(ctx context.Context, req resource.CreateR
140141
return
141142
}
142143

143-
// Read back the created resource to get computed fields
144-
readParams := &kbapi.ReadExceptionListItemParams{
145-
Id: (*kbapi.SecurityExceptionsAPIExceptionListItemId)(&createResp.JSON200.Id),
146-
}
147-
148-
readResp, diags := kibana_oapi.GetExceptionListItem(ctx, client, readParams)
149-
resp.Diagnostics.Append(diags...)
150-
if resp.Diagnostics.HasError() {
151-
return
152-
}
153-
154-
if readResp == nil || readResp.JSON200 == nil {
155-
resp.Diagnostics.AddError("Failed to read created exception item", "API returned empty response")
156-
return
157-
}
158-
159-
// Update state with response
160-
diags = r.updateStateFromAPIResponse(ctx, &plan, readResp.JSON200)
144+
// Update state with create response
145+
diags = r.updateStateFromAPIResponse(ctx, &plan, createResp.JSON200)
161146
resp.Diagnostics.Append(diags...)
162147
if resp.Diagnostics.HasError() {
163148
return
@@ -224,13 +209,13 @@ func (r *ExceptionItemResource) updateStateFromAPIResponse(ctx context.Context,
224209
model.Meta = types.StringNull()
225210
}
226211

227-
// Set entries (convert back to JSON)
212+
// Set entries (convert back to JSON and normalize)
228213
entriesJSON, err := json.Marshal(apiResp.Entries)
229214
if err != nil {
230215
diags.AddError("Failed to serialize entries", err.Error())
231216
return diags
232217
}
233-
model.Entries = types.StringValue(string(entriesJSON))
218+
model.Entries = jsontypes.NewNormalizedValue(string(entriesJSON))
234219

235220
// Set optional comments
236221
if len(apiResp.Comments) > 0 {

internal/kibana/security/exception_item/models.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
package exception_item
22

33
import (
4+
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
45
"github.com/hashicorp/terraform-plugin-framework/types"
56
)
67

78
type ExceptionItemModel struct {
8-
ID types.String `tfsdk:"id"`
9-
ItemID types.String `tfsdk:"item_id"`
10-
ListID types.String `tfsdk:"list_id"`
11-
Name types.String `tfsdk:"name"`
12-
Description types.String `tfsdk:"description"`
13-
Type types.String `tfsdk:"type"`
14-
NamespaceType types.String `tfsdk:"namespace_type"`
15-
OsTypes types.List `tfsdk:"os_types"`
16-
Tags types.List `tfsdk:"tags"`
17-
Meta types.String `tfsdk:"meta"`
18-
Entries types.String `tfsdk:"entries"`
19-
Comments types.List `tfsdk:"comments"`
20-
ExpireTime types.String `tfsdk:"expire_time"`
21-
CreatedAt types.String `tfsdk:"created_at"`
22-
CreatedBy types.String `tfsdk:"created_by"`
23-
UpdatedAt types.String `tfsdk:"updated_at"`
24-
UpdatedBy types.String `tfsdk:"updated_by"`
25-
TieBreakerID types.String `tfsdk:"tie_breaker_id"`
9+
ID types.String `tfsdk:"id"`
10+
ItemID types.String `tfsdk:"item_id"`
11+
ListID types.String `tfsdk:"list_id"`
12+
Name types.String `tfsdk:"name"`
13+
Description types.String `tfsdk:"description"`
14+
Type types.String `tfsdk:"type"`
15+
NamespaceType types.String `tfsdk:"namespace_type"`
16+
OsTypes types.List `tfsdk:"os_types"`
17+
Tags types.List `tfsdk:"tags"`
18+
Meta types.String `tfsdk:"meta"`
19+
Entries jsontypes.Normalized `tfsdk:"entries"`
20+
Comments types.List `tfsdk:"comments"`
21+
ExpireTime types.String `tfsdk:"expire_time"`
22+
CreatedAt types.String `tfsdk:"created_at"`
23+
CreatedBy types.String `tfsdk:"created_by"`
24+
UpdatedAt types.String `tfsdk:"updated_at"`
25+
UpdatedBy types.String `tfsdk:"updated_by"`
26+
TieBreakerID types.String `tfsdk:"tie_breaker_id"`
2627
}
2728

2829
type CommentModel struct {

internal/kibana/security/exception_item/schema.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
_ "embed"
66

7+
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
78
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
89
"github.com/hashicorp/terraform-plugin-framework/resource"
910
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -91,6 +92,7 @@ func (r *ExceptionItemResource) Schema(_ context.Context, _ resource.SchemaReque
9192
"entries": schema.StringAttribute{
9293
MarkdownDescription: "The exception item entries as JSON string. This defines the conditions under which the exception applies.",
9394
Required: true,
95+
CustomType: jsontypes.NormalizedType{},
9496
},
9597
"comments": schema.ListNestedAttribute{
9698
MarkdownDescription: "Array of comments about the exception item.",
@@ -109,7 +111,7 @@ func (r *ExceptionItemResource) Schema(_ context.Context, _ resource.SchemaReque
109111
},
110112
},
111113
"expire_time": schema.StringAttribute{
112-
MarkdownDescription: "The exception item's expiration date in ISO format. This field is only available for regular exception items, not endpoint exceptions.",
114+
MarkdownDescription: "The exception item's expiration date in RFC3339 format. This field is only available for regular exception items, not endpoint exceptions.",
113115
Optional: true,
114116
},
115117
"created_at": schema.StringAttribute{

internal/kibana/security/exception_list/create.go

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,8 @@ func (r *ExceptionListResource) Create(ctx context.Context, req resource.CreateR
9494
return
9595
}
9696

97-
// Read back the created resource to get computed fields
98-
readParams := &kbapi.ReadExceptionListParams{
99-
Id: (*kbapi.SecurityExceptionsAPIExceptionListId)(&createResp.JSON200.Id),
100-
}
101-
102-
readResp, diags := kibana_oapi.GetExceptionList(ctx, client, readParams)
103-
resp.Diagnostics.Append(diags...)
104-
if resp.Diagnostics.HasError() {
105-
return
106-
}
107-
108-
if readResp == nil || readResp.JSON200 == nil {
109-
resp.Diagnostics.AddError("Failed to read created exception list", "API returned empty response")
110-
return
111-
}
112-
113-
// Update state with response
114-
diags = r.updateStateFromAPIResponse(ctx, &plan, readResp.JSON200)
97+
// Update state with create response
98+
diags = r.updateStateFromAPIResponse(ctx, &plan, createResp.JSON200)
11599
resp.Diagnostics.Append(diags...)
116100
if resp.Diagnostics.HasError() {
117101
return

internal/kibana/security/exception_list/update.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ func (r *ExceptionListResource) Update(ctx context.Context, req resource.UpdateR
3131
Id: &id,
3232
Name: kbapi.SecurityExceptionsAPIExceptionListName(plan.Name.ValueString()),
3333
Description: kbapi.SecurityExceptionsAPIExceptionListDescription(plan.Description.ValueString()),
34+
// Type is required by the API even though it has RequiresReplace in the schema
35+
// The API will reject updates without this field, even though the value cannot change
36+
Type: kbapi.SecurityExceptionsAPIExceptionListType(plan.Type.ValueString()),
3437
}
3538

3639
// Set optional namespace_type (should not change, but include it)

0 commit comments

Comments
 (0)