|
| 1 | +@ignore |
| 2 | +Feature: Prepare API keys for the api-key-auth feature |
| 3 | + |
| 4 | +# The hashes below are sha256 of the key strings, which is what |
| 5 | +# mongoApiKeyAuthenticator stores — the key itself never is: |
| 6 | +# |
| 7 | +# printf 'rhak_valid' | shasum -a 256 |
| 8 | +# printf 'rhak_noroles' | shasum -a 256 |
| 9 | +# printf 'rhak_expired' | shasum -a 256 |
| 10 | +# printf 'rhak_poweruser' | shasum -a 256 |
| 11 | +# |
| 12 | +# Keys live in a database of their own, created here db -> collection -> |
| 13 | +# documents. Not restheart-test: that one holds users and acl and is not a place |
| 14 | +# to be creating arbitrary collections in. |
| 15 | +# |
| 16 | +# The documents are written with PUT and `wm=upsert`. Without the write mode a |
| 17 | +# PUT on a document that does not exist yet answers 404, since the default mode |
| 18 | +# updates rather than creates — and upsert is also what makes this setup |
| 19 | +# re-runnable, where a POST would add four more keys on every run. |
| 20 | + |
| 21 | +Background: |
| 22 | + * url 'http://localhost:8080' |
| 23 | + * def basic = |
| 24 | + """ |
| 25 | + function(creds) { |
| 26 | + var temp = creds.username + ':' + creds.password; |
| 27 | + var Base64 = Java.type('java.util.Base64'); |
| 28 | + var encoded = Base64.getEncoder().encodeToString(temp.toString().getBytes()); |
| 29 | + return 'Basic ' + encoded; |
| 30 | + } |
| 31 | + """ |
| 32 | + |
| 33 | + * def admin = basic({username: 'admin', password: 'secret'}) |
| 34 | + |
| 35 | +Scenario: Create the test-apikeys db and seed the keys |
| 36 | + |
| 37 | + * header Authorization = admin |
| 38 | + Given path 'test-apikeys' |
| 39 | + When method PUT |
| 40 | + Then assert responseStatus == 201 || responseStatus == 200 |
| 41 | + |
| 42 | + * header Authorization = admin |
| 43 | + Given path 'test-apikeys/keys' |
| 44 | + When method PUT |
| 45 | + Then assert responseStatus == 201 || responseStatus == 200 |
| 46 | + |
| 47 | + # A working key. `admin` is the role the test ACL grants path-prefix "/" to, |
| 48 | + # so this one can reach /secho. |
| 49 | + * header Authorization = admin |
| 50 | + Given path 'test-apikeys/keys/valid' |
| 51 | + And param wm = 'upsert' |
| 52 | + And request |
| 53 | + """ |
| 54 | + { |
| 55 | + "hash": "cff1ff8ac100e099a885d9079ebe17919408763bca38289196d38490f335ff42", |
| 56 | + "user": "admin", |
| 57 | + "roles": ["admin"] |
| 58 | + } |
| 59 | + """ |
| 60 | + When method PUT |
| 61 | + Then assert responseStatus == 201 || responseStatus == 200 |
| 62 | + |
| 63 | + # Same principal, no roles at all. Deny-by-default made observable: the |
| 64 | + # request authenticates and then reaches nothing. |
| 65 | + * header Authorization = admin |
| 66 | + Given path 'test-apikeys/keys/noroles' |
| 67 | + And param wm = 'upsert' |
| 68 | + And request |
| 69 | + """ |
| 70 | + { |
| 71 | + "hash": "a6c408d4deabb91ca3e358f28ba7e21526249bb352e82696958b0bc30924fd45", |
| 72 | + "user": "admin", |
| 73 | + "roles": [] |
| 74 | + } |
| 75 | + """ |
| 76 | + When method PUT |
| 77 | + Then assert responseStatus == 201 || responseStatus == 200 |
| 78 | + |
| 79 | + # Expired an hour ago. Present in the collection on purpose: a TTL index |
| 80 | + # reclaims lazily, so the authenticator has to refuse it on its own. |
| 81 | + * header Authorization = admin |
| 82 | + Given path 'test-apikeys/keys/expired' |
| 83 | + And param wm = 'upsert' |
| 84 | + And request |
| 85 | + """ |
| 86 | + { |
| 87 | + "hash": "c8f0f2bb100cea93d40356502f09746413f4ebcffc2b2c411cdd9657d0af23c8", |
| 88 | + "user": "admin", |
| 89 | + "roles": ["admin"], |
| 90 | + "expiresAt": { "$date": 1000000000000 } |
| 91 | + } |
| 92 | + """ |
| 93 | + When method PUT |
| 94 | + Then assert responseStatus == 201 || responseStatus == 200 |
| 95 | + |
| 96 | + # A narrower role. The test ACL grants poweruser only GET /testdb, so this key |
| 97 | + # is what shows that the *specific* roles on the key document drive |
| 98 | + # authorization — not merely that some role arrived. |
| 99 | + * header Authorization = admin |
| 100 | + Given path 'test-apikeys/keys/poweruser' |
| 101 | + And param wm = 'upsert' |
| 102 | + And request |
| 103 | + """ |
| 104 | + { |
| 105 | + "hash": "ca8dcb2d445daf0ce1415b6e3a87c5da3bf5811281dfd87592d61569e8d9f994", |
| 106 | + "user": "admin", |
| 107 | + "roles": ["poweruser"] |
| 108 | + } |
| 109 | + """ |
| 110 | + When method PUT |
| 111 | + Then assert responseStatus == 201 || responseStatus == 200 |
0 commit comments