|
| 1 | +use std::collections::BTreeMap; |
| 2 | + |
| 3 | +use stackable_operator::{ |
| 4 | + client::Client, |
| 5 | + commons::opa::{OpaApiVersion, OpaConfig}, |
| 6 | + k8s_openapi::api::core::v1::ConfigMap, |
| 7 | + kube::ResourceExt, |
| 8 | +}; |
| 9 | + |
| 10 | +use crate::crd::v1alpha1::HiveCluster; |
| 11 | + |
| 12 | +const HIVE_METASTORE_PRE_EVENT_LISTENERS: &str = "hive.metastore.pre.event.listeners"; |
| 13 | +const HIVE_SECURITY_METASTORE_AUTHORIZATION_MANAGER: &str = |
| 14 | + "hive.security.metastore.authorization.manager"; |
| 15 | + |
| 16 | +const OPA_AUTHORIZATION_PRE_EVENT_LISTENER_V3: &str = |
| 17 | + "com.bosch.bdps.hms3.OpaAuthorizationPreEventListener"; |
| 18 | +const OPA_BASED_AUTHORIZATION_PROVIDER_V3: &str = |
| 19 | + "com.bosch.bdps.hms3.OpaBasedAuthorizationProvider"; |
| 20 | +const OPA_AUTHORIZATION_PRE_EVENT_LISTENER_V4: &str = |
| 21 | + "com.bosch.bdps.hms4.OpaAuthorizationPreEventListener"; |
| 22 | +const OPA_BASED_AUTHORIZATION_PROVIDER_V4: &str = |
| 23 | + "com.bosch.bdps.hms4.OpaBasedAuthorizationProvider"; |
| 24 | + |
| 25 | +const OPA_AUTHORIZATION_BASE_ENDPOINT: &str = "com.bosch.bdps.opa.authorization.base.endpoint"; |
| 26 | +const OPA_AUTHORIZATION_POLICY_URL_DATA_BASE: &str = |
| 27 | + "com.bosch.bdps.opa.authorization.policy.url.database"; |
| 28 | +const OPA_AUTHORIZATION_POLICY_URL_TABLE: &str = |
| 29 | + "com.bosch.bdps.opa.authorization.policy.url.table"; |
| 30 | +const OPA_AUTHORIZATION_POLICY_URL_COLUMN: &str = |
| 31 | + "com.bosch.bdps.opa.authorization.policy.url.column"; |
| 32 | +const OPA_AUTHORIZATION_POLICY_URL_PARTITION: &str = |
| 33 | + "com.bosch.bdps.opa.authorization.policy.url.partition"; |
| 34 | +const OPA_AUTHORIZATION_POLICY_URL_USER: &str = "com.bosch.bdps.opa.authorization.policy.url.user"; |
| 35 | + |
| 36 | +pub const OPA_TLS_VOLUME_NAME: &str = "opa-tls"; |
| 37 | + |
| 38 | +pub struct HiveOpaConfig { |
| 39 | + /// Endpoint for OPA, e.g. |
| 40 | + /// `http://localhost:8081/v1/data/hms/allow` |
| 41 | + pub(crate) base_endpoint: String, |
| 42 | + /// Policy to check database authorization, e.g. |
| 43 | + /// `http://localhost:8081/v1/data/hms/database_allow` |
| 44 | + pub(crate) policy_url_database: String, |
| 45 | + /// Policy to check table authorization, e.g. |
| 46 | + /// `http://localhost:8081/v1/data/hms/table_allow` |
| 47 | + pub(crate) policy_url_table: String, |
| 48 | + /// Policy to check column authorization, e.g. |
| 49 | + /// `http://localhost:8081/v1/data/hms/column_allow` |
| 50 | + pub(crate) policy_url_column: String, |
| 51 | + /// Policy to check partition authorization, e.g. |
| 52 | + /// `http://localhost:8081/v1/data/hms/partition_allow` |
| 53 | + pub(crate) policy_url_partition: String, |
| 54 | + /// Policy to check user authorization, e.g. |
| 55 | + /// `http://localhost:8081/v1/data/hms/user_allow` |
| 56 | + pub(crate) policy_url_user: String, |
| 57 | + /// Optional TLS secret class for OPA communication. |
| 58 | + /// If set, the CA certificate from this secret class will be added |
| 59 | + /// to hive's truststore to make it trust OPA's TLS certificate. |
| 60 | + pub(crate) tls_secret_class: Option<String>, |
| 61 | +} |
| 62 | + |
| 63 | +impl HiveOpaConfig { |
| 64 | + pub async fn from_opa_config( |
| 65 | + client: &Client, |
| 66 | + hive: &HiveCluster, |
| 67 | + opa_config: &OpaConfig, |
| 68 | + ) -> Result<Self, stackable_operator::commons::opa::Error> { |
| 69 | + // See: https://github.com/boschglobal/hive-metastore-opa-authorizer?tab=readme-ov-file#configuration |
| 70 | + // TODO: get document root once (client call) and build the other strings |
| 71 | + let base_endpoint = opa_config |
| 72 | + .full_document_url_from_config_map(client, hive, Some("allow"), OpaApiVersion::V1) |
| 73 | + .await?; |
| 74 | + |
| 75 | + let policy_url_database = opa_config |
| 76 | + .full_document_url_from_config_map( |
| 77 | + client, |
| 78 | + hive, |
| 79 | + Some("database_allow"), |
| 80 | + OpaApiVersion::V1, |
| 81 | + ) |
| 82 | + .await?; |
| 83 | + let policy_url_table = opa_config |
| 84 | + .full_document_url_from_config_map(client, hive, Some("table_allow"), OpaApiVersion::V1) |
| 85 | + .await?; |
| 86 | + let policy_url_column = opa_config |
| 87 | + .full_document_url_from_config_map( |
| 88 | + client, |
| 89 | + hive, |
| 90 | + Some("column_allow"), |
| 91 | + OpaApiVersion::V1, |
| 92 | + ) |
| 93 | + .await?; |
| 94 | + let policy_url_partition = opa_config |
| 95 | + .full_document_url_from_config_map( |
| 96 | + client, |
| 97 | + hive, |
| 98 | + Some("partition_allow"), |
| 99 | + OpaApiVersion::V1, |
| 100 | + ) |
| 101 | + .await?; |
| 102 | + let policy_url_user = opa_config |
| 103 | + .full_document_url_from_config_map(client, hive, Some("user_allow"), OpaApiVersion::V1) |
| 104 | + .await?; |
| 105 | + |
| 106 | + let tls_secret_class = client |
| 107 | + .get::<ConfigMap>( |
| 108 | + &opa_config.config_map_name, |
| 109 | + hive.namespace().as_deref().unwrap_or("default"), |
| 110 | + ) |
| 111 | + .await |
| 112 | + .ok() |
| 113 | + .and_then(|cm| cm.data) |
| 114 | + .and_then(|mut data| data.remove("OPA_SECRET_CLASS")); |
| 115 | + |
| 116 | + Ok(HiveOpaConfig { |
| 117 | + base_endpoint, |
| 118 | + policy_url_database, |
| 119 | + policy_url_table, |
| 120 | + policy_url_column, |
| 121 | + policy_url_partition, |
| 122 | + policy_url_user, |
| 123 | + tls_secret_class, |
| 124 | + }) |
| 125 | + } |
| 126 | + |
| 127 | + pub fn as_config(&self, product_version: &str) -> BTreeMap<String, String> { |
| 128 | + let (pre_event_listener, authorization_provider) = if product_version.starts_with("3.") { |
| 129 | + ( |
| 130 | + OPA_AUTHORIZATION_PRE_EVENT_LISTENER_V3, |
| 131 | + OPA_BASED_AUTHORIZATION_PROVIDER_V3, |
| 132 | + ) |
| 133 | + } else { |
| 134 | + ( |
| 135 | + OPA_AUTHORIZATION_PRE_EVENT_LISTENER_V4, |
| 136 | + OPA_BASED_AUTHORIZATION_PROVIDER_V4, |
| 137 | + ) |
| 138 | + }; |
| 139 | + |
| 140 | + BTreeMap::from([ |
| 141 | + ( |
| 142 | + HIVE_METASTORE_PRE_EVENT_LISTENERS.to_string(), |
| 143 | + pre_event_listener.to_string(), |
| 144 | + ), |
| 145 | + ( |
| 146 | + HIVE_SECURITY_METASTORE_AUTHORIZATION_MANAGER.to_string(), |
| 147 | + authorization_provider.to_string(), |
| 148 | + ), |
| 149 | + ( |
| 150 | + OPA_AUTHORIZATION_BASE_ENDPOINT.to_string(), |
| 151 | + self.base_endpoint.to_owned(), |
| 152 | + ), |
| 153 | + ( |
| 154 | + OPA_AUTHORIZATION_POLICY_URL_DATA_BASE.to_string(), |
| 155 | + self.policy_url_database.to_owned(), |
| 156 | + ), |
| 157 | + ( |
| 158 | + OPA_AUTHORIZATION_POLICY_URL_TABLE.to_string(), |
| 159 | + self.policy_url_table.to_owned(), |
| 160 | + ), |
| 161 | + ( |
| 162 | + OPA_AUTHORIZATION_POLICY_URL_COLUMN.to_string(), |
| 163 | + self.policy_url_column.to_owned(), |
| 164 | + ), |
| 165 | + ( |
| 166 | + OPA_AUTHORIZATION_POLICY_URL_PARTITION.to_string(), |
| 167 | + self.policy_url_partition.to_owned(), |
| 168 | + ), |
| 169 | + ( |
| 170 | + OPA_AUTHORIZATION_POLICY_URL_USER.to_string(), |
| 171 | + self.policy_url_user.to_owned(), |
| 172 | + ), |
| 173 | + ]) |
| 174 | + } |
| 175 | + |
| 176 | + pub fn tls_mount_path(&self) -> Option<String> { |
| 177 | + self.tls_secret_class |
| 178 | + .as_ref() |
| 179 | + .map(|_| format!("/stackable/secrets/{OPA_TLS_VOLUME_NAME}")) |
| 180 | + } |
| 181 | +} |
0 commit comments