inspec-core 2.1.84 → 2.2.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b8f1a62efaa756d818227d18b3b61aa106f61c597a9cd9b46bd28ea0dc9eb66a
4
- data.tar.gz: ee17dec7f433f4e9581c61e22692f91d45a7afb48e7af52e195f6a9a0a76661e
3
+ metadata.gz: d5a0147fcfec73a7c62fafd050d13b9cf3696d33892a769295cae623af5126fc
4
+ data.tar.gz: ea24534b1a9887a625310aabbab3054884d0d54ab0462378b2e1adb6d6a9b8fa
5
5
  SHA512:
6
- metadata.gz: a6c8d5e7e4a4e2e89df4fcce36dc81745a05f071bd55089ac9e3ae8bf8a7edb6fe067db0f810307c23e5e1b75863bdf5553985f5ca1404ce1ee5280346c6553a
7
- data.tar.gz: fc0b64a7a2d49a9dd65f8db7f2c171ca2fa877abcadd82a2331487f45cc7e378f3f3fadec7f20bad01232f62b1f2d6eca5d4228efde9dc9cc61e65ec5a32592b
6
+ metadata.gz: 4a2da0d531f2611b278ad64d6d877a48ab6e70226e4a535c409b3a05c5e81582fc049800de66eae9690848f85822903d32275b82ea6c5cfab830a2db2304cef0
7
+ data.tar.gz: 9d86cf80e4fc09a27fce83455c57ea349f10ed301f12ef30ac3d987da3b2cc18173d903574ade9ca702d7cb3eb76f766207a2511f5047adb0949c4cfc670a4ef
@@ -1,26 +1,49 @@
1
1
  # Change Log
2
2
  <!-- usage documentation: http://expeditor-docs.es.chef.io/configuration/changelog/ -->
3
- <!-- latest_release 2.1.84 -->
4
- ## [v2.1.84](https://github.com/inspec/inspec/tree/v2.1.84) (2018-05-31)
3
+ <!-- latest_release 2.2.8 -->
4
+ ## [v2.2.8](https://github.com/inspec/inspec/tree/v2.2.8) (2018-06-07)
5
5
 
6
- #### Merged Pull Requests
7
- - Generate new org builds [#3087](https://github.com/inspec/inspec/pull/3087) ([jquick](https://github.com/jquick))
6
+ #### New Resources
7
+ - Add aws_elb and aws_elbs resources [#3079](https://github.com/inspec/inspec/pull/3079) ([clintoncwolfe](https://github.com/clintoncwolfe))
8
8
  <!-- latest_release -->
9
9
 
10
- <!-- release_rollup since=2.1.83 -->
11
- ### Changes since 2.1.83 release
10
+ <!-- release_rollup since=2.1.84 -->
11
+ ### Changes since 2.1.84 release
12
+
13
+ #### New Resources
14
+ - Add aws_elb and aws_elbs resources [#3079](https://github.com/inspec/inspec/pull/3079) ([clintoncwolfe](https://github.com/clintoncwolfe)) <!-- 2.2.8 -->
15
+ - Adds a aws_flow_log resource with unit and integration testing. [#2906](https://github.com/inspec/inspec/pull/2906) ([miah](https://github.com/miah)) <!-- 2.2.7 -->
16
+
17
+ #### Bug Fixes
18
+ - Update documentation for shadow resource. [#3042](https://github.com/inspec/inspec/pull/3042) ([miah](https://github.com/miah)) <!-- 2.2.6 -->
19
+ - Two fixes to FilterTable `where` criteria handling [#3045](https://github.com/inspec/inspec/pull/3045) ([clintoncwolfe](https://github.com/clintoncwolfe)) <!-- 2.2.0 -->
20
+
21
+ #### Enhancements
22
+ - Allow custom resources to access all other resources [#3108](https://github.com/inspec/inspec/pull/3108) ([jquick](https://github.com/jquick)) <!-- 2.2.3 -->
23
+ - Add common methods to FilterTable [#3104](https://github.com/inspec/inspec/pull/3104) ([clintoncwolfe](https://github.com/clintoncwolfe)) <!-- 2.2.2 -->
24
+ - Detect windows packages with trailing/leading spaces [#3106](https://github.com/inspec/inspec/pull/3106) ([jquick](https://github.com/jquick)) <!-- 2.1.86 -->
12
25
 
13
26
  #### Merged Pull Requests
14
- - Generate new org builds [#3087](https://github.com/inspec/inspec/pull/3087) ([jquick](https://github.com/jquick)) <!-- 2.1.84 -->
27
+ - Clean up issues in documentation [#3058](https://github.com/inspec/inspec/pull/3058) ([miah](https://github.com/miah)) <!-- 2.2.5 -->
28
+ - Update Junit.rb to add failures attribute [#3086](https://github.com/inspec/inspec/pull/3086) ([scboucher](https://github.com/scboucher)) <!-- 2.2.4 -->
29
+ - Add lazy-loading to FilterTable [#3093](https://github.com/inspec/inspec/pull/3093) ([clintoncwolfe](https://github.com/clintoncwolfe)) <!-- 2.2.1 -->
30
+ - Refactor: Perform internal rename and add comments to FilterTable [#3047](https://github.com/inspec/inspec/pull/3047) ([clintoncwolfe](https://github.com/clintoncwolfe)) <!-- 2.1.88 -->
31
+ - Spellcheck FilterTable Developer Documentation [#3111](https://github.com/inspec/inspec/pull/3111) ([jerryaldrichiii](https://github.com/jerryaldrichiii)) <!-- 2.1.87 -->
32
+ - support local npm package searches [#3105](https://github.com/inspec/inspec/pull/3105) ([arlimus](https://github.com/arlimus)) <!-- 2.1.85 -->
15
33
  <!-- release_rollup -->
16
34
 
17
35
  <!-- latest_stable_release -->
36
+ ## [v2.1.84](https://github.com/inspec/inspec/tree/v2.1.84) (2018-05-31)
37
+
38
+ #### Merged Pull Requests
39
+ - Generate new org builds [#3087](https://github.com/inspec/inspec/pull/3087) ([jquick](https://github.com/jquick))
40
+ <!-- latest_stable_release -->
41
+
18
42
  ## [v2.1.83](https://github.com/chef/inspec/tree/v2.1.83) (2018-05-18)
19
43
 
20
44
  #### Merged Pull Requests
21
45
  - Update ruby required version. [#3070](https://github.com/chef/inspec/pull/3070) ([jquick](https://github.com/jquick))
22
46
  - Test new gem builds [#3071](https://github.com/chef/inspec/pull/3071) ([jquick](https://github.com/jquick))
23
- <!-- latest_stable_release -->
24
47
 
25
48
  ## [v2.1.81](https://github.com/chef/inspec/tree/v2.1.81) (2018-05-17)
26
49
 
data/README.md CHANGED
@@ -451,3 +451,4 @@ distributed under the License is distributed on an "AS IS" BASIS,
451
451
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
452
452
  See the License for the specific language governing permissions and
453
453
  limitations under the License.
454
+
@@ -0,0 +1,353 @@
1
+ # Internals of FilterTable
2
+
3
+ If you just want to _use_ FilterTable, see filtertable-usage.md . Reading this may make you more confused, not less.
4
+
5
+ ## What makes this hard?
6
+
7
+ FilterTable was created in 2016 in an attempt to consolidate the pluralization features of several resources. They each had slightly different feature-sets, and were all in the wild, so FilterTable exposes some extensive side-effects to provide those features.
8
+
9
+ Additionally, the ways in which the classes relate is not straightforward.
10
+
11
+ ## Where is the code?
12
+
13
+ The main FilterTable code is in [utils/filter.rb](https://github.com/chef/inspec/blob/master/lib/utils/filter.rb).
14
+
15
+ Also educational is the unit test for Filtertable, at test/unit/utils/filter_table_test.rb . Recent work has focused on using functional tests to exercise FilterTable; see test/unit/mocks/profiles/filter_table and test/functional/filter_table_test.rb .
16
+
17
+ The file utils/filter_array.rb appears to be unrelated.
18
+
19
+ ## What are the classes involved?
20
+
21
+ ### FilterTable::Factory
22
+
23
+ This class is responsible for the definition of the filtertable. It provides the methods that are used by the resource author to configure the filtertable.
24
+
25
+ FilterTable::Factory initializes three instance variables:
26
+ ```
27
+ @filter_methods = []
28
+ @custom_properties = {}
29
+ @resource = nil # This appears to be unused
30
+ ```
31
+
32
+ ### FilterTable::Table
33
+
34
+ This is the actual innards of the implementation. The Factory's goal is to configure a Table subclass and attach it to the resource you are authoring. The table is a container for the raw data your resource provides, and performs filtration services.
35
+
36
+ ### FilterTable::ExceptionCatcher
37
+
38
+ TODO
39
+
40
+ ## What are the major entry points? (FilterTable::Factory)
41
+
42
+ A resource class using FilterTable typically will call a sequence similar to this, in the class body:
43
+
44
+ ```
45
+ filter = FilterTable.create
46
+ .register_column(:thing_ids, field: :thing_id)
47
+ .install_filter_methods_on_resource(self, :table)
48
+ ```
49
+
50
+ Legacy code might look like this:
51
+ ```
52
+ filter = FilterTable.create
53
+ filter.add_accessor(:entries)
54
+ .add(:exists?) { |x| !x.entries.empty? }
55
+ .add(:count) { |x| x.entries.count }
56
+ .add(:thing_ids, field: :thing_id)
57
+ .connect(self, :table)
58
+ ```
59
+
60
+ ### create
61
+
62
+ Returns a blank instance of a FilterTable::Factory. It also adds a default implementation of `where` `raw_data`, and `entries` using `register_filter_method`, `count` using `register_custom_property`, and `exist?` using `register_custom_matcher`.
63
+
64
+ ### register\_filter\_method
65
+
66
+ Legacy name (alias): `add_accessor`
67
+
68
+ This simply pushes the provided method name onto the `@filter_methods` instance variable array. See "filter_method" behavior section below for what this does.
69
+
70
+ After adding the method name to the array, it returns `self` - the FilterTable::Factory instance - so that method chaining will work.
71
+
72
+ ### register\_column
73
+
74
+ Legacy name (alias): `add`
75
+
76
+ This is currently simply an alias for `register_custom_property`. See it for details. By calling it with a distinctive name, we'll be able to add functionality in the future (especially around introspection).
77
+
78
+ ### register\_custom\_matcher
79
+
80
+ Legacy name (alias): `add`
81
+
82
+ This is currently simply an alias for `register_custom_property`. See it for details. By calling it with a distinctive name, we'll be able to add functionality in the future (especially around introspection).
83
+
84
+ ### register\_custom\_property
85
+
86
+ Legacy name (alias): `add`
87
+
88
+ This method has very complex behavior, ans should likely be split into several use cases. `register_custom_property` requires a symbol (which will be used as a method name _to be added to the resource class_), then also accepts a block and/or additional args. These things - name, block, and opts - are packed into a simple Struct called a CustomPropertyType. The name stored in the Struct will be `opts[:field]` if provided, and the method name if not.
89
+
90
+ The CustomPropertyType Struct is then appended to the Hash `@custom_properties`, keyed on the method name provided. `self` is then returned for method chaining.
91
+
92
+ The implementation of the custom property method is generated by `create_custom_property_body`, and varies based on whether a block was provided to `register_custom_property`.
93
+
94
+ #### Behavior when a block is provided
95
+
96
+ This behavior is implemented by lines 398-404.
97
+
98
+ If a block is provided, it is turned into a Lambda and used as the method body.
99
+
100
+ The block will be provided two arguments (though most users only use the first):
101
+ 1. The FilterTable::Table instance that wraps the raw data.
102
+ 2. An optional value used as an additional opportunity to filter.
103
+
104
+ For example, this is common in legacy code:
105
+ ```
106
+ filter.add(:exists?) { |x| !x.entries.empty? }
107
+ ```
108
+
109
+ Here, `x` is the Table instance, which exposes the `entries` method (which returns an array, one entry for each raw data row).
110
+
111
+ You could also implement a more sophisticated property, which semantically should re-filter the table based on the candidate value, and return the new table.
112
+
113
+ ```
114
+ filter.add(:smaller_than) { |table, threshold| table.where { some_field <= threshold } }
115
+ ```
116
+
117
+ ```
118
+ things.smaller_than(12)
119
+ ```
120
+
121
+ If you provide _both_ a block and opts, only the block is used, and the options are ignored.
122
+
123
+ #### Behavior when no block is provided
124
+
125
+ If you do not provide a block, you _must_ provide a `:field` option (though that does no appear to be enforced). The behavior is to define a method with the name provided, that has a conditional return type. The method body is defined in lines 306-423.
126
+
127
+ If called without arguments, it returns an array of the values in the raw data for that column.
128
+ ```
129
+ things.thing_ids => [1,2,3,4]
130
+ ```
131
+
132
+ If called with an argument, it instead calls `where` passing the name of the field and the argument, effectively filtering.
133
+ ```
134
+ things.thing_ids(2) => FilterTable::Table that only contains a row where thing_id = 2
135
+ ```
136
+
137
+ If called with a block, it passes the block to where.
138
+ ```
139
+ things.thing_ids { some_code } => Same as things.where { some_code }
140
+ ```
141
+
142
+ POSSIBLE BUG: I think this case is broken; it certainly seems ill-advised.
143
+
144
+ #### Known Options
145
+
146
+ You can provide options to `register_custom_property` / `add`, after the desired method name.
147
+
148
+ ##### field
149
+
150
+ This is the most common option, and is mandatory if a block is not provided. It selects an implementation in which the desired method will be defined such that it returns an array of the row values using the specified key. In other words, this acts as a "column fetcher", like in SQL: "SELECT some_column FROM some_table"
151
+
152
+ Internally, (line 271-278), a Struct type is created to represent a row of raw data. The struct's attribute list is taken from the `field` options passed to `register_custom_property` / `add`. This new type is stored as `row_eval_context_type`. It is used as the evaluation context for block-mode `where` calls.
153
+
154
+ * No checking is performed to see if the field name is actually a column in the raw data (the raw data hasn't been fetched yet, so we can't check).
155
+ * You can't have two `register_custom_property` / `add` calls that reference the same field, because the Struct would see that as a duplicate attribute.
156
+
157
+ POSSIBLE BUG: We could deduplicate the field names when defining the Struct, thus allowing multiple properties to use the same field.
158
+
159
+ ##### style
160
+
161
+ The `style` option is intended to effect post-processing of the return value from the generated method. To date there is only one recognized value, `:simple`, which `flatten`s, `uniq`s, and `compact`s the array value of the property. This is implemented on line 416.
162
+
163
+ No other values for `:style` have been seen.
164
+
165
+ ##### lazy
166
+
167
+ This option implements column-wise lazy loading. The value of the option is expected to a lambda expecting 3 arguments: row (a Hash representing a row of the raw data), condition (a sought value to filter for), and table (a reference to the FilterTable::Table subclass, which may be used for context).
168
+
169
+ See the usage guide for details on the usage of the lazy mechanism; this document will examine the internals.
170
+
171
+ ### install_filter_methods_on_resource
172
+
173
+ Legacy name (alias): connect
174
+
175
+ This method is called like this:
176
+
177
+ ```
178
+ filter.install_filter_methods_on_resource(self, :data_fetching_method_name)
179
+ ```
180
+
181
+ `filter` is an instance of FilterTable::Factory. `self` is a reference to the resource class you are authoring. `data_fetching_method_name` is a symbol, the name of a method that will return the actual data to be processed by the FilterTable - as an array of hashes.
182
+
183
+ Note that 'connect' does not refer to Connectors.
184
+
185
+ `register_custom_property` and `register_filter_method` did nothing other than add register names for methods that we'd like to have added to the resource class. No filtering ability is present, nor are the methods defined, at this point.
186
+
187
+ So, `install_filter_methods_on_resource`/`connect`'s job is to actually install everything.
188
+
189
+ #### Defines a special Struct type to support block-mode where
190
+
191
+ At lines 270-278, a new Struct type `row_eval_context_type` is defined, with attributes for each of the known table fields. We're careful to filter out any fields that have a block implementation, because they cannot be accessed as a row value, and so should not be present on the row context. The motivation for this struct type is to implement the block-mode behavior of `where`. Because each struct represents a row, and it has the attributes (accessors) for the fields, block-mode `where` is implemented by `instance_eval`ing against each row as a struct.
192
+
193
+ Additionally, an instance variable, `@criteria_string` is defined, with an accessor. `to_s` is implemented, using `@criteria_string`, or `super` if not defined. I guess we then rely on the `Struct` class to stringify.
194
+
195
+ `@criteria_string` is a trace - a string indicating the filter criteria used to create the table. I found no location where this per-row trace data was used.
196
+
197
+ Additionally, an instance variable is setup to refer to the filter table later. This is required for lazy-loading columns.
198
+
199
+ Table fields are determined by listing the `field_name`s of the CustomProperties.
200
+
201
+ BUG: this means that any `register_custom_property` / `add` call that uses a block but not options will end up with an attribute in the row Struct. Thus, `filter.add(:exists?) { ... }` results in a row Struct that includes an attribute named `exists?` which may be undesired. This attribute will never have a value, because when the structs are instantiated, the block for the field is not called.
202
+
203
+ #### Re-pack the "connectors"
204
+
205
+ On lines 280-282, the list of custom properties ("connectors", registered using the `register_custom_property` / `add` method) are repacked into an array of hashes of two elements - the desired method name and the lambda that will be used as the method body. The lambda is created by the private method `create_custom_property_body`; see `register_custom_property` for discussion about how the implementation behaves.
206
+
207
+ #### Subclass FilterTable::Table into an anonymous class
208
+
209
+ At line 286, create the local var `table_class`, which refers to an anonymous class that subclasses FilterTable::Table. The class is opened and two groups of methods are defined.
210
+
211
+ Lines 288-290 install the "custom_property" methods, using the names and lambdas determined on line 281.
212
+
213
+ Lines 292-294 allow the Table subclass to introspect on the CustomProperties by slipping a reference to it in the class body, forming a closure.
214
+
215
+ Line 296-303 define a method, `create_eval_context_for_row`. This is used when executing a block-mode `where`; see line 120.
216
+
217
+ #### Setup the row context struct for lazy loading
218
+
219
+ If you have a lazy field named `color` and it has not yet been populated, we need to trigger it to populate the first time it is read. If a block-mode where is used (`my_resource.where { color == :red }`), then we have to intercept the Struct's default `getter method`, and call the lazy column's lambda.
220
+
221
+ Lines 306-329 do exactly that, by defining methods on the Struct subclass we're using for context. We continue to rely on the default Struct setter (`[]=`) and getter at the end (`[]`).
222
+
223
+ #### Install methods on the resource
224
+
225
+ Lines 337-348 install the "filter_methods" and "custom properties" methods onto the resource that you are authoring.
226
+
227
+ Line 337-338 collects the names of the methods to define - by agglomerating the names of the "filter_methods" and "custom properties" methods. They are treated the same.
228
+
229
+ Line 339 uses `send` with a block to call `define_method` on the resource class that you're authoring. Using a block with `send` is undocumented, but is treated as an implicit argument (per StackOverflow) , so the end result is that the block is used as the body for the new method being defined.
230
+
231
+ The method body is wrapped in an exception-catching facility that catches skipped or failed resource exceptions and wraps them in a specialized exception catcher class. TBD: understand this better.
232
+
233
+ Line 342 constructs an instance of the anonymous FilterTable::Table subclass defined at 284. It passes three args:
234
+
235
+ 1. `self`. A reference to the resource instance.
236
+ 2. The return value of calling the data fetcher method (that is an array of hashes, the raw data).
237
+ 3. The string ' with', which is probably informing the criteria stringification. The extra space is intentional, as it follows the resource name: 'my_things with color == :red' might be a result.
238
+
239
+ On line 343, we then immediately call a method on that "FilterTable::Table subclass instance". The method name is the same as the one we're defining on the resource - but we're calling it on the Table. Recall we defined all the "custom_property" methods on the Table subclass at line 288-290. The method gets called with any args or block passed, and since it's the last thing, it provides the return value.
240
+
241
+ ## What is its behavior? (FilterTable::Table)
242
+
243
+ Assume that your resource has a method, `fetch_data`, which returns a fixed array:
244
+
245
+ ```
246
+ [
247
+ { id: 1, name: 'Dani', color: 'blue' },
248
+ { id: 2, name: 'Mike', color: 'red' },
249
+ { id: 3, name: 'Erika', color: 'green' },
250
+ ]
251
+ ```
252
+
253
+ Assume that you then perform this sequence in your resource class body:
254
+
255
+ ```
256
+ filter = FilterTable.create
257
+ filter.register_filter_method(:entries)
258
+ filter.register_filter_method(:where)
259
+ filter.register_custom_property(:exists?) { |x| !x.exists.empty? }
260
+ filter.register_custom_property(:names, field: :name)
261
+ filter.install_filter_methods_on_resource(self, :fetch_data)
262
+ ```
263
+
264
+ Legacy code equivalent:
265
+
266
+ ```
267
+ filter = FilterTable.create
268
+ filter.add_accessor(:entries)
269
+ filter.add_accessor(:where)
270
+ filter.add(:exists?) { |x| !x.exists.empty? }
271
+ filter.add(:names, field: :name)
272
+ filter.connect(self, :fetch_data)
273
+ ```
274
+
275
+ We know from the above exploration of `install_filter_methods_on_resource` / `connect` that we now have several new methods on the resource class, all of which delegate to the FilterTable::Table implementation.
276
+
277
+ ### FilterTable::Table constructor and internals
278
+
279
+ Factory calls the FilterTable::Table constructor at 87-93 with three args. Table stores them into instance vars:
280
+ * @resource_instance - this was passed in as `self` from line 342
281
+ * @raw_data - an array of hashes
282
+ * @criteria_string - This looks to be stringification trace data; the string ' with' was passed in by Factory.
283
+ * @populated_lazy_columns = a hash, by lazy field name, with boolean values. This is set true if `populate_lazy_field` is called on a field.
284
+
285
+ The first three get exposed via `attr_reader`s.
286
+
287
+ ### `entries` behavior
288
+
289
+ From usage, I expect entries to return a structure that resembles an array of hashes representing the (filtered) data.
290
+
291
+ #### A new method `entries` is defined on the resource class
292
+
293
+ That is performed by Factory#connect line 339.
294
+
295
+ #### It delegates to FilterTable::Table#entries
296
+
297
+ This is a real method defined in filter.rb line 155.
298
+
299
+ It loops over the provided raw data (@raw_data) and builds an array, calling `create_eval_context_for_row` (see Factory lines 297-303) on each row; also appending a stringification trace to each entry. The array is returned.
300
+
301
+ #### `entries` conclusion
302
+
303
+ Not Surprising: It does behave as expected - an array of Hash-like structs representing the table. I don't know why it adds in the per-row stringification data - I've never seen that used.
304
+
305
+ Surprising: this is a real method with a concrete implementation. That means that you can't call `filter.add_accessor` with arbitrary method names - `:entries` means something very specific.
306
+
307
+ Surprising: I would not recommend this method be used for data access; instead I would recommend using `raw_data`.
308
+
309
+ ### `where` behavior
310
+
311
+ From usage, I expect this to take either method params or a block (both of which are magical), perform filtering, and return some object that contains only the filtered rows.
312
+
313
+ So, what happens when you call `register_filter_method(:where)` and then call `resource.where`?
314
+
315
+ #### A new method `where` is defined on the resource class
316
+
317
+ That is performed by Factory#connect line 339.
318
+
319
+ #### It delegates to FilterTable::Table#where
320
+
321
+ Like `entries`, this is a real implemented method on FilterTable::Table, at line 98.
322
+
323
+ The method accepts all params as the local var `conditions` which defaults to an empty Hash. A block, if any, is also explicitly assigned the name `block`.
324
+
325
+ The implementation opens with two guard clauses, both of which will return `self` (which is the FilterTable::Table subclass instance).
326
+
327
+ MISFEATURE: The first guard clause simply returns the Table if `conditions` is not a Hash. That would mean that someone called it like: `thing.where(:apples, :bananas, :cantaloupes)`. That misuse is silently ignored; I think we should probably throw a ResourceFailed or something.
328
+
329
+ The second guard clause is a sensible degenerate case - return the existing Table if there are no conditions and no block. So `thing.where` is OK.
330
+
331
+ Line 103 initializes a local var, `new_criteria_string`, which again is a stringification tracker.
332
+
333
+ Line 104 initializes a var to track the `filtered_raw_data`.
334
+
335
+ Lines 108-113 loop over the provided Hash `conditions`. If the requested field is lazy, it requests that it be populated (note that `populate_lazy_field` is idempotent - it won't fetch a field twice). Next, it repeatedly down-filters `filtered_raw_data` by calling the private method `filter_raw_data` on it. `filter_raw_data` does some syntactic sugaring for common types, Integers and Floats and Regexp matching. Additionally, the line 108-113 loop builds up the stringification tracker, `new_criteria_string`, by stringifying the field name and target value.
336
+
337
+ Line 118-135 begins work if a filtration block has been provided. At this point, `filtered_raw_data` has been initialized with the raw data, and (if method params were provided) has also been filtered down.
338
+
339
+ Line 120 filters the rows of the raw data using an interesting approach. Each row is inflated to a Struct using `create_eval_context_for_row` (see line 297). Then the provided block is `instance_eval`'d against the Struct. Because the Struct was defined with attributes (that is, accessor methods) for each declared field name (from FilterTable::Factory#register_custom_property), you can use field names in the block, and each row-as-struct will be able to respond. If the field happened to be lazy, we'll call our custom getter from lines 306-329.
340
+
341
+ _That just explained a major spooky side-effect for me._
342
+
343
+ Lines 125-134 do something with stringification tracing. TODO.
344
+
345
+ Finally, at line 137, the FilterTable::Table anonymous subclass is again used to construct a new instance, passing on the resource reference, the newly filtered raw data table, and the newly adjusted stringification tracer.
346
+
347
+ That new Table instance is returned, and thus `where` allows you to chain.
348
+
349
+ #### `where` conclusion
350
+
351
+ Unsurprising: How where works with method params.
352
+ Surprising: How where works in block mode, `instance_eval`'ing against each row-as-Struct.
353
+ Surprising: You can use method-mode and block-mode together if you want.
@@ -0,0 +1,533 @@
1
+ # Using FilterTable to write a Resource
2
+
3
+ ## When do I use FilterTable?
4
+
5
+ FilterTable is intended to help you author "plural" resources.
6
+
7
+ Plural resources examine platform objects in bulk. For example, sorting through which packages are installed on a system, or which virtual machines are on a cloud provider. You don't know the identifiers of the objects, but you may know some of their properties, and you may want to filter the objects based on those - for example, all processes running more than an hour, or all VMs on a particular subnet.
8
+
9
+ Singular resources, in contrast, are designed to examine a particular platform object in detail, _when you know an identifier_. For example, you would use a singular resource to fetch a VM by its ID, then interrogate its networking configuration. Singular resources are able to provide richer properties and matchers than plural resources, because the semantics are clearer.
10
+
11
+ If you can't tell if the resource you are authoring is singular or plural, STOP and consult with a team member. This is a fundamental design question, and while we have had some resources that "straddle the fence" in the past, they are very difficult to use and maintain.
12
+
13
+ ### Should I use FilterTable to represent list properties?
14
+
15
+ Suppose you have a person, and you want to represent that person's shoes. Should you use FilterTable for that?
16
+
17
+ NO. FilterTable is intended to represent pluralities inherent to the resource itself, not a property of the resource. So, you would use FilterTable to represent _people_. To represent shoes, you could have a simple, dumb array-of-strings property on Person. Or, you could create a new resource, Shoe, or Shoes, which has a person_name or person_id property. Or expose a complex structure as a low-level property, and create mid-level properties/matchers that compute on the values internally (`shoe_fit?`, `has_shoes_for_occasion?('red_carpet')`)
18
+
19
+ ### May I have multiple FilterTable installations on a class?
20
+
21
+ In theory, yes - that would be used to implement different data fetching / caching strategies. It is a very advanced usage, and no core resources currently do this, as far as I know.
22
+
23
+ ## Example Usage
24
+
25
+ Suppose you are writing a resource, `things`. You want it to behave like any plural resource (we'll explore what that means in a moment). That is the basic expected behavior of any plural resource.
26
+
27
+ ### How do I declare my interaction with FilterTable?
28
+
29
+ ```ruby
30
+
31
+ require 'utils/filter'
32
+
33
+ class Thing < Inspec.resource(1)
34
+ #... other Resource DSL work goes here ...
35
+
36
+ # FilterTable setup
37
+ filter_table_config = FilterTable.create
38
+ filter_table_config.register_column(:thing_ids, field: :thing_id)
39
+ filter_table_config.register_column(:colors, field: :color, style: :simple)
40
+ filter_table_config.install_filter_methods_on_resource(self, :fetch_data)
41
+
42
+ def fetch_data
43
+ # This method should return an array of hashes - the raw data. We'll hardcode it here.
44
+ [
45
+ { thing_id: 1, color: :red },
46
+ { thing_id: 2, color: :blue, tackiness: 'very' },
47
+ { thing_id: 3, color: :red },
48
+ ]
49
+ end
50
+
51
+ def some_other_property
52
+ # We'll examine this later
53
+ end
54
+
55
+ end
56
+ ```
57
+
58
+ Note that all of the methods on `filter_table_config` support chaining, so you will sometimes see it as:
59
+ ```ruby
60
+ filter_table_config = FilterTable.create
61
+ .register_column(:thing_ids, field: :thing_id)
62
+ .register_column(:colors, field: :color, style: :simple)
63
+ .install_filter_methods_on_resource(self, :fetch_data)
64
+ ```
65
+ etc.
66
+
67
+ ## Standard behavior
68
+
69
+ With a (fairly standard) implementation like that above, what behavior do you get out of the box?
70
+
71
+ ### Some things are defined for you
72
+
73
+ In the past, you needed to request certain methods be installed. These are now installed automatically: `where`, `entries`, `raw_data`, `count`, and `exist?`. You only have to declare your columns unique to your resource, and then attach the data fetcher.
74
+
75
+ ### Your class is still just a Resource class
76
+
77
+ Nothing special immediately happens to your class or instances of it. The data fetcher is not called yet.
78
+
79
+ ### Instances of your class can create a specialized FilterTable::Table instance
80
+
81
+ When most of the following methods are called, it may trigger the instantiation of a FilterTable::Table anonymous subclass. That instance will have called the raw data fetcher, and will wrap the raw data inside it. Many of the following methods return the Table instance.
82
+
83
+ ### A `where` method you can call with nil to get the Table
84
+
85
+ The resource class gains a method, `where`. If called with a single `nil` param or no params, it will call the data fetcher method, wrap it up, and return the Table instance. Calling `where` in other modes will do the same thing, but will filter the data.
86
+
87
+ ```ruby
88
+ describe things.where(nil)
89
+ it { should exist }
90
+ its('count') { should cmp 3 }
91
+ end
92
+
93
+ # This works, too, but for different internal reasons
94
+ describe things.where
95
+ it { should exist }
96
+ its('count') { should cmp 3 }
97
+ end
98
+ ```
99
+
100
+ ### A `where` method you can call with hash params, with loose matching
101
+
102
+ If you call `where` as a method with no block and passing hash params, with keys you know are in the raw data, it will fetch the raw data, then filter row-wise and return the resulting Table.
103
+
104
+ Multiple criteria are joined with a logical AND.
105
+
106
+ The filtering is fancy, not just straight equality.
107
+
108
+ ```ruby
109
+ describe things.where(color: :red) do
110
+ its('count') { should cmp 2 }
111
+ end
112
+
113
+ # Regexes
114
+ describe things.where(color: /^re/) do
115
+ its('count') { should cmp 2 }
116
+ end
117
+
118
+ # It eventually falls out to === comparison
119
+ # Here, range membership 1..2
120
+ describe things.where(thing_id: (1..2)) do
121
+ its('count') { should cmp 2 }
122
+ end
123
+
124
+ # Things that don't exist are silently ignored, but do not match
125
+ # See https://github.com/chef/inspec/issues/2943
126
+ describe things.where(none_such: :nope) do
127
+ its('count') { should cmp 0 }
128
+ end
129
+
130
+ # irregular rows are supported
131
+ # Only one row has the :tackiness key, with value 'very'.
132
+ describe things.where(tackiness: 'very') do
133
+ its('count') { should cmp 1 }
134
+ end
135
+
136
+ ```
137
+
138
+ ### A `where` method you can call with a block, referencing some fields
139
+
140
+ You can also call the `where` method with a block. The block is executed row-wise. If it returns truthy, the row is included in the results. register_custom_propertyitionally, within the block each field declared with the `register_custom_property` configuration method is available as a data accessor.
141
+
142
+ ```ruby
143
+
144
+ # You can have any logic you want in the block
145
+ describe things.where { true } do
146
+ its('count') { should cmp 3 }
147
+ end
148
+
149
+ # You can access any fields you declared using `register_column`
150
+ describe things.where { thing_id > 2 } do
151
+ its('count') { should cmp 1 }
152
+ end
153
+ ```
154
+
155
+ ### You can chain off of `where` or any other Table without re-fetching raw data
156
+
157
+ The first time `where` is called, the data fetcher method is called. `where` performs filtration on the raw data table. It then constructs a new FilterTable::Table, directly passing in the filtered raw data; this is then the return value from `where`.
158
+
159
+ ```ruby
160
+ # This only calls fetch_data once
161
+ describe things.where(color: :red).where { thing_id > 2 } do
162
+ its('count') { should cmp 1 }
163
+ end
164
+ ```
165
+
166
+ Some other methods return a Table object, and they may be chained without a re-fetch as well.
167
+
168
+ ### An `entries` method that will return an array of Structs
169
+
170
+ The other `register_filter_method` call enables a pre-defined method, `entries`. `entries` is much simpler than `where` - in fact, its behavior is unrelated. It returns an encapsulated version of the raw data - a plain array, containing Structs as row-entries. Each struct has an attribute for each time you called `register_column`.
171
+
172
+ Overall, in my opinion, `entries` is less useful than `params` (which returns the raw data). Wrapping in Structs does not seem to add much benefit.
173
+
174
+ Importantly, note that the return value of `entries` is not the resource, nor the Table - in other words, you cannot chain it. However, you can call `entries` on any Table.
175
+
176
+ If you call `entries` without chaining it after `where`, calling entries will trigger the call to the data fetching method.
177
+
178
+ ```ruby
179
+
180
+ # Access the entries array
181
+ describe things.entries do
182
+ # This is Array#count, not the resource's `count` method
183
+ its('count') { should cmp 3}
184
+ end
185
+
186
+ # Access the entries array after chaining off of where
187
+ describe things.where(color: :red).entries do
188
+ # This is Array#count, not the resource's or table's `count` method
189
+ its('count') { should cmp 2}
190
+ end
191
+
192
+ # You can access the struct elements as a method, as a hash keyed on symbol, or as a hash keyed on string
193
+ describe things.entries.first.color do
194
+ it { should cmp :red }
195
+ end
196
+ describe things.entries.first[:color] do
197
+ it { should cmp :red }
198
+ end
199
+ describe things.entries.first['color'] do
200
+ it { should cmp :red }
201
+ end
202
+ ```
203
+
204
+ ### You get an `exist?` matcher defined on the resource and the table
205
+
206
+ This `register_custom_matcher` call:
207
+ ```ruby
208
+ filter_table_config.register_custom_matcher(:exist?) { |filter_table| !filter_table.entries.empty? }
209
+ ```
210
+
211
+ causes a new method to be defined on both the resource class and the Table class. The body of the method is taken from the block that is provided. When the method it called, it will receive the FilterTable::Table instance as its first parameter. (It may also accept a second param, but that doesn't make sense for this method - see thing_ids).
212
+
213
+ As when you are implementing matchers on a singular resource, the only thing that distinguishes this as a matcher is the fact that it ends in `?`.
214
+
215
+ ```ruby
216
+ # Bare call on the matcher (called as a method on the resource)
217
+ describe things do
218
+ it { should exist }
219
+ end
220
+
221
+ # Chained on where (called as a method on the Table)
222
+ describe things.where(color: :red) do
223
+ it { should exist }
224
+ end
225
+ ```
226
+
227
+ ### You get an `count` property defined on the resource and the table
228
+
229
+ This `register_custom_property` call:
230
+ ```ruby
231
+ filter_table_config.register_custom_property(:count) { |filter_table| filter_table.entries.count }
232
+ ```
233
+
234
+ causes a new method to be defined on both the resource class and the Table class. As with `exists?`, the body is taken from the block.
235
+
236
+ ```ruby
237
+ # Bare call on the property (called as a method on the resource)
238
+ describe things do
239
+ its('count') { should cmp 3 }
240
+ end
241
+
242
+ # Chained on where (called as a method on the Table)
243
+ describe things.where(color: :red) do
244
+ its('count') { should cmp 2 }
245
+ end
246
+ ```
247
+
248
+ ### A `thing_ids` method that will return an array of plain values when called without params
249
+
250
+ This `register_column` call:
251
+ ```ruby
252
+ filter_table_config.register_column(:thing_ids, field: :thing_id)
253
+ ```
254
+
255
+ will cause a method to be defined on both the resource and the Table. Note that this `register_column` call does not provide a block; so FilterTable::Factory generates a method body. The `:field` option specifies which column to access in the raw data (that is, which hash key in the array-of-hashes).
256
+
257
+ The implementation provided by Factory changes behavior based on calling pattern. If no params or block is provided, a simple array is returned, containing the column-wise values in the raw data.
258
+
259
+ ```ruby
260
+
261
+ # Use it to check for presence / absence of a member
262
+ # This retains nice output formatting - we're testing on a Table associated with a Things resource
263
+ describe things.where(color: :red) do
264
+ its('thing_ids') { should include 3 }
265
+ end
266
+
267
+ # Equivalent but with poor formatting - we're testing an anonymous array
268
+ describe things.where(color: :red).thing_ids do
269
+ it { should include 3 }
270
+ end
271
+
272
+ # Use as a test-less enumerator
273
+ things.where(color: :red).thing_ids.each do |thing_id|
274
+ # Do something with thing_id, maybe
275
+ # describe thing(thing_id) do ...
276
+ end
277
+
278
+ # Can be used without where - enumerates all Thing IDs with no filter
279
+ things.thing_ids.each do |thing_id|
280
+ # Do something with thing_id, maybe
281
+ # describe thing(thing_id) do ...
282
+ end
283
+
284
+ ```
285
+
286
+ ### A `colors` method that will return a flattened and uniq'd array of values
287
+
288
+ This method behaves just like `thing_ids`, except that it returns the values of the `color` column. In addition, the `style: :simple` option causes it to flatten and uniq the array of values when called without args or a block.
289
+
290
+ ```ruby
291
+ # Three rows in the data: red, blue, red
292
+ describe things.colors do
293
+ its('count') { should cmp 2 }
294
+ it { should include :red }
295
+ it { should include :blue }
296
+ end
297
+
298
+ ```
299
+
300
+ ### A `colors` method that can filter on a value and return a Table
301
+
302
+ You also get this for `thing_ids`. This is unrelated to `style: :simple` for `colors`.
303
+
304
+ People definitely use this in the wild. It reads badly to me; I think this is a legacy usage that we should consider deprecating. To me, this seems to imply that there is a sub-resource (here, colors) we are auditing. At least two core resouces (`xinetd_conf` and `users`) advocate this as their primary use.
305
+
306
+ ```ruby
307
+ # Filter on colors
308
+ describe things.colors(:red) do
309
+ its('count') { should cmp 2 }
310
+ end
311
+
312
+ # Same, but doesn't imply we're now operating on some 'color' resource
313
+ describe things.where(color: :red) do
314
+ its('count') { should cmp 2 }
315
+ end
316
+ ```
317
+
318
+ ### A `colors` method that can filter on a block and return a Table
319
+
320
+ You also get this for `thing_ids`. This is unrelated to `style: :simple` for `colors`.
321
+
322
+ I haven't seen this used in the wild, but its existence gives me a headache.
323
+
324
+ ```ruby
325
+ # Example A, B, C, and D are semantically the same
326
+
327
+ # A: Filter both on colors and the block
328
+ describe things.colors(:red) { thing_id < 2 } do
329
+ its('count') { should cmp 1 }
330
+ its('thing_ids') { should include 1 }
331
+ end
332
+
333
+ # B use one where block
334
+ describe things.where { color == :red && thing_id < 2 } do
335
+ its('count') { should cmp 1 }
336
+ its('thing_ids') { should include 1 }
337
+ end
338
+
339
+ # C use two where blocks
340
+ describe things.where { color == :red }.where { thing_id < 2 } do
341
+ its('count') { should cmp 1 }
342
+ its('thing_ids') { should include 1 }
343
+ end
344
+
345
+ # D use a where param and a where block
346
+ describe things.where(color: :red) { thing_id < 2 } do
347
+ its('count') { should cmp 1 }
348
+ its('thing_ids') { should include 1 }
349
+ end
350
+
351
+ # This has nothing to do with colors at all, and may be broken - the lack of an arg to `colors` may make it never match
352
+ describe things.colors { thing_id < 2 } do
353
+ its('count') { should cmp 1 }
354
+ end
355
+ ```
356
+
357
+ ### You can call `params` or `raw_data` on any Table to get the raw data
358
+
359
+ People _definitely_ use this out in the wild. Unlike `entries`, which wraps each row in a Struct and omits undeclared fields, `raw_data` simply returns the actual raw data array-of-hashes. It is not `dup`'d.
360
+
361
+ ```ruby
362
+ tacky_things = things.where(color: :blue).raw_data.select { |row| row[:tackiness] }
363
+ tacky_things.map { |row| row[:thing_id] }.each do |thing_id|
364
+ # Use to audit a singular Thing
365
+ describe thing(thing_id) do
366
+ it { should_not be_paisley }
367
+ end
368
+ end
369
+ ```
370
+
371
+ ### You can call `resource_instance` on any Table to get the resource instance
372
+
373
+ You could use this to do something fairly complicated.
374
+
375
+ ```ruby
376
+ describe things.where do # Just getting a Table object
377
+ its('resource_instance.some_method') { should cmp 'some_value' }
378
+ end
379
+ ```
380
+
381
+ However, the resource instance won't know about the filtration, so I'm not sure what good this does. Chances are, someone is doing something horrid using this feature in the wild.
382
+
383
+ ## Lazy Loading
384
+
385
+ ### What is Lazy Loading
386
+
387
+ In some cases, the raw data may require multiple actions to populate. For example, if you wanted a list of processes, and their open files, you might need to call 'ps' once, then 'lsof' one or more times. That would become slow, and so you would only want to do it if you knew it was going to be used.
388
+
389
+ Lazy loaded columns are absent in the raw data, until they are accessed (either by method-where, block-where, or a list property). When they are accessed, a user-provided Lambda is called, which populates one or more columns. FilterTable remembers which lazy columns have been populated, and will not call the lambda again.
390
+
391
+ ### Declaring a lazy field
392
+
393
+ You declare a field to be lazy by providing an option, `lazy`, whose value is the lambda to be called.
394
+
395
+ You can use the 'stabby lambda' syntax:
396
+
397
+ ```ruby
398
+ filter_table_config.register_column(
399
+ :open_files,
400
+ field: :files,
401
+ lazy: ->() {|r,c,t| r[:files] = lookup_files_for_pid(r[:pid])},
402
+ )
403
+ ```
404
+
405
+ You can also refer to a *class* method. You cannot use an instance method, because FilterTable binds to the resource class, not the resource instance.
406
+
407
+ ```ruby
408
+ def self.populate_lsof(row, criteria, table)
409
+ row[:files] = ...
410
+ end
411
+ filter_table_config.register_column(
412
+ :open_files,
413
+ field: :files,
414
+ lazy: method(:populate_lsof),
415
+ )
416
+ ```
417
+
418
+ ### Arguments to the fetcher lambda
419
+
420
+ The lambda will be provided three arguments:
421
+
422
+ 1. `row`. This is a Hash, the current row of the raw_data. You will likely need to examine this to find an ID value or other field that will act as a search key for your fetch. You are expected to add one or more entries to this hash, as a result of your fetch.
423
+ 2. `condition`. In some cases, a condition (desired value) is provided; the semantics of this are up to you.
424
+ 3. `table`. A reference to the FilterTable. You can use this to access other context - including the entire raw data (`table.raw_data`) or the resource instance (`table.resource_instance`).
425
+
426
+ ### Clobbering
427
+
428
+ Lazy-loading will not clobber an existing value in raw data. For example:
429
+
430
+ ```ruby
431
+ # Your raw data table:
432
+ [
433
+ { id: 1 },
434
+ { id: 2, color: :blue },
435
+ { id: 3 },
436
+ ]
437
+
438
+ # On lazy load, set all rows to color red
439
+ filter_table_config.register_column(
440
+ :colors,
441
+ field: :color,
442
+ lazy: ->() { |r,c,t| r[:color] = :red },
443
+ )
444
+
445
+ # Trigger a fetch
446
+ my_resource.colors => [:red, :blue, :red]
447
+
448
+ # Raw data now:
449
+ [
450
+ { id: 1, color: :red },
451
+ { id: 2, color: :blue },
452
+ { id: 3, color: :red },
453
+ ]
454
+ ```
455
+ Note that not only was the `:color` blue not overwritten, in fact the fetcher lambda was only called twice.
456
+
457
+ ### Can I set multiple columns at once?
458
+
459
+ Yes. If your fetching action provides you with data to populate multiple columns, you are free to set any columns you wish in the `row`.
460
+
461
+ You can even have multiple lazy columns share an implementation; the first one to be called will populate all the columns that share that implementation, and if any of the others are later triggered, the no-clobber effect will kick in, and the fetcher will not be called again.
462
+
463
+ ### Can I set multiple rows at once?
464
+
465
+ Yes. Using `table.raw_data`, you could perform a column-at-once population. After the fetcher was called for the first row, all other rows would already be populated, so the fetcher would not be called again due to the no-clobber effect.
466
+
467
+ ## Gotchas and Surprises
468
+
469
+ ### Methods defined with `register_column` will change their return type based on their call pattern
470
+
471
+ To me, calling things.thing_ids should always return the same type of value. But if you call it with args or a block, it not only runs a filter, but also changes its return type to Table.
472
+
473
+ ```ruby
474
+
475
+ # This is an Array of color values (symbols, here)
476
+ things.colors
477
+
478
+ # This is a FilterTable::Table and these are equivalent
479
+ things.colors(:red)
480
+ things.where(color: :red)
481
+
482
+ # This is a FilterTable::Table and these are equivalent
483
+ things.colors { color == :red } # I think there is a bug here which makes this never match
484
+ things.where(color: :red)
485
+
486
+ ```
487
+
488
+ ### `entries` will not have fields present in the raw data
489
+
490
+ `entries` will only know about the fields declared by `register_column` with `field:`. And...
491
+
492
+ ### `entries` will have things that are not fields
493
+
494
+ Each time you call `register_custom_property`, `register_custom_matcher` or `register_column` - even for things like `count` and `exists?` - that will add an attribute to the Struct that is used to represent a row. Those attributes will always be nil.
495
+
496
+ ### `register_column` does not know about what fields are in the raw data
497
+
498
+ This is because the raw data fetcher is not called until as late as possible. That's good - it might be expensive - but it also means we can't scan it for columns. There are ways around that.
499
+
500
+ ### `where` param-mode and raw data access is sensitive to symbols vs strings
501
+
502
+ ### You can't call resource methods on a Table directly
503
+
504
+ ### You can't use a column name in a `where` block unless it was declared as a field using `register_column`
505
+
506
+ ```ruby
507
+ # This will give a NameError - :tackiness is in the raw
508
+ # data hash but not declared using `register_custom_property`.
509
+ describe things.where { tackiness == 'very' } do
510
+ its('count') { should cmp 1 }
511
+ end
512
+ # NameError: undefined local variable or method `tackiness' for #<struct :exists?=nil, count=nil, id=nil>
513
+
514
+ # But this works:
515
+ describe things.where(tackiness: 'very') do
516
+ its('count') { should cmp 1 }
517
+ end
518
+ ```
519
+
520
+ ### The eval context of a where block is an anonymous Struct class
521
+
522
+ You can't get to the resource or the table from there. (It's the Entry Struct type).
523
+
524
+ ### You can in fact filter for `nil` values
525
+
526
+ ### There is no obvious accessor for the Table instance
527
+
528
+ You can in fact get the FilterTable::Table instance by calling `where` with no args. But that is not obvious.
529
+
530
+ ### There is no way to get the FilterTable::Factory object used to configure the resource
531
+
532
+ Especially while developing in inspec shell, it would be nice to be able to get at the FilterTable::Factory object, perhaps to add more accessors.
533
+