|
| 1 | +# --- |
| 2 | +# jupyter: |
| 3 | +# kernelspec: |
| 4 | +# name: s3-specs |
| 5 | +# display_name: S3 Specs |
| 6 | +# language_info: |
| 7 | +# name: python |
| 8 | +# --- |
| 9 | + |
| 10 | +# # Bucket Policy via MGC CLI |
| 11 | +# |
| 12 | +# Restringir determinadas ações passíveis de serem executadas em um bucket, ou |
| 13 | +# compartilhar acessos de leitura e escrita com outros usuários (Principals), |
| 14 | +# são exemplos de possibilidades que a funcionalidade de Bucket Policy (políticas |
| 15 | +# do conteiner) permitem. |
| 16 | +# |
| 17 | +# A configuração, remoção e atualização de políticas em um bucket, por documentos |
| 18 | +# de policy (arquivos JSON) pode ser feita via MGC CLI. Este é o assunto desta |
| 19 | +# especificação. |
| 20 | +# |
| 21 | + |
| 22 | +# + tags=["parameters"] |
| 23 | +config = "../params.example.yaml" |
| 24 | +config = "../params.yaml" |
| 25 | +config = "../params/br-ne1.yaml" |
| 26 | +# - |
| 27 | + |
| 28 | +# + {"jupyter": {"source_hidden": true}} |
| 29 | +import pytest |
| 30 | +import logging |
| 31 | +import subprocess |
| 32 | +from shlex import split |
| 33 | +from s3_helpers import ( |
| 34 | + run_example, |
| 35 | +) |
| 36 | +pytestmark = [pytest.mark.basic, pytest.mark.cli] |
| 37 | +# - |
| 38 | +# ## Como fazer |
| 39 | + |
| 40 | +# ### Listar os comandos disponíveis |
| 41 | +# |
| 42 | +# O manual de uso com os comandos disponíveis na CLI relacionados a Bucket Policy |
| 43 | +# pode ser consultado via terminal usando o comando abaixo: |
| 44 | + |
| 45 | +commands = [ |
| 46 | + "{mgc_path} object-storage buckets policy --help", |
| 47 | +] |
| 48 | + |
| 49 | +# + {"jupyter": {"source_hidden": true}} |
| 50 | +@pytest.mark.parametrize("cmd_template", commands) |
| 51 | +def test_cli_help_policy(cmd_template, mgc_path): |
| 52 | + cmd = split(cmd_template.format(mgc_path=mgc_path)) |
| 53 | + result = subprocess.run(cmd, capture_output=True, text=True) |
| 54 | + |
| 55 | + assert result.returncode == 0, f"Command failed with error: {result.stderr}" |
| 56 | + logging.debug(f"Output from {cmd_template}: {result.stdout}") |
| 57 | + assert all(snippet in result.stdout for snippet in ["delete", "get", "set"]) |
| 58 | + |
| 59 | +run_example(__name__, "test_cli_help_policy", config=config) |
| 60 | +# - |
| 61 | + |
| 62 | +# ### Documento de política válido |
| 63 | +# |
| 64 | +# Para fins de exemplificar o uso dos comandos, usaremos um documento JSON de política |
| 65 | +# válido bem simples, que habilita o download de todos os objetos (`*`) em um bucket |
| 66 | +# (`Resource`) para qualquer (`*`) usuário (`Principal`). |
| 67 | + |
| 68 | +# + |
| 69 | +@pytest.fixture |
| 70 | +def valid_simple_policy(): |
| 71 | + return """{ |
| 72 | + "Version": "2012-10-17", |
| 73 | + "Statement": [ |
| 74 | + { |
| 75 | + "Effect": "Allow", |
| 76 | + "Principal": "*", |
| 77 | + "Action": "s3:GetObject", |
| 78 | + "Resource": "<bucket_name>/*" |
| 79 | + } |
| 80 | + ] |
| 81 | + }""" |
| 82 | +# - |
| 83 | + |
| 84 | +# > **Nota:** Substitua `<bucket_name>` pelo nome do seu bucket. |
| 85 | + |
| 86 | +# ### Atribuir uma política a um bucket |
| 87 | +# |
| 88 | +# Para definir uma política de bucket usando a MGC CLI, utilize o comando: |
| 89 | + |
| 90 | +set_policy_commands = [ |
| 91 | + "{mgc_path} object-storage buckets policy set --dst {bucket_name} --policy '{policy_json_content}'", |
| 92 | +] |
| 93 | + |
| 94 | +# + {"jupyter": {"source_hidden": true}} |
| 95 | +@pytest.mark.parametrize("cmd_template", set_policy_commands) |
| 96 | +def test_cli_set_policy(cmd_template, active_mgc_workspace, mgc_path, valid_simple_policy, existing_bucket_name): |
| 97 | + bucket_name = existing_bucket_name |
| 98 | + policy = valid_simple_policy.replace("<bucket_name>", bucket_name) |
| 99 | + logging.info(policy) |
| 100 | + cmd = split(cmd_template.format( |
| 101 | + mgc_path=mgc_path, |
| 102 | + bucket_name=bucket_name, |
| 103 | + policy_json_content=policy |
| 104 | + )) |
| 105 | + logging.info(f"{cmd}") |
| 106 | + result = subprocess.run(cmd, capture_output=True, text=True) |
| 107 | + |
| 108 | + assert result.returncode == 0, f"Command failed with error: {result.stderr}" |
| 109 | + logging.info(f"Output from {cmd_template}: {result.stdout}") |
| 110 | + |
| 111 | +run_example(__name__, "test_cli_set_policy", config=config) |
| 112 | +# - |
| 113 | + |
| 114 | +# ### Remover uma política de um bucket |
| 115 | +# |
| 116 | +# Para deletar uma política de bucket usando a MGC CLI, utilize o comando: |
| 117 | + |
| 118 | +delete_policy_commands = [ |
| 119 | + "{mgc_path} object-storage buckets policy delete --dst {bucket_name}", |
| 120 | +] |
| 121 | + |
| 122 | +# + {"jupyter": {"source_hidden": true}} |
| 123 | +from utils.policy import fixture_bucket_with_policy |
| 124 | + |
| 125 | +@pytest.mark.parametrize( |
| 126 | + "cmd_template", |
| 127 | + delete_policy_commands, |
| 128 | +) |
| 129 | +def test_cli_delete_policy(cmd_template, fixture_bucket_with_policy, active_mgc_workspace, mgc_path): |
| 130 | + bucket_name, _policy_doc, _s3_clients, _object_prefix, _content = fixture_bucket_with_policy |
| 131 | + cmd = split(cmd_template.format(mgc_path=mgc_path, bucket_name=bucket_name)) |
| 132 | + logging.info(f"{cmd}") |
| 133 | + result = subprocess.run(cmd, capture_output=True, text=True) |
| 134 | + |
| 135 | + assert result.returncode == 0, f"Command failed with error: {result.stderr}" |
| 136 | + logging.info(f"Output from {cmd_template}: {result.stdout}") |
| 137 | + |
| 138 | +run_example(__name__, "test_cli_delete_policy", config=config) |
| 139 | +# - |
| 140 | + |
| 141 | +# ## Documentos relacionados |
| 142 | +# |
| 143 | +# Uma breve lista de outras referências sobre Bucket Policy |
| 144 | +# |
| 145 | +# - Docs Magalu Cloud: [Docs > Armazenamento > Object Storage > Como Fazer > Permissões > Bucket Policy](https://docs.magalu.cloud/docs/storage/object-storage/how-to/permissions/policies) |
| 146 | +# - Docs Magalu Cloud: [Docs > Armazenamento > Object Storage > Controle de Acesso de Dados > Bucket Policy](https://docs.magalu.cloud/docs/storage/object-storage/access-control/bucket_policy_overview/) |
| 147 | +# - s3-specs: [python/boto3 policies spec 1](https://magalucloud.github.io/s3-specs/runs/profiles_policies_test_br-ne1.html) |
| 148 | +# - s3-specs: [python/boto3 policies spec 2](https://magalucloud.github.io/s3-specs/runs/policies_test_test_br-ne1.html) |
| 149 | +# - s3-tester (specs legadas): [legacy s3-tester spec id:091](https://github.com/MagaluCloud/s3-tester/blob/main/spec/091_bucket-policy_spec.sh) |
| 150 | +# - AWS S3: [Bucket Policies for Amazon S3](https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucket-policies.html) |
| 151 | +# - AWS S3: [Bucket policies](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-example-bucket-policies.html) |
| 152 | + |
0 commit comments