-
-
Notifications
You must be signed in to change notification settings - Fork 6
chore: Use internal secret for JWT key #686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
87bcb06
chore: Use internal secret for JWT token
adwk67 47c0f0a
Merge branch 'main' into chore/use-internal-secrets
adwk67 a595224
remove connections.secretKey references, changelog, env-var comments
adwk67 d06543c
cleaned up comments
adwk67 8fe1fd4
review feedback: refactoring
adwk67 c00aa2f
replace openssl with rand crate
adwk67 bec6bcd
fix conflict
adwk67 7d06d46
use rand crate for generating keys
adwk67 923cbb2
corrected error text
adwk67 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| use std::collections::BTreeMap; | ||
|
|
||
| use base64::{Engine as _, engine::general_purpose}; | ||
| use rand::{TryRngCore, rand_core::OsError, rngs::OsRng}; | ||
| use snafu::{OptionExt, ResultExt, Snafu}; | ||
| use stackable_operator::{ | ||
| builder::meta::ObjectMetaBuilder, client::Client, k8s_openapi::api::core::v1::Secret, | ||
| kube::ResourceExt, logging::controller::ReconcilerError, | ||
| }; | ||
| use strum::{EnumDiscriminants, IntoStaticStr}; | ||
|
|
||
| use crate::{airflow_controller::AIRFLOW_CONTROLLER_NAME, crd::v1alpha1}; | ||
|
|
||
| // Used for env-vars: AIRFLOW__WEBSERVER__SECRET_KEY, AIRFLOW__API__SECRET_KEY | ||
| // N.B. AIRFLOW__WEBSERVER__SECRET_KEY is deprecated as of 3.0.2. | ||
| // Secret key used to run the api server. It should be as random as possible. | ||
| // It should be consistent across instances of the webserver. The webserver key | ||
| // is also used to authorize requests to Celery workers when logs are retrieved. | ||
| pub const ENV_INTERNAL_SECRET: &str = "INTERNAL_SECRET"; | ||
| // Used for env-var: AIRFLOW__API_AUTH__JWT_SECRET | ||
| // Secret key used to encode and decode JWTs to authenticate to public and | ||
| // private APIs. It should be as random as possible, but consistent across | ||
| // instances of API services. | ||
| pub const ENV_JWT_SECRET: &str = "JWT_SECRET"; | ||
|
|
||
| type Result<T, E = Error> = std::result::Result<T, E>; | ||
|
|
||
| impl ReconcilerError for Error { | ||
| fn category(&self) -> &'static str { | ||
| ErrorDiscriminants::from(self).into() | ||
| } | ||
| } | ||
|
|
||
| #[derive(Snafu, Debug, EnumDiscriminants)] | ||
| #[strum_discriminants(derive(IntoStaticStr))] | ||
| pub enum Error { | ||
| #[snafu(display("object defines no namespace"))] | ||
| ObjectHasNoNamespace, | ||
|
|
||
| #[snafu(display("object is missing metadata to build owner reference"))] | ||
| ObjectMissingMetadataForOwnerRef { | ||
| source: stackable_operator::builder::meta::Error, | ||
| }, | ||
|
|
||
| #[snafu(display("failed to retrieve secret for internal communications"))] | ||
| FailedToRetrieveInternalSecret { | ||
| source: stackable_operator::client::Error, | ||
| }, | ||
|
|
||
| #[snafu(display("failed to apply internal secret"))] | ||
| ApplyInternalSecret { | ||
| source: stackable_operator::client::Error, | ||
| }, | ||
|
|
||
| #[snafu(display("object defines no namespace"))] | ||
| SeedRandomGenerator { source: OsError }, | ||
| } | ||
|
|
||
| pub async fn create_random_secret( | ||
| secret_name: &str, | ||
| secret_key: &str, | ||
| secret_byte_size: usize, | ||
| airflow: &v1alpha1::AirflowCluster, | ||
| client: &Client, | ||
| ) -> Result<()> { | ||
| let mut internal_secret = BTreeMap::new(); | ||
| internal_secret.insert(secret_key.to_string(), get_random_base64(secret_byte_size)?); | ||
|
|
||
| let secret = Secret { | ||
| immutable: Some(true), | ||
| metadata: ObjectMetaBuilder::new() | ||
| .name(secret_name) | ||
| .namespace_opt(airflow.namespace()) | ||
| .ownerreference_from_resource(airflow, None, Some(true)) | ||
| .context(ObjectMissingMetadataForOwnerRefSnafu)? | ||
| .build(), | ||
| string_data: Some(internal_secret), | ||
| ..Secret::default() | ||
| }; | ||
|
|
||
| if client | ||
| .get_opt::<Secret>( | ||
| &secret.name_any(), | ||
| secret | ||
| .namespace() | ||
| .as_deref() | ||
| .context(ObjectHasNoNamespaceSnafu)?, | ||
| ) | ||
| .await | ||
| .context(FailedToRetrieveInternalSecretSnafu)? | ||
| .is_none() | ||
| { | ||
| client | ||
| .apply_patch(AIRFLOW_CONTROLLER_NAME, &secret, &secret) | ||
| .await | ||
| .context(ApplyInternalSecretSnafu)?; | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn get_random_base64(byte_size: usize) -> Result<String, Error> { | ||
| let mut buf = vec![0u8; byte_size]; | ||
| // OsRng is a cryptographically secure pseudo-random number generator | ||
| // (CSPRNG) and also has no possible state to leak and cannot be | ||
| // improperly seeded. See: https://rust-random.github.io/book/guide-gen.html#cryptographically-secure-pseudo-random-number-generator | ||
| // and https://github.com/rust-random/rand/blob/master/SECURITY.md#specific-generators | ||
| // This call explicity returns a Result. An alternative would be to | ||
| // use let mut rng = StdRng::from_os_rng() and then use fill_bytes | ||
| // but this may *still* panic if the underlying (OS) mechanism fails | ||
| // for some reason, so keep the potential panic transparent. | ||
| OsRng | ||
| .try_fill_bytes(&mut buf) | ||
| .context(SeedRandomGeneratorSnafu)?; | ||
| Ok(general_purpose::STANDARD.encode(buf)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.