- 
                Notifications
    You must be signed in to change notification settings 
- Fork 55
CM-25773 - Support TF Plan scans #153
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
          
     Merged
      
      
    
  
     Merged
                    Changes from 6 commits
      Commits
    
    
            Show all changes
          
          
            36 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      082730c
              
                CM-25773- initial commit
              
              
                EfratIsrael 12b8a5c
              
                lint fix
              
              
                EfratIsrael a09ff32
              
                remove new line
              
              
                EfratIsrael 197e459
              
                add try-except
              
              
                EfratIsrael 14d1c52
              
                change list to List
              
              
                EfratIsrael c03bb27
              
                key error exception handler
              
              
                EfratIsrael 11ca913
              
                prettier
              
              
                EfratIsrael 84242b9
              
                encoding
              
              
                EfratIsrael d13d518
              
                fix path
              
              
                EfratIsrael 803c275
              
                lint
              
              
                EfratIsrael d32568d
              
                review fixing
              
              
                EfratIsrael 763622e
              
                renaming + typo fixing
              
              
                EfratIsrael 2bf1ded
              
                remove redundant json load check
              
              
                EfratIsrael 7bda660
              
                change soft fail
              
              
                EfratIsrael f8d4933
              
                add json.loads wrapper
              
              
                EfratIsrael 046b08d
              
                lint
              
              
                EfratIsrael a8050ea
              
                remove constructor
              
              
                EfratIsrael 184c9d8
              
                refactor iac parsing
              
              
                EfratIsrael 97f1b03
              
                handle null after + add test
              
              
                EfratIsrael 7c3296a
              
                Fix error handling
              
              
                MarshalX f9042bf
              
                Fix replacement of files
              
              
                MarshalX f2a5ea5
              
                fix iac doc manipulation
              
              
                EfratIsrael d4cd8f9
              
                revert iac manipultaion outside pre scan doc
              
              
                EfratIsrael 1d6a90a
              
                adding tests for different plans +
              
              
                EfratIsrael f0249e3
              
                fix readme
              
              
                EfratIsrael 27d4505
              
                fix readme
              
              
                EfratIsrael dab3f92
              
                fixing
              
              
                EfratIsrael 4c3784c
              
                pr fixing
              
              
                EfratIsrael 1e1ac2d
              
                pr fixing
              
              
                EfratIsrael 9ab3334
              
                lint
              
              
                EfratIsrael 921b047
              
                test fix
              
              
                EfratIsrael 805b0f6
              
                add test for generate document
              
              
                EfratIsrael b68e68f
              
                lint
              
              
                EfratIsrael fa0196e
              
                update test
              
              
                EfratIsrael cfb3b88
              
                typo fix
              
              
                EfratIsrael b0435d6
              
                move \n
              
              
                EfratIsrael File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import json | ||
| from typing import List | ||
|  | ||
| from cycode.cli.models import ChangeResource | ||
|  | ||
|  | ||
| def generate_tf_content_from_tfplan(tfplan: str) -> str: | ||
| planned_resources = _extract_resources(tfplan) | ||
| return _generate_tf_content(planned_resources) | ||
|  | ||
|  | ||
| def _extract_resources(tfplan: str) -> List[ChangeResource]: | ||
| tfplan_json = json.loads(tfplan) | ||
|         
                  EfratIsrael marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| resources: List[ChangeResource] = [] | ||
| for change in tfplan_json.get('resource_changes', []): | ||
| resources.append( | ||
| ChangeResource(resource_type=change['type'], name=change['name'], values=change['change']['after']) | ||
|         
                  EfratIsrael marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| ) | ||
| return resources | ||
|  | ||
|  | ||
| def _generate_tf_content(resources: List[ChangeResource]) -> str: | ||
|         
                  EfratIsrael marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| tf_content = '' | ||
| for resource in resources: | ||
| tf_content += f'resource \"{resource.resource_type}\" \"{resource.name}\" {{\n' | ||
| for key, value in resource.values.items(): | ||
| tf_content += f' {key} = {json.dumps(value)}\n' | ||
|         
                  EfratIsrael marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| tf_content += '}\n\n' | ||
| return tf_content | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import os | ||
|  | ||
|  | ||
| def change_filename_extension(filename: str, extension: str) -> str: | ||
|         
                  EfratIsrael marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| base_name, old_ext = os.path.splitext(filename) | ||
|         
                  EfratIsrael marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| return base_name + '.' + extension | ||
