Skip to content
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

904 read permissions of entities within flex model or flex context #1071

Open
wants to merge 27 commits into
base: main
Choose a base branch
from

Conversation

Flix6x
Copy link
Contributor

@Flix6x Flix6x commented May 24, 2024

Closes #904.

Signed-off-by: F.N. Claessen <[email protected]>
Signed-off-by: F.N. Claessen <[email protected]>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nhoening I need advice on where to move these new util functions, for example, to:

  • api/common/utils/args_parsing.py
  • auth/loaders.py
  • or elsewhere, maybe even becoming a new acl method on some class? I'm out of my league here.

I saw other ctx_loaders were pointing to various things, such as:

  • ctx_loader=lambda bdf: bdf.sensor
  • ctx_loader=GenericAsset
  • ctx_loader=AccountIdField.load_current
  • ctx_loader=AuditLog.account_table_acl
  • ctx_loader=AuditLog.user_table_acl

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These have nothing to do with auth directly, but are lookup functions that rely on our planning data structure. So I guess this location isn't bad, really.

I don't see why ctx_loader should be used, as no AuthContext is touched.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why ctx_loader should be used, as no AuthContext is touched.

I'm not sure what to do with this. Am I using ctx_loader incorrectly?

Signed-off-by: F.N. Claessen <[email protected]>
Signed-off-by: F.N. Claessen <[email protected]>
Signed-off-by: F.N. Claessen <[email protected]>
@Flix6x Flix6x mentioned this pull request May 24, 2024
11 tasks
flexmeasures/api/v3_0/tests/test_sensor_schedules.py Outdated Show resolved Hide resolved
flexmeasures/auth/decorators.py Show resolved Hide resolved
flexmeasures/auth/decorators.py Outdated Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These have nothing to do with auth directly, but are lookup functions that rely on our planning data structure. So I guess this location isn't bad, really.

I don't see why ctx_loader should be used, as no AuthContext is touched.

@Flix6x Flix6x marked this pull request as ready for review June 8, 2024 21:54
@Flix6x Flix6x requested a review from nhoening June 8, 2024 22:05
Flix6x and others added 5 commits June 10, 2024 16:29
@Flix6x Flix6x added this to the 0.23.0 milestone Aug 19, 2024
Copy link
Contributor

@nhoening nhoening left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the improvements, this is good.
I disagree with one potential security problem in particular, like in the last review.

context_from_args = kwargs.get(ctx_arg_name)
# skip check in case (optional) argument was not passed
if context_from_args is None:
return None
Copy link
Contributor

@nhoening nhoening Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I (still) believe this should be a LookupError. Above, you skip the auth check if this looking up of a context by argument name did not work out (which I also am in favor of not doing). This could quite easily happen by programmer mistake, and could easily be overlooked during testing.

ctx_arg_name is an optional attribute, but if it is given, we need to actually use it.

Copy link
Contributor Author

@Flix6x Flix6x Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the major problem blocking my progress on this PR. How do you propose I deal with checking authorization on an optional argument (in this case coming from an optional API field)? Should I add a boolean argument to permission_required_for_context telling the argument is optional (i.e. skip if context is None) or required (i.e. raise if context is None)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not check that context_from_args has to be something if the developer gave information to look it up?

I.e. if ctx_arg_name is not None, then we expect that context_from_args is also not None, otherwise we got wrong parameterisation.

Simply put, if permission_required_for_context() has no context, we cannot say the permission is granted.

Why did you change the logic so that it could be granted? The field was optional before. Maybe it is because the flex context can contain no sensors to check? That is a case I can imagine, and maybe we could add a parameter to permission_required_for_context() that says pass_if_no_context_found or similar.

But the interpretation has to fit.

context = [context]
for ctx in context:
if isinstance(ctx, tuple):
c = ctx[0] # c[0] is the context, c[1] is its origin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's mention for ease of reading that the origin is just a string.


def load_context(
ctx_arg_pos, ctx_arg_name, ctx_loader, pass_ctx_to_loader, args, kwargs
) -> AuthModelMixin | None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this return type still valid? It can be a list or tuples with origins, right?

context = context_from_args
if context is None:
raise LookupError(f"No context could be loaded from {context_from_args}.")
return context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thought: We provide several means of looking up context(s). Should we maybe check here that all context(s) are actually a subclass of AuthModelMixin and throw a meaningful LookupError if they are not?

I just checked, and check_access does this check. So we can not do it here. But it could help debugging problems.

flex_context_loader = partial(sensor_loader, parent_key="flex-context")


def find_sensor_ids(data, parent_key="") -> list[tuple[int, str]]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type hint data?

@permission_required_for_context(
"read",
ctx_arg_name="flex_model",
ctx_loader=flex_model_loader,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add a comment: "This extracts sensors from the flex model parameter - user needs read access to them"

@nhoening nhoening modified the milestones: 0.23.0, 0.24.0 Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Read permissions of entities within flex-model or flex-context
2 participants