File: aws.rst

package info (click to toggle)
python-hvac 2.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,800 kB
  • sloc: python: 29,360; makefile: 42; sh: 14
file content (190 lines) | stat: -rw-r--r-- 4,688 bytes parent folder | download | duplicates (3)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
AWS
===

.. contents::

Configure Root IAM Credentials
------------------------------

Source reference: :py:meth:`hvac.api.secrets_engines.Aws.configure_root_iam_credentials`

.. code:: python

    import os

    import hvac
    client = hvac.Client()

    client.secrets.aws.configure_root_iam_credentials(
        access_key=os.getenv('AWS_ACCESS_KEY_ID'),
        secret_key=os.getenv('AWS_SECRET_ACCESS_KEY'),
    )

Rotate Root IAM Credentials
---------------------------

Source reference: :py:meth:`hvac.api.secrets_engines.Aws.rotate_root_iam_credentials`

.. code:: python

    import hvac
    client = hvac.Client()

    client.secrets.aws.rotate_root_iam_credentials()

Configure Lease
---------------

Source reference: :py:meth:`hvac.api.secrets_engines.Aws.configure_lease`

.. code:: python

    import hvac
    client = hvac.Client()

    # Set the default least TTL to 300 seconds / 5 minutes
    client.secrets.aws.configure_lease(
        lease='300s',
    )

Read Lease
----------

Source reference: :py:meth:`hvac.api.secrets_engines.Aws.read_lease`

.. code:: python

    import hvac
    client = hvac.Client()

    read_lease_response = client.secrets.aws.read_lease()
    print('The current "lease_max" TTL is: {lease_max}'.format(
        lease_max=read_lease_response['data']['lease_max'],
    ))

Create or Update Role
---------------------

Source reference: :py:meth:`hvac.api.secrets_engines.Aws.create_or_update_role`

.. code:: python

    import hvac
    client = hvac.Client()

    describe_ec2_policy_doc = {
        'Version': '2012-10-17',
        'Statement': [
            {
                'Resource': '*'
                'Action': 'ec2:Describe*',
                'Effect': 'Allow',
            },
        ],
    }
    client.secrets.aws.create_or_update_role(
        name='hvac-role',
        credential_type='assumed_role',
        policy_document=describe_ec2_policy_doc,
        policy_arns=['arn:aws:iam::aws:policy/AmazonVPCReadOnlyAccess'],
    )

Legacy Parameters
`````````````````

.. note::
    In previous versions of Vault (before version 0.11.0), this API route only supports the `policy_document` and `policy_arns` parameters (which hvac will translate to `policy` and `arn` parameters respectively in the request sent to Vault). If running these versions of Vault, the `legacy_params` parameter on this method can be set to `True`.

For older versions of Vault (any version before 0.11.0):

.. code:: python

    import hvac
    client = hvac.Client()

    describe_ec2_policy_doc = {
        'Version': '2012-10-17',
        'Statement': [
            {
                'Resource': '*'
                'Action': 'ec2:Describe*',
                'Effect': 'Allow',
            },
        ],
    }

    # Note: with the legacy params, the `policy_arns` parameter is translated to `arn`
    # in the request sent to Vault and only one ARN is accepted. If a list is provided,
    # hvac will only use the first ARN in the list.
    client.secrets.aws.create_or_update_role(
        name='hvac-role',
        credential_type='assumed_role',
        policy_document=describe_ec2_policy_doc,
        policy_arns='arn:aws:iam::aws:policy/AmazonVPCReadOnlyAccess',
        legacy_params=True,
    )

Read Role
---------

Source reference: :py:meth:`hvac.api.secrets_engines.Aws.read_role`

.. code:: python

    import hvac
    client = hvac.Client()

    read_role_response = client.secrets.aws.read_role(
        name='hvac-role',
    )
    print('The credential type for role "hvac-role" is: {cred_type}'.format(
        cred_type=read_role_response['data']['credential_types'],
    ))


List Roles
----------

Source reference: :py:meth:`hvac.api.secrets_engines.Aws.list_roles`

.. code:: python

    import hvac
    client = hvac.Client()

    list_roles_response = client.secrets.aws.list_roles()
    print('AWS secrets engine role listing: {roles}'.format(
        roles=', '.join(list_roles_response['data']['keys'])
    ))

Delete Role
-----------

Source reference: :py:meth:`hvac.api.secrets_engines.Aws.delete_role`

.. code:: python

    import hvac
    client = hvac.Client()

    client.secrets.aws.delete_role(
        name='hvac-role',
    )

Generate Credentials
--------------------

Source reference: :py:meth:`hvac.api.secrets_engines.Aws.generate_credentials`

.. code:: python

    import hvac
    client = hvac.Client()

    gen_creds_response = client.secrets.aws.generate_credentials(
        name='hvac-role',
    )
    print('Generated access / secret keys: {access} / {secret}'.format(
        access=gen_creds_response['data']['access_key'],
        secret=gen_creds_response['data']['secret_key'],
    ))