|
7 | 7 |
|
8 | 8 | package org.elasticsearch.xpack.security.authz; |
9 | 9 |
|
| 10 | +import org.elasticsearch.action.support.IndexComponentSelector; |
| 11 | +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; |
10 | 12 | import org.elasticsearch.common.Strings; |
11 | 13 | import org.elasticsearch.common.util.set.Sets; |
12 | 14 | import org.elasticsearch.core.Nullable; |
@@ -90,20 +92,56 @@ public String actionDenied( |
90 | 92 |
|
91 | 93 | if (ClusterPrivilegeResolver.isClusterAction(action)) { |
92 | 94 | final Collection<String> privileges = findClusterPrivilegesThatGrant(authentication, action, request); |
93 | | - if (privileges != null && privileges.size() > 0) { |
| 95 | + if (privileges != null && false == privileges.isEmpty()) { |
94 | 96 | message = message |
95 | 97 | + ", this action is granted by the cluster privileges [" |
96 | 98 | + collectionToCommaDelimitedString(privileges) |
97 | 99 | + "]"; |
98 | 100 | } |
99 | 101 | } else if (isIndexAction(action)) { |
100 | | - final Collection<String> privileges = findIndexPrivilegesThatGrant(action); |
101 | | - if (privileges != null && privileges.size() > 0) { |
102 | | - message = message |
103 | | - + ", this action is granted by the index privileges [" |
104 | | - + collectionToCommaDelimitedString(privileges) |
105 | | - + "]"; |
| 102 | + String[] indices = AuthorizationEngine.RequestInfo.indices(request); |
| 103 | + boolean hasFailuresSelector = false; |
| 104 | + boolean hasNullOrDataSelector = false; |
| 105 | + if (indices != null) { |
| 106 | + for (String index : indices) { |
| 107 | + if (IndexNameExpressionResolver.hasSelector(index, IndexComponentSelector.FAILURES)) { |
| 108 | + hasFailuresSelector = true; |
| 109 | + } else { |
| 110 | + hasNullOrDataSelector = true; |
| 111 | + } |
| 112 | + // we found both selectors, we can stop |
| 113 | + if (hasNullOrDataSelector && hasFailuresSelector) { |
| 114 | + break; |
| 115 | + } |
| 116 | + } |
106 | 117 | } |
| 118 | + |
| 119 | + if (hasNullOrDataSelector) { |
| 120 | + final Collection<String> privileges = findIndexPrivilegesThatGrant(action, IndexComponentSelector.DATA); |
| 121 | + if (privileges != null && false == privileges.isEmpty()) { |
| 122 | + message = message |
| 123 | + + ", this action is granted by the index privileges [" |
| 124 | + + collectionToCommaDelimitedString(privileges) |
| 125 | + + "]"; |
| 126 | + } |
| 127 | + } |
| 128 | + if (hasFailuresSelector) { |
| 129 | + final Collection<String> privileges = findIndexPrivilegesThatGrant(action, IndexComponentSelector.FAILURES); |
| 130 | + if (privileges != null && false == privileges.isEmpty()) { |
| 131 | + if (hasNullOrDataSelector) { |
| 132 | + message = message |
| 133 | + + " for data access, or [" |
| 134 | + + collectionToCommaDelimitedString(privileges) |
| 135 | + + "] for access with the failures selector"; |
| 136 | + } else { |
| 137 | + message = message |
| 138 | + + ", this action is granted by the index privileges [" |
| 139 | + + collectionToCommaDelimitedString(privileges) |
| 140 | + + "]"; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
107 | 145 | } |
108 | 146 |
|
109 | 147 | return message; |
@@ -132,8 +170,8 @@ protected Collection<String> findClusterPrivilegesThatGrant( |
132 | 170 | return ClusterPrivilegeResolver.findPrivilegesThatGrant(action, request, authentication); |
133 | 171 | } |
134 | 172 |
|
135 | | - protected Collection<String> findIndexPrivilegesThatGrant(String action) { |
136 | | - return IndexPrivilege.findPrivilegesThatGrant(action); |
| 173 | + protected Collection<String> findIndexPrivilegesThatGrant(String action, IndexComponentSelector selector) { |
| 174 | + return IndexPrivilege.findPrivilegesThatGrant(action, selector); |
137 | 175 | } |
138 | 176 |
|
139 | 177 | private String remoteClusterText(@Nullable String clusterAlias) { |
|
0 commit comments