Heroku and Laravel Passport - laravel

I try to install my app on heroku. This app is a php-laravel app with the "passport" for the authentication. All is running fine in my local machine (mac os).
When I try to do a simple 'post' with postman, I have this error :
2018-03-17T17:05:22.059708+00:00 app[web.1]: [17-Mar-2018 17:05:22 UTC] [2018-03-17 17:05:22] production.ERROR: Key path "file:///app/storage/oauth-private.key" does not exist or is not readable {"exception":"[object] (LogicException(code: 0): Key path \"file:///app/storage/oauth-private.key\" does not exist or is not readable at /app/vendor/league/oauth2-server/src/CryptKey.php:45)"} []
To setup passport, I generated the keys with :
php artisan passport:install
And I see the keys in my database in heroku. So the command worked properly.
So what is this error ?
I tried also to regenerate the keys, to stop and restart the application. Without successes.
Thanks for your suggestions.
Merci
Dominique
EDIT : in fact, the key files are not generated in the folder app/storage, that's why there is this error. But why these files are not generated ?

The solution is here: https://github.com/laravel/passport/issues/267
Add these few lines into your composer.json under the "scripts" property, then commit and deploy into Heroku:
"post-install-cmd": [
"php artisan clear-compiled",
"chmod -R 777 storage",
"php artisan passport:keys"
]
But, after that you have to delete the keys from the table "oauth-clients", then regenerate these keys with :
php artisan passport:install

