-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathUser.java
266 lines (236 loc) · 9.58 KB
/
User.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.commons.authuser;
import static org.opensearch.commons.authuser.Utils.unescapePipe;
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.apache.http.util.EntityUtils;
import org.opensearch.client.Response;
import org.opensearch.common.Nullable;
import org.opensearch.common.inject.internal.ToStringBuilder;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.commons.ConfigConstants;
import org.opensearch.core.common.Strings;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
/**
* Gets current Authenticated User - name, odfe roles.
* If security-plugin is not installed or disabled, it returns empty for user name and roles.
*/
final public class User implements Writeable, ToXContent {
// field name in toXContent
public static final String NAME_FIELD = "name";
public static final String BACKEND_ROLES_FIELD = "backend_roles";
public static final String ROLES_FIELD = "roles";
public static final String CUSTOM_ATTRIBUTE_NAMES_FIELD = "custom_attribute_names";
public static final String REQUESTED_TENANT_FIELD = "user_requested_tenant";
private final String name;
private final List<String> backendRoles;
private final List<String> roles;
private final List<String> customAttNames;
@Nullable
private final String requestedTenant;
public User() {
name = "";
backendRoles = new ArrayList<>();
roles = new ArrayList<>();
customAttNames = new ArrayList<>();
requestedTenant = null;
}
public User(final String name, final List<String> backendRoles, List<String> roles, List<String> customAttNames) {
this.name = name;
this.backendRoles = backendRoles;
this.roles = roles;
this.customAttNames = customAttNames;
this.requestedTenant = null;
}
public User(
final String name,
final List<String> backendRoles,
final List<String> roles,
final List<String> customAttNames,
@Nullable final String requestedTenant
) {
this.name = name;
this.backendRoles = backendRoles;
this.roles = roles;
this.customAttNames = customAttNames;
this.requestedTenant = requestedTenant;
}
/**
* Reponse of "GET /_opendistro/_security/authinfo"
* @param response
* @throws IOException
*/
public User(final Response response) throws IOException {
this(EntityUtils.toString(response.getEntity()));
}
@SuppressWarnings("unchecked")
public User(String json) {
if (Strings.isNullOrEmpty(json)) {
throw new IllegalArgumentException("Response json cannot be null");
}
Map<String, Object> mapValue = XContentHelper.convertToMap(JsonXContent.jsonXContent, json, false);
name = (String) mapValue.get("user_name");
backendRoles = (List<String>) mapValue.get("backend_roles");
roles = (List<String>) mapValue.get("roles");
customAttNames = (List<String>) mapValue.get("custom_attribute_names");
requestedTenant = (String) mapValue.getOrDefault("user_requested_tenant", null);
}
public User(StreamInput in) throws IOException {
name = in.readString();
backendRoles = in.readStringList();
roles = in.readStringList();
customAttNames = in.readStringList();
requestedTenant = in.readOptionalString();
}
public static User parse(XContentParser parser) throws IOException {
String name = "";
List<String> backendRoles = new ArrayList<>();
List<String> roles = new ArrayList<>();
List<String> customAttNames = new ArrayList<>();
String requestedTenant = null;
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser);
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
String fieldName = parser.currentName();
parser.nextToken();
switch (fieldName) {
case NAME_FIELD:
name = parser.text();
break;
case BACKEND_ROLES_FIELD:
ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.currentToken(), parser);
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
backendRoles.add(parser.text());
}
break;
case ROLES_FIELD:
ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.currentToken(), parser);
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
roles.add(parser.text());
}
break;
case CUSTOM_ATTRIBUTE_NAMES_FIELD:
ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.currentToken(), parser);
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
customAttNames.add(parser.text());
}
break;
case REQUESTED_TENANT_FIELD:
requestedTenant = parser.textOrNull();
break;
default:
break;
}
}
return new User(name, backendRoles, roles, customAttNames, requestedTenant);
}
/**
* User String format must be pipe separated as : user_name|backendrole1,backendrole2|roles1,role2
* @param userString
* @return
*/
public static User parse(final String userString) {
if (Strings.isNullOrEmpty(userString)) {
return null;
}
// Split on unescaped pipes (negative lookbehind for backslash)
String[] strs = userString.split("(?<!\\\\)\\|");
if ((strs.length == 0) || (Strings.isNullOrEmpty(strs[0]))) {
return null;
}
// Unescape the values
String userName = unescapePipe(strs[0].trim());
List<String> backendRoles = new ArrayList<>();
List<String> roles = new ArrayList<>();
String requestedTenant = null;
if ((strs.length > 1) && !Strings.isNullOrEmpty(strs[1])) {
backendRoles.addAll(Arrays.stream(strs[1].split(",")).map(Utils::unescapePipe).toList());
}
if ((strs.length > 2) && !Strings.isNullOrEmpty(strs[2])) {
roles.addAll(Arrays.stream(strs[2].split(",")).map(Utils::unescapePipe).toList());
}
if ((strs.length > 3) && !Strings.isNullOrEmpty(strs[3])) {
requestedTenant = unescapePipe(strs[3].trim());
}
return new User(userName, backendRoles, roles, Arrays.asList(), requestedTenant);
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder
.startObject()
.field(NAME_FIELD, name)
.field(BACKEND_ROLES_FIELD, backendRoles)
.field(ROLES_FIELD, roles)
.field(CUSTOM_ATTRIBUTE_NAMES_FIELD, customAttNames)
.field(REQUESTED_TENANT_FIELD, requestedTenant);
return builder.endObject();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(name);
out.writeStringCollection(backendRoles);
out.writeStringCollection(roles);
out.writeStringCollection(customAttNames);
out.writeOptionalString(requestedTenant);
}
@Override
public String toString() {
ToStringBuilder builder = new ToStringBuilder(this.getClass());
builder.add(NAME_FIELD, name);
builder.add(BACKEND_ROLES_FIELD, backendRoles);
builder.add(ROLES_FIELD, roles);
builder.add(CUSTOM_ATTRIBUTE_NAMES_FIELD, customAttNames);
builder.add(REQUESTED_TENANT_FIELD, requestedTenant);
return builder.toString();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof User)) {
return false;
}
User that = (User) obj;
return this.name.equals(that.name)
&& this.getBackendRoles().equals(that.backendRoles)
&& this.getRoles().equals(that.roles)
&& this.getCustomAttNames().equals(that.customAttNames)
&& (Objects.equals(this.requestedTenant, that.requestedTenant));
}
public String getName() {
return name;
}
public List<String> getBackendRoles() {
return backendRoles;
}
public List<String> getRoles() {
return roles;
}
public List<String> getCustomAttNames() {
return customAttNames;
}
@Nullable
public String getRequestedTenant() {
return requestedTenant;
}
public boolean isAdminDn(Settings settings) {
if (settings == null) {
return false;
}
List<String> adminDns = settings.getAsList(ConfigConstants.OPENSEARCH_SECURITY_AUTHCZ_ADMIN_DN, Collections.emptyList());
return adminDns.contains(this.name);
}
}