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

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 this issue May 26, 2024 · 3 comments

Comments

@songhan89
Copy link

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):

  • OS: Mac Sonoma 14.5

Additional context
Using python 3.11

@Dylankjy
Copy link

Dylankjy commented Jul 1, 2024

+1

@terftw
Copy link

terftw commented Jul 31, 2024

facing the same issue too

@terftw
Copy link

terftw commented Jul 31, 2024

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-----"

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

No branches or pull requests

3 participants