diff --git a/README.md b/README.md index f9ef16e..248cc16 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ We can then, from a list of Strings (Logs for example), have a poorly formatted This code has more logical complexity: -![img.png](images/repl_value1.png) +![img.png](images/repl_value3.png) It works with flags: @@ -46,6 +46,8 @@ It works with flags: - **flag_attribute**: It marks that an attribute was found - **flag_dois_pontos**: It marks that the algorithm is prepared to find the content of an attribute (the content is after the :) - **flag_colchetes**: It marks that an attribute items values will be analyzed +- **flag_descobre_tipo**: It marks for find the type of an attribute (String or Numeric) +- **flag_string**: If True, the attribute will be a String; If False, it's a Numeric attribute >**Note**: The attribute to be redacted NEED to be a String. If the attribute is a set of attributes, the algorithm will ignore all the set. In the example code **redact.py**, the attribute **items** is in the list but it will not redact anything. @@ -61,7 +63,7 @@ What the algorithm performs: : need to respect a JSON association - 'value' is the value between '', yes the value needs to be a String + 'value' is the value between '' for a String, or a Numeric value (Integer or Double with a . and no '') So you can use the class **Redaction.py** in you code, like this: diff --git a/Redaction.py b/Redaction.py index 717b6d3..4d0e4d2 100644 --- a/Redaction.py +++ b/Redaction.py @@ -33,6 +33,8 @@ class Redaction(): # flag_vezes = 0 flag_dois_pontos = False flag_colchetes = False + flag_string = True + flag_descobre_tipo = False i = 0 z = pattern str_acc = "" @@ -45,7 +47,9 @@ class Redaction(): print("except") if (message[i] == ":" and not flag_aspas and flag_attribute): flag_dois_pontos = True - if (flag_aspas and message[i] != "'" and message[i] != "\"" and flag_attribute and flag_dois_pontos): + flag_descobre_tipo = True + if ((flag_aspas and message[i] != "'" and message[i] != "\"" and flag_attribute and flag_dois_pontos) + or (message[i] in "0123456789." and flag_attribute and flag_dois_pontos and not flag_string)): str_acc = str_acc + message[i] message = message[0:i] + "*" + message[i + 1:len(message) + i] if (message[i] == "{" and flag_dois_pontos and not flag_aspas): @@ -53,25 +57,48 @@ class Redaction(): flag_dois_pontos = False flag_aspas = False flag_colchetes = False + flag_descobre_tipo = False + flag_string = True if ((message[i] == "}" or message[i] == "]") and not flag_aspas): flag_attribute = False flag_dois_pontos = False flag_aspas = False flag_colchetes = False + flag_descobre_tipo = False + flag_string = True str_acc = "" - if (flag_dois_pontos and not flag_aspas and message[i] == "["): - flag_colchetes = True - if (message[i] == "," and not flag_aspas and not flag_colchetes): + if ((message[i] == "}" or message[i] == "]") and not flag_aspas and not flag_string): flag_attribute = False flag_dois_pontos = False flag_aspas = False flag_colchetes = False + flag_descobre_tipo = False + flag_string = True + str_acc = "" + if (flag_dois_pontos and not flag_aspas and message[i] == "["): + flag_colchetes = True + if (message[i] == "," and not flag_aspas and not flag_colchetes and not flag_string): + flag_attribute = False + flag_dois_pontos = False + flag_aspas = False + flag_colchetes = False + flag_descobre_tipo = False + flag_string = True str_acc = "" if ((message[i] == "'" or message[i] == "\"")): flag_aspas = not flag_aspas - if (flag_aspas == False and flag_attribute == True and flag_dois_pontos and len(str_acc) > 0 and not flag_colchetes): + if (flag_descobre_tipo): + flag_string = True + flag_descobre_tipo = False + if (message[i] in "01234567890." and flag_descobre_tipo): + flag_string = False + flag_descobre_tipo = False + str_acc = str_acc + message[i] + message = message[0:i] + "*" + message[i + 1:len(message) + i] + if (flag_aspas == False and flag_attribute == True and flag_dois_pontos and len(str_acc) > 0 and not flag_colchetes and flag_string): flag_attribute = False flag_dois_pontos = False + flag_descobre_tipo = False str_acc = "" i = i + 1 return message diff --git a/images/repl_value3.png b/images/repl_value3.png new file mode 100644 index 0000000..5ab31e6 Binary files /dev/null and b/images/repl_value3.png differ diff --git a/redact.py b/redact.py index 01cd061..28ad9c1 100644 --- a/redact.py +++ b/redact.py @@ -18,12 +18,14 @@ ATTRIBUTE_PATTERNS = [ "original", "type", "solicitacaoPagador", - "expiracao", #Não funciona para atributos numéricos, apenas para String + "expiracao", "chave", "description", "items", "example", - "required" + "required", + "x-scope", + "maxLength" ] Messages = [ @@ -37,7 +39,7 @@ Messages = [ "User's date of birth: 04/29/1990", "User's IP address: 192.168.1.1", "User's API key: a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6", - '{ "nome": "João Silva", "cpf": "123.456.789-00", "teste": "Teste Valor", "valor": 200.00, "original": "Original Valor", "type": "Tipo Valor", "solicitacaoPagador": "Solicitação Pagador", "expiracao": "2021-12-31", "chave": "Chave Valor", "description": "Descrição Valor", "items": ["Item1", "Item2"], "example": "Exemplo Valor"}' + '{ "nome": "João Silva", "cpf": "123.456.789-00", "maxLength": [1.0, 2.0, 3.0], "teste": "Teste Valor", "valor": 200.00, "original": "Original Valor", "type": "Tipo Valor", "solicitacaoPagador": "Solicitação Pagador", "expiracao": "2021-12-31", "chave": "Chave Valor", "description": "Descrição Valor", "items": ["Item1", "Item2"], "example": "Exemplo Valor"}' ] redaction = Redaction.Redaction()