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
6 changes: 6 additions & 0 deletions .codescene/code-health-rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
{ "name": "String Heavy Function Arguments", "weight": 0.0 },
{ "name": "Primitive Obsession", "weight": 0.0 }
]
},
{
"matching_content_path": "pluginkit/settings/parse.go",
"rules": [
{ "name": "String Heavy Function Arguments", "weight": 0.0 }
]
}
]
}
4 changes: 4 additions & 0 deletions pluginkit/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Releases of this module are tagged `pluginkit/vX.Y.Z`. Releases up to
- `Protect` takes the plugin's id and a `RouteSettings`, which holds the middleware as `Wrap`, a breaking change.
- `Protect` recovers each panic of a plugin's handler, on its public paths too. While nothing went out, `RouteSettings.Answer` answers it, else the connection is cut, and `RouteSettings.Report` receives a `RouteFailure` with the stack.
- `RoutesEach` reads each plugin's routes and public paths inside a recover, and fails at the new `StepRoutes` a plugin whose `Routes` or `PublicPaths` panics, or whose `Routes` answers no handler.
- `settings.Check` checks the settings a plugin declares: seven types (`text`, `count`, `flag`, `duration`, `choice`, `secret`, `lines`), each key lowercase words joined by one underscore and outside the starts the application reserves, whole-number bounds on a count only, the options of a choice, a default for a count, a duration and a choice that its type accepts, a flag default of true or false, no default for a secret, and no `of`. A plugin with settings may not hold two hyphens in a row in its id or end it with one.
- `settings.Name` names the variable of each setting from the application's prefix, the plugin's id and the key.
- `Schema.Resolve` works out each value from the environment values the application found, then from the default. An empty value counts as unset, and a value its type rejects is named by its variable.
- A secret's value is a `settings.Hidden`, which prints, logs and encodes as `set` or `not set`.
- The module needs Go 1.27.4.

