{"id":18375416,"url":"https://github.com/riboseinc/attr_masker","last_synced_at":"2025-04-06T20:31:00.300Z","repository":{"id":22861882,"uuid":"93031175","full_name":"riboseinc/attr_masker","owner":"riboseinc","description":"A fork of attr_encrypted, repurposed to handle data masking in test databases","archived":false,"fork":false,"pushed_at":"2021-12-22T06:36:50.000Z","size":199,"stargazers_count":17,"open_issues_count":12,"forks_count":8,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-05T23:32:27.197Z","etag":null,"topics":["database","privacy-protection","rails"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/riboseinc.png","metadata":{"files":{"readme":"README.adoc","changelog":"CHANGELOG.adoc","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-01T07:50:51.000Z","updated_at":"2023-03-31T06:37:08.000Z","dependencies_parsed_at":"2022-08-17T15:31:20.299Z","dependency_job_id":null,"html_url":"https://github.com/riboseinc/attr_masker","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/riboseinc%2Fattr_masker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/riboseinc%2Fattr_masker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/riboseinc%2Fattr_masker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/riboseinc%2Fattr_masker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/riboseinc","download_url":"https://codeload.github.com/riboseinc/attr_masker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247547192,"owners_count":20956514,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","privacy-protection","rails"],"created_at":"2024-11-06T00:18:50.124Z","updated_at":"2025-04-06T20:30:59.893Z","avatar_url":"https://github.com/riboseinc.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"= attr_masker\n:source-highlighter: pygments\n:pygments-style: native\n:pygments-linenums-mode: inline\n\nifdef::env-github[]\nimage:https://img.shields.io/gem/v/attr_masker[\n\t\"Gem Version\",\n\tlink=\"https://rubygems.org/gems/attr_masker\"]\nimage:https://img.shields.io/github/workflow/status/riboseinc/attr_masker/Tests[\n\t\"Build Status\",\n\tlink=\"https://github.com/riboseinc/attr_masker/actions\"]\nimage:https://img.shields.io/codeclimate/maintainability/riboseinc/attr_masker[\n\t\"Code Climate\",\n\tlink=\"https://codeclimate.com/github/riboseinc/attr_masker\"]\nimage:https://img.shields.io/codecov/c/github/riboseinc/attr_masker[\n\t\"Test Coverage\",\n\tlink=\"https://codecov.io/gh/riboseinc/attr_masker\"]\nimage:https://img.shields.io/badge/documentation-rdoc-informational[\n\t\"Documentation on RubyDoc.info\",\n\tlink=\"https://rubydoc.info/gems/attr_masker\"]\nendif::[]\n\nMask ActiveRecord/Mongoid data with ease!\n\n== Introduction\n\nThis gem is intended to mask sensitive data so that production database dumps\ncan be used in staging or test environments.\n\n* Works with Rails 4.2+ and modern Rubies\n* Supports ActiveRecord and Mongoid models\n\n\n== Usage instructions\n\n=== Installation\n\nAdd `attr_masker` to your `Gemfile`:\n\n[source,ruby]\n----\ngem \"attr_masker\"\n# or the HEAD version of the gem\n# gem \"attr_masker\", github: \"riboseinc/attr_masker\"\n----\n\n\nThen install the gem:\n\n[source,sh]\n----\nbundle install\n----\n\n=== Basic usage\n\nIn your models, define attributes which should be masked:\n\n[source,ruby]\n----\nclass User\n  attr_masker :email, :first_name, :last_name\nend\n----\n\nThen, when you want to mask the data, run the `db:mask` Rake task in some\nRails environment other than production, for example:\n\n[source,sh]\n----\nbundle exec rake db:mask RAILS_ENV=staging\n----\n\nWARNING: Data are destructively overwritten.  Run `rake db:mask` with care!\n\n=== Masking records selectively\n\nYou can use `:if` and `:unless` options to prevent some records from being\naltered.\n\n[source,ruby]\n----\n# evaluates given proc for each record, and the record is passed as a proc's\n# argument\nattr_masker :email :unless =\u003e -\u003e(record) { ! record.tester_user? }\n\n# calls #tester_user? method on each record\nattr_masker :first_name, :if =\u003e :tester_user?\n----\n\nThe ActiveRecord's `::default_scope` method has no effect on masking.  All\ntable records are updated, provided that `:if` and `:unless` filters allow that.\nFor example, if you're soft-deleting your data (with a gem like\nhttps://github.com/rubysherpas/paranoia[Paranoia]), records marked as deleted\nwill be masked as well.\n\n=== Built-in maskers\n\nAttr Masker comes with several built-in maskers.\n\n==== `AttrMasker::Maskers::Simple`\n\nSimply replaces any value with the `\"(redacted)\"`.  Only useful for columns\ncontaining textual data.\n\nThis is a default masker.  It is used when `:masker` option is unspecified.\n\n[example]\n====\n[source,ruby]\n----\nattr_masker :first_name\nattr_masker :last_name, :masker =\u003e AttrMasker::Maskers::Simple.new\n----\n\nWould set both `first_name` and `last_name` attributes to `\"(redacted)\"`.\n====\n\n==== `AttrMasker::Maskers::Replacing`\n\nReplaces characters with some masker string (single asterisk by default).\nCan be initialized with options.\n\n[options=\"header\"]\n|===\n|Name|Default|Description\n\n|`replacement`|`\"*\"`|Replacement string, can be empty.\n|`alphanum_only`|`false`|When true, only alphanumeric characters are replaced.\n|===\n\n[example]\n====\n[source,ruby]\n----\nrm = AttrMasker::Maskers::Replacing.new(character: \"X\", alphanum_only: true)\nattr_masker :phone, :masker =\u003e rm\n----\n\nWould mask `\"123-456-7890\"` as `\"XXX-XXX-XXXX\"`.\n====\n\n=== Using custom maskers\n\nApart from built-in maskers, any object which responds to `#call` can be used,\ne.g. some lambda or `Method` instance.  For instance, you may want to produce\nunique values basing on other attributes, to mask selectively, or to use\ntool like https://github.com/skalee/well_read_faker[Well Read Faker] to\ngenerate random replacement values:\n\n[source,ruby]\n----\nrequire \"well_read_faker\"\n\nattr_masker :email, masker: -\u003e(model:, **) { \"user#{model.id}@example.com\" }\nattr_masker :phone, masker: -\u003e(value:, **) { \"******\" + value[-3..-1] }\nattr_masker :bio, masker: -\u003e(**) { WellReadFaker.paragraph }\n----\n\nMasker is called with following keyword arguments:\n\n`value`:: Original value of the field which is about to be masked\n\n`model`:: Model instance\n\n`attribute_name`:: Name of the attribute which is about to be masked\n\n`masking_options`:: Hash of options which were passed in `#attr_masker` call\n\nThis list is likely to be extended in future versions, and that will not be\nconsidered a breaking change, therefore it is strongly recommended to always\nuse a splat (`**`) at end of argument list of masker's `#call` method.\n\n=== Configuration file\n\nIt is also possible to contain all the maskers configuration in one file.\nJust place it in `config/attr_masker.rb`, and it will be loaded from a Rake\ntask after the application is fully loaded.  That means you can re-open classes\nand add masker definitions there, for example:\n\n[source,ruby]\n----\n# config/attr_masker.rb\n\nclass User\n  attr_masker :first_name, :last_name\nend\n\nclass Email\n  attr_masker :address, -\u003e(model:, **) { \"mail#{model.id}@example.com\" }\nend\n----\n\n== Roadmap \u0026 TODOs\n\n- documentation\n- spec tests\n- Make the `Rails.env` (in which `db:mask` could be run) configurable\n** maybe by passing `ENV` vars\n- more masking options!\n** default scrambling algorithms?\n** structured text preserving algorithms\n*** _e.g._, keeping an HTML snippet valid HTML, but with masked inner text\n** structured *Object* preserving algorithms\n*** _i.e._ generalization of the above HTML scenario\n- I18n of the default `\"(redacted)\"` phrase\n- ...\n\n== Acknowledgements\n\nhttps://github.com/attr-encrypted/attr_encrypted[attr_encrypted] for the initial\ncode structure\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Friboseinc%2Fattr_masker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Friboseinc%2Fattr_masker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Friboseinc%2Fattr_masker/lists"}