Skip to content

Commit

Permalink
feat: working import
Browse files Browse the repository at this point in the history
  • Loading branch information
johnson2427 committed Jun 25, 2024
1 parent 1cad64e commit 736142f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
28 changes: 15 additions & 13 deletions ape_aws/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import ClassVar

import boto3 # type: ignore[import]
from pydantic import BaseModel, Field, ConfigDict
from pydantic import BaseModel, Field, ConfigDict, field_validator


class AliasResponse(BaseModel):
Expand Down Expand Up @@ -78,19 +78,22 @@ class ImportKeyRequest(CreateKeyModel):
class ImportKey(ImportKeyRequest):
key_id: str = Field(default=None, alias="KeyId")
public_key: bytes = Field(default=None, alias="PublicKey")
private_key: bytes = Field(
default=ec.generate_private_key(
ec.SeCP256K1(),
default_backend()
).private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption(),
),
alias="PrivateKey",
)
private_key: bytes | None = Field(default=None, alias="PrivateKey")
import_token: bytes = Field(default=None, alias="ImportToken")

@field_validator("private_key")
def validate_private_key(cls, value):
if not isinstance(value, bytes):
return ec.generate_private_key(
ec.SECP256K1(),
default_backend()
).private_bytes(
encoding=serialization.Encoding.DER,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption(),
)
return value

@property
def encrypted_key(self):
if not self.public_key:
Expand Down Expand Up @@ -172,7 +175,6 @@ def create_key(self, key_spec: CreateKey | ImportKey):
return key_id

def import_key(self, key_spec: ImportKey):
breakpoint()
return self.client.import_key_material(
KeyId=key_spec.key_id,
ImportToken=key_spec.import_token,
Expand Down
12 changes: 9 additions & 3 deletions ape_aws/kms/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ def create_key(

@kms.command(name="import")
@ape_cli_context()
@click.option(
"-p",
"--private-key",
"private_key",
multiple=False,
help="The private key to import",
)
@click.option(
"-a",
"--admin",
Expand All @@ -81,7 +88,6 @@ def create_key(
)
@click.argument("alias_name")
@click.argument("description")
@click.argument("private_key")
def import_key(
cli_ctx,
alias_name: str,
Expand All @@ -98,8 +104,8 @@ def import_key(
)
key_id = kms_client.create_key(key_spec)
create_key_response = kms_client.get_parameters(key_id)
public_key = base64.b64encode(create_key_response["PublicKey"])
import_token = base64.b64encode(create_key_response["ImportToken"])
public_key = create_key_response["PublicKey"]
import_token = create_key_response["ImportToken"]
import_key_spec = ImportKey(
**key_spec.model_dump(),
key_id=key_id,
Expand Down

0 comments on commit 736142f

Please sign in to comment.