-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ELEBUILD-140] Anonymizer script moet niet afbreken wanneer deze een …
…tabel of veld niet kan vinden - error logging op hoger niveau - afhandelen als uitvoeren custom sql query fout gaat, - readme updaten - in example.sql extra tabellen en kolommen toevoegen voor opvangen van randscenario's
- Loading branch information
Cindy Yap
committed
Jul 20, 2023
1 parent
3836fef
commit 6ca058f
Showing
5 changed files
with
119 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,104 @@ | ||
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) | ||
|
||
## Eleven Anonymizer | ||
|
||
This Anonymizer program is based on [DivanteLtd/Anonymizer](https://github.com/DivanteLtd/anonymizer). | ||
|
||
This version is written in [Typescript](https://www.typescriptlang.org/) and [Deno](https://deno.land) and can be build to an executable. | ||
This version is written in [Typescript](https://www.typescriptlang.org/) and [Deno](https://deno.land) and can be build | ||
to an executable. | ||
|
||
### Usage | ||
Keep in mind that this tool will do actions on the database. Please make sure you are testing this first before committing any changes to the database. | ||
|
||
Keep in mind that this tool will do actions on the database. Please make sure you are testing this first before | ||
committing any changes to the database. | ||
There is no warranty for using this script, use at your own risk. | ||
|
||
### Why make this? | ||
While the original Anonymizer by DivanteLtd works fine it needs much more work to be installed and implemented on our computers/servers. | ||
We wanted the functionality used in that version, but be able to just call the Anonymizer from anywhere and point to the needed configurations. | ||
|
||
Also by making most of the configurations as command line arguments we are able to use the same Anonymizer and run it on a DTAP environment without creating multiple configuration files. | ||
While the original Anonymizer by DivanteLtd works fine it needs much more work to be installed and implemented on our | ||
computers/servers. | ||
We wanted the functionality used in that version, but be able to just call the Anonymizer from anywhere and point to the | ||
needed configurations. | ||
|
||
Also by making most of the configurations as command line arguments we are able to use the same Anonymizer and run it on | ||
a DTAP environment without creating multiple configuration files. | ||
|
||
### Why Typescript and Deno | ||
|
||
Typescript is something we are more comfortable with in terms of implementation. | ||
As a trial we wanted to make an executable CLI script. This is something that Typescript + Node could give us. Initially this project was made using Typescript + Node but after a while we choose Deno as it was a much better use case for this project. | ||
The benefits of using Deno over Node is that Deno is secure by default. Things like File access and Network access can be enabled but are disabled by default. And even if enabled it can be configured to only allow specific locations or hosts/ip/ports. | ||
As a trial we wanted to make an executable CLI script. This is something that Typescript + Node could give us. Initially | ||
this project was made using Typescript + Node but after a while we choose Deno as it was a much better use case for this | ||
project. | ||
The benefits of using Deno over Node is that Deno is secure by default. Things like File access and Network access can | ||
be enabled but are disabled by default. And even if enabled it can be configured to only allow specific locations or | ||
hosts/ip/ports. | ||
|
||
Since this scripts goal is to anonymize databases, security should be one of the main focuses. | ||
|
||
### Making sure the integrity of the imports are correct | ||
Deno has a way to make sure that the imported packages can't just update their code on the server without you automatically retrieving this code. | ||
|
||
Deno has a way to make sure that the imported packages can't just update their code on the server without you | ||
automatically retrieving this code. | ||
That's why we also have a `lock.json` in the project. | ||
|
||
When this project is opened for the first time on a new computer please run: | ||
|
||
```deno cache --lock=lock.json src/deps.ts``` | ||
|
||
This will make sure that the correct versions are downloaded into the computers cache where each import is integrity checked. | ||
This will make sure that the correct versions are downloaded into the computers cache where each import is integrity | ||
checked. | ||
|
||
### How to build to an executable | ||
To compile the executable the following arguments need to be set during compile otherwise the executable will not be able to run correctly | ||
|
||
To compile the executable the following arguments need to be set during compile otherwise the executable will not be | ||
able to run correctly | ||
|
||
```deno compile --allow-read --allow-net --allow-env=ANONYMIZER_LOCAL_HOSTNAME,ANONYMIZER_LOCAL_PORT,ANONYMIZER_LOCAL_DATABASE,ANONYMIZER_LOCAL_USERNAME,ANONYMIZER_LOCAL_PASSWORD,ANONYMIZER_CONFIG,ANONYMIZER_LOCAL_CONNECTION_TIMEOUT,FAKER_LOCALE --output=build/anonymizer src/anonymizer.ts``` | ||
|
||
It is recommended to limit the `--allow` flags like for example `--allow-read=/var/www` and `--allow-net=127.0.0.1`. | ||
|
||
### How to use | ||
|
||
Using the executable can be done as follows | ||
|
||
```ANONYMIZER_LOCAL_DATABASE=<db_name> ANONYMIZER_LOCAL_USERNAME=<db_user> ANONYMIZER_LOCAL_PASSWORD=<db_pass> ANONYMIZER_CONFIG=<path/to/json/config/file> FAKER_LOCALE=<isolang> anonymizer``` | ||
```ANONYMIZER_LOCAL_DATABASE=<db_name> ANONYMIZER_LOCAL_USERNAME=<db_user> ANONYMIZER_LOCAL_PASSWORD=<db_password> ANONYMIZER_CONFIG=<path/to/json/config/file> FAKER_LOCALE=<isolang> anonymizer``` | ||
|
||
### Use the example to test | ||
|
||
You can use the provided `example.sql` and `example.json` to verify the tool | ||
|
||
1. Use the `example.sql` to create an `example` database with a `dummy_data` table and some entries | ||
2. Create an `example` user and password and only grant it permissions to the `example` database. | ||
3. Then run the Anonymizer using: `ANONYMIZER_LOCAL_DATABASE=example ANONYMIZER_LOCAL_USERNAME=example ANONYMIZER_LOCAL_PASSWORD=example ANONYMIZER_CONFIG=<path/to/example.json> FAKER_LOCALE=en anonymizer` | ||
1. Import `example.sql` into your database, with the name `example`, to obtain dummy data | ||
2. Create a user and password in your database and only grant it permissions to this database. In this case the user and password are both `example` | ||
3. Then run, based on your database name, user and password, the Anonymizer using: | ||
```bash | ||
ANONYMIZER_LOCAL_DATABASE=example ANONYMIZER_LOCAL_USERNAME=example ANONYMIZER_LOCAL_PASSWORD=example ANONYMIZER_CONFIG=./example/example.json FAKER_LOCALE=en anonymizer | ||
``` | ||
4. The following ENV variables are optional | ||
* `ANONYMIZER_LOCAL_HOSTNAME` sets the hostname of MySQL (defaults to `127.0.0.1`) | ||
* `ANONYMIZER_LOCAL_PORT` sets the port number of MySQL (defaults to `3306` ) | ||
* `ANONYMIZER_LOCAL_CONNECTION_TIMEOUT` set DB connection timeout in seconds (defaults to `60` ) | ||
* `FAKER_LOCALE` sets the faker locale (defaults to `en` ) | ||
* `ANONYMIZER_LOCAL_HOSTNAME` sets the hostname of MySQL (defaults to `127.0.0.1`) | ||
* `ANONYMIZER_LOCAL_PORT` sets the port number of MySQL (defaults to `3306` ) | ||
* `ANONYMIZER_LOCAL_CONNECTION_TIMEOUT` set DB connection timeout in seconds (defaults to `60` ) | ||
* `FAKER_LOCALE` sets the faker locale (defaults to `en` ) | ||
|
||
### Unit tests | ||
You can also run unit test by running the following command `deno test` | ||
|
||
You can also run unit test by running the following command | ||
|
||
``` bash | ||
deno test --allow-env | ||
``` | ||
|
||
You can also generate a coverage report by running the following commands | ||
|
||
`deno test --allow-env --coverage=cov_profile` | ||
``` bash | ||
deno test --allow-env --coverage=cov_profile | ||
``` | ||
|
||
`deno coverage cov_profile --lcov > cov_profile/cov_profile.lcov` | ||
```bash | ||
deno coverage cov_profile --lcov > cov_profile/cov_profile.lcov | ||
``` | ||
|
||
If you have the `genhtml` package you can generate a html report of the coverage | ||
|
||
`genhtml -o cov_profile/html cov_profile/cov_profile.lcov` | ||
```bash | ||
genhtml -o cov_profile/html cov_profile/cov_profile.lcov | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,14 +34,22 @@ CREATE TABLE `dummy_data` ( | |
`city` varchar(250) DEFAULT NULL, | ||
`email` varchar(250) DEFAULT NULL, | ||
`telephone` varchar(250) DEFAULT NULL, | ||
`custom` varchar(250) DEFAULT NULL, | ||
`custom_column` varchar(250) DEFAULT NULL, /*Column for testing custom queries*/ | ||
`ignored_column` varchar(250) DEFAULT NULL, /*Column to test if script still runs if column does exist in database, but not mentioned in config file*/ | ||
PRIMARY KEY (`id`) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
); | ||
|
||
/*Table with empty columns to test if script still runs if table does exist in database, but not mentioned in config file*/ | ||
CREATE TABLE `ignored_table` ( | ||
`id` int(11) unsigned NOT NULL AUTO_INCREMENT, | ||
`username` varchar(250) DEFAULT NULL, | ||
PRIMARY KEY (`id`) | ||
); | ||
|
||
LOCK TABLES `dummy_data` WRITE; | ||
/*!40000 ALTER TABLE `dummy_data` DISABLE KEYS */; | ||
|
||
INSERT INTO `dummy_data` (`id`, `username`, `first_name`, `last_name`, `street`, `city`, `email`, `telephone`, `custom`) | ||
INSERT INTO `dummy_data` (`id`, `username`, `first_name`, `last_name`, `street`, `city`, `email`, `telephone`, `custom_column`) | ||
VALUES | ||
(1,'user 1','first name 1','last name 1','street 1','city 1','[email protected]','0612345678','[email protected]'), | ||
(2,'user 2','first name 2','last name 2','street 2','city 2','[email protected]','0612345678','[email protected]'), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters