-
Notifications
You must be signed in to change notification settings - Fork 12
refactor(spider-core)!: Unify external resource group credentials (fixes #468). #469
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
Open
sitaowang1998
wants to merge
6
commits into
y-scope:main
Choose a base branch
from
sitaowang1998:unify-em-credential
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
157a106
Use external rg credential for add resource group proto
sitaowang1998 4ad532c
Consilidate external rg credential into core
sitaowang1998 7754046
Reexport external resource group credential
sitaowang1998 8405a1e
Use secrecy for password
sitaowang1998 f805c46
Add from_env for resource group credentials
sitaowang1998 2a45463
Merge branch 'main' into unify-em-credential
sitaowang1998 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| pub mod id; | ||
| pub mod io; | ||
| pub mod resource_group; | ||
| pub mod scheduler; |
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,87 @@ | ||
| //! Resource group types shared across Spider components. | ||
|
|
||
| use secrecy::ExposeSecret; | ||
| use secrecy::SecretSlice; | ||
|
|
||
| /// Environment variable that supplies the external resource group ID. | ||
| pub const EXTERNAL_RESOURCE_GROUP_ID_ENV: &str = "SPIDER_EXTERNAL_RESOURCE_GROUP_ID"; | ||
|
|
||
| /// Environment variable that supplies the external resource group password. | ||
| pub const EXTERNAL_RESOURCE_GROUP_PASSWORD_ENV: &str = "SPIDER_EXTERNAL_RESOURCE_GROUP_PASSWORD"; | ||
|
|
||
| /// Credentials identifying and authenticating an external resource group. | ||
| #[derive(Debug, Clone)] | ||
| pub struct ExternalResourceGroupCredentials { | ||
| /// The external resource group ID. | ||
| external_resource_group_id: String, | ||
|
|
||
| /// The resource group password. | ||
| password: SecretSlice<u8>, | ||
| } | ||
|
|
||
| impl ExternalResourceGroupCredentials { | ||
| /// Factory function. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// The newly created external resource group credentials. | ||
| #[must_use] | ||
| pub fn new(external_resource_group_id: String, password: Vec<u8>) -> Self { | ||
| Self { | ||
| external_resource_group_id, | ||
| password: password.into(), | ||
| } | ||
| } | ||
|
|
||
| /// Factory function. | ||
| /// | ||
| /// Reads the external resource group credentials from the [`EXTERNAL_RESOURCE_GROUP_ID_ENV`] | ||
| /// and [`EXTERNAL_RESOURCE_GROUP_PASSWORD_ENV`] environment variables. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// The credentials on success. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if: | ||
| /// | ||
| /// * [`ExternalResourceGroupCredentialsError::MissingEnvVar`] if either environment variable is | ||
| /// unset. | ||
| pub fn from_env() -> Result<Self, ExternalResourceGroupCredentialsError> { | ||
| let external_resource_group_id = | ||
| std::env::var(EXTERNAL_RESOURCE_GROUP_ID_ENV).map_err(|_| { | ||
| ExternalResourceGroupCredentialsError::MissingEnvVar(EXTERNAL_RESOURCE_GROUP_ID_ENV) | ||
| })?; | ||
| let password = std::env::var(EXTERNAL_RESOURCE_GROUP_PASSWORD_ENV).map_err(|_| { | ||
| ExternalResourceGroupCredentialsError::MissingEnvVar( | ||
| EXTERNAL_RESOURCE_GROUP_PASSWORD_ENV, | ||
| ) | ||
| })?; | ||
| Ok(Self::new(external_resource_group_id, password.into_bytes())) | ||
| } | ||
|
|
||
| /// # Returns | ||
| /// | ||
| /// The external resource group ID. | ||
| #[must_use] | ||
| pub fn get_external_resource_group_id(&self) -> &str { | ||
| &self.external_resource_group_id | ||
| } | ||
|
|
||
| /// # Returns | ||
| /// | ||
| /// The resource group password. | ||
| #[must_use] | ||
| pub fn get_password(&self) -> &[u8] { | ||
| self.password.expose_secret() | ||
| } | ||
| } | ||
|
|
||
| /// An error returned while reading external resource group credentials from the environment. | ||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum ExternalResourceGroupCredentialsError { | ||
| /// A required environment variable is unavailable. | ||
| #[error("required environment variable `{0}` is not set")] | ||
| MissingEnvVar(&'static str), | ||
| } | ||
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,48 @@ | ||
| //! Conversions between protobuf and Spider core resource group types. | ||
|
|
||
| use spider_core::types::resource_group::ExternalResourceGroupCredentials; | ||
|
|
||
| use crate::storage; | ||
|
|
||
| impl From<&ExternalResourceGroupCredentials> for storage::ExternalResourceGroupCredentials { | ||
| fn from(credentials: &ExternalResourceGroupCredentials) -> Self { | ||
| Self { | ||
| external_resource_group_id: credentials.get_external_resource_group_id().to_owned(), | ||
| password: credentials.get_password().to_vec(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<storage::ExternalResourceGroupCredentials> for ExternalResourceGroupCredentials { | ||
| fn from(credentials: storage::ExternalResourceGroupCredentials) -> Self { | ||
| Self::new(credentials.external_resource_group_id, credentials.password) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use prost::Message; | ||
| use spider_core::types::resource_group::ExternalResourceGroupCredentials; | ||
|
|
||
| use crate::storage; | ||
|
|
||
| #[test] | ||
| fn test_external_resource_group_credentials_protocol_round_trip() { | ||
| let credentials = ExternalResourceGroupCredentials::new( | ||
| "external-resource-group".to_owned(), | ||
| vec![0, 1, 2, 255], | ||
| ); | ||
|
|
||
| let encoded = storage::ExternalResourceGroupCredentials::from(&credentials).encode_to_vec(); | ||
| let decoded = ExternalResourceGroupCredentials::from( | ||
| storage::ExternalResourceGroupCredentials::decode(encoded.as_slice()) | ||
| .expect("external resource group credentials should decode"), | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| decoded.get_external_resource_group_id(), | ||
| credentials.get_external_resource_group_id() | ||
| ); | ||
| assert_eq!(decoded.get_password(), credentials.get_password()); | ||
| } | ||
| } |
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 | ||
|---|---|---|---|---|
|
|
@@ -7,9 +7,9 @@ pub use mariadb::MariaDbStorageConnector; | |||
| pub use protocol::DbStorage; | ||||
| pub use protocol::ExecutionManagerLivenessManagement; | ||||
| pub use protocol::ExternalJobOrchestration; | ||||
| pub use protocol::ExternalResourceGroupCredentials; | ||||
| pub use protocol::InternalJobOrchestration; | ||||
| pub use protocol::RecoverableJobContext; | ||||
| pub use protocol::ResourceGroupManagement; | ||||
| pub use protocol::SchedulerRegistrationManagement; | ||||
| pub use protocol::SessionManagement; | ||||
| pub use spider_core::types::resource_group::ExternalResourceGroupCredentials; | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't re-export this mod in
Suggested change
|
||||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about dropping
external?EXTERNAL_RESOURCE_GROUP_PASSWORD_ENV->RESOURCE_GROUP_PASSWORD_ENVSPIDER_EXTERNAL_RESOURCE_GROUP_PASSWORD->SPIDER_RESOURCE_GROUP_PASSWORDThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with changing the env name, but not the variable name. Inside Spider we need to distinguish user provided resource group id and password against internal resource group id.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not suggesting to rename the first en var. I don't think we need to differentiate "external" and "internal" passward since there's only one password, right?