-
-
Notifications
You must be signed in to change notification settings - Fork 16
fix: Don't allow uppercase characters in Kubernetes object names #1095
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xeniape
wants to merge
10
commits into
main
Choose a base branch
from
fix/no-uppercase-in-kubernetes-resource-names
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8c9b1b3
fix: separate between official and kubernetes-native RFC 1123 definition
xeniape be30a23
add changelog entry
xeniape abd7043
subdomain format also kubernetes-specific
xeniape 897b751
adjust variable names and add docs, fix changelog
xeniape c541acc
rename functions
xeniape 5e6ec66
fix links in docs
xeniape 91108ce
make const public
xeniape fd13803
Merge branch 'main' into fix/no-uppercase-in-kubernetes-resource-names
xeniape 1475131
Update crates/stackable-operator/src/validation.rs
xeniape 43be1be
Update crates/stackable-operator/src/validation.rs
xeniape File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 | ||||
---|---|---|---|---|---|---|
|
@@ -16,27 +16,38 @@ use regex::Regex; | |||||
use snafu::Snafu; | ||||||
|
||||||
/// Minimal length required by RFC 1123 is 63. Up to 255 allowed, unsupported by k8s. | ||||||
const RFC_1123_LABEL_MAX_LENGTH: usize = 63; | ||||||
pub const RFC_1123_LABEL_FMT: &str = "[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])?"; | ||||||
const RFC_1123_LABEL_ERROR_MSG: &str = "a RFC 1123 label must consist of alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character"; | ||||||
pub const RFC_1123_LABEL_MAX_LENGTH: usize = 63; | ||||||
// This is a modified RFC 1123 format according to the Kubernetes specification, see https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names | ||||||
pub const LOWERCASE_RFC_1123_LABEL_FMT: &str = "[a-z0-9]([-a-z0-9]*[a-z0-9])?"; | ||||||
const LOWERCASE_RFC_1123_LABEL_ERROR_MSG: &str = "a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character"; | ||||||
|
||||||
/// This is a subdomain's max length in DNS (RFC 1123) | ||||||
const RFC_1123_SUBDOMAIN_MAX_LENGTH: usize = 253; | ||||||
const RFC_1123_SUBDOMAIN_FMT: &str = | ||||||
concatcp!(RFC_1123_LABEL_FMT, "(\\.", RFC_1123_LABEL_FMT, ")*"); | ||||||
// This is a RFC 1123 format, see https://www.rfc-editor.org/rfc/rfc1123 | ||||||
const RFC_1123_LABEL_FMT: &str = "[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])?"; | ||||||
|
||||||
const DOMAIN_MAX_LENGTH: usize = RFC_1123_SUBDOMAIN_MAX_LENGTH; | ||||||
/// Same as [`RFC_1123_SUBDOMAIN_FMT`], but allows a trailing dot | ||||||
const DOMAIN_FMT: &str = concatcp!(RFC_1123_SUBDOMAIN_FMT, "\\.?"); | ||||||
/// This is a subdomain's max length in DNS (RFC 1123) | ||||||
pub const RFC_1123_SUBDOMAIN_MAX_LENGTH: usize = 253; | ||||||
pub const LOWERCASE_RFC_1123_SUBDOMAIN_FMT: &str = concatcp!( | ||||||
LOWERCASE_RFC_1123_LABEL_FMT, | ||||||
"(\\.", | ||||||
LOWERCASE_RFC_1123_LABEL_FMT, | ||||||
")*" | ||||||
); | ||||||
const LOWERCASE_RFC_1123_SUBDOMAIN_ERROR_MSG: &str = "a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character"; | ||||||
|
||||||
pub const DOMAIN_MAX_LENGTH: usize = RFC_1123_SUBDOMAIN_MAX_LENGTH; | ||||||
|
||||||
/// Same as [`RFC_1123_LABEL_FMT`], but allows a trailing dot | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
const DOMAIN_FMT: &str = concatcp!(RFC_1123_LABEL_FMT, "(\\.", RFC_1123_LABEL_FMT, ")*\\.?"); | ||||||
const DOMAIN_ERROR_MSG: &str = "a domain must consist of alphanumeric characters, '-' or '.', and must start with an alphanumeric character and end with an alphanumeric character or '.'"; | ||||||
|
||||||
// FIXME: According to https://www.rfc-editor.org/rfc/rfc1035#section-2.3.1 domain names must start with a letter | ||||||
// (and not a number). | ||||||
const RFC_1035_LABEL_FMT: &str = "[a-z]([-a-z0-9]*[a-z0-9])?"; | ||||||
const RFC_1035_LABEL_ERROR_MSG: &str = "a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character"; | ||||||
// This is a modified RFC 1035 format according to the Kubernetes specification, see https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#rfc-1035-label-names | ||||||
pub const LOWERCASE_RFC_1035_LABEL_FMT: &str = "[a-z]([-a-z0-9]*[a-z0-9])?"; | ||||||
const LOWERCASE_RFC_1035_LABEL_ERROR_MSG: &str = "a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character"; | ||||||
|
||||||
// This is a label's max length in DNS (RFC 1035) | ||||||
const RFC_1035_LABEL_MAX_LENGTH: usize = 63; | ||||||
pub const RFC_1035_LABEL_MAX_LENGTH: usize = 63; | ||||||
|
||||||
// Technically Kerberos allows more realm names | ||||||
// (https://web.mit.edu/kerberos/krb5-1.21/doc/admin/realm_config.html#realm-name), | ||||||
|
@@ -54,12 +65,19 @@ pub(crate) static DOMAIN_REGEX: LazyLock<Regex> = LazyLock::new(|| { | |||||
Regex::new(&format!("^{DOMAIN_FMT}$")).expect("failed to compile domain regex") | ||||||
}); | ||||||
|
||||||
static RFC_1123_LABEL_REGEX: LazyLock<Regex> = LazyLock::new(|| { | ||||||
Regex::new(&format!("^{RFC_1123_LABEL_FMT}$")).expect("failed to compile RFC 1123 label regex") | ||||||
static LOWERCASE_RFC_1123_LABEL_REGEX: LazyLock<Regex> = LazyLock::new(|| { | ||||||
Regex::new(&format!("^{LOWERCASE_RFC_1123_LABEL_FMT}$")) | ||||||
.expect("failed to compile RFC 1123 label regex") | ||||||
}); | ||||||
|
||||||
static LOWERCASE_RFC_1123_SUBDOMAIN_REGEX: LazyLock<Regex> = LazyLock::new(|| { | ||||||
Regex::new(&format!("^{LOWERCASE_RFC_1123_SUBDOMAIN_FMT}$")) | ||||||
.expect("failed to compile RFC 1123 subdomain regex") | ||||||
}); | ||||||
|
||||||
static RFC_1035_LABEL_REGEX: LazyLock<Regex> = LazyLock::new(|| { | ||||||
Regex::new(&format!("^{RFC_1035_LABEL_FMT}$")).expect("failed to compile RFC 1035 label regex") | ||||||
static LOWERCASE_RFC_1035_LABEL_REGEX: LazyLock<Regex> = LazyLock::new(|| { | ||||||
Regex::new(&format!("^{LOWERCASE_RFC_1035_LABEL_FMT}$")) | ||||||
.expect("failed to compile RFC 1035 label regex") | ||||||
}); | ||||||
|
||||||
pub(crate) static KERBEROS_REALM_NAME_REGEX: LazyLock<Regex> = LazyLock::new(|| { | ||||||
|
@@ -198,28 +216,44 @@ pub fn is_domain(value: &str) -> Result { | |||||
]) | ||||||
} | ||||||
|
||||||
/// Tests for a string that conforms to the definition of a label in DNS (RFC 1123). | ||||||
/// Tests for a string that conforms to the kubernetes-specific definition of a label in DNS (RFC 1123) | ||||||
/// used in Namespace names, see: [Kubernetes Docs](https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names) | ||||||
/// Maximum label length supported by k8s is 63 characters (minimum required). | ||||||
pub fn is_rfc_1123_label(value: &str) -> Result { | ||||||
pub fn is_lowercase_rfc_1123_label(value: &str) -> Result { | ||||||
validate_all([ | ||||||
validate_str_length(value, RFC_1123_LABEL_MAX_LENGTH), | ||||||
validate_str_regex( | ||||||
value, | ||||||
&RFC_1123_LABEL_REGEX, | ||||||
RFC_1123_LABEL_ERROR_MSG, | ||||||
&LOWERCASE_RFC_1123_LABEL_REGEX, | ||||||
LOWERCASE_RFC_1123_LABEL_ERROR_MSG, | ||||||
&["example-label", "1-label-1"], | ||||||
), | ||||||
]) | ||||||
} | ||||||
|
||||||
/// Tests for a string that conforms to the definition of a label in DNS (RFC 1035). | ||||||
pub fn is_rfc_1035_label(value: &str) -> Result { | ||||||
/// Tests for a string that conforms to the kubernetes-specific definition of a subdomain in DNS (RFC 1123) | ||||||
/// used in ConfigMap names, see [Kubernetes Docs](https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names) | ||||||
pub fn is_lowercase_rfc_1123_subdomain(value: &str) -> Result { | ||||||
validate_all([ | ||||||
validate_str_length(value, RFC_1123_SUBDOMAIN_MAX_LENGTH), | ||||||
validate_str_regex( | ||||||
value, | ||||||
&LOWERCASE_RFC_1123_SUBDOMAIN_REGEX, | ||||||
LOWERCASE_RFC_1123_SUBDOMAIN_ERROR_MSG, | ||||||
&["example.com"], | ||||||
), | ||||||
]) | ||||||
} | ||||||
|
||||||
/// Tests for a string that conforms to the kubernetes-specific definition of a label in DNS (RFC 1035) | ||||||
/// used in Service names, see: [Kubernetes Docs](https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#rfc-1035-label-names) | ||||||
pub fn is_lowercase_rfc_1035_label(value: &str) -> Result { | ||||||
validate_all([ | ||||||
validate_str_length(value, RFC_1035_LABEL_MAX_LENGTH), | ||||||
validate_str_regex( | ||||||
value, | ||||||
&RFC_1035_LABEL_REGEX, | ||||||
RFC_1035_LABEL_ERROR_MSG, | ||||||
&LOWERCASE_RFC_1035_LABEL_REGEX, | ||||||
LOWERCASE_RFC_1035_LABEL_ERROR_MSG, | ||||||
&["my-name", "abc-123"], | ||||||
), | ||||||
]) | ||||||
|
@@ -261,7 +295,7 @@ pub fn name_is_dns_label(name: &str, prefix: bool) -> Result { | |||||
name = mask_trailing_dash(name); | ||||||
} | ||||||
|
||||||
is_rfc_1035_label(&name) | ||||||
is_lowercase_rfc_1035_label(&name) | ||||||
} | ||||||
|
||||||
/// Validates a namespace name. | ||||||
|
@@ -277,28 +311,14 @@ mod tests { | |||||
|
||||||
use super::*; | ||||||
|
||||||
const RFC_1123_SUBDOMAIN_ERROR_MSG: &str = "a RFC 1123 subdomain must consist of alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character"; | ||||||
|
||||||
static RFC_1123_SUBDOMAIN_REGEX: LazyLock<Regex> = LazyLock::new(|| { | ||||||
Regex::new(&format!("^{RFC_1123_SUBDOMAIN_FMT}$")) | ||||||
.expect("failed to compile RFC 1123 subdomain regex") | ||||||
}); | ||||||
|
||||||
/// Tests for a string that conforms to the definition of a subdomain in DNS (RFC 1123). | ||||||
fn is_rfc_1123_subdomain(value: &str) -> Result { | ||||||
validate_all([ | ||||||
validate_str_length(value, RFC_1123_SUBDOMAIN_MAX_LENGTH), | ||||||
validate_str_regex( | ||||||
value, | ||||||
&RFC_1123_SUBDOMAIN_REGEX, | ||||||
RFC_1123_SUBDOMAIN_ERROR_MSG, | ||||||
&["example.com"], | ||||||
), | ||||||
]) | ||||||
} | ||||||
|
||||||
#[rstest] | ||||||
#[case("")] | ||||||
#[case("A")] | ||||||
#[case("aBc")] | ||||||
#[case("ABC")] | ||||||
#[case("A1")] | ||||||
#[case("A-1")] | ||||||
#[case("1-A")] | ||||||
#[case("-")] | ||||||
#[case("a-")] | ||||||
#[case("-a")] | ||||||
|
@@ -325,6 +345,24 @@ mod tests { | |||||
#[case("1 ")] | ||||||
#[case(" 1")] | ||||||
#[case("1 2")] | ||||||
#[case("A.a")] | ||||||
#[case("aB.a")] | ||||||
#[case("ab.A")] | ||||||
#[case("A1.a")] | ||||||
#[case("a1.A")] | ||||||
#[case("A.1")] | ||||||
#[case("aB.1")] | ||||||
#[case("A1.1")] | ||||||
#[case("0.A")] | ||||||
#[case("01.A")] | ||||||
#[case("012.A")] | ||||||
#[case("1A.a")] | ||||||
#[case("1a.A")] | ||||||
#[case("1A.1")] | ||||||
#[case("a.B.c.d.e")] | ||||||
#[case("A.B.C.D.E")] | ||||||
#[case("aa.bB.cc.dd.ee")] | ||||||
#[case("AA.BB.CC.DD.EE")] | ||||||
#[case("a@b")] | ||||||
#[case("a,b")] | ||||||
#[case("a_b")] | ||||||
|
@@ -335,77 +373,53 @@ mod tests { | |||||
#[case("a$b")] | ||||||
#[case(&"a".repeat(254))] | ||||||
fn is_rfc_1123_subdomain_fail(#[case] value: &str) { | ||||||
assert!(is_rfc_1123_subdomain(value).is_err()); | ||||||
assert!(is_lowercase_rfc_1123_subdomain(value).is_err()); | ||||||
} | ||||||
|
||||||
#[rstest] | ||||||
#[case("a")] | ||||||
#[case("A")] | ||||||
#[case("ab")] | ||||||
#[case("abc")] | ||||||
#[case("aBc")] | ||||||
#[case("ABC")] | ||||||
#[case("a1")] | ||||||
#[case("A1")] | ||||||
#[case("a-1")] | ||||||
#[case("A-1")] | ||||||
#[case("a--1--2--b")] | ||||||
#[case("0")] | ||||||
#[case("01")] | ||||||
#[case("012")] | ||||||
#[case("1a")] | ||||||
#[case("1-a")] | ||||||
#[case("1-A")] | ||||||
#[case("1--a--b--2")] | ||||||
#[case("a.a")] | ||||||
#[case("A.a")] | ||||||
#[case("ab.a")] | ||||||
#[case("aB.a")] | ||||||
#[case("ab.A")] | ||||||
#[case("abc.a")] | ||||||
#[case("a1.a")] | ||||||
#[case("A1.a")] | ||||||
#[case("a1.A")] | ||||||
#[case("a-1.a")] | ||||||
#[case("a--1--2--b.a")] | ||||||
#[case("a.1")] | ||||||
#[case("A.1")] | ||||||
#[case("ab.1")] | ||||||
#[case("aB.1")] | ||||||
#[case("abc.1")] | ||||||
#[case("a1.1")] | ||||||
#[case("A1.1")] | ||||||
#[case("a-1.1")] | ||||||
#[case("a--1--2--b.1")] | ||||||
#[case("0.a")] | ||||||
#[case("0.A")] | ||||||
#[case("01.a")] | ||||||
#[case("01.A")] | ||||||
#[case("012.a")] | ||||||
#[case("012.A")] | ||||||
#[case("1a.a")] | ||||||
#[case("1A.a")] | ||||||
#[case("1a.A")] | ||||||
#[case("1-a.a")] | ||||||
#[case("1--a--b--2")] | ||||||
#[case("0.1")] | ||||||
#[case("01.1")] | ||||||
#[case("012.1")] | ||||||
#[case("1a.1")] | ||||||
#[case("1A.1")] | ||||||
#[case("1-a.1")] | ||||||
#[case("1--a--b--2.1")] | ||||||
#[case("a.b.c.d.e")] | ||||||
#[case("a.B.c.d.e")] | ||||||
#[case("A.B.C.D.E")] | ||||||
#[case("aa.bb.cc.dd.ee")] | ||||||
#[case("aa.bB.cc.dd.ee")] | ||||||
#[case("AA.BB.CC.DD.EE")] | ||||||
#[case("1.2.3.4.5")] | ||||||
#[case("11.22.33.44.55")] | ||||||
#[case(&"a".repeat(253))] | ||||||
fn is_rfc_1123_subdomain_pass(#[case] value: &str) { | ||||||
assert!(is_rfc_1123_subdomain(value).is_ok()); | ||||||
assert!(is_lowercase_rfc_1123_subdomain(value).is_ok()); | ||||||
// Every valid RFC1123 is also a valid domain | ||||||
assert!(is_domain(value).is_ok()); | ||||||
} | ||||||
|
@@ -469,7 +483,7 @@ mod tests { | |||||
#[case("1 2")] | ||||||
#[case(&"a".repeat(64))] | ||||||
fn is_rfc_1035_label_fail(#[case] value: &str) { | ||||||
assert!(is_rfc_1035_label(value).is_err()); | ||||||
assert!(is_lowercase_rfc_1035_label(value).is_err()); | ||||||
} | ||||||
|
||||||
#[rstest] | ||||||
|
@@ -481,6 +495,6 @@ mod tests { | |||||
#[case("a--1--2--b")] | ||||||
#[case(&"a".repeat(63))] | ||||||
fn is_rfc_1035_label_pass(#[case] value: &str) { | ||||||
assert!(is_rfc_1035_label(value).is_ok()); | ||||||
assert!(is_lowercase_rfc_1035_label(value).is_ok()); | ||||||
} | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.