-
Notifications
You must be signed in to change notification settings - Fork 50
/
example.py
executable file
·83 lines (68 loc) · 2.43 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
import logging
import os
from datetime import datetime, timezone
from bitwarden_sdk import BitwardenClient, DeviceType, client_settings_from_dict
# Create the BitwardenClient, which is used to interact with the SDK
client = BitwardenClient(
client_settings_from_dict(
{
"apiUrl": os.getenv("API_URL", "http://localhost:4000"),
"deviceType": DeviceType.SDK,
"identityUrl": os.getenv("IDENTITY_URL", "http://localhost:33656"),
"userAgent": "Python",
}
)
)
# Add some logging & set the org id
logging.basicConfig(level=logging.DEBUG)
organization_id = os.getenv("ORGANIZATION_ID")
# Set the state file location
# Note: the path must exist, the file will be created & managed by the sdk
state_path = os.getenv("STATE_FILE")
# Attempt to authenticate with the Secrets Manager Access Token
client.auth().login_access_token(os.getenv("ACCESS_TOKEN"), state_path)
# -- Example Project Commands --
project = client.projects().create(organization_id, "ProjectName")
project2 = client.projects().create(organization_id, "AnotherProject")
updated_project = client.projects().update(
organization_id, project.data.id, "Cool New Project Name"
)
get_that_project = client.projects().get(project.data.id)
input("Press Enter to delete the project...")
client.projects().delete([project.data.id])
print(client.projects().list(organization_id))
# -- Example Secret Commands --
if client.secrets().sync(organization_id, None).data.has_changes is True:
print("There are changes to sync")
else:
print("No changes to sync")
last_synced_date = datetime.now(tz=timezone.utc)
print(client.secrets().sync(organization_id, last_synced_date))
secret = client.secrets().create(
organization_id,
"TEST_SECRET",
"This is a test secret",
"Secret1234!",
[project2.data.id],
)
secret2 = client.secrets().create(
organization_id,
"ANOTHER_SECRET",
"Secret1234!",
None,
[project2.data.id],
)
secret_updated = client.secrets().update(
organization_id,
secret.data.id,
"TEST_SECRET_UPDATED",
"This as an updated test secret",
"Secret1234!_updated",
[project2.data.id],
)
secrets_retrieved = client.secrets().get_by_ids([secret.data.id, secret2.data.id])
# cleanup
input("Press Enter to cleanup secrets and projects...")
client.secrets().delete([secret.id for secret in secrets_retrieved.data.data])
client.projects().delete([project2.data.id])