|
| 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_privileges(application: 'myapp', name: 'read,write', ignore: 404) |
| 31 | + ADMIN_CLIENT.security.delete_user(username: client_username) |
| 32 | + end |
| 33 | + |
| 34 | + let(:client_username) { "query_api_keys_#{Time.new.to_i}"} |
| 35 | + |
| 36 | + let(:client) do |
| 37 | + Elasticsearch::Client.new( |
| 38 | + host: "https://#{HOST_URI.host}:#{HOST_URI.port}", |
| 39 | + user: client_username, |
| 40 | + password: 'test-password', |
| 41 | + transport_options: TRANSPORT_OPTIONS |
| 42 | + ) |
| 43 | + end |
| 44 | + |
| 45 | + it 'queries API keys' do |
| 46 | + key_name_1 = "query-key-1-#{Time.new.to_i}" |
| 47 | + response = client.security.create_api_key( |
| 48 | + body: { |
| 49 | + name: key_name_1, |
| 50 | + expiration: "1d", |
| 51 | + role_descriptors: {}, |
| 52 | + metadata: { search: "this" } |
| 53 | + } |
| 54 | + ) |
| 55 | + expect(response['name']).to eq key_name_1 |
| 56 | + expect(response['api_key']).not_to be nil |
| 57 | + api_key_id_1 = response['id'] |
| 58 | + |
| 59 | + key_name_2 = "query-key-2-#{Time.new.to_i}" |
| 60 | + response = client.security.create_api_key( |
| 61 | + body: { |
| 62 | + name: key_name_2, |
| 63 | + expiration: "2d", |
| 64 | + role_descriptors: { "role-a" => { "cluster": [ "monitor"] } }, |
| 65 | + metadata: { search: false } |
| 66 | + } |
| 67 | + ) |
| 68 | + expect(response['name']).to eq key_name_2 |
| 69 | + expect(response['api_key']).not_to be nil |
| 70 | + api_key_id_2 = response['id'] |
| 71 | + |
| 72 | + key_name_3 = "query-key-3#{Time.new.to_i}" |
| 73 | + response = client.security.create_api_key( |
| 74 | + body: { |
| 75 | + name: key_name_3, |
| 76 | + expiration: "3d", |
| 77 | + } |
| 78 | + ) |
| 79 | + expect(response['name']).to eq key_name_3 |
| 80 | + expect(response['api_key']).not_to be nil |
| 81 | + api_key_id_3 = response['id'] |
| 82 | + |
| 83 | + response = client.security.authenticate |
| 84 | + owner_name = response['username'] |
| 85 | + |
| 86 | + response = client.security.query_api_keys( |
| 87 | + body: { |
| 88 | + query: { wildcard: { name: key_name_1 }} |
| 89 | + } |
| 90 | + ) |
| 91 | + expect(response['total']).to eq 1 |
| 92 | + expect(response['count']).to eq 1 |
| 93 | + expect(response['api_keys'].first['id']).to eq api_key_id_1 |
| 94 | + |
| 95 | + response = client.security.query_api_keys( |
| 96 | + body: { |
| 97 | + query: { wildcard: { name: key_name_2 }} |
| 98 | + } |
| 99 | + ) |
| 100 | + expect(response['total']).to eq 1 |
| 101 | + expect(response['count']).to eq 1 |
| 102 | + expect(response['api_keys'].first['id']).to eq api_key_id_2 |
| 103 | + |
| 104 | + response = client.security.query_api_keys( |
| 105 | + body: { |
| 106 | + query: { wildcard: { name: key_name_3 }} |
| 107 | + } |
| 108 | + ) |
| 109 | + expect(response['total']).to eq 1 |
| 110 | + expect(response['count']).to eq 1 |
| 111 | + expect(response['api_keys'].first['id']).to eq api_key_id_3 |
| 112 | + end |
| 113 | +end |
0 commit comments