Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Terraform Cloud / Terraform Enterprise #458

Merged
merged 9 commits into from
May 3, 2021

Conversation

ayshiff
Copy link
Contributor

@ayshiff ayshiff commented Apr 25, 2021

Q A
πŸ› Bug fix? no
πŸš€ New feature? yes
⚠ Deprecations? yes/no
❌ BC Break no
πŸ”— Related issues #434
❓ Documentation yes snyk/driftctl-docs#47

Description

This PR adds support for Terraform Cloud / Terraform Enterprise.

As described in the issue the logic is as follow:

  1. Fetch hosted-state-download-url from the API with the provided $WORKSPACE_ID (tfstate+tfcloud://$WORKSPACE_ID) and the API token through the headers (--headers 'Authorization=Bearer $API_TOKEN')
  2. Use HTTPReader with the retrieved hosted-state-download-url

Note that the logic is the same for Terraform Cloud and Terraform Enterprise as the API is the same for both.

Here is a complete command example:

driftctl scan --from tfstate+tfcloud://$WORKSPACE_ID --headers 'Authorization=Bearer $API_TOKEN'

I also added some test the different cases we can have:

  • The provided workspace_id is wrong (404)
  • The provided authentication api_token is wrong (401)
  • The workspace_id and api_token are good

I have as well updated the documentation in snyk/driftctl-docs#47 to fit this new state reader.

@codecov
Copy link

codecov bot commented Apr 25, 2021

Codecov Report

Merging #458 (ae65fb4) into v0.8 (2ef6593) will decrease coverage by 0.00%.
The diff coverage is 70.37%.

Impacted file tree graph

@@            Coverage Diff             @@
##             v0.8     #458      +/-   ##
==========================================
- Coverage   70.73%   70.73%   -0.01%     
==========================================
  Files         286      287       +1     
  Lines        6445     6472      +27     
==========================================
+ Hits         4559     4578      +19     
- Misses       1516     1521       +5     
- Partials      370      373       +3     
Impacted Files Coverage Ξ”
pkg/iac/terraform/state/backend/backend.go 40.00% <0.00%> (-4.45%) ⬇️
pkg/iac/terraform/state/backend/tfcloud_reader.go 68.42% <68.42%> (ΓΈ)
pkg/cmd/scan.go 82.12% <100.00%> (+0.46%) ⬆️

@wbeuil
Copy link
Contributor

wbeuil commented Apr 26, 2021

Thanks @ayshiff for your contribution. You did great on a first step to support TF Cloud. But we should converge to what they do in Terraform for our user to use the support quite the same way.

You can find all the information for the credentials part here. They rely on their .terraformrc file which can be created with terraform login which is simply:

credentials "app.terraform.io" {
  token = "XXX"
}

We should, thus, support 2 ways:

  • Get the token from .terraformrc (maybe in another filename as well)
  • Inline token from CLI (what you did but would rather write simply --token or something else)

I didn't go further into terraform package but we could re-use one of their functions/methods to fetch the credentials from the file. Maybe they even have a function/method the fetch the state with the token and the workspace_id.

Copy link
Contributor

@sundowndev sundowndev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! πŸ‘πŸ½

Left few comments about error handling and tests. We also might discuss a bit about authentication, as there are multiple ways to authenticate to TFC. We usually don't perform integration tests for backends, so we have to discuss about that too.

pkg/iac/terraform/state/backend/cloud_reader.go Outdated Show resolved Hide resolved
pkg/iac/terraform/state/backend/cloud_reader.go Outdated Show resolved Hide resolved
pkg/iac/terraform/state/backend/cloud_reader.go Outdated Show resolved Hide resolved
pkg/iac/terraform/state/backend/cloud_reader_test.go Outdated Show resolved Hide resolved
@ayshiff
Copy link
Contributor Author

ayshiff commented Apr 27, 2021

I fixed the various issues that came up and added a new --terraform-cloud-token flag to provide the Terraform Cloud API token.

I still have to work on the way to retrieve the token from .terraformrc as I didn't have the time to look at it in detail.

@eliecharra
Copy link
Contributor

I still have to work on the way to retrieve the token from .terraformrc as I didn't have the time to look at it in detail.

@wbeuil @sundowndev Maybe this could be splitted in another PR ? I'm fine with that

@sundowndev
Copy link
Contributor

sundowndev commented Apr 27, 2021

I still have to work on the way to retrieve the token from .terraformrc as I didn't have the time to look at it in detail.

@wbeuil @sundowndev Maybe this could be splitted in another PR ? I'm fine with that

Well I don't really know much about how credentials management work in TFC and what it implies to support the .terraformrc file, but yeah we could introduce this feature with the flag only for now.

@wbeuil
Copy link
Contributor

wbeuil commented Apr 27, 2021

We could indeed split this PR into two.

Just for the record, there is this function which seems to read and load everything there is inside a config file (.terraformrc in linux, terraform.rc in windows).

@sundowndev sundowndev added kind/enhancement New feature or improvement priority/1 labels Apr 27, 2021
@sundowndev sundowndev added this to Review in driftctl via automation Apr 27, 2021
@sundowndev sundowndev added this to the v0.8.0 milestone Apr 27, 2021
Copy link
Contributor

@sundowndev sundowndev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems great, I just left some comments about the code

pkg/cmd/scan.go Outdated Show resolved Hide resolved
pkg/cmd/scan.go Outdated Show resolved Hide resolved
pkg/iac/terraform/state/backend/cloud_reader.go Outdated Show resolved Hide resolved
pkg/iac/terraform/state/backend/cloud_reader.go Outdated Show resolved Hide resolved
pkg/iac/terraform/state/backend/cloud_reader.go Outdated Show resolved Hide resolved
pkg/iac/terraform/state/backend/cloud_reader.go Outdated Show resolved Hide resolved
pkg/iac/terraform/state/backend/cloud_reader.go Outdated Show resolved Hide resolved
pkg/cmd/scan_test.go Show resolved Hide resolved
@ayshiff ayshiff changed the base branch from main to v0.8 April 28, 2021 10:34
Copy link
Contributor

@wbeuil wbeuil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small comments to be fully consistent with the new wording. Can you also update your branch to have the latest changes from @sundowndev ? Thanks !

}

type Backend io.ReadCloser

type Options struct {
Headers map[string]string
Headers map[string]string
TerraformCloudToken string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we reworded everything with TFCloud, I would suggest to do it everywhere to be consistent. Thus, let's call it TFCloudToken.

)

const BackendKeyTFCloud = "tfcloud"
const TerraformCloudAPI = "https://app.terraform.io/api/v2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TFCloudAPI

@ayshiff ayshiff requested a review from wbeuil May 3, 2021 13:32
Copy link
Contributor

@wbeuil wbeuil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM !

@sundowndev sundowndev merged commit b64276a into snyk:v0.8 May 3, 2021
driftctl automation moved this from Review to Done May 3, 2021
@eliecharra
Copy link
Contributor

Hey @all-contributors please add @ayshiff for code

@allcontributors
Copy link
Contributor

@eliecharra

I've put up a pull request to add @ayshiff! πŸŽ‰

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement New feature or improvement priority/1
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

5 participants