## Correction Stage
Lambda for staging corrections 

## Requirements
- Python 3.11

## OWS Dependencies

- [ows-worksheet-correction](https://github.com/theorchard/ows-worksheet-correction)

## Running 


### Set up the environment
Copy the `.env.shadow` to a local `.env`:
```shell
cp .env.shadow .env
```

#### AWS Creds
Generate your AWS credentials using [awsume](https://www.notion.so/AWS-Access-f841b9dd815d4443a80e96a86c92cd2f?pvs=4#f11f08d09f16493a8a3e9dbf38d87164): `awsume prod`.

Get your `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, and `AWS_SESSION_TOKEN` by running `awsume prod -s`.

Copy the values into your `.env` and/or export them in your CLI.

Example export:
```shell
export AWS_ACCESS_KEY_ID=<secret>
export AWS_SECRET_ACCESS_KEY=<secret>
export AWS_SESSION_TOKEN=<secret>
```

#### Log into ECR:
```shell
# After generating aws creds
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 437795906767.dkr.ecr.us-east-1.amazonaws.com
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 086679231553.dkr.ecr.us-east-1.amazonaws.com
```

#### Snowflake Creds
You will need to have a Snowflake SSH key pair to run this lambda -- using a username and password is no longer valid.

If you do not have a Snowflake SSH key pair set up, follow these instructions:
1. create a snowflake subdirectory: `mkdir ~/.ssh/snowflake`
2. navigate to the new subdirectory: `cd ~/.ssh/snowflake`
3. run the following command to create a public and private key:
```shell
openssl genrsa 2048 | openssl pkcs8 -topk8 -inform PEM -out rsa_key.p8
openssl rsa -in rsa_key.p8 -pubout -out rsa_key.pub
```
4. you will be prompted to create a **passphrase** -- **use something you will remember and/or jot it down, you will need this to decrypt your private key**
5. copy your `rsa_key.pub` (without the header/footer) and add it to [terraform](https://github.com/theorchard/terraform-infra/blob/master/prod/snowflake/orchard/users/users.auto.tfvars) ([example PR](https://github.com/theorchard/terraform-infra/pull/15503))


Make sure to fill in all the necessary snowflake environment variables in your `.env`:
* `SNOWFLAKE_USER` is your email (i.e. `dczinsky@sonymusic-pde.com`)
* `SNOWFLAKE_KEY_PASSPHRASE` is the key passphrase you used when creating your key-pair
* `SNOWFLAKE_PRIVATE_KEY_PATH` is the path to your private key on your machine -- if you followed the instructions above it should be `~/.ssh/snowflake/rsa_key.p8`


You may need to export these variables:
```shell
export SNOWFLAKE_USER=dczinsky@sonymusic-pde.com
export SNOWFLAKE_PRIVATE_KEY_PATH=~/.ssh/snowflake/rsa_key.p8
export SNOWFLAKE_KEY_PASSPHRASE=<secret>
```

or source all your environment variables from your `.env`:
```shell
source .env
```


### Start local lambda

```shell
make docker_local_up
```

Note: This brings up the local code, if you want to pull and run the most recently deployed image run `make start_ows`, it may take a while either way 

### Execute Function

Use the HTTP client of your choice. The body of the request is the `abacus_event` passed into the function.

```shell
curl --request POST \
  --url http://localhost:9000/2015-03-31/functions/function/invocations \
  --header 'Content-Type: application/json' \
  --data $(cat tests/sample_event.json)
```

### Running the linter and the unit tests

```shell
make docker_test
```

### Running the integration tests

```shell
make test_integration
```

Note: this is set up to run against the most recently deployed image, so if you want it to run against any local changes you'll need to set it up differently.
