-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation for API key role descriptors (#82049)
Put Role API prevents creation of invalidate role descriptors by validating that the given cluster privileges and index previleges can be resolved. However, the same validation is not performed when creating API keys. As a result, users are able to create invalidate API keys which then fail at use time. The experience is not user friendly and inconsistent. This PR fixes it by adding the same validation logic for API key creation. Resolves: #67311
- Loading branch information
Showing
4 changed files
with
118 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...ava/org/elasticsearch/xpack/core/security/action/role/RoleDescriptorRequestValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.security.action.role; | ||
|
||
import org.elasticsearch.action.ActionRequestValidationException; | ||
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; | ||
import org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilege; | ||
import org.elasticsearch.xpack.core.security.authz.privilege.ClusterPrivilegeResolver; | ||
import org.elasticsearch.xpack.core.security.authz.privilege.IndexPrivilege; | ||
import org.elasticsearch.xpack.core.security.support.MetadataUtils; | ||
|
||
import java.util.Set; | ||
|
||
import static org.elasticsearch.action.ValidateActions.addValidationError; | ||
|
||
public class RoleDescriptorRequestValidator { | ||
|
||
private RoleDescriptorRequestValidator() {} | ||
|
||
public static ActionRequestValidationException validate(RoleDescriptor roleDescriptor) { | ||
return validate(roleDescriptor, null); | ||
} | ||
|
||
public static ActionRequestValidationException validate( | ||
RoleDescriptor roleDescriptor, | ||
ActionRequestValidationException validationException | ||
) { | ||
if (roleDescriptor.getName() == null) { | ||
validationException = addValidationError("role name is missing", validationException); | ||
} | ||
if (roleDescriptor.getClusterPrivileges() != null) { | ||
for (String cp : roleDescriptor.getClusterPrivileges()) { | ||
try { | ||
ClusterPrivilegeResolver.resolve(cp); | ||
} catch (IllegalArgumentException ile) { | ||
validationException = addValidationError(ile.getMessage(), validationException); | ||
} | ||
} | ||
} | ||
if (roleDescriptor.getIndicesPrivileges() != null) { | ||
for (RoleDescriptor.IndicesPrivileges idp : roleDescriptor.getIndicesPrivileges()) { | ||
try { | ||
IndexPrivilege.get(Set.of(idp.getPrivileges())); | ||
} catch (IllegalArgumentException ile) { | ||
validationException = addValidationError(ile.getMessage(), validationException); | ||
} | ||
} | ||
} | ||
if (roleDescriptor.getApplicationPrivileges() != null) { | ||
for (RoleDescriptor.ApplicationResourcePrivileges privilege : roleDescriptor.getApplicationPrivileges()) { | ||
try { | ||
ApplicationPrivilege.validateApplicationNameOrWildcard(privilege.getApplication()); | ||
} catch (IllegalArgumentException e) { | ||
validationException = addValidationError(e.getMessage(), validationException); | ||
} | ||
for (String privilegeName : privilege.getPrivileges()) { | ||
try { | ||
ApplicationPrivilege.validatePrivilegeOrActionName(privilegeName); | ||
} catch (IllegalArgumentException e) { | ||
validationException = addValidationError(e.getMessage(), validationException); | ||
} | ||
} | ||
} | ||
} | ||
if (roleDescriptor.getMetadata() != null && MetadataUtils.containsReservedMetadata(roleDescriptor.getMetadata())) { | ||
validationException = addValidationError( | ||
"role descriptor metadata keys may not start with [" + MetadataUtils.RESERVED_PREFIX + "]", | ||
validationException | ||
); | ||
} | ||
return validationException; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters