-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[HLRC] Add support for put privileges API #35679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bizybot
merged 17 commits into
elastic:master
from
bizybot:create-application-privileges-rest-api
Dec 9, 2018
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
914dc0b
[HLRC] Add support for put privileges API
7f8e4b1
Remove unused import.
69f158e
Merge branch 'master' into create-application-privileges-rest-api
66be0a0
Address review comments
4bf07c5
Merge branch 'master' into create-application-privileges-rest-api
99b2647
Changes after merge
9f5219b
precommit, removed unused import
8048adb
Update delete privileges to avoid using low level client
0619451
uff, forgot to run precommit
9f5b178
Address review comments
c3d47c2
Merge branch 'master' into create-application-privileges-rest-api
5690591
Address feedback, correct documentation
19fbd61
Merge branch 'master' into create-application-privileges-rest-api
a1119a8
Merge branch 'master' into create-application-privileges-rest-api
5e358e4
Remove duplicate check
a9e4df9
Merge branch 'master' into create-application-privileges-rest-api
e7f77f6
Address review comment
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...rest-high-level/src/main/java/org/elasticsearch/client/security/PutPrivilegesRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.client.security; | ||
|
|
||
| import org.elasticsearch.client.Validatable; | ||
| import org.elasticsearch.client.security.user.privileges.ApplicationPrivilege; | ||
| import org.elasticsearch.common.Nullable; | ||
| import org.elasticsearch.common.xcontent.ToXContentObject; | ||
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Map.Entry; | ||
| import java.util.Objects; | ||
| import java.util.TreeMap; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Request object for creating/updating application privileges. | ||
| */ | ||
| public final class PutPrivilegesRequest implements Validatable, ToXContentObject { | ||
|
|
||
| private final Map<String, List<ApplicationPrivilege>> privileges; | ||
| private final RefreshPolicy refreshPolicy; | ||
|
|
||
| public PutPrivilegesRequest(final List<ApplicationPrivilege> privileges, @Nullable final RefreshPolicy refreshPolicy) { | ||
| if (privileges == null || privileges.isEmpty()) { | ||
| throw new IllegalArgumentException("privileges are required"); | ||
| } | ||
| this.privileges = Collections.unmodifiableMap(privileges.stream() | ||
| .collect(Collectors.groupingBy(ApplicationPrivilege::getApplication, TreeMap::new, Collectors.toList()))); | ||
| this.refreshPolicy = refreshPolicy == null ? RefreshPolicy.IMMEDIATE : refreshPolicy; | ||
| } | ||
|
|
||
| /** | ||
| * @return a map of application name to list of | ||
| * {@link ApplicationPrivilege}s | ||
| */ | ||
| public Map<String, List<ApplicationPrivilege>> getPrivileges() { | ||
| return privileges; | ||
| } | ||
|
|
||
| public RefreshPolicy getRefreshPolicy() { | ||
| return refreshPolicy; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(privileges, refreshPolicy); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || (this.getClass() != o.getClass())) { | ||
| return false; | ||
| } | ||
| final PutPrivilegesRequest that = (PutPrivilegesRequest) o; | ||
| return privileges.equals(that.privileges) && (refreshPolicy == that.refreshPolicy); | ||
| } | ||
|
|
||
| @Override | ||
| public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException { | ||
| builder.startObject(); | ||
| for (Entry<String, List<ApplicationPrivilege>> entry : privileges.entrySet()) { | ||
| builder.field(entry.getKey()); | ||
| builder.startObject(); | ||
| for (ApplicationPrivilege applicationPrivilege : entry.getValue()) { | ||
| builder.field(applicationPrivilege.getName()); | ||
| applicationPrivilege.toXContent(builder, params); | ||
| } | ||
| builder.endObject(); | ||
| } | ||
| return builder.endObject(); | ||
| } | ||
|
|
||
| } | ||
93 changes: 93 additions & 0 deletions
93
...est-high-level/src/main/java/org/elasticsearch/client/security/PutPrivilegesResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.client.security; | ||
|
|
||
| import org.elasticsearch.common.ParsingException; | ||
| import org.elasticsearch.common.Strings; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Map.Entry; | ||
|
|
||
| /** | ||
| * Response when creating/updating one or more application privileges to the | ||
| * security index. | ||
| */ | ||
| public final class PutPrivilegesResponse { | ||
|
|
||
| /* | ||
| * Map of application name to a map of privilege name to boolean denoting | ||
| * created or update status. | ||
| */ | ||
| private final Map<String, Map<String, Boolean>> applicationPrivilegesCreatedOrUpdated; | ||
|
|
||
| public PutPrivilegesResponse(final Map<String, Map<String, Boolean>> applicationPrivilegesCreatedOrUpdated) { | ||
| this.applicationPrivilegesCreatedOrUpdated = Collections.unmodifiableMap(applicationPrivilegesCreatedOrUpdated); | ||
| } | ||
|
|
||
| /** | ||
| * Get response status for the request to create or update application | ||
| * privileges. | ||
| * | ||
| * @param applicationName application name as specified in the request | ||
| * @param privilegeName privilege name as specified in the request | ||
| * @return {@code true} if the privilege was created, {@code false} if the | ||
| * privilege was updated | ||
| * @throws IllegalArgumentException thrown for unknown application name or | ||
| * privilege name. | ||
| */ | ||
| public boolean wasCreated(final String applicationName, final String privilegeName) { | ||
| if (Strings.hasText(applicationName) == false) { | ||
jkakavas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new IllegalArgumentException("application name is required"); | ||
| } | ||
| if (Strings.hasText(privilegeName) == false) { | ||
| throw new IllegalArgumentException("privilege name is required"); | ||
| } | ||
| if (applicationPrivilegesCreatedOrUpdated.get(applicationName) == null | ||
| || applicationPrivilegesCreatedOrUpdated.get(applicationName).get(privilegeName) == null) { | ||
| throw new IllegalArgumentException("application name or privilege name not found in the response"); | ||
| } | ||
| return applicationPrivilegesCreatedOrUpdated.get(applicationName).get(privilegeName); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public static PutPrivilegesResponse fromXContent(final XContentParser parser) throws IOException { | ||
tvernum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| final Map<String, Map<String, Boolean>> applicationPrivilegesCreatedOrUpdated = new HashMap<>(); | ||
| XContentParser.Token token = parser.currentToken(); | ||
| if (token == null) { | ||
| token = parser.nextToken(); | ||
| } | ||
| final Map<String, Object> appNameToPrivStatus = parser.map(); | ||
| for (Entry<String, Object> entry : appNameToPrivStatus.entrySet()) { | ||
| if (entry.getValue() instanceof Map) { | ||
| final Map<String, Boolean> privilegeToStatus = applicationPrivilegesCreatedOrUpdated.computeIfAbsent(entry.getKey(), | ||
| (a) -> new HashMap<>()); | ||
| Map<String, Object> createdOrUpdated = (Map<String, Object>) entry.getValue(); | ||
| createdOrUpdated.forEach((k, v) -> privilegeToStatus.put(k, ((Map<String, Boolean>) v).get("created"))); | ||
bizybot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| throw new ParsingException(parser.getTokenLocation(), "Failed to parse object, unexpected structure"); | ||
| } | ||
| } | ||
| return new PutPrivilegesResponse(applicationPrivilegesCreatedOrUpdated); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.