Skip to content

Commit

Permalink
Merge pull request #33 from eerkunt/issue-20/git-authentication-is-no…
Browse files Browse the repository at this point in the history
…t-supported

Issue 20/git authentication is not supported
  • Loading branch information
eerkunt authored Oct 23, 2018
2 parents 14684d0 + 3990a08 commit 7c8a9f4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ build
dist
example/tf_files/*
terraform_compliance.egg-info

.DS_Store
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ optional arguments:
Directory consists of BDD features
--tfdir terraform_directory, -t terraform_directory
Directory consists of Terraform Files
--identity ssh_private_key, -i ssh_private_key
SSH Private key file used for GIT authentication
```

You can also push additional arguments that is specific for `radish`. Just to explain how it works ;
Expand Down Expand Up @@ -110,6 +112,16 @@ or if all of your features/terraform files are in a remote git repository, you c
-t git:https://some.git.repository/terraform-repo.git
```

## Running terraform-compliance with private GIT repositories
terraform-compliance 0.4.0 supports ssh authentication via git repositories. All you need to do is using `-i` flag and
pointing your ssh private key for git authentication

```
~# terraform-compliance -f /path/to/features -t git:ssh://fqdn/path/go/repo.git -i /path/to/private.key
```

If you already configured your `~/.ssh/config` and pointing remote host, and private key file, you don't even need to
use `-i` argument, it will be used automatically.

## Example
![Example Run](terraform-compliance-demo.gif)
Expand Down
21 changes: 13 additions & 8 deletions terraform_compliance/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@


__app_name__ = "terraform-compliance"
__version__ = "0.3.10"
__version__ = "0.4.0"


class ArgHandling(object):
pass

#TODO: Handle all directory/protocol handling via a better class structure here.
#TODO: Extend git: (on features or tf files argument) into native URLs instead of using a prefix here.

def cli():
args = ArgHandling()
parser = ArgumentParser(prog=__app_name__,
Expand All @@ -28,6 +25,8 @@ def cli():
parser.add_argument("--tfdir", "-t", dest="tf_dir", metavar='terraform_directory', action=ReadableDir,
help="Directory (or git repository with 'git:' prefix) consists of Terraform Files",
required=True)
parser.add_argument("--identity", "-i", dest="ssh_key", metavar='ssh_private_key', type=str, nargs='?',
help="SSH Private key that will be use on git authentication.", required=False)
parser.add_argument("--version", "-v", action="version", version=__version__)

_, radish_arguments = parser.parse_known_args(namespace=args)
Expand All @@ -37,20 +36,26 @@ def cli():
steps_directory = os.path.join(os.path.split(os.path.abspath(__file__))[0], 'steps')
print('Steps : {}'.format(steps_directory))

# SSH Key is given for git authentication
ssh_cmd = {}
if args.ssh_key:
ssh_cmd = {"GIT_SSH_COMMAND": "ssh -l {} -i {}".format('git', args.ssh_key)}

# A remote repository used here
if args.features.startswith('http'):
if args.features.startswith(('http', 'https', 'ssh')):
features_git_repo = args.features
args.features = mkdtemp()
Repo.clone_from(features_git_repo, args.features)

Repo.clone_from(url=features_git_repo, to_path=args.features, env=ssh_cmd)
features_directory = os.path.join(os.path.abspath(args.features))
print('Features : {}{}'.format(features_directory, (' ({})'.format(features_git_repo) if 'features_git_repo' in locals() else '')))

tf_tmp_dir = mkdtemp()

# A remote repository is used here.
if args.tf_dir.startswith('http'):
if args.tf_dir.startswith(('http', 'https', 'ssh')):
tf_git_repo = args.tf_dir
Repo.clone_from(tf_git_repo, tf_tmp_dir)
Repo.clone_from(url=tf_git_repo, to_path=tf_tmp_dir, env=ssh_cmd)

# A local directory is used here
else:
Expand Down

0 comments on commit 7c8a9f4

Please sign in to comment.