| 1 |
import pytest |
| 2 |
|
| 3 |
import consul |
| 4 |
from tests.utils import find_recursive |
| 5 |
|
| 6 |
|
| 7 |
class TestConsulAcl: |
| 8 |
def test_acl_token_permission_denied(self, acl_consul) -> None: |
| 9 |
c, _master_token, _consul_version = acl_consul |
| 10 |
|
| 11 |
|
| 12 |
pytest.raises(consul.ACLPermissionDenied, c.acl.token.list) |
| 13 |
pytest.raises(consul.ACLPermissionDenied, c.acl.token.create) |
| 14 |
pytest.raises( |
| 15 |
consul.ACLPermissionDenied, c.acl.token.update, accessor_id="00000000-0000-0000-0000-000000000002" |
| 16 |
) |
| 17 |
pytest.raises(consul.ACLPermissionDenied, c.acl.token.clone, accessor_id="00000000-0000-0000-0000-000000000002") |
| 18 |
pytest.raises(consul.ACLPermissionDenied, c.acl.token.read, accessor_id="00000000-0000-0000-0000-000000000002") |
| 19 |
pytest.raises( |
| 20 |
consul.ACLPermissionDenied, c.acl.token.delete, accessor_id="00000000-0000-0000-0000-000000000002" |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
pytest.raises(consul.ACLPermissionDenied, c.acl.token.list, token="anonymous") |
| 25 |
pytest.raises(consul.ACLPermissionDenied, c.acl.token.create, token="anonymous") |
| 26 |
pytest.raises( |
| 27 |
consul.ACLPermissionDenied, |
| 28 |
c.acl.token.update, |
| 29 |
accessor_id="00000000-0000-0000-0000-000000000002", |
| 30 |
token="anonymous", |
| 31 |
) |
| 32 |
pytest.raises( |
| 33 |
consul.ACLPermissionDenied, |
| 34 |
c.acl.token.clone, |
| 35 |
accessor_id="00000000-0000-0000-0000-000000000002", |
| 36 |
token="anonymous", |
| 37 |
) |
| 38 |
pytest.raises( |
| 39 |
consul.ACLPermissionDenied, |
| 40 |
c.acl.token.read, |
| 41 |
accessor_id="00000000-0000-0000-0000-000000000002", |
| 42 |
token="anonymous", |
| 43 |
) |
| 44 |
pytest.raises( |
| 45 |
consul.ACLPermissionDenied, |
| 46 |
c.acl.token.delete, |
| 47 |
accessor_id="00000000-0000-0000-0000-000000000002", |
| 48 |
token="anonymous", |
| 49 |
) |
| 50 |
|
| 51 |
def test_acl_token_list(self, acl_consul) -> None: |
| 52 |
c, master_token, _consul_version = acl_consul |
| 53 |
|
| 54 |
|
| 55 |
acls = c.acl.token.list(token=master_token) |
| 56 |
|
| 57 |
master_token_repr = { |
| 58 |
"Description": "Initial Management Token", |
| 59 |
"Policies": [{"ID": "00000000-0000-0000-0000-000000000001", "Name": "global-management"}], |
| 60 |
"SecretID": master_token, |
| 61 |
} |
| 62 |
anonymous_token_repr = { |
| 63 |
"AccessorID": "00000000-0000-0000-0000-000000000002", |
| 64 |
"SecretID": "anonymous", |
| 65 |
} |
| 66 |
assert find_recursive(acls, master_token_repr) |
| 67 |
assert find_recursive(acls, anonymous_token_repr) |
| 68 |
|
| 69 |
def test_acl_token_read(self, acl_consul) -> None: |
| 70 |
c, master_token, _consul_version = acl_consul |
| 71 |
|
| 72 |
|
| 73 |
pytest.raises(consul.ConsulException, c.acl.token.read, accessor_id="unknown", token=master_token) |
| 74 |
|
| 75 |
anonymous_token_repr = { |
| 76 |
"AccessorID": "00000000-0000-0000-0000-000000000002", |
| 77 |
"SecretID": "anonymous", |
| 78 |
} |
| 79 |
acl = c.acl.token.read(accessor_id="00000000-0000-0000-0000-000000000002", token=master_token) |
| 80 |
assert find_recursive(acl, anonymous_token_repr) |
| 81 |
|
| 82 |
def test_acl_token_create(self, acl_consul) -> None: |
| 83 |
c, master_token, _consul_version = acl_consul |
| 84 |
|
| 85 |
c.acl.token.create(accessor_id="00000000-DEAD-BEEF-0000-000000000000", token=master_token) |
| 86 |
c.acl.token.create(secret_id="DEADBEEF-0000-0000-0000-000000000000", token=master_token) |
| 87 |
c.acl.token.create( |
| 88 |
secret_id="00000000-A5A5-0000-0000-000000000000", |
| 89 |
accessor_id="00000000-0000-A5A5-0000-000000000000", |
| 90 |
description="some token!", |
| 91 |
token=master_token, |
| 92 |
) |
| 93 |
|
| 94 |
assert c.acl.token.read(accessor_id="00000000-DEAD-BEEF-0000-000000000000", token=master_token) |
| 95 |
assert c.acl.token.read(accessor_id="00000000-0000-A5A5-0000-000000000000", token=master_token) |
| 96 |
|
| 97 |
expected = [ |
| 98 |
{ |
| 99 |
"AccessorID": "00000000-DEAD-BEEF-0000-000000000000", |
| 100 |
"Description": "", |
| 101 |
}, |
| 102 |
{ |
| 103 |
"SecretID": "DEADBEEF-0000-0000-0000-000000000000", |
| 104 |
"Description": "", |
| 105 |
}, |
| 106 |
{ |
| 107 |
"AccessorID": "00000000-0000-A5A5-0000-000000000000", |
| 108 |
"SecretID": "00000000-A5A5-0000-0000-000000000000", |
| 109 |
"Description": "some token!", |
| 110 |
}, |
| 111 |
] |
| 112 |
acl = c.acl.token.list(token=master_token) |
| 113 |
assert find_recursive(acl, expected) |
| 114 |
|
| 115 |
def test_acl_token_clone(self, acl_consul) -> None: |
| 116 |
c, master_token, _consul_version = acl_consul |
| 117 |
|
| 118 |
assert len(c.acl.token.list(token=master_token)) == 2 |
| 119 |
|
| 120 |
|
| 121 |
pytest.raises(consul.ConsulException, c.acl.token.clone, accessor_id="unknown", token=master_token) |
| 122 |
|
| 123 |
c.acl.token.create(accessor_id="00000000-DEAD-BEEF-0000-000000000000", token=master_token) |
| 124 |
c.acl.token.clone(accessor_id="00000000-DEAD-BEEF-0000-000000000000", description="cloned", token=master_token) |
| 125 |
assert len(c.acl.token.list(token=master_token)) == 4 |
| 126 |
|
| 127 |
expected = [ |
| 128 |
{ |
| 129 |
"AccessorID": "00000000-DEAD-BEEF-0000-000000000000", |
| 130 |
}, |
| 131 |
{ |
| 132 |
"Description": "cloned", |
| 133 |
}, |
| 134 |
] |
| 135 |
acl = c.acl.token.list(token=master_token) |
| 136 |
assert find_recursive(acl, expected) |
| 137 |
|
| 138 |
def test_acl_token_update(self, acl_consul) -> None: |
| 139 |
c, master_token, _consul_version = acl_consul |
| 140 |
|
| 141 |
|
| 142 |
pytest.raises(consul.ConsulException, c.acl.token.update, accessor_id="unknown", token=master_token) |
| 143 |
|
| 144 |
assert len(c.acl.token.list(token=master_token)) == 2 |
| 145 |
c.acl.token.create( |
| 146 |
accessor_id="00000000-DEAD-BEEF-0000-000000000000", description="original", token=master_token |
| 147 |
) |
| 148 |
assert len(c.acl.token.list(token=master_token)) == 3 |
| 149 |
c.acl.token.update( |
| 150 |
accessor_id="00000000-DEAD-BEEF-0000-000000000000", description="updated", token=master_token |
| 151 |
) |
| 152 |
assert len(c.acl.token.list(token=master_token)) == 3 |
| 153 |
|
| 154 |
expected = { |
| 155 |
"AccessorID": "00000000-DEAD-BEEF-0000-000000000000", |
| 156 |
"Description": "updated", |
| 157 |
} |
| 158 |
acl = c.acl.token.read(accessor_id="00000000-DEAD-BEEF-0000-000000000000", token=master_token) |
| 159 |
assert find_recursive(acl, expected) |
| 160 |
|
| 161 |
def test_acl_token_delete(self, acl_consul) -> None: |
| 162 |
c, master_token, _consul_version = acl_consul |
| 163 |
|
| 164 |
assert len(c.acl.token.list(token=master_token)) == 2 |
| 165 |
c.acl.token.create(accessor_id="00000000-DEAD-BEEF-0000-000000000000", token=master_token) |
| 166 |
assert len(c.acl.token.list(token=master_token)) == 3 |
| 167 |
assert c.acl.token.read(accessor_id="00000000-DEAD-BEEF-0000-000000000000", token=master_token) |
| 168 |
|
| 169 |
|
| 170 |
c.acl.token.delete(accessor_id="00000000-DEAD-BEEF-0000-000000000000", token=master_token) |
| 171 |
assert len(c.acl.token.list(token=master_token)) == 2 |
| 172 |
pytest.raises( |
| 173 |
consul.ConsulException, |
| 174 |
c.acl.token.read, |
| 175 |
accessor_id="00000000-DEAD-BEEF-0000-000000000000", |
| 176 |
token=master_token, |
| 177 |
) |
| 178 |
|
| 179 |
def test_acl_policy_list(self, acl_consul) -> None: |
| 180 |
c, master_token, _consul_version = acl_consul |
| 181 |
|
| 182 |
|
| 183 |
policies = c.acl.policy.list(token=master_token) |
| 184 |
assert find_recursive(policies, {"ID": "00000000-0000-0000-0000-000000000001", "Name": "global-management"}) |
| 185 |
|
| 186 |
def test_acl_policy_read(self, acl_consul) -> None: |
| 187 |
c, master_token, _consul_version = acl_consul |
| 188 |
|
| 189 |
|
| 190 |
pytest.raises(consul.ConsulException, c.acl.policy.read, uuid="unknown", token=master_token) |
| 191 |
|
| 192 |
policy = c.acl.policy.read(uuid="00000000-0000-0000-0000-000000000001", token=master_token) |
| 193 |
assert find_recursive(policy, {"ID": "00000000-0000-0000-0000-000000000001", "Name": "global-management"}) |
| 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 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|