@cloud-copilot/iam-shrink

中文文档
0.1.89发布于 2026/08/29AGPL-3.0-or-laterTypeScriptESM + CJS构建来源已验证近 30 天下载 3761

Shrink IAM Policies

原始使用文档
文档目录12 节
  1. Getting Small While Staying Sane
  2. Existing Wildcards
  3. Removing Preexisting Wildcards
  4. Use in Browser
  5. Use in CLI
  6. Installation
  7. Help
  8. Shrink IAM Actions
  9. Configuring iterations
  10. Specify Access Levels
  11. Other CLI Options
  12. Use in TypeScript/Node

Shrink IAM Actions

NPM Version License: AGPL v3 GuardDog Known Vulnerabilities

Built in the Unix philosophy, this is a small tool with two goals:

  1. Shrink IAM actions lists by creating patterns that match only the actions specified and no others.
  2. Do #1 in a way that won't make your coworkers hate you.

Using Action Wildcards is not recommended, sometimes there are IAM Limits you can't get around. This tool helps you stay within those limits.

Getting Small While Staying Sane

IAM Actions are camel cased into a number of words. For example:

  • s3:GetObject -> "Get" "Object"
  • s3:GetObjectTagging -> "Get" "Object" "Tagging"

IAM Shrink will only replace one word at a time and will never replace part of a word. So for instance s3:GetObject will never get shrunk to something like s3:*et*. This is to balance size reduction with readability.

Existing Wildcards

If your input already contains wildcards, they will be preserved. For example:

cat "s3:Get*Tagging" | iam-shrink
# Output
s3:Get*Tagging

Existing wildcards will be removed under three conditions:

  1. If the wildcard does not match any actual actions and effectively does nothing. For instance if you input s3:Get*NonExistentAction, it will be removed.
  2. If the wildcard is redundant or can be replaced with a more general wildcard. For instance if you input s3:GetObject* and s3:Get*, only s3:Get* will be kept.
  3. If the shrink process finds a smaller wildcard that replaces the existing one. For instance if you input s3:GetObject*, but during the shrink process iam-shrink finds is valid s3:Get*, s3:GetObject* will be removed.

Removing Preexisting Wildcards

If you want to remove all existing wildcards from you policy you can use iam-expand before using iam-shrink.

curl "https://government-secrets.s3.amazonaws.com/secret-policy.json" | iam-expand | iam-shrink

Use in Browser

https://iam.cloudcopilot.io/tools/iam-shrink

Use in CLI

Installation

You can install it globally. This also works in the default AWS CloudShell!

npm install -g @cloud-copilot/iam-shrink

Depending on your configuration sudo may be required to install globally.

Help

iam-shrink --help

Shrink IAM Actions

Pass in Argument

It's unlikely that you will pass in on the CLI a number of actions after the command name, but you can. You'll need a large number of actions for it to be practical, so it's mostly for automation.

Usage: iam-shrink s3:GetBucketTagging s3:GetJobTagging s3:GetObjectTagging s3:GetObjectVersionTagging s3:GetStorageLensConfigurationTagging
# Output
s3:Get*Tagging

Read from stdin

If no actions are passed as arguments, the CLI will read from stdin.

cat "s3:GetBucketTagging s3:GetJobTagging s3:GetObjectTagging s3:GetObjectVersionTagging s3:GetStorageLensConfigurationTagging" | iam-shrink
# Output
s3:Get*Tagging

Shrink JSON input

If the input is a valid json document, the CLI will find every instance of Action and NotAction that is an array of strings and shrink them.

Given policy.json

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "groundstation:GetAgentConfiguration",
        "groundstation:GetConfig",
        "groundstation:GetDataflowEndpointGroup",
        "groundstation:GetMinuteUsage",
        "groundstation:GetMissionProfile",
        "groundstation:GetSatellite",
        "groundstation:ListConfigs",
        "groundstation:ListContacts",
        "groundstation:ListDataflowEndpointGroups",
        "groundstation:ListEphemerides",
        "groundstation:ListGroundStations",
        "groundstation:ListMissionProfiles",
        "groundstation:ListSatellites",
        "groundstation:ListTagsForResource",
        "s3:GetBucketTagging",
        "s3:GetJobTagging",
        "s3:GetObjectTagging",
        "s3:GetObjectVersionTagging",
        "s3:GetStorageLensConfigurationTagging"
      ],
      "Resource": "*"
    },
    {
      "Effect": "Deny",
      "NotAction": [
        "organizations:DeleteOrganization",
        "organizations:DeleteOrganizationalUnit",
        "organizations:DeletePolicy",
        "organizations:DeleteResourcePolicy",
        "organizations:LeaveOrganization"
      ],
      "Resource": "*"
    }
  ]
}
cat policy.json | iam-shrink > smaller-policy.json

