/
data.go
403 lines (319 loc) · 11.5 KB
/
data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package privatestate
import (
"context"
"encoding/json"
"fmt"
"reflect"
"strings"
"unicode/utf8"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/logging"
)
// Data contains private state data for the framework and providers.
type Data struct {
// Potential future usage:
// Framework contains private state data for framework usage.
Framework map[string][]byte
// Provider contains private state data for provider usage.
Provider *ProviderData
}
// Bytes returns a JSON encoded slice of bytes containing the merged
// framework and provider private state data.
func (d *Data) Bytes(ctx context.Context) ([]byte, diag.Diagnostics) {
var diags diag.Diagnostics
if d == nil {
return nil, nil
}
if (d.Provider == nil || len(d.Provider.data) == 0) && len(d.Framework) == 0 {
return nil, nil
}
var providerData map[string][]byte
if d.Provider != nil {
providerData = d.Provider.data
}
mergedMap := make(map[string][]byte, len(d.Framework)+len(providerData))
for _, m := range []map[string][]byte{d.Framework, providerData} {
for k, v := range m {
if len(v) == 0 {
continue
}
// Values in FrameworkData and ProviderData should never be invalid UTF-8, but let's make sure.
if !utf8.Valid(v) {
diags.AddError(
"Error Encoding Private State",
"An error was encountered when validating private state value."+
fmt.Sprintf("The value associated with key %q is is not valid UTF-8.\n\n", k)+
"This is always a problem with Terraform or terraform-plugin-framework. Please report this to the provider developer.",
)
tflog.Error(ctx, "error encoding private state: invalid UTF-8 value", map[string]interface{}{"key": k, "value": v})
continue
}
// Values in FrameworkData and ProviderData should never be invalid JSON, but let's make sure.
if !json.Valid(v) {
diags.AddError(
"Error Encoding Private State",
fmt.Sprintf("An error was encountered when validating private state value."+
fmt.Sprintf("The value associated with key %q is is not valid JSON.\n\n", k)+
"This is always a problem with Terraform or terraform-plugin-framework. Please report this to the provider developer."),
)
tflog.Error(ctx, "error encoding private state: invalid JSON value", map[string]interface{}{"key": k, "value": v})
continue
}
mergedMap[k] = v
}
}
if diags.HasError() {
return nil, diags
}
bytes, err := json.Marshal(mergedMap)
if err != nil {
diags.AddError(
"Error Encoding Private State",
fmt.Sprintf("An error was encountered when encoding private state: %s.\n\n"+
"This is always a problem with Terraform or terraform-plugin-framework. Please report this to the provider developer.", err),
)
return nil, diags
}
return bytes, diags
}
// NewData creates a new Data based on the given slice of bytes.
// It must be a JSON encoded slice of bytes, that is map[string][]byte.
func NewData(ctx context.Context, data []byte) (*Data, diag.Diagnostics) {
var (
dataMap map[string][]byte
diags diag.Diagnostics
)
if len(data) == 0 {
return nil, nil
}
err := json.Unmarshal(data, &dataMap)
if err != nil {
// terraform-plugin-sdk stored private state by marshalling its data
// as map[string]any, which is slightly incompatible with trying to
// unmarshal it as map[string][]byte. If unmarshalling with
// map[string]any works, we can ignore it for now, as provider
// developers did not have access to managing the private state data.
//
// TODO: We can extract the terraform-plugin-sdk resource timeouts key
// here to extract its prior data, if necessary.
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/400
if anyErr := json.Unmarshal(data, new(map[string]any)); anyErr == nil {
logging.FrameworkWarn(ctx, "Discarding incompatible resource private state data", map[string]any{logging.KeyError: err.Error()})
return nil, nil
}
diags.AddError(
"Error Decoding Private State",
fmt.Sprintf("An error was encountered when decoding private state: %s.\n\n"+
"This is always a problem with Terraform or terraform-plugin-framework. Please report this to the provider developer.", err),
)
return nil, diags
}
output := Data{
Framework: make(map[string][]byte),
Provider: &ProviderData{
make(map[string][]byte),
},
}
for k, v := range dataMap {
if !utf8.Valid(v) {
diags.AddError(
"Error Decoding Private State",
"An error was encountered when validating private state value.\n"+
fmt.Sprintf("The value being supplied for key %q is is not valid UTF-8.\n\n", k)+
"This is always a problem with Terraform or terraform-plugin-framework. Please report this to the provider developer.",
)
tflog.Error(ctx, "error decoding private state: invalid UTF-8 value", map[string]interface{}{"key": k, "value": v})
continue
}
if !json.Valid(v) {
diags.AddError(
"Error Decoding Private State",
"An error was encountered when validating private state value.\n"+
fmt.Sprintf("The value being supplied for key %q is is not valid JSON.\n\n", k)+
"This is always a problem with Terraform or terraform-plugin-framework. Please report this to the provider developer.",
)
tflog.Error(ctx, "error decoding private state: invalid JSON value", map[string]interface{}{"key": k, "value": v})
continue
}
if isInvalidProviderDataKey(ctx, k) {
output.Framework[k] = v
continue
}
output.Provider.data[k] = v
}
if diags.HasError() {
return nil, diags
}
return &output, diags
}
// EmptyData creates an initialised but empty Data.
func EmptyData(ctx context.Context) *Data {
return &Data{
Provider: EmptyProviderData(ctx),
}
}
// NewProviderData creates a new ProviderData based on the given slice of bytes.
// It must be a JSON encoded slice of bytes, that is map[string][]byte.
func NewProviderData(ctx context.Context, data []byte) (*ProviderData, diag.Diagnostics) {
providerData := EmptyProviderData(ctx)
if len(data) == 0 {
return providerData, nil
}
var (
dataMap map[string][]byte
diags diag.Diagnostics
)
err := json.Unmarshal(data, &dataMap)
if err != nil {
diags.AddError(
"Error Decoding Provider Data",
fmt.Sprintf("An error was encountered when decoding provider data: %s.\n\n"+
"Please check that the data you are supplying is a byte representation of valid JSON.", err),
)
return nil, diags
}
for k, v := range dataMap {
diags.Append(providerData.SetKey(ctx, k, v)...)
}
if diags.HasError() {
return nil, diags
}
return providerData, diags
}
// EmptyProviderData creates a ProviderData containing initialised but empty data.
func EmptyProviderData(ctx context.Context) *ProviderData {
return &ProviderData{
data: make(map[string][]byte),
}
}
// ProviderData contains private state data for provider usage.
type ProviderData struct {
data map[string][]byte
}
// Equal returns true if the given ProviderData is exactly equivalent. The
// internal data is compared byte-for-byte, not accounting for semantic
// equivalency such as JSON whitespace or property reordering.
func (d *ProviderData) Equal(o *ProviderData) bool {
if d == nil && o == nil {
return true
}
if d == nil || o == nil {
return false
}
if !reflect.DeepEqual(d.data, o.data) {
return false
}
return true
}
// GetKey returns the private state data associated with the given key.
//
// If the key is reserved for framework usage, an error diagnostic
// is returned. If the key is valid, but private state data is not found,
// nil is returned.
//
// The naming of keys only matters in context of a single resource,
// however care should be taken that any historical keys are not reused
// without accounting for older resource instances that may still have
// older data at the key.
func (d *ProviderData) GetKey(ctx context.Context, key string) ([]byte, diag.Diagnostics) {
if d == nil || d.data == nil {
return nil, nil
}
diags := ValidateProviderDataKey(ctx, key)
if diags.HasError() {
return nil, diags
}
value, ok := d.data[key]
if !ok {
return nil, nil
}
return value, nil
}
// SetKey sets the private state data at the given key.
//
// If the key is reserved for framework usage, an error diagnostic
// is returned. The data must be valid JSON and UTF-8 safe or an error
// diagnostic is returned.
//
// The naming of keys only matters in context of a single resource,
// however care should be taken that any historical keys are not reused
// without accounting for older resource instances that may still have
// older data at the key.
func (d *ProviderData) SetKey(ctx context.Context, key string, value []byte) diag.Diagnostics {
var diags diag.Diagnostics
if d == nil {
tflog.Error(ctx, "error calling SetKey on uninitialized ProviderData")
diags.AddError("Uninitialized ProviderData",
"ProviderData must be initialized before it is used.\n\n"+
"Call privatestate.NewProviderData to obtain an initialized instance of ProviderData.",
)
return diags
}
if d.data == nil {
d.data = make(map[string][]byte)
}
diags.Append(ValidateProviderDataKey(ctx, key)...)
if diags.HasError() {
return diags
}
if !utf8.Valid(value) {
tflog.Error(ctx, "invalid UTF-8 value", map[string]interface{}{"key": key, "value": value})
diags.AddError("UTF-8 Invalid",
"Values stored in private state must be valid UTF-8.\n\n"+
fmt.Sprintf("The value being supplied for key %q is invalid. Please verify that the value is valid UTF-8.", key),
)
return diags
}
if !json.Valid(value) {
tflog.Error(ctx, "invalid JSON value", map[string]interface{}{"key": key, "value": value})
diags.AddError("JSON Invalid",
"Values stored in private state must be valid JSON.\n\n"+
fmt.Sprintf("The value being supplied for key %q is invalid. Please verify that the value is valid JSON.", key),
)
return diags
}
d.data[key] = value
return nil
}
// ValidateProviderDataKey determines whether the key supplied is allowed on the basis of any
// restrictions that are in place, such as key prefixes that are reserved for use with
// framework private state data.
func ValidateProviderDataKey(ctx context.Context, key string) diag.Diagnostics {
if isInvalidProviderDataKey(ctx, key) {
return diag.Diagnostics{
diag.NewErrorDiagnostic(
"Restricted Resource Private State Namespace",
"Using a period ('.') as a prefix for a key used in private state is not allowed.\n\n"+
fmt.Sprintf("The key %q is invalid. Please check the key you are supplying does not use a a period ('.') as a prefix.", key),
),
}
}
return nil
}
// isInvalidProviderDataKey determines whether the supplied key has a prefix that is reserved for
// keys in Data.Framework
func isInvalidProviderDataKey(_ context.Context, key string) bool {
return strings.HasPrefix(key, ".")
}
// MustMarshalToJson is for use in tests and panics if input cannot be marshalled to JSON.
func MustMarshalToJson(input map[string][]byte) []byte {
output, err := json.Marshal(input)
if err != nil {
panic(err)
}
return output
}
// MustProviderData is for use in tests and panics if the underlying call to NewProviderData
// returns diag.Diagnostics that contains any errors.
func MustProviderData(ctx context.Context, data []byte) *ProviderData {
providerData, diags := NewProviderData(ctx, data)
if diags.HasError() {
var diagMsgs []string
for _, v := range diags {
diagMsgs = append(diagMsgs, fmt.Sprintf("%s: %s", v.Summary(), v.Detail()))
}
panic(fmt.Sprintf("error creating new provider data: %s", strings.Join(diagMsgs, ", ")))
}
return providerData
}