About the #Dom answer, It will log out your users with every deployment, so if you're really using Heroku and not Dokku (as in my case), I recommend you to generate the keys by using that command: php artisan passport:keys and then via Nano copy the keys generated in storage/oauth-public.key and storage/oauth-private.key into multiline env variables, then you can use this post install script in composer.json:
"post-install-cmd": [
"php artisan clear-compiled",
"chmod -R 777 storage",
"echo -n $OAUTH_PRIVATE_KEY > storage/oauth-private.key",
"echo -n $OAUTH_PUBLIC_KEY > storage/oauth-public.key" ]
That will regenerate the keys from ENV with every deployment and keep your users logged in.
If that solution doesn't work, you could still remove '/storage/*.key' line from .gitignore

Laravel Passport has a configuration that allows to set public and private keys as environment variables.
You can run php artisan vendor:publish --tag=passport-config on your local machine and commit the change.
Then set PASSPORT_PRIVATE_KEY and PASSPORT_PUBLIC_KEY on Heroku config.
Found from this blog

My solution was quite straight forward:
go to your .gitignore file
comment out /storage/*.key
re-deploy to heroku
It appears that the oauth-keys are ignored by default in Laravel (v.7)

Loading Keys From The Environment
Alternatively, you may publish Passport's configuration file using the vendor:publish Artisan command:
php artisan vendor:publish --tag=passport-config
After the configuration file has been published, you may load your application's encryption keys by defining them as environment variables:
PASSPORT_PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
<private key here>
-----END RSA PRIVATE KEY-----"
PASSPORT_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----
<public key here>
-----END PUBLIC KEY-----"
Passport documentation

I have a better solution to the problem that does not require making your keys public
connect to your heroku account via you terminal
Run heroku ps:exec
Run php artisan passport:keys

Related

Error when running database seeder through Heroku

I am unable to setup a packages folder structure with db:seed on Laravel framework.
I was able to deploy and migrate the regular Laravel project on heroku,
but I am having issue trying to use the class argument for the artisan db:seed command.
my folders structure:
root-dir/packages/clientname/projectname/app
root-dir/packages/clientname/projectname/config
root-dir/packages/clientname/projectname/database/seeders
root-dir/packages/clientname/projectname/resources
In composer I have autoload > psr-4 mapped to the packages seeders folder
"autoload": {
"psr-4": {
"ClientName\\ProjectName\\Database\\Seeders\\": "packages/clientname/projectname/database/seeders/"
}
},
running the following command
$ heroku run --app client-project php artisan db:seed --class=\\ClientName\\ProjectName\\Database\\Seeders\\DatabaseSeeder
returns an error
In Container.php line 811:
Target class [Database\Seeders\ClientNameProjectNameDatabaseSeedersDatabaseSeeder] does not exist.
Note the backslashes have been removed. How can I resolve this issue?
Checking out the source for this command, it appears there's a fair bit of escaping going on. I would suggest either double-escaping your backslashes like this:
heroku run --app client-project php artisan db:seed --class=\\\\ClientName\\\\ProjectName\\\\Database\\\\Seeders\\\\DatabaseSeeder
Or try some quoting around the command:
heroku run --app client-project 'php artisan db:seed --class=\\ClientName\\ProjectName\\Database\\Seeders\\DatabaseSeeder'

Laravel deployment with deployer fails updating shared/.env file

Im quite new to laravel and the concept of CI/CD. But i have invested the last 24 hours to get something up and running. Actually i'm using gitlab.com as repo. There i have configured the CI/CD functionality.
The deployments should be done to SRV1 which has configured its corresponding user with a cert. The SRV1 should then clone the necessary files from the gitlab repo by using deployer. The gitlab repo also has the public key from SRV1 user. This chain is working quite good
The problem now is, that i want to update the .env file placed under shared/.env using the .gitlab-ci.yml file. But for some reasons, this won't work:
Here is my file: https://pastebin.com/RsjQf9L3
Basically, the deployment is done by this command sequence:
- cp .env.staging .env
- php artisan key:generate
- php artisan config:clear
- php artisan config:cache
- php artisan deploy dev.morast.ch -s upload
I thought, that wen doing
- cp .env.staging .env
It will replace the content of shared/.env with the content of .env.staging
In fact, the file gets replaced (i see it due to the date code) but the content don't equals the .env.staging file.
What do i wrong? Thanks for your help.
By the way, i always get an
The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths.
Maybe this could also be related to my .gitlab-ci.yml file?
thanks!

Key path "file:///app/storage/oauth-private.key" does not exist or is not readable [duplicate]

I try to install my app on heroku. This app is a php-laravel app with the "passport" for the authentication. All is running fine in my local machine (mac os).
When I try to do a simple 'post' with postman, I have this error :
2018-03-17T17:05:22.059708+00:00 app[web.1]: [17-Mar-2018 17:05:22 UTC] [2018-03-17 17:05:22] production.ERROR: Key path "file:///app/storage/oauth-private.key" does not exist or is not readable {"exception":"[object] (LogicException(code: 0): Key path \"file:///app/storage/oauth-private.key\" does not exist or is not readable at /app/vendor/league/oauth2-server/src/CryptKey.php:45)"} []
To setup passport, I generated the keys with :
php artisan passport:install
And I see the keys in my database in heroku. So the command worked properly.
So what is this error ?
I tried also to regenerate the keys, to stop and restart the application. Without successes.
Thanks for your suggestions.
Merci
Dominique
EDIT : in fact, the key files are not generated in the folder app/storage, that's why there is this error. But why these files are not generated ?
The solution is here: https://github.com/laravel/passport/issues/267
Add these few lines into your composer.json under the "scripts" property, then commit and deploy into Heroku:
"post-install-cmd": [
"php artisan clear-compiled",
"chmod -R 777 storage",
"php artisan passport:keys"
]
But, after that you have to delete the keys from the table "oauth-clients", then regenerate these keys with :
php artisan passport:install
About the #Dom answer, It will log out your users with every deployment, so if you're really using Heroku and not Dokku (as in my case), I recommend you to generate the keys by using that command: php artisan passport:keys and then via Nano copy the keys generated in storage/oauth-public.key and storage/oauth-private.key into multiline env variables, then you can use this post install script in composer.json:
"post-install-cmd": [
"php artisan clear-compiled",
"chmod -R 777 storage",
"echo -n $OAUTH_PRIVATE_KEY > storage/oauth-private.key",
"echo -n $OAUTH_PUBLIC_KEY > storage/oauth-public.key" ]
That will regenerate the keys from ENV with every deployment and keep your users logged in.
If that solution doesn't work, you could still remove '/storage/*.key' line from .gitignore
Laravel Passport has a configuration that allows to set public and private keys as environment variables.
You can run php artisan vendor:publish --tag=passport-config on your local machine and commit the change.
Then set PASSPORT_PRIVATE_KEY and PASSPORT_PUBLIC_KEY on Heroku config.
Found from this blog
My solution was quite straight forward:
go to your .gitignore file
comment out /storage/*.key
re-deploy to heroku
It appears that the oauth-keys are ignored by default in Laravel (v.7)
Loading Keys From The Environment
Alternatively, you may publish Passport's configuration file using the vendor:publish Artisan command:
php artisan vendor:publish --tag=passport-config
After the configuration file has been published, you may load your application's encryption keys by defining them as environment variables:
PASSPORT_PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
<private key here>
-----END RSA PRIVATE KEY-----"
PASSPORT_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----
<public key here>
-----END PUBLIC KEY-----"
Passport documentation
I have a better solution to the problem that does not require making your keys public
connect to your heroku account via you terminal
Run heroku ps:exec
Run php artisan passport:keys

Problem with executing laravel migrations on Elastic Beanstalk

I deployed my laravel application using Elastic beanstalk, and I need to execute php artisan:migrate command on the remote database.
Based on Maximilian's tutorial I created init.config file inside .ebextensions with contents:
container_commands:
01initdb:
command: "php artisan migrate"
The status of the deployment is Healthy, but it didn't create any table!
any clues, please?
run php artisan config:cache then run migrate command
As you did not include any relevant logs. I am guessing you need to run your migration within the staging folder.
An example of how you can run migrations:
04_run_migrations:
command: "php artisan migrate --force"
cwd: "/var/app/staging"
leader_only: true
--force is needed cause php artisan migrate asks you if you are sure to run it on a production environment
leader_only is needed if you use horizontal scaling.
Source:
https://github.com/rennokki/laravel-aws-eb/blob/master/.ebextensions/01_deploy.config

Refreshing again and again shows error message

I was playing with Laravel by refreshing the page 10 times in a second and did this many times continuously. I got the below error
The only supported ciphers are AES-128-CBC and AES-256-CBC with the
correct key lengths.
This is a test case which can occur anytime by end user also. Is there any way to fix it?
I already have key in my env file. This issue occurs only when i refresh the page again and again,
I already checked the answer but could not help
As posted by one of the users at this issue at GitHub,
Under a heavy load of requests, two async requests are made, and during the second request the .env file is locked, so you receive the error only for that request.
To solve this, you can create a cache using below command, which will bypass your .env file on further requests.
php artisan config:cache
Run following command from application folder:
$ php artisan key:generate
First make the key if doesn't exists by running following command.
php artisan key:generate
Next clear your config cache.
php artisan config:clear
Propably application cached your config without app-key.

Resources