如何在开源(Github + Heroku)应用程序中保护 Google API 密钥

How to protect Google API Key in an open-source (Github + Heroku) application

我正在创建一个应用程序,希望在未来几周内开源。源代码在 Github 上,如果有新提交通过 Travis CI 测试,Heroku 会自动部署代码。

在这个应用程序中,我有几个 API 密钥,我通过在我的 heroku dynos 中使用环境变量设法将它们排除在开源存储库之外。

对于 Google 服务器到服务器 API,但是,我必须有一个 .p12 文件。在 php 中,以下将验证我的客户端:

$client = new Google_Client();
$client->setApplicationName("Client_Calendar");
$service = new Google_Service_Calendar($client);

$key = file_get_contents('myKey.p12');
var_dump($key);

$cred = new Google_Auth_AssertionCredentials(
  'xxx@gserviceaccount.com',
  array('https://www.googleapis.com/auth/calendar'),
  $key
);

$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}

...

$event = $service->events->insert($calendarId, $event, $sendNotifications);

起初,我以为我可以提取 $key 变量的内容并将其插入到另一个 heroku 环境变量中,但内容是加密的。

那么,问题来了:如何保护您的 .p12 密钥在开源存储库中不被盗?

PS:我只是创建 Google 日历事件并向与会者发送通知;如果你知道不使用 .p12 文件的方法,我会洗耳恭听。

别犯。说真的,就是这么简单。 heroku config variables 你走在正确的轨道上。事实上,即使在这里发布它,您也可能想要申请一个新密钥。

有人建议将整个配置文件存储在其他可能需要您可以存储的凭据的地方。 S3 是做这类事情的好地方。 S3 也有一个很棒的 PHP 组件,用于访问 S3 存储桶。