首页 > 解决方案 > 使用 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 个选项之一:

我一直在尝试做的是创建一个不带参数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);

标签: pythonauthenticationdevopsdatadog

解决方案


API 仅通过 API/App 密钥进行验证,这些密钥几乎是管理员用户。没有任何方法可以将用户级身份验证添加到端点。

您需要在您的终端进行某种身份验证来证明用户是谁,然后将其与 DD 上的管理员列表进行比较,然后,如果它们是合法的,则调用您的 API 脚本。也许跳过所有只有 DD 管理员用户可以访问并从那里运行 API 脚本的机器。


推荐阅读