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: generate API Credentials in Admin Portal #1833

Closed
wants to merge 1 commit into from

Conversation

yuanyuan-git-tech
Copy link
Contributor

@yuanyuan-git-tech yuanyuan-git-tech commented Aug 1, 2023

https://2u-internal.atlassian.net/browse/ENT-7415
Merge checklist:

  • Any new requirements are in the right place (do not manually modify the requirements/*.txt files)
    • base.in if needed in production but edx-platform doesn't install it
    • test-master.in if edx-platform pins it, with a matching version
    • make upgrade && make requirements have been run to regenerate requirements
  • make static has been run to update webpack bundling if any static content was updated
  • ./manage.py makemigrations has been run
    • Checkout the Database Migration Confluence page for helpful tips on creating migrations.
    • Note: This must be run if you modified any models.
      • It may or may not make a migration depending on exactly what you modified, but it should still be run.
    • This should be run from either a venv with all the lms/edx-enterprise requirements installed or if you checked out edx-enterprise into the src directory used by lms, you can run this command through an lms shell.
      • It would be ./manage.py lms makemigrations in the shell.
  • Version bumped
  • Changelog record added
  • Translations updated (see docs/internationalization.rst but also this isn't blocking for merge atm)

Post merge:

  • Tag pushed and a new version released
    • Note: Assets will be added automatically. You just need to provide a tag (should match your version number) and title and description.
  • After versioned build finishes in GitHub Actions, verify version has been pushed to PyPI
    • Each step in the release build has a condition flag that checks if the rest of the steps are done and if so will deploy to PyPi.
      (so basically once your build finishes, after maybe a minute you should see the new version in PyPi automatically (on refresh))
  • PR created in edx-platform to upgrade dependencies (including edx-enterprise)
    • This must be done after the version is visible in PyPi as make upgrade in edx-platform will look for the latest version in PyPi.
    • Note: the edx-enterprise constraint in edx-platform must also be bumped to the latest version in PyPi.

@yuanyuan-git-tech
Copy link
Contributor Author

yuanyuan-git-tech commented Aug 7, 2023

A new flag on the Enterprise Customer django admin screen under the subheader “Integration and learning platform settings” called “Allow generation of API credentials”:
Screenshot 2023-08-07 at 9 49 02 AM
Enterprise API endpoints:
Screenshot 2023-08-14 at 10 35 21 AM

@yuanyuan-git-tech yuanyuan-git-tech requested a review from a team August 7, 2023 14:10
@@ -26,7 +26,7 @@ jobs:
- name: Build package
run: python setup.py sdist bdist_wheel
- name: Publish to PyPi
uses: pypa/gh-action-pypi-publish@master
uses: pypa/gh-action-pypi-publish@release/v1
Copy link
Contributor

Choose a reason for hiding this comment

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

any chance I could get context on this change?

Copy link
Contributor

Choose a reason for hiding this comment

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

i'm also curious why this change was made?

Copy link
Contributor

@alex-sheehan-edx alex-sheehan-edx left a comment

Choose a reason for hiding this comment

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

awesome start. A couple of pieces of feed back and I want to sit and think about your tests for a second but we're definitely on our way.

Get the enterprise customer name given an user id.
"""
try:
return str(EnterpriseCustomerUser.objects.get(user_id=user_id).enterprise_customer.name)
Copy link
Contributor

Choose a reason for hiding this comment

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

[Question] this has a small bug when the user is assigned to multiple customers but I'm curious if this breaks with the .get

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it that one user corresponds to one EnterpriseCustomerUser? If not, how should i determine the value of Application's name for the current user?

Copy link
Contributor

Choose a reason for hiding this comment

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

Alex is right here, user_id probably isnt enough here to disambiguate which object you want. I believe there is 1 EnterpriseCustomerUser for every enterprise that an LMS user has access to.

# Verifies the requesting user is connected to an enterprise that has API credentialing bool set to True
user = request.user
if not has_api_credentials_enabled(user.id):
return Response({'detail': 'Can not access to API credentials.'}, status=status.HTTP_405_METHOD_NOT_ALLOWED)
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT no need for the to ie -

Can not access API credential viewset

Also I don't think 405 is quite the right status code here... 403 might be a little closer https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses

# Verifies the requesting user is connected to an enterprise that has API credentialing bool set to True
user = request.user
if not has_api_credentials_enabled(user.id):
return Response({'detail': 'Can not access to API credentials.'}, status=status.HTTP_405_METHOD_NOT_ALLOWED)
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT same message changes as above

Copy link
Contributor

Choose a reason for hiding this comment

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

as well as HTTP status code changes

# If an application already exists for the user, throw a 409.
queryset = self.get_queryset()
if queryset:
return Response({'detail': 'Application exists.'}, status=status.HTTP_409_CONFLICT)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure we want to return an error here. If the user hits the endpoint I think we just want to generate a new pair of credentials

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 saw throw a 409 if an application already exists for the user for the CREATE endpoint in the ticket. Should i generate a new pair of credentials?

feature_role_object, __ = EnterpriseFeatureRole.objects.get_or_create(name=role_name)
EnterpriseFeatureUserRoleAssignment.objects.get_or_create(user=user, role=feature_role_object)

application = Application.objects.create(
Copy link
Contributor

Choose a reason for hiding this comment

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

when you create the application object, does it automatically come with a client ID and secret?

URL: /enterprise/api/v1/enterprise_customer_api_credentials/{user_id}
"""
response = {'message': 'partial_update function is not offered in this path.'}
return Response(response, status=status.HTTP_403_FORBIDDEN)
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this should be the 405 HTTP status code

URL: /enterprise/api/v1/enterprise_customer_api_credentials/{user_id}
"""
if not has_api_credentials_enabled(request.user.id):
return Response({'detail': 'Can not access to API credentials.'}, status=status.HTTP_405_METHOD_NOT_ALLOWED)
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

@@ -0,0 +1,327 @@
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

I see, I think my commit might have been added to your PR, we can fix this

self.assertEqual(updated_instance.name, self.data['name'])
self.assertEqual(updated_instance.authorization_grant_type, self.data['authorization_grant_type'])
self.assertEqual(updated_instance.client_type, self.data['client_type'])
self.assertEqual(updated_instance.redirect_uris, self.data['redirect_uris'])
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe you need to add a new line

@yuanyuan-git-tech yuanyuan-git-tech changed the base branch from master to 2.0.11 August 9, 2023 16:49
@yuanyuan-git-tech yuanyuan-git-tech changed the base branch from 2.0.11 to master August 9, 2023 16:49
Copy link
Contributor

@johnnagro johnnagro left a comment

Choose a reason for hiding this comment

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

lets huddle tomorrow. i can help you address the merge conflicts and we can discuss a couple feedback items

@@ -26,7 +26,7 @@ jobs:
- name: Build package
run: python setup.py sdist bdist_wheel
- name: Publish to PyPi
uses: pypa/gh-action-pypi-publish@master
uses: pypa/gh-action-pypi-publish@release/v1
Copy link
Contributor

Choose a reason for hiding this comment

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

i'm also curious why this change was made?

Get the enterprise customer name given an user id.
"""
try:
return str(EnterpriseCustomerUser.objects.get(user_id=user_id).enterprise_customer.name)
Copy link
Contributor

Choose a reason for hiding this comment

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

Alex is right here, user_id probably isnt enough here to disambiguate which object you want. I believe there is 1 EnterpriseCustomerUser for every enterprise that an LMS user has access to.

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Aug 23, 2023
@openedx-webhooks
Copy link

Thanks for the pull request, @yuanyuan-git-tech! Please note that it may take us up to several weeks or months to complete a review and merge your PR.

Feel free to add as much of the following information to the ticket as you can:

  • supporting documentation
  • Open edX discussion forum threads
  • timeline information ("this must be merged by XX date", and why that is)
  • partner information ("this is a course on edx.org")
  • any other information that can help Product understand the context for the PR

All technical communication about the code itself will be done via the GitHub pull request interface. As a reminder, our process documentation is here.

Please let us know once your PR is ready for our review and all tests are green.

⚠️ We can't start reviewing your pull request until you've submitted a signed contributor agreement or indicated your institutional affiliation. Please see the CONTRIBUTING file for more information. If you've signed an agreement in the past, you may need to re-sign. See The New Home of the Open edX Codebase for details.

Once you've signed the CLA, please allow 1 business day for it to be processed. After this time, you can re-run the CLA check by editing the PR title. If the problem persists, you can tag the @openedx/cla-problems team in a comment on your PR for further assistance.

@johnnagro
Copy link
Contributor

attempting to repackage and rework this #1854

@johnnagro johnnagro closed this Aug 29, 2023
@openedx-webhooks
Copy link

@yuanyuan-git-tech Even though your pull request wasn’t merged, please take a moment to answer a two question survey so we can improve your experience in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
open-source-contribution PR author is not from Axim or 2U
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants