-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #213 from brharrington/v1.4.x-issue-175
fixes #175, backport validation rules
- Loading branch information
Showing
28 changed files
with
1,141 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
atlas-core/src/main/scala/com/netflix/atlas/core/validation/HasKeyRule.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2015 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.atlas.core.validation | ||
|
||
import com.netflix.atlas.core.util.SmallHashMap | ||
import com.typesafe.config.Config | ||
|
||
/** | ||
* Verifies that the tags contain a specified key. Sample config: | ||
* | ||
* ``` | ||
* key = name | ||
* ``` | ||
*/ | ||
class HasKeyRule(config: Config) extends Rule { | ||
private val key = config.getString("key") | ||
|
||
def validate(tags: SmallHashMap[String, String]): ValidationResult = { | ||
if (tags.contains(key)) ValidationResult.Pass else failure(s"missing '$key': ${tags.keys}") | ||
} | ||
} | ||
|
42 changes: 42 additions & 0 deletions
42
atlas-core/src/main/scala/com/netflix/atlas/core/validation/KeyLengthRule.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2015 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.atlas.core.validation | ||
|
||
import com.typesafe.config.Config | ||
|
||
/** | ||
* Verifies that the keys are within the specified length bounds. Sample config: | ||
* | ||
* ``` | ||
* min-length = 2 | ||
* max-length = 60 | ||
* ``` | ||
*/ | ||
class KeyLengthRule(config: Config) extends TagRule { | ||
private val minLength = config.getInt("min-length") | ||
private val maxLength = config.getInt("max-length") | ||
|
||
def validate(k: String, v: String): ValidationResult = { | ||
k.length match { | ||
case len if len > maxLength => | ||
failure(s"key too long: [$k] ($len > $maxLength)") | ||
case len if len < minLength => | ||
failure(s"key too short: [$k] ($len < $minLength)") | ||
case _ => | ||
ValidationResult.Pass | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
atlas-core/src/main/scala/com/netflix/atlas/core/validation/KeyPatternRule.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2015 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.atlas.core.validation | ||
|
||
import java.util.regex.Pattern | ||
|
||
import com.typesafe.config.Config | ||
|
||
/** | ||
* Verifies that the keys match a specified pattern. Sample config: | ||
* | ||
* ``` | ||
* pattern = "^[-_.a-zA-Z0-9]{4,60}$" | ||
* ``` | ||
*/ | ||
class KeyPatternRule(config: Config) extends TagRule { | ||
|
||
private val pattern = Pattern.compile(config.getString("pattern")) | ||
|
||
def validate(k: String, v: String): ValidationResult = { | ||
if (pattern.matcher(k).matches()) ValidationResult.Pass else { | ||
failure(s"key doesn't match pattern '$pattern': [$k]") | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
atlas-core/src/main/scala/com/netflix/atlas/core/validation/MaxUserTagsRule.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2015 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.atlas.core.validation | ||
|
||
import com.netflix.atlas.core.model.TagKey | ||
import com.netflix.atlas.core.util.SmallHashMap | ||
import com.typesafe.config.Config | ||
|
||
/** | ||
* Verifies that the number of custom user tags are within a specified limit. Sample config: | ||
* | ||
* ``` | ||
* limit = 10 | ||
* ``` | ||
*/ | ||
class MaxUserTagsRule(config: Config) extends Rule { | ||
|
||
private val limit = config.getInt("limit") | ||
|
||
override def validate(tags: SmallHashMap[String, String]): ValidationResult = { | ||
var count = 0 | ||
val iter = tags.entriesIterator | ||
while (iter.hasNext) { | ||
if (!TagKey.isRestricted(iter.key)) count += 1 | ||
iter.nextEntry() | ||
} | ||
if (count <= limit) ValidationResult.Pass else { | ||
failure(s"too many user tags: $count > $limit") | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
atlas-core/src/main/scala/com/netflix/atlas/core/validation/NonStandardKeyRule.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2015 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.atlas.core.validation | ||
|
||
import com.netflix.atlas.core.model.TagKey | ||
import com.typesafe.config.Config | ||
|
||
/** | ||
* Verifies that no non-standard keys are used with a standardized prefix ("nf." and "atlas."). | ||
*/ | ||
class NonStandardKeyRule(config: Config) extends TagRule { | ||
|
||
override def validate(k: String, v: String): ValidationResult = { | ||
if (TagKey.isValid(k)) ValidationResult.Pass else failure(s"non-standard key: $k") | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
atlas-core/src/main/scala/com/netflix/atlas/core/validation/Rule.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2015 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.atlas.core.validation | ||
|
||
import com.netflix.atlas.core.util.SmallHashMap | ||
import com.typesafe.config.Config | ||
|
||
/** | ||
* Base type for validation rules. | ||
*/ | ||
trait Rule { | ||
|
||
private val ruleName = getClass.getSimpleName | ||
|
||
/** | ||
* Validates that the tag map matches the rule. | ||
*/ | ||
def validate(tags: Map[String, String]): ValidationResult = { | ||
tags match { | ||
case m: SmallHashMap[String, String] => validate(m) | ||
case _ => validate(SmallHashMap(tags)) | ||
} | ||
} | ||
|
||
/** | ||
* Validates that the tag map matches the rule. | ||
*/ | ||
def validate(tags: SmallHashMap[String, String]): ValidationResult | ||
|
||
/** | ||
* Helper for generating the failure response. | ||
*/ | ||
protected def failure(reason: String): ValidationResult = { | ||
ValidationResult.Fail(ruleName, reason) | ||
} | ||
} | ||
|
||
object Rule { | ||
def load(ruleConfigs: java.util.List[_ <: Config]): List[Rule] = { | ||
import scala.collection.JavaConverters._ | ||
load(ruleConfigs.asScala.toList) | ||
} | ||
|
||
def load(ruleConfigs: List[_ <: Config]): List[Rule] = { | ||
ruleConfigs.map { cfg => | ||
val cls = Class.forName(cfg.getString("class")) | ||
cls.getConstructor(classOf[Config]).newInstance(cfg).asInstanceOf[Rule] | ||
} | ||
} | ||
|
||
@scala.annotation.tailrec | ||
def validate(tags: Map[String, String], rules: List[Rule]): ValidationResult = { | ||
if (rules.isEmpty) ValidationResult.Pass else { | ||
val res = rules.head.validate(tags) | ||
if (res.isFailure) res else validate(tags, rules.tail) | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.