Shi*_*ani 7 google-app-engine ruby-on-rails secret-key
我正在将我的Rails应用程序部署到GAE,其代码存储在github中.
显然,我需要隐藏我的密钥和数据库密码.
在Heroku中,我可以使用Heroku GUI非常轻松地在环境变量中设置它们,因此它不会出现在任何源代码或数据库中.
GAE怎么样?我无法在app.yaml中设置它们,因为:
任何的想法?
我在回答类似问题时解决了这个问题。本质上,您可以创建一个credentials.yaml文件并将app.yaml其导入到app.yaml. 这将允许您将凭据指定为 ENV 变量,同时保留忽略 git 中的文件的能力。该includes:标签允许您在app.yaml.
例子app.yaml:
runtime: go
api_version: go1
env_variables:
FIST_VAR: myFirstVar
includes:
- credentials.yaml
Run Code Online (Sandbox Code Playgroud)
credentials.yaml:
env_variables:
SECOND_VAR: mySecondVar
API_KEY: key-123
Run Code Online (Sandbox Code Playgroud)
存储此信息的最安全方法是使用项目元数据。在Flexible/ManagedVM环境中,您可以通过简单的http请求访问元数据。
来自谷歌博客文章:
借助计算引擎、容器引擎和托管虚拟机,您可以通过 CURL 获取元数据。
ManagedVM 是现在“AppEngine 灵活环境”的旧名称。既然您说您在 App Engine 上使用 Ruby,那么您必须使用灵活/托管虚拟机。因此您应该能够使用这些“神奇的 URL”。
因此,要获取在 Ruby 中调用的应用程序机密,mysecret您可以执行以下操作:
Net::HTTP.get(
URI.parse('http://metadata.google.internal/computeMetadata/v1/project/attributes/mysecret'))
Run Code Online (Sandbox Code Playgroud)
(针对 @joshlf)以下是如何使用Python 访问 AppEngine 标准环境上的项目元数据:
# 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')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1380 次 |
| 最近记录: |