terraform-compliance / cli

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

Support for aws codecommit private git repository #654

Open amitkewal opened 1 year ago

amitkewal commented 1 year ago

Support for aws codecommit private git repository

When I ran terraform-compliance on the repository present in the aws codecommit , the terraform-compliance throws the error:

File "C:\Users\AKewal\AppData\Local\Programs\Python\Python310\lib\site-packages\terraform_compliance\main.py", line 95, in cli
    raise ValueError("Bad feature directory:" + args.features)
ValueError: Bad feature directory:https://git-codecommit.us-east-1.amazonaws.com/v1/repos/MY_REPO

Suggested Solution description ( if you have any ): I have looked over the code and found out it supports only URL ending with *.git


 if args.features.startswith(('http', 'https', 'ssh')):
        # Default to master branch and full repository
        if args.features.endswith('.git'):
            features_git_repo = args.features
            features_git_branch = "master"

        # Optionally allow for directory and branch
        elif '.git//' in args.features and '?ref=' in args.features:
            # Split on .git/
            features_git_list = args.features.split('.git/', 1)
            # Everything up to .git is the repository
            features_git_repo = features_git_list[0] + '.git'

            # Split the directory and branch ref
            features_git_list = features_git_list[1].split('?ref=', 1)
            features_dir = features_git_list[0]
            features_git_branch = features_git_list[1]

        else:  # invalid
            raise ValueError("Bad feature directory:" + args.features)

AWS Codecommit git URL sample: https://git-codecommit.us-east-1.amazonaws.com/v1/repos/MY_REPO. I tweaked the code and didn't filter this url based on .git ending and was able to clone the repo and run the terraform compliance command successfully.

Sharing code snippet here

    if args.features.startswith(('http', 'https', 'ssh')):
        # Default to master branch and full repository
        features_codecommit_list = 

> args.features.split('//', 2)

        if args.features.endswith('.git'):
            features_git_repo = args.features
            features_git_branch = 'master'

        # Optionally allow for directory and branch
        elif '.git//' in args.features and '?ref=' in args.features:
            # Split on .git/
            features_git_list = args.features.split('.git/', 1)
            # Everything up to .git is the repository
            features_git_repo = features_git_list[0] + '.git'

            # Split the directory and branch ref
            features_git_list = features_git_list[1].split('?ref=', 1)
            features_dir = features_git_list[0]
            features_git_branch = features_git_list[1]

>        elif len(features_codecommit_list) == 3:
>             features_split_list = features_codecommit_list[2].split('?ref=',1)
>             if '?ref=' in args.features:
>                 features_dir = features_split_list[0]
>                 features_git_branch = features_split_list[1]
>                 features_git_repo = features_codecommit_list[0] + '//' + features_codecommit_list[1] #args.feaures
>             else:
>                 features_dir = features_split_list[0]
>                 features_git_repo = features_codecommit_list[0] + '//' + features_codecommit_list[1] #args.feaures
> 
>         elif len(features_codecommit_list) == 2:
>             features_split_list = args.features.split('?ref=',1)
>             if '?ref=' in args.features:
>                 features_git_branch = features_split_list[1]
>                 features_git_repo = features_split_list[0] #args.feaures
>             else:
>                 features_git_repo = features_split_list[0] #args.feaures

        else:  # invalid

            raise ValueError("Bad feature directory:" + args.features)
chrisdaish commented 1 year ago

Can confirm this issue also occurs with Azure DevOps when using https:// with git.