|
| 1 | +# Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +# license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright |
| 4 | +# ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +# the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +require 'base64' |
| 19 | +require_relative '../platinum_helper' |
| 20 | + |
| 21 | +describe 'API keys' do |
| 22 | + before do |
| 23 | + ADMIN_CLIENT.security.put_user( |
| 24 | + username: client_username, |
| 25 | + body: { password: 'test-password', roles: ['superuser'] } |
| 26 | + ) |
| 27 | + end |
| 28 | + |
| 29 | + after do |
| 30 | + ADMIN_CLIENT.security.delete_user(username: client_username) |
| 31 | + end |
| 32 | + |
| 33 | + let(:client_username) { "superuser#{Time.now.to_i}"} |
| 34 | + |
| 35 | + let(:client) do |
| 36 | + Elasticsearch::Client.new( |
| 37 | + host: "https://#{HOST_URI.host}:#{HOST_URI.port}", |
| 38 | + user: client_username, |
| 39 | + password: 'test-password', |
| 40 | + transport_options: TRANSPORT_OPTIONS |
| 41 | + ) |
| 42 | + end |
| 43 | + |
| 44 | + it 'gets api key (with role descriptors + metadata)' do |
| 45 | + response = client.security.create_api_key( |
| 46 | + body: { |
| 47 | + name: "api-key-role", |
| 48 | + expiration: "1d", |
| 49 | + role_descriptors: { |
| 50 | + 'role-a': { |
| 51 | + cluster: ["all"], |
| 52 | + index: [ |
| 53 | + { |
| 54 | + names: ["index-a"], |
| 55 | + privileges: ["read"] |
| 56 | + } |
| 57 | + ] |
| 58 | + } |
| 59 | + }, |
| 60 | + metadata: { |
| 61 | + string: "bean", |
| 62 | + number: 5, |
| 63 | + boolean: true |
| 64 | + } |
| 65 | + } |
| 66 | + ) |
| 67 | + expect(response['name']).to eq 'api-key-role' |
| 68 | + expect(response['id']).not_to be nil |
| 69 | + expect(response['api_key']).not_to be nil |
| 70 | + expect(response['expiration']).not_to be nil |
| 71 | + api_key_id = response['id'] |
| 72 | + api_key_name = response['name'] |
| 73 | + credentials = response['encoded'] |
| 74 | + |
| 75 | + response = client.security.authenticate |
| 76 | + owner_name = response['username'] |
| 77 | + |
| 78 | + response = client.security.get_api_key(id: api_key_id) |
| 79 | + expect(response['api_keys'].first['id']).to eq api_key_id |
| 80 | + expect(response['api_keys'].first['name']).to eq api_key_name |
| 81 | + expect(response['api_keys'].first['username']).to eq owner_name |
| 82 | + expect(response['api_keys'].first['invalidated']).to eq false |
| 83 | + expect(response['api_keys'].first['creation']).not_to be nil |
| 84 | + expect(response['api_keys'].first['metadata']['string']).to eq 'bean' |
| 85 | + expect(response['api_keys'].first['metadata']['number']).to eq 5 |
| 86 | + expect(response['api_keys'].first['metadata']['boolean']).to eq true |
| 87 | + expect(response['api_keys'].first['role_descriptors']) |
| 88 | + .to eq( |
| 89 | + { |
| 90 | + 'role-a' => { |
| 91 | + 'cluster' => ['all'], |
| 92 | + 'indices' => [ |
| 93 | + { |
| 94 | + 'names' => ['index-a'], |
| 95 | + 'privileges' => ['read'], |
| 96 | + 'allow_restricted_indices' => false |
| 97 | + } |
| 98 | + ], |
| 99 | + 'applications' => [ ], |
| 100 | + 'run_as' => [ ], |
| 101 | + 'metadata' => { }, |
| 102 | + 'transient_metadata' => { 'enabled' => true } |
| 103 | + } |
| 104 | + } |
| 105 | + ) |
| 106 | + |
| 107 | + response = client.security.get_api_key(owner: true) |
| 108 | + expect(response['api_keys'].length).to eq 1 |
| 109 | + expect(response['api_keys'].first['id']).to eq api_key_id |
| 110 | + expect(response['api_keys'].first['name']).to eq api_key_name |
| 111 | + expect(response['api_keys'].first['username']).to eq owner_name |
| 112 | + expect(response['api_keys'].first['invalidated']).to eq false |
| 113 | + expect(response['api_keys'].first['creation']).not_to be nil |
| 114 | + |
| 115 | + client = Elasticsearch::Client.new( |
| 116 | + host: "https://#{HOST_URI.host}:#{HOST_URI.port}", |
| 117 | + api_key: credentials, |
| 118 | + transport_options: TRANSPORT_OPTIONS |
| 119 | + ) |
| 120 | + |
| 121 | + response = client.security.get_api_key(id: api_key_id) |
| 122 | + expect(response['api_keys'].length).to eq 1 |
| 123 | + expect(response['api_keys'].first['id']).to eq api_key_id |
| 124 | + expect(response['api_keys'].first['name']).to eq api_key_name |
| 125 | + expect(response['api_keys'].first['username']).to eq owner_name |
| 126 | + end |
| 127 | +end |
0 commit comments