Skip to content

Commit

Permalink
fix: minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielHougaard committed Feb 6, 2025
1 parent 1f47da7 commit a1901a0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from infisical_sdk.api_types import SymmetricEncryptionfrom infisical_sdk.api_types import KmsKeysOrderBy

# Infisical Python SDK

The Infisical SDK provides a convenient way to interact with the Infisical API.
Expand Down Expand Up @@ -328,13 +326,13 @@ deleted_key = client.kms.delete_key(
```python
encrypted_data = client.kms.encrypt_data(
key_id="<key-id>",
plaintext="TXkgc2VjcmV0IG1lc3NhZ2U=" # must be base64 encoded
base64EncodedPlaintext="TXkgc2VjcmV0IG1lc3NhZ2U=" # must be base64 encoded
)
```

**Parameters:**
- `key_id` (str): The ID of the key to encrypt the data with.
- `plaintext` (str): The plaintext data to encrypt (must be base64 encoded).
- `base64EncodedPlaintext` (str): The plaintext data to encrypt (must be base64 encoded).

**Returns:**
- `str`: The encrypted ciphertext.
Expand Down
9 changes: 8 additions & 1 deletion infisical_sdk/api_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,21 @@ class MachineIdentityLoginResponse(BaseModel):
accessTokenMaxTTL: int
tokenType: str


class SymmetricEncryption(str, Enum):
AES_GCM_256 = "aes-256-gcm"
AES_GCM_128 = "aes-128-gcm"


class OrderDirection(str, Enum):
ASC = "asc"
DESC = "desc"


class KmsKeysOrderBy(str, Enum):
NAME = "name"


@dataclass
class KmsKey(BaseModel):
"""Infisical KMS Key"""
Expand All @@ -151,6 +155,7 @@ class KmsKey(BaseModel):
version: int
encryptionAlgorithm: SymmetricEncryption


@dataclass
class ListKmsKeysResponse(BaseModel):
"""Complete response model for Kms Keys API"""
Expand All @@ -177,12 +182,14 @@ def from_dict(cls, data: Dict) -> 'SingleKmsKeyResponse':
key=KmsKey.from_dict(data['key']),
)


@dataclass
class KmsKeyEncryptDataResponse(BaseModel):
"""Response model for encrypt data API"""
ciphertext: str


@dataclass
class KmsKeyDecryptDataResponse(BaseModel):
"""Response model for decrypt data API"""
plaintext: str
plaintext: str
18 changes: 10 additions & 8 deletions infisical_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
from botocore.exceptions import NoCredentialsError

from .infisical_requests import InfisicalRequests
from .api_types import ListSecretsResponse, MachineIdentityLoginResponse, ListKmsKeysResponse, SingleKmsKeyResponse, \
KmsKey, SymmetricEncryption, KmsKeyEncryptDataResponse, KmsKeyDecryptDataResponse, KmsKeysOrderBy, OrderDirection
from .api_types import SingleSecretResponse, BaseSecret

from .api_types import ListSecretsResponse, SingleSecretResponse, BaseSecret
from .api_types import SymmetricEncryption, KmsKeysOrderBy, OrderDirection
from .api_types import ListKmsKeysResponse, SingleKmsKeyResponse, MachineIdentityLoginResponse
from .api_types import KmsKey, KmsKeyEncryptDataResponse, KmsKeyDecryptDataResponse


class InfisicalSDKClient:
Expand Down Expand Up @@ -346,6 +348,7 @@ def delete_secret_by_name(

return result.data.secret


class KMS:
def __init__(self, client: InfisicalSDKClient) -> None:
self.client = client
Expand Down Expand Up @@ -462,13 +465,13 @@ def delete_key(
def encrypt_data(
self,
key_id: str,
plaintext: str) -> str:
base64EncodedPlaintext: str) -> str:
"""
Encrypt data with the specified KMS key.
:param key_id: The ID of the key to decrypt the ciphertext with
:type key_id: str
:param plaintext: The base64 encoded plaintext to encrypt
:param base64EncodedPlaintext: The base64 encoded plaintext to encrypt
:type plaintext: str
Expand All @@ -477,7 +480,7 @@ def encrypt_data(
"""

request_body = {
"plaintext": plaintext
"plaintext": base64EncodedPlaintext
}

result = self.client.api.post(
Expand Down Expand Up @@ -509,11 +512,10 @@ def decrypt_data(
"ciphertext": ciphertext
}


result = self.client.api.post(
path=f"/api/v1/kms/keys/{key_id}/decrypt",
json=request_body,
model=KmsKeyDecryptDataResponse
)

return result.data.plaintext
return result.data.plaintext

0 comments on commit a1901a0

Please sign in to comment.