Безопасность на уровне полей
Безопасность на уровне полей ограничивает поля, к которым пользователи имеют доступ для чтения. В частности, она ограничивает доступ к полям из API-интерфейсов чтения документов.
Для включения безопасности на уровне полей укажите поля, к которым каждый роль имеет доступ, в рамках разрешений индексов в определении роли. Таким образом, безопасность на уровне полей связана с хорошо определенным набором потоков данных или индексов (и, возможно, набором документов).
Следующее определение роли предоставляет доступ для чтения только к полям category, @timestamp и message во всех потоках данных и индексах events-*.
resp = client.security.put_role(
name="test_role1",
indices=[
{
"names": [
"events-*"
],
"privileges": [
"read"
],
"field_security": {
"grant": [
"category",
"@timestamp",
"message"
]
}
}
],
)
print(resp) const response = await client.security.putRole({
name: "test_role1",
indices: [
{
names: ["events-*"],
privileges: ["read"],
field_security: {
grant: ["category", "@timestamp", "message"],
},
},
],
});
console.log(response); POST /_security/role/test_role1
{
"indices": [
{
"names": [ "events-*" ],
"privileges": [ "read" ],
"field_security" : {
"grant" : [ "category", "@timestamp", "message" ]
}
}
]
} Доступ к следующим метаданным всегда разрешен: _id, _type, _parent, _routing, _timestamp, _ttl, _size и _index. Если вы укажете пустой список полей, доступны будут только эти метаданные.
Полное опущение поля fields отключает безопасность на уровне полей.
Вы также можете указать выражения для полей. Например, следующий пример предоставляет доступ для чтения ко всем полям, начинающимся с префикса event_:
resp = client.security.put_role(
name="test_role2",
indices=[
{
"names": [
"*"
],
"privileges": [
"read"
],
"field_security": {
"grant": [
"event_*"
]
}
}
],
)
print(resp) const response = await client.security.putRole({
name: "test_role2",
indices: [
{
names: ["*"],
privileges: ["read"],
field_security: {
grant: ["event_*"],
},
},
],
});
console.log(response); POST /_security/role/test_role2
{
"indices" : [
{
"names" : [ "*" ],
"privileges" : [ "read" ],
"field_security" : {
"grant" : [ "event_*" ]
}
}
]
} Используйте нотацию точек для ссылки на вложенные поля в более сложных документах. Например, предположим, что следующий документ:
{
"customer": {
"handle": "Jim",
"email": "jim@mycompany.com",
"phone": "555-555-5555"
}
} Следующее определение роли разрешает доступ для чтения только к полю handle клиента:
resp = client.security.put_role(
name="test_role3",
indices=[
{
"names": [
"*"
],
"privileges": [
"read"
],
"field_security": {
"grant": [
"customer.handle"
]
}
}
],
)
print(resp) const response = await client.security.putRole({
name: "test_role3",
indices: [
{
names: ["*"],
privileges: ["read"],
field_security: {
grant: ["customer.handle"],
},
},
],
});
console.log(response); POST /_security/role/test_role3
{
"indices" : [
{
"names" : [ "*" ],
"privileges" : [ "read" ],
"field_security" : {
"grant" : [ "customer.handle" ]
}
}
]
} Здесь демонстрируется поддержка подстановочных знаков. Например, используйте customer.*, чтобы предоставить доступ для чтения только к данным customer:
resp = client.security.put_role(
name="test_role4",
indices=[
{
"names": [
"*"
],
"privileges": [
"read"
],
"field_security": {
"grant": [
"customer.*"
]
}
}
],
)
print(resp) const response = await client.security.putRole({
name: "test_role4",
indices: [
{
names: ["*"],
privileges: ["read"],
field_security: {
grant: ["customer.*"],
},
},
],
});
console.log(response); POST /_security/role/test_role4
{
"indices" : [
{
"names" : [ "*" ],
"privileges" : [ "read" ],
"field_security" : {
"grant" : [ "customer.*" ]
}
}
]
} Вы можете запретить доступ к полям с помощью следующего синтаксиса:
resp = client.security.put_role(
name="test_role5",
indices=[
{
"names": [
"*"
],
"privileges": [
"read"
],
"field_security": {
"grant": [
"*"
],
"except": [
"customer.handle"
]
}
}
],
)
print(resp) const response = await client.security.putRole({
name: "test_role5",
indices: [
{
names: ["*"],
privileges: ["read"],
field_security: {
grant: ["*"],
except: ["customer.handle"],
},
},
],
});
console.log(response); POST /_security/role/test_role5
{
"indices" : [
{
"names" : [ "*" ],
"privileges" : [ "read" ],
"field_security" : {
"grant" : [ "*"],
"except": [ "customer.handle" ]
}
}
]
} Применяются следующие правила:
- Отсутствие
field_securityв роли эквивалентно доступу *. - Если разрешение явно предоставлено для некоторых полей, можно указать поля, доступ к которым запрещён. Запрещённые поля должны быть подмножеством полей, для которых разрешение было предоставлено.
- Определение запрещённых и разрешённых полей подразумевает доступ ко всем разрешённым полям, за исключением тех, которые соответствуют шаблону в запрещённых полях.
Например:
resp = client.security.put_role(
name="test_role6",
indices=[
{
"names": [
"*"
],
"privileges": [
"read"
],
"field_security": {
"except": [
"customer.handle"
],
"grant": [
"customer.*"
]
}
}
],
)
print(resp) const response = await client.security.putRole({
name: "test_role6",
indices: [
{
names: ["*"],
privileges: ["read"],
field_security: {
except: ["customer.handle"],
grant: ["customer.*"],
},
},
],
});
console.log(response); POST /_security/role/test_role6
{
"indices" : [
{
"names" : [ "*" ],
"privileges" : [ "read" ],
"field_security" : {
"except": [ "customer.handle" ],
"grant" : [ "customer.*" ]
}
}
]
} В приведённом примере пользователи могут читать все поля с префиксом «customer.» за исключением «customer.handle».
Пустой массив для grant (например, "grant" : []) означает, что доступ к полям не предоставлен.
Если у пользователя несколько ролей, определяющих разрешения на уровне полей, результирующие разрешения на уровне полей для каждого потока данных или индекса являются объединением разрешений отдельных ролей. Например, если объединены эти две роли:
resp = client.security.put_role(
name="test_role7",
indices=[
{
"names": [
"*"
],
"privileges": [
"read"
],
"field_security": {
"grant": [
"a.*"
],
"except": [
"a.b*"
]
}
}
],
)
print(resp)
resp1 = client.security.put_role(
name="test_role8",
indices=[
{
"names": [
"*"
],
"privileges": [
"read"
],
"field_security": {
"grant": [
"a.b*"
],
"except": [
"a.b.c*"
]
}
}
],
)
print(resp1) const response = await client.security.putRole({
name: "test_role7",
indices: [
{
names: ["*"],
privileges: ["read"],
field_security: {
grant: ["a.*"],
except: ["a.b*"],
},
},
],
});
console.log(response);
const response1 = await client.security.putRole({
name: "test_role8",
indices: [
{
names: ["*"],
privileges: ["read"],
field_security: {
grant: ["a.b*"],
except: ["a.b.c*"],
},
},
],
});
console.log(response1); POST /_security/role/test_role7
{
"indices" : [
{
"names" : [ "*" ],
"privileges" : [ "read" ],
"field_security" : {
"grant": [ "a.*" ],
"except" : [ "a.b*" ]
}
}
]
}
POST /_security/role/test_role8
{
"indices" : [
{
"names" : [ "*" ],
"privileges" : [ "read" ],
"field_security" : {
"grant": [ "a.b*" ],
"except" : [ "a.b.c*" ]
}
}
]
} Результирующее разрешение равно:
{
// role 1 + role 2
...
"indices" : [
{
"names" : [ "*" ],
"privileges" : [ "read" ],
"field_security" : {
"grant": [ "a.*" ],
"except" : [ "a.b.c*" ]
}
}
]
} Безопасность на уровне полей не должна настраиваться для полей alias. Для защиты конкретного поля необходимо использовать его имя напрямую.
Дополнительную информацию см. в разделе Настройка безопасности на уровне полей и документов.
© 2023-2025 Elasticsearch
As of September 2024, Elasticsearch is available under a choice of three licenses: the Server Side Public License (SSPL), the Elastic License, or the AGPLv3 (OSI approved).
Elasticsearch and the Elasticsearch logo are trademarks of Elasticsearch B.V., registered in the U.S. and in other countries.
https://www.elastic.co/guide/en/elasticsearch/reference/8.17/field-level-security.html