Python 如何在GoogleColaboratory中向拥有共享链接的用户隐藏密钥?
我编写了一个脚本,从API中提取一些数据并构建一个Excel文件。我不是开发人员,这是我写的第一个真正的程序。我在GoogleColab上托管了代码 在clear中有API密钥。我想通过谷歌硬盘共享链接与需要生成Excel文件的人共享,以便他们能够执行该文件。然而,我不希望在clear中包含API密钥,以避免企业之外的意外共享 我想知道如何隐藏这个。。。或者如何为用户提供一种在不知道密码的情况下执行文件的替代方法。我无法访问企业内部的共享Web服务器 问候Python 如何在GoogleColaboratory中向拥有共享链接的用户隐藏密钥?,python,security,google-colaboratory,Python,Security,Google Colaboratory,我编写了一个脚本,从API中提取一些数据并构建一个Excel文件。我不是开发人员,这是我写的第一个真正的程序。我在GoogleColab上托管了代码 在clear中有API密钥。我想通过谷歌硬盘共享链接与需要生成Excel文件的人共享,以便他们能够执行该文件。然而,我不希望在clear中包含API密钥,以避免企业之外的意外共享 我想知道如何隐藏这个。。。或者如何为用户提供一种在不知道密码的情况下执行文件的替代方法。我无法访问企业内部的共享Web服务器 问候 CLIENT_KEY = u'****
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=(',', ': ')))
...
尝试
getpass
。例如:
from getpass import getpass
secret = getpass('Enter the secret value: ')
然后,您可以共享笔记本,每个用户都可以输入一个不同的值,稍后您可以在笔记本中使用该值作为常规Python变量。您可以将密钥另存为Google Drive上的文件。然后将文件读入Colab 现在,您可以在Google Drive中设置访问密钥文件的权限。只有您和共享密钥文件的人才能使用它 更新 正如@efbbrown所建议的,您可以创建一个aws密钥文件并将其存储在Google Drive中,例如
[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
但现在(2020年),您不再需要pydrive
。你可以
- 打开Colab左侧的文件窗格李>
- 选择“安装驱动器”
- 通过单击“连接到谷歌硬盘”接受
- 使用下面的代码将该文件复制到Colab
~/.aws/config
。因此,您可以这样做(如果上面的文件名为aws\u config
)
要扩展@Korakot Chaovavanich的答案,以下是该解决方案的一步一步:
现在,您的AWS密钥位于两个变量中
access\u token\u key
和access\u token\u secret
我建议使用GCP的:
您可以获得一些有用的功能,如权限管理(在IAM和Admin中),您可以通过秘密版本更新密码等。。真的很有用
先决条件:
- 拥有一个谷歌云平台项目
- 在IAM&Admin中,您应该扮演角色:“机密管理器机密访问器””
- 在机密管理器中:
# 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!
测试时,不要使用真实密码或密码进行尝试
享受吧 听起来您想设置一个环境变量。我以前没有使用过GoogleColaboratory,但我会寻找一个允许您设置环境变量的功能。然后,在您的代码中,您可以
导入os
并使用os.environ[“CLIENT_KEY”]
等等。这似乎很有趣,但由于Colab基于临时环境,这是否意味着每次都要生成环境变量,wich将使密钥在代码中的其他地方可用?不能在帐户级别设置Colab环境变量是相当荒谬的。在很多方面,可乐是一个玩具。谢谢。我觉得lke给4个键来复制过去对某些人来说太复杂了。但是我可以找到一种方法来加密代码中的4个密钥,并使其仅在使用密码时可用。非常好。请记住不要稍后打印变量。在某种程度上,您刚刚使用Google身份验证来解决这个问题。用户必须通过GoogleDrive的身份验证才能工作,现在的负担是通过该服务进行身份验证——该服务是交互式的,并且需要粘贴令牌。这是非常不受欢迎的,但如果它对你有效-太好了@rjurney您有没有其他解决方案可以让用户避免将钥匙嵌入笔记本?@effbrown我使用S3。我将文件复制到那里,然后使用3 LOC中的boto3将它们下载到Colab笔记本中。通过这种方式,我可以确定文件已经上传和下载,而不是等待谷歌硬盘同步。有时,数百个其他文件在同步队列中的位置更高,我无法运行最新代码。S3解决了这个问题。目前,在一个项目中,我将凭据放在笔记本中,但一般来说,我会使用Google Drive提示用户输入凭据,或者以Google Drive的方式提示用户输入凭据。正如我在上面所评论的,这需要使用Google Drive进行交互式身份验证,这在许多情况下是不可取的。从某种程度上说,这可以解决问题,但通过使用谷歌身份验证而不是共享秘密,确实可以缓解问题。现在,尽管您必须维护一个带有凭据的共享驱动器,并向每个用户授予该共享驱动器的权限。@rjurney如果您创建一个新的colab笔记本,您可以自动将其连接到google drive(使用UI,无需复制和粘贴令牌)。
[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
!pip install -U -q PyDrive
# 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", "")
# 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!