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

feat: add import/export of roles with permissions #1662

Merged
merged 1 commit into from
Jul 7, 2021

Conversation

krsnik93
Copy link
Contributor

Description

This PR introduces two new CLI commands:

  • flask fab export-roles
  • flask fab import-roles

In combination, they allow for quick migration of custom roles with permissions from one host to another.

I initially tried using Model2SchemaConverter to create schemas for sqla models. After making a couple of changes, I was able to use it to serialize nested models. However, I was not able to de-serialize back from this result, so I resorted to creating individual marshmallow schemas.

I don't think export_roles and import_roles should live in SecurityManager classes, but not sure where to put them. Would appreciate feedback.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Is CRUD MVC related.
  • Is Auth, RBAC security related.
  • Changes the security db schema.
  • Introduces new feature
  • Removes existing feature

Copy link
Owner

@dpgaspar dpgaspar left a comment

Choose a reason for hiding this comment

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

Looking good, but missing:

  • tests, unit-tests and cli
  • User's can define builtin roles these should be included also

@krsnik93
Copy link
Contributor Author

@dpgaspar regarding builtin roles, I can see that they get inserted into the database when the SecurityManager is initialized:

for role_name in self.builtin_roles:
self.add_role(role_name)

So as long as the user initializes the app or runs it, the builtin roles will be in the database as well and will get exported. I tried adding an entry for FAB_ROLES in config.py and running flask run followed by flask fab export-roles and the resulting file had the builtin roles included as well.

@krsnik93 krsnik93 force-pushed the export-roles branch 3 times, most recently from 3926388 to 0e27c1c Compare June 29, 2021 18:31
@codecov
Copy link

codecov bot commented Jul 1, 2021

Codecov Report

Merging #1662 (6838e2b) into master (efc6944) will increase coverage by 0.38%.
The diff coverage is 87.69%.

❗ Current head 6838e2b differs from pull request most recent head 2c1c6af. Consider uploading reports for the commit 2c1c6af to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1662      +/-   ##
==========================================
+ Coverage   75.45%   75.83%   +0.38%     
==========================================
  Files          54       55       +1     
  Lines        7870     8025     +155     
==========================================
+ Hits         5938     6086     +148     
- Misses       1932     1939       +7     
Flag Coverage Δ
python 75.83% <87.69%> (+0.38%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
flask_appbuilder/api/manager.py 92.30% <ø> (ø)
flask_appbuilder/models/decorators.py 0.00% <ø> (ø)
flask_appbuilder/security/sqla/models.py 97.93% <ø> (ø)
flask_appbuilder/security/views.py 55.43% <9.52%> (-0.54%) ⬇️
flask_appbuilder/base.py 83.85% <50.00%> (ø)
flask_appbuilder/utils/base.py 82.35% <85.71%> (+2.35%) ⬆️
flask_appbuilder/security/manager.py 72.54% <92.68%> (+0.46%) ⬆️
flask_appbuilder/__init__.py 100.00% <100.00%> (ø)
flask_appbuilder/api/__init__.py 97.65% <100.00%> (+0.01%) ⬆️
flask_appbuilder/baseviews.py 89.92% <100.00%> (+0.05%) ⬆️
... and 7 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b9b3259...2c1c6af. Read the comment docs.

Copy link
Owner

@dpgaspar dpgaspar left a comment

Choose a reason for hiding this comment

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

looking good, left a couple of comments regarding tests


export_result = src_cli_runner.invoke(export_roles, [f"--path={filename}"])
self.assertEqual(export_result.exit_code, 0)
self.assertTrue(os.path.exists(filename))
Copy link
Owner

Choose a reason for hiding this comment

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

Would be great if we could assert the content of the file also.
nit: can you split these into 2 distinct tests? by assert the content you can add the file has a fixture

self.assertEqual(
len(dst_app_builder.sm.get_all_roles()),
len(src_app_builder.sm.get_all_roles()),
)
Copy link
Owner

Choose a reason for hiding this comment

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

can you also assert the permissions on the DB after the import?

@krsnik93 krsnik93 force-pushed the export-roles branch 3 times, most recently from 5c25d3e to b52bf62 Compare July 5, 2021 10:57
@krsnik93
Copy link
Contributor Author

krsnik93 commented Jul 5, 2021

@dpgaspar I think there might be some issues with the code coverage tool as it flags lines that are certainly covered...

Copy link
Owner

@dpgaspar dpgaspar left a comment

Choose a reason for hiding this comment

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

LGTM!
Would be great if you could drop a few lines here: https://flask-appbuilder.readthedocs.io/en/latest/cli.html

@krsnik93
Copy link
Contributor Author

krsnik93 commented Jul 6, 2021

@dpgaspar do you agree about the coverage being off or should I take a look?

@krsnik93
Copy link
Contributor Author

krsnik93 commented Jul 6, 2021

Hi @dpgaspar the link does not work for me, says "Uri expired"

@krsnik93
Copy link
Contributor Author

krsnik93 commented Jul 6, 2021

@dpgaspar you are right that nose isn't picking up all the tests, in fact it's not picking up test_fab_cli.py at all. I am working on a fix.

@krsnik93
Copy link
Contributor Author

krsnik93 commented Jul 6, 2021

@dpgaspar looking good now

@dpgaspar
Copy link
Owner

dpgaspar commented Jul 6, 2021

@krsnik93 great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants