Skip to content

Commit 30e4a98

Browse files
iigoninbennygoerzigKarstenSchnitterKai Sternad
committed
fix authToken
Signed-off-by: Igonin <iigonin@sternad.de> Co-authored-by: Benny Goerzig <benny.goerzig@sap.com> Co-authored-by: Karsten Schnitter <k.schnitter@sap.com> Co-authored-by: Kai Sternad <k.sternad@sternad.de>
1 parent f269c5f commit 30e4a98

3 files changed

Lines changed: 29 additions & 22 deletions

File tree

src/main/java/org/opensearch/security/dlic/rest/api/InternalUsersApiAction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@ public Map<String, RequestContentValidator.DataType> allowedKeys() {
338338
.put("opendistro_security_roles", DataType.ARRAY)
339339
.put("hash", DataType.STRING)
340340
.put("password", DataType.STRING)
341+
.put("service", DataType.BOOLEAN)
342+
.put("enabled", DataType.BOOLEAN)
341343
.build();
342344
}
343345
});

src/main/java/org/opensearch/security/securityconf/impl/v7/InternalUserV7.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ public void setAttributes(Map<String, String> attributes) {
127127
this.attributes = attributes;
128128
}
129129

130-
public boolean enabled() {
130+
public boolean isEnabled() {
131131
return this.enabled;
132132
}
133133

134-
public boolean service() {
134+
public boolean isService() {
135135
return this.service;
136136
}
137137

src/main/java/org/opensearch/security/user/UserService.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,24 @@ public SecurityDynamicConfiguration<?> createOrUpdateAccount(ObjectNode contentA
155155
throw new UserServiceException(NO_ACCOUNT_NAME_MESSAGE);
156156
}
157157

158-
SecurityJsonNode attributeNode = securityJsonNode.get("attributes");
158+
// Read service flag from top level (boolean)
159+
SecurityJsonNode serviceNode = securityJsonNode.get("service");
160+
boolean isServiceAccount = !serviceNode.isNull() && Boolean.parseBoolean(serviceNode.asString());
159161

160-
if (!attributeNode.get("service").isNull() && attributeNode.get("service").asString().equalsIgnoreCase("true")) { // If this is a
161-
// service account
162+
if (isServiceAccount) {
162163
verifyServiceAccount(securityJsonNode, accountName);
163164
String password = generatePassword();
164165
contentAsNode.put("hash", passwordHasher.hash(password.toCharArray()));
165-
contentAsNode.put("service", "true");
166+
contentAsNode.put("service", true);
166167
} else {
167-
contentAsNode.put("service", "false");
168+
contentAsNode.put("service", false);
168169
}
169170

171+
// Read enabled flag from top level (boolean), default to true
172+
SecurityJsonNode enabledNode = securityJsonNode.get("enabled");
173+
boolean isEnabled = enabledNode.isNull() || Boolean.parseBoolean(enabledNode.asString());
174+
contentAsNode.put("enabled", isEnabled);
175+
170176
securityJsonNode = new SecurityJsonNode(contentAsNode);
171177
final var foundRestrictedContents = restrictedFromUsername(accountName);
172178
if (foundRestrictedContents.isPresent()) {
@@ -185,10 +191,6 @@ public SecurityDynamicConfiguration<?> createOrUpdateAccount(ObjectNode contentA
185191
contentAsNode.remove("password");
186192
}
187193

188-
if (!attributeNode.get("enabled").isNull()) {
189-
contentAsNode.put("enabled", securityJsonNode.get("enabled").asString());
190-
}
191-
192194
final boolean userExisted = internalUsersConfiguration.exists(accountName);
193195

194196
// sanity checks, hash is mandatory for newly created users
@@ -273,21 +275,23 @@ public AuthToken generateAuthToken(String accountName) throws IOException {
273275
final ObjectNode contentAsNode = (ObjectNode) accountDetails;
274276
SecurityJsonNode securityJsonNode = new SecurityJsonNode(contentAsNode);
275277

276-
Optional.ofNullable(securityJsonNode.get("attributes").get("service"))
277-
.map(SecurityJsonNode::asString)
278-
.filter("true"::equalsIgnoreCase)
279-
.orElseThrow(() -> new UserServiceException(AUTH_TOKEN_GENERATION_MESSAGE));
278+
var serviceNode = securityJsonNode.get("service");
279+
boolean isService = !serviceNode.isNull() && Boolean.parseBoolean(serviceNode.asString());
280+
if (!isService) {
281+
throw new UserServiceException(AUTH_TOKEN_GENERATION_MESSAGE);
282+
}
280283

281-
Optional.ofNullable(securityJsonNode.get("attributes").get("enabled"))
282-
.map(SecurityJsonNode::asString)
283-
.filter("true"::equalsIgnoreCase)
284-
.orElseThrow(() -> new UserServiceException(AUTH_TOKEN_GENERATION_MESSAGE));
284+
var enabledNode = securityJsonNode.get("enabled");
285+
boolean isEnabled = enabledNode.isNull() || Boolean.parseBoolean(enabledNode.asString());
286+
if (!isEnabled) {
287+
throw new UserServiceException(AUTH_TOKEN_GENERATION_MESSAGE);
288+
}
285289

286290
// Generate a new password for the account and store the hash of it
287291
String plainTextPassword = generatePassword();
288292
contentAsNode.put("hash", passwordHasher.hash(plainTextPassword.toCharArray()));
289-
contentAsNode.put("enabled", "true");
290-
contentAsNode.put("service", "true");
293+
contentAsNode.put("enabled", true);
294+
contentAsNode.put("service", true);
291295

292296
// Update the internal user associated with the auth token
293297
internalUsersConfiguration.remove(accountName);
@@ -296,7 +300,8 @@ public AuthToken generateAuthToken(String accountName) throws IOException {
296300
accountName,
297301
DefaultObjectMapper.readTree(contentAsNode, internalUsersConfiguration.getImplementingClass())
298302
);
299-
saveAndUpdateConfigs(getUserConfigName().toString(), client, CType.INTERNALUSERS, internalUsersConfiguration);
303+
saveAndUpdateConfigs(securityIndex, client, CType.INTERNALUSERS, internalUsersConfiguration);
304+
configurationRepository.reloadConfiguration(java.util.Set.of(CType.INTERNALUSERS), null);
300305

301306
authToken = Base64.getUrlEncoder().encodeToString((accountName + ":" + plainTextPassword).getBytes(StandardCharsets.UTF_8));
302307
return new BasicAuthToken("Basic " + authToken);

0 commit comments

Comments
 (0)