Google应用引擎:隐藏Rails密钥的最佳做法?

Shi*_*ani 7 google-app-engine ruby-on-rails secret-key

我正在将我的Rails应用程序部署到GAE,其代码存储在github中.

显然,我需要隐藏我的密钥和数据库密码.

在Heroku中,我可以使用Heroku GUI非常轻松地在环境变量中设置它们,因此它不会出现在任何源代码或数据库中.

GAE怎么样?我无法在app.yaml中设置它们,因为:

  1. .gitignore不是一个选项:即使我通过.gitignore隐藏app.yaml文件或替代json文件,我必须将其保存在我的本地计算机中.这意味着只有我可以部署,我必须自己做备份.这很糟糕.
  2. 有人说我可以在数据库中存储秘密值.但我也想隐藏数据库密码.

任何的想法?

Rae*_*bal 5

我在回答类似问题时解决了这个问题。本质上,您可以创建一个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)


Fra*_*son 4

存储此信息的最安全方法是使用项目元数据。在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)