josacar / triki

Mysql, PostgreSQL and SQL dump obfuscator aka anonimizer
MIT License
38 stars 4 forks source link

Ability to save part of string #14

Closed lictw closed 1 year ago

lictw commented 1 year ago

Hello! The project seems very useful for my case, but the documentation leaves much to be desired.. I need to mask all names under * except first letter, can it be possible with triki?

Also does triki support partial truncating of tables? Left N rows or by date in one of columns?

josacar commented 1 year ago

Hi, I guess you could do it, it's not properly documented but I think I used in the Ruby fork.

Also with this you can obfuscate based on conditions, i.e. a date column.

I'm open to any PRs as this is just a pet project that I maintain from time to time.

Thanks for the feedback!

lictw commented 1 year ago

Okay, thanks, I will try to understand how to use it!)

josacar commented 1 year ago

This weekend I will try to improve the documentation and provide examples with it.

Let me know how it goes.

josacar commented 1 year ago

I haven't tested it, but it should be something like:

require "triki"

obfuscator = Triki.new({
  "people" => {
      "email"                     => { :type => :email, :skip_regexes => [/^[\w\.\_]+@my_company\.com$/i] },
       "ethnicity"               =>  { :type => :fixed, :string => ->(row : RowAsHash) { length = row["email"][1..-1].length; %Q{#{row["email"][0]}#{'*' * length}"} } }
    }
})

obfuscator.obfuscate(STDIN, STDOUT)
lictw commented 1 year ago
Error: undefined method '[]' for Int32 (compile-time type is (Int32 | String | Nil))

Thanks for helping, but there is an error.

lictw commented 1 year ago
"type" => { :type => :fixed, :string => ->(row : Triki::ConfigApplicator::RowAsHash) { row["type"].to_s[0] }},

I tried to debug, but I got even more complex error:

In lib/triki/src/triki/config_parser.cr:27:28

 27 | type_column[k] = v
                       ^
Error: expected argument #2 to 'Hash(Symbol, Array(Regex) | Array(String) | Bool | Int32 | Proc(Hash(String, Int32 | String | Nil), (Int32 | String | Nil)) | Proc(Hash(String, Int32 | String | Nil), Bool) | Proc(String) | Range(Int32, Int32) | String | Symbol)#[]=' to be (Array(Regex) | Array(String) | Bool | Int32 | Proc(Hash(String, Int32 | String | Nil), (Int32 | String | Nil)) | Proc(Hash(String, Int32 | String | Nil), Bool) | Proc(String) | Range(Int32, Int32) | String | Symbol), not (Proc(Hash(String, Int32 | String | Nil), Char) | Symbol)

Overloads are:
 - Hash(K, V)#[]=(key : K, value : V)
josacar commented 1 year ago

Looking at the message seems you have to convert that char to string.

josacar commented 1 year ago

I think this can work, although it's tricky af

require "triki"

obfuscator = Triki.new({
  "people" => {
      "email" => { :type => :fixed, :string => ->(row : Triki::ConfigApplicator::RowAsHash) { "#{row["email"].to_s[0]}#{"*" * (row["email"].to_s.size - 1)}".as(Triki::ConfigApplicator::RowContent) } }
    }
})

obfuscator.obfuscate(STDIN, STDOUT)