Skip to content

Commit aa71c74

Browse files
committed
[API] Adds service accounts endpoints
1 parent 8255cb5 commit aa71c74

10 files changed

+607
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
module Elasticsearch
19+
module XPack
20+
module API
21+
module Security
22+
module Actions
23+
# Evicts tokens from the service account token caches.
24+
# This functionality is in Beta and is subject to change. The design and
25+
# code is less mature than official GA features and is being provided
26+
# as-is with no warranties. Beta features are not subject to the support
27+
# SLA of official GA features.
28+
#
29+
# @option arguments [String] :namespace An identifier for the namespace
30+
# @option arguments [String] :service An identifier for the service name
31+
# @option arguments [List] :name A comma-separated list of service token names
32+
# @option arguments [Hash] :headers Custom HTTP headers
33+
#
34+
# @see https://www.elastic.co/guide/en/elasticsearch/reference/7.x/security-api-clear-service-token-caches.html
35+
#
36+
def clear_cached_service_tokens(arguments = {})
37+
raise ArgumentError, "Required argument 'namespace' missing" unless arguments[:namespace]
38+
raise ArgumentError, "Required argument 'service' missing" unless arguments[:service]
39+
raise ArgumentError, "Required argument 'name' missing" unless arguments[:name]
40+
41+
headers = arguments.delete(:headers) || {}
42+
43+
arguments = arguments.clone
44+
45+
_namespace = arguments.delete(:namespace)
46+
47+
_service = arguments.delete(:service)
48+
49+
_name = arguments.delete(:name)
50+
51+
method = Elasticsearch::API::HTTP_POST
52+
path = "_security/service/#{Elasticsearch::API::Utils.__listify(_namespace)}/#{Elasticsearch::API::Utils.__listify(_service)}/credential/token/#{Elasticsearch::API::Utils.__listify(_name)}/_clear_cache"
53+
params = {}
54+
55+
body = nil
56+
perform_request(method, path, params, body, headers).body
57+
end
58+
end
59+
end
60+
end
61+
end
62+
end
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
module Elasticsearch
19+
module XPack
20+
module API
21+
module Security
22+
module Actions
23+
# Creates a service account token for access without requiring basic authentication.
24+
# This functionality is in Beta and is subject to change. The design and
25+
# code is less mature than official GA features and is being provided
26+
# as-is with no warranties. Beta features are not subject to the support
27+
# SLA of official GA features.
28+
#
29+
# @option arguments [String] :namespace An identifier for the namespace
30+
# @option arguments [String] :service An identifier for the service name
31+
# @option arguments [String] :name An identifier for the token name
32+
# @option arguments [String] :refresh If `true` then refresh the affected shards to make this operation visible to search, if `wait_for` (the default) then wait for a refresh to make this operation visible to search, if `false` then do nothing with refreshes. (options: true, false, wait_for)
33+
# @option arguments [Hash] :headers Custom HTTP headers
34+
#
35+
# @see https://www.elastic.co/guide/en/elasticsearch/reference/7.x/security-api-create-service-token.html
36+
#
37+
def create_service_token(arguments = {})
38+
raise ArgumentError, "Required argument 'namespace' missing" unless arguments[:namespace]
39+
raise ArgumentError, "Required argument 'service' missing" unless arguments[:service]
40+
41+
headers = arguments.delete(:headers) || {}
42+
43+
arguments = arguments.clone
44+
45+
_namespace = arguments.delete(:namespace)
46+
47+
_service = arguments.delete(:service)
48+
49+
_name = arguments.delete(:name)
50+
51+
method = Elasticsearch::API::HTTP_PUT
52+
path = if _namespace && _service && _name
53+
"_security/service/#{Elasticsearch::API::Utils.__listify(_namespace)}/#{Elasticsearch::API::Utils.__listify(_service)}/credential/token/#{Elasticsearch::API::Utils.__listify(_name)}"
54+
else
55+
"_security/service/#{Elasticsearch::API::Utils.__listify(_namespace)}/#{Elasticsearch::API::Utils.__listify(_service)}/credential/token"
56+
end
57+
params = Elasticsearch::API::Utils.__validate_and_extract_params arguments, ParamsRegistry.get(__method__)
58+
59+
body = nil
60+
perform_request(method, path, params, body, headers).body
61+
end
62+
63+
# Register this action with its valid params when the module is loaded.
64+
#
65+
# @since 6.2.0
66+
ParamsRegistry.register(:create_service_token, [
67+
:refresh
68+
].freeze)
69+
end
70+
end
71+
end
72+
end
73+
end
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
module Elasticsearch
19+
module XPack
20+
module API
21+
module Security
22+
module Actions
23+
# Deletes a service account token.
24+
# This functionality is in Beta and is subject to change. The design and
25+
# code is less mature than official GA features and is being provided
26+
# as-is with no warranties. Beta features are not subject to the support
27+
# SLA of official GA features.
28+
#
29+
# @option arguments [String] :namespace An identifier for the namespace
30+
# @option arguments [String] :service An identifier for the service name
31+
# @option arguments [String] :name An identifier for the token name
32+
# @option arguments [String] :refresh If `true` then refresh the affected shards to make this operation visible to search, if `wait_for` (the default) then wait for a refresh to make this operation visible to search, if `false` then do nothing with refreshes. (options: true, false, wait_for)
33+
# @option arguments [Hash] :headers Custom HTTP headers
34+
#
35+
# @see https://www.elastic.co/guide/en/elasticsearch/reference/7.x/security-api-delete-service-token.html
36+
#
37+
def delete_service_token(arguments = {})
38+
raise ArgumentError, "Required argument 'namespace' missing" unless arguments[:namespace]
39+
raise ArgumentError, "Required argument 'service' missing" unless arguments[:service]
40+
raise ArgumentError, "Required argument 'name' missing" unless arguments[:name]
41+
42+
headers = arguments.delete(:headers) || {}
43+
44+
arguments = arguments.clone
45+
46+
_namespace = arguments.delete(:namespace)
47+
48+
_service = arguments.delete(:service)
49+
50+
_name = arguments.delete(:name)
51+
52+
method = Elasticsearch::API::HTTP_DELETE
53+
path = "_security/service/#{Elasticsearch::API::Utils.__listify(_namespace)}/#{Elasticsearch::API::Utils.__listify(_service)}/credential/token/#{Elasticsearch::API::Utils.__listify(_name)}"
54+
params = Elasticsearch::API::Utils.__validate_and_extract_params arguments, ParamsRegistry.get(__method__)
55+
56+
body = nil
57+
perform_request(method, path, params, body, headers).body
58+
end
59+
60+
# Register this action with its valid params when the module is loaded.
61+
#
62+
# @since 6.2.0
63+
ParamsRegistry.register(:delete_service_token, [
64+
:refresh
65+
].freeze)
66+
end
67+
end
68+
end
69+
end
70+
end
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
module Elasticsearch
19+
module XPack
20+
module API
21+
module Security
22+
module Actions
23+
# Retrieves information about service accounts.
24+
# This functionality is in Beta and is subject to change. The design and
25+
# code is less mature than official GA features and is being provided
26+
# as-is with no warranties. Beta features are not subject to the support
27+
# SLA of official GA features.
28+
#
29+
# @option arguments [String] :namespace An identifier for the namespace
30+
# @option arguments [String] :service An identifier for the service name
31+
# @option arguments [Hash] :headers Custom HTTP headers
32+
#
33+
# @see https://www.elastic.co/guide/en/elasticsearch/reference/7.x/security-api-get-service-accounts.html
34+
#
35+
def get_service_accounts(arguments = {})
36+
headers = arguments.delete(:headers) || {}
37+
38+
arguments = arguments.clone
39+
40+
_namespace = arguments.delete(:namespace)
41+
42+
_service = arguments.delete(:service)
43+
44+
method = Elasticsearch::API::HTTP_GET
45+
path = if _namespace && _service
46+
"_security/service/#{Elasticsearch::API::Utils.__listify(_namespace)}/#{Elasticsearch::API::Utils.__listify(_service)}"
47+
elsif _namespace
48+
"_security/service/#{Elasticsearch::API::Utils.__listify(_namespace)}"
49+
else
50+
"_security/service"
51+
end
52+
params = {}
53+
54+
body = nil
55+
perform_request(method, path, params, body, headers).body
56+
end
57+
end
58+
end
59+
end
60+
end
61+
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
module Elasticsearch
19+
module XPack
20+
module API
21+
module Security
22+
module Actions
23+
# Retrieves information of all service credentials for a service account.
24+
# This functionality is in Beta and is subject to change. The design and
25+
# code is less mature than official GA features and is being provided
26+
# as-is with no warranties. Beta features are not subject to the support
27+
# SLA of official GA features.
28+
#
29+
# @option arguments [String] :namespace An identifier for the namespace
30+
# @option arguments [String] :service An identifier for the service name
31+
# @option arguments [Hash] :headers Custom HTTP headers
32+
#
33+
# @see https://www.elastic.co/guide/en/elasticsearch/reference/7.x/security-api-get-service-credentials.html
34+
#
35+
def get_service_credentials(arguments = {})
36+
raise ArgumentError, "Required argument 'namespace' missing" unless arguments[:namespace]
37+
raise ArgumentError, "Required argument 'service' missing" unless arguments[:service]
38+
39+
headers = arguments.delete(:headers) || {}
40+
41+
arguments = arguments.clone
42+
43+
_namespace = arguments.delete(:namespace)
44+
45+
_service = arguments.delete(:service)
46+
47+
method = Elasticsearch::API::HTTP_GET
48+
path = "_security/service/#{Elasticsearch::API::Utils.__listify(_namespace)}/#{Elasticsearch::API::Utils.__listify(_service)}/credential"
49+
params = {}
50+
51+
body = nil
52+
perform_request(method, path, params, body, headers).body
53+
end
54+
end
55+
end
56+
end
57+
end
58+
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 'spec_helper'
19+
20+
describe 'client#security#clear_cached_service_tokens' do
21+
let(:expected_args) do
22+
[
23+
'POST',
24+
'_security/service/foo/bar/credential/token/service_token/_clear_cache',
25+
{},
26+
nil,
27+
{}
28+
]
29+
end
30+
31+
it 'performs the request' do
32+
expect(client_double.security.clear_cached_service_tokens(
33+
namespace: 'foo', service: 'bar', name: 'service_token')
34+
).to eq({})
35+
end
36+
37+
let(:client) do
38+
Class.new { include Elasticsearch::XPack::API }.new
39+
end
40+
41+
it 'requires the :namespace argument' do
42+
expect {
43+
client.security.clear_cached_service_tokens(service: 'bar', name: 'service_token')
44+
}.to raise_exception(ArgumentError)
45+
end
46+
47+
it 'requires the :service argument' do
48+
expect {
49+
client.security.clear_cached_service_tokens(namespace: 'foo', name: 'service_token')
50+
}.to raise_exception(ArgumentError)
51+
end
52+
53+
it 'requires the :name argument' do
54+
expect {
55+
client.security.clear_cached_service_tokens(service: 'bar', namespace: 'foo')
56+
}.to raise_exception(ArgumentError)
57+
end
58+
end

0 commit comments

Comments
 (0)