Skip to content

Commit 44830bb

Browse files
fix: Require an RFC 1035 label name for Services
1 parent b8c5686 commit 44830bb

1 file changed

Lines changed: 41 additions & 7 deletions

File tree

rust/operator-binary/src/framework.rs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ pub enum Error {
4747
source: stackable_operator::kvp::LabelValueError,
4848
},
4949

50+
#[snafu(display("not a valid label name as defined in RFC 1035"))]
51+
InvalidRfc1035LabelName {
52+
source: stackable_operator::validation::Errors,
53+
},
54+
5055
#[snafu(display("not a valid DNS subdomain name as defined in RFC 1123"))]
5156
InvalidRfc1123DnsSubdomainName {
5257
source: stackable_operator::validation::Errors,
@@ -72,6 +77,11 @@ pub const MAX_RFC_1123_DNS_SUBDOMAIN_NAME_LENGTH: usize = 253;
7277
/// Duplicates the private constant [`stackable-operator::validation::RFC_1123_LABEL_MAX_LENGTH`]
7378
pub const MAX_RFC_1123_LABEL_NAME_LENGTH: usize = 63;
7479

80+
/// Maximum length of label names as defined in RFC 1035.
81+
///
82+
/// Duplicates the private constant [`stackable-operator::validation::RFC_1035_LABEL_MAX_LENGTH`]
83+
pub const MAX_RFC_1035_LABEL_NAME_LENGTH: usize = 63;
84+
7585
/// Maximum length of label values
7686
///
7787
/// Duplicates the private constant [`stackable-operator::kvp::label::value::LABEL_VALUE_MAX_LEN`]
@@ -174,6 +184,9 @@ macro_rules! attributed_string_type {
174184
(@from_str $name:ident, $s:expr, is_rfc_1123_label_name) => {
175185
stackable_operator::validation::is_rfc_1123_label($s).context(InvalidRfc1123LabelNameSnafu)?;
176186
};
187+
(@from_str $name:ident, $s:expr, is_rfc_1035_label_name) => {
188+
stackable_operator::validation::is_rfc_1035_label($s).context(InvalidRfc1035LabelNameSnafu)?;
189+
};
177190
(@from_str $name:ident, $s:expr, is_valid_label_value) => {
178191
LabelValue::from_str($s).context(InvalidLabelValueSnafu)?;
179192
};
@@ -190,6 +203,8 @@ macro_rules! attributed_string_type {
190203
};
191204
(@trait_impl $name:ident, is_rfc_1123_label_name) => {
192205
};
206+
(@trait_impl $name:ident, is_rfc_1035_label_name) => {
207+
};
193208
(@trait_impl $name:ident, is_uid) => {
194209
impl From<Uuid> for $name {
195210
fn from(value: Uuid) -> Self {
@@ -298,8 +313,8 @@ attributed_string_type! {
298313
ServiceName,
299314
"The name of a Service",
300315
"opensearch-nodes-default-headless",
301-
(max_length = min(MAX_RFC_1123_LABEL_NAME_LENGTH, MAX_LABEL_VALUE_LENGTH)),
302-
is_rfc_1123_label_name,
316+
(max_length = min(MAX_RFC_1035_LABEL_NAME_LENGTH, MAX_LABEL_VALUE_LENGTH)),
317+
is_rfc_1035_label_name,
303318
is_valid_label_value
304319
}
305320
attributed_string_type! {
@@ -398,10 +413,11 @@ mod tests {
398413

399414
use super::{
400415
ClusterName, ClusterRoleName, ConfigMapName, ControllerName, EmptyStringSnafu, Error,
401-
ErrorDiscriminants, InvalidLabelValueSnafu, InvalidRfc1123DnsSubdomainNameSnafu,
402-
InvalidRfc1123LabelNameSnafu, InvalidUidSnafu, LabelValue, LengthExceededSnafu,
403-
NamespaceName, OperatorName, PersistentVolumeClaimName, ProductVersion, RoleBindingName,
404-
RoleGroupName, RoleName, ServiceAccountName, ServiceName, StatefulSetName, Uid, VolumeName,
416+
ErrorDiscriminants, InvalidLabelValueSnafu, InvalidRfc1035LabelNameSnafu,
417+
InvalidRfc1123DnsSubdomainNameSnafu, InvalidRfc1123LabelNameSnafu, InvalidUidSnafu,
418+
LabelValue, LengthExceededSnafu, NamespaceName, OperatorName, PersistentVolumeClaimName,
419+
ProductVersion, RoleBindingName, RoleGroupName, RoleName, ServiceAccountName, ServiceName,
420+
StatefulSetName, Uid, VolumeName,
405421
};
406422
use crate::framework::{NameIsValidLabelValue, ProductName};
407423

@@ -478,6 +494,24 @@ mod tests {
478494
);
479495
}
480496

497+
attributed_string_type! {
498+
IsRfc1035LabelNameTest,
499+
"is_rfc_1035_label_name test",
500+
"a-b",
501+
is_rfc_1035_label_name
502+
}
503+
504+
#[test]
505+
fn test_attributed_string_type_is_rfc_1035_label_name() {
506+
type T = IsRfc1035LabelNameTest;
507+
508+
T::test_example();
509+
assert_eq!(
510+
Err(ErrorDiscriminants::InvalidRfc1035LabelName),
511+
T::from_str("A").map_err(ErrorDiscriminants::from)
512+
);
513+
}
514+
481515
attributed_string_type! {
482516
IsRfc1123DnsSubdomainNameTest,
483517
"is_rfc_1123_dns_subdomain_name test",
@@ -499,7 +533,7 @@ mod tests {
499533
attributed_string_type! {
500534
IsRfc1123LabelNameTest,
501535
"is_rfc_1123_label_name test",
502-
"a-b",
536+
"1-a",
503537
is_rfc_1123_label_name
504538
}
505539

0 commit comments

Comments
 (0)