存儲此信息最安全的方法是使用project metadata。在Flexible/ManagedVM環境中,您可以通過simple http request訪問元數據。
從谷歌的博客文章:
隨着計算引擎,集裝箱引擎和託管虛擬機,有一種神奇的網址,你可以捲曲獲得的元數據。
ManagedVMs是現在所謂「AppEngine上靈活的環境」的舊名。既然你說你在App Engine上使用Ruby,你必須使用Flexible/ManagedVMs。因此,您應該可以使用這些「神奇的網址」。
因此,要獲得在Ruby中稱爲mysecret
應用程序的祕密,你可以這樣做:
Net::HTTP.get(
URI.parse('http://metadata.google.internal/computeMetadata/v1/project/attributes/mysecret'))
(對於@joshlf)下面是如何訪問項目元AppEngine上標準環境中的Python:
# Note that the code will not work on dev_appserver,
# you will need to switch to some other mechanism
# for configuration in that environment
# Specifically the project_id will resolve to something
# compute engine API will treat as invalid
from google.appengine.api import app_identity
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
compute = discovery.build(
'compute', 'v1', credentials=GoogleCredentials.get_application_default())
def get_project_metadata(metadata_key):
project_id = app_identity.get_application_id()
project = compute.projects().get(project=project_id).execute()
for entry in project['commonInstanceMetadata']['items']:
if entry['key'] == metadata_key:
return entry['value']
return None
get_project_metadata('my_key')
我使用GPG在'.env'文件中加密環境變量並使用[dotgpg](https://github.com/ConradIrwin/dotgpg)來管理它們。您可以加密您的'app.yaml'文件並將其存儲在存儲庫中。 – p4sh4
@ p4sh4感謝您的評論。這個想法很好,但是你有沒有用CI工具(比如Circle CI)嘗試過你的想法? –
是的,CircleCI尤其如此 - 但我將測試和部署所需的環境變量添加到CircleCI本身,因爲可以以安全的方式進行測試和部署,並且我不會在那裏解密'.env'文件。 – p4sh4