简体   繁体   English

如何使用 AWS 秘密管理器轮换为自定义 lambda function 生成 SSH 私钥

[英]How to generate SSH private key using AWS secret manager rotation for custom lambda function

I've integrated AWS secret manager (ASM) for rotating SSH private keys.我已经集成了 AWS 密钥管理器 (ASM),用于轮换 SSH 私钥。 All thing works fine.一切正常。 I'm able to SSH on my Linux instance using the retrieved secret key value by the get-secret-value command.我可以使用get-secret-value命令检索到的密钥值在我的 Linux 实例上 SSH 。

Also, I've created a custom lambda function in ruby, for rotating my secret(SSK key) as below.另外,我在 ruby 中创建了一个自定义 lambda function,用于旋转我的秘密(SSK 密钥),如下所示。

require 'json'
require 'aws-sdk-secretsmanager'
require 'base64'

def lambda_handler(event:, context:)

    event['SecretId'] = "my-secret-id"
    region = 'my-region'

    arn = event['SecretId']
    token = event['ClientRequestToken']
    step = event['Step']    
    
    client = Aws::SecretsManager::Client.new(region: region)
     
    
    metadata = client.describe_secret(secret_id: arn)
   
        versions = metadata[:version_ids_to_stages]        
    
        if metadata[:rotation_enabled] == false
            puts "Secret %s is not enabled for rotation" % arn
        end
        
    if step == "createSecret"
        create_secret(client, arn, token)

    elsif step == "setSecret"
        set_secret(client, arn, token)

    elsif step == "testSecret"
        test_secret(client, arn, token)

    elsif step == "finishSecret"
        finish_secret(client, arn, token)

    else
        puts "Invalid step parameter"
         create_secret(client, arn, token)
        return
    end     
    
        { statusCode: 200, body: JSON.generate('Hello from Lambda!') }
end


def create_secret(client, arn, token)
    
    client.get_secret_value(secret_id: arn, version_stage: "AWSCURRENT")
    
    begin
        client.get_secret_value(secret_id: arn, version_id: token, version_stage: "AWSPENDING")
        puts "createSecret: Successfully retrieved secret for %s." % arn
    rescue
        puts "Not found secret with label AWSPENDING"

        # Here I want to generate a new SSH key and encode it,
        # For Database passwords rotation `client.get_random_password(password_length: "desired length", exclude_characters: "ExcludeCharactersType")` option is available, but for SSH I'm unable to find the generate method
        
    end
end

If anyone has an idea about how to generate a new ssh private key inside the create_secret method then please guide me.如果有人知道如何在create_secret方法中生成new ssh private key ,请指导我。 Thanks in advance.提前致谢。

Your lambda function is implementation is incomplete and missing some of the steps described here .您的 lambda function 实施不完整,缺少此处描述的一些步骤。 You need to use the generic template , written in python and adapt it for Ruby.您需要使用用 python 编写的通用模板并将其调整为 Ruby。

In create_secret you generate the key:在 create_secret 中,您生成密钥:

  • If you need an EC2 key pair, see this example .如果您需要 EC2 密钥对,请参阅此示例
  • If you need a generate public/private key pair, sample code is available here .如果您需要生成公钥/私钥对,可在此处获得示例代码。

In set_secret you need to update the ssh key in your Linux instances.在 set_secret 中,您需要更新 Linux 实例中的 ssh 密钥。 Before changing your ssh configuration, you need to test that the new key works.在更改 ssh 配置之前,您需要测试新密钥是否有效。

In the optional test_secret step, you test, if you can use the secret to connect.在可选的 test_secret 步骤中,您测试是否可以使用密钥进行连接。

In finish_secret you label the new secret as the current one.在 finish_secret 中,您将 label 新的秘密作为当前秘密。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM