-
Notifications
You must be signed in to change notification settings - Fork 8
fix(auth): Govern token rescope via restrictions, not a handler guard #904
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
ShJ-code
wants to merge
1
commit into
openstack-experimental:main
Choose a base branch
from
ShJ-code:fix-763-test-rescope
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
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
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 |
|---|---|---|
|
|
@@ -84,14 +84,12 @@ async fn create_inner( | |
| let provider_scope: Option<ProviderScope> = req.auth.scope.clone().map(Into::into); | ||
| let authz_info = get_authz_info(state, provider_scope.as_ref()).await?; | ||
|
|
||
| // This is a new authentication/reauthentication. Check if that is allowed at | ||
| // all | ||
| if let Some(token_restriction) = ctx.token_restriction() | ||
|
Collaborator
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. I am very unsure in that . The lines that the issue were referring to (iirc) were removed with 466a398 so you should not be required to change anything here |
||
| && !token_restriction.allow_rescope | ||
| && req.auth.scope.is_some() | ||
| { | ||
| return Err(KeystoneApiError::AuthenticationRescopeForbidden); | ||
| } | ||
| // No rescope guard here: scope constraints are enforced downstream by | ||
| // issue_token_context. It calls SecurityContext::validate_scope_boundaries, | ||
| // which keeps a restricted token within its bounds (pinned project, or | ||
| // allow_rescope == false => current project only) and rejects disallowed scope | ||
| // types, then validates the user actually holds a role on the target scope before | ||
| // the token is issued. | ||
|
|
||
| let vsc = state | ||
| .provider | ||
|
|
@@ -669,4 +667,270 @@ mod tests { | |
|
|
||
| assert_eq!(response.status(), StatusCode::UNAUTHORIZED); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[traced_test] | ||
| async fn test_post_token_rescope_not_rejected_with_restriction() { | ||
| // Regression test for #763. A token (re)authentication carrying a | ||
| // restriction with `allow_rescope = false` and requesting a scope must no | ||
| // longer be rejected by the handler; whether the scope is permissible is | ||
| // decided by `issue_token_context` (mocked here to succeed). | ||
| use openstack_keystone_core_types::auth::AuthzInfoBuilder; | ||
| use openstack_keystone_core_types::identity::UserResponseBuilder; | ||
| use openstack_keystone_core_types::resource::ProjectBuilder; | ||
| use openstack_keystone_core_types::role::RoleRefBuilder; | ||
| use openstack_keystone_core_types::token::{ | ||
| FernetToken, ProjectScopePayload, TokenRestriction, | ||
| }; | ||
|
|
||
| let config = Config::default(); | ||
| let project = Project { | ||
| id: "pid".into(), | ||
| domain_id: "pdid".into(), | ||
| enabled: true, | ||
| ..Default::default() | ||
| }; | ||
| let project_domain = Domain { | ||
| id: "pdid".into(), | ||
| enabled: true, | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| // Context returned by token authentication: bound to `pid` and explicitly | ||
| // forbidding rescope. | ||
| let auth_vsc = { | ||
| let user_resp = UserResponseBuilder::default() | ||
| .id("uid") | ||
| .name("uname".to_string()) | ||
| .domain_id("user_domain_id".to_string()) | ||
| .enabled(true) | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| let restriction = TokenRestriction { | ||
| allow_rescope: false, | ||
| allow_renew: true, | ||
| id: "tr".into(), | ||
| domain_id: "user_domain_id".into(), | ||
| project_id: Some("pid".into()), | ||
| user_id: Some("uid".into()), | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| let authz = AuthzInfoBuilder::default() | ||
| .roles(vec![ | ||
| RoleRefBuilder::default() | ||
| .id("admin") | ||
| .name("admin") | ||
| .build() | ||
| .unwrap(), | ||
| ]) | ||
| .scope(ScopeInfo::Project { | ||
| project: ProjectBuilder::default() | ||
| .id("pid") | ||
| .domain_id("pdid") | ||
| .enabled(true) | ||
| .name("pname") | ||
| .build() | ||
| .unwrap(), | ||
| project_domain: DomainBuilder::default() | ||
| .id("pdid") | ||
| .name("pdname") | ||
| .enabled(true) | ||
| .build() | ||
| .unwrap(), | ||
| }) | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| let sc = SecurityContext::test_build() | ||
| .authentication_context(AuthenticationContext::Token(FernetToken::ProjectScope( | ||
| ProjectScopePayload { | ||
| user_id: "uid".into(), | ||
| methods: Vec::from(["password".to_string()]), | ||
| project_id: "pid".into(), | ||
| ..Default::default() | ||
| }, | ||
| ))) | ||
| .principal(PrincipalInfo { | ||
| identity: IdentityInfo::User( | ||
| UserIdentityInfoBuilder::default() | ||
| .user_id("uid") | ||
| .user(user_resp) | ||
| .user_domain( | ||
| DomainBuilder::default() | ||
| .id("user_domain_id") | ||
| .name("user_domain_name") | ||
| .enabled(true) | ||
| .build() | ||
| .unwrap(), | ||
| ) | ||
| .build() | ||
| .unwrap(), | ||
| ), | ||
| }) | ||
| .authorization(authz) | ||
| .token_restriction(restriction) | ||
| .build(); | ||
| openstack_keystone_core::auth::ValidatedSecurityContext::test_new(sc) | ||
| }; | ||
|
|
||
| // Project-scoped context produced by a successful issue_token_context | ||
| // (identical shape to `test_post`'s fixture). | ||
| let issued_vsc = { | ||
| let user_resp = UserResponseBuilder::default() | ||
| .id("uid") | ||
| .name("uname".to_string()) | ||
| .domain_id("user_domain_id".to_string()) | ||
| .enabled(true) | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| let fernet_payload = ProjectScopePayload { | ||
| user_id: "uid".into(), | ||
| methods: Vec::from(["token".to_string()]), | ||
| project_id: "pid".into(), | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| let authz = AuthzInfoBuilder::default() | ||
| .roles(vec![ | ||
| RoleRefBuilder::default() | ||
| .id("admin") | ||
| .name("admin") | ||
| .build() | ||
| .unwrap(), | ||
| ]) | ||
| .scope(ScopeInfo::Project { | ||
| project: ProjectBuilder::default() | ||
| .id("pid") | ||
| .domain_id("pdid") | ||
| .enabled(true) | ||
| .name("pname") | ||
| .build() | ||
| .unwrap(), | ||
| project_domain: DomainBuilder::default() | ||
| .id("pdid") | ||
| .name("pdname") | ||
| .enabled(true) | ||
| .build() | ||
| .unwrap(), | ||
| }) | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| let sc = SecurityContext::test_build() | ||
| .authentication_context(AuthenticationContext::Token(FernetToken::ProjectScope( | ||
| fernet_payload.clone(), | ||
| ))) | ||
| .principal(PrincipalInfo { | ||
| identity: IdentityInfo::User( | ||
| UserIdentityInfoBuilder::default() | ||
| .user_id("uid") | ||
| .user(user_resp) | ||
| .user_domain( | ||
| DomainBuilder::default() | ||
| .id("user_domain_id") | ||
| .name("user_domain_name") | ||
| .enabled(true) | ||
| .build() | ||
| .unwrap(), | ||
| ) | ||
| .build() | ||
| .unwrap(), | ||
| ), | ||
| }) | ||
| .token(FernetToken::ProjectScope(fernet_payload)) | ||
| .authorization(authz) | ||
| .build(); | ||
| openstack_keystone_core::auth::ValidatedSecurityContext::test_new(sc) | ||
| }; | ||
|
|
||
| let mut token_mock = MockTokenProvider::default(); | ||
| let auth_vsc_clone = auth_vsc.clone(); | ||
| token_mock | ||
| .expect_authorize_by_token() | ||
| .returning(move |_, _, _, _| Ok(auth_vsc_clone.clone())); | ||
| let issued_clone = issued_vsc.clone(); | ||
| token_mock | ||
| .expect_issue_token_context() | ||
| .once() | ||
| .returning(move |_, _, _| Ok(issued_clone.clone())); | ||
| token_mock | ||
| .expect_encode_token() | ||
| .returning(|_| Ok("token".to_string())); | ||
|
|
||
| let mut resource_mock = MockResourceProvider::default(); | ||
| resource_mock | ||
| .expect_get_project() | ||
| .withf(|_, id: &'_ str| id == "pid") | ||
| .returning(move |_, _| Ok(Some(project.clone()))); | ||
| resource_mock | ||
| .expect_get_domain() | ||
| .withf(|_, id: &'_ str| id == "pdid") | ||
| .returning(move |_, _| Ok(Some(project_domain.clone()))); | ||
|
|
||
| let mut catalog_mock = MockCatalogProvider::default(); | ||
| catalog_mock | ||
| .expect_get_catalog() | ||
| .returning(|_, _| Ok(Vec::new())); | ||
|
|
||
| let provider = Provider::mocked_builder() | ||
| .mock_resource(resource_mock) | ||
| .mock_token(token_mock) | ||
| .mock_catalog(catalog_mock) | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| let state = Arc::new( | ||
| Service::new( | ||
| ConfigManager::not_watched(config), | ||
| DatabaseConnection::Disconnected, | ||
| provider, | ||
| Arc::new(MockPolicy::default()), | ||
| AuditDispatcher::noop(), | ||
| None, | ||
| ) | ||
| .await | ||
| .unwrap(), | ||
| ); | ||
|
|
||
| let mut api = openapi_router() | ||
| .layer(TraceLayer::new_for_http()) | ||
| .with_state(state.clone()); | ||
|
|
||
| let response = api | ||
| .as_service() | ||
| .oneshot( | ||
| Request::builder() | ||
| .uri("/") | ||
| .method("POST") | ||
| .header(header::CONTENT_TYPE, "application/json") | ||
| .body(Body::from( | ||
| serde_json::to_vec(&json!({ | ||
| "auth": { | ||
| "identity": { | ||
| "methods": ["token"], | ||
| "token": { "id": "parent_token" } | ||
| }, | ||
| "scope": { | ||
| "project": { | ||
| "id": "pid", | ||
| "name": "pname", | ||
| "domain": { "id": "pdid", "name": "pdname" } | ||
| } | ||
| } | ||
| } | ||
| })) | ||
| .unwrap(), | ||
| )) | ||
| .unwrap(), | ||
| ) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| // Previously this returned 400 (AuthenticationRescopeForbidden). With the | ||
| // guard removed it reaches issue_token_context and succeeds. | ||
| assert_eq!(response.status(), StatusCode::OK); | ||
| } | ||
| } | ||
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.
this change is unnecessary (at least within this change). The test you modify is not dealing with restricted token, it only uses regular authentication tokens. Moreover I plan on removing token restrictions as a concept completely since they are now superseded by a more universal solution.