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

[api] Rest API for PKI app #455

Merged
merged 33 commits into from
Jul 25, 2021
Merged

[api] Rest API for PKI app #455

merged 33 commits into from
Jul 25, 2021

Conversation

ManishShah120
Copy link
Member

@ManishShah120 ManishShah120 commented May 12, 2021

Closes #462

@coveralls
Copy link

coveralls commented May 12, 2021

Coverage Status

Coverage increased (+0.03%) to 98.673% when pulling a020222 on rest-api-for-pki-app into 41badf5 on master.

Copy link
Member

@atb00ker atb00ker left a comment

Choose a reason for hiding this comment

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

Going in the correct direction, one question:

openwisp_controller/pki/api/views.py Outdated Show resolved Hide resolved
@atb00ker atb00ker added this to In progress in [GSoC 2021] OpenWISP REST API May 17, 2021
@ManishShah120 ManishShah120 force-pushed the rest-api-for-pki-app branch 2 times, most recently from ea2152d to 61de479 Compare May 20, 2021 12:51
@ManishShah120 ManishShah120 marked this pull request as ready for review May 27, 2021 08:29
@ManishShah120 ManishShah120 changed the title [WIP] [api] Rest API for PKI app [api] Rest API for PKI app May 27, 2021
@ManishShah120 ManishShah120 moved this from In progress to Needs Review in [GSoC 2021] OpenWISP REST API May 27, 2021
@atb00ker atb00ker moved this from Needs Review to In progress in [GSoC 2021] OpenWISP REST API May 31, 2021
@ManishShah120
Copy link
Member Author

@ManishShah120 why in the cert detail endpoint the name of the CA is shown while in the list endpoint it's the ID?

I would always show the ID because it's the only way to be super sure someone is dealing with a specific object.

Let's be consistent.

@nemesisdesign, I tried to keep it similar to the admin panel, But, Fixed it now. 😊

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

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

@ManishShah120 I merged the latest master in and some query checks failed can you please double check?

@ManishShah120
Copy link
Member Author

@ManishShah120 I merged the latest master in and some query checks failed can you please double check?

Fixed it 👍 @nemesisdesign

atb00ker
atb00ker previously approved these changes Jul 17, 2021
Copy link
Member

@atb00ker atb00ker left a comment

Choose a reason for hiding this comment

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

LGTM! 😄

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

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

So here's some important actions that can be performed from the admin but seem missing from the API.

  • renew cert
  • renew CA
  • revoke certificate

To keep it simple, we can introduce these actions for singular objects (leaving the implementation for performing renew/revoke on multiple objects for a future phase if needed at all).

The models have method for each of these actions so the implementation should be pretty easy.

The API urls can be like:

POST /cert/{id}/renew/
POST /ca/{id}/renew/
POST /cert/{id}/revoke/

if data.get('certificate') and data.get('private_key'):
data = get_import_data(instance)
data.update({'ca': instance.ca})
return data
Copy link
Member

Choose a reason for hiding this comment

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

this looks mostly the same as the Ca serializer

def validate_validity_end(self, value):
if value is None:
value = default_cert_validity_end()
return value
Copy link
Member

Choose a reason for hiding this comment

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

these two method also are almost identical

Copy link
Member

Choose a reason for hiding this comment

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

can you please move these 3 methods above to a BaseListSerializer which inherits from BaseSerializer and then use it as a base for CertListSerializer and CatListSerializer?

The line which adds data.update({'ca': instance.ca}) can be added with super()

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

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

almost there

instance.renew()
return Response(
{_("CA '{}' renewed successfully".format(instance.name))}, status=302,
)
Copy link
Member

Choose a reason for hiding this comment

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

return the serialized data with the updated data and 200 as status code, applies to the other endpoints as well

Copy link
Member Author

Choose a reason for hiding this comment

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

This is done. 👍

@ManishShah120
Copy link
Member Author

@nemesisdesign I have made the requested changes for the revoke and renew endpoints, but there's one minor issue, i.e., I have used the CaDetailSerializer & CertDetailSerializer for serializing the instance data for returning it, but since in this serializers some of the fields are set to write so these fields are also displayed in the response page.

However, this will not be a an issue, in production. Still I will see if I can fix it.

@nemesifier
Copy link
Member

@nemesisdesign I have made the requested changes for the revoke and renew endpoints, but there's one minor issue, i.e., I have used the CaDetailSerializer & CertDetailSerializer for serializing the instance data for returning it, but since in this serializers some of the fields are set to write so these fields are also displayed in the response page.

However, this will not be a an issue, in production. Still I will see if I can fix it.

Sure, better fix this and ensure the serializers are correct, you simply need to create a new serializers inheriting the serializer you're using now and removing the write_only fields.

@ManishShah120
Copy link
Member Author

Sure, better fix this and ensure the serializers are correct, you simply need to create a new serializers inheriting the serializer you're using now and removing the write_only fields.

Done, Fixed it. 👍

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

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

Almost there, let's do some more code improvement and then we're done.

return data


def CertList_fields(fields=None):
Copy link
Member

Choose a reason for hiding this comment

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

two doubts here:

  • why the default argument is None? it looks like there should be no default argument
  • why this naming? Capital letter is used for classes, I think this should be named get_cert_list_fields

Eg:

def get_cert_list_fields(fields):
    pass

return value


def CaDetail_fields(fields=None):
Copy link
Member

Choose a reason for hiding this comment

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

same here as for get_cert_list_fields

return value


def CertDetail_fields(fields=None):
Copy link
Member

Choose a reason for hiding this comment

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

same here

def validate_validity_end(self, value):
if value is None:
value = default_cert_validity_end()
return value
Copy link
Member

Choose a reason for hiding this comment

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

can you please move these 3 methods above to a BaseListSerializer which inherits from BaseSerializer and then use it as a base for CertListSerializer and CatListSerializer?

The line which adds data.update({'ca': instance.ca}) can be added with super()

@ManishShah120
Copy link
Member Author

Hi @nemesisdesign have implemented the BaseListSerializer, Is the implementation correct.

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

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

I found another weird thing: why does the list endpoint provide the info about extensions but the details endpoint does not?

@nemesifier nemesifier moved this from Needs Review to In progress in [GSoC 2021] OpenWISP REST API Jul 24, 2021
@ManishShah120
Copy link
Member Author

I found another weird thing: why does the list endpoint provide the info about extensions but the details endpoint does not?

@nemesisdesign currently I have also removed the passphrase field from the detail endpoint, Should I leave it as as it is or should I make it visible too in the detail endpoint.

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

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

I found another weird thing: why does the list endpoint provide the info about extensions but the details endpoint does not?

@nemesisdesign currently I have also removed the passphrase field from the detail endpoint, Should I leave it as as it is or should I make it visible too in the detail endpoint.

Passphrase shall be write_only: a020222.

With this change I think we're done here.

@nemesifier nemesifier merged commit 3f8e4c6 into master Jul 25, 2021
@nemesifier nemesifier deleted the rest-api-for-pki-app branch July 25, 2021 17:07
[GSoC 2021] OpenWISP REST API automation moved this from In progress to Done Jul 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

[api] Create API endpoints for the models of PKI App
5 participants