Skip to content

Commit 122eec4

Browse files
feat: Replace is_config_map_key in attributed_string_type with regex
1 parent bb83878 commit 122eec4

2 files changed

Lines changed: 17 additions & 148 deletions

File tree

rust/operator-binary/src/framework.rs

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ pub mod kvp;
3232
pub mod product_logging;
3333
pub mod role_group_utils;
3434
pub mod role_utils;
35-
pub mod validation;
3635

3736
#[derive(Debug, EnumDiscriminants, Snafu)]
3837
#[strum_discriminants(derive(IntoStaticStr))]
@@ -52,11 +51,6 @@ pub enum Error {
5251
#[snafu(display("regular expression not matched"))]
5352
RegexNotMatched { value: String, regex: &'static str },
5453

55-
#[snafu(display("not a valid ConfigMap key"))]
56-
InvalidConfigMapKey {
57-
source: crate::framework::validation::Error,
58-
},
59-
6054
#[snafu(display("not a valid label value"))]
6155
InvalidLabelValue {
6256
source: stackable_operator::kvp::LabelValueError,
@@ -237,7 +231,7 @@ macro_rules! attributed_string_type {
237231
None
238232
},
239233
"pattern": match $name::REGEX {
240-
$crate::framework::Regex::Expression(regex) => Some(regex),
234+
$crate::framework::Regex::Expression(regex) => Some(std::format!("^{regex}$")),
241235
_ => None
242236
}
243237
})
@@ -292,9 +286,6 @@ macro_rules! attributed_string_type {
292286
}
293287
);
294288
};
295-
(@from_str $name:ident, $s:expr, is_config_map_key) => {
296-
$crate::framework::validation::is_config_map_key($s).context($crate::framework::InvalidConfigMapKeySnafu)?;
297-
};
298289
(@from_str $name:ident, $s:expr, is_rfc_1035_label_name) => {
299290
stackable_operator::validation::is_lowercase_rfc_1035_label($s).context($crate::framework::InvalidRfc1035LabelNameSnafu)?;
300291
};
@@ -331,12 +322,6 @@ macro_rules! attributed_string_type {
331322
// regex has no influence on the min_length.
332323
attributed_string_type!(@min_length $($attribute)*)
333324
};
334-
(@min_length is_config_map_key $($attribute:tt)*) => {
335-
$crate::framework::max(
336-
1,
337-
attributed_string_type!(@min_length $($attribute)*)
338-
)
339-
};
340325
(@min_length is_rfc_1035_label_name $($attribute:tt)*) => {
341326
$crate::framework::max(
342327
1,
@@ -388,12 +373,6 @@ macro_rules! attributed_string_type {
388373
// regex has no influence on the max_length.
389374
attributed_string_type!(@max_length $($attribute)*)
390375
};
391-
(@max_length is_config_map_key $($attribute:tt)*) => {
392-
$crate::framework::min(
393-
stackable_operator::validation::RFC_1123_SUBDOMAIN_MAX_LENGTH,
394-
attributed_string_type!(@max_length $($attribute)*)
395-
)
396-
};
397376
(@max_length is_rfc_1035_label_name $($attribute:tt)*) => {
398377
$crate::framework::min(
399378
stackable_operator::validation::RFC_1035_LABEL_MAX_LENGTH,
@@ -443,10 +422,6 @@ macro_rules! attributed_string_type {
443422
$crate::framework::Regex::Expression($regex)
444423
.combine(attributed_string_type!(@regex $($attribute)*))
445424
};
446-
(@regex is_config_map_key $($attribute:tt)*) => {
447-
$crate::framework::Regex::Expression($crate::framework::validation::CONFIG_MAP_KEY_FMT)
448-
.combine(attributed_string_type!(@regex $($attribute)*))
449-
};
450425
(@regex is_rfc_1035_label_name $($attribute:tt)*) => {
451426
$crate::framework::Regex::Expression(stackable_operator::validation::LOWERCASE_RFC_1035_LABEL_FMT)
452427
.combine(attributed_string_type!(@regex $($attribute)*))
@@ -461,11 +436,11 @@ macro_rules! attributed_string_type {
461436
};
462437
(@regex is_valid_label_value $($attribute:tt)*) => {
463438
// regular expression from stackable_operator::kvp::label::LABEL_VALUE_REGEX
464-
$crate::framework::Regex::Expression("^[a-z0-9A-Z]([a-z0-9A-Z-_.]*[a-z0-9A-Z]+)?$")
439+
$crate::framework::Regex::Expression("[a-z0-9A-Z]([a-z0-9A-Z-_.]*[a-z0-9A-Z]+)?")
465440
.combine(attributed_string_type!(@regex $($attribute)*))
466441
};
467442
(@regex is_uid $($attribute:tt)*) => {
468-
$crate::framework::Regex::Expression("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$")
443+
$crate::framework::Regex::Expression("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}")
469444
.combine(attributed_string_type!(@regex $($attribute)*))
470445
};
471446

@@ -477,8 +452,6 @@ macro_rules! attributed_string_type {
477452
};
478453
(@trait_impl $name:ident, (regex = $regex:expr)) => {
479454
};
480-
(@trait_impl $name:ident, is_config_map_key) => {
481-
};
482455
(@trait_impl $name:ident, is_rfc_1035_label_name) => {
483456
impl $name {
484457
pub const IS_RFC_1035_LABEL_NAME: bool = true;
@@ -581,11 +554,12 @@ attributed_string_type! {
581554
}
582555
attributed_string_type! {
583556
ConfigMapKey,
584-
"The key for a ConfigMap or Secret",
557+
"The key for a ConfigMap",
585558
"log4j2.properties",
559+
(min_length = 1),
586560
// see https://github.com/kubernetes/kubernetes/blob/v1.34.1/staging/src/k8s.io/apimachinery/pkg/util/validation/validation.go#L435-L451
587561
(max_length = RFC_1123_SUBDOMAIN_MAX_LENGTH),
588-
is_config_map_key
562+
(regex = "[-._a-zA-Z0-9]+")
589563
}
590564
attributed_string_type! {
591565
ContainerName,
@@ -638,6 +612,15 @@ attributed_string_type! {
638612
// problems on other Kubernetes providers, the length is restricted here.
639613
is_rfc_1123_dns_subdomain_name
640614
}
615+
attributed_string_type! {
616+
SecretKey,
617+
"The key for a Secret",
618+
"accessKey",
619+
(min_length = 1),
620+
// see https://github.com/kubernetes/kubernetes/blob/v1.34.1/staging/src/k8s.io/apimachinery/pkg/util/validation/validation.go#L435-L451
621+
(max_length = RFC_1123_SUBDOMAIN_MAX_LENGTH),
622+
(regex = "[-._a-zA-Z0-9]+")
623+
}
641624
attributed_string_type! {
642625
ServiceAccountName,
643626
"The name of a ServiceAccount",
@@ -840,7 +823,7 @@ mod tests {
840823
"test",
841824
(min_length = 4),
842825
(max_length = 8),
843-
(regex = "^[est]+$")
826+
(regex = "[est]+")
844827
}
845828

846829
#[test]
@@ -854,7 +837,7 @@ mod tests {
854837
"type": "string",
855838
"minLength": 4,
856839
"maxLength": 8,
857-
"pattern": "^[tes]+$",
840+
"pattern": "^[est]+$",
858841
}),
859842
JsonSchemaTest::json_schema(&mut SchemaGenerator::default())
860843
);
@@ -936,24 +919,6 @@ mod tests {
936919
);
937920
}
938921

939-
attributed_string_type! {
940-
IsConfigMapKeyTest,
941-
"is_config_map_key test",
942-
"a_B-c.1",
943-
is_config_map_key
944-
}
945-
946-
#[test]
947-
fn test_attributed_string_type_is_config_map_key() {
948-
type T = IsConfigMapKeyTest;
949-
950-
T::test_example();
951-
assert_eq!(
952-
Err(ErrorDiscriminants::InvalidConfigMapKey),
953-
T::from_str(" ").map_err(ErrorDiscriminants::from)
954-
);
955-
}
956-
957922
attributed_string_type! {
958923
IsRfc1035LabelNameTest,
959924
"is_rfc_1035_label_name test",

rust/operator-binary/src/framework/validation.rs

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)