## Authenticating AWS EC2 Adapter 

This document will go through the steps for authenticating the AWS EC2 adapter with AWS Signature 4 Authentication. Properly configuring the properties for an adapter in IAP is critical for getting the adapter online. You can read more about adapter authentication <a href="https://docs.itential.com/opensource/docs/authentication" target="_blank">HERE</a>. 

### AWS Authentication
The AWS EC2 adapter requires AWS Authentication it does not utilize any of the autnetication methods provided by the adapter library as a result, the auth_method should be set to no_authentication. The adapter utilizes AWS signature 4 authentication. There are 3 flavors of doing this. 

The first way is using a "service" account and its AWS keys to authentication as that account. In this case, you will get the aws_access_key, aws_secret_key, and aws_session_token from AWS and configure them into the adapter service instance as shown below.

The second way is using AWS STS. this still requires a "service" account and its AWS keys to authentication as that account. In this case, you will get the aws_access_key, aws_secret_key, and aws_session_token from AWS and configure them into the adapter service instance as shown below. In addition, you will provide STS paramaters in the workflow tasks that tell the adapter the role you want used on the particular call.

The third authentication method is to use an IAM role. With this method, you do not need any authentication keys as the adapter will utilize an "internal" AWS call to get the things that it needs for authentication. Since the adapter needs to make the call to this "internal" AWS IP address, the IAP server needs to be where it has access to that address or you will not be able to use this method.

If you change authentication methods, you should change this section accordingly and merge it back into the adapter repository.

### AWS Signature 4 Service Account Authentication
The AWS EC2 adapter requires AWS Signature 4 Authentication. If you change authentication methods, you should change this section accordingly and merge it back into the adapter repository.

This authentication is done in the adapter itself and not in the adapter libraries. This is why the auth_method is set to "no_authentication".

STEPS  
1. Ensure you have access to a AWS EC2 server and that it is running
2. Follow the steps in the README.md to import the adapter into IAP if you have not already done so
3. Use the properties below for the ```properties.authentication``` field
```json
"authentication": {
  "auth_method": "no_authentication",
  "aws_access_key": "aws_access_key",
  "aws_secret_key": "aws_secret_key",
  "aws_session_token": "aws_session_token"
}
```
you can leave all of the other properties in the authentication section, they will not be used for AWS EC2 authentication.
4. Restart the adapter. If your properties were set correctly, the adapter should go online. 

### AWS Security Token Service
The AWS EC2 adapter also supports AWS Security Token Service (STS) Authentication. For using this authentication, you need to use the calls in the Adapter that have the STSRole suffix on them and pass the STS information into the method.

### AWS IAM Role
The AWS EC2 adapter also supports AWS IAM Role Authentication. For using this authentication, you need to use the calls in the Adapter that have the STSRole suffix on them and pass the RoleName into the method.

### AMAZON STEPS FOR IAM ROLE
Increase number of hops if running IAP inside of docker on EC2 instance
```bash
aws sso login --profile aws-bota-1
<export aws keys for CLI access>

aws ec2 modify-instance-metadata-options  --instance-id i-0e150236026b7c45d  --http-put-response-hop-limit 3 --http-endpoint enabled --region us-east-1
```

Create a new role and attach to it policies:
- go to your EC2 instance, select it
- Actions->Security->Modify IAM Role
- Click 'Create New IAM Role'
- Create a role:
```text
Trusted entity type: AWS service
Use Case: EC2
```

Add policies to the role
- AmazonEC2FullAccess (Provides full access to Amazon EC2 via the AWS Management Console.)

Save the role

Go back to your EC2 instance and Actions->Security->Modify IAM Role, associate newly created role with your EC2 instance

### Troubleshooting
- Make sure you copied over the correct access key, secret key and session token.
- Turn on debug level logs for the adapter in IAP Admin Essentials.
- Turn on auth_logging for the adapter in IAP Admin Essentials (adapter properties).
- Investigate the logs - in particular:
  - The FULL REQUEST log to make sure the proper headers are being sent with the request.
  - The FULL BODY log to make sure the payload is accurate.
  - The CALL RETURN log to see what the other system is telling us.
- Credentials should be ** masked ** by the adapter so make sure you verify the username and password - including that there are erroneous spaces at the front or end.
- Remember when you are done to turn auth_logging off as you do not want to log credentials.
- For IAM, you can run this on the IAP server to verify you are getting to the "internal" AWS Server
```bash
TOKEN=`curl -v -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` && curl -v -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/iam/security-credentials/<rolename>
```