Xii*_*ryo 16 python security google-colaboratory
我编写了一个脚本,用于从 API 中提取一些数据并构建一个 Excel 文件。我不是开发人员,这是我编写的第一个真正的程序。我在 Google Colab 上托管了代码。
有明文的 API 密钥。我想通过 Google Drive 共享链接与需要生成 Excel 文件的人共享它,以便他们可以执行它。但是,我不希望明文包含 API 密钥,以避免在企业外意外共享。
我想知道如何隐藏它......或者如何为用户提供另一种方法来在不知道密码的情况下执行文件。我无法访问企业内部的共享网络服务器。
问候
CLIENT_KEY = u'*****'
CLIENT_SECRET = u'*****'
BASE_URL = u'*****'
access_token_key = '*****'
access_token_secret = '*****'
print ('Getting user profile...',)
oauth = OAuth(CLIENT_KEY, client_secret=CLIENT_SECRET, resource_owner_key=access_token_key,
resource_owner_secret=access_token_secret)
r = requests.get(url=BASE_URL + '1/user/me/profile', auth=oauth)
print (json.dumps(r.json(), sort_keys=True, indent=4, separators=(',', ': ')))
...
Run Code Online (Sandbox Code Playgroud)
小智 25
我建议使用 GCP 的Secret Manager:
您可以获得有用的功能,例如权限管理(在 IAM 和管理中),您可以通过秘密版本更新密码等。非常有用。
先决条件:
这是使用 python 3 获取秘密的方法:
# Install the module and import it :
!pip install google-cloud-secret-manager
from google.cloud import secretmanager
# Create a Client:
client = secretmanager.SecretManagerServiceClient()
secret_name = "my-secret" # => To be replaced with your secret name
project_id = 'my-project' # => To be replaced with your GCP Project
# Forge the path to the latest version of your secret with an F-string:
resource_name = f"projects/{project_id}/secrets/{secret_name}/versions/latest"
# Get your secret :
response = client.access_secret_version(request={"name": resource_name})
secret_string = response.payload.data.decode('UTF-8')
# Tada ! you secret is in the secret_string variable!
Run Code Online (Sandbox Code Playgroud)
测试时请勿尝试使用您的真实密码或秘密。
享受 !
Bob*_*ith 11
试试getpass
。例如:
from getpass import getpass
secret = getpass('Enter the secret value: ')
Run Code Online (Sandbox Code Playgroud)
然后,您可以共享笔记本,每个用户都可以输入一个不同的值,然后您可以稍后在笔记本中将其用作常规 Python 变量。
您可以将密钥保存为 Google Drive 上的文件。然后将文件读入 Colab。
现在您可以设置访问 Google Drive 中的密钥文件的权限。只有您和您共享密钥文件的人才能使用它。
正如@efbbrown 建议的那样,您可以创建一个 aws 密钥文件并将其存储在 Google Drive 中,例如
[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Run Code Online (Sandbox Code Playgroud)
但是现在(2020 年)您不再需要pydrive
了。你可以
存储凭证的默认位置是~/.aws/config
. 所以你可以这样做(如果你上面的文件被命名aws_config
)
!mkdir -p ~/.aws
!cp "/content/drive/My Drive/aws_config" ~/.aws/config
Run Code Online (Sandbox Code Playgroud)
为了扩展@Korakot Chaovavanich 的回答,以下是该解决方案的分步说明:
[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Run Code Online (Sandbox Code Playgroud)
!pip install -U -q PyDrive
Run Code Online (Sandbox Code Playgroud)
(其中一些代码来自@wenkesj 对这个问题的回答。)
# Imports
import os
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
# Google drive authentication
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
# File params
local_save_dir = "/root/.aws"
filename = "credentials"
save_path = "{0}/{1}".format(local_save_dir, filename)
# Choose/create a local (colab) directory to store the data.
local_download_path = os.path.expanduser(local_save_dir)
try:
os.makedirs(local_download_path)
except: pass
drive_list = drive.ListFile().GetList()
f = [x for x in drive_list if x["title"] == filename][0]
print('title: %s, id: %s' % (f['title'], f['id']))
fname = os.path.join(local_download_path, f['title'])
print('downloading to {}'.format(fname))
f_ = drive.CreateFile({'id': f['id']})
f_.GetContentFile(fname)
with open(save_path) as creds:
for i, line in enumerate(creds):
if i == 1:
access_token_key = line.replace("aws_access_key_id=", "").replace("\n", "")
if i == 2:
access_token_secret = line.replace("aws_secret_access_key=", "").replace("\n", "")
Run Code Online (Sandbox Code Playgroud)
现在您的 AWS 密钥位于两个变量access_token_key
& 中access_token_secret
。
归档时间: |
|
查看次数: |
6183 次 |
最近记录: |