mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-12 16:16:55 +00:00
refactor: route notify dispatch through app context (#3789)
* refactor: route notify dispatch through app context * refactor: route admin IAM globals through app context (#3791) * refactor: centralize IAM root credential access (#3792)
This commit is contained in:
@@ -47,6 +47,7 @@ pub mod keyring;
|
||||
pub mod manager;
|
||||
pub mod oidc;
|
||||
pub mod oidc_state;
|
||||
mod root_credentials;
|
||||
pub mod store;
|
||||
pub mod sys;
|
||||
pub mod utils;
|
||||
@@ -60,6 +61,10 @@ pub(crate) type IamStore = EcstoreStore;
|
||||
pub(crate) type IamConfigObjectInfo = <IamStore as rustfs_storage_api::ObjectOperations>::ObjectInfo;
|
||||
pub(crate) type IamConfigObjectOptions = <IamStore as rustfs_storage_api::ObjectOperations>::ObjectOptions;
|
||||
|
||||
pub fn is_root_access_key(access_key: &str) -> bool {
|
||||
root_credentials::is_root_access_key(access_key)
|
||||
}
|
||||
|
||||
pub(crate) async fn read_iam_config_no_lock(api: Arc<IamStore>, file: &str) -> IamStorageResult<Vec<u8>> {
|
||||
ecstore_read_config_no_lock(api, file).await
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ use crate::{
|
||||
},
|
||||
};
|
||||
use futures::future::join_all;
|
||||
use rustfs_credentials::{Credentials, EMBEDDED_POLICY_TYPE, INHERITED_POLICY_TYPE, get_global_action_cred};
|
||||
use rustfs_credentials::{Credentials, EMBEDDED_POLICY_TYPE, INHERITED_POLICY_TYPE};
|
||||
use rustfs_madmin::{AccountStatus, AddOrUpdateUserReq, GroupDesc};
|
||||
use rustfs_policy::{
|
||||
arn::ARN,
|
||||
@@ -2137,11 +2137,7 @@ fn set_default_canned_policies(policies: &mut HashMap<String, PolicyDoc>) {
|
||||
}
|
||||
|
||||
pub fn get_token_signing_key() -> Option<String> {
|
||||
if let Some(s) = get_global_action_cred() {
|
||||
Some(s.secret_key)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
crate::root_credentials::token_signing_key()
|
||||
}
|
||||
|
||||
pub fn extract_jwt_claims(u: &UserIdentity) -> Result<HashMap<String, Value>> {
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright 2024 RustFS Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use rustfs_credentials::{Credentials, get_global_action_cred};
|
||||
|
||||
pub(crate) fn credentials() -> Option<Credentials> {
|
||||
get_global_action_cred()
|
||||
}
|
||||
|
||||
pub(crate) fn credentials_or_default() -> Credentials {
|
||||
credentials().unwrap_or_default()
|
||||
}
|
||||
|
||||
pub(crate) fn token_signing_key() -> Option<String> {
|
||||
credentials().map(|cred| cred.secret_key)
|
||||
}
|
||||
|
||||
pub(crate) fn is_root_access_key(access_key: &str) -> bool {
|
||||
credentials().is_some_and(|cred| cred.access_key == access_key)
|
||||
}
|
||||
@@ -23,9 +23,9 @@ use crate::{
|
||||
error::{is_err_no_such_policy, is_err_no_such_user},
|
||||
keyring,
|
||||
manager::{extract_jwt_claims, extract_jwt_claims_allow_missing_exp, get_default_policyes},
|
||||
root_credentials,
|
||||
};
|
||||
use futures::future::join_all;
|
||||
use rustfs_credentials::get_global_action_cred;
|
||||
use rustfs_io_metrics::record_system_path_failure;
|
||||
use rustfs_policy::{auth::UserIdentity, policy::PolicyDoc};
|
||||
use rustfs_storage_api::{HTTPPreconditions, ListOperations as _, ObjectInfoOrErr as StorageObjectInfoOrErr, ObjectOperations};
|
||||
@@ -165,7 +165,7 @@ impl ObjectStore {
|
||||
}
|
||||
|
||||
const STREAM_IO_HEADER_LEN: usize = 41;
|
||||
let cred = get_global_action_cred().unwrap_or_default();
|
||||
let cred = root_credentials::credentials_or_default();
|
||||
let mut last_err = None;
|
||||
|
||||
let mut try_decrypt_with_key = |key: &[u8], source: DecryptSource| -> Option<DecryptOutcome> {
|
||||
@@ -1273,16 +1273,16 @@ impl Store for ObjectStore {
|
||||
mod tests {
|
||||
use super::{DecryptSource, ObjectStore};
|
||||
use crate::keyring;
|
||||
use rustfs_credentials::{Credentials, get_global_action_cred, init_global_action_credentials};
|
||||
use rustfs_credentials::{Credentials, init_global_action_credentials};
|
||||
use serial_test::serial;
|
||||
use temp_env::with_vars;
|
||||
|
||||
fn test_cred() -> Credentials {
|
||||
if let Some(cred) = get_global_action_cred() {
|
||||
if let Some(cred) = crate::root_credentials::credentials() {
|
||||
return cred;
|
||||
}
|
||||
let _ = init_global_action_credentials(Some("COMPATTESTAK".to_string()), Some("COMPATTESTSK1234567890".to_string()));
|
||||
get_global_action_cred().unwrap_or_default()
|
||||
crate::root_credentials::credentials_or_default()
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -28,7 +28,7 @@ use crate::{
|
||||
notify_iam_delete_policy, notify_iam_delete_service_account, notify_iam_delete_user, notify_iam_load_group,
|
||||
notify_iam_load_policy, notify_iam_load_policy_mapping, notify_iam_load_service_account, notify_iam_load_user,
|
||||
};
|
||||
use rustfs_credentials::{Credentials, EMBEDDED_POLICY_TYPE, INHERITED_POLICY_TYPE, get_global_action_cred};
|
||||
use rustfs_credentials::{Credentials, EMBEDDED_POLICY_TYPE, INHERITED_POLICY_TYPE};
|
||||
use rustfs_madmin::AddOrUpdateUserReq;
|
||||
use rustfs_madmin::GroupDesc;
|
||||
use rustfs_policy::arn::ARN;
|
||||
@@ -748,7 +748,7 @@ impl<T: Store> IamSys<T> {
|
||||
}
|
||||
|
||||
pub async fn check_key(&self, access_key: &str) -> Result<(Option<UserIdentity>, bool)> {
|
||||
if let Some(sys_cred) = get_global_action_cred()
|
||||
if let Some(sys_cred) = crate::root_credentials::credentials()
|
||||
&& sys_cred.access_key == access_key
|
||||
{
|
||||
return Ok((Some(UserIdentity::new(sys_cred)), true));
|
||||
@@ -1010,7 +1010,7 @@ impl<T: Store> IamSys<T> {
|
||||
}
|
||||
|
||||
pub(crate) async fn prepare_sts_auth(&self, args: &Args<'_>, parent_user: &str) -> PreparedIamAuth {
|
||||
let is_owner = matches!(get_global_action_cred(), Some(cred) if cred.access_key == parent_user);
|
||||
let is_owner = crate::is_root_access_key(parent_user);
|
||||
let role_arn = args.get_role_arn();
|
||||
|
||||
let (effective_groups, groups_source, policies) = if is_owner {
|
||||
@@ -1162,7 +1162,7 @@ impl<T: Store> IamSys<T> {
|
||||
&& matches!(mode, PreparedServicePolicyMode::SessionBound)
|
||||
&& matches!(session_policy, PreparedSessionPolicy::Policy(_));
|
||||
|
||||
let is_owner = matches!(get_global_action_cred(), Some(cred) if cred.access_key == parent_user);
|
||||
let is_owner = crate::is_root_access_key(parent_user);
|
||||
let role_arn = args.get_role_arn();
|
||||
|
||||
let svc_policies = if is_owner || bypass_parent_policy {
|
||||
@@ -1352,7 +1352,7 @@ mod tests {
|
||||
use crate::error::Error;
|
||||
use crate::manager::get_default_policyes;
|
||||
use crate::store::{GroupInfo, MappedPolicy, Store, UserType};
|
||||
use rustfs_credentials::{Credentials, get_global_action_cred, init_global_action_credentials};
|
||||
use rustfs_credentials::{Credentials, init_global_action_credentials};
|
||||
use rustfs_policy::auth::{UserIdentity, get_new_credentials_with_metadata};
|
||||
use rustfs_policy::policy::Args;
|
||||
use rustfs_policy::policy::action::{Action, AdminAction, S3Action};
|
||||
@@ -1660,7 +1660,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn ensure_test_global_credentials() {
|
||||
if get_global_action_cred().is_none() {
|
||||
if crate::root_credentials::credentials().is_none() {
|
||||
let _ = init_global_action_credentials(Some("TESTROOTACCESSKEY".to_string()), Some("TESTROOTSECRET123".to_string()));
|
||||
}
|
||||
}
|
||||
@@ -1940,9 +1940,8 @@ mod tests {
|
||||
let iam_sys = IamSys::new(cache_manager);
|
||||
|
||||
let parent_user = "sts-fallback-test-parent";
|
||||
let token_secret = get_global_action_cred()
|
||||
.expect("global action credentials should be initialized")
|
||||
.secret_key;
|
||||
let token_secret = crate::root_credentials::token_signing_key()
|
||||
.unwrap_or_else(|| unreachable!("global action credentials should be initialized"));
|
||||
let mut claims = HashMap::new();
|
||||
claims.insert("parent".to_string(), Value::String(parent_user.to_string()));
|
||||
claims.insert(
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use rustfs_credentials;
|
||||
use rustfs_policy::policy::action::S3Action as PolicyS3Action;
|
||||
use serde_json;
|
||||
use std::collections::HashMap;
|
||||
@@ -299,11 +298,7 @@ pub async fn is_authorized(
|
||||
let policy_action: rustfs_policy::policy::action::Action = action.clone().into();
|
||||
|
||||
// Check if user is the owner (admin)
|
||||
let is_owner = if let Some(global_cred) = rustfs_credentials::get_global_action_cred() {
|
||||
session_context.principal.access_key() == global_cred.access_key
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let is_owner = rustfs_iam::is_root_access_key(session_context.principal.access_key());
|
||||
|
||||
let args = rustfs_policy::policy::Args {
|
||||
account: session_context.principal.access_key(),
|
||||
|
||||
Reference in New Issue
Block a user