Skip to content

Commit 1f47da7

Browse files
feature: KMS operation support + docs
1 parent 224c16c commit 1f47da7

File tree

5 files changed

+388
-4
lines changed

5 files changed

+388
-4
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,6 @@ target/
6464

6565
#Ipython Notebook
6666
.ipynb_checkpoints
67+
68+
# IDEs
69+
.idea

README.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from infisical_sdk.api_types import SymmetricEncryptionfrom infisical_sdk.api_types import KmsKeysOrderBy
2+
13
# Infisical Python SDK
24

35
The Infisical SDK provides a convenient way to interact with the Infisical API.
@@ -208,3 +210,147 @@ deleted_secret = client.secrets.delete_secret_by_name(
208210

209211
**Returns:**
210212
- `BaseSecret`: The response after deleting the secret.
213+
214+
### `kms`
215+
216+
This sub-class handles KMS related operations:
217+
218+
#### List KMS Keys
219+
220+
```python
221+
kms_keys = client.kms.list_keys(
222+
project_id="<project-id>",
223+
offset=0, # Optional
224+
limit=100, # Optional
225+
order_by=KmsKeysOrderBy.NAME, # Optional
226+
order_direction=OrderDirection.ASC, # Optional
227+
search=None # Optional
228+
)
229+
```
230+
231+
**Parameters:**
232+
- `project_id` (str): The ID of your project.
233+
- `offset` (int, optional): The offset to paginate from.
234+
- `limit` (int, optional): The page size for paginating.
235+
- `order_by` (KmsKeysOrderBy, optional): The key property to order the list response by.
236+
- `order_direction` (OrderDirection, optional): The direction to order the list response in.
237+
- `search` (str, optional): The text value to filter key names by.
238+
239+
**Returns:**
240+
- `ListKmsKeysResponse`: The response containing the list of KMS keys.
241+
242+
#### Get KMS Key by ID
243+
244+
```python
245+
kms_key = client.kms.get_key_by_id(
246+
key_id="<key-id>"
247+
)
248+
```
249+
250+
**Parameters:**
251+
- `key_id` (str): The ID of the key to retrieve.
252+
253+
**Returns:**
254+
- `KmsKey`: The specified key.
255+
256+
#### Get KMS Key by Name
257+
258+
```python
259+
kms_key = client.kms.get_key_by_name(
260+
key_name="my-key",
261+
project_id="<project-id>"
262+
)
263+
```
264+
265+
**Parameters:**
266+
- `key_name` (str): The name of the key to retrieve.
267+
- `project_id` (str): The ID of your project.
268+
269+
**Returns:**
270+
- `KmsKey`: The specified key.
271+
272+
#### Create KMS Key
273+
274+
```python
275+
kms_key = client.kms.create_key(
276+
name="my-key",
277+
project_id="<project-id>",
278+
encryption_algorithm=SymmetricEncryption.AES_GCM_256,
279+
description=None # Optional
280+
)
281+
```
282+
283+
**Parameters:**
284+
- `name` (str): The name of the key (must be slug-friendly).
285+
- `project_id` (str): The ID of your project.
286+
- `encryption_algorithm` (SymmetricEncryption): The encryption alogrithm this key should use.
287+
- `description` (str, optional): A description of your key.
288+
289+
**Returns:**
290+
- `KmsKey`: The newly created key.
291+
292+
#### Update KMS Key
293+
294+
```python
295+
updated_key = client.kms.update_key(
296+
key_id="<key-id>",
297+
name="my-updated-key", # Optional
298+
description="Updated description", # Optional
299+
is_disabled=True # Optional
300+
)
301+
```
302+
303+
**Parameters:**
304+
- `key_id` (str): The ID of the key to be updated.
305+
- `name` (str, optional): The updated name of the key (must be slug-friendly).
306+
- `description` (str): The updated description of the key.
307+
- `is_disabled` (str): The flag to disable operations with this key.
308+
309+
**Returns:**
310+
- `KmsKey`: The updated key.
311+
312+
#### Delete KMS Key
313+
314+
```python
315+
deleted_key = client.kms.delete_key(
316+
key_id="<key-id>"
317+
)
318+
```
319+
320+
**Parameters:**
321+
- `key_id` (str): The ID of the key to be deleted.
322+
323+
**Returns:**
324+
- `KmsKey`: The deleted key.
325+
326+
#### Encrypt Data with KMS Key
327+
328+
```python
329+
encrypted_data = client.kms.encrypt_data(
330+
key_id="<key-id>",
331+
plaintext="TXkgc2VjcmV0IG1lc3NhZ2U=" # must be base64 encoded
332+
)
333+
```
334+
335+
**Parameters:**
336+
- `key_id` (str): The ID of the key to encrypt the data with.
337+
- `plaintext` (str): The plaintext data to encrypt (must be base64 encoded).
338+
339+
**Returns:**
340+
- `str`: The encrypted ciphertext.
341+
342+
#### Decrypte Data with KMS Key
343+
344+
```python
345+
decrypted_data = client.kms.decrypt_data(
346+
key_id="<key-id>",
347+
ciphertext="Aq96Ry7sMH3k/ogaIB5MiSfH+LblQRBu69lcJe0GfIvI48ZvbWY+9JulyoQYdjAx"
348+
)
349+
```
350+
351+
**Parameters:**
352+
- `key_id` (str): The ID of the key to decrypt the data with.
353+
- `ciphertext` (str): The ciphertext returned from the encrypt operation.
354+
355+
**Returns:**
356+
- `str`: The base64 encoded plaintext.

example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
sdkInstance = InfisicalSDKClient(host="https://app.infisical.com")
44

5-
sdkInstance.auth.universalAuth.login("<>", "<>")
5+
sdkInstance.auth.universal_auth.login("<>", "<>")
66

77
# new_secret = sdkInstance.secrets.create_secret_by_name(
88
# secret_name="NEW_SECRET",

infisical_sdk/api_types.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class SingleSecretResponse(BaseModel):
112112
secret: BaseSecret
113113

114114
@classmethod
115-
def from_dict(cls, data: Dict) -> 'ListSecretsResponse':
115+
def from_dict(cls, data: Dict) -> 'SingleSecretResponse':
116116
return cls(
117117
secret=BaseSecret.from_dict(data['secret']),
118118
)
@@ -125,3 +125,64 @@ class MachineIdentityLoginResponse(BaseModel):
125125
expiresIn: int
126126
accessTokenMaxTTL: int
127127
tokenType: str
128+
129+
class SymmetricEncryption(str, Enum):
130+
AES_GCM_256 = "aes-256-gcm"
131+
AES_GCM_128 = "aes-128-gcm"
132+
133+
class OrderDirection(str, Enum):
134+
ASC = "asc"
135+
DESC = "desc"
136+
137+
class KmsKeysOrderBy(str, Enum):
138+
NAME = "name"
139+
140+
@dataclass
141+
class KmsKey(BaseModel):
142+
"""Infisical KMS Key"""
143+
id: str
144+
description: str
145+
isDisabled: bool
146+
orgId: str
147+
name: str
148+
createdAt: str
149+
updatedAt: str
150+
projectId: str
151+
version: int
152+
encryptionAlgorithm: SymmetricEncryption
153+
154+
@dataclass
155+
class ListKmsKeysResponse(BaseModel):
156+
"""Complete response model for Kms Keys API"""
157+
keys: List[KmsKey]
158+
totalCount: int
159+
160+
@classmethod
161+
def from_dict(cls, data: Dict) -> 'ListKmsKeysResponse':
162+
"""Create model from dictionary with camelCase keys, handling nested objects"""
163+
return cls(
164+
keys=[KmsKey.from_dict(key) for key in data['keys']],
165+
totalCount=data['totalCount']
166+
)
167+
168+
169+
@dataclass
170+
class SingleKmsKeyResponse(BaseModel):
171+
"""Response model for get/create/update/delete API"""
172+
key: KmsKey
173+
174+
@classmethod
175+
def from_dict(cls, data: Dict) -> 'SingleKmsKeyResponse':
176+
return cls(
177+
key=KmsKey.from_dict(data['key']),
178+
)
179+
180+
@dataclass
181+
class KmsKeyEncryptDataResponse(BaseModel):
182+
"""Response model for encrypt data API"""
183+
ciphertext: str
184+
185+
@dataclass
186+
class KmsKeyDecryptDataResponse(BaseModel):
187+
"""Response model for decrypt data API"""
188+
plaintext: str

0 commit comments

Comments
 (0)