|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | +use snafu::Snafu; |
| 3 | +use stackable_operator::schemars::{self, JsonSchema}; |
| 4 | + |
| 5 | +#[derive(Snafu, Debug)] |
| 6 | +pub enum Error { |
| 7 | + #[snafu(display( |
| 8 | + "The sensitive properties algorithm '{algorithm}' is not supported in NiFi 2.X.X. Please see https://nifi.apache.org/docs/nifi-docs/html/administration-guide.html#updating-the-sensitive-properties-algorithm on how to upgrade the algorithm." |
| 9 | + ))] |
| 10 | + UnsupportedSensitivePropertiesAlgorithm { algorithm: String }, |
| 11 | +} |
| 12 | + |
| 13 | +/// These settings configure the encryption of sensitive properties in NiFi processors. |
| 14 | +/// NiFi supports encrypting sensitive properties in processors as they are written to disk. |
| 15 | +/// You can configure the encryption algorithm and the key to use. |
| 16 | +/// You can also let the operator generate an encryption key for you. |
| 17 | +#[derive(Clone, Debug, Default, Deserialize, Eq, JsonSchema, PartialEq, Serialize)] |
| 18 | +#[serde(rename_all = "camelCase")] |
| 19 | +pub struct NifiSensitivePropertiesConfig { |
| 20 | + /// A reference to a Secret. The Secret needs to contain a key `nifiSensitivePropsKey`. |
| 21 | + /// If `autoGenerate` is false and this object is missing, the Operator will raise an error. |
| 22 | + /// The encryption key needs to be at least 12 characters long. |
| 23 | + pub key_secret: String, |
| 24 | + |
| 25 | + /// Whether to generate the `keySecret` if it is missing. |
| 26 | + /// Defaults to `false`. |
| 27 | + #[serde(default)] |
| 28 | + pub auto_generate: bool, |
| 29 | + |
| 30 | + /// This is setting the `nifi.sensitive.props.algorithm` property in NiFi. |
| 31 | + /// This setting configures the encryption algorithm to use to encrypt sensitive properties. |
| 32 | + /// Valid values are: |
| 33 | + /// |
| 34 | + /// `nifiPbkdf2AesGcm256` (the default value), |
| 35 | + /// `nifiArgon2AesGcm256`, |
| 36 | + /// |
| 37 | + /// The following algorithms are deprecated and will be removed in future versions: |
| 38 | + /// |
| 39 | + /// `nifiArgon2AesGcm128`, |
| 40 | + /// `nifiBcryptAesGcm128`, |
| 41 | + /// `nifiBcryptAesGcm256`, |
| 42 | + /// `nifiPbkdf2AesGcm128`, |
| 43 | + /// `nifiScryptAesGcm128`, |
| 44 | + /// `nifiScryptAesGcm256`. |
| 45 | + /// |
| 46 | + /// Learn more about the specifics of the algorithm parameters in the |
| 47 | + /// [NiFi documentation](https://nifi.apache.org/docs/nifi-docs/html/administration-guide.html#property-encryption-algorithms). |
| 48 | + pub algorithm: Option<NifiSensitiveKeyAlgorithm>, |
| 49 | +} |
| 50 | + |
| 51 | +#[derive(strum::Display, Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)] |
| 52 | +#[serde(rename_all = "camelCase")] |
| 53 | +pub enum NifiSensitiveKeyAlgorithm { |
| 54 | + // supported in v2 |
| 55 | + #[strum(serialize = "NIFI_PBKDF2_AES_GCM_256")] |
| 56 | + NifiPbkdf2AesGcm256, |
| 57 | + // supported in v2 |
| 58 | + #[strum(serialize = "NIFI_ARGON2_AES_GCM_256")] |
| 59 | + NifiArgon2AesGcm256, |
| 60 | + // Deprecated in v1 -> can be removed when 1.x.x is no longer supported |
| 61 | + #[strum(serialize = "NIFI_BCRYPT_AES_GCM_128")] |
| 62 | + NifiBcryptAesGcm128, |
| 63 | + // Deprecated in v1 -> can be removed when 1.x.x is no longer supported |
| 64 | + #[strum(serialize = "NIFI_BCRYPT_AES_GCM_256")] |
| 65 | + NifiBcryptAesGcm256, |
| 66 | + // Deprecated in v1 -> can be removed when 1.x.x is no longer supported |
| 67 | + #[strum(serialize = "NIFI_PBKDF2_AES_GCM_128")] |
| 68 | + NifiPbkdf2AesGcm128, |
| 69 | + // Deprecated in v1 -> can be removed when 1.x.x is no longer supported |
| 70 | + #[strum(serialize = "NIFI_ARGON2_AES_GCM_128")] |
| 71 | + NifiArgon2AesGcm128, |
| 72 | + // Deprecated in v1 -> can be removed when 1.x.x is no longer supported |
| 73 | + #[strum(serialize = "NIFI_SCRYPT_AES_GCM_128")] |
| 74 | + NifiScryptAesGcm128, |
| 75 | + // Deprecated in v1 -> can be removed when 1.x.x is no longer supported |
| 76 | + #[strum(serialize = "NIFI_SCRYPT_AES_GCM_256")] |
| 77 | + NifiScryptAesGcm256, |
| 78 | +} |
| 79 | + |
| 80 | +impl NifiSensitiveKeyAlgorithm { |
| 81 | + /// Checks if the used encryption algorithm is supported or deprecated. |
| 82 | + /// Will warn for deprecation and error out for missing support. |
| 83 | + pub fn check_for_nifi_version(&self, product_version: &str) -> Result<(), Error> { |
| 84 | + let algorithm = self.to_string(); |
| 85 | + |
| 86 | + match self { |
| 87 | + // Allowed and supported in NiFi 1.x.x and 2.x.x |
| 88 | + NifiSensitiveKeyAlgorithm::NifiPbkdf2AesGcm256 |
| 89 | + | NifiSensitiveKeyAlgorithm::NifiArgon2AesGcm256 => {} |
| 90 | + // All others are deprecated in 1.x.x and removed in 2.x.x |
| 91 | + // see https://nifi.apache.org/docs/nifi-docs/html/administration-guide.html#property-encryption-algorithms |
| 92 | + _ => { |
| 93 | + if product_version.starts_with("1.") { |
| 94 | + tracing::warn!( |
| 95 | + "You are using a deprecated sensitive properties algorithm '{algorithm}'. Please update to '{pbkd}' or '{argon}'.", |
| 96 | + pbkd = NifiSensitiveKeyAlgorithm::NifiPbkdf2AesGcm256, |
| 97 | + argon = NifiSensitiveKeyAlgorithm::NifiArgon2AesGcm256 |
| 98 | + ) |
| 99 | + } else { |
| 100 | + return Err(Error::UnsupportedSensitivePropertiesAlgorithm { algorithm }); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + Ok(()) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl Default for NifiSensitiveKeyAlgorithm { |
| 110 | + fn default() -> Self { |
| 111 | + Self::NifiArgon2AesGcm256 |
| 112 | + } |
| 113 | +} |
0 commit comments