zufardhiyaulhaq/safekubectl
GitHub: zufardhiyaulhaq/safekubectl
一个 kubectl 包装器工具,在执行 delete、apply、drain 等危险操作前发出警告并要求确认,防止误操作造成集群事故。
Stars: 3 | Forks: 0
# safekubectl
[](https://github.com/zufardhiyaulhaq/safekubectl/actions/workflows/ci.yml)
[](https://github.com/zufardhiyaulhaq/safekubectl/actions/workflows/release.yml)
一个 kubectl 包装器,在执行危险操作之前会发出警告并提示确认。
## 功能
- 在执行危险操作(delete、apply、patch、edit、drain、exec、cordon、taint、rollout)之前发出警告
- 可配置的确认模式(confirm 或 warn-only)
- 受保护的 namespace 和 cluster 始终需要确认
- 针对危险操作的审计日志
- 通过 YAML 配置文件进行完全配置
## 安装说明
### 下载二进制文件
从 [GitHub Releases](https://github.com/zufardhiyaulhaq/safekubectl/releases) 页面下载最新的版本。
```
# Linux (amd64)
curl -LO https://github.com/zufardhiyaulhaq/safekubectl/releases/latest/download/safekubectl-linux-amd64.tar.gz
tar -xzf safekubectl-linux-amd64.tar.gz
sudo mv safekubectl /usr/local/bin/safekubectl
# Linux (arm64)
curl -LO https://github.com/zufardhiyaulhaq/safekubectl/releases/latest/download/safekubectl-linux-arm64.tar.gz
tar -xzf safekubectl-linux-arm64.tar.gz
sudo mv safekubectl /usr/local/bin/safekubectl
# macOS (Intel)
curl -LO https://github.com/zufardhiyaulhaq/safekubectl/releases/latest/download/safekubectl-darwin-amd64.tar.gz
tar -xzf safekubectl-darwin-amd64.tar.gz
sudo mv safekubectl /usr/local/bin/safekubectl
# macOS (Apple Silicon)
curl -LO https://github.com/zufardhiyaulhaq/safekubectl/releases/latest/download/safekubectl-darwin-arm64.tar.gz
tar -xzf safekubectl-darwin-arm64.tar.gz
sudo mv safekubectl /usr/local/bin/safekubectl
```
### 从源码构建
```
# Clone 仓库
git clone https://github.com/zufardhiyaulhaq/safekubectl.git
cd safekubectl
# Build
go build -o safekubectl .
# 安装到 PATH(可选)
sudo mv safekubectl /usr/local/bin/
```
### 使用 Go Install
```
go install github.com/zufardhiyaulhaq/safekubectl/cmd/safekubectl@latest
```
## 使用说明
将 `safekubectl` 作为 `kubectl` 的直接替代品使用:
```
# 安全操作直接通过,无需提示
safekubectl get pods
safekubectl describe deployment nginx
# 危险操作显示警告并需要确认
safekubectl delete pod nginx -n production
```
### 示例输出
```
⚠️ DANGEROUS OPERATION DETECTED
├── Operation: delete
├── Resource: pod/nginx
├── Namespace: production
└── Cluster: prod-us-east-1
Proceed? [y/N]:
```
## 配置说明
配置文件位置:`~/.safekubectl/config.yaml`
你可以使用 `SAFEKUBECTL_CONFIG` 环境变量覆盖配置路径:
```
export SAFEKUBECTL_CONFIG=/path/to/config.yaml
```
### 默认配置
如果不存在配置文件,safekubectl 将使用以下默认值:
```
# 确认模式:"confirm"(需要 y/N)或 "warn-only"(显示警告并继续)
mode: confirm
# 被视为危险的操作
dangerousOperations:
- delete
- apply
- patch
- edit
- update
- rollout
- drain
- exec
- cordon
- taint
# 无论何种模式都始终需要确认的 Namespaces
protectedNamespaces:
- kube-system
# 无论何种模式都始终需要确认的 Clusters/contexts
protectedClusters: []
# Audit 日志配置
audit:
enabled: false
path: ~/.safekubectl/audit.log
```
### 配置选项
#### `mode`
| 值 | 描述 |
|-------|-------------|
| `confirm` | 显示警告并需要 `y/N` 确认(默认) |
| `warn-only` | 显示警告并自动继续执行 |
注意:受保护的 namespace 和 cluster 始终需要确认,即使在 `warn-only` 模式下也是如此。
#### `dangerousOperations`
会触发警告的 kubectl 操作列表。默认包括:
- `delete` - 删除资源
- `apply` - 应用配置更改
- `patch` - 修补资源
- `edit` - 就地编辑资源
- `update` - 更新资源
- `rollout` - rollout 操作(restart、undo 等)
- `drain` - 排空 node
- `exec` - 在 container 中执行命令
- `cordon` - 将 node 标记为不可调度
- `taint` - 给 node 添加 taint
#### `protectedNamespaces`
始终需要确认的 namespace,即使在 `warn-only` 模式下:
```
protectedNamespaces:
- kube-system
- production
- prod
```
#### `protectedClusters`
始终需要确认的 cluster context:
```
protectedClusters:
- prod-us-east-1
- prod-eu-west-1
```
#### `audit`
启用审计日志以追踪危险操作:
```
audit:
enabled: true
path: ~/.safekubectl/audit.log
```
审计日志格式:
```
[2024-01-15T10:30:00+00:00] EXECUTED | operation=delete resource=pod/nginx namespace=production cluster=prod-us-east-1 confirmed=true command="delete pod nginx -n production"
[2024-01-15T10:31:00+00:00] DENIED | operation=delete resource=deployment/web namespace=production cluster=prod-us-east-1 confirmed=false command="delete deployment web -n production"
```
## 示例配置
### 生产环境安全配置
```
mode: confirm
dangerousOperations:
- delete
- apply
- patch
- edit
- update
- rollout
- drain
- exec
- cordon
- taint
protectedNamespaces:
- kube-system
- production
- prod
- default
protectedClusters:
- prod-us-east-1
- prod-us-west-2
- prod-eu-west-1
audit:
enabled: true
path: ~/.safekubectl/audit.log
```
### 开发环境配置(较宽松)
```
mode: warn-only
dangerousOperations:
- delete
- drain
protectedNamespaces:
- kube-system
protectedClusters: []
audit:
enabled: false
```
## Shell 别名(可选)
要将 `safekubectl` 设为你的默认 kubectl,请添加一个别名:
```
# 添加到 ~/.bashrc 或 ~/.zshrc
alias kubectl='safekubectl'
```
## 开发说明
### 运行测试
```
go test ./... -v
```
### 运行测试并生成覆盖率
```
go test ./... -cover
```
### 构建
```
go build -o safekubectl .
```
## 许可证
MIT License
标签:EVTX分析, kubectl插件, Pandas, 子域名突变, 安全防护, 操作审计, 日志审计, 运维工具