Gives this file in smaller-policy.json

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["groundstation:List*", "groundstation:Get*", "s3:Get*Tagging"],
      "Resource": "*"
    },
    {
      "Effect": "Deny",
      "NotAction": ["organizations:Delete*", "organizations:Leave*"],
      "Resource": "*"
    }
  ]
}

Configuring iterations

By default, the CLI will do two iterations of shrinking. This generally does a good balance between reducing size and maintaining readability. This can be adjusted with the --iterations flag.

Assuming the AWS Read Only policy is in readonly.json

You can change this with the `--iterations` flag.

```bash
# Default two iterations
cat readonly.json | iam-shrink | wc -m
# 61305 characters

# Increasing iterations
cat readonly.json | iam-shrink --iterations 3 | wc -m
# 45983 characters
cat readonly.json | iam-shrink --iterations 4 | wc -m
# 43654 characters
cat readonly.json | iam-shrink --iterations 5 | wc -m
# 43336 characters

# Unlimited iterations until the policy cannot be further reduced
cat readonly.json | iam-shrink --iterations 0 | wc -m
# 43281 characters

If you want to shrink the policy as much as possible, you can use --iterations 0. This will keep shrinking the policy until it can't be reduced any further.

Specify Access Levels

AWS has Access Levels that are assigned to all permissions in IAM. They are:

  • List
  • Read
  • Write
  • Tagging
  • Permissions management

By default iam-shrink will shrink all actions regardless of their access level. You can specify a list of access levels using the --levels argument to shrink only those actions.

# Shrink all actions
cat big-policy.json | iam-shrink

# Shrink only Read, List, and Tagging actions. Write, and Permissions management actions will be included without any wildcards
cat big-policy.json | iam-shrink --levels read list tagging

Other CLI Options

  • --remove-sids: Remove all Sid fields from the policy.
  • --remove-whitespace: Remove all whitespace from the output.

Use in TypeScript/Node

You can use the shrink function in your own code.

import { shrink } from '@cloud-copilot/iam-shrink'

const actions = [
  's3:GetBucketTagging',
  's3:GetJobTagging',
  's3:GetObjectTagging',
  's3:GetObjectVersionTagging',
  's3:GetStorageLensConfigurationTagging'
]

const shrunk = await shrink(actions)
console.log(shrunk)
// [ s3:Get*Tagging ]

You can specify the number of iterations as well.

import { shrink } from '@cloud-copilot/iam-shrink'

const bigListOfActions = getBigListOfActions()

const smallerList = await shrink(bigListOfActions, { iterations: 3 })
console.log(shrunk)

版本与发布记录

@cloud-copilot/iam-shrink 共发布 90 个版本,首次发布于 2024/10/02;过去 12 个月发布了 43 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.89latest2026/08/292 个月前
0.1.882026/08/222 个月前
0.1.872026/08/152 个月前
0.1.862026/08/083 个月前
0.1.852026/08/013 个月前
0.1.842026/07/253 个月前

关于 @cloud-copilot/iam-shrink 的常见问题

@cloud-copilot/iam-shrink 是什么?

Shrink IAM Policies

如何安装 @cloud-copilot/iam-shrink?

在项目目录运行 npm install @cloud-copilot/iam-shrink,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@cloud-copilot/iam-data(>=0.12.0 <1.0.0)、@cloud-copilot/iam-expand(>=0.11.25 <1.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 iam-shrink,可以用 npx iam-shrink 直接运行。

@cloud-copilot/iam-shrink 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cloud-copilot/iam-shrink 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@cloud-copilot/iam-shrink 可以商用吗?

@cloud-copilot/iam-shrink 使用 AGPL-3.0-or-later 许可。最严格的 Copyleft:除分发外,通过网络向用户提供基于它的服务也需要开源对应源码。闭源 SaaS 使用前务必评估。以上为概括,不构成法律意见,请以许可原文为准。

@cloud-copilot/iam-shrink 的下载量有多少?

根据 npm 官方下载统计,@cloud-copilot/iam-shrink 在 2026/10/10 – 2026/11/08 这 30 天内被下载 3,761 次,日均约 125 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cloud-copilot/iam-shrink 还在维护吗?

最新版本 0.1.89 发布于 2026/08/29(2 个月前);至今共发布 90 个版本,其中过去 12 个月发布了 43 个;首次发布于 2024/10/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。