optify 0.11.0__tar.gz → 0.12.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {optify-0.11.0 → optify-0.12.0}/PKG-INFO +1 -1
- {optify-0.11.0 → optify-0.12.0}/pyproject.toml +2 -2
- {optify-0.11.0 → optify-0.12.0}/python/optify/Cargo.lock +2 -2
- {optify-0.11.0 → optify-0.12.0}/python/optify/Cargo.toml +2 -2
- {optify-0.11.0 → optify-0.12.0}/rust/optify/Cargo.toml +1 -1
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/builder/builder_impl.rs +22 -14
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/builder/builder_options.rs +20 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/builder/loading_result.rs +2 -1
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/lib.rs +1 -0
- optify-0.12.0/rust/optify/src/policies/feature_policies.rs +32 -0
- optify-0.12.0/rust/optify/src/policies/mod.rs +10 -0
- optify-0.12.0/rust/optify/src/policies/policies_file_contents.rs +12 -0
- optify-0.12.0/rust/optify/src/policies/policy_denied_error.rs +29 -0
- optify-0.12.0/rust/optify/src/policies/policy_store.rs +169 -0
- optify-0.12.0/rust/optify/src/policies/requester_feature_policy.rs +52 -0
- optify-0.12.0/rust/optify/src/policies/requester_policy.rs +38 -0
- optify-0.12.0/rust/optify/src/policies/validate_requester_policies.rs +56 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/provider/provider_impl.rs +20 -52
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/provider/provider_trait.rs +2 -1
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/provider/watcher.rs +6 -5
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/schema/feature.rs +1 -1
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/schema/mod.rs +0 -1
- optify-0.11.0/rust/optify/src/schema/policies.rs +0 -88
- {optify-0.11.0 → optify-0.12.0}/LICENSE.txt +0 -0
- {optify-0.11.0 → optify-0.12.0}/README.md +0 -0
- {optify-0.11.0 → optify-0.12.0}/optify/__init__.py +0 -0
- {optify-0.11.0 → optify-0.12.0}/optify/optify.pyi +0 -0
- {optify-0.11.0 → optify-0.12.0}/optify/py.typed +0 -0
- {optify-0.11.0 → optify-0.12.0}/python/optify/.gitignore +0 -0
- {optify-0.11.0 → optify-0.12.0}/python/optify/.vscode/settings.json +0 -0
- {optify-0.11.0 → optify-0.12.0}/python/optify/LICENSE.txt +0 -0
- {optify-0.11.0 → optify-0.12.0}/python/optify/README.md +0 -0
- {optify-0.11.0 → optify-0.12.0}/python/optify/src/lib.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/python/optify/src/preferences.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/python/optify/src/provider.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/python/optify/src/watcher.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/python/optify/tests/test_builder.py +0 -0
- {optify-0.11.0 → optify-0.12.0}/python/optify/tests/test_dacite.py +0 -0
- {optify-0.11.0 → optify-0.12.0}/python/optify/tests/test_optify.py +0 -0
- {optify-0.11.0 → optify-0.12.0}/python/optify/tests/test_provider.py +0 -0
- {optify-0.11.0 → optify-0.12.0}/python/optify/tests/test_pydantic.py +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/Cargo.toml +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/README.md +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/benches/caching_benchmark.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/benches/get_all_options_benchmark.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/benches/get_options_benchmark.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/benches/loading_benchmark.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/build.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/builder/builder_trait.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/builder/extract_configurable_string_files_from_config.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/builder/extract_files_from_config.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/builder/get_canonical_feature_name.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/builder/get_supported_extensions.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/builder/mod.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/builder/watcher_builder.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/configurable_string/configurable_string_impl.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/configurable_string/mod.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/configurable_values/configurable_list_impl.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/configurable_values/locator.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/configurable_values/mod.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/json/escape_json_pointer.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/json/merge.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/json/mod.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/json/reader.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/provider/constraints.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/provider/get_options_preferences.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/provider/mod.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/provider/watcher_options.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/schema/conditions.rs +0 -0
- {optify-0.11.0 → optify-0.12.0}/rust/optify/src/schema/metadata.rs +0 -0
|
@@ -26,11 +26,11 @@ build-backend = "maturin"
|
|
|
26
26
|
|
|
27
27
|
[package]
|
|
28
28
|
name = "optify"
|
|
29
|
-
version = "0.
|
|
29
|
+
version = "0.12.0"
|
|
30
30
|
|
|
31
31
|
[tool.poetry]
|
|
32
32
|
name = "optify"
|
|
33
|
-
version = "0.
|
|
33
|
+
version = "0.12.0"
|
|
34
34
|
description = "Simplifies getting the right configuration options for a process using pre-loaded configurations from files to manage options for experiments or flights."
|
|
35
35
|
authors = ["Justin D. Harris"]
|
|
36
36
|
maintainers = ["Justin D. Harris"]
|
|
@@ -941,7 +941,7 @@ checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
|
|
941
941
|
|
|
942
942
|
[[package]]
|
|
943
943
|
name = "optify"
|
|
944
|
-
version = "1.
|
|
944
|
+
version = "1.5.0"
|
|
945
945
|
dependencies = [
|
|
946
946
|
"config",
|
|
947
947
|
"cow-utils",
|
|
@@ -961,7 +961,7 @@ dependencies = [
|
|
|
961
961
|
|
|
962
962
|
[[package]]
|
|
963
963
|
name = "optify-python"
|
|
964
|
-
version = "0.
|
|
964
|
+
version = "0.12.0"
|
|
965
965
|
dependencies = [
|
|
966
966
|
"optify",
|
|
967
967
|
"pyo3",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "optify-python"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.12.0"
|
|
4
4
|
edition = "2021"
|
|
5
5
|
|
|
6
6
|
description = "Simplifies getting the right configuration options for a process using pre-loaded configurations from files (JSON, YAML, etc.) to manage options for experiments or flights. This library is mainly made to support building implementations for other languages such as Node.js, Python, and Ruby. It is not meant to be consumed directly yet."
|
|
@@ -15,5 +15,5 @@ name = "optify"
|
|
|
15
15
|
crate-type = ["cdylib"]
|
|
16
16
|
|
|
17
17
|
[dependencies]
|
|
18
|
-
optify = { path = "../../rust/optify", version = "1.
|
|
18
|
+
optify = { path = "../../rust/optify", version = "1.5.0" }
|
|
19
19
|
pyo3 = "0.29.0"
|
|
@@ -16,9 +16,9 @@ use crate::configurable_string::LoadedFiles;
|
|
|
16
16
|
use crate::configurable_values::locator::{find_configurable_values, ConfigurableValuePointers};
|
|
17
17
|
use crate::json::merge::{merge_json_with_defaults, FrozenPaths};
|
|
18
18
|
use crate::json::reader::read_json_from_file_as;
|
|
19
|
+
use crate::policies::PolicyStore;
|
|
19
20
|
use crate::provider::{
|
|
20
|
-
Aliases, Conditions, Features, OptionsProvider,
|
|
21
|
-
Sources,
|
|
21
|
+
Aliases, Conditions, Features, OptionsProvider, ReferencedFileToFeatureNames, Sources,
|
|
22
22
|
};
|
|
23
23
|
use crate::schema::feature::FeatureConfiguration;
|
|
24
24
|
use crate::schema::metadata::OptionsMetadata;
|
|
@@ -30,17 +30,17 @@ type Imports = HashMap<String, Vec<String>>;
|
|
|
30
30
|
#[derive(Clone)]
|
|
31
31
|
pub struct OptionsProviderBuilder {
|
|
32
32
|
aliases: Aliases,
|
|
33
|
-
all_configurable_string_pointers: HashSet<String>,
|
|
34
33
|
all_configurable_list_pointers: HashSet<String>,
|
|
35
|
-
|
|
36
|
-
keyed_configurable_string_pointers: HashMap<String, HashSet<String>>,
|
|
34
|
+
all_configurable_string_pointers: HashSet<String>,
|
|
37
35
|
builder_options: BuilderOptions,
|
|
38
36
|
conditions: Conditions,
|
|
39
37
|
dependents: Dependents,
|
|
40
38
|
features: Features,
|
|
41
39
|
imports: Imports,
|
|
40
|
+
keyed_configurable_list_pointers: HashMap<String, HashSet<String>>,
|
|
41
|
+
keyed_configurable_string_pointers: HashMap<String, HashSet<String>>,
|
|
42
42
|
loaded_files: LoadedFiles,
|
|
43
|
-
|
|
43
|
+
policy_store: PolicyStore,
|
|
44
44
|
/// A map of files to the features that reference them.
|
|
45
45
|
/// The keys are relative file paths and the values are lists of canonical feature names.
|
|
46
46
|
/// This is only populated if the `BuilderOptions` enable file reference tracking.
|
|
@@ -189,17 +189,17 @@ impl OptionsProviderBuilder {
|
|
|
189
189
|
pub fn new() -> Self {
|
|
190
190
|
OptionsProviderBuilder {
|
|
191
191
|
aliases: Aliases::new(),
|
|
192
|
-
all_configurable_string_pointers: HashSet::new(),
|
|
193
192
|
all_configurable_list_pointers: HashSet::new(),
|
|
194
|
-
|
|
195
|
-
keyed_configurable_string_pointers: HashMap::new(),
|
|
193
|
+
all_configurable_string_pointers: HashSet::new(),
|
|
196
194
|
builder_options: BuilderOptions::default(),
|
|
197
195
|
conditions: Conditions::new(),
|
|
198
196
|
dependents: Dependents::new(),
|
|
199
197
|
features: Features::new(),
|
|
200
198
|
imports: HashMap::new(),
|
|
199
|
+
keyed_configurable_list_pointers: HashMap::new(),
|
|
200
|
+
keyed_configurable_string_pointers: HashMap::new(),
|
|
201
201
|
loaded_files: LoadedFiles::new(),
|
|
202
|
-
|
|
202
|
+
policy_store: PolicyStore::default(),
|
|
203
203
|
referenced_file_to_feature_names: HashMap::new(),
|
|
204
204
|
schema: None,
|
|
205
205
|
sources: Sources::new(),
|
|
@@ -240,7 +240,7 @@ impl OptionsProviderBuilder {
|
|
|
240
240
|
keyed_configurable_string_pointers,
|
|
241
241
|
std::mem::take(&mut self.conditions),
|
|
242
242
|
std::mem::take(&mut self.features),
|
|
243
|
-
std::mem::take(&mut self.
|
|
243
|
+
std::mem::take(&mut self.policy_store),
|
|
244
244
|
referenced_file_to_feature_names,
|
|
245
245
|
std::mem::take(&mut self.loaded_files),
|
|
246
246
|
std::mem::take(&mut self.sources),
|
|
@@ -277,6 +277,9 @@ impl OptionsProviderBuilder {
|
|
|
277
277
|
.dependents = Some(sorted_dependents);
|
|
278
278
|
}
|
|
279
279
|
|
|
280
|
+
self.policy_store
|
|
281
|
+
.validate_requester_policies(&self.features, &self.aliases)?;
|
|
282
|
+
|
|
280
283
|
Ok(())
|
|
281
284
|
}
|
|
282
285
|
|
|
@@ -344,8 +347,8 @@ impl OptionsProviderBuilder {
|
|
|
344
347
|
.insert(canonical_feature_name.clone(), conditions);
|
|
345
348
|
}
|
|
346
349
|
if let Some(policies) = info.policies {
|
|
347
|
-
self.
|
|
348
|
-
.
|
|
350
|
+
self.policy_store
|
|
351
|
+
.insert_policy(canonical_feature_name.clone(), policies);
|
|
349
352
|
}
|
|
350
353
|
if let Some(imports) = info.imports {
|
|
351
354
|
self.imports.insert(canonical_feature_name.clone(), imports);
|
|
@@ -570,6 +573,11 @@ impl OptionsRegistryBuilder<OptionsProvider> for OptionsProviderBuilder {
|
|
|
570
573
|
self.builder_options.clone()
|
|
571
574
|
};
|
|
572
575
|
|
|
576
|
+
if let Some(policies_path) = &builder_options.policies_path {
|
|
577
|
+
self.policy_store
|
|
578
|
+
.load_policies_from_file(directory, policies_path, &config_path)?;
|
|
579
|
+
}
|
|
580
|
+
|
|
573
581
|
let supported_extensions = get_supported_extensions();
|
|
574
582
|
|
|
575
583
|
let loading_results: Vec<Result<LoadingResult, String>> = walkdir::WalkDir::new(directory)
|
|
@@ -694,7 +702,7 @@ impl OptionsRegistryBuilder<OptionsProvider> for OptionsProviderBuilder {
|
|
|
694
702
|
keyed_configurable_string_pointers,
|
|
695
703
|
self.conditions.clone(),
|
|
696
704
|
self.features.clone(),
|
|
697
|
-
self.
|
|
705
|
+
self.policy_store.clone(),
|
|
698
706
|
referenced_file_to_feature_names,
|
|
699
707
|
self.loaded_files.clone(),
|
|
700
708
|
self.sources.clone(),
|
|
@@ -29,6 +29,12 @@ pub(crate) struct BuilderOptionsConfig {
|
|
|
29
29
|
pub schema_path: Option<PathBuf>,
|
|
30
30
|
#[serde(default)]
|
|
31
31
|
pub track_file_references: Option<TrackReferenceMode>,
|
|
32
|
+
/// Path, always relative to the directory containing this config file, to the file
|
|
33
|
+
/// declaring requester feature policies.
|
|
34
|
+
/// There is no default: requester feature policies are only loaded when this is explicitly set, and it is an error if the resolved path is not a file.
|
|
35
|
+
/// May be JSON, YAML, or any other format supported by the `config` crate.
|
|
36
|
+
#[serde(default)]
|
|
37
|
+
pub policies_path: Option<PathBuf>,
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
impl BuilderOptionsConfig {
|
|
@@ -52,6 +58,11 @@ impl BuilderOptionsConfig {
|
|
|
52
58
|
} else {
|
|
53
59
|
self.schema_path.or(defaults.schema_path)
|
|
54
60
|
},
|
|
61
|
+
policies_path: if overrides.policies_path != defaults.policies_path {
|
|
62
|
+
overrides.policies_path.clone()
|
|
63
|
+
} else {
|
|
64
|
+
self.policies_path.or(defaults.policies_path)
|
|
65
|
+
},
|
|
55
66
|
track_file_references: if overrides.track_file_references
|
|
56
67
|
!= defaults.track_file_references
|
|
57
68
|
{
|
|
@@ -71,6 +82,10 @@ pub struct BuilderOptions {
|
|
|
71
82
|
pub are_configurable_strings_enabled: bool,
|
|
72
83
|
pub are_configurable_values_enabled: bool,
|
|
73
84
|
pub schema_path: Option<PathBuf>,
|
|
85
|
+
/// Path to the file declaring requester feature policies. Relative to the directory being
|
|
86
|
+
/// added, unless absolute.
|
|
87
|
+
/// Defaults to `.optify/policies.json` when unset.
|
|
88
|
+
pub policies_path: Option<PathBuf>,
|
|
74
89
|
pub track_file_references: TrackReferenceMode,
|
|
75
90
|
}
|
|
76
91
|
|
|
@@ -85,11 +100,13 @@ mod tests {
|
|
|
85
100
|
are_configurable_strings_enabled: true,
|
|
86
101
|
are_configurable_values_enabled: true,
|
|
87
102
|
schema_path: Some(PathBuf::from("override_schema.json")),
|
|
103
|
+
policies_path: None,
|
|
88
104
|
track_file_references: TrackReferenceMode::ConfigurableStrings,
|
|
89
105
|
};
|
|
90
106
|
let config = BuilderOptionsConfig {
|
|
91
107
|
are_configurable_values_enabled: Some(false),
|
|
92
108
|
schema_path: Some(PathBuf::from("config_schema.json")),
|
|
109
|
+
policies_path: None,
|
|
93
110
|
track_file_references: Some(TrackReferenceMode::None),
|
|
94
111
|
};
|
|
95
112
|
|
|
@@ -114,6 +131,7 @@ mod tests {
|
|
|
114
131
|
let config = BuilderOptionsConfig {
|
|
115
132
|
are_configurable_values_enabled: Some(true),
|
|
116
133
|
schema_path: Some(PathBuf::from("config_schema.json")),
|
|
134
|
+
policies_path: None,
|
|
117
135
|
track_file_references: Some(TrackReferenceMode::ConfigurableStrings),
|
|
118
136
|
};
|
|
119
137
|
|
|
@@ -139,11 +157,13 @@ mod tests {
|
|
|
139
157
|
are_configurable_strings_enabled: false,
|
|
140
158
|
are_configurable_values_enabled: false,
|
|
141
159
|
schema_path: None,
|
|
160
|
+
policies_path: None,
|
|
142
161
|
track_file_references: TrackReferenceMode::ConfigurableStrings,
|
|
143
162
|
};
|
|
144
163
|
let config = BuilderOptionsConfig {
|
|
145
164
|
are_configurable_values_enabled: Some(true),
|
|
146
165
|
schema_path: Some(PathBuf::from("config_schema.json")),
|
|
166
|
+
policies_path: None,
|
|
147
167
|
track_file_references: None,
|
|
148
168
|
};
|
|
149
169
|
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
use crate::{
|
|
2
2
|
configurable_values::locator::ConfigurableValuePointers,
|
|
3
|
+
policies::Policies,
|
|
3
4
|
provider::SourceValue,
|
|
4
|
-
schema::{conditions::ConditionExpression, metadata::OptionsMetadata
|
|
5
|
+
schema::{conditions::ConditionExpression, metadata::OptionsMetadata},
|
|
5
6
|
};
|
|
6
7
|
|
|
7
8
|
#[allow(clippy::large_enum_variant)]
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
use std::collections::HashMap;
|
|
2
|
+
|
|
3
|
+
use serde::{Deserialize, Serialize};
|
|
4
|
+
|
|
5
|
+
use super::requester_policy::RequesterPolicy;
|
|
6
|
+
|
|
7
|
+
pub(crate) type PoliciesMap = HashMap<String, Policies>;
|
|
8
|
+
|
|
9
|
+
/// Policies that restrict access to a feature based on values in the request's preferences.
|
|
10
|
+
///
|
|
11
|
+
/// Policies are checked for the **top-level features** in a request.
|
|
12
|
+
/// Unlike conditions, policies are **not** evaluated on imported features — a feature may
|
|
13
|
+
/// freely import another feature that has policies without those policies being enforced.
|
|
14
|
+
///
|
|
15
|
+
/// See https://github.com/juharris/optify#policies for details and a comparison with conditions.
|
|
16
|
+
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
17
|
+
#[serde(deny_unknown_fields)]
|
|
18
|
+
pub struct Policies {
|
|
19
|
+
/// Restrictions based on the requester identifier passed via preferences.
|
|
20
|
+
///
|
|
21
|
+
/// Use `allow` to specify an allowlist (only those requesters may use this feature).
|
|
22
|
+
/// Use `block` to specify a denylist (all requesters except those listed may use this feature).
|
|
23
|
+
/// `allow` and `block` are mutually exclusive.
|
|
24
|
+
pub requester: RequesterPolicy,
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
impl Policies {
|
|
28
|
+
/// Returns `true` if the given requester is permitted to use the feature.
|
|
29
|
+
pub fn is_requester_permitted(&self, requester: &str) -> bool {
|
|
30
|
+
self.requester.is_permitted(requester)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
pub mod feature_policies;
|
|
2
|
+
pub mod policies_file_contents;
|
|
3
|
+
pub mod policy_denied_error;
|
|
4
|
+
pub mod policy_store;
|
|
5
|
+
pub mod requester_feature_policy;
|
|
6
|
+
pub mod requester_policy;
|
|
7
|
+
pub mod validate_requester_policies;
|
|
8
|
+
|
|
9
|
+
pub use feature_policies::Policies;
|
|
10
|
+
pub(crate) use policy_store::PolicyStore;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
use serde::Deserialize;
|
|
2
|
+
|
|
3
|
+
use super::requester_feature_policy::RequesterPoliciesMap;
|
|
4
|
+
|
|
5
|
+
#[derive(Deserialize)]
|
|
6
|
+
pub(crate) struct PoliciesFileContents {
|
|
7
|
+
// Helps ignore the `$schema` property when parsing.
|
|
8
|
+
#[serde(rename = "$schema")]
|
|
9
|
+
#[allow(dead_code)]
|
|
10
|
+
pub(crate) schema: Option<String>,
|
|
11
|
+
pub(crate) requesters: RequesterPoliciesMap,
|
|
12
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
use std::fmt;
|
|
2
|
+
|
|
3
|
+
/// Error returned when a requester is not permitted to use a feature.
|
|
4
|
+
#[derive(Clone, Debug)]
|
|
5
|
+
pub struct PolicyDeniedError {
|
|
6
|
+
pub feature_name: String,
|
|
7
|
+
pub requester: String,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
impl PolicyDeniedError {
|
|
11
|
+
pub fn new(feature_name: impl Into<String>, requester: impl Into<String>) -> Self {
|
|
12
|
+
Self {
|
|
13
|
+
feature_name: feature_name.into(),
|
|
14
|
+
requester: requester.into(),
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
impl fmt::Display for PolicyDeniedError {
|
|
20
|
+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
21
|
+
write!(
|
|
22
|
+
f,
|
|
23
|
+
"Requester \"{}\" is not permitted to use feature \"{}\".",
|
|
24
|
+
self.requester, self.feature_name
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
impl std::error::Error for PolicyDeniedError {}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
use std::path::Path;
|
|
2
|
+
|
|
3
|
+
use crate::provider::{Aliases, Features};
|
|
4
|
+
|
|
5
|
+
use super::feature_policies::{Policies, PoliciesMap};
|
|
6
|
+
use super::policies_file_contents::PoliciesFileContents;
|
|
7
|
+
use super::policy_denied_error::PolicyDeniedError;
|
|
8
|
+
use super::requester_feature_policy::{RequesterFeaturePolicy, RequesterPoliciesMap};
|
|
9
|
+
use super::validate_requester_policies::validate_requester_policies;
|
|
10
|
+
|
|
11
|
+
/// A policy store holding feature policies and requester policies.
|
|
12
|
+
/// Responsible for all policy-related logic in the provider and builder.
|
|
13
|
+
#[derive(Clone, Debug, Default)]
|
|
14
|
+
pub(crate) struct PolicyStore {
|
|
15
|
+
policies: PoliciesMap,
|
|
16
|
+
requester_policies: RequesterPoliciesMap,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
impl PolicyStore {
|
|
20
|
+
#[allow(dead_code)]
|
|
21
|
+
pub(crate) fn new(policies: PoliciesMap, requester_policies: RequesterPoliciesMap) -> Self {
|
|
22
|
+
Self {
|
|
23
|
+
policies,
|
|
24
|
+
requester_policies,
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/// Checks policies for a list of features for the given requester.
|
|
29
|
+
pub(crate) fn check_policies(
|
|
30
|
+
&self,
|
|
31
|
+
requester: &str,
|
|
32
|
+
canonical_feature_names: &[impl AsRef<str>],
|
|
33
|
+
) -> Result<(), String> {
|
|
34
|
+
let requester_policy = self.requester_policies.get(requester);
|
|
35
|
+
for feature_name in canonical_feature_names {
|
|
36
|
+
let canonical = feature_name.as_ref();
|
|
37
|
+
if let Some(requester_policy) = requester_policy {
|
|
38
|
+
if !requester_policy.is_permitted(canonical) {
|
|
39
|
+
return Err(PolicyDeniedError::new(canonical, requester).to_string());
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if let Some(policies) = self.policies.get(canonical) {
|
|
43
|
+
if !policies.is_requester_permitted(requester) {
|
|
44
|
+
return Err(PolicyDeniedError::new(canonical, requester).to_string());
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
Ok(())
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
pub(crate) fn get_policies(&self, canonical_feature_name: &str) -> Option<&Policies> {
|
|
52
|
+
self.policies.get(canonical_feature_name)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
pub(crate) fn insert_policy(&mut self, canonical_feature_name: String, policies: Policies) {
|
|
56
|
+
self.policies.insert(canonical_feature_name, policies);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
pub(crate) fn insert_requester_policy(
|
|
60
|
+
&mut self,
|
|
61
|
+
requester: String,
|
|
62
|
+
policy: RequesterFeaturePolicy,
|
|
63
|
+
) -> Option<RequesterFeaturePolicy> {
|
|
64
|
+
self.requester_policies.insert(requester, policy)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/// Loads policies from the given file and inserts them into the store.
|
|
68
|
+
pub(crate) fn load_policies_from_file(
|
|
69
|
+
&mut self,
|
|
70
|
+
directory: &Path,
|
|
71
|
+
policies_path: &Path,
|
|
72
|
+
config_path: &Path,
|
|
73
|
+
) -> Result<(), String> {
|
|
74
|
+
let resolved_policies_path = directory.join(policies_path);
|
|
75
|
+
if !resolved_policies_path.is_file() {
|
|
76
|
+
return Err(format!(
|
|
77
|
+
"Error loading policies: '{}' declared via 'policiesPath' in {} is not a file.",
|
|
78
|
+
resolved_policies_path.display(),
|
|
79
|
+
config_path.display()
|
|
80
|
+
));
|
|
81
|
+
}
|
|
82
|
+
let file = config::File::from(resolved_policies_path.as_path());
|
|
83
|
+
let policies_config = config::Config::builder()
|
|
84
|
+
.add_source(file)
|
|
85
|
+
.build()
|
|
86
|
+
.map_err(|e| {
|
|
87
|
+
format!(
|
|
88
|
+
"Error loading policies from {}: {e}",
|
|
89
|
+
resolved_policies_path.display()
|
|
90
|
+
)
|
|
91
|
+
})?;
|
|
92
|
+
let policies_file: PoliciesFileContents =
|
|
93
|
+
policies_config.try_deserialize().map_err(|e| {
|
|
94
|
+
format!(
|
|
95
|
+
"Error deserializing policies from {}: {e}",
|
|
96
|
+
resolved_policies_path.display()
|
|
97
|
+
)
|
|
98
|
+
})?;
|
|
99
|
+
for (requester, policy) in policies_file.requesters {
|
|
100
|
+
if self
|
|
101
|
+
.insert_requester_policy(requester.clone(), policy)
|
|
102
|
+
.is_some()
|
|
103
|
+
{
|
|
104
|
+
return Err(format!(
|
|
105
|
+
"Error loading policies from {}: policies for requester '{requester}' were already defined.",
|
|
106
|
+
resolved_policies_path.display()
|
|
107
|
+
));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
Ok(())
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/// Checks whether the requester is permitted for the given feature.
|
|
114
|
+
///
|
|
115
|
+
/// Both `requester_policies` and feature `policies` must permit the requester.
|
|
116
|
+
///
|
|
117
|
+
/// Returns `Ok(true)` if permitted, no policy is set, or no requester is given.
|
|
118
|
+
/// Returns `Ok(false)` if denied and `raise_if_policy_denied` is false.
|
|
119
|
+
/// Returns `Err(message)` if denied and `raise_if_policy_denied` is true.
|
|
120
|
+
pub(crate) fn is_feature_permitted_for_requester(
|
|
121
|
+
&self,
|
|
122
|
+
canonical_feature_name: &str,
|
|
123
|
+
requester: Option<&str>,
|
|
124
|
+
raise_if_policy_denied: bool,
|
|
125
|
+
) -> Result<bool, String> {
|
|
126
|
+
if let Some(requester) = requester {
|
|
127
|
+
let permitted =
|
|
128
|
+
self.is_requester_permitted_for_feature(canonical_feature_name, requester);
|
|
129
|
+
if !permitted {
|
|
130
|
+
if raise_if_policy_denied {
|
|
131
|
+
return Err(
|
|
132
|
+
PolicyDeniedError::new(canonical_feature_name, requester).to_string()
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
return Ok(false);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
Ok(true)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/// Checks whether the requester is permitted for the given feature.
|
|
142
|
+
/// Both `requester_policies` and feature `policies` must permit the requester.
|
|
143
|
+
pub(crate) fn is_requester_permitted_for_feature(
|
|
144
|
+
&self,
|
|
145
|
+
canonical_feature_name: &str,
|
|
146
|
+
requester: &str,
|
|
147
|
+
) -> bool {
|
|
148
|
+
if let Some(requester_policy) = self.requester_policies.get(requester) {
|
|
149
|
+
if !requester_policy.is_permitted(canonical_feature_name) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if let Some(policies) = self.policies.get(canonical_feature_name) {
|
|
154
|
+
if !policies.is_requester_permitted(requester) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
true
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/// Validates the requester policies against features and aliases loaded so far.
|
|
162
|
+
pub(crate) fn validate_requester_policies(
|
|
163
|
+
&self,
|
|
164
|
+
features: &Features,
|
|
165
|
+
aliases: &Aliases,
|
|
166
|
+
) -> Result<(), String> {
|
|
167
|
+
validate_requester_policies(&self.requester_policies, features, aliases, &self.policies)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
use std::collections::{HashMap, HashSet};
|
|
2
|
+
|
|
3
|
+
use serde::{Deserialize, Serialize};
|
|
4
|
+
|
|
5
|
+
/// Maps a requester identifier to the policy that determines which canonical feature names
|
|
6
|
+
/// the requester is permitted to use, declared in `.optify/policies.json`.
|
|
7
|
+
pub(crate) type RequesterPoliciesMap = HashMap<String, RequesterFeaturePolicy>;
|
|
8
|
+
|
|
9
|
+
/// The policy for the canonical feature names that a requester is permitted to use.
|
|
10
|
+
///
|
|
11
|
+
/// Either `allow` or `block` must be specified, not both.
|
|
12
|
+
///
|
|
13
|
+
/// - `allow`: Only the listed features may be used by the requester.
|
|
14
|
+
/// An empty set means no feature is currently allowed.
|
|
15
|
+
/// - `block`: The listed features may not be used by the requester.
|
|
16
|
+
/// All other features are allowed.
|
|
17
|
+
///
|
|
18
|
+
/// Feature names must be canonical feature names.
|
|
19
|
+
///
|
|
20
|
+
/// See https://github.com/juharris/optify#policies for more information.
|
|
21
|
+
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
22
|
+
#[serde(untagged)]
|
|
23
|
+
pub enum RequesterFeaturePolicy {
|
|
24
|
+
Allow { allow: HashSet<String> },
|
|
25
|
+
Block { block: HashSet<String> },
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
impl RequesterFeaturePolicy {
|
|
29
|
+
/// Returns `true` if the given canonical feature name is permitted by this policy.
|
|
30
|
+
pub fn is_permitted(&self, canonical_feature_name: &str) -> bool {
|
|
31
|
+
match self {
|
|
32
|
+
Self::Allow { allow } => allow.contains(canonical_feature_name),
|
|
33
|
+
Self::Block { block } => !block.contains(canonical_feature_name),
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/// Returns `true` if this policy explicitly names the given canonical feature name.
|
|
38
|
+
pub fn mentions_feature(&self, canonical_feature_name: &str) -> bool {
|
|
39
|
+
match self {
|
|
40
|
+
Self::Allow { allow } => allow.contains(canonical_feature_name),
|
|
41
|
+
Self::Block { block } => block.contains(canonical_feature_name),
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/// Returns an iterator over the feature names referenced by this policy.
|
|
46
|
+
pub fn feature_names(&self) -> impl Iterator<Item = &String> {
|
|
47
|
+
match self {
|
|
48
|
+
Self::Allow { allow } => allow.iter(),
|
|
49
|
+
Self::Block { block } => block.iter(),
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
use std::collections::HashSet;
|
|
2
|
+
|
|
3
|
+
use serde::{Deserialize, Serialize};
|
|
4
|
+
|
|
5
|
+
/// The policy for the requester identifier passed via preferences.
|
|
6
|
+
///
|
|
7
|
+
/// Either `allow` or `block` must be specified, not both.
|
|
8
|
+
///
|
|
9
|
+
/// - `allow`: Only the listed requesters may use this feature.
|
|
10
|
+
/// An empty set means no requester is currently allowed.
|
|
11
|
+
/// - `block`: The listed requesters may not use this feature.
|
|
12
|
+
/// All other requesters are allowed.
|
|
13
|
+
///
|
|
14
|
+
/// See https://github.com/juharris/optify#policies for more information.
|
|
15
|
+
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
16
|
+
#[serde(untagged)]
|
|
17
|
+
pub enum RequesterPolicy {
|
|
18
|
+
Allow { allow: HashSet<String> },
|
|
19
|
+
Block { block: HashSet<String> },
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
impl RequesterPolicy {
|
|
23
|
+
/// Returns `true` if the given requester is permitted by this policy.
|
|
24
|
+
pub fn is_permitted(&self, value: &str) -> bool {
|
|
25
|
+
match self {
|
|
26
|
+
Self::Allow { allow } => allow.contains(value),
|
|
27
|
+
Self::Block { block } => !block.contains(value),
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// Returns `true` if this policy explicitly names the given requester.
|
|
32
|
+
pub fn mentions_requester(&self, requester: &str) -> bool {
|
|
33
|
+
match self {
|
|
34
|
+
Self::Allow { allow } => allow.contains(requester),
|
|
35
|
+
Self::Block { block } => block.contains(requester),
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
use crate::provider::{Aliases, Features};
|
|
2
|
+
|
|
3
|
+
use super::feature_policies::PoliciesMap;
|
|
4
|
+
use super::requester_feature_policy::RequesterPoliciesMap;
|
|
5
|
+
|
|
6
|
+
/// Validates the requester policies declared in `.optify/policies.json` files against the
|
|
7
|
+
/// features loaded so far.
|
|
8
|
+
///
|
|
9
|
+
/// - Every feature name referenced must be a canonical feature name.
|
|
10
|
+
/// - When either `.optify/policies.json` explicitly names a feature or a feature's own
|
|
11
|
+
/// `policies.requester` explicitly names a requester, the two policies must agree for
|
|
12
|
+
/// that requester/feature pair.
|
|
13
|
+
///
|
|
14
|
+
/// Both `.optify/policies.json` and each feature's own `policies.requester` are
|
|
15
|
+
/// independently configurable and are both checked at runtime (see
|
|
16
|
+
/// `PolicyStore::is_requester_permitted_for_feature`); this only validates that they
|
|
17
|
+
/// don't explicitly contradict each other.
|
|
18
|
+
/// Nothing is merged here.
|
|
19
|
+
pub(crate) fn validate_requester_policies(
|
|
20
|
+
requester_policies: &RequesterPoliciesMap,
|
|
21
|
+
features: &Features,
|
|
22
|
+
aliases: &Aliases,
|
|
23
|
+
policies: &PoliciesMap,
|
|
24
|
+
) -> Result<(), String> {
|
|
25
|
+
for (requester, policy) in requester_policies {
|
|
26
|
+
// Make sure feature names in `requester_policies` are canonical and exist.
|
|
27
|
+
for feature_name in policy.feature_names() {
|
|
28
|
+
if features.contains_key(feature_name) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
let uni_case_feature_name = unicase::UniCase::new(feature_name.clone());
|
|
32
|
+
if let Some(canonical_feature_name) = aliases.get(&uni_case_feature_name) {
|
|
33
|
+
return Err(format!(
|
|
34
|
+
"Error validating policies for requester '{requester}': '{feature_name}' is an alias for canonical feature name '{canonical_feature_name}'. Policies must use canonical feature names for clarity and easier navigation."
|
|
35
|
+
));
|
|
36
|
+
}
|
|
37
|
+
return Err(format!(
|
|
38
|
+
"Error validating policies for requester '{requester}': feature '{feature_name}' does not exist."
|
|
39
|
+
));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
for (feature_name, policies) in policies {
|
|
43
|
+
let feature_policy = &policies.requester;
|
|
44
|
+
if (policy.mentions_feature(feature_name)
|
|
45
|
+
|| feature_policy.mentions_requester(requester))
|
|
46
|
+
&& policy.is_permitted(feature_name) != feature_policy.is_permitted(requester)
|
|
47
|
+
{
|
|
48
|
+
return Err(format!(
|
|
49
|
+
"Conflicting policies for requester '{requester}' and feature '{feature_name}': '.optify/policies.json' and the feature's own policies disagree."
|
|
50
|
+
));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
Ok(())
|
|
56
|
+
}
|
|
@@ -3,16 +3,13 @@ use std::{collections::HashMap, path::Path, sync::RwLock};
|
|
|
3
3
|
use crate::builder::builder_options::BuilderOptions;
|
|
4
4
|
use crate::configurable_values::configurable_list_impl::ConfigurableList;
|
|
5
5
|
|
|
6
|
+
use crate::policies::{Policies, PolicyStore};
|
|
6
7
|
use crate::{
|
|
7
8
|
builder::{OptionsProviderBuilder, OptionsRegistryBuilder},
|
|
8
9
|
configurable_string::LoadedFiles,
|
|
9
10
|
json::merge::{merge_json_with_defaults, FrozenPaths},
|
|
10
11
|
provider::GetOptionsPreferences,
|
|
11
|
-
schema::{
|
|
12
|
-
conditions::ConditionExpression,
|
|
13
|
-
metadata::OptionsMetadata,
|
|
14
|
-
policies::{Policies, PolicyDeniedError},
|
|
15
|
-
},
|
|
12
|
+
schema::{conditions::ConditionExpression, metadata::OptionsMetadata},
|
|
16
13
|
};
|
|
17
14
|
|
|
18
15
|
use super::OptionsRegistry;
|
|
@@ -26,7 +23,6 @@ pub(crate) type SourceValue = serde_json::Value;
|
|
|
26
23
|
pub(crate) type Aliases = HashMap<unicase::UniCase<String>, String>;
|
|
27
24
|
pub(crate) type Conditions = HashMap<String, ConditionExpression>;
|
|
28
25
|
pub(crate) type Features = HashMap<String, OptionsMetadata>;
|
|
29
|
-
pub(crate) type PoliciesMap = HashMap<String, Policies>;
|
|
30
26
|
pub(crate) type ReferencedFileToFeatureNames = HashMap<String, Vec<String>>;
|
|
31
27
|
pub(crate) type Sources = HashMap<String, SourceValue>;
|
|
32
28
|
|
|
@@ -45,13 +41,13 @@ pub struct OptionsProvider {
|
|
|
45
41
|
aliases: Aliases,
|
|
46
42
|
conditions: Conditions,
|
|
47
43
|
features: Features,
|
|
48
|
-
|
|
44
|
+
loaded_files: LoadedFiles,
|
|
45
|
+
policy_store: PolicyStore,
|
|
49
46
|
/// A map of files to their referencing features.
|
|
50
47
|
/// The keys are relative file paths and the values are lists of canonical feature names.
|
|
51
48
|
/// This allows fast lookup of features when a specific file is modified.
|
|
52
49
|
/// This is only populated if the `BuilderOptions` enable file reference tracking.
|
|
53
50
|
referenced_file_to_feature_names: Option<ReferencedFileToFeatureNames>,
|
|
54
|
-
loaded_files: LoadedFiles,
|
|
55
51
|
sources: Sources,
|
|
56
52
|
|
|
57
53
|
// Caches - using RwLock for thread-safe interior mutability
|
|
@@ -69,7 +65,7 @@ impl OptionsProvider {
|
|
|
69
65
|
keyed_configurable_string_pointers: HashMap<String, Vec<String>>,
|
|
70
66
|
conditions: Conditions,
|
|
71
67
|
features: Features,
|
|
72
|
-
|
|
68
|
+
policy_store: PolicyStore,
|
|
73
69
|
referenced_file_to_feature_names: Option<ReferencedFileToFeatureNames>,
|
|
74
70
|
loaded_files: LoadedFiles,
|
|
75
71
|
sources: Sources,
|
|
@@ -82,9 +78,9 @@ impl OptionsProvider {
|
|
|
82
78
|
aliases,
|
|
83
79
|
conditions,
|
|
84
80
|
features,
|
|
85
|
-
policies,
|
|
86
|
-
referenced_file_to_feature_names,
|
|
87
81
|
loaded_files,
|
|
82
|
+
policy_store,
|
|
83
|
+
referenced_file_to_feature_names,
|
|
88
84
|
sources,
|
|
89
85
|
entire_config_cache: RwLock::new(EntireConfigCache::new()),
|
|
90
86
|
options_cache: RwLock::new(OptionsCache::new()),
|
|
@@ -192,7 +188,8 @@ impl OptionsProvider {
|
|
|
192
188
|
source.get(key).cloned()
|
|
193
189
|
}
|
|
194
190
|
(None, _) => {
|
|
195
|
-
// No override.
|
|
191
|
+
// No override.
|
|
192
|
+
// Find first source with key (iterating in reverse = highest priority first).
|
|
196
193
|
// Use the last source with the key to avoid merging to an empty object.
|
|
197
194
|
let mut result = None;
|
|
198
195
|
let mut frozen_paths = FrozenPaths::new();
|
|
@@ -433,32 +430,6 @@ impl OptionsProvider {
|
|
|
433
430
|
}
|
|
434
431
|
Ok(())
|
|
435
432
|
}
|
|
436
|
-
|
|
437
|
-
/// Checks whether the requester is permitted for the given feature.
|
|
438
|
-
///
|
|
439
|
-
/// Returns `Ok(true)` if permitted, no policy is set, or no requester is given.
|
|
440
|
-
/// Returns `Ok(false)` if denied and `raise_if_policy_denied` is false.
|
|
441
|
-
/// Returns `Err(message)` if denied and `raise_if_policy_denied` is true.
|
|
442
|
-
fn is_feature_permitted_for_requester(
|
|
443
|
-
&self,
|
|
444
|
-
canonical_feature_name: &str,
|
|
445
|
-
requester: Option<&str>,
|
|
446
|
-
raise_if_policy_denied: bool,
|
|
447
|
-
) -> Result<bool, String> {
|
|
448
|
-
if let Some(requester) = requester {
|
|
449
|
-
if let Some(policies) = self.policies.get(canonical_feature_name) {
|
|
450
|
-
if !policies.is_requester_permitted(requester) {
|
|
451
|
-
if raise_if_policy_denied {
|
|
452
|
-
return Err(
|
|
453
|
-
PolicyDeniedError::new(canonical_feature_name, requester).to_string()
|
|
454
|
-
);
|
|
455
|
-
}
|
|
456
|
-
return Ok(false);
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
Ok(true)
|
|
461
|
-
}
|
|
462
433
|
}
|
|
463
434
|
|
|
464
435
|
impl OptionsRegistry for OptionsProvider {
|
|
@@ -601,7 +572,7 @@ impl OptionsRegistry for OptionsProvider {
|
|
|
601
572
|
}
|
|
602
573
|
}
|
|
603
574
|
|
|
604
|
-
if !self.is_feature_permitted_for_requester(
|
|
575
|
+
if !self.policy_store.is_feature_permitted_for_requester(
|
|
605
576
|
&canonical_feature_name,
|
|
606
577
|
requester,
|
|
607
578
|
raise_if_policy_denied,
|
|
@@ -679,21 +650,18 @@ impl OptionsRegistry for OptionsProvider {
|
|
|
679
650
|
feature_names: &[impl AsRef<str>],
|
|
680
651
|
_cache_options: Option<&CacheOptions>,
|
|
681
652
|
) -> Result<(), String> {
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
);
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
|
-
Ok(())
|
|
653
|
+
let canonical_feature_names = feature_names
|
|
654
|
+
.iter()
|
|
655
|
+
.map(|f| self.get_canonical_feature_name(f.as_ref()))
|
|
656
|
+
.collect::<Result<Vec<_>, _>>()?;
|
|
657
|
+
self.policy_store
|
|
658
|
+
.check_policies(requester, &canonical_feature_names)
|
|
693
659
|
}
|
|
694
660
|
|
|
695
661
|
fn get_policies(&self, canonical_feature_name: &str) -> Option<Policies> {
|
|
696
|
-
self.
|
|
662
|
+
self.policy_store
|
|
663
|
+
.get_policies(canonical_feature_name)
|
|
664
|
+
.cloned()
|
|
697
665
|
}
|
|
698
666
|
|
|
699
667
|
fn map_feature_names(
|
|
@@ -732,7 +700,7 @@ impl OptionsRegistry for OptionsProvider {
|
|
|
732
700
|
}
|
|
733
701
|
}
|
|
734
702
|
|
|
735
|
-
if !self.is_feature_permitted_for_requester(
|
|
703
|
+
if !self.policy_store.is_feature_permitted_for_requester(
|
|
736
704
|
&canonical_feature_name,
|
|
737
705
|
requester,
|
|
738
706
|
raise_if_policy_denied,
|
|
@@ -4,9 +4,10 @@ use serde_json::Value;
|
|
|
4
4
|
|
|
5
5
|
use crate::builder::builder_options::BuilderOptions;
|
|
6
6
|
|
|
7
|
+
use crate::policies::Policies;
|
|
7
8
|
use crate::{
|
|
8
9
|
provider::{CacheOptions, Features, GetOptionsPreferences},
|
|
9
|
-
schema::
|
|
10
|
+
schema::metadata::OptionsMetadata,
|
|
10
11
|
};
|
|
11
12
|
|
|
12
13
|
/// Trait defining the core functionality for an options provider
|
|
@@ -5,10 +5,11 @@ use std::sync::{mpsc::channel, Arc, Mutex, RwLock};
|
|
|
5
5
|
|
|
6
6
|
use crate::builder::builder_options::BuilderOptions;
|
|
7
7
|
use crate::builder::{OptionsRegistryBuilder, OptionsWatcherBuilder};
|
|
8
|
+
use crate::policies::Policies;
|
|
8
9
|
use crate::provider::{
|
|
9
10
|
CacheOptions, Features, GetOptionsPreferences, OptionsProvider, OptionsRegistry, WatcherOptions,
|
|
10
11
|
};
|
|
11
|
-
use crate::schema::
|
|
12
|
+
use crate::schema::metadata::OptionsMetadata;
|
|
12
13
|
|
|
13
14
|
pub type OptionsWatcherListener = Arc<dyn Fn(&HashSet<PathBuf>) + Send + Sync>;
|
|
14
15
|
|
|
@@ -342,18 +343,18 @@ impl OptionsRegistry for OptionsWatcher {
|
|
|
342
343
|
)
|
|
343
344
|
}
|
|
344
345
|
|
|
345
|
-
fn
|
|
346
|
+
fn get_policies(&self, canonical_feature_name: &str) -> Option<Policies> {
|
|
346
347
|
self.current_provider
|
|
347
348
|
.read()
|
|
348
349
|
.unwrap()
|
|
349
|
-
.
|
|
350
|
+
.get_policies(canonical_feature_name)
|
|
350
351
|
}
|
|
351
352
|
|
|
352
|
-
fn
|
|
353
|
+
fn has_conditions(&self, canonical_feature_name: &str) -> bool {
|
|
353
354
|
self.current_provider
|
|
354
355
|
.read()
|
|
355
356
|
.unwrap()
|
|
356
|
-
.
|
|
357
|
+
.has_conditions(canonical_feature_name)
|
|
357
358
|
}
|
|
358
359
|
|
|
359
360
|
fn map_feature_names(
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
use std::collections::HashSet;
|
|
2
|
-
use std::fmt;
|
|
3
|
-
|
|
4
|
-
use serde::{Deserialize, Serialize};
|
|
5
|
-
|
|
6
|
-
/// Error returned when a requester is not permitted to use a feature.
|
|
7
|
-
#[derive(Clone, Debug)]
|
|
8
|
-
pub struct PolicyDeniedError {
|
|
9
|
-
pub feature_name: String,
|
|
10
|
-
pub requester: String,
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
impl PolicyDeniedError {
|
|
14
|
-
pub fn new(feature_name: impl Into<String>, requester: impl Into<String>) -> Self {
|
|
15
|
-
Self {
|
|
16
|
-
feature_name: feature_name.into(),
|
|
17
|
-
requester: requester.into(),
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
impl fmt::Display for PolicyDeniedError {
|
|
23
|
-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
24
|
-
write!(
|
|
25
|
-
f,
|
|
26
|
-
"Requester \"{}\" is not permitted to use feature \"{}\".",
|
|
27
|
-
self.requester, self.feature_name
|
|
28
|
-
)
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
impl std::error::Error for PolicyDeniedError {}
|
|
33
|
-
|
|
34
|
-
/// The policy for the requester identifier passed via preferences.
|
|
35
|
-
///
|
|
36
|
-
/// Either `allow` or `block` must be specified, not both.
|
|
37
|
-
///
|
|
38
|
-
/// - `allow`: Only the listed requesters may use this feature.
|
|
39
|
-
/// An empty set means no requester is currently allowed.
|
|
40
|
-
/// - `block`: The listed requesters may not use this feature.
|
|
41
|
-
/// All other requesters are allowed.
|
|
42
|
-
///
|
|
43
|
-
/// See https://github.com/juharris/optify#policies for more information.
|
|
44
|
-
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
45
|
-
#[serde(untagged)]
|
|
46
|
-
pub enum RequesterPolicy {
|
|
47
|
-
Allow { allow: HashSet<String> },
|
|
48
|
-
Block { block: HashSet<String> },
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
impl RequesterPolicy {
|
|
52
|
-
/// Returns `true` if the given requester is permitted by this policy.
|
|
53
|
-
pub fn is_permitted(&self, value: &str) -> bool {
|
|
54
|
-
match self {
|
|
55
|
-
Self::Allow { allow } => allow.contains(value),
|
|
56
|
-
Self::Block { block } => !block.contains(value),
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/// Policies that restrict access to a feature based on values in the request's preferences.
|
|
62
|
-
///
|
|
63
|
-
/// Policies are checked for the **top-level features** in a request.
|
|
64
|
-
/// Unlike conditions, policies are **not** evaluated on imported features — a feature may
|
|
65
|
-
/// freely import another feature that has policies without those policies being enforced.
|
|
66
|
-
///
|
|
67
|
-
/// See https://github.com/juharris/optify#policies for details and a comparison with conditions.
|
|
68
|
-
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
69
|
-
#[serde(deny_unknown_fields)]
|
|
70
|
-
pub struct Policies {
|
|
71
|
-
/// Restrictions based on the requester identifier passed via preferences.
|
|
72
|
-
///
|
|
73
|
-
/// Use `allow` to specify an allowlist (only those requesters may use this feature).
|
|
74
|
-
/// Use `block` to specify a denylist (all requesters except those listed may use this feature).
|
|
75
|
-
/// `allow` and `block` are mutually exclusive.
|
|
76
|
-
pub requester: Option<RequesterPolicy>,
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
impl Policies {
|
|
80
|
-
/// Returns `true` if the given requester is permitted to use the feature.
|
|
81
|
-
///
|
|
82
|
-
/// If no `requester` policy is set, all requesters are permitted.
|
|
83
|
-
pub fn is_requester_permitted(&self, requester: &str) -> bool {
|
|
84
|
-
self.requester
|
|
85
|
-
.as_ref()
|
|
86
|
-
.is_none_or(|p| p.is_permitted(requester))
|
|
87
|
-
}
|
|
88
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{optify-0.11.0 → optify-0.12.0}/rust/optify/src/configurable_string/configurable_string_impl.rs
RENAMED
|
File without changes
|
|
File without changes
|
{optify-0.11.0 → optify-0.12.0}/rust/optify/src/configurable_values/configurable_list_impl.rs
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|