|
| 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 | + |
| 7 | +package org.elasticsearch.xpack.core.security.action.user; |
| 8 | + |
| 9 | +import org.elasticsearch.Version; |
| 10 | +import org.elasticsearch.common.bytes.BytesReference; |
| 11 | +import org.elasticsearch.common.collect.MapBuilder; |
| 12 | +import org.elasticsearch.common.io.stream.BytesStreamOutput; |
| 13 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 14 | +import org.elasticsearch.common.xcontent.ToXContent; |
| 15 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 16 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 17 | +import org.elasticsearch.common.xcontent.XContentType; |
| 18 | +import org.elasticsearch.protocol.AbstractHlrcStreamableXContentTestCase; |
| 19 | +import org.elasticsearch.test.VersionUtils; |
| 20 | +import org.hamcrest.Matchers; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.LinkedHashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Locale; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.stream.Collectors; |
| 33 | + |
| 34 | +import static org.hamcrest.Matchers.equalTo; |
| 35 | + |
| 36 | +public class HasPrivilegesResponseTests |
| 37 | + extends AbstractHlrcStreamableXContentTestCase<HasPrivilegesResponse, org.elasticsearch.client.security.HasPrivilegesResponse> { |
| 38 | + |
| 39 | + public void testSerializationV64OrV65() throws IOException { |
| 40 | + final HasPrivilegesResponse original = randomResponse(); |
| 41 | + final Version version = VersionUtils.randomVersionBetween(random(), Version.V_6_4_0, Version.V_6_5_1); |
| 42 | + final HasPrivilegesResponse copy = serializeAndDeserialize(original, version); |
| 43 | + |
| 44 | + assertThat(copy.isCompleteMatch(), equalTo(original.isCompleteMatch())); |
| 45 | + assertThat(copy.getClusterPrivileges().entrySet(), Matchers.emptyIterable()); |
| 46 | + assertThat(copy.getIndexPrivileges(), equalTo(original.getIndexPrivileges())); |
| 47 | + assertThat(copy.getApplicationPrivileges(), equalTo(original.getApplicationPrivileges())); |
| 48 | + } |
| 49 | + |
| 50 | + public void testSerializationV63() throws IOException { |
| 51 | + final HasPrivilegesResponse original = randomResponse(); |
| 52 | + final HasPrivilegesResponse copy = serializeAndDeserialize(original, Version.V_6_3_0); |
| 53 | + |
| 54 | + assertThat(copy.isCompleteMatch(), equalTo(original.isCompleteMatch())); |
| 55 | + assertThat(copy.getClusterPrivileges().entrySet(), Matchers.emptyIterable()); |
| 56 | + assertThat(copy.getIndexPrivileges(), equalTo(original.getIndexPrivileges())); |
| 57 | + assertThat(copy.getApplicationPrivileges(), equalTo(Collections.emptyMap())); |
| 58 | + } |
| 59 | + |
| 60 | + public void testToXContent() throws Exception { |
| 61 | + final HasPrivilegesResponse response = new HasPrivilegesResponse("daredevil", false, |
| 62 | + Collections.singletonMap("manage", true), |
| 63 | + Arrays.asList( |
| 64 | + new HasPrivilegesResponse.ResourcePrivileges("staff", |
| 65 | + MapBuilder.<String, Boolean>newMapBuilder(new LinkedHashMap<>()) |
| 66 | + .put("read", true).put("index", true).put("delete", false).put("manage", false).map()), |
| 67 | + new HasPrivilegesResponse.ResourcePrivileges("customers", |
| 68 | + MapBuilder.<String, Boolean>newMapBuilder(new LinkedHashMap<>()) |
| 69 | + .put("read", true).put("index", true).put("delete", true).put("manage", false).map()) |
| 70 | + ), Collections.emptyMap()); |
| 71 | + |
| 72 | + final XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent()); |
| 73 | + response.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 74 | + BytesReference bytes = BytesReference.bytes(builder); |
| 75 | + |
| 76 | + final String json = bytes.utf8ToString(); |
| 77 | + assertThat(json, equalTo("{" + |
| 78 | + "\"username\":\"daredevil\"," + |
| 79 | + "\"has_all_requested\":false," + |
| 80 | + "\"cluster\":{\"manage\":true}," + |
| 81 | + "\"index\":{" + |
| 82 | + "\"customers\":{\"read\":true,\"index\":true,\"delete\":true,\"manage\":false}," + |
| 83 | + "\"staff\":{\"read\":true,\"index\":true,\"delete\":false,\"manage\":false}" + |
| 84 | + "}," + |
| 85 | + "\"application\":{}" + |
| 86 | + "}")); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + protected boolean supportsUnknownFields() { |
| 91 | + // Because we have nested objects with { string : boolean }, unknown fields cause parsing problems |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + protected HasPrivilegesResponse createBlankInstance() { |
| 97 | + return new HasPrivilegesResponse(); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + protected HasPrivilegesResponse createTestInstance() { |
| 102 | + return randomResponse(); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public org.elasticsearch.client.security.HasPrivilegesResponse doHlrcParseInstance(XContentParser parser) throws IOException { |
| 107 | + return org.elasticsearch.client.security.HasPrivilegesResponse.fromXContent(parser); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public HasPrivilegesResponse convertHlrcToInternal(org.elasticsearch.client.security.HasPrivilegesResponse hlrc) { |
| 112 | + return new HasPrivilegesResponse( |
| 113 | + hlrc.getUsername(), |
| 114 | + hlrc.hasAllRequested(), |
| 115 | + hlrc.getClusterPrivileges(), |
| 116 | + toResourcePrivileges(hlrc.getIndexPrivileges()), |
| 117 | + hlrc.getApplicationPrivileges().entrySet().stream() |
| 118 | + .collect(Collectors.toMap(Map.Entry::getKey, e -> toResourcePrivileges(e.getValue()))) |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + private static List<HasPrivilegesResponse.ResourcePrivileges> toResourcePrivileges(Map<String, Map<String, Boolean>> map) { |
| 123 | + return map.entrySet().stream() |
| 124 | + .map(e -> new HasPrivilegesResponse.ResourcePrivileges(e.getKey(), e.getValue())) |
| 125 | + .collect(Collectors.toList()); |
| 126 | + } |
| 127 | + |
| 128 | + private HasPrivilegesResponse serializeAndDeserialize(HasPrivilegesResponse original, Version version) throws IOException { |
| 129 | + logger.info("Test serialize/deserialize with version {}", version); |
| 130 | + final BytesStreamOutput out = new BytesStreamOutput(); |
| 131 | + out.setVersion(version); |
| 132 | + original.writeTo(out); |
| 133 | + |
| 134 | + final HasPrivilegesResponse copy = new HasPrivilegesResponse(); |
| 135 | + final StreamInput in = out.bytes().streamInput(); |
| 136 | + in.setVersion(version); |
| 137 | + copy.readFrom(in); |
| 138 | + assertThat(in.read(), equalTo(-1)); |
| 139 | + return copy; |
| 140 | + } |
| 141 | + |
| 142 | + private HasPrivilegesResponse randomResponse() { |
| 143 | + final String username = randomAlphaOfLengthBetween(4, 12); |
| 144 | + final Map<String, Boolean> cluster = new HashMap<>(); |
| 145 | + for (String priv : randomArray(1, 6, String[]::new, () -> randomAlphaOfLengthBetween(3, 12))) { |
| 146 | + cluster.put(priv, randomBoolean()); |
| 147 | + } |
| 148 | + final Collection<HasPrivilegesResponse.ResourcePrivileges> index = randomResourcePrivileges(); |
| 149 | + final Map<String, Collection<HasPrivilegesResponse.ResourcePrivileges>> application = new HashMap<>(); |
| 150 | + for (String app : randomArray(1, 3, String[]::new, () -> randomAlphaOfLengthBetween(3, 6).toLowerCase(Locale.ROOT))) { |
| 151 | + application.put(app, randomResourcePrivileges()); |
| 152 | + } |
| 153 | + return new HasPrivilegesResponse(username, randomBoolean(), cluster, index, application); |
| 154 | + } |
| 155 | + |
| 156 | + private Collection<HasPrivilegesResponse.ResourcePrivileges> randomResourcePrivileges() { |
| 157 | + final Collection<HasPrivilegesResponse.ResourcePrivileges> list = new ArrayList<>(); |
| 158 | + for (String resource : randomArray(1, 3, String[]::new, () -> randomAlphaOfLengthBetween(2, 6))) { |
| 159 | + final Map<String, Boolean> privileges = new HashMap<>(); |
| 160 | + for (String priv : randomArray(1, 5, String[]::new, () -> randomAlphaOfLengthBetween(3, 8))) { |
| 161 | + privileges.put(priv, randomBoolean()); |
| 162 | + } |
| 163 | + list.add(new HasPrivilegesResponse.ResourcePrivileges(resource, privileges)); |
| 164 | + } |
| 165 | + return list; |
| 166 | + } |
| 167 | +} |
0 commit comments