hparadiz/aws-instance-connect

使用 AWS Instance Connect API 授权 SSH 密钥,并自动使用该密钥为您运行 SSH 命令。

0.4 2022-04-18 20:37 UTC

This package is auto-updated.

Last update: 2024-09-19 02:08:47 UTC


README

Latest Unstable Version License

此工具允许您使用仅有的 AWS IAM 凭据(如果您与 AWS 合作,这些凭据可能已经在您的家目录中)即可 SSH 登录 AWS EC2 实例。

更具体地说,它使用 AWS SDK 访问 AWS Instance Connect,以高安全性快速 SSH 登录您的 EC2 实例,因为生成的密钥仅用于一次性使用,然后立即销毁。

asciicast

安装

composer global require hparadiz/aws-instance-connect

配置

请按照 AWS 指示在 ~/.aws/credentials 中设置您的 AWS 凭据。

默认区域将从 ~/.aws/config 中获取。

设置

  1. 转到 AWS 控制台中的 IAM -> 策略。
  2. 创建一个新的 JSON 策略。
  3. 粘贴以下内容并保存。
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "ec2-instance-connect:SendSSHPublicKey",
            "Resource": "arn:aws:ec2:region:account-id:instance/*"
        },
        {
            "Effect": "Allow",
            "Action": "ec2:DescribeInstances",
            "Resource": "*"
        }
    ]
}

此策略将允许 AWS Instance Connect 在所有区域与您的所有 EC2 实例一起工作。有关更具体的网络安全策略规则,请参阅 AWS 文档。

通过 CLI 使用

$ ic --help
Usage: ic [options] [<instanceId>] [operands]

Operands:
  [<instanceId>]  Target instance

Options:
  -v, --version       Show version information and quit
  -h, --help          Show this help and quit
  -N, --no-connect    Authorize the SSH key and exit.
  -u, --user <arg>    Set user for SSH connection. Defaults to ubuntu.
  -R, --region <arg>  AWS Region
  -n, --name <arg>    The name of the instance (AWS Tag 'Name')

通过代码使用

$IC = new InstanceConnect();
$IC->region = 'us-east-1';

// the username for the SHH connection
$IC->user = 'ubuntu';

// optional (will prompt for an instance if not set)
$IC->name = 'i-0e19ee2d63877633f';

$IC->publicKey = '/home/user/.ssh/rsa.pub';
$IC->privateKey = '/home/user/.ssh/rsa';

// for the key to be authorized but for no SSH connection to be created
// to use the key with something else like SCP or a tunnel
$IC->noConnect = false;

$IC->start();

常见问题解答

为什么?

通过使用 AWS 凭据登录而不是 SSH 密钥,更容易管理您的用户,并且您实际上可以撤销访问权限,而无需手动删除任何密钥。您可以通过将用户添加或从 AWS 控制台的正常用户管理界面中删除用户来添加或删除用户。

好吧,但说真的。这安全吗?

代码非常简单。大约有 200 行代码。请随意阅读。我使用 phpseclib 创建密钥,而官方 AWS SDK 执行实际工作。

为什么选择 PHP?

由于我从事 PHP 项目工作,这对我的便利性来说只是顺理成章。

支持

我为自己编写了这个工具,但我希望其他人也能从中受益。如果人们开始使用它,我将很乐意进一步工作。请随时提出功能请求。我渴望了解其他使用案例。

如果您想支持此项目,请参阅以下链接。

Ko-Fi: https://ko-fi.com/henryparadiz

BTC - bc1qqqejxpuxgeyxx5fkyan8tpeuwyenks8fa4zldf