terraform-compliance / cli

a lightweight, security focused, BDD test framework against terraform.
https://terraform-compliance.com
MIT License
1.35k stars 151 forks source link

Unable to access the identify file when trying to access the identify file from Folder other than the Terraform module #572

Closed Aniket281292 closed 2 years ago

Aniket281292 commented 2 years ago

Question:

Trying to run feature files present as a part of private bitbucket repository. For authentication purposes, providing the -i parameter and path to the ssh key. When this key is included as part of the Terraform module, this script works fine. command: docker run --rm -v $PWD:/target -it eerkunt/terraform-compliance -f git:ssh://bitbucket.org/.../tf-compliance.git -p plan.out.json -i keyfile_name

Folder Structure

Terraform module

  • main.tf
  • plan.out.json
  • ....
  • ....
  • keyfile_name

However, when the key file is part of a different root directory, the same command throws error of identify file not being accessible. command: docker run --rm -v $PWD:/target -it eerkunt/terraform-compliance -f git:ssh://bitbucket.org/.../tf-compliance.git -p plan.out.json -i /Users/xyz/.ssh/keyfile_name

Folder Structure

xyz -> .ssh. -> keyfile_name . .

Terraform module

  • main.tf
  • plan.out.json
  • ....
  • ....

Error: cmdline: git clone -v --depth=1 --branch=master ssh://bitbucket.org/.../tf-compliance.git /tmp/tmpb4_p5u stderr: 'Cloning into '/tmp/tmpb4_p5u'... Warning: Identity file /Users/xyz/.ssh/keyfile_name not accessible: No such file or directory. Warning: Permanently added '........' (RSA) to the list of known hosts. git@bitbucket.org: Permission denied (publickey). fatal: Could not read from remote repository.

Can we not run this command from anywhere else other than from the Terraform Module itself?

eerkunt commented 2 years ago

Hello @Aniket281292,

The problem is actually not within terraform-compliance but about how you initiate it via docker.

As you provide -v $PWD:/target you are actually mounting $PWD host directory to /target container directory. Please see the documentation about this

Since your ssh key is not within your $PWD, all you need to do is to mount it to the container as well, and then call it from whatever container directory you are mapping to.

e.g.

docker run --rm \ 
-v $PWD:/target \
-v /Users/xyz/.ssh/:/ssh \
-it eerkunt/terraform-compliance \
-f git:ssh://bitbucket.org/.../tf-compliance.git \
-p plan.out.json \
-i /ssh/keyfile_name
Aniket281292 commented 2 years ago

@eerkunt,using the following format did not help since it threw the same error. -v $PWD:/target \ -v /Users/xyz/.ssh/:/ssh \

What helped was when i tried running command from the root (~) directory. i provided both the rsa key and plan.json files as part of this root directory.