-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HLRC] Add support for put privileges API (#35679)
This commit adds support for API to create or update application privileges in high-level rest client.
- Loading branch information
Showing
12 changed files
with
726 additions
and
69 deletions.
There are no files selected for viewing
This file contains 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 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 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(); | ||
} | ||
|
||
} |
105 changes: 105 additions & 0 deletions
105
...est-high-level/src/main/java/org/elasticsearch/client/security/PutPrivilegesResponse.java
This file contains 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,105 @@ | ||
/* | ||
* 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) { | ||
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 { | ||
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<>()); | ||
final Map<String, Object> createdOrUpdated = (Map<String, Object>) entry.getValue(); | ||
for (String privilegeName : createdOrUpdated.keySet()) { | ||
if (createdOrUpdated.get(privilegeName) instanceof Map) { | ||
final Map<String, Object> statusMap = (Map<String, Object>) createdOrUpdated.get(privilegeName); | ||
final Object status = statusMap.get("created"); | ||
if (status instanceof Boolean) { | ||
privilegeToStatus.put(privilegeName, (Boolean) status); | ||
} else { | ||
throw new ParsingException(parser.getTokenLocation(), "Failed to parse object, unexpected structure"); | ||
} | ||
} else { | ||
throw new ParsingException(parser.getTokenLocation(), "Failed to parse object, unexpected structure"); | ||
} | ||
} | ||
} else { | ||
throw new ParsingException(parser.getTokenLocation(), "Failed to parse object, unexpected structure"); | ||
} | ||
} | ||
return new PutPrivilegesResponse(applicationPrivilegesCreatedOrUpdated); | ||
} | ||
} |
This file contains 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 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.