【问题标题】:How can I generate OpenSSH RSA key using python from the private key stored in Secrets manager in AWS?如何使用 python 从存储在 AWS 的 Secrets Manager 中的私钥生成 OpenSSH RSA 密钥?
【发布时间】:2020-11-23 20:47:18
【问题描述】:

我有以下代码生成由 putty 生成并存储在 AWS 机密管理器中的密钥的公共和私有行的 json:

import boto3
import base64
from botocore.exceptions import ClientError
def get_secret():
    secret_name = "Server/Name"
    region_name = "us-west-1"
    # Create a Secrets Manager client
    session = boto3.session.Session()
    client = session.client(
        service_name='secretsmanager',
        region_name=region_name
    )
    # In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
    # See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
    # We rethrow the exception by default.
    try:
        get_secret_value_response = client.get_secret_value(
            SecretId=secret_name
        )
    except ClientError as e:
        if e.response['Error']['Code'] == 'DecryptionFailureException':
            # Secrets Manager can't decrypt the protected secret text using the provided KMS key.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
        elif e.response['Error']['Code'] == 'InternalServiceErrorException':
            # An error occurred on the server side.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
        elif e.response['Error']['Code'] == 'InvalidParameterException':
            # You provided an invalid value for a parameter.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
        elif e.response['Error']['Code'] == 'InvalidRequestException':
            # You provided a parameter value that is not valid for the current state of the resource.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
        elif e.response['Error']['Code'] == 'ResourceNotFoundException':
            # We can't find the resource that you asked for.
            # Deal with the exception here, and/or rethrow at your discretion.
            raise e
    else:
        # Decrypts secret using the associated KMS CMK.
        # Depending on whether the secret is a string or binary, one of these fields will be populated.
        if 'SecretString' in get_secret_value_response:
            secret = get_secret_value_response['SecretString']
        else:
            decoded_binary_secret = base64.b64decode(get_secret_value_response['SecretBinary'])
    return json.loads(secret)

key = get_secret()
print(key)

而当前的 JSON 输出是

{'Key': 'PuTTY-User-Key-File-2: ssh-rsa Encryption: none Comment: rsa-key-20200608 Public-Lines: 6 AAAAB3NzaC1yc2EAAAABJQAAAQEA9CfhCMQOh0OCjzQsgpceJwklTtKJYOZLpl02 ********publicLines********* 
Private-Lines: 14 r1ePzc1522MZrfWYn3t7sBhLWA26jqMTWeqSkflnW1MMGay8fpkiTpVZPnX7Oe5L +hgkHZSJDgyzHpkA22XqQgi6uAfK7lugTCkYfq3n4xVU3U+ CzNr+kuMxNRoJBOyU ********privateLines*********'}

但我想生成我的私线的 OpenSSH RSA 格式密钥。

预期输出是:

-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQCtCnvOu+3qxh+mGvejBMsAVDVA/c8C4su1M6q0xPFISwHOQsmP
k3AE2pjwKHOsa0IqxPG39EKrdYhBHB5geMQv3httLIoXzj0oxMEIEhqZ2AgA378D
Qtky91WcZE67KpmWDOXG95FCBZj7PX7XbdwR+Uk4kLhE2Z6vkWDe7st8rQIDAQAB
AoGAakmZOKfogJ/HiuDfoQttobsXpt7/i7dBBwFAZp7d0dj4t/gAFKesU97tt/4w
5wRO9TRZgPORC/46fjvGUN19KyHs3JRhiYrsKVnJNBBpAG1+9pBi+avPR9sxBKG7
HWiSXKRRCddwWpDuyMrzyre+k+mAtulbF6kecBIrSfEBg6ECQQC+Hht9S93CMmiy
YKUiBwMhvbMoYukGGIhB3WSZK28PvYadV2uVAHYCxGq2U1ULJwHWac5OMDzR+J4y
MuxlJT5I0Y4fr4qgkQJARWfPg+l7jPj1csj56r2e1cxhYqamU6rNP3PHjKdzJ5zK
eS87SRFybqd578kFEdfaR+CesRKzXswfDTKoM77wnQ==
-----END RSA PRIVATE KEY-----

【问题讨论】:

    标签: python rsa openssh aws-secrets-manager


    【解决方案1】:

    您可以只将已编码的密钥存储为字符串,而不用 JSON 包装它们。例如,请参阅 a similar question 存储私钥。

    【讨论】:

      猜你喜欢
      • 2020-02-14
      • 2020-08-14
      • 2021-10-22
      • 2020-09-29
      • 2021-10-29
      • 2020-02-27
      • 2020-07-23
      • 2019-02-15
      • 2020-10-26
      相关资源
      最近更新 更多