`aws secretsmanager list-secrets` 命令到 return 个秘密并按标签过滤它们
`aws secretsmanager list-secrets` command to return secrets and filter them by tag
如何调用 aws secretsmanager list-secrets
命令并按标签过滤秘密?我在这里看不到这样的例子:https://docs.aws.amazon.com/cli/latest/reference/secretsmanager/list-secrets.html
另外,Amazon 的文档似乎有误。它在那个页面上说 --max-items
但实际上应该是 --max-results
。也没有提到如何在该 wiki 页面上进行过滤。
[原文:2019 年 12 月]
您可以使用jq,例如:
aws secretsmanager list-secrets \
| jq '.SecretList[] | select((.Tags[]|select(.Key=="Name")|.Value) | test("^Production$|^Staging$"))'
您还可以使用 awscli 的内置查询选项,例如:
aws secretsmanager list-secrets \
--query "SecretList[?Tags[?Key=='Name' && Value=='Production']]"
您可以将布尔测试与 awscli 的内置查询选项一起使用,例如:
aws secretsmanager list-secrets \
--query "SecretList[?Tags[?Key=='Name' && (Value=='Production' || Value='Staging')]]"
这是使用 Python 和 boto3 的解决方案的概要:
from functools import partial
import boto3
def filter_tags(key, values, secret):
for tag in secret['Tags']:
if tag['Key'] == key and tag['Value'] in values:
return True
return False
sm = boto3.client('secretsmanager')
paginator = sm.get_paginator('list_secrets')
secrets_list_iterator = paginator.paginate()
filter_production = partial(filter_tags, 'Name', ['Production', 'Staging'])
for secrets in secrets_list_iterator:
for s in filter(filter_production, secrets['SecretList']):
print(s['Name'], s['Tags'])
[更新时间:2021 年 1 月]
aws secretsmanager list-secrets 命令现在支持通过 --filters
选项进行过滤。但是 .. 我建议你不要使用它,除非你了解它的实际工作原理(见下文)并且你会从它的特定实现中受益。
这是一个示例,说明如何使用它来过滤名称 以 开头的机密:
aws secretsmanager list-secrets \
--filters Key=name,Values=Production
注意--filters
选项不能做完全匹配,只能做'begins with'匹配,所以使用时要谨慎。如果您有名称为 Production 和 Production-Old 的密文,两者都将被退回。这可能不是您想要的,因此在这种情况下请使用上面描述的原始客户端查询。
这是一个示例,说明如何使用它来过滤名称 以 生产或登台开头的机密:
aws secretsmanager list-secrets \
--filters Key=name,Values=Production,Staging
这是一个示例,说明如何使用它来过滤具有 开始 阶段 或 标签值的标签键的秘密开始于 dev:
aws secretsmanager list-secrets \
--filters Key=tag-key,Values=stage Key=tag-value,Values=dev
注意:--filters
选项实现逻辑或,而不是逻辑与。
这是一个 boto3 示例,过滤 以 开头的标签键名称 或 以 开头的标签值 生产或分期:
import boto3
sm = boto3.client('secretsmanager')
res = sm.list_secrets(Filters=[
{ 'Key': 'tag-key', 'Values': ['Name'] },
{ 'Key': 'tag-value', 'Values': ['Production', 'Staging'] },
])
for secret in res['SecretList']:
print(secret['Name'], secret['Tags'])
如何调用 aws secretsmanager list-secrets
命令并按标签过滤秘密?我在这里看不到这样的例子:https://docs.aws.amazon.com/cli/latest/reference/secretsmanager/list-secrets.html
另外,Amazon 的文档似乎有误。它在那个页面上说 --max-items
但实际上应该是 --max-results
。也没有提到如何在该 wiki 页面上进行过滤。
[原文:2019 年 12 月]
您可以使用jq,例如:
aws secretsmanager list-secrets \
| jq '.SecretList[] | select((.Tags[]|select(.Key=="Name")|.Value) | test("^Production$|^Staging$"))'
您还可以使用 awscli 的内置查询选项,例如:
aws secretsmanager list-secrets \
--query "SecretList[?Tags[?Key=='Name' && Value=='Production']]"
您可以将布尔测试与 awscli 的内置查询选项一起使用,例如:
aws secretsmanager list-secrets \
--query "SecretList[?Tags[?Key=='Name' && (Value=='Production' || Value='Staging')]]"
这是使用 Python 和 boto3 的解决方案的概要:
from functools import partial
import boto3
def filter_tags(key, values, secret):
for tag in secret['Tags']:
if tag['Key'] == key and tag['Value'] in values:
return True
return False
sm = boto3.client('secretsmanager')
paginator = sm.get_paginator('list_secrets')
secrets_list_iterator = paginator.paginate()
filter_production = partial(filter_tags, 'Name', ['Production', 'Staging'])
for secrets in secrets_list_iterator:
for s in filter(filter_production, secrets['SecretList']):
print(s['Name'], s['Tags'])
[更新时间:2021 年 1 月]
aws secretsmanager list-secrets 命令现在支持通过 --filters
选项进行过滤。但是 .. 我建议你不要使用它,除非你了解它的实际工作原理(见下文)并且你会从它的特定实现中受益。
这是一个示例,说明如何使用它来过滤名称 以 开头的机密:
aws secretsmanager list-secrets \
--filters Key=name,Values=Production
注意--filters
选项不能做完全匹配,只能做'begins with'匹配,所以使用时要谨慎。如果您有名称为 Production 和 Production-Old 的密文,两者都将被退回。这可能不是您想要的,因此在这种情况下请使用上面描述的原始客户端查询。
这是一个示例,说明如何使用它来过滤名称 以 生产或登台开头的机密:
aws secretsmanager list-secrets \
--filters Key=name,Values=Production,Staging
这是一个示例,说明如何使用它来过滤具有 开始 阶段 或 标签值的标签键的秘密开始于 dev:
aws secretsmanager list-secrets \
--filters Key=tag-key,Values=stage Key=tag-value,Values=dev
注意:--filters
选项实现逻辑或,而不是逻辑与。
这是一个 boto3 示例,过滤 以 开头的标签键名称 或 以 开头的标签值 生产或分期:
import boto3
sm = boto3.client('secretsmanager')
res = sm.list_secrets(Filters=[
{ 'Key': 'tag-key', 'Values': ['Name'] },
{ 'Key': 'tag-value', 'Values': ['Production', 'Staging'] },
])
for secret in res['SecretList']:
print(secret['Name'], secret['Tags'])