在 laravel 中写入 oauth 密钥文件的安全位置

Safe location to write oauth key files in laravel

我正在将我的 laravel 应用程序部署到 AWS Elastic Beanstalk,我遇到了为 laravel 护照保留 oauth 密钥的问题。

我经历了 and this。虽然 S3 选项听起来很合理,但我仍然想要一种更安全的方式,并想从 AWS 中检查秘密管理器。

自 laravel 护照 provides the option to load keys from a custom folder, I figured I could use the AWS PHP SDK to retrieve a secret key 并将其写入 storage/app/oauth-public.keystorage/app/oauth-private.key 并从那里加载护照。

这种方法在部署到 beantalk 后工作正常,但是 storage/app 文件夹是生成 oauth.*.key 文件的安全位置吗?或者有更好的 way/safer 地方吗?

下面是我在Providers/AuthServiceProvider.php

中的启动函数
public function boot()
{
    $this->registerPolicies();
    Passport::routes();
    Passport::tokensExpireIn(now()->addDays(5));

    // load keys from aws secret manager if they don't exist
    if(!file_exists(storage_path().'/app/oauth-public.key') && !file_exists(storage_path().'/app/oauth-private.key')) {
        $keys = json_decode($this->getPassportKeys());
        $public_key = implode("\n", explode('\n', $keys->PASSPORT_PUBLIC_KEY));
        Storage::put('oauth-public.key', $public_key);
        $private_key = implode("\n", explode('\n', $keys->PASSPORT_PRIVATE_KEY));
        Storage::put('oauth-private.key', $private_key);
    }
    Passport::loadKeysFrom(storage_path().'/app');
}

通常您在 AWS 上的应用程序应该是无状态。这意味着不应在实例上存储任何数据,因为它们可以随时因活动扩展、AZ 重新平衡或其他活动而被替换。

因此,通常您会将应用程序数据存储在您的实例之外。对于秘密,比如你的钥匙,好的位置可能是 SSM Parameter Store or Secrets Manager (SM).

您已经在使用 SM,我认为这很好。如果您将它们本地存储在 storage/app 中,则只要您部署新版本的应用程序,此文件夹就会被删除。所以你必须确保你总是从 SM 获得密钥。您也可以考虑将文件存储在内存中,而不是硬盘驱动器上。这将允许您更快地获取它们,而无需从本地存储中读取它们。