Skip to content

Commit

Permalink
Remove Version.V_6_x_x constants use in security (elastic#41185)
Browse files Browse the repository at this point in the history
This removes some use of the v6 constants in various parts of
security.
Mostly this is BWC testing code, which is no longer needed as ES8 will
not need to maintain compatibility with ES6.

Relates: elastic#41164
  • Loading branch information
tvernum authored and akhil10x5 committed May 2, 2019
1 parent bc81565 commit 22ec412
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.client.security.hlrc;

import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.Version;
import org.elasticsearch.client.security.HasPrivilegesResponse;
import org.elasticsearch.common.bytes.BytesReference;
Expand All @@ -32,9 +31,7 @@
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.client.AbstractHlrcStreamableXContentTestCase;
import org.elasticsearch.test.VersionUtils;
import org.elasticsearch.xpack.core.security.authz.permission.ResourcePrivileges;
import org.hamcrest.Matchers;
import org.junit.Assert;

import java.io.IOException;
Expand All @@ -55,28 +52,6 @@ public class HasPrivilegesResponseTests extends AbstractHlrcStreamableXContentTe
org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse,
HasPrivilegesResponse> {

public void testSerializationV64OrV65() throws IOException {
final org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse original = randomResponse();
final Version version = VersionUtils.randomVersionBetween(LuceneTestCase.random(), Version.V_6_4_0, Version.V_6_5_1);
final org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse copy = serializeAndDeserialize(original, version);

Assert.assertThat(copy.isCompleteMatch(), equalTo(original.isCompleteMatch()));
Assert.assertThat(copy.getClusterPrivileges().entrySet(), Matchers.emptyIterable());
Assert.assertThat(copy.getIndexPrivileges(), equalTo(original.getIndexPrivileges()));
Assert.assertThat(copy.getApplicationPrivileges(), equalTo(original.getApplicationPrivileges()));
}

public void testSerializationV63() throws IOException {
final org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse original = randomResponse();
final org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse copy =
serializeAndDeserialize(original, Version.V_6_3_0);

Assert.assertThat(copy.isCompleteMatch(), equalTo(original.isCompleteMatch()));
Assert.assertThat(copy.getClusterPrivileges().entrySet(), Matchers.emptyIterable());
Assert.assertThat(copy.getIndexPrivileges(), equalTo(original.getIndexPrivileges()));
Assert.assertThat(copy.getApplicationPrivileges(), equalTo(Collections.emptyMap()));
}

public void testToXContent() throws Exception {
final org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse response =
new org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse("daredevil",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
package org.elasticsearch.xpack.core.security.action.role;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.WriteRequest;
Expand Down Expand Up @@ -168,10 +167,8 @@ public void readFrom(StreamInput in) throws IOException {
for (int i = 0; i < indicesSize; i++) {
indicesPrivileges.add(new RoleDescriptor.IndicesPrivileges(in));
}
if (in.getVersion().onOrAfter(Version.V_6_4_0)) {
applicationPrivileges = in.readList(RoleDescriptor.ApplicationResourcePrivileges::new);
conditionalClusterPrivileges = ConditionalClusterPrivileges.readArray(in);
}
applicationPrivileges = in.readList(RoleDescriptor.ApplicationResourcePrivileges::new);
conditionalClusterPrivileges = ConditionalClusterPrivileges.readArray(in);
runAs = in.readStringArray();
refreshPolicy = RefreshPolicy.readFrom(in);
metadata = in.readMap();
Expand All @@ -186,10 +183,8 @@ public void writeTo(StreamOutput out) throws IOException {
for (RoleDescriptor.IndicesPrivileges index : indicesPrivileges) {
index.writeTo(out);
}
if (out.getVersion().onOrAfter(Version.V_6_4_0)) {
out.writeList(applicationPrivileges);
ConditionalClusterPrivileges.writeArray(out, this.conditionalClusterPrivileges);
}
out.writeList(applicationPrivileges);
ConditionalClusterPrivileges.writeArray(out, this.conditionalClusterPrivileges);
out.writeStringArray(runAs);
refreshPolicy.writeTo(out);
out.writeMap(metadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
*/
package org.elasticsearch.xpack.core.security.action.user;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.core.security.authc.Authentication;
import org.elasticsearch.xpack.core.security.user.User;

import java.io.IOException;

Expand All @@ -31,23 +29,13 @@ public Authentication authentication() {
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
if (out.getVersion().before(Version.V_6_6_0)) {
User.writeTo(authentication.getUser(), out);
} else {
authentication.writeTo(out);
}
authentication.writeTo(out);
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
if (in.getVersion().before(Version.V_6_6_0)) {
final User user = User.readFrom(in);
final Authentication.RealmRef unknownRealm = new Authentication.RealmRef("__unknown", "__unknown", "__unknown");
authentication = new Authentication(user, unknownRealm, unknownRealm);
} else {
authentication = new Authentication(in);
}
authentication = new Authentication(in);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
package org.elasticsearch.xpack.core.security.action.user;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
Expand Down Expand Up @@ -145,11 +144,7 @@ public Indices(StreamInput in) throws IOException {
return new FieldPermissionsDefinition.FieldGrantExcludeGroup(grant, exclude);
}));
queries = Collections.unmodifiableSet(in.readSet(StreamInput::readBytesReference));
if (in.getVersion().onOrAfter(Version.V_6_7_0)) {
this.allowRestrictedIndices = in.readBoolean();
} else {
this.allowRestrictedIndices = false;
}
this.allowRestrictedIndices = in.readBoolean();
}

public Set<String> getIndices() {
Expand Down Expand Up @@ -254,9 +249,7 @@ public void writeTo(StreamOutput out) throws IOException {
output.writeOptionalStringArray(fields.getExcludedFields());
});
out.writeCollection(queries, StreamOutput::writeBytesReference);
if (out.getVersion().onOrAfter(Version.V_6_7_0)) {
out.writeBoolean(allowRestrictedIndices);
}
out.writeBoolean(allowRestrictedIndices);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
package org.elasticsearch.xpack.core.security.action.user;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.common.io.stream.StreamInput;
Expand Down Expand Up @@ -109,9 +108,7 @@ public void readFrom(StreamInput in) throws IOException {
for (int i = 0; i < indexSize; i++) {
indexPrivileges[i] = new RoleDescriptor.IndicesPrivileges(in);
}
if (in.getVersion().onOrAfter(Version.V_6_4_0)) {
applicationPrivileges = in.readArray(ApplicationResourcePrivileges::new, ApplicationResourcePrivileges[]::new);
}
applicationPrivileges = in.readArray(ApplicationResourcePrivileges::new, ApplicationResourcePrivileges[]::new);
}

@Override
Expand All @@ -123,9 +120,7 @@ public void writeTo(StreamOutput out) throws IOException {
for (RoleDescriptor.IndicesPrivileges priv : indexPrivileges) {
priv.writeTo(out);
}
if (out.getVersion().onOrAfter(Version.V_6_4_0)) {
out.writeArray(ApplicationResourcePrivileges::write, applicationPrivileges);
}
out.writeArray(ApplicationResourcePrivileges::write, applicationPrivileges);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
package org.elasticsearch.xpack.core.security.action.user;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
Expand Down Expand Up @@ -103,16 +102,10 @@ public int hashCode() {
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
completeMatch = in.readBoolean();
if (in.getVersion().onOrAfter(Version.V_6_6_0 )) {
cluster = in.readMap(StreamInput::readString, StreamInput::readBoolean);
}
cluster = in.readMap(StreamInput::readString, StreamInput::readBoolean);
index = readResourcePrivileges(in);
if (in.getVersion().onOrAfter(Version.V_6_4_0)) {
application = in.readMap(StreamInput::readString, HasPrivilegesResponse::readResourcePrivileges);
}
if (in.getVersion().onOrAfter(Version.V_6_6_0)) {
username = in.readString();
}
application = in.readMap(StreamInput::readString, HasPrivilegesResponse::readResourcePrivileges);
username = in.readString();
}

private static Set<ResourcePrivileges> readResourcePrivileges(StreamInput in) throws IOException {
Expand All @@ -130,16 +123,10 @@ private static Set<ResourcePrivileges> readResourcePrivileges(StreamInput in) th
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeBoolean(completeMatch);
if (out.getVersion().onOrAfter(Version.V_6_6_0)) {
out.writeMap(cluster, StreamOutput::writeString, StreamOutput::writeBoolean);
}
out.writeMap(cluster, StreamOutput::writeString, StreamOutput::writeBoolean);
writeResourcePrivileges(out, index);
if (out.getVersion().onOrAfter(Version.V_6_4_0)) {
out.writeMap(application, StreamOutput::writeString, HasPrivilegesResponse::writeResourcePrivileges);
}
if (out.getVersion().onOrAfter(Version.V_6_6_0)) {
out.writeString(username);
}
out.writeMap(application, StreamOutput::writeString, HasPrivilegesResponse::writeResourcePrivileges);
out.writeString(username);
}

private static void writeResourcePrivileges(StreamOutput out, Set<ResourcePrivileges> privileges) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,8 @@ public Authentication(StreamInput in) throws IOException {
this.lookedUpBy = null;
}
this.version = in.getVersion();
if (in.getVersion().onOrAfter(Version.V_6_7_0)) {
type = AuthenticationType.values()[in.readVInt()];
metadata = in.readMap();
} else {
type = AuthenticationType.REALM;
metadata = Collections.emptyMap();
}
type = AuthenticationType.values()[in.readVInt()];
metadata = in.readMap();
}

public User getUser() {
Expand Down Expand Up @@ -165,10 +160,8 @@ public void writeTo(StreamOutput out) throws IOException {
} else {
out.writeBoolean(false);
}
if (out.getVersion().onOrAfter(Version.V_6_7_0)) {
out.writeVInt(type.ordinal());
out.writeMap(metadata);
}
out.writeVInt(type.ordinal());
out.writeMap(metadata);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.elasticsearch.xpack.core.security.authz;

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.Version;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
Expand Down Expand Up @@ -122,13 +121,8 @@ public RoleDescriptor(StreamInput in) throws IOException {
this.metadata = in.readMap();
this.transientMetadata = in.readMap();

if (in.getVersion().onOrAfter(Version.V_6_4_0)) {
this.applicationPrivileges = in.readArray(ApplicationResourcePrivileges::new, ApplicationResourcePrivileges[]::new);
this.conditionalClusterPrivileges = ConditionalClusterPrivileges.readArray(in);
} else {
this.applicationPrivileges = ApplicationResourcePrivileges.NONE;
this.conditionalClusterPrivileges = ConditionalClusterPrivileges.EMPTY_ARRAY;
}
this.applicationPrivileges = in.readArray(ApplicationResourcePrivileges::new, ApplicationResourcePrivileges[]::new);
this.conditionalClusterPrivileges = ConditionalClusterPrivileges.readArray(in);
}

public String getName() {
Expand Down Expand Up @@ -264,10 +258,8 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeStringArray(runAs);
out.writeMap(metadata);
out.writeMap(transientMetadata);
if (out.getVersion().onOrAfter(Version.V_6_4_0)) {
out.writeArray(ApplicationResourcePrivileges::write, applicationPrivileges);
ConditionalClusterPrivileges.writeArray(out, getConditionalClusterPrivileges());
}
out.writeArray(ApplicationResourcePrivileges::write, applicationPrivileges);
ConditionalClusterPrivileges.writeArray(out, getConditionalClusterPrivileges());
}

public static RoleDescriptor parse(String name, BytesReference source, boolean allow2xFormat, XContentType xContentType)
Expand Down Expand Up @@ -608,11 +600,7 @@ public IndicesPrivileges(StreamInput in) throws IOException {
this.deniedFields = in.readOptionalStringArray();
this.privileges = in.readStringArray();
this.query = in.readOptionalBytesReference();
if (in.getVersion().onOrAfter(Version.V_6_7_0)) {
allowRestrictedIndices = in.readBoolean();
} else {
allowRestrictedIndices = false;
}
this.allowRestrictedIndices = in.readBoolean();
}

@Override
Expand All @@ -622,9 +610,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalStringArray(deniedFields);
out.writeStringArray(privileges);
out.writeOptionalBytesReference(query);
if (out.getVersion().onOrAfter(Version.V_6_7_0)) {
out.writeBoolean(allowRestrictedIndices);
}
out.writeBoolean(allowRestrictedIndices);
}

public static Builder builder() {
Expand Down
Loading

0 comments on commit 22ec412

Please sign in to comment.