# Apollo-Auth0-deploy

## Running cli application with Docker

# ⚠️ WARNING
This tool can be destructive to your Auth0 tenant. Please ensure you have read the documentation and tested the tool on a development tenant before using it in production.

With this utility you can deploy, dump and copy configurations between tenants.

# Known issues:

- Cannot get/set configuration variables (Rule's secrets)
- Cannot get id's/secrets from fresh created entities
- Cannot export hooks issue https://github.com/auth0/auth0-deploy-cli/issues/208
- Delete database if no entity provided https://github.com/auth0/auth0-deploy-cli/issues/201
- Replace mappings not work for all: https://github.com/auth0/auth0-deploy-cli/issues/133

# TODO:

- Add method to get all id's/secrets

# Legend:

<tenant_name> - take the name of your subdomain from auth0. Example: <tenant_name>.auth0.com

<working_dir> - subfolder of your configuration under ./conf/

FILES - wildcarded list of files. 
```
FILES="-f=/rules/'Fill block metadata.js' -f=/rules/'Fill block metadata.json'"
FILES="-f='/rules/F*'"
```

DRY_RUN - 1|0 allows to run export or import operations on tenant. 

```
# Default
DRY_RUN=0
```

AUTH0_ALLOW_DELETE - 1|0 allows to delete nonexistent objects. It's dangerous!

```
# Default
AUTH0_ALLOW_DELETE=0
```

**1.** Install [Docker](https://www.docker.com/) and Make.

**2.** Build Docker image.

```bash
$ cd /path/to/application/directory/
$ make docker/build
```
**3.** Setup env files

It is possible to use tool between tenants so create as many env's as count of tenants.
You need to copy .env.example -> .env.<tenant_name>

Several environment variables must be set:

* Auth0 domain and app: ``AUTH0_DOMAIN``, ``AUTH0_CLIENT_ID``, ``AUTH0_CLIENT_SECRET``.

**3.** Run the application to check is it good

```bash
$ make auth0/status ENV_FILE=<tenant_name> WORKING_DIR=temp FILES="-f=/rules/'Fill block metadata'.*" DRY_RUN=1        
$ make auth0/deploy ENV_FILE=sme-develop WORKING_DIR=temp FILES="-f='/rules/F**'" DRY_RUN=0 
```

**3.** Run the application to check is it good

```bash
$ make auth0/status ENV_FILE=<tenant_name> WORKING_DIR=temp FILES="-f=/rules/'Fill block metadata'.*" DRY_RUN=1        
$ make auth0/deploy ENV_FILE=sme-develop WORKING_DIR=temp FILES="-f='/rules/F*'" DRY_RUN=0 
```
**4.** Deploy to Auth0

You can use masking of real variables at deploy time.
1. Set up variables in .env.<tenant_name>.
2. Use ##VAR_NAME## mask in your configuration

```bash
$ make auth0/deploy ENV_FILE=sme-develop WORKING_DIR=temp FILES="-f='/rules/F**'" DRY_RUN=0 
```

**5.** Dump from Auth0

It will run application and download tenant configuration.

```bash
$ make auth0/dump ENV_FILE=<tenant_name> WORKING_DIR=<working_dir> DRY_RUN=0
```

# Terraform

## Terraform installation

To install Terraform, find the [appropriate package](https://www.terraform.io/downloads.html) for your system and download it. Terraform is packaged as a zip archive.

After downloading Terraform, unzip the package. Terraform runs as a single binary named terraform. Any other files in the package can be safely removed and Terraform will still function.

The final step is to make sure that the terraform binary is available on the PATH. See [this page](https://stackoverflow.com/questions/14637979/how-to-permanently-set-path-on-linux) for instructions on setting the PATH on Linux and Mac. [This page](https://stackoverflow.com/questions/1618280/where-can-i-set-path-to-make-exe-on-windows) contains instructions for setting the PATH on Windows.

## Verifying the Installation

After installing Terraform, verify the installation worked by opening a new terminal session and checking that `terraform` is available. By executing `terraform` you should see help output similar to this:

```text
$ terraform
Usage: terraform [--version] [--help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

Common commands:
    apply              Builds or changes infrastructure
    console            Interactive console for Terraform interpolations
# ...
```

If you get an error that `terraform` could not be found, your `PATH` environment variable was not set up properly. Please go back and ensure that your `PATH` variable contains the directory where Terraform was installed.


## Configuration

### Terraform provider configuration

Auth0 provider requires `domain`, `client_id` and `client_secret`. We've a file called terraform.tfvars-example. Terraform automatically reads files with `tfvars` extension. First of all you'll need to copy this file to `terraform.tfvars` without `-example` suffix. After that you'll need to provide correct values, you can find them in Auth0. We'll create an application with admin permissions in all tenants with the same name `Terraform`, you can copy credentials from this application.

### Terraform backend configuration

Terraform supports team-based workflows with a feature known as [remote backends](https://www.terraform.io/docs/backends/index.html). Remote backends allow Terraform to use a shared storage space for state data, so any member of your team can use Terraform to manage the same infrastructure.

We use an S3 bucket to store terraform state. To get access to the S3 bucket each user should have an access to `gdb-infra-dev` account. If you don't have an access to `gdb-infra-dev` account please ask an administrator to grant you an access.

After login to `gdb-infra-dev` account you will be able to create `AWS_ACCESS_KEY` and `AWS_SECRET_ACCESS_KEY` for yourself, if you already have one, you can skip it.

You'll need to create [AWS CLI configuration](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) on your workstation.   

Run following command:

```shell script
aws configure --profile gdb-infra-dev
```

You'll be prompted to enter `AWS_ACCESS_KEY` and `AWS_SECRET_ACCESS_KEY` as well as `Default region` and `Output format`

```text
$ aws configure --profile gdb-infra-dev
AWS Access Key ID [None]: AKIAI44QH8DHBEXAMPLE
AWS Secret Access Key [None]: je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY
Default region name [None]: us-east-1
Default output format [None]: json
``` 

After that you can move forward and initialize configuration.

## Initialization

The first command to run for a new configuration -- or after checking out an existing configuration from version control -- is `terraform init`, which initializes various local settings and data that will be used by subsequent commands.

You will need to run `terraform init` inside `terraform/dev` directory as a result it will create `.terraform` directory.

## Plan/Apply changes

`terraform plan` command will show you difference between a state and changes in the code or actual configuration if someone has made manual changes to resource managed by Terraform.

`terraform apply` command will do the same as `terraform plan` but in addition it allows you to apply changes after confirmation.

  