-
Notifications
You must be signed in to change notification settings - Fork 24.7k
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 get application privileges API (#35556)
This commits adds support for the Get Application Privileges API to the HLRC Relates: #29827
- Loading branch information
Showing
12 changed files
with
972 additions
and
6 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
93 changes: 93 additions & 0 deletions
93
...rest-high-level/src/main/java/org/elasticsearch/client/security/GetPrivilegesRequest.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,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.client.Validatable; | ||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.util.CollectionUtils; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Request object to get application privilege(s) | ||
*/ | ||
public final class GetPrivilegesRequest implements Validatable { | ||
private final String applicationName; | ||
private final String[] privilegeNames; | ||
|
||
public GetPrivilegesRequest(@Nullable final String applicationName, @Nullable final String... privilegeNames) { | ||
if ((CollectionUtils.isEmpty(privilegeNames) == false) && Strings.isNullOrEmpty(applicationName)) { | ||
throw new IllegalArgumentException("privilege cannot be specified when application is missing"); | ||
} | ||
this.applicationName = applicationName; | ||
this.privilegeNames = privilegeNames; | ||
} | ||
|
||
/** | ||
* Constructs a {@link GetPrivilegesRequest} to request all the privileges defined for all applications | ||
*/ | ||
public static GetPrivilegesRequest getAllPrivileges() { | ||
return new GetPrivilegesRequest(null); | ||
} | ||
|
||
/** | ||
* Constructs a {@link GetPrivilegesRequest} to request all the privileges defined for the specified {@code applicationName} | ||
* | ||
* @param applicationName the name of the application for which the privileges are requested | ||
*/ | ||
public static GetPrivilegesRequest getApplicationPrivileges(String applicationName) { | ||
if (Strings.isNullOrEmpty(applicationName)) { | ||
throw new IllegalArgumentException("application name is required"); | ||
} | ||
return new GetPrivilegesRequest(applicationName); | ||
} | ||
|
||
/** | ||
* @return the name of the application for which to return certain privileges | ||
*/ | ||
public String getApplicationName() { | ||
return applicationName; | ||
} | ||
|
||
/** | ||
* @return an array of privilege names to return or null if all should be returned | ||
*/ | ||
public String[] getPrivilegeNames() { | ||
return privilegeNames; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
GetPrivilegesRequest that = (GetPrivilegesRequest) o; | ||
return Objects.equals(applicationName, that.applicationName) && | ||
Arrays.equals(privilegeNames, that.privilegeNames); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = Objects.hash(applicationName); | ||
result = 31 * result + Arrays.hashCode(privilegeNames); | ||
return result; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...est-high-level/src/main/java/org/elasticsearch/client/security/GetPrivilegesResponse.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,77 @@ | ||
/* | ||
* 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.security.user.privileges.ApplicationPrivilege; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.common.xcontent.XContentParserUtils; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
/** | ||
* Get application privileges response | ||
*/ | ||
public final class GetPrivilegesResponse { | ||
|
||
private Set<ApplicationPrivilege> privileges; | ||
|
||
public Set<ApplicationPrivilege> getPrivileges() { | ||
return privileges; | ||
} | ||
|
||
public GetPrivilegesResponse(Collection<ApplicationPrivilege> privileges) { | ||
this.privileges = Collections.unmodifiableSet(new HashSet<>(privileges)); | ||
} | ||
|
||
public static GetPrivilegesResponse fromXContent(XContentParser parser) throws IOException { | ||
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation); | ||
List<ApplicationPrivilege> privileges = new ArrayList<>(); | ||
XContentParser.Token token; | ||
while ((token = parser.nextToken()) != null) { | ||
if (token == XContentParser.Token.FIELD_NAME) { | ||
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation); | ||
while (parser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
privileges.add(ApplicationPrivilege.PARSER.parse(parser, null)); | ||
} | ||
} | ||
} | ||
return new GetPrivilegesResponse(new HashSet<>(privileges)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
GetPrivilegesResponse that = (GetPrivilegesResponse) o; | ||
return Objects.equals(privileges, that.privileges); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(privileges); | ||
} | ||
} |
Oops, something went wrong.