Skip to content

Commit 7c7bedf

Browse files
committed
enable tls
1 parent 5c9c73c commit 7c7bedf

File tree

7 files changed

+126
-50
lines changed

7 files changed

+126
-50
lines changed

rust/operator-binary/src/command.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
use stackable_operator::crd::s3;
22

3-
use crate::crd::{
4-
DB_PASSWORD_ENV, DB_PASSWORD_PLACEHOLDER, DB_USERNAME_ENV, DB_USERNAME_PLACEHOLDER,
5-
HIVE_METASTORE_LOG4J2_PROPERTIES, HIVE_SITE_XML, STACKABLE_CONFIG_DIR,
6-
STACKABLE_CONFIG_MOUNT_DIR, STACKABLE_LOG_CONFIG_MOUNT_DIR, STACKABLE_TRUST_STORE,
7-
STACKABLE_TRUST_STORE_PASSWORD, v1alpha1,
3+
use crate::{
4+
config::opa::HiveOpaConfig,
5+
crd::{
6+
DB_PASSWORD_ENV, DB_PASSWORD_PLACEHOLDER, DB_USERNAME_ENV, DB_USERNAME_PLACEHOLDER,
7+
HIVE_METASTORE_LOG4J2_PROPERTIES, HIVE_SITE_XML, STACKABLE_CONFIG_DIR,
8+
STACKABLE_CONFIG_MOUNT_DIR, STACKABLE_LOG_CONFIG_MOUNT_DIR, STACKABLE_TRUST_STORE,
9+
STACKABLE_TRUST_STORE_PASSWORD, v1alpha1,
10+
},
811
};
912

1013
pub fn build_container_command_args(
1114
hive: &v1alpha1::HiveCluster,
1215
start_command: String,
1316
s3_connection_spec: Option<&s3::v1alpha1::ConnectionSpec>,
17+
hive_opa_config: Option<&HiveOpaConfig>,
1418
) -> Vec<String> {
1519
let mut args = vec![
1620
// copy config files to a writeable empty folder in order to set s3 access and secret keys
@@ -51,6 +55,14 @@ pub fn build_container_command_args(
5155
}
5256
}
5357

58+
if let Some(opa) = hive_opa_config {
59+
if let Some(ca_cert_dir) = opa.tls_ca_cert_mount_path() {
60+
args.push(format!(
61+
"cert-tools generate-pkcs12-truststore --pkcs12 {STACKABLE_TRUST_STORE}:{STACKABLE_TRUST_STORE_PASSWORD} --pem {ca_cert_dir}/ca.crt --out {STACKABLE_TRUST_STORE} --out-password {STACKABLE_TRUST_STORE_PASSWORD}"
62+
));
63+
}
64+
}
65+
5466
// db credentials
5567
args.extend([
5668
format!("echo replacing {DB_USERNAME_PLACEHOLDER} and {DB_PASSWORD_PLACEHOLDER} with secret values."),

rust/operator-binary/src/config/opa.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl HiveOpaConfig {
121121
])
122122
}
123123

124-
pub fn tls_mount_path(&self) -> Option<String> {
124+
pub fn tls_ca_cert_mount_path(&self) -> Option<String> {
125125
self.tls_secret_class
126126
.as_ref()
127127
.map(|_| format!("/stackable/secrets/{OPA_TLS_VOLUME_NAME}"))

rust/operator-binary/src/controller.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use stackable_operator::{
2828
security::PodSecurityContextBuilder,
2929
volume::{
3030
ListenerOperatorVolumeSourceBuilder, ListenerOperatorVolumeSourceBuilderError,
31-
ListenerReference, VolumeBuilder,
31+
ListenerReference, SecretOperatorVolumeSourceBuilder, VolumeBuilder,
3232
},
3333
},
3434
},
@@ -86,7 +86,7 @@ use crate::{
8686
command::build_container_command_args,
8787
config::{
8888
jvm::{construct_hadoop_heapsize_env, construct_non_heap_jvm_args},
89-
opa::HiveOpaConfig,
89+
opa::{HiveOpaConfig, OPA_TLS_VOLUME_NAME},
9090
},
9191
crd::{
9292
APP_NAME, CORE_SITE_XML, Container, DB_PASSWORD_ENV, DB_USERNAME_ENV, HIVE_PORT,
@@ -323,6 +323,11 @@ pub enum Error {
323323
InvalidOpaConfig {
324324
source: stackable_operator::commons::opa::Error,
325325
},
326+
327+
#[snafu(display("failed to build TLS certificate SecretClass Volume"))]
328+
TlsCertSecretClassVolumeBuild {
329+
source: stackable_operator::builder::pod::volume::SecretOperatorVolumeSourceBuilderError,
330+
},
326331
}
327332
type Result<T, E = Error> = std::result::Result<T, E>;
328333

@@ -472,6 +477,7 @@ pub async fn reconcile_hive(
472477
s3_connection_spec.as_ref(),
473478
&config,
474479
&rbac_sa.name_any(),
480+
hive_opa_config.as_ref(),
475481
)?;
476482

477483
cluster_resources
@@ -742,6 +748,7 @@ fn build_metastore_rolegroup_statefulset(
742748
s3_connection: Option<&s3::v1alpha1::ConnectionSpec>,
743749
merged_config: &MetaStoreConfig,
744750
sa_name: &str,
751+
hive_opa_config: Option<&HiveOpaConfig>,
745752
) -> Result<StatefulSet> {
746753
let role = hive.role(hive_role).context(InternalOperatorFailureSnafu)?;
747754
let rolegroup = hive
@@ -815,6 +822,32 @@ fn build_metastore_rolegroup_statefulset(
815822
}
816823
}
817824

825+
// Add OPA TLS certs if configured
826+
if let Some((tls_secret_class, tls_mount_path)) =
827+
hive_opa_config.as_ref().and_then(|opa_config| {
828+
opa_config
829+
.tls_secret_class
830+
.as_ref()
831+
.zip(opa_config.tls_ca_cert_mount_path())
832+
})
833+
{
834+
container_builder
835+
.add_volume_mount(OPA_TLS_VOLUME_NAME, &tls_mount_path)
836+
.context(AddVolumeMountSnafu)?;
837+
838+
let opa_tls_volume = VolumeBuilder::new(OPA_TLS_VOLUME_NAME)
839+
.ephemeral(
840+
SecretOperatorVolumeSourceBuilder::new(tls_secret_class)
841+
.build()
842+
.context(TlsCertSecretClassVolumeBuildSnafu)?,
843+
)
844+
.build();
845+
846+
pod_builder
847+
.add_volume(opa_tls_volume)
848+
.context(AddVolumeSnafu)?;
849+
}
850+
818851
let db_type = hive.db_type();
819852
let start_command = if resolved_product_image.product_version.starts_with("3.") {
820853
// The schematool version in 3.1.x does *not* support the `-initOrUpgradeSchema` flag yet, so we can not use that.
@@ -866,6 +899,7 @@ fn build_metastore_rolegroup_statefulset(
866899
create_vector_shutdown_file_command(STACKABLE_LOG_DIR),
867900
},
868901
s3_connection,
902+
hive_opa_config,
869903
))
870904
.add_volume_mount(STACKABLE_CONFIG_DIR_NAME, STACKABLE_CONFIG_DIR)
871905
.context(AddVolumeMountSnafu)?

tests/templates/kuttl/logging/test_log_aggregation.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def check_sent_events():
2323
},
2424
)
2525

26-
assert (
27-
response.status_code == 200
28-
), "Cannot access the API of the vector aggregator."
26+
assert response.status_code == 200, (
27+
"Cannot access the API of the vector aggregator."
28+
)
2929

3030
result = response.json()
3131

@@ -35,13 +35,13 @@ def check_sent_events():
3535
componentId = transform["componentId"]
3636

3737
if componentId == "filteredInvalidEvents":
38-
assert (
39-
sentEvents is None or sentEvents["sentEventsTotal"] == 0
40-
), "Invalid log events were sent."
38+
assert sentEvents is None or sentEvents["sentEventsTotal"] == 0, (
39+
"Invalid log events were sent."
40+
)
4141
else:
42-
assert (
43-
sentEvents is not None and sentEvents["sentEventsTotal"] > 0
44-
), f'No events were sent in "{componentId}".'
42+
assert sentEvents is not None and sentEvents["sentEventsTotal"] > 0, (
43+
f'No events were sent in "{componentId}".'
44+
)
4545

4646

4747
if __name__ == "__main__":

tests/templates/kuttl/smoke/50-assert.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
22
apiVersion: kuttl.dev/v1beta1
33
kind: TestAssert
4-
timeout: 600
4+
timeout: 300
55
commands:
6-
- script: kubectl -n $NAMESPACE rollout status daemonset opa-server-default --timeout 600s
6+
- script: kubectl -n $NAMESPACE rollout status daemonset opa-server-default --timeout 300s
77
---
88
apiVersion: v1
99
kind: ConfigMap

tests/templates/kuttl/smoke/50-install-opa.yaml.j2

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,58 @@
11
---
2-
apiVersion: opa.stackable.tech/v1alpha1
3-
kind: OpaCluster
4-
metadata:
5-
name: opa
6-
spec:
7-
image:
2+
apiVersion: kuttl.dev/v1beta1
3+
kind: TestStep
4+
commands:
5+
- script: |
6+
kubectl apply -n $NAMESPACE -f - <<EOF
7+
---
8+
apiVersion: opa.stackable.tech/v1alpha1
9+
kind: OpaCluster
10+
metadata:
11+
name: opa
12+
spec:
13+
image:
814
{% if test_scenario['values']['opa-latest'].find(",") > 0 %}
9-
custom: "{{ test_scenario['values']['opa-latest'].split(',')[1] }}"
10-
productVersion: "{{ test_scenario['values']['opa-latest'].split(',')[0] }}"
15+
custom: "{{ test_scenario['values']['opa-latest'].split(',')[1] }}"
16+
productVersion: "{{ test_scenario['values']['opa-latest'].split(',')[0] }}"
1117
{% else %}
12-
productVersion: "{{ test_scenario['values']['opa-latest'] }}"
18+
productVersion: "{{ test_scenario['values']['opa-latest'] }}"
1319
{% endif %}
14-
pullPolicy: IfNotPresent
15-
clusterConfig:
20+
pullPolicy: IfNotPresent
21+
clusterConfig:
22+
tls:
23+
serverSecretClass: opa-tls-$NAMESPACE
1624
{% if lookup('env', 'VECTOR_AGGREGATOR') %}
17-
vectorAggregatorConfigMapName: vector-aggregator-discovery
25+
vectorAggregatorConfigMapName: vector-aggregator-discovery
1826
{% endif %}
19-
servers:
20-
config:
21-
logging:
22-
enableVectorAgent: {{ lookup('env', 'VECTOR_AGGREGATOR') | length > 0 }}
23-
containers:
24-
opa:
25-
console:
26-
level: INFO
27-
file:
28-
level: INFO
29-
loggers:
30-
decision:
31-
level: INFO
32-
roleGroups:
33-
default: {}
27+
servers:
28+
config:
29+
logging:
30+
enableVectorAgent: {{ lookup('env', 'VECTOR_AGGREGATOR') | length > 0 }}
31+
containers:
32+
opa:
33+
console:
34+
level: INFO
35+
file:
36+
level: INFO
37+
loggers:
38+
decision:
39+
level: INFO
40+
roleGroups:
41+
default: {}
42+
---
43+
apiVersion: secrets.stackable.tech/v1alpha1
44+
kind: SecretClass
45+
metadata:
46+
name: opa-tls-$NAMESPACE
47+
spec:
48+
backend:
49+
autoTls:
50+
ca:
51+
autoGenerate: true
52+
secret:
53+
name: opa-tls-ca-$NAMESPACE
54+
namespace: $NAMESPACE
55+
3456
---
3557
apiVersion: v1
3658
kind: ConfigMap

tests/templates/kuttl/smoke/test_metastore_opa.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ def table(db_name, table_name, location):
3636

3737

3838
if __name__ == "__main__":
39-
all_args = argparse.ArgumentParser(description="Test hive-metastore-opa-authorizer and rego rules.")
39+
all_args = argparse.ArgumentParser(
40+
description="Test hive-metastore-opa-authorizer and rego rules."
41+
)
4042
all_args.add_argument("-p", "--port", help="Metastore server port", default="9083")
4143
all_args.add_argument(
4244
"-d", "--database", help="Test DB name", default="db_not_allowed"
@@ -53,15 +55,21 @@ def table(db_name, table_name, location):
5355
# Creating database object using builder
5456
database = DatabaseBuilder(database_name).build()
5557

56-
print(f"[INFO] Trying to access '{database_name}' which is expected to fail due to 'database_allow' authorization policy...!")
58+
print(
59+
f"[INFO] Trying to access '{database_name}' which is expected to fail due to 'database_allow' authorization policy...!"
60+
)
5761

5862
with HiveMetastoreClient(host, port) as hive_client:
5963
try:
6064
hive_client.create_database_if_not_exists(database)
6165
except Exception as e:
6266
print(f"[DENIED] {e}")
63-
print(f"[SUCCESS] Test hive-metastore-opa-authorizer succeeded. Could not access database '{database_name}'!")
67+
print(
68+
f"[SUCCESS] Test hive-metastore-opa-authorizer succeeded. Could not access database '{database_name}'!"
69+
)
6470
exit(0)
6571

66-
print(f"[ERROR] Test hive-metastore-opa-authorizer failed. Could access database '{database_name}'!")
72+
print(
73+
f"[ERROR] Test hive-metastore-opa-authorizer failed. Could access database '{database_name}'!"
74+
)
6775
exit(-1)

0 commit comments

Comments
 (0)