# @cloud-copilot/iam-shrink 中文文档

规范地址：https://npmcn.com/package/%40cloud-copilot/iam-shrink/

版本：0\.1\.89
npm 元数据更新时间：2026-08-29T12:12:19.613Z
中文译文更新时间：2026-11-10T02:21:00.374Z
软件包许可：AGPL-3\.0-or-later

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40cloud-copilot/iam-shrink
原文对照：https://npmcn.com/package/%40cloud-copilot/iam-shrink/?lang=original
项目主页：https://github.com/cloud-copilot/iam-shrink#readme
源码仓库：https://github.com/cloud-copilot/iam-shrink

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：269 kB，105 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：1 个
- peer 依赖：@cloud-copilot/iam-data \>=0\.12\.0 \<1\.0\.0，@cloud-copilot/iam-expand \>=0\.11\.25 \<1\.0\.0
- 首次发布：2024-10-01T18:58:11.287Z
- 最新版本发布：2026-08-29T12:12:19.283Z
- 近 30 天下载量：3761（2026-10-10 至 2026-11-08，npm 官方下载统计）
- 已发布版本数：90（近 12 个月 43 个）

## 中文简介

缩进IAM政策

## 安装

```sh
npm install @cloud-copilot/iam-shrink
```

也可以使用：`pnpm add @cloud-copilot/iam-shrink`、`yarn add @cloud-copilot/iam-shrink`、`bun add @cloud-copilot/iam-shrink`

## README

# 缩小 IAM 动作

[![NPM Version](https://img.shields.io/npm/v/@cloud-copilot/iam-shrink.svg?logo=nodedotjs)](https://www.npmjs.com/package/@cloud-copilot/iam-shrink) [![License: AGPL v3](https://img.shields.io/github/license/cloud-copilot/iam-shrink)](https://npmcn.com/package/%40cloud-copilot/iam-shrink/LICENSE.txt) [![GuardDog](https://github.com/cloud-copilot/iam-shrink/actions/workflows/guarddog.yml/badge.svg)](https://github.com/cloud-copilot/iam-shrink/actions/workflows/guarddog.yml) [![Known Vulnerabilities](https://snyk.io/test/github/cloud-copilot/iam-shrink/badge.svg?targetFile=package.json&style=flat-square)](https://snyk.io/test/github/cloud-copilot/iam-shrink?targetFile=package.json)

由Unix哲学构建,这是一个小工具,有两个目标:

1.  缩小 IAM 动作列表,通过创建只匹配指定动作且没有其它动作的图案.
2.  做一号,不会让你的同事讨厌你.

不推荐使用动作通关卡, 有时有 [IAM 限制](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_iam-quotas.html) 你走不动的 这个工具可以帮助你保持这些极限.

## 留在圣所的同时获得小的

IAM Actions以骆驼为例被分成多个单词. 例如:

-   `s3:GetObject` - "得到" "对象"
-   `s3:GetObjectTagging` - "得到" "对象" "拖曳"

行政会议 收缩只会一次替换一个单词,永远不会替换部分单词. 举个例子 `s3:GetObject` 永远不会被缩小到类似的东西 `s3:*et*`。这是为了平衡缩小规模和可读性.

## 已存在的通配符

如果您的输入中已经包含通配符, 它们将被保存 。 例如:

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

现有的通配符将在三个条件下被去除:

1.  如果通配符与任何实际动作不相匹配,并且有效无所作为. 例如,如果输入 `s3:Get*NonExistentAction`,它将被移除.
2.  如果通配符是多余的,或者可以用更通俗的通配符来取而代之. 例如,如果输入 `s3:GetObject*` 财务报告和审定财务报表 `s3:Get*`,仅限 `s3:Get*` 将保留.
3.  如果收缩过程找到一个更小的通配符来取代现有的通配符. 例如,如果输入 `s3:GetObject*`,但在收缩过程中 iam-shrink 发现是有效的 `s3:Get*`, (中文(简体) ), `s3:GetObject*` 将被删除.

## 删除已存在的通配符

如果您想要从策略中删除所有已有通配符, 您可以使用 [缩放](https://github.com/cloud-copilot/iam-expand) 在使用 iam-shrink 之前.

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

## 在浏览器中使用

[https://iam.cloudcopilot.io/tools/iam-shrink 页面存档备份,存于互联网档案馆](https://iam.cloudcopilot.io/tools/iam-shrink)

## 用于 CLI

### 安装

你可以在全球安装 这在默认的 AWS CloudShell 中也是有效的!

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

_根据您的配置, 可能需要在全球安装 sudo ._

### 帮助

```bash
iam-shrink --help
```

### 缩小 IAM 动作

#### 在参数中通过

在命令名称之后你不可能通过 CLI 一些动作,但你可以。 需要大量动作才能实用,所以大多用于自动化.

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

#### 从 stdin 读取

如果不通过任何行动作为论据,CLI将从stdin读取.

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

#### 收缩 JSON 输入

如果输入是有效的json文档,则CLI将找到每个实例 `Action` 财务报告和审定财务报表 `NotAction` 这是一系列的字符串并缩小它们.

鉴于 `policy.json`

```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": "*"
    }
  ]
}
```

```bash
cat policy.json | iam-shrink > smaller-policy.json
```

将此文件输入 `smaller-policy.json`

```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": "*"
    }
  ]
}
```

### 配置重现

默认情况下,CLI会进行两次迭接收缩. 这一般在缩小体积和保持可读性之间起到良好的平衡. 这一点可以随同 `--iterations` 旗帜.

假设 [AWS 只读策略](https://docs.aws.amazon.com/aws-managed-policy/latest/reference/ReadOnlyAccess.html) 进入 `readonly.json`

````bash
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
````

如果你想尽可能缩小保险单,你可以使用 `--iterations 0`这将持续缩小政策,直到不能进一步削减.

## 指定访问级别

AWS已经 [访问级别](https://docs.aws.amazon.com/service-authorization/latest/reference/reference_policies_actions-resources-contextkeys.html#actions_table) 被分配到IAM中的所有权限。 它们是:

-   `List`
-   `Read`
-   `Write`
-   `Tagging`
-   `Permissions management`

默认 iam- shrink 将会缩小所有动作, 不管它们的访问级别。 您可以使用 `--levels` 仅仅缩小这些行动的论据.

```bash
# 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

```

## 其他 CLI 选项

-   `--remove-sids`: 删除全部 `Sid` 字段.
-   `--remove-whitespace`: 从输出中删除所有空白.

## 在类型脚本/节点中使用

你可以使用自己代码中的收缩功能.

```typescript
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 ]
```

您也可以指定重复次数 .

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

const bigListOfActions = getBigListOfActions()

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

## 常见问题

### @cloud-copilot/iam-shrink 是什么？

缩进IAM政策

### 如何安装 @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 和提交记录判断。

