-
Notifications
You must be signed in to change notification settings - Fork 445
Core: Extract shared credential rotation pre-condition check (Closes #4026) #4420
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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. | ||
| */ | ||
| package org.apache.polaris.core.auth; | ||
|
|
||
| import org.apache.iceberg.exceptions.ForbiddenException; | ||
| import org.apache.polaris.core.entity.PolarisEntityConstants; | ||
|
|
||
| /** | ||
| * Common pre-condition checks shared across authorizer implementations for credential-related | ||
| * operations. | ||
| */ | ||
| public final class AuthorizationPreConditions { | ||
|
|
||
| private AuthorizationPreConditions() {} | ||
|
|
||
| /** | ||
| * Checks whether the principal is required to rotate credentials before performing the requested | ||
| * operation. If the principal has the {@code PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE} | ||
| * property set, only {@link PolarisAuthorizableOperation#ROTATE_CREDENTIALS} is allowed. | ||
| * | ||
| * @param polarisPrincipal the principal attempting the operation | ||
| * @param authzOp the operation being attempted | ||
| * @param enforceCredentialRotationRequiredState whether the enforcement flag is enabled | ||
| * @throws ForbiddenException if the principal must rotate credentials first | ||
| */ | ||
| public static void checkCredentialRotationRequired( | ||
| PolarisPrincipal polarisPrincipal, | ||
| PolarisAuthorizableOperation authzOp, | ||
| boolean enforceCredentialRotationRequiredState) { | ||
|
Contributor
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. It might be best to pass a
Author
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. Thank you @dimas-b for your review and inputs. Quick clarification on your Here's my understanding: you are suggesting an option to replace the boolean That way the flag-reading is unified in one place along with the rotation check. This should work well, please confirm that's what you meant before I push — or did you have a different thought in mind? |
||
| if (enforceCredentialRotationRequiredState | ||
| && authzOp != PolarisAuthorizableOperation.ROTATE_CREDENTIALS | ||
| && polarisPrincipal | ||
| .getProperties() | ||
| .containsKey(PolarisEntityConstants.PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE)) { | ||
| throw new ForbiddenException( | ||
| "Principal '%s' is not authorized for op %s due to PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE", | ||
| polarisPrincipal.getName(), authzOp); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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. | ||
| */ | ||
| package org.apache.polaris.core.auth; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThatCode; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import org.apache.iceberg.exceptions.ForbiddenException; | ||
| import org.apache.polaris.core.entity.PolarisEntityConstants; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class AuthorizationPreConditionsTest { | ||
|
|
||
| private static final String ROTATION_KEY = | ||
| PolarisEntityConstants.PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE; | ||
|
|
||
| @Test | ||
| public void testFlagOff_noException() { | ||
| PolarisPrincipal principal = | ||
| PolarisPrincipal.of("alice", Map.of(ROTATION_KEY, "true"), Set.of("role")); | ||
|
|
||
| assertThatCode( | ||
| () -> | ||
| AuthorizationPreConditions.checkCredentialRotationRequired( | ||
| principal, PolarisAuthorizableOperation.LIST_CATALOGS, false)) | ||
| .doesNotThrowAnyException(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFlagOn_rotateCredentialsOp_noException() { | ||
| PolarisPrincipal principal = | ||
| PolarisPrincipal.of("alice", Map.of(ROTATION_KEY, "true"), Set.of("role")); | ||
|
|
||
| assertThatCode( | ||
| () -> | ||
| AuthorizationPreConditions.checkCredentialRotationRequired( | ||
| principal, PolarisAuthorizableOperation.ROTATE_CREDENTIALS, true)) | ||
| .doesNotThrowAnyException(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFlagOn_nonRotationOp_propertySet_throwsForbidden() { | ||
| PolarisPrincipal principal = | ||
| PolarisPrincipal.of("alice", Map.of(ROTATION_KEY, "true"), Set.of("role")); | ||
|
|
||
| assertThatThrownBy( | ||
| () -> | ||
| AuthorizationPreConditions.checkCredentialRotationRequired( | ||
| principal, PolarisAuthorizableOperation.LIST_CATALOGS, true)) | ||
| .isInstanceOf(ForbiddenException.class) | ||
| .hasMessageContaining("PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFlagOn_nonRotationOp_propertyNotSet_noException() { | ||
| PolarisPrincipal principal = PolarisPrincipal.of("alice", Map.of(), Set.of("role")); | ||
|
|
||
| assertThatCode( | ||
| () -> | ||
| AuthorizationPreConditions.checkCredentialRotationRequired( | ||
| principal, PolarisAuthorizableOperation.LIST_CATALOGS, true)) | ||
| .doesNotThrowAnyException(); | ||
| } | ||
| } |
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 would suggest to add a unit test with the different conditions:
- flag off, no exception, regardless of property/op
- flag on + rotation property absent, no exception
- flag on + rotation property present + op == ROTATE_CREDENTIALS, no exception
- flag on + rotation property present + op != ROTATE_CREDENTIALS, ForbiddenException
Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks @jbonofre Added in 4244163 —
AuthorizationPreConditionsTestcovers all four cases:testFlagOff_noException— flag off, no exception regardless of property/optestFlagOn_nonRotationOp_propertyNotSet_noException— flag on + property absenttestFlagOn_rotateCredentialsOp_noException— flag on + property present + op == ROTATE_CREDENTIALStestFlagOn_nonRotationOp_propertySet_throwsForbidden— flag on + property present + op != ROTATE_CREDENTIALS