Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "path/filepath" |
| 19 | "reflect" |
| 20 | "strconv" |
| 21 | "strings" |
| 22 | |
| 23 | "github.com/google/blueprint/proptools" |
| 24 | ) |
| 25 | |
| 26 | // "neverallow" rules for the build system. |
| 27 | // |
| 28 | // This allows things which aren't related to the build system and are enforced |
| 29 | // for sanity, in progress code refactors, or policy to be expressed in a |
| 30 | // straightforward away disjoint from implementations and tests which should |
| 31 | // work regardless of these restrictions. |
| 32 | // |
| 33 | // A module is disallowed if all of the following are true: |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 34 | // - it is in one of the "In" paths |
| 35 | // - it is not in one of the "NotIn" paths |
| 36 | // - it has all "With" properties matched |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 37 | // - - values are matched in their entirety |
| 38 | // - - nil is interpreted as an empty string |
| 39 | // - - nested properties are separated with a '.' |
| 40 | // - - if the property is a list, any of the values in the list being matches |
| 41 | // counts as a match |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 42 | // - it has none of the "Without" properties matched (same rules as above) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 43 | |
| 44 | func registerNeverallowMutator(ctx RegisterMutatorsContext) { |
| 45 | ctx.BottomUp("neverallow", neverallowMutator).Parallel() |
| 46 | } |
| 47 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 48 | var neverallows = []Rule{} |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 49 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 50 | func init() { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame^] | 51 | AddNeverAllowRules(createIncludeDirsRules()...) |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 52 | AddNeverAllowRules(createTrebleRules()...) |
| 53 | AddNeverAllowRules(createLibcoreRules()...) |
| 54 | AddNeverAllowRules(createMediaRules()...) |
| 55 | AddNeverAllowRules(createJavaDeviceForHostRules()...) |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 56 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 57 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 58 | // Add a NeverAllow rule to the set of rules to apply. |
| 59 | func AddNeverAllowRules(rules ...Rule) { |
| 60 | neverallows = append(neverallows, rules...) |
| 61 | } |
| 62 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame^] | 63 | func createIncludeDirsRules() []Rule { |
| 64 | // The list of paths that cannot be referenced using include_dirs |
| 65 | paths := []string{ |
| 66 | "art", |
| 67 | "libcore", |
| 68 | "libnativehelper", |
| 69 | "external/apache-harmony", |
| 70 | "external/apache-xml", |
| 71 | "external/boringssl", |
| 72 | "external/bouncycastle", |
| 73 | "external/conscrypt", |
| 74 | "external/icu", |
| 75 | "external/okhttp", |
| 76 | "external/vixl", |
| 77 | "external/wycheproof", |
| 78 | "system/core/libnativebridge", |
| 79 | "system/core/libnativehelper", |
| 80 | } |
| 81 | |
| 82 | // Create a composite matcher that will match if the value starts with any of the restricted |
| 83 | // paths. A / is appended to the prefix to ensure that restricting path X does not affect paths |
| 84 | // XY. |
| 85 | rules := make([]Rule, 0, len(paths)) |
| 86 | for _, path := range paths { |
| 87 | rule := |
| 88 | NeverAllow(). |
| 89 | WithMatcher("include_dirs", StartsWith(path+"/")). |
| 90 | Because("include_dirs is deprecated, all usages of '" + path + "' have been migrated" + |
| 91 | " to use alternate mechanisms and so can no longer be used.") |
| 92 | |
| 93 | rules = append(rules, rule) |
| 94 | } |
| 95 | |
| 96 | return rules |
| 97 | } |
| 98 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 99 | func createTrebleRules() []Rule { |
| 100 | return []Rule{ |
| 101 | NeverAllow(). |
| 102 | In("vendor", "device"). |
| 103 | With("vndk.enabled", "true"). |
| 104 | Without("vendor", "true"). |
| 105 | Because("the VNDK can never contain a library that is device dependent."), |
| 106 | NeverAllow(). |
| 107 | With("vndk.enabled", "true"). |
| 108 | Without("vendor", "true"). |
| 109 | Without("owner", ""). |
| 110 | Because("a VNDK module can never have an owner."), |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 111 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 112 | // TODO(b/67974785): always enforce the manifest |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 113 | NeverAllow(). |
| 114 | Without("name", "libhidltransport-impl-internal"). |
| 115 | With("product_variables.enforce_vintf_manifest.cflags", "*"). |
| 116 | Because("manifest enforcement should be independent of ."), |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 117 | |
| 118 | // TODO(b/67975799): vendor code should always use /vendor/bin/sh |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 119 | NeverAllow(). |
| 120 | Without("name", "libc_bionic_ndk"). |
| 121 | With("product_variables.treble_linker_namespaces.cflags", "*"). |
| 122 | Because("nothing should care if linker namespaces are enabled or not"), |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 123 | |
| 124 | // Example: |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 125 | // *NeverAllow().with("Srcs", "main.cpp")) |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 129 | func createLibcoreRules() []Rule { |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 130 | var coreLibraryProjects = []string{ |
| 131 | "libcore", |
| 132 | "external/apache-harmony", |
| 133 | "external/apache-xml", |
| 134 | "external/bouncycastle", |
| 135 | "external/conscrypt", |
| 136 | "external/icu", |
| 137 | "external/okhttp", |
| 138 | "external/wycheproof", |
Paul Duffin | b6c6bdd | 2019-06-07 11:43:55 +0100 | [diff] [blame] | 139 | |
| 140 | // Not really a core library but still needs access to same capabilities. |
| 141 | "development", |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Paul Duffin | a3d0986 | 2019-06-11 13:40:47 +0100 | [diff] [blame] | 144 | // Core library constraints. The sdk_version: "none" can only be used in core library projects. |
| 145 | // Access to core library targets is restricted using visibility rules. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 146 | rules := []Rule{ |
| 147 | NeverAllow(). |
| 148 | NotIn(coreLibraryProjects...). |
| 149 | With("sdk_version", "none"), |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 152 | return rules |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 155 | func createMediaRules() []Rule { |
| 156 | return []Rule{ |
| 157 | NeverAllow(). |
| 158 | With("libs", "updatable-media"). |
| 159 | Because("updatable-media includes private APIs. Use updatable_media_stubs instead."), |
Dongwon Kang | 50a299f | 2019-02-04 09:00:51 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 163 | func createJavaDeviceForHostRules() []Rule { |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 164 | javaDeviceForHostProjectsWhitelist := []string{ |
Colin Cross | b5191a5 | 2019-04-11 14:07:38 -0700 | [diff] [blame] | 165 | "external/guava", |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 166 | "external/robolectric-shadows", |
| 167 | "framework/layoutlib", |
| 168 | } |
| 169 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 170 | return []Rule{ |
| 171 | NeverAllow(). |
| 172 | NotIn(javaDeviceForHostProjectsWhitelist...). |
| 173 | ModuleType("java_device_for_host", "java_host_for_device"). |
| 174 | Because("java_device_for_host can only be used in whitelisted projects"), |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 178 | func neverallowMutator(ctx BottomUpMutatorContext) { |
| 179 | m, ok := ctx.Module().(Module) |
| 180 | if !ok { |
| 181 | return |
| 182 | } |
| 183 | |
| 184 | dir := ctx.ModuleDir() + "/" |
| 185 | properties := m.GetProperties() |
| 186 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 187 | for _, r := range neverallows { |
| 188 | n := r.(*rule) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 189 | if !n.appliesToPath(dir) { |
| 190 | continue |
| 191 | } |
| 192 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 193 | if !n.appliesToModuleType(ctx.ModuleType()) { |
| 194 | continue |
| 195 | } |
| 196 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 197 | if !n.appliesToProperties(properties) { |
| 198 | continue |
| 199 | } |
| 200 | |
| 201 | ctx.ModuleErrorf("violates " + n.String()) |
| 202 | } |
| 203 | } |
| 204 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 205 | type ValueMatcher interface { |
| 206 | test(string) bool |
| 207 | String() string |
| 208 | } |
| 209 | |
| 210 | type equalMatcher struct { |
| 211 | expected string |
| 212 | } |
| 213 | |
| 214 | func (m *equalMatcher) test(value string) bool { |
| 215 | return m.expected == value |
| 216 | } |
| 217 | |
| 218 | func (m *equalMatcher) String() string { |
| 219 | return "=" + m.expected |
| 220 | } |
| 221 | |
| 222 | type anyMatcher struct { |
| 223 | } |
| 224 | |
| 225 | func (m *anyMatcher) test(value string) bool { |
| 226 | return true |
| 227 | } |
| 228 | |
| 229 | func (m *anyMatcher) String() string { |
| 230 | return "=*" |
| 231 | } |
| 232 | |
| 233 | var anyMatcherInstance = &anyMatcher{} |
| 234 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame^] | 235 | type startsWithMatcher struct { |
| 236 | prefix string |
| 237 | } |
| 238 | |
| 239 | func (m *startsWithMatcher) test(value string) bool { |
| 240 | return strings.HasPrefix(value, m.prefix) |
| 241 | } |
| 242 | |
| 243 | func (m *startsWithMatcher) String() string { |
| 244 | return ".starts-with(" + m.prefix + ")" |
| 245 | } |
| 246 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 247 | type ruleProperty struct { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 248 | fields []string // e.x.: Vndk.Enabled |
| 249 | matcher ValueMatcher |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 252 | // A NeverAllow rule. |
| 253 | type Rule interface { |
| 254 | In(path ...string) Rule |
| 255 | |
| 256 | NotIn(path ...string) Rule |
| 257 | |
| 258 | ModuleType(types ...string) Rule |
| 259 | |
| 260 | NotModuleType(types ...string) Rule |
| 261 | |
| 262 | With(properties, value string) Rule |
| 263 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame^] | 264 | WithMatcher(properties string, matcher ValueMatcher) Rule |
| 265 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 266 | Without(properties, value string) Rule |
| 267 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame^] | 268 | WithoutMatcher(properties string, matcher ValueMatcher) Rule |
| 269 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 270 | Because(reason string) Rule |
| 271 | } |
| 272 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 273 | type rule struct { |
| 274 | // User string for why this is a thing. |
| 275 | reason string |
| 276 | |
| 277 | paths []string |
| 278 | unlessPaths []string |
| 279 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 280 | moduleTypes []string |
| 281 | unlessModuleTypes []string |
| 282 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 283 | props []ruleProperty |
| 284 | unlessProps []ruleProperty |
| 285 | } |
| 286 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 287 | // Create a new NeverAllow rule. |
| 288 | func NeverAllow() Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 289 | return &rule{} |
| 290 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 291 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 292 | func (r *rule) In(path ...string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 293 | r.paths = append(r.paths, cleanPaths(path)...) |
| 294 | return r |
| 295 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 296 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 297 | func (r *rule) NotIn(path ...string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 298 | r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...) |
| 299 | return r |
| 300 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 301 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 302 | func (r *rule) ModuleType(types ...string) Rule { |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 303 | r.moduleTypes = append(r.moduleTypes, types...) |
| 304 | return r |
| 305 | } |
| 306 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 307 | func (r *rule) NotModuleType(types ...string) Rule { |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 308 | r.unlessModuleTypes = append(r.unlessModuleTypes, types...) |
| 309 | return r |
| 310 | } |
| 311 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 312 | func (r *rule) With(properties, value string) Rule { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame^] | 313 | return r.WithMatcher(properties, selectMatcher(value)) |
| 314 | } |
| 315 | |
| 316 | func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 317 | r.props = append(r.props, ruleProperty{ |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 318 | fields: fieldNamesForProperties(properties), |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame^] | 319 | matcher: matcher, |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 320 | }) |
| 321 | return r |
| 322 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 323 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 324 | func (r *rule) Without(properties, value string) Rule { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame^] | 325 | return r.WithoutMatcher(properties, selectMatcher(value)) |
| 326 | } |
| 327 | |
| 328 | func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 329 | r.unlessProps = append(r.unlessProps, ruleProperty{ |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 330 | fields: fieldNamesForProperties(properties), |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame^] | 331 | matcher: matcher, |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 332 | }) |
| 333 | return r |
| 334 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 335 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 336 | func selectMatcher(expected string) ValueMatcher { |
| 337 | if expected == "*" { |
| 338 | return anyMatcherInstance |
| 339 | } |
| 340 | return &equalMatcher{expected: expected} |
| 341 | } |
| 342 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 343 | func (r *rule) Because(reason string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 344 | r.reason = reason |
| 345 | return r |
| 346 | } |
| 347 | |
| 348 | func (r *rule) String() string { |
| 349 | s := "neverallow" |
| 350 | for _, v := range r.paths { |
| 351 | s += " dir:" + v + "*" |
| 352 | } |
| 353 | for _, v := range r.unlessPaths { |
| 354 | s += " -dir:" + v + "*" |
| 355 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 356 | for _, v := range r.moduleTypes { |
| 357 | s += " type:" + v |
| 358 | } |
| 359 | for _, v := range r.unlessModuleTypes { |
| 360 | s += " -type:" + v |
| 361 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 362 | for _, v := range r.props { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 363 | s += " " + strings.Join(v.fields, ".") + v.matcher.String() |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 364 | } |
| 365 | for _, v := range r.unlessProps { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 366 | s += " -" + strings.Join(v.fields, ".") + v.matcher.String() |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 367 | } |
| 368 | if len(r.reason) != 0 { |
| 369 | s += " which is restricted because " + r.reason |
| 370 | } |
| 371 | return s |
| 372 | } |
| 373 | |
| 374 | func (r *rule) appliesToPath(dir string) bool { |
| 375 | includePath := len(r.paths) == 0 || hasAnyPrefix(dir, r.paths) |
| 376 | excludePath := hasAnyPrefix(dir, r.unlessPaths) |
| 377 | return includePath && !excludePath |
| 378 | } |
| 379 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 380 | func (r *rule) appliesToModuleType(moduleType string) bool { |
| 381 | return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes) |
| 382 | } |
| 383 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 384 | func (r *rule) appliesToProperties(properties []interface{}) bool { |
| 385 | includeProps := hasAllProperties(properties, r.props) |
| 386 | excludeProps := hasAnyProperty(properties, r.unlessProps) |
| 387 | return includeProps && !excludeProps |
| 388 | } |
| 389 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame^] | 390 | func StartsWith(prefix string) ValueMatcher { |
| 391 | return &startsWithMatcher{prefix} |
| 392 | } |
| 393 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 394 | // assorted utils |
| 395 | |
| 396 | func cleanPaths(paths []string) []string { |
| 397 | res := make([]string, len(paths)) |
| 398 | for i, v := range paths { |
| 399 | res[i] = filepath.Clean(v) + "/" |
| 400 | } |
| 401 | return res |
| 402 | } |
| 403 | |
| 404 | func fieldNamesForProperties(propertyNames string) []string { |
| 405 | names := strings.Split(propertyNames, ".") |
| 406 | for i, v := range names { |
| 407 | names[i] = proptools.FieldNameForProperty(v) |
| 408 | } |
| 409 | return names |
| 410 | } |
| 411 | |
| 412 | func hasAnyPrefix(s string, prefixes []string) bool { |
| 413 | for _, prefix := range prefixes { |
| 414 | if strings.HasPrefix(s, prefix) { |
| 415 | return true |
| 416 | } |
| 417 | } |
| 418 | return false |
| 419 | } |
| 420 | |
| 421 | func hasAnyProperty(properties []interface{}, props []ruleProperty) bool { |
| 422 | for _, v := range props { |
| 423 | if hasProperty(properties, v) { |
| 424 | return true |
| 425 | } |
| 426 | } |
| 427 | return false |
| 428 | } |
| 429 | |
| 430 | func hasAllProperties(properties []interface{}, props []ruleProperty) bool { |
| 431 | for _, v := range props { |
| 432 | if !hasProperty(properties, v) { |
| 433 | return false |
| 434 | } |
| 435 | } |
| 436 | return true |
| 437 | } |
| 438 | |
| 439 | func hasProperty(properties []interface{}, prop ruleProperty) bool { |
| 440 | for _, propertyStruct := range properties { |
| 441 | propertiesValue := reflect.ValueOf(propertyStruct).Elem() |
| 442 | for _, v := range prop.fields { |
| 443 | if !propertiesValue.IsValid() { |
| 444 | break |
| 445 | } |
| 446 | propertiesValue = propertiesValue.FieldByName(v) |
| 447 | } |
| 448 | if !propertiesValue.IsValid() { |
| 449 | continue |
| 450 | } |
| 451 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 452 | check := func(value string) bool { |
| 453 | return prop.matcher.test(value) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | if matchValue(propertiesValue, check) { |
| 457 | return true |
| 458 | } |
| 459 | } |
| 460 | return false |
| 461 | } |
| 462 | |
| 463 | func matchValue(value reflect.Value, check func(string) bool) bool { |
| 464 | if !value.IsValid() { |
| 465 | return false |
| 466 | } |
| 467 | |
| 468 | if value.Kind() == reflect.Ptr { |
| 469 | if value.IsNil() { |
| 470 | return check("") |
| 471 | } |
| 472 | value = value.Elem() |
| 473 | } |
| 474 | |
| 475 | switch value.Kind() { |
| 476 | case reflect.String: |
| 477 | return check(value.String()) |
| 478 | case reflect.Bool: |
| 479 | return check(strconv.FormatBool(value.Bool())) |
| 480 | case reflect.Int: |
| 481 | return check(strconv.FormatInt(value.Int(), 10)) |
| 482 | case reflect.Slice: |
| 483 | slice, ok := value.Interface().([]string) |
| 484 | if !ok { |
| 485 | panic("Can only handle slice of string") |
| 486 | } |
| 487 | for _, v := range slice { |
| 488 | if check(v) { |
| 489 | return true |
| 490 | } |
| 491 | } |
| 492 | return false |
| 493 | } |
| 494 | |
| 495 | panic("Can't handle type: " + value.Kind().String()) |
| 496 | } |