python - 使用 Datadog 的 Python API,是否可以获取您自己的凭据,如果可以,如何获取?
问题描述
我正在尝试创建一个仅在管理员用户运行它并返回异常时才有效的函数。但是,我不完全确定如何获取自己的凭据。也就是说,我只想检查当前用户的access_role
.
为了得到其他人的access_role
,我可以简单地运行api.User.get_all()
,它返回如下内容:
{'handle': 'someone@somesite.com', 'name': None, 'access_role': 'st', 'verified': False, 'disabled': False, 'is_admin': False, 'role': None, 'email': 'Someone@SomeSite.com', 'icon': 'https://secure.gravatar.com/avatar/000000000000000000000000000000000?s=00&z=retro'}
该access_role
字段显示 3 个选项之一:
adm
~ 管理员用户st
~ 标准用户ro
~ 只读用户
我一直在尝试做的是创建一个不带参数get_me()
并返回所有这些信息的函数,但对于调用它的人来说。
就get_me()
功能而言,我有时间在网上寻找资源。
# Function that returns the current users information
def get_me():
# If there is a command to do this, I'd put it here...
return api.User.some_command();
不过,希望我可以将最终结果应用于这样的函数delete()
:
# Deletes a dashboard iff the user is an admin user
def delete(self, idno):
cred = self.get_me();
if cred['access_role'] == 'adm':
print 'Dashboard successfully deleted!'
return api.Dashboard.delete(idno);
else:
print 'Not authorized to delete this Dashboard!'
return api.Dashboard.get(idno);
解决方案
API 仅通过 API/App 密钥进行验证,这些密钥几乎是管理员用户。没有任何方法可以将用户级身份验证添加到端点。
您需要在您的终端进行某种身份验证来证明用户是谁,然后将其与 DD 上的管理员列表进行比较,然后,如果它们是合法的,则调用您的 API 脚本。也许跳过所有只有 DD 管理员用户可以访问并从那里运行 API 脚本的机器。
推荐阅读
- npm - 如何将 npm 参数传递给 dotnet publish?
- python - 要根据随机输入字母在字典中查找单词,这段代码会高效执行吗?
- c# - 为什么 protobuf-net 反序列化的 DataType 对象不显示 Enum 值?
- jenkins - 根据投票结果构建 Jenkins Job
- android - 5 英寸设备从 sw320dp 获取尺寸,而不是 sw480dp
- assembly - 16 位汇编:不能取消引用某些寄存器
- sql - 该索引是否有用?
- javascript - 主线程忙时可以进行垃圾收集吗?
- django-rest-framework - 使用 @api_view FBV 接收 url 参数
- linux - 如何在 nslcd.service 之后启动 docker.service?