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


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

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

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

但是,对于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 配置变量方面走在正确的轨道上。事实上,即使在这里发布它,你也可能想要请求一个新的密钥。

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