-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Create/Update role mapping API #34171
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
Changes from 5 commits
231a018
8aa1db5
8f9ec11
c568146
7c0ace3
7654961
998f28a
e83fbb5
4f78586
06944f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* | ||
| * 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.support.expressiondsl.RoleMapperExpression; | ||
| import org.elasticsearch.common.Nullable; | ||
| import org.elasticsearch.common.Strings; | ||
| 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.Objects; | ||
|
|
||
| /** | ||
| * Request object to create or update a role mapping. | ||
| */ | ||
| public final class PutRoleMappingRequest implements Validatable, ToXContentObject { | ||
|
|
||
| private String name; | ||
| private boolean enabled; | ||
| private List<String> roles; | ||
| private RoleMapperExpression rules; | ||
|
|
||
| @Nullable private Map<String, Object> metadata; | ||
|
||
| @Nullable private RefreshPolicy refreshPolicy; | ||
|
||
|
|
||
| public PutRoleMappingRequest(final String name, final boolean enabled, final List<String> roles, final RoleMapperExpression rules, | ||
| @Nullable final Map<String, Object> metadata, @Nullable final RefreshPolicy refreshPolicy) { | ||
| if (Strings.hasText(name) == false) { | ||
| throw new IllegalArgumentException("role-mapping name is missing"); | ||
| } | ||
| this.name = name; | ||
| this.enabled = enabled; | ||
| if (roles == null || roles.isEmpty()) { | ||
| throw new IllegalArgumentException("role-mapping roles are missing"); | ||
| } | ||
| this.roles = Collections.unmodifiableList(roles); | ||
| this.rules = Objects.requireNonNull(rules, "role-mapping rules are missing"); | ||
| this.metadata = (metadata == null) ? Collections.emptyMap() : metadata; | ||
| this.refreshPolicy = (refreshPolicy == null) ? RefreshPolicy.getDefault() : refreshPolicy; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public boolean isEnabled() { | ||
| return enabled; | ||
| } | ||
|
|
||
| public List<String> getRoles() { | ||
| return roles; | ||
| } | ||
|
|
||
| public RoleMapperExpression getRules() { | ||
| return rules; | ||
| } | ||
|
|
||
| public Map<String, Object> getMetadata() { | ||
| return metadata; | ||
| } | ||
|
|
||
| public RefreshPolicy getRefreshPolicy() { | ||
| return refreshPolicy; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(name, enabled, refreshPolicy, roles, rules, metadata); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (this == obj) { | ||
| return true; | ||
| } | ||
| if (obj == null) { | ||
| return false; | ||
| } | ||
| if (getClass() != obj.getClass()) { | ||
| return false; | ||
| } | ||
| final PutRoleMappingRequest other = (PutRoleMappingRequest) obj; | ||
|
|
||
| return (enabled == other.enabled) && | ||
| (refreshPolicy == other.refreshPolicy) && | ||
| Objects.equals(name, other.name) && | ||
| Objects.equals(roles, other.roles) && | ||
| Objects.equals(rules, other.rules) && | ||
| Objects.equals(metadata, other.metadata); | ||
| } | ||
|
|
||
| @Override | ||
| public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
| builder.startObject(); | ||
| builder.field("enabled", enabled); | ||
| builder.field("roles", roles); | ||
| builder.field("rules", rules); | ||
| builder.field("metadata", metadata); | ||
| return builder.endObject(); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * 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.ParseField; | ||
| import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Objects; | ||
|
|
||
| import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; | ||
|
|
||
| /** | ||
| * Response when adding/updating a role mapping. Returns a boolean field for | ||
| * whether the role mapping was created or updated. | ||
| */ | ||
| public final class PutRoleMappingResponse { | ||
|
|
||
| private boolean created; | ||
|
||
|
|
||
| public PutRoleMappingResponse(boolean created) { | ||
| super(); | ||
|
||
| this.created = created; | ||
| } | ||
|
|
||
| public boolean isCreated() { | ||
| return created; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| final PutRoleMappingResponse that = (PutRoleMappingResponse) o; | ||
| return created == that.created; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(created); | ||
| } | ||
|
|
||
| private static final ConstructingObjectParser<PutRoleMappingResponse, Void> PARSER = new ConstructingObjectParser<>( | ||
| "put_role_mapping_response", true, args -> new PutRoleMappingResponse((boolean) args[0])); | ||
| static { | ||
| PARSER.declareBoolean(constructorArg(), new ParseField("created")); | ||
| PARSER.declareObject((a,b) -> {}, (parser, context) -> null, new ParseField("role_mapping")); // ignore the role_mapping field! | ||
bizybot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public static PutRoleMappingResponse fromXContent(XContentParser parser) throws IOException { | ||
| return PARSER.parse(parser, null); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make all of the class member variables final
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, Thanks.