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

The stripe api key placement is very confusing #2055

Open
jTiKey opened this issue May 7, 2024 · 14 comments
Open

The stripe api key placement is very confusing #2055

jTiKey opened this issue May 7, 2024 · 14 comments
Labels

Comments

@jTiKey
Copy link

jTiKey commented May 7, 2024

So, your docs say to put them in .env.
If you do that, the server start showing warnings:

?: (djstripe.I002) Your keys are defined in the settings files and are also in the admin. You can now add and manage them directly from the django admin.
	HINT: We suggest adding STRIPE_TEST_SECRET_KEY and STRIPE_LIVE_SECRET_KEY directly from the Django Admin. And removing them from the settings files.

If I remove them from the .env then the webhook breaks:

   File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/webhooks.py", line 207, in from_request
     stripe_account = StripeModel._find_owner_account(data=data)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/base.py", line 344, in _find_owner_account
     return Account.get_or_retrieve_for_api_key(api_key)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/account.py", line 147, in get_or_retrieve_for_api_key
    apikey_instance, _ = APIKey.objects.get_or_create_by_api_key(api_key)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/api.py", line 42, in get_or_create_by_api_key
     key_type, livemode = get_api_key_details_by_prefix(secret)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/api.py", line 28, in get_api_key_details_by_prefix
     raise InvalidStripeAPIKey(f"Invalid API key: {api_key!r}")
 djstripe.exceptions.InvalidStripeAPIKey: Invalid API key: ''`

Software versions

Dj-Stripe version: 2.8.4
Python version: 3.11.8
Django version:4.2.11
Stripe API version: default
Database type and version: postgres 15

@jTiKey jTiKey added the bug label May 7, 2024
@hyusetiawan
Copy link

I am having the same problem here, the way it's set up is rather confusing and not pythonic (there should only be 1 way of doing things). This also happens for DJSTRIPE_WEBHOOK_SECRET, do we add it in the settings.py or do we add it through webhook here: https://github.com/dj-stripe/dj-stripe/blob/master/docs/usage/webhooks.md?

@jleclanche
Copy link
Member

The settings are deprecated and they're only there for compatibility with older versions. Everything is in database.

@jTiKey
Copy link
Author

jTiKey commented May 15, 2024

@jleclanche What is this AI generated response? Do you care to read the whole thing, not only one line? If I used my time to report a bug, why can't you put some effort into reading issues before closing them?

Your docs say to use the settings for the API keys.
The webhook stops functioning if there is no api keys in the settings. If you looks into your source code it literally uses the settings api keys there.

@jleclanche
Copy link
Member

@jTiKey The docs are outdated, this is a known issue (#1841) unrelated to this particular issue, sorry for lack of clarity here but please don't be hostile.

In any case, I understand they right now are saying something incorrect on the website - use the in-database api keys and you shouldn't be getting this error. If you are still getting it in that setup, then something else is at play and feel free to reopen.

@jleclanche
Copy link
Member

jleclanche commented May 15, 2024

If you looks into your source code it literally uses the settings api keys there.

That part is the legacy compatibility piece of code which creates API keys in database from the settings, if those are set. Make sure they are not set. This code will be gone in 3.0

@jTiKey
Copy link
Author

jTiKey commented May 15, 2024

I cannot reopen an issue you closed.

I'm sure they are not set and I'm getting this error:


`File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/webhooks.py", line 207, in from_request
     stripe_account = StripeModel._find_owner_account(data=data)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/base.py", line 344, in _find_owner_account
     return Account.get_or_retrieve_for_api_key(api_key)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/account.py", line 147, in get_or_retrieve_for_api_key
    apikey_instance, _ = APIKey.objects.get_or_create_by_api_key(api_key)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/api.py", line 42, in get_or_create_by_api_key
     key_type, livemode = get_api_key_details_by_prefix(secret)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/.heroku/python/lib/python3.11/site-packages/djstripe/models/api.py", line 28, in get_api_key_details_by_prefix
     raise InvalidStripeAPIKey(f"Invalid API key: {api_key!r}")
 djstripe.exceptions.InvalidStripeAPIKey: Invalid API key: ''

@kennell
Copy link

kennell commented May 30, 2024

@jleclanche What is the reasoning with storing API keys in the DB? This is a rather unusual approach and does not align with security best practices.

@jTiKey
Copy link
Author

jTiKey commented May 30, 2024

@jleclanche What is the reasoning with storing API keys in the DB? This is a rather unusual approach and does not align with security best practices.

The ability to have multiple API keys work. But yeah, I'd rather have that as an optional option and let people use the env keys without the db

@abe-101
Copy link
Sponsor Member

abe-101 commented May 30, 2024

For what its worth the docs for the latest version 2.8 is now available at dj-stripe.dev

@abe-101
Copy link
Sponsor Member

abe-101 commented May 30, 2024

Having the API keys in the db allows the application to act on behalf of multiple stripe accounts (multiple API keys)

@jTiKey
Copy link
Author

jTiKey commented May 30, 2024

For what its worth the docs for the latest 2.8 is now available at dj-stripe.dev

Seems outdated, since @jleclanche said using .env is obsolete.

@abe-101
Copy link
Sponsor Member

abe-101 commented May 30, 2024

For what its worth the docs for the latest 2.8 is now available at dj-stripe.dev

Seems outdated, since @jleclanche said using .env is obsolete.

Im not saying its up to date

Im simply saying that the docs from version 2.8 is available on the website

Until recently the website only had version 2.5

@kavdev kavdev reopened this Jun 3, 2024
@kavdev
Copy link
Member

kavdev commented Jun 3, 2024

Reopening until we have a bugfix or a clear "cannot reproduce"

@kavdev
Copy link
Member

kavdev commented Jun 3, 2024

See also #2060

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

No branches or pull requests

6 participants