Skip to content

Commit 99bb124

Browse files
Remove redundant enum SecurityConfigFileType
1 parent 7a1fbda commit 99bb124

6 files changed

Lines changed: 319 additions & 337 deletions

File tree

rust/operator-binary/src/controller.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,14 @@ pub enum ValidatedSecurity {
187187
/// At least one security setting is managed by the operator
188188
ManagedByOperator {
189189
managing_role_group: RoleGroupName,
190-
settings: v1alpha1::SecurityConfig,
190+
settings: v1alpha1::SecuritySettings,
191191
tls_server_secret_class: SecretClassName,
192192
tls_internal_secret_class: SecretClassName,
193193
},
194194

195195
/// All security settings are managed by the API
196196
ManagedByApi {
197-
settings: v1alpha1::SecurityConfig,
197+
settings: v1alpha1::SecuritySettings,
198198
tls_server_secret_class: Option<SecretClassName>,
199199
tls_internal_secret_class: SecretClassName,
200200
},
@@ -594,7 +594,7 @@ mod tests {
594594
]
595595
.into(),
596596
Some(ValidatedSecurity::ManagedByApi {
597-
settings: v1alpha1::SecurityConfig::default(),
597+
settings: v1alpha1::SecuritySettings::default(),
598598
tls_server_secret_class: None,
599599
tls_internal_secret_class: SecretClassName::from_str_unsafe("tls"),
600600
}),

rust/operator-binary/src/controller/build/node_config.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -623,19 +623,19 @@ mod tests {
623623
product_specific_common_config: GenericProductSpecificCommonConfig::default(),
624624
};
625625

626-
let security_settings = v1alpha1::SecurityConfig {
627-
config: v1alpha1::SecurityConfigFileType {
628-
managed_by: v1alpha1::SecurityConfigFileTypeManagedBy::Operator,
629-
content: v1alpha1::SecurityConfigFileTypeContent::ValueFrom(
630-
v1alpha1::SecurityConfigFileTypeContentValueFrom::ConfigMapKeyRef(
626+
let security_settings = v1alpha1::SecuritySettings {
627+
config: v1alpha1::SecuritySettingsFileType {
628+
managed_by: v1alpha1::SecuritySettingsFileTypeManagedBy::Operator,
629+
content: v1alpha1::SecuritySettingsFileTypeContent::ValueFrom(
630+
v1alpha1::SecuritySettingsFileTypeContentValueFrom::ConfigMapKeyRef(
631631
v1alpha1::ConfigMapKeyRef {
632632
name: ConfigMapName::from_str_unsafe("security-config"),
633633
key: ConfigMapKey::from_str_unsafe("config.yml"),
634634
},
635635
),
636636
),
637637
},
638-
..v1alpha1::SecurityConfig::default()
638+
..v1alpha1::SecuritySettings::default()
639639
};
640640
let tls_server_secret_class = SecretClassName::from_str_unsafe("tls");
641641
let tls_internal_secret_class = SecretClassName::from_str_unsafe("tls");

rust/operator-binary/src/controller/build/role_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ mod tests {
428428
)]
429429
.into(),
430430
Some(ValidatedSecurity::ManagedByApi {
431-
settings: v1alpha1::SecurityConfig::default(),
431+
settings: v1alpha1::SecuritySettings::default(),
432432
tls_server_secret_class: Some(SecretClassName::from_str_unsafe("tls")),
433433
tls_internal_secret_class: SecretClassName::from_str_unsafe("tls"),
434434
}),

rust/operator-binary/src/controller/build/role_group_builder.rs

Lines changed: 84 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use stackable_operator::{
3535
},
3636
utils::COMMON_BASH_TRAP_FUNCTIONS,
3737
};
38-
use strum::IntoEnumIterator;
3938

4039
use super::{
4140
node_config::{CONFIGURATION_FILE_OPENSEARCH_YML, NodeConfig},
@@ -52,7 +51,7 @@ use crate::{
5251
MAX_OPENSEARCH_SERVER_LOG_FILES_SIZE, vector_config_file_extra_env_vars,
5352
},
5453
},
55-
crd::{SecurityConfigFileType, v1alpha1},
54+
crd::{ExtendedSecuritySettingsFileType, v1alpha1},
5655
framework::{
5756
builder::{
5857
meta::ownerreference_from_resource,
@@ -111,7 +110,7 @@ pub enum RoleGroupSecurityMode {
111110
/// The security plugin is enabled and all settings are initialized by an arbitrary role group.
112111
/// The security settings are mounted to the main container for all role groups.
113112
Initializing {
114-
settings: v1alpha1::SecurityConfig,
113+
settings: v1alpha1::SecuritySettings,
115114
tls_server_secret_class: Option<SecretClassName>,
116115
tls_internal_secret_class: SecretClassName,
117116
},
@@ -122,7 +121,7 @@ pub enum RoleGroupSecurityMode {
122121
/// init container and the security settings are mounted to and updated in the
123122
/// [`v1alpha1::Container::UpdateSecurityConfig`] side-car container.
124123
Managing {
125-
settings: v1alpha1::SecurityConfig,
124+
settings: v1alpha1::SecuritySettings,
126125
tls_server_secret_class: SecretClassName,
127126
tls_internal_secret_class: SecretClassName,
128127
},
@@ -298,9 +297,12 @@ impl<'a> RoleGroupBuilder<'a> {
298297
if let RoleGroupSecurityMode::Initializing { settings, .. }
299298
| RoleGroupSecurityMode::Managing { settings, .. } = &self.security_mode
300299
{
301-
for file_type in SecurityConfigFileType::iter() {
302-
if let Some(value) = settings.value(file_type) {
303-
data.insert(file_type.filename(), value.to_string());
300+
for file_type in settings {
301+
if let v1alpha1::SecuritySettingsFileTypeContent::Value(
302+
v1alpha1::SecuritySettingsFileTypeContentValue { value },
303+
) = &file_type.content
304+
{
305+
data.insert(file_type.filename.to_owned(), value.to_string());
304306
}
305307
}
306308
}
@@ -722,8 +724,8 @@ impl<'a> RoleGroupBuilder<'a> {
722724
});
723725
};
724726

725-
if let RoleGroupSecurityMode::Initializing { .. } = self.security_mode {
726-
volume_mounts.extend(self.security_config_volume_mounts());
727+
if let RoleGroupSecurityMode::Initializing { settings, .. } = &self.security_mode {
728+
volume_mounts.extend(self.security_config_volume_mounts(settings));
727729
};
728730

729731
if !self.cluster.keystores.is_empty() {
@@ -786,28 +788,37 @@ impl<'a> RoleGroupBuilder<'a> {
786788

787789
/// Builds the security settings volume mounts for the [`v1alpha1::Container::OpenSearch`]
788790
/// container or the [`v1alpha1::Container::UpdateSecurityConfig`] container
789-
fn security_config_volume_mounts(&self) -> Vec<VolumeMount> {
791+
fn security_config_volume_mounts(
792+
&self,
793+
settings: &v1alpha1::SecuritySettings,
794+
) -> Vec<VolumeMount> {
790795
let mut volume_mounts = vec![];
791796

792797
let opensearch_path_conf = self.node_config.opensearch_path_conf();
793798

794-
for file_type in SecurityConfigFileType::iter() {
795-
let volume_name = format!("security-config-file-{}", file_type.volume_name());
799+
for file_type in settings {
796800
volume_mounts.push(VolumeMount {
797801
mount_path: format!(
798802
"{opensearch_path_conf}/opensearch-security/{filename}",
799-
filename = file_type.filename()
803+
filename = file_type.filename.to_owned()
800804
),
801-
name: volume_name,
805+
name: Self::security_settings_file_type_volume_name(&file_type).to_string(),
802806
read_only: Some(true),
803-
sub_path: Some(file_type.filename()),
807+
sub_path: Some(file_type.filename.to_owned()),
804808
..VolumeMount::default()
805809
});
806810
}
807811

808812
volume_mounts
809813
}
810814

815+
fn security_settings_file_type_volume_name(
816+
file_type: &ExtendedSecuritySettingsFileType,
817+
) -> VolumeName {
818+
VolumeName::from_str(&format!("security-config-file-{}", file_type.id))
819+
.expect("should be a valid VolumeName")
820+
}
821+
811822
/// Builds the [`v1alpha1::Container::Vector`] container for the [`PodTemplateSpec`] if it is
812823
/// enabled
813824
fn build_maybe_vector_container(&self) -> Option<Container> {
@@ -866,7 +877,7 @@ impl<'a> RoleGroupBuilder<'a> {
866877
..VolumeMount::default()
867878
},
868879
];
869-
volume_mounts.extend(self.security_config_volume_mounts());
880+
volume_mounts.extend(self.security_config_volume_mounts(settings));
870881

871882
let mut env_vars = EnvVarSet::new()
872883
.with_value(
@@ -878,15 +889,12 @@ impl<'a> RoleGroupBuilder<'a> {
878889
FieldPathEnvVar::Name,
879890
);
880891

881-
for file_type in SecurityConfigFileType::iter() {
882-
let managed_by_operator = settings.security_config(file_type).managed_by
883-
== v1alpha1::SecurityConfigFileTypeManagedBy::Operator;
892+
for file_type in settings {
893+
let managed_by_operator =
894+
*file_type.managed_by == v1alpha1::SecuritySettingsFileTypeManagedBy::Operator;
884895

885896
env_vars = env_vars.with_value(
886-
&EnvVarName::from_str_unsafe(&format!(
887-
"MANAGE_{}",
888-
file_type.volume_name().to_uppercase()
889-
)),
897+
&EnvVarName::from_str_unsafe(&format!("MANAGE_{}", file_type.id.to_uppercase())),
890898
managed_by_operator.to_string(),
891899
);
892900
}
@@ -1091,61 +1099,66 @@ impl<'a> RoleGroupBuilder<'a> {
10911099

10921100
/// Builds the security settings volumes for the [`PodTemplateSpec`]
10931101
/// It is not checked if these volumes are required in this role group.
1094-
fn build_security_settings_volumes(&self, settings: &v1alpha1::SecurityConfig) -> Vec<Volume> {
1102+
fn build_security_settings_volumes(
1103+
&self,
1104+
settings: &v1alpha1::SecuritySettings,
1105+
) -> Vec<Volume> {
10951106
let mut volumes = vec![];
10961107

1097-
for file_type in SecurityConfigFileType::iter() {
1098-
let volume_name = format!("security-config-file-{}", file_type.volume_name());
1099-
if settings.value(file_type).is_some() {
1100-
let volume = Volume {
1108+
for file_type in settings {
1109+
let volume_name = Self::security_settings_file_type_volume_name(&file_type).to_string();
1110+
1111+
let volume = match &file_type.content {
1112+
v1alpha1::SecuritySettingsFileTypeContent::Value(_) => Volume {
11011113
name: volume_name,
11021114
config_map: Some(ConfigMapVolumeSource {
11031115
items: Some(vec![KeyToPath {
1104-
key: file_type.filename(),
1116+
key: file_type.filename.to_owned(),
11051117
mode: Some(0o660),
1106-
path: file_type.filename(),
1118+
path: file_type.filename.to_owned(),
11071119
}]),
11081120
name: self.resource_names.role_group_config_map().to_string(),
11091121
..Default::default()
11101122
}),
11111123
..Volume::default()
1112-
};
1113-
volumes.push(volume);
1114-
} else if let Some(v1alpha1::ConfigMapKeyRef { name, key }) =
1115-
settings.config_map_key_ref(file_type)
1116-
{
1117-
let volume = Volume {
1124+
},
1125+
v1alpha1::SecuritySettingsFileTypeContent::ValueFrom(
1126+
v1alpha1::SecuritySettingsFileTypeContentValueFrom::ConfigMapKeyRef(
1127+
v1alpha1::ConfigMapKeyRef { name, key },
1128+
),
1129+
) => Volume {
11181130
name: volume_name,
11191131
config_map: Some(ConfigMapVolumeSource {
11201132
items: Some(vec![KeyToPath {
11211133
key: key.to_string(),
11221134
mode: Some(0o660),
1123-
path: file_type.filename(),
1135+
path: file_type.filename.to_owned(),
11241136
}]),
11251137
name: name.to_string(),
11261138
..ConfigMapVolumeSource::default()
11271139
}),
11281140
..Volume::default()
1129-
};
1130-
volumes.push(volume);
1131-
} else if let Some(v1alpha1::SecretKeyRef { name, key }) =
1132-
settings.secret_key_ref(file_type)
1133-
{
1134-
let volume = Volume {
1141+
},
1142+
v1alpha1::SecuritySettingsFileTypeContent::ValueFrom(
1143+
v1alpha1::SecuritySettingsFileTypeContentValueFrom::SecretKeyRef(
1144+
v1alpha1::SecretKeyRef { name, key },
1145+
),
1146+
) => Volume {
11351147
name: volume_name,
11361148
secret: Some(SecretVolumeSource {
11371149
items: Some(vec![KeyToPath {
11381150
key: key.to_string(),
11391151
mode: Some(0o660),
1140-
path: file_type.filename(),
1152+
path: file_type.filename.to_owned(),
11411153
}]),
11421154
secret_name: Some(name.to_string()),
11431155
..SecretVolumeSource::default()
11441156
}),
11451157
..Volume::default()
1146-
};
1147-
volumes.push(volume);
1148-
}
1158+
},
1159+
};
1160+
1161+
volumes.push(volume);
11491162
}
11501163

11511164
volumes
@@ -1406,6 +1419,16 @@ mod tests {
14061419
let _ = OPENSEARCH_KEYSTORE_VOLUME_NAME;
14071420
}
14081421

1422+
#[test]
1423+
fn test_security_settings_file_type_volume_name() {
1424+
let security_settings = v1alpha1::SecuritySettings::default();
1425+
1426+
for file_type in &security_settings {
1427+
// Test that the function does not panic
1428+
let _ = RoleGroupBuilder::security_settings_file_type_volume_name(&file_type);
1429+
}
1430+
}
1431+
14091432
fn context_names() -> ContextNames {
14101433
ContextNames {
14111434
product_name: ProductName::from_str_unsafe("opensearch"),
@@ -1471,11 +1494,11 @@ mod tests {
14711494
product_specific_common_config: GenericProductSpecificCommonConfig::default(),
14721495
};
14731496

1474-
let security_settings = v1alpha1::SecurityConfig {
1475-
config: v1alpha1::SecurityConfigFileType {
1476-
managed_by: v1alpha1::SecurityConfigFileTypeManagedBy::Operator,
1477-
content: v1alpha1::SecurityConfigFileTypeContent::Value(
1478-
v1alpha1::SecurityConfigFileTypeContentValue {
1497+
let security_settings = v1alpha1::SecuritySettings {
1498+
config: v1alpha1::SecuritySettingsFileType {
1499+
managed_by: v1alpha1::SecuritySettingsFileTypeManagedBy::Operator,
1500+
content: v1alpha1::SecuritySettingsFileTypeContent::Value(
1501+
v1alpha1::SecuritySettingsFileTypeContentValue {
14791502
value: json!({
14801503
"_meta": {
14811504
"type": "config",
@@ -1492,29 +1515,29 @@ mod tests {
14921515
},
14931516
),
14941517
},
1495-
internal_users: v1alpha1::SecurityConfigFileType {
1496-
managed_by: v1alpha1::SecurityConfigFileTypeManagedBy::Api,
1497-
content: v1alpha1::SecurityConfigFileTypeContent::ValueFrom(
1498-
v1alpha1::SecurityConfigFileTypeContentValueFrom::SecretKeyRef(
1518+
internal_users: v1alpha1::SecuritySettingsFileType {
1519+
managed_by: v1alpha1::SecuritySettingsFileTypeManagedBy::Api,
1520+
content: v1alpha1::SecuritySettingsFileTypeContent::ValueFrom(
1521+
v1alpha1::SecuritySettingsFileTypeContentValueFrom::SecretKeyRef(
14991522
v1alpha1::SecretKeyRef {
15001523
name: SecretName::from_str_unsafe("opensearch-security-config"),
15011524
key: SecretKey::from_str_unsafe("internal_users.yml"),
15021525
},
15031526
),
15041527
),
15051528
},
1506-
roles: v1alpha1::SecurityConfigFileType {
1507-
managed_by: v1alpha1::SecurityConfigFileTypeManagedBy::Api,
1508-
content: v1alpha1::SecurityConfigFileTypeContent::ValueFrom(
1509-
v1alpha1::SecurityConfigFileTypeContentValueFrom::ConfigMapKeyRef(
1529+
roles: v1alpha1::SecuritySettingsFileType {
1530+
managed_by: v1alpha1::SecuritySettingsFileTypeManagedBy::Api,
1531+
content: v1alpha1::SecuritySettingsFileTypeContent::ValueFrom(
1532+
v1alpha1::SecuritySettingsFileTypeContentValueFrom::ConfigMapKeyRef(
15101533
v1alpha1::ConfigMapKeyRef {
15111534
name: ConfigMapName::from_str_unsafe("opensearch-security-config"),
15121535
key: ConfigMapKey::from_str_unsafe("roles.yml"),
15131536
},
15141537
),
15151538
),
15161539
},
1517-
..v1alpha1::SecurityConfig::default()
1540+
..v1alpha1::SecuritySettings::default()
15181541
};
15191542

15201543
let security = match security_mode {

0 commit comments

Comments
 (0)