|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.core.security.action.user; |
| 7 | + |
| 8 | +import org.elasticsearch.action.ActionResponse; |
| 9 | +import org.elasticsearch.common.Strings; |
| 10 | +import org.elasticsearch.common.bytes.BytesReference; |
| 11 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 12 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 13 | +import org.elasticsearch.common.io.stream.Writeable; |
| 14 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 15 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 16 | +import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; |
| 17 | +import org.elasticsearch.xpack.core.security.authz.permission.FieldPermissionsDefinition; |
| 18 | +import org.elasticsearch.xpack.core.security.authz.privilege.ConditionalClusterPrivilege; |
| 19 | +import org.elasticsearch.xpack.core.security.authz.privilege.ConditionalClusterPrivileges; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.Objects; |
| 25 | +import java.util.Set; |
| 26 | +import java.util.TreeSet; |
| 27 | +import java.util.stream.Collectors; |
| 28 | + |
| 29 | +/** |
| 30 | + * Response for a {@link GetUserPrivilegesRequest} |
| 31 | + */ |
| 32 | +public final class GetUserPrivilegesResponse extends ActionResponse { |
| 33 | + |
| 34 | + private Set<String> cluster; |
| 35 | + private Set<ConditionalClusterPrivilege> conditionalCluster; |
| 36 | + private Set<Indices> index; |
| 37 | + private Set<RoleDescriptor.ApplicationResourcePrivileges> application; |
| 38 | + private Set<String> runAs; |
| 39 | + |
| 40 | + public GetUserPrivilegesResponse() { |
| 41 | + this(Collections.emptySet(), Collections.emptySet(), Collections.emptySet(), Collections.emptySet(), Collections.emptySet()); |
| 42 | + } |
| 43 | + |
| 44 | + public GetUserPrivilegesResponse(Set<String> cluster, Set<ConditionalClusterPrivilege> conditionalCluster, |
| 45 | + Set<Indices> index, |
| 46 | + Set<RoleDescriptor.ApplicationResourcePrivileges> application, |
| 47 | + Set<String> runAs) { |
| 48 | + this.cluster = Collections.unmodifiableSet(cluster); |
| 49 | + this.conditionalCluster = Collections.unmodifiableSet(conditionalCluster); |
| 50 | + this.index = Collections.unmodifiableSet(index); |
| 51 | + this.application = Collections.unmodifiableSet(application); |
| 52 | + this.runAs = Collections.unmodifiableSet(runAs); |
| 53 | + } |
| 54 | + |
| 55 | + public Set<String> getClusterPrivileges() { |
| 56 | + return cluster; |
| 57 | + } |
| 58 | + |
| 59 | + public Set<ConditionalClusterPrivilege> getConditionalClusterPrivileges() { |
| 60 | + return conditionalCluster; |
| 61 | + } |
| 62 | + |
| 63 | + public Set<Indices> getIndexPrivileges() { |
| 64 | + return index; |
| 65 | + } |
| 66 | + |
| 67 | + public Set<RoleDescriptor.ApplicationResourcePrivileges> getApplicationPrivileges() { |
| 68 | + return application; |
| 69 | + } |
| 70 | + |
| 71 | + public Set<String> getRunAs() { |
| 72 | + return runAs; |
| 73 | + } |
| 74 | + |
| 75 | + public void readFrom(StreamInput in) throws IOException { |
| 76 | + super.readFrom(in); |
| 77 | + cluster = Collections.unmodifiableSet(in.readSet(StreamInput::readString)); |
| 78 | + conditionalCluster = Collections.unmodifiableSet(in.readSet(ConditionalClusterPrivileges.READER)); |
| 79 | + index = Collections.unmodifiableSet(in.readSet(Indices::new)); |
| 80 | + application = Collections.unmodifiableSet(in.readSet(RoleDescriptor.ApplicationResourcePrivileges::createFrom)); |
| 81 | + runAs = Collections.unmodifiableSet(in.readSet(StreamInput::readString)); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void writeTo(StreamOutput out) throws IOException { |
| 86 | + super.writeTo(out); |
| 87 | + out.writeCollection(cluster, StreamOutput::writeString); |
| 88 | + out.writeCollection(conditionalCluster, ConditionalClusterPrivileges.WRITER); |
| 89 | + out.writeCollection(index, (o, p) -> p.writeTo(o)); |
| 90 | + out.writeCollection(application, (o, p) -> p.writeTo(o)); |
| 91 | + out.writeCollection(runAs, StreamOutput::writeString); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public boolean equals(Object other) { |
| 96 | + if (this == other) { |
| 97 | + return true; |
| 98 | + } |
| 99 | + if (other == null || getClass() != other.getClass()) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + final GetUserPrivilegesResponse that = (GetUserPrivilegesResponse) other; |
| 103 | + return Objects.equals(cluster, that.cluster) && |
| 104 | + Objects.equals(conditionalCluster, that.conditionalCluster) && |
| 105 | + Objects.equals(index, that.index) && |
| 106 | + Objects.equals(application, that.application) && |
| 107 | + Objects.equals(runAs, that.runAs); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public int hashCode() { |
| 112 | + return Objects.hash(cluster, conditionalCluster, index, application, runAs); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * This is modelled on {@link RoleDescriptor.IndicesPrivileges}, with support for multiple DLS and FLS field sets. |
| 117 | + */ |
| 118 | + public static class Indices implements ToXContentObject, Writeable { |
| 119 | + |
| 120 | + private final Set<String> indices; |
| 121 | + private final Set<String> privileges; |
| 122 | + private final Set<FieldPermissionsDefinition.FieldGrantExcludeGroup> fieldSecurity; |
| 123 | + private final Set<BytesReference> queries; |
| 124 | + |
| 125 | + public Indices(Collection<String> indices, Collection<String> privileges, |
| 126 | + Set<FieldPermissionsDefinition.FieldGrantExcludeGroup> fieldSecurity, Set<BytesReference> queries) { |
| 127 | + // The use of TreeSet is to provide a consistent order that can be relied upon in tests |
| 128 | + this.indices = Collections.unmodifiableSet(new TreeSet<>(Objects.requireNonNull(indices))); |
| 129 | + this.privileges = Collections.unmodifiableSet(new TreeSet<>(Objects.requireNonNull(privileges))); |
| 130 | + this.fieldSecurity = Collections.unmodifiableSet(Objects.requireNonNull(fieldSecurity)); |
| 131 | + this.queries = Collections.unmodifiableSet(Objects.requireNonNull(queries)); |
| 132 | + } |
| 133 | + |
| 134 | + public Indices(StreamInput in) throws IOException { |
| 135 | + indices = Collections.unmodifiableSet(in.readSet(StreamInput::readString)); |
| 136 | + privileges = Collections.unmodifiableSet(in.readSet(StreamInput::readString)); |
| 137 | + fieldSecurity = Collections.unmodifiableSet(in.readSet(input -> { |
| 138 | + final String[] grant = input.readOptionalStringArray(); |
| 139 | + final String[] exclude = input.readOptionalStringArray(); |
| 140 | + return new FieldPermissionsDefinition.FieldGrantExcludeGroup(grant, exclude); |
| 141 | + })); |
| 142 | + queries = Collections.unmodifiableSet(in.readSet(StreamInput::readBytesReference)); |
| 143 | + } |
| 144 | + |
| 145 | + public Set<String> getIndices() { |
| 146 | + return indices; |
| 147 | + } |
| 148 | + |
| 149 | + public Set<String> getPrivileges() { |
| 150 | + return privileges; |
| 151 | + } |
| 152 | + |
| 153 | + public Set<FieldPermissionsDefinition.FieldGrantExcludeGroup> getFieldSecurity() { |
| 154 | + return fieldSecurity; |
| 155 | + } |
| 156 | + |
| 157 | + public Set<BytesReference> getQueries() { |
| 158 | + return queries; |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public String toString() { |
| 163 | + StringBuilder sb = new StringBuilder(getClass().getSimpleName()) |
| 164 | + .append("[") |
| 165 | + .append("indices=[").append(Strings.collectionToCommaDelimitedString(indices)) |
| 166 | + .append("], privileges=[").append(Strings.collectionToCommaDelimitedString(privileges)) |
| 167 | + .append("]"); |
| 168 | + if (fieldSecurity.isEmpty() == false) { |
| 169 | + sb.append(", fls=[").append(Strings.collectionToCommaDelimitedString(fieldSecurity)).append("]"); |
| 170 | + } |
| 171 | + if (queries.isEmpty() == false) { |
| 172 | + sb.append(", dls=[") |
| 173 | + .append(queries.stream().map(BytesReference::utf8ToString).collect(Collectors.joining(","))) |
| 174 | + .append("]"); |
| 175 | + } |
| 176 | + sb.append("]"); |
| 177 | + return sb.toString(); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public boolean equals(Object o) { |
| 182 | + if (this == o) return true; |
| 183 | + if (o == null || getClass() != o.getClass()) return false; |
| 184 | + |
| 185 | + Indices that = (Indices) o; |
| 186 | + |
| 187 | + return this.indices.equals(that.indices) |
| 188 | + && this.privileges.equals(that.privileges) |
| 189 | + && this.fieldSecurity.equals(that.fieldSecurity) |
| 190 | + && this.queries.equals(that.queries); |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public int hashCode() { |
| 195 | + return Objects.hash(indices, privileges, fieldSecurity, queries); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 200 | + builder.startObject(); |
| 201 | + builder.field(RoleDescriptor.Fields.NAMES.getPreferredName(), indices); |
| 202 | + builder.field(RoleDescriptor.Fields.PRIVILEGES.getPreferredName(), privileges); |
| 203 | + if (fieldSecurity.stream().anyMatch(g -> nonEmpty(g.getGrantedFields()) || nonEmpty(g.getExcludedFields()))) { |
| 204 | + builder.startArray(RoleDescriptor.Fields.FIELD_PERMISSIONS.getPreferredName()); |
| 205 | + for (FieldPermissionsDefinition.FieldGrantExcludeGroup group : this.fieldSecurity) { |
| 206 | + builder.startObject(); |
| 207 | + if (nonEmpty(group.getGrantedFields())) { |
| 208 | + builder.array(RoleDescriptor.Fields.GRANT_FIELDS.getPreferredName(), group.getGrantedFields()); |
| 209 | + } |
| 210 | + if (nonEmpty(group.getExcludedFields())) { |
| 211 | + builder.array(RoleDescriptor.Fields.EXCEPT_FIELDS.getPreferredName(), group.getExcludedFields()); |
| 212 | + } |
| 213 | + builder.endObject(); |
| 214 | + } |
| 215 | + builder.endArray(); |
| 216 | + } |
| 217 | + if (queries.isEmpty() == false) { |
| 218 | + builder.startArray(RoleDescriptor.Fields.QUERY.getPreferredName()); |
| 219 | + for (BytesReference q : queries) { |
| 220 | + builder.value(q.utf8ToString()); |
| 221 | + } |
| 222 | + builder.endArray(); |
| 223 | + } |
| 224 | + return builder.endObject(); |
| 225 | + } |
| 226 | + |
| 227 | + private boolean nonEmpty(String[] grantedFields) { |
| 228 | + return grantedFields != null && grantedFields.length != 0; |
| 229 | + } |
| 230 | + |
| 231 | + @Override |
| 232 | + public void writeTo(StreamOutput out) throws IOException { |
| 233 | + out.writeCollection(indices, StreamOutput::writeString); |
| 234 | + out.writeCollection(privileges, StreamOutput::writeString); |
| 235 | + out.writeCollection(fieldSecurity, (output, fields) -> { |
| 236 | + output.writeOptionalStringArray(fields.getGrantedFields()); |
| 237 | + output.writeOptionalStringArray(fields.getExcludedFields()); |
| 238 | + }); |
| 239 | + out.writeCollection(queries, StreamOutput::writeBytesReference); |
| 240 | + } |
| 241 | + } |
| 242 | +} |
0 commit comments