|         
                  EfratIsrael marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
              Empty file.
          
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import os | ||
|  | ||
| from cycode.cli.helpers import tf_content_generator | ||
|  | ||
| examples_main_dir = '../../test_files/tf_content_generator_files' | ||
|  | ||
|  | ||
| def test_generate_tf_content_from_tfplan() -> None: | ||
| examples_directories = [ | ||
| name for name in os.listdir(examples_main_dir) if os.path.isdir(os.path.join(examples_main_dir, name)) | ||
| ] | ||
|  | ||
| for example in examples_directories: | ||
| tfplan_path = os.path.join(examples_main_dir, example, 'tfplan.json') | ||
| tf_expected_content_path = os.path.join(examples_main_dir, example, 'tf_content.txt') | ||
| with open(tfplan_path, 'r') as tfplan_file, open(tf_expected_content_path, 'r') as tf_expected_content_file: | ||
|         
                  doratias18 marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| tfplan_content: str = tfplan_file.read() | ||
| tf_expected_content: str = tf_expected_content_file.read() | ||
| tf_content = tf_content_generator.generate_tf_content_from_tfplan(tfplan_content) | ||
| assert tf_content == tf_expected_content | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            14 changes: 14 additions & 0 deletions
          
          14 
        
  tests/test_files/tf_content_generator_files/example_1/tf_content.txt
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| resource "aws_s3_bucket" "efrat-env-var-test" { | ||
| bucket = "efrat-env-var-test" | ||
| force_destroy = false | ||
| tags = null | ||
| timeouts = null | ||
| } | ||
|  | ||
| resource "aws_s3_bucket_public_access_block" "efrat-env-var-test" { | ||
| block_public_acls = false | ||
| block_public_policy = true | ||
| ignore_public_acls = false | ||
| restrict_public_buckets = true | ||
| } | ||
|  | 
        
          
          
            218 changes: 218 additions & 0 deletions
          
          218 
        
  tests/test_files/tf_content_generator_files/example_1/tfplan.json
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| { | ||
| "format_version": "1.2", | ||
| "terraform_version": "1.5.4", | ||
| "variables": { | ||
| "IT_IS_FALSE": { | ||
| "value": "false" | ||
| } | ||
| }, | ||
| "planned_values": { | ||
| "root_module": { | ||
| "resources": [ | ||
| { | ||
| "address": "aws_s3_bucket.efrat-env-var-test", | ||
| "mode": "managed", | ||
| "type": "aws_s3_bucket", | ||
| "name": "efrat-env-var-test", | ||
| "provider_name": "registry.terraform.io/hashicorp/aws", | ||
| "schema_version": 0, | ||
| "values": { | ||
| "bucket": "efrat-env-var-test", | ||
| "force_destroy": false, | ||
| "tags": null, | ||
| "timeouts": null | ||
| }, | ||
| "sensitive_values": { | ||
| "cors_rule": [], | ||
| "grant": [], | ||
| "lifecycle_rule": [], | ||
| "logging": [], | ||
| "object_lock_configuration": [], | ||
| "replication_configuration": [], | ||
| "server_side_encryption_configuration": [], | ||
| "tags_all": {}, | ||
| "versioning": [], | ||
| "website": [] | ||
| } | ||
| }, | ||
| { | ||
| "address": "aws_s3_bucket_public_access_block.efrat-env-var-test", | ||
| "mode": "managed", | ||
| "type": "aws_s3_bucket_public_access_block", | ||
| "name": "efrat-env-var-test", | ||
| "provider_name": "registry.terraform.io/hashicorp/aws", | ||
| "schema_version": 0, | ||
| "values": { | ||
| "block_public_acls": false, | ||
| "block_public_policy": true, | ||
| "ignore_public_acls": false, | ||
| "restrict_public_buckets": true | ||
| }, | ||
| "sensitive_values": {} | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| "resource_changes": [ | ||
| { | ||
| "address": "aws_s3_bucket.efrat-env-var-test", | ||
| "mode": "managed", | ||
| "type": "aws_s3_bucket", | ||
| "name": "efrat-env-var-test", | ||
| "provider_name": "registry.terraform.io/hashicorp/aws", | ||
| "change": { | ||
| "actions": [ | ||
| "create" | ||
| ], | ||
| "before": null, | ||
| "after": { | ||
| "bucket": "efrat-env-var-test", | ||
| "force_destroy": false, | ||
| "tags": null, | ||
| "timeouts": null | ||
| }, | ||
| "after_unknown": { | ||
| "acceleration_status": true, | ||
| "acl": true, | ||
| "arn": true, | ||
| "bucket_domain_name": true, | ||
| "bucket_prefix": true, | ||
| "bucket_regional_domain_name": true, | ||
| "cors_rule": true, | ||
| "grant": true, | ||
| "hosted_zone_id": true, | ||
| "id": true, | ||
| "lifecycle_rule": true, | ||
| "logging": true, | ||
| "object_lock_configuration": true, | ||
| "object_lock_enabled": true, | ||
| "policy": true, | ||
| "region": true, | ||
| "replication_configuration": true, | ||
| "request_payer": true, | ||
| "server_side_encryption_configuration": true, | ||
| "tags_all": true, | ||
| "versioning": true, | ||
| "website": true, | ||
| "website_domain": true, | ||
| "website_endpoint": true | ||
| }, | ||
| "before_sensitive": false, | ||
| "after_sensitive": { | ||
| "cors_rule": [], | ||
| "grant": [], | ||
| "lifecycle_rule": [], | ||
| "logging": [], | ||
| "object_lock_configuration": [], | ||
| "replication_configuration": [], | ||
| "server_side_encryption_configuration": [], | ||
| "tags_all": {}, | ||
| "versioning": [], | ||
| "website": [] | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| "address": "aws_s3_bucket_public_access_block.efrat-env-var-test", | ||
| "mode": "managed", | ||
| "type": "aws_s3_bucket_public_access_block", | ||
| "name": "efrat-env-var-test", | ||
| "provider_name": "registry.terraform.io/hashicorp/aws", | ||
| "change": { | ||
| "actions": [ | ||
| "create" | ||
| ], | ||
| "before": null, | ||
| "after": { | ||
| "block_public_acls": false, | ||
| "block_public_policy": true, | ||
| "ignore_public_acls": false, | ||
| "restrict_public_buckets": true | ||
| }, | ||
| "after_unknown": { | ||
| "bucket": true, | ||
| "id": true | ||
| }, | ||
| "before_sensitive": false, | ||
| "after_sensitive": {} | ||
| } | ||
| } | ||
| ], | ||
| "configuration": { | ||
| "provider_config": { | ||
| "aws": { | ||
| "name": "aws", | ||
| "full_name": "registry.terraform.io/hashicorp/aws", | ||
| "expressions": { | ||
| "profile": { | ||
| "constant_value": "efrat" | ||
| }, | ||
| "region": { | ||
| "constant_value": "us-east-1" | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "root_module": { | ||
| "resources": [ | ||
| { | ||
| "address": "aws_s3_bucket.efrat-env-var-test", | ||
| "mode": "managed", | ||
| "type": "aws_s3_bucket", | ||
| "name": "efrat-env-var-test", | ||
| "provider_config_key": "aws", | ||
| "expressions": { | ||
| "bucket": { | ||
| "constant_value": "efrat-env-var-test" | ||
| } | ||
| }, | ||
| "schema_version": 0 | ||
| }, | ||
| { | ||
| "address": "aws_s3_bucket_public_access_block.efrat-env-var-test", | ||
| "mode": "managed", | ||
| "type": "aws_s3_bucket_public_access_block", | ||
| "name": "efrat-env-var-test", | ||
| "provider_config_key": "aws", | ||
| "expressions": { | ||
| "block_public_acls": { | ||
| "references": [ | ||
| "var.IT_IS_FALSE" | ||
| ] | ||
| }, | ||
| "block_public_policy": { | ||
| "constant_value": true | ||
| }, | ||
| "bucket": { | ||
| "references": [ | ||
| "aws_s3_bucket.efrat-env-var-test.id", | ||
| "aws_s3_bucket.efrat-env-var-test" | ||
| ] | ||
| }, | ||
| "ignore_public_acls": { | ||
| "constant_value": false | ||
| }, | ||
| "restrict_public_buckets": { | ||
| "constant_value": true | ||
| } | ||
| }, | ||
| "schema_version": 0 | ||
| } | ||
| ], | ||
| "variables": { | ||
| "IT_IS_FALSE": { | ||
| "description": "This is an example input variable using env variables." | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "relevant_attributes": [ | ||
| { | ||
| "resource": "aws_s3_bucket.efrat-env-var-test", | ||
| "attribute": [ | ||
| "id" | ||
| ] | ||
| } | ||
| ], | ||
| "timestamp": "2023-07-31T17:54:18Z" | ||
| } | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.