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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
from contextlib import contextmanager
from unittest import TestCase
from hvac import exceptions
from tests.utils.hvac_integration_test_case import HvacIntegrationTestCase
class TestToken(HvacIntegrationTestCase, TestCase):
# would rather these be pytest fixtures
@contextmanager
def prep_policy(self, name):
try:
yield (name, self.prep_policy(name))
finally:
self.client.sys.delete_policy(name)
@contextmanager
def prep_role(self, name, policies=None):
role = self.client.auth.token.create_or_update_role(
name, allowed_policies=policies
)
assert role.status_code == 204
try:
yield (name, role, policies)
finally:
self.client.auth.token.delete_role(name)
@contextmanager
def test_policy(self):
with self.prep_policy(["testpolicy"]) as p:
yield p
@contextmanager
def test_role(self):
with self.test_policy() as p, self.prep_role(
name="testrole", policies=p[0]
) as r:
yield r
def test_auth_token_manipulation(self):
result = self.client.auth.token.create(ttl="1h", renewable=True)
assert result["auth"]["client_token"]
lookup = self.client.auth.token.lookup(result["auth"]["client_token"])
assert result["auth"]["client_token"] == lookup["data"]["id"]
renew = self.client.auth.token.renew(lookup["data"]["id"])
assert result["auth"]["client_token"] == renew["auth"]["client_token"]
self.client.auth.token.revoke(lookup["data"]["id"])
try:
lookup = self.client.auth.token.lookup(result["auth"]["client_token"])
assert False
except exceptions.Forbidden:
assert True
except exceptions.InvalidPath:
assert True
except exceptions.InvalidRequest:
assert True
def test_self_auth_token_manipulation(self):
result = self.client.auth.token.create(ttl="1h", renewable=True)
assert result["auth"]["client_token"]
self.client.token = result["auth"]["client_token"]
lookup = self.client.auth.token.lookup_self()
assert result["auth"]["client_token"] == lookup["data"]["id"]
renew = self.client.auth.token.renew_self()
assert result["auth"]["client_token"] == renew["auth"]["client_token"]
self.client.auth.token.revoke_self()
try:
lookup = self.client.auth.token.lookup(result["auth"]["client_token"])
assert False
except exceptions.Forbidden:
assert True
except exceptions.InvalidPath:
assert True
except exceptions.InvalidRequest:
assert True
def test_auth_orphaned_token_manipulation(self):
result = self.client.auth.token.create_orphan(ttl="1h", renewable=True)
assert result["auth"]["client_token"]
lookup = self.client.auth.token.lookup(result["auth"]["client_token"])
assert result["auth"]["client_token"] == lookup["data"]["id"]
renew = self.client.auth.token.renew(lookup["data"]["id"])
assert result["auth"]["client_token"] == renew["auth"]["client_token"]
self.client.auth.token.revoke(lookup["data"]["id"])
try:
lookup = self.client.auth.token.lookup(result["auth"]["client_token"])
assert False
except exceptions.Forbidden:
assert True
except exceptions.InvalidPath:
assert True
except exceptions.InvalidRequest:
assert True
def test_token_accessor(self):
# Create token, check accessor is provided
result = self.client.auth.token.create(ttl="1h")
token_accessor = result["auth"].get("accessor", None)
assert token_accessor
# Look up token by accessor, make sure token is excluded from results
lookup = self.client.auth.token.lookup_accessor(token_accessor)
assert lookup["data"]["accessor"] == token_accessor
assert not lookup["data"]["id"]
# Revoke token using the accessor
self.client.auth.token.revoke_accessor(token_accessor)
# Look up by accessor should fail
with self.assertRaises(exceptions.InvalidRequest):
lookup = self.client.auth.token.lookup_accessor(token_accessor)
# As should regular lookup
with self.assertRaises(exceptions.Forbidden):
lookup = self.client.auth.token.lookup(result["auth"]["client_token"])
def test_create_token_explicit_max_ttl(self):
token = self.client.auth.token.create(ttl="30m", explicit_max_ttl="5m")
assert token["auth"]["client_token"]
assert token["auth"]["lease_duration"] == 300
# Validate token
lookup = self.client.auth.token.lookup(token["auth"]["client_token"])
assert token["auth"]["client_token"] == lookup["data"]["id"]
def test_create_token_max_ttl(self):
token = self.client.auth.token.create(ttl="5m")
assert token["auth"]["client_token"]
assert token["auth"]["lease_duration"] == 300
# Validate token
lookup = self.client.auth.token.lookup(token["auth"]["client_token"])
assert token["auth"]["client_token"] == lookup["data"]["id"]
def test_create_token_periodic(self):
token = self.client.auth.token.create(period="30m")
assert token["auth"]["client_token"]
assert token["auth"]["lease_duration"] == 1800
# Validate token
lookup = self.client.auth.token.lookup(token["auth"]["client_token"])
assert token["auth"]["client_token"] == lookup["data"]["id"]
assert lookup["data"]["period"] == 1800
def test_create_wrapped_token_periodic(self):
response = self.client.auth.token.create(period="30m", wrap_ttl="15m")
assert "wrap_info" in response, repr(response)
assert response["wrap_info"] is not None, repr(response)
assert response["auth"] is None, repr(response)
assert response["wrap_info"]["ttl"] == 900
assert "token" in response["wrap_info"]
# unwrap
token = self.client.sys.unwrap(token=response["wrap_info"]["token"])
assert token["auth"]["client_token"]
assert token["auth"]["lease_duration"] == 1800
# Validate token
lookup = self.client.auth.token.lookup(token["auth"]["client_token"])
assert token["auth"]["client_token"] == lookup["data"]["id"]
assert lookup["data"]["period"] == 1800
def test_token_roles(self):
# No roles, list_token_roles == None
with self.assertRaises(exceptions.InvalidPath):
self.client.auth.token.list_roles()
try:
# Create token role
assert (
self.client.auth.token.create_or_update_role("testrole").status_code
== 204
)
# List token roles
during = self.client.auth.token.list_roles()["data"]["keys"]
assert len(during) == 1
assert during[0] == "testrole"
finally:
# Delete token role
self.client.auth.token.delete_role("testrole")
# No roles, list_token_roles == None
with self.assertRaises(exceptions.InvalidPath):
self.client.auth.token.list_roles()
def test_create_token_w_role(self):
with self.test_role() as test_role:
role_name, _, policies = test_role
expected_policies = ["default"] + policies
# Create token against role
token = self.client.auth.token.create(ttl="1h", role_name=role_name)
assert token["auth"]["client_token"]
assert token["auth"]["policies"] == expected_policies
def test_create_wrapped_token_w_role(self):
with self.test_role() as test_role:
role_name, _, policies = test_role
expected_policies = ["default"] + policies
# Create token against role
response = self.client.auth.token.create(
ttl="1h", role_name=role_name, wrap_ttl="15m"
)
assert "wrap_info" in response, repr(response)
assert response["wrap_info"] is not None, repr(response)
assert response["auth"] is None, repr(response)
assert response["wrap_info"]["ttl"] == 900
assert "token" in response["wrap_info"]
# unwrap
token = self.client.sys.unwrap(token=response["wrap_info"]["token"])
assert token["auth"]["client_token"]
assert token["auth"]["policies"] == expected_policies
|