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

时间:2016-02-01 18:37:37

标签: php heroku github google-api google-calendar-api

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

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

但是,对于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文件的情况下知道如何做到这一点,我很满意。

1 个答案:

答案 0 :(得分:0)

不要承诺。说真的,这很简单。您使用heroku config variables走在正确的轨道上。实际上,即使在此处发布,您也可能想要申请新密钥。

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