如何在 terraform 中解密 windows 管理员密码?
How to decrypt windows administrator password in terraform?
我正在配置一个 windows 服务器用于在 AWS 中使用 Terraform 进行测试。每次我需要用我的 PEM 文件解密我的 windows 密码才能连接。相反,我选择了 terraform 参数 get_password_data
and stored my password_data
in tfstate file. Now how do i decrypt the same with interpolation syntax rsadecrypt
请找到我下面的地形代码
### Resource for EC2 instance creation ###
resource "aws_instance" "ec2" {
ami = "${var.ami}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
subnet_id = "${var.subnet_id}"
security_groups = ["${var.security_groups}"]
availability_zone = "${var.availability_zone}"
private_ip = "x.x.x.x"
get_password_data = "true"
connection {
password = "${rsadecrypt(self.password_data)}"
}
root_block_device {
volume_type = "${var.volume_type}"
volume_size = "${var.volume_size}"
delete_on_termination = "true"
}
tags {
"Cost Center" = "R1"
"Name" = "AD-test"
"Purpose" = "Task"
"Server Name" = "Active Directory"
"SME Name" = "Ravi"
}
}
output "instance_id" {
value = "${aws_instance.ec2.id}"
}
### Resource for EBS volume creation ###
resource "aws_ebs_volume" "additional_vol" {
availability_zone = "${var.availability_zone}"
size = "${var.size}"
type = "${var.type}"
}
### Output of Volume ID ###
output "vol_id" {
value = "${aws_ebs_volume.additional_vol.id}"
}
### Resource for Volume attachment ###
resource "aws_volume_attachment" "attach_vol" {
device_name = "${var.device_name}"
volume_id = "${aws_ebs_volume.additional_vol.id}"
instance_id = "${aws_instance.ec2.id}"
skip_destroy = "true"
}
密码是使用您在启动实例时指定的key_pair加密的,您仍然需要使用它来解密,因为password_data
仍然只是base64编码加密 密码数据.
你应该使用${rsadecrypt(self.password_data,file("/path/to/private_key.pem"))}
这是有充分理由的。你真的不希望只是一个 base64 编码的密码在状态中浮动。
简短版本:
您缺少插值函数中的第二个参数。
我知道这与实际问题无关,但如果您不想在 public 环境中公开您的私钥(例如 Git)
我宁愿打印加密后的密码
resource "aws_instance" "ec2" {
ami = .....
instance_type = .....
security_groups = [.....]
subnet_id = .....
iam_instance_profile = .....
key_name = .....
get_password_data = "true"
tags = {
Name = .....
}
}
像这样
output "Administrator_Password" {
value = [
aws_instance.ec2.password_data
]
}
然后,
获取base64密码并将其放入名为pwdbase64.txt
的文件中
运行这条命令解码base64到bin文件
certutil -decode pwdbase64.txt password.bin
运行这条命令解密你的password.bin
openssl rsautl -decrypt -inkey privatekey.openssh -in password.bin
如果你不会玩openssl。请检查 post
privatekey.openssh 应如下所示:
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCd+qQbLiSVuNludd67EtepR3g1+VzV6gjsZ+Q+RtuLf88cYQA3
6M4rjVAy......1svfaU/powWKk7WWeE58dnnTZoLvHQ
ZUvFlHE/LUHCQkx8sSECQGatJGiS5fgZhvpzLn4amNwKkozZ3tc02fMzu8IgdEit
jrk5Zq8Vg71vH1Z5OU0kjgrR4ZCjG9ngGdaFV7K7ki0=
-----END RSA PRIVATE KEY-----
public 密钥应如下所示:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQAB......iFZmwQ==
terraform 密钥对代码应如下所示
resource "aws_key_pair" "key_pair_ec2" {
key_name = "key_pair_ec2"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQAB......iFZmwQ=="
}
Pd: 你可以使用puttygen生成密钥
您可以直接使用 tls_private_key
生成密钥,然后直接将生成的密码复制到 AWS SSM Parameter Store 中,而不是将 .pem 文件放在周围或显式输入 public 密钥,这样您可以在你的基础设施建立起来后从那里取回它。
这是我生成密钥的方式:
resource "tls_private_key" "instance_key" {
algorithm = "RSA"
}
resource "aws_key_pair" "instance_key_pair" {
key_name = "${local.name_prefix}-instance-key"
public_key = tls_private_key.instance_key.public_key_openssh
}
在您的 aws_instance
中,您要确保设置了这些:
key_name = aws_key_pair.instance_key_pair.key_name
get_password_data = true
最后将生成的密码存入SSM(注意:需要将私钥包裹起来nonsensitive
):
resource "aws_ssm_parameter" "windows_ec2" {
depends_on = [aws_instance.winserver_instance[0]]
name = "/Microsoft/AD/${var.environment}/ec2-win-password"
type = "SecureString"
value = rsadecrypt(aws_instance.winserver_instance[0].password_data, nonsensitive(tls_private_key.instance_key
.private_key_pem))
}
我正在配置一个 windows 服务器用于在 AWS 中使用 Terraform 进行测试。每次我需要用我的 PEM 文件解密我的 windows 密码才能连接。相反,我选择了 terraform 参数 get_password_data
and stored my password_data
in tfstate file. Now how do i decrypt the same with interpolation syntax rsadecrypt
请找到我下面的地形代码
### Resource for EC2 instance creation ###
resource "aws_instance" "ec2" {
ami = "${var.ami}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
subnet_id = "${var.subnet_id}"
security_groups = ["${var.security_groups}"]
availability_zone = "${var.availability_zone}"
private_ip = "x.x.x.x"
get_password_data = "true"
connection {
password = "${rsadecrypt(self.password_data)}"
}
root_block_device {
volume_type = "${var.volume_type}"
volume_size = "${var.volume_size}"
delete_on_termination = "true"
}
tags {
"Cost Center" = "R1"
"Name" = "AD-test"
"Purpose" = "Task"
"Server Name" = "Active Directory"
"SME Name" = "Ravi"
}
}
output "instance_id" {
value = "${aws_instance.ec2.id}"
}
### Resource for EBS volume creation ###
resource "aws_ebs_volume" "additional_vol" {
availability_zone = "${var.availability_zone}"
size = "${var.size}"
type = "${var.type}"
}
### Output of Volume ID ###
output "vol_id" {
value = "${aws_ebs_volume.additional_vol.id}"
}
### Resource for Volume attachment ###
resource "aws_volume_attachment" "attach_vol" {
device_name = "${var.device_name}"
volume_id = "${aws_ebs_volume.additional_vol.id}"
instance_id = "${aws_instance.ec2.id}"
skip_destroy = "true"
}
密码是使用您在启动实例时指定的key_pair加密的,您仍然需要使用它来解密,因为password_data
仍然只是base64编码加密 密码数据.
你应该使用${rsadecrypt(self.password_data,file("/path/to/private_key.pem"))}
这是有充分理由的。你真的不希望只是一个 base64 编码的密码在状态中浮动。
简短版本: 您缺少插值函数中的第二个参数。
我知道这与实际问题无关,但如果您不想在 public 环境中公开您的私钥(例如 Git)
我宁愿打印加密后的密码
resource "aws_instance" "ec2" {
ami = .....
instance_type = .....
security_groups = [.....]
subnet_id = .....
iam_instance_profile = .....
key_name = .....
get_password_data = "true"
tags = {
Name = .....
}
}
像这样
output "Administrator_Password" {
value = [
aws_instance.ec2.password_data
]
}
然后,
获取base64密码并将其放入名为pwdbase64.txt
的文件中运行这条命令解码base64到bin文件
certutil -decode pwdbase64.txt password.bin
运行这条命令解密你的password.bin
openssl rsautl -decrypt -inkey privatekey.openssh -in password.bin
如果你不会玩openssl。请检查
privatekey.openssh 应如下所示:
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCd+qQbLiSVuNludd67EtepR3g1+VzV6gjsZ+Q+RtuLf88cYQA3
6M4rjVAy......1svfaU/powWKk7WWeE58dnnTZoLvHQ
ZUvFlHE/LUHCQkx8sSECQGatJGiS5fgZhvpzLn4amNwKkozZ3tc02fMzu8IgdEit
jrk5Zq8Vg71vH1Z5OU0kjgrR4ZCjG9ngGdaFV7K7ki0=
-----END RSA PRIVATE KEY-----
public 密钥应如下所示:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQAB......iFZmwQ==
terraform 密钥对代码应如下所示
resource "aws_key_pair" "key_pair_ec2" {
key_name = "key_pair_ec2"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQAB......iFZmwQ=="
}
Pd: 你可以使用puttygen生成密钥
您可以直接使用 tls_private_key
生成密钥,然后直接将生成的密码复制到 AWS SSM Parameter Store 中,而不是将 .pem 文件放在周围或显式输入 public 密钥,这样您可以在你的基础设施建立起来后从那里取回它。
这是我生成密钥的方式:
resource "tls_private_key" "instance_key" {
algorithm = "RSA"
}
resource "aws_key_pair" "instance_key_pair" {
key_name = "${local.name_prefix}-instance-key"
public_key = tls_private_key.instance_key.public_key_openssh
}
在您的 aws_instance
中,您要确保设置了这些:
key_name = aws_key_pair.instance_key_pair.key_name
get_password_data = true
最后将生成的密码存入SSM(注意:需要将私钥包裹起来nonsensitive
):
resource "aws_ssm_parameter" "windows_ec2" {
depends_on = [aws_instance.winserver_instance[0]]
name = "/Microsoft/AD/${var.environment}/ec2-win-password"
type = "SecureString"
value = rsadecrypt(aws_instance.winserver_instance[0].password_data, nonsensitive(tls_private_key.instance_key
.private_key_pem))
}