opengovsg / sgid-client-python

The official Python client for sgID.
MIT License
3 stars 0 forks source link

Bug: `convert_to_pkcs8` returns error. Failed to import private key. Check that privateKey is a valid PKCS1 or PKCS8 key. #53

Open songhan89 opened 6 months ago

songhan89 commented 6 months ago

Describe the bug convert_to_pkcs8 in sgid python library returns error.

  File "/Users/songhanwong/Projects/sgid-flask/sgid-client-python/examples/flask/env/lib/python3.11/site-packages/flask/__main__.py", line 3, in <module>
    main()
  File "/Users/songhanwong/Projects/sgid-flask/sgid-client-python/examples/flask/env/lib/python3.11/site-packages/flask/cli.py", line 1063, in main
    cli.main()
  File "/Users/songhanwong/Projects/sgid-flask/sgid-client-python/examples/flask/env/lib/python3.11/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/Users/songhanwong/Projects/sgid-flask/sgid-client-python/examples/flask/env/lib/python3.11/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/songhanwong/Projects/sgid-flask/sgid-client-python/examples/flask/env/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/songhanwong/Projects/sgid-flask/sgid-client-python/examples/flask/env/lib/python3.11/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/songhanwong/Projects/sgid-flask/sgid-client-python/examples/flask/env/lib/python3.11/site-packages/click/decorators.py", line 92, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/songhanwong/Projects/sgid-flask/sgid-client-python/examples/flask/env/lib/python3.11/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/songhanwong/Projects/sgid-flask/sgid-client-python/examples/flask/env/lib/python3.11/site-packages/flask/cli.py", line 911, in run_command
    raise e from None
  File "/Users/songhanwong/Projects/sgid-flask/sgid-client-python/examples/flask/env/lib/python3.11/site-packages/flask/cli.py", line 897, in run_command
    app = info.load_app()
          ^^^^^^^^^^^^^^^
  File "/Users/songhanwong/Projects/sgid-flask/sgid-client-python/examples/flask/env/lib/python3.11/site-packages/flask/cli.py", line 312, in load_app
    app = locate_app(import_name, None, raise_if_not_found=False)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/songhanwong/Projects/sgid-flask/sgid-client-python/examples/flask/env/lib/python3.11/site-packages/flask/cli.py", line 218, in locate_app
    __import__(module_name)
  File "/Users/songhanwong/Projects/sgid-flask/sgid-client-python/examples/flask/app.py", line 28, in <module>
    sgid_client = SgidClient(
                  ^^^^^^^^^^^
  File "/Users/songhanwong/Projects/sgid-flask/sgid-client-python/examples/flask/env/lib/python3.11/site-packages/sgid_client/SgidClient.py", line 67, in __init__
    self.private_key = convert_to_pkcs8(private_key)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/songhanwong/Projects/sgid-flask/sgid-client-python/examples/flask/env/lib/python3.11/site-packages/sgid_client/util.py", line 23, in convert_to_pkcs8
    raise Exception(Errors.PRIVATE_KEY_IMPORT) from exc
Exception: Failed to import private key. Check that privateKey is a valid PKCS1 or PKCS8 key

The same private key is working in TypeScript sgid library.

To Reproduce Steps to reproduce the behavior:

  1. Follow the python sgid-client tutorial
  2. Update the .env file with your sgid project credentials.
  3. run the flask application with python -m flask run

Expected behavior The same credentials are used in the Typescript sgid library and it worked there for ts.

Desktop (please complete the following information):

Additional context Using python 3.11

Dylankjy commented 5 months ago

+1

terftw commented 4 months ago

facing the same issue too

terftw commented 4 months ago

I did some testing and found that the format in your env variables should be declared in the format below in order for the SGID client to be initialized, hope this helps

SGID_PUBLIC_KEY="-----BEGIN PUBLIC KEY----- your public key -----END PUBLIC KEY-----" SGID_PRIVATE_KEY="-----BEGIN PRIVATE KEY----- your private key -----END PRIVATE KEY-----"