## 0.12.0 - 2026-11-07
Expand Down
8 changes: 8 additions & 0 deletions pluginkit/features/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,11 @@ func TestBoot(t *testing.T) {
func TestRoutes(t *testing.T) {
runFeature(t, "routes.feature", initializeRoutes())
}

func TestSettings(t *testing.T) {
runFeature(t, "settings.feature", initializeSettings())
}

func TestSettingsDeclarations(t *testing.T) {
runFeature(t, "settings-declarations.feature", initializeSettings())
}
76 changes: 76 additions & 0 deletions pluginkit/features/settings-declarations.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
Feature: Checking the settings a plugin declares
Before a plugin registers, a downstream application checks the settings
its plugin.json declares. Each type has its own rules for bounds, options
and defaults, each key follows one shape, and the application reserves
key starts of its own. A declaration outside the rules is rejected with
the setting's place, its key and the rule it breaks. The rejection of a
secret never quotes its default, because plugin.json is public.

Background:
Given the application names plugin variables under "APP_PLUGIN_"
And the application reserves the key starts "job_" and "db_"

Scenario: A plugin that declares one setting of each type passes the check
Given the plugin "notes" declares these settings:
| key | type | default | min | max | options |
| title | text | Notes | | | |
| per_page | count | 10 | 1 | 50 | |
| open | flag | | | | |
| keep_for | duration | 720h | | | |
| order | choice | newest | | | newest, oldest |
| tags | lines | | | | |
| api_key | secret | | | | |
When the settings of "notes" are checked
Then the settings pass the check

Scenario Outline: A declaration outside the rules is rejected with its reason
Given the plugin "notes" declares <declaration>
When the settings of "notes" are checked
Then the check rejects them with "<reason>"

Examples:
| declaration | reason |
| a setting "mood" of the type "colour" | settings[0] mood: type "colour" is not one of text, count, flag, duration, choice, secret, lines |
| a text setting "Max__Seats" | settings[0] Max__Seats: a key is lowercase words joined by one underscore |
| a text setting "_title" | settings[0] _title: a key is lowercase words joined by one underscore |
| a text setting "job_every" | settings[0] job_every: a key must not start with job_ or db_ |
| a text setting "db_conns" | settings[0] db_conns: a key must not start with job_ or db_ |
| the text setting "title" twice | settings[1] title: the key is declared twice |
| a count "max_seats" with no default | settings[0] max_seats: a count needs a default |
| a count "max_seats" with the default "lots" | settings[0] max_seats: the default must be a whole number, got "lots" |
| a count "max_seats" from 1 to 50 with the default "60" | settings[0] max_seats: the default must stand at or below 50, got "60" |
| a count "max_seats" from 10 to 5 with the default "7" | settings[0] max_seats: min 10 stands above max 5 |
| a count "max_seats" from 1.5 with the default "4" | settings[0] max_seats: min 1.5 is not a whole number |
| a text "title" with the max 80 | settings[0] title: min and max apply only to a count |
| a lines "tags" with the min 1 | settings[0] tags: min and max apply only to a count |
| a text "title" with the options "a" and "b" | settings[0] title: options apply only to a choice |
| a choice "order" with no options | settings[0] order: a choice needs options |
| a choice "order" of "new " and "oldest" with the default "oldest" | settings[0] order: an option must not start or end with a space, got "new " |
| a choice "order" of "newest" and "oldest" with no default | settings[0] order: a choice needs a default |
| a choice "order" of "newest" and "oldest" with the default "random" | settings[0] order: the default must be one of newest, oldest, got "random" |
| a duration "keep_for" with no default | settings[0] keep_for: a duration needs a default |
| a duration "keep_for" with the default "soon" | settings[0] keep_for: the default must be a duration like 30s, got "soon" |
| a flag "open" with the default "maybe" | settings[0] open: the default must be true or false, got "maybe" |
| a secret "api_key" with the default "key-for-example" | settings[0] api_key: a secret takes no default, because plugin.json is public |
| a text "title" that names the content type "post" | settings[0] title: a text setting takes no of field |

Scenario: The rejection of a secret's default never quotes it
Given the plugin "notes" declares a secret "api_key" with the default "key-for-example"
When the settings of "notes" are checked
Then the check rejects them with "settings[0] api_key: a secret takes no default, because plugin.json is public"
And the reason does not hold "key-for-example"

Scenario Outline: A plugin with settings keeps its id clear of doubled and trailing hyphens
Given the plugin "<id>" declares the text setting "title"
When the settings of "<id>" are checked
Then the check rejects them with "the id <id> must not hold two hyphens in a row or end with one when the plugin has settings"

Examples:
| id |
| notes--pro |
| notes- |

Scenario: A plugin with no settings keeps any id the wiring takes
Given the plugin "notes--pro" declares no settings
When the settings of "notes--pro" are checked
Then the settings pass the check
111 changes: 111 additions & 0 deletions pluginkit/features/settings.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
Feature: Working out each plugin setting from the environment and its default
A downstream application lists the settings each plugin declares in its
plugin.json. After the check, it looks up the environment variable of
each setting and works out each value from the values it found, reading
no environment itself. A value the environment holds wins over the
default, an empty value counts as unset, and a value its type rejects is
named by its variable. A secret comes from the environment only and
prints as set or not set, never as itself.

Background:
Given the application names plugin variables under "APP_PLUGIN_"
And the plugin "notes" declares these settings:
| key | type | default | min | max | options |
| title | text | Notes | | | |
| per_page | count | 10 | 1 | 50 | |
| open | flag | false | | | |
| keep_for | duration | 720h | | | |
| order | choice | newest | | | newest, oldest |
| tags | lines | news | | | |
| api_key | secret | | | | |

Scenario: Each setting is read from a variable named after its plugin and its key
Then the settings of "notes" are read from these variables:
| key | variable |
| title | APP_PLUGIN_NOTES__TITLE |
| per_page | APP_PLUGIN_NOTES__PER_PAGE |
| open | APP_PLUGIN_NOTES__OPEN |
| keep_for | APP_PLUGIN_NOTES__KEEP_FOR |
| order | APP_PLUGIN_NOTES__ORDER |
| tags | APP_PLUGIN_NOTES__TAGS |
| api_key | APP_PLUGIN_NOTES__API_KEY |

Scenario Outline: Each hyphen of a plugin id becomes an underscore in the variable
Given the plugin "<id>" declares the text setting "max_seats"
Then the setting "max_seats" of "<id>" is read from "<variable>"

Examples:
| id | variable |
| events | APP_PLUGIN_EVENTS__MAX_SEATS |
| events-pro | APP_PLUGIN_EVENTS_PRO__MAX_SEATS |
| events-pro-max | APP_PLUGIN_EVENTS_PRO_MAX__MAX_SEATS |

Scenario: Each setting takes its default when the environment holds none
When the settings of "notes" are worked out
Then the settings read:
| key | value |
| title | Notes |
| per_page | 10 |
| open | false |
| keep_for | 720h |
| order | newest |
| tags | news |
| api_key | not set |

Scenario Outline: A value in the environment wins over the default
Given the environment holds "<value>" in "<variable>"
When the settings of "notes" are worked out
Then "<key>" reads "<read>"

Examples:
| key | variable | value | read |
| title | APP_PLUGIN_NOTES__TITLE | Field Notes | Field Notes |
| per_page | APP_PLUGIN_NOTES__PER_PAGE | 25 | 25 |
| open | APP_PLUGIN_NOTES__OPEN | true | true |
| keep_for | APP_PLUGIN_NOTES__KEEP_FOR | 48h | 48h |
| order | APP_PLUGIN_NOTES__ORDER | oldest | oldest |
| tags | APP_PLUGIN_NOTES__TAGS | news,, events | news, events |
| api_key | APP_PLUGIN_NOTES__API_KEY | key-for-example | set |

Scenario: An empty value counts as unset
Given the environment holds "" in "APP_PLUGIN_NOTES__PER_PAGE"
And the environment holds "" in "APP_PLUGIN_NOTES__API_KEY"
When the settings of "notes" are worked out
Then "per_page" reads "10"
And "api_key" reads "not set"

Scenario Outline: A value its type rejects is named by its variable
Given the environment holds "<value>" in "<variable>"
When the settings of "notes" are worked out
Then the settings are rejected with "<reason>"

Examples:
| variable | value | reason |
| APP_PLUGIN_NOTES__PER_PAGE | banana | APP_PLUGIN_NOTES__PER_PAGE: must be a whole number, got "banana" |
| APP_PLUGIN_NOTES__PER_PAGE | 2.5 | APP_PLUGIN_NOTES__PER_PAGE: must be a whole number, got "2.5" |
| APP_PLUGIN_NOTES__PER_PAGE | 60 | APP_PLUGIN_NOTES__PER_PAGE: must stand at or below 50, got "60" |
| APP_PLUGIN_NOTES__PER_PAGE | 0 | APP_PLUGIN_NOTES__PER_PAGE: must stand at or above 1, got "0" |
| APP_PLUGIN_NOTES__KEEP_FOR | soon | APP_PLUGIN_NOTES__KEEP_FOR: must be a duration like 30s, got "soon" |
| APP_PLUGIN_NOTES__KEEP_FOR | -1h | APP_PLUGIN_NOTES__KEEP_FOR: must stand above zero, got "-1h" |
| APP_PLUGIN_NOTES__OPEN | maybe | APP_PLUGIN_NOTES__OPEN: must be true or false, got "maybe" |
| APP_PLUGIN_NOTES__ORDER | random | APP_PLUGIN_NOTES__ORDER: must be one of newest, oldest, got "random" |

Scenario: The first rejected value in the manifest's order is the one named
Given the environment holds "banana" in "APP_PLUGIN_NOTES__PER_PAGE"
And the environment holds "random" in "APP_PLUGIN_NOTES__ORDER"
When the settings of "notes" are worked out
Then the settings are rejected with "APP_PLUGIN_NOTES__PER_PAGE: must be a whole number, got "banana""

Scenario: A secret reads its value only when asked for and prints as set
Given the environment holds "key-for-example" in "APP_PLUGIN_NOTES__API_KEY"
When the settings of "notes" are worked out
Then the secret "api_key" reads "key-for-example" when asked for
And every print or log of the settings shows "set" for "api_key"
And no print, log or encoding of the settings holds "key-for-example"

Scenario: A value rejected beside a secret never shows the secret
Given the environment holds "key-for-example" in "APP_PLUGIN_NOTES__API_KEY"
And the environment holds "banana" in "APP_PLUGIN_NOTES__PER_PAGE"
When the settings of "notes" are worked out
Then the settings are rejected with "APP_PLUGIN_NOTES__PER_PAGE: must be a whole number, got "banana""
And the reason does not hold "key-for-example"
Loading
Loading