Replace Streamable w/ Writeable in RoleDescriptor (#34544)

This commit replaces usage of Streamable with Writeable within the
RoleDescriptor class (and inner classes).

Relates: #34389
This commit is contained in:
Tim Vernum 2018-10-18 17:28:43 +11:00 committed by GitHub
parent 9200e15b74
commit 47e9082bb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 56 deletions

View File

@ -165,10 +165,10 @@ public class PutRoleRequest extends ActionRequest implements WriteRequest<PutRol
int indicesSize = in.readVInt(); int indicesSize = in.readVInt();
indicesPrivileges = new ArrayList<>(indicesSize); indicesPrivileges = new ArrayList<>(indicesSize);
for (int i = 0; i < indicesSize; i++) { for (int i = 0; i < indicesSize; i++) {
indicesPrivileges.add(RoleDescriptor.IndicesPrivileges.createFrom(in)); indicesPrivileges.add(new RoleDescriptor.IndicesPrivileges(in));
} }
if (in.getVersion().onOrAfter(Version.V_6_4_0)) { if (in.getVersion().onOrAfter(Version.V_6_4_0)) {
applicationPrivileges = in.readList(RoleDescriptor.ApplicationResourcePrivileges::createFrom); applicationPrivileges = in.readList(RoleDescriptor.ApplicationResourcePrivileges::new);
conditionalClusterPrivileges = ConditionalClusterPrivileges.readArray(in); conditionalClusterPrivileges = ConditionalClusterPrivileges.readArray(in);
} }
runAs = in.readStringArray(); runAs = in.readStringArray();
@ -186,7 +186,7 @@ public class PutRoleRequest extends ActionRequest implements WriteRequest<PutRol
index.writeTo(out); index.writeTo(out);
} }
if (out.getVersion().onOrAfter(Version.V_6_4_0)) { if (out.getVersion().onOrAfter(Version.V_6_4_0)) {
out.writeStreamableList(applicationPrivileges); out.writeList(applicationPrivileges);
ConditionalClusterPrivileges.writeArray(out, this.conditionalClusterPrivileges); ConditionalClusterPrivileges.writeArray(out, this.conditionalClusterPrivileges);
} }
out.writeStringArray(runAs); out.writeStringArray(runAs);

View File

@ -107,10 +107,10 @@ public class HasPrivilegesRequest extends ActionRequest implements UserRequest {
int indexSize = in.readVInt(); int indexSize = in.readVInt();
indexPrivileges = new RoleDescriptor.IndicesPrivileges[indexSize]; indexPrivileges = new RoleDescriptor.IndicesPrivileges[indexSize];
for (int i = 0; i < indexSize; i++) { for (int i = 0; i < indexSize; i++) {
indexPrivileges[i] = RoleDescriptor.IndicesPrivileges.createFrom(in); indexPrivileges[i] = new RoleDescriptor.IndicesPrivileges(in);
} }
if (in.getVersion().onOrAfter(Version.V_6_4_0)) { if (in.getVersion().onOrAfter(Version.V_6_4_0)) {
applicationPrivileges = in.readArray(ApplicationResourcePrivileges::createFrom, ApplicationResourcePrivileges[]::new); applicationPrivileges = in.readArray(ApplicationResourcePrivileges::new, ApplicationResourcePrivileges[]::new);
} }
} }

View File

@ -15,7 +15,7 @@ import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ObjectParser;
@ -238,7 +238,7 @@ public class RoleDescriptor implements ToXContentObject {
int size = in.readVInt(); int size = in.readVInt();
IndicesPrivileges[] indicesPrivileges = new IndicesPrivileges[size]; IndicesPrivileges[] indicesPrivileges = new IndicesPrivileges[size];
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
indicesPrivileges[i] = IndicesPrivileges.createFrom(in); indicesPrivileges[i] = new IndicesPrivileges(in);
} }
String[] runAs = in.readStringArray(); String[] runAs = in.readStringArray();
Map<String, Object> metadata = in.readMap(); Map<String, Object> metadata = in.readMap();
@ -248,7 +248,7 @@ public class RoleDescriptor implements ToXContentObject {
final ApplicationResourcePrivileges[] applicationPrivileges; final ApplicationResourcePrivileges[] applicationPrivileges;
final ConditionalClusterPrivilege[] conditionalClusterPrivileges; final ConditionalClusterPrivilege[] conditionalClusterPrivileges;
if (in.getVersion().onOrAfter(Version.V_6_4_0)) { if (in.getVersion().onOrAfter(Version.V_6_4_0)) {
applicationPrivileges = in.readArray(ApplicationResourcePrivileges::createFrom, ApplicationResourcePrivileges[]::new); applicationPrivileges = in.readArray(ApplicationResourcePrivileges::new, ApplicationResourcePrivileges[]::new);
conditionalClusterPrivileges = ConditionalClusterPrivileges.readArray(in); conditionalClusterPrivileges = ConditionalClusterPrivileges.readArray(in);
} else { } else {
applicationPrivileges = ApplicationResourcePrivileges.NONE; applicationPrivileges = ApplicationResourcePrivileges.NONE;
@ -581,7 +581,7 @@ public class RoleDescriptor implements ToXContentObject {
* A class representing permissions for a group of indices mapped to * A class representing permissions for a group of indices mapped to
* privileges, field permissions, and a query. * privileges, field permissions, and a query.
*/ */
public static class IndicesPrivileges implements ToXContentObject, Streamable { public static class IndicesPrivileges implements ToXContentObject, Writeable {
private static final IndicesPrivileges[] NONE = new IndicesPrivileges[0]; private static final IndicesPrivileges[] NONE = new IndicesPrivileges[0];
@ -594,6 +594,23 @@ public class RoleDescriptor implements ToXContentObject {
private IndicesPrivileges() { private IndicesPrivileges() {
} }
public IndicesPrivileges(StreamInput in) throws IOException {
this.indices = in.readStringArray();
this.grantedFields = in.readOptionalStringArray();
this.deniedFields = in.readOptionalStringArray();
this.privileges = in.readStringArray();
this.query = in.readOptionalBytesReference();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeStringArray(indices);
out.writeOptionalStringArray(grantedFields);
out.writeOptionalStringArray(deniedFields);
out.writeStringArray(privileges);
out.writeOptionalBytesReference(query);
}
public static Builder builder() { public static Builder builder() {
return new Builder(); return new Builder();
} }
@ -722,30 +739,6 @@ public class RoleDescriptor implements ToXContentObject {
return builder.endObject(); return builder.endObject();
} }
public static IndicesPrivileges createFrom(StreamInput in) throws IOException {
IndicesPrivileges ip = new IndicesPrivileges();
ip.readFrom(in);
return ip;
}
@Override
public void readFrom(StreamInput in) throws IOException {
this.indices = in.readStringArray();
this.grantedFields = in.readOptionalStringArray();
this.deniedFields = in.readOptionalStringArray();
this.privileges = in.readStringArray();
this.query = in.readOptionalBytesReference();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeStringArray(indices);
out.writeOptionalStringArray(grantedFields);
out.writeOptionalStringArray(deniedFields);
out.writeStringArray(privileges);
out.writeOptionalBytesReference(query);
}
public static class Builder { public static class Builder {
private IndicesPrivileges indicesPrivileges = new IndicesPrivileges(); private IndicesPrivileges indicesPrivileges = new IndicesPrivileges();
@ -802,7 +795,7 @@ public class RoleDescriptor implements ToXContentObject {
} }
} }
public static class ApplicationResourcePrivileges implements ToXContentObject, Streamable { public static class ApplicationResourcePrivileges implements ToXContentObject, Writeable {
private static final ApplicationResourcePrivileges[] NONE = new ApplicationResourcePrivileges[0]; private static final ApplicationResourcePrivileges[] NONE = new ApplicationResourcePrivileges[0];
private static final ObjectParser<ApplicationResourcePrivileges.Builder, Void> PARSER = new ObjectParser<>("application", private static final ObjectParser<ApplicationResourcePrivileges.Builder, Void> PARSER = new ObjectParser<>("application",
@ -821,6 +814,19 @@ public class RoleDescriptor implements ToXContentObject {
private ApplicationResourcePrivileges() { private ApplicationResourcePrivileges() {
} }
public ApplicationResourcePrivileges(StreamInput in) throws IOException {
this.application = in.readString();
this.privileges = in.readStringArray();
this.resources = in.readStringArray();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(application);
out.writeStringArray(privileges);
out.writeStringArray(resources);
}
public static Builder builder() { public static Builder builder() {
return new Builder(); return new Builder();
} }
@ -882,26 +888,6 @@ public class RoleDescriptor implements ToXContentObject {
return builder.endObject(); return builder.endObject();
} }
public static ApplicationResourcePrivileges createFrom(StreamInput in) throws IOException {
ApplicationResourcePrivileges ip = new ApplicationResourcePrivileges();
ip.readFrom(in);
return ip;
}
@Override
public void readFrom(StreamInput in) throws IOException {
this.application = in.readString();
this.privileges = in.readStringArray();
this.resources = in.readStringArray();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(application);
out.writeStringArray(privileges);
out.writeStringArray(resources);
}
public static void write(StreamOutput out, ApplicationResourcePrivileges privileges) throws IOException { public static void write(StreamOutput out, ApplicationResourcePrivileges privileges) throws IOException {
privileges.writeTo(out); privileges.writeTo(out);
} }

View File

@ -191,7 +191,7 @@ public class IndicesPermissionTests extends ESTestCase {
indicesPrivileges.build().writeTo(out); indicesPrivileges.build().writeTo(out);
out.close(); out.close();
StreamInput in = out.bytes().streamInput(); StreamInput in = out.bytes().streamInput();
RoleDescriptor.IndicesPrivileges readIndicesPrivileges = RoleDescriptor.IndicesPrivileges.createFrom(in); RoleDescriptor.IndicesPrivileges readIndicesPrivileges = new RoleDescriptor.IndicesPrivileges(in);
assertEquals(readIndicesPrivileges, indicesPrivileges.build()); assertEquals(readIndicesPrivileges, indicesPrivileges.build());
out = new BytesStreamOutput(); out = new BytesStreamOutput();
@ -206,7 +206,7 @@ public class IndicesPermissionTests extends ESTestCase {
out.close(); out.close();
in = out.bytes().streamInput(); in = out.bytes().streamInput();
in.setVersion(Version.V_6_0_0); in.setVersion(Version.V_6_0_0);
RoleDescriptor.IndicesPrivileges readIndicesPrivileges2 = RoleDescriptor.IndicesPrivileges.createFrom(in); RoleDescriptor.IndicesPrivileges readIndicesPrivileges2 = new RoleDescriptor.IndicesPrivileges(in);
assertEquals(readIndicesPrivileges, readIndicesPrivileges2); assertEquals(readIndicesPrivileges, readIndicesPrivileges2);
} }