Como instanciar edge functions em seu edge firewall
Instancie funções serverless diretamente no seu edge firewall.
Este guia utiliza a função configurada Recusando uma requisição baseando-se em Geoip. Você pode repetir este processo usando qualquer função já disponível no Marketplace da Azion ou desenvolver outras funções.
Este guia também presume que você possui uma network list do tipo Countries.
- Acesse o Azion Console > Edge Firewall.
- Clique no edge firewall que você deseja configurar.
- Na aba Main Settings, ative o switch do módulo Edge Functions para habilitar as funções.
- Clique no botão Save.
- Vá para a aba Functions Instances.
- Clique em + Function Instance.
- Nomeie sua instância de função. Por exemplo:
Função de Recusar Geoip
. - No menu suspenso Edge Functions, selecione a função que deseja usar. Neste exemplo,
Função de Recusar Geoip
.
- Se desejar, pode editar os Arguments.
- Clique no botão Save.
A página de funções agora lista a instância recém-criada. No entanto, esta nova função ainda não está ativa em sua. Você precisa definir o que acionará a função.
Ainda na página do Edge Firewall:
- Navegue até a aba Rules Engine.
- Clique no botão + Rule.
- Dê um nome para o seu conjunto de regras.
- Na seção Criteria, selecione a variável
Network
. - Como operador de comparação, selecione matches.
- Como argumento, selecione a network list do tipo Countries que deseja usar. Por exemplo:
Países Bloqueados Geoip
. - Na seção Behaviors, selecione Run Function na lista de comportamentos.
- Selecione a função que deseja usar. Neste exemplo,
Função de Recusar Geoip
. - Clique no botão Save.
Quando sua aplicação receber uma requisição gerada de um dos países na lista de rede, a função em seu firewall recusará a requisição com base no Geo IP.
- Execute a seguinte requisição
PATCH
no seu terminal, substituindo[VALOR DO TOKEN]
pelo seu token pessoal e a variável<edge_firewall_id>
pelo ID do seu edge firewall para ativar o módulo Edge Functions:
curl --location --request PATCH 'https://api.azionapi.net/edge_firewall/<edge_firewall_id>' \--header 'Accept: application/json; version=3' \--header 'Authorization: Token [VALOR DO TOKEN]' \--header 'Content-Type: application/json' \--data '{ "edge_functions_enabled": true}'
- Você receberá uma resposta com o valor atualizado.
- Execute a seguinte requisição
GET
para obter oedge_function_id
da funçãoRecusar Geoip
:
curl --location 'https://api.azionapi.net/edge_functions' \--header 'Accept: application/json; version=3' \--header 'Authorization: Token [TOKEN VALUE]'
- Você receberá uma resposta semelhante a esta:
{ "count": 1, "total_pages": 1, "schema_version": 3, "links": { "previous": null, "next": null }, "results": [ { "id": 13426, "name": "Função de Recusar Geoip", "language": "javascript", "code": "async function firewallHandler(event){\n // Access the country code through geoip\n let countryCode = event.request.metadata[\"geoip_country_code\"]\n\n // Do some logic here\n // In this example, we are blocking access from Brazil\n if (countryCode === \"BR\"){\n event.deny();\n }\n\n // Then, if it comes from any other country,\n // the processing continues\n event.continue();\n}\n\naddEventListener(\"firewall\", (event)=>event.waitUntil(firewallHandler(event)));", "json_args": { "value": "hello_world" }, "function_to_run": "", "initiator_type": "edge_firewall", "active": true, "last_editor": "user@email.com", "modified": "2023-11-14T20:09:48.657555Z", "is_proprietary_code": false, "reference_count": 1 } ]}
- Copie o valor
<edge_function_id>
. - Execute a seguinte requisição
POST
, substituindo a variável<edge_firewall_id
pelo ID do seu edge firewall e o valor<edge_function_id>
pelo valor que você recebeu na resposta anterior:
curl --location 'https://api.azionapi.net/edge_firewall/<edge_firewall_id>/functions_instances' \--header 'Accept: application/json; version=3' \--header 'Authorization: Token [TOKEN VALUE]' \--header 'Content-Type: application/json' \--data '{ "name": "Recusar Geoip", "edge_function": <edge_function_id>, "json_args": {}}'
- Você receberá uma resposta semelhante a esta:
{ "results": { "name": "Another function", "json_args": {}, "edge_function": 13426, "last_modified": "2023-11-22T18:50:11.812819Z", "last_editor": "user@email.com", "id": 2806 }, "schema_version": 3}
Chave | Descrição |
---|---|
name | Nome da função instanciada |
edge_function | O ID da edge function. Este não é o mesmo que o ID da função instanciada, que é único para cada instância de função |
json_args | Definição de argumentos necessários para a função. Algumas funções não aceitam argumentos |
- Execute a seguinte requisição
GET
para obter oid
da network listBlocked Countries Geoip
:
curl --location 'https://api.azionapi.net/network_lists' \--header 'Accept: application/json; version=3' \--header 'Authorization: Token [VALOR DO TOKEN]'
- Você receberá uma resposta semelhante a esta:
{ "count": 2, "total_pages": 1, "schema_version": 3, "links": { "previous": null, "next": null }, "results": [ { "id": 6217, "last_editor": "user@email.com", "last_modified": "2023-11-14T21:35:39.808175Z", "list_type": "countries", "name": "Negar Geoip", "country_list": [ "Brasil" ], "ip_list": [] } ]}}
Chave | Descrição |
---|---|
name | Nome da lista de rede |
id | ID da lista de rede |
list_type | Definição do tipo da lista de rede |
country_list | Itens que compõem a lista |
- Execute a seguinte requisição
POST
no seu terminal, substituindo[VALOR DO TOKEN]
pelo seu personal token, a variável<edge_firewall_id>
pelo ID do seu edge firewall, o valor<edge_function_instance_id>
pelo ID da instância da função e o valor<network_list_id>
pelo ID da network list:
curl --location 'https://api.azionapi.net/edge_firewall/<edge_firewall_id>/rules_engine' \--header 'Accept: application/json; version=3' \--header 'Authorization: Token [TOKEN VALUE]' \--header 'Content-Type: application/json' \--data '{ "name": "Create rule", "is_active": true, "behaviors": [ { "name": "run_function", "argument": <edge_function_instance_id> } ], "criteria": [ [ { "variable": "network", "operator": "is_in_list", "conditional": "if", "argument": <network_list_id> } ] ]}'
- Você receberá uma resposta semelhante a esta, confirmando que sua função foi criada com sucesso:
{ "results": { "name": "Create rule", "is_active": true, "behaviors": [ { "name": "run_function", "argument": 2718 } ], "criteria": [ [ { "variable": "network", "operator": "is_in_list", "conditional": "if", "argument": 6217 } ] ], "last_modified": "2023-11-22T17:57:28.584143Z", "last_editor": "user@email.com", "id": 28370, "order": 1 }, "schema_version": 3}
- Aguarde alguns minutos para que as alterações se propaguem.
Quando sua aplicação receber uma requisição gerada de um dos países na network list, a função em seu firewall recusará a requisição com base no Geo IP.
Contribuidores