Git Product home page Git Product logo

turbot / steampipe-plugin-terraform Goto Github PK

View Code? Open in Web Editor NEW
28.0 10.0 3.0 462 KB

Use SQL to instantly query resources, data sources and more from Terraform code. Open source CLI. No DB required.

Home Page: https://hub.steampipe.io/plugins/turbot/terraform

License: Apache License 2.0

Makefile 0.19% PLSQL 2.05% Go 97.76%
sql steampipe steampipe-plugin terraform postgresql postgresql-fdw hacktoberfest backup etl sqlite

steampipe-plugin-terraform's Introduction

image

Terraform Plugin for Steampipe

Use SQL to query data from Terraform configuration files.

Quick start

Install the plugin with Steampipe:

steampipe plugin install terraform

Configure your config file to include directories with Terraform configuration files. If no directory is specified, the current working directory will be used.

Run steampipe:

steampipe query

Query all resources in your Terraform files:

select
  name,
  type,
  jsonb_pretty(arguments) as args
from
  terraform_resource;
> select name, type, jsonb_pretty(arguments) as args from terraform_resource;
+------------+----------------+--------------------------------------------+
| name       | type           | args                                       |
+------------+----------------+--------------------------------------------+
| app_server | aws_instance   | {                                          |
|            |                |     "ami": "ami-830c94e3",                 |
|            |                |     "tags": {                              |
|            |                |         "Name": "ExampleAppServerInstance" |
|            |                |     },                                     |
|            |                |     "instance_type": "t2.micro"            |
|            |                | }                                          |
| app_volume | aws_ebs_volume | {                                          |
|            |                |     "size": 40,                            |
|            |                |     "tags": {                              |
|            |                |         "Name": "HelloWorld"               |
|            |                |     },                                     |
|            |                |     "availability_zone": "us-west-2a"      |
|            |                | }                                          |
| app_bucket | aws_s3_bucket  | {                                          |
|            |                |     "acl": "private",                      |
|            |                |     "tags": {                              |
|            |                |         "Name": "Test bucket",             |
|            |                |         "Environment": "Dev"               |
|            |                |     },                                     |
|            |                |     "bucket": "my-app-bucket"              |
|            |                | }                                          |
+------------+----------------+--------------------------------------------+

Engines

This plugin is available for the following engines:

Engine Description
Steampipe The Steampipe CLI exposes APIs and services as a high-performance relational database, giving you the ability to write SQL-based queries to explore dynamic data. Mods extend Steampipe's capabilities with dashboards, reports, and controls built with simple HCL. The Steampipe CLI is a turnkey solution that includes its own Postgres database, plugin management, and mod support.
Postgres FDW Steampipe Postgres FDWs are native Postgres Foreign Data Wrappers that translate APIs to foreign tables. Unlike Steampipe CLI, which ships with its own Postgres server instance, the Steampipe Postgres FDWs can be installed in any supported Postgres database version.
SQLite Extension Steampipe SQLite Extensions provide SQLite virtual tables that translate your queries into API calls, transparently fetching information from your API or service as you request it.
Export Steampipe Plugin Exporters provide a flexible mechanism for exporting information from cloud services and APIs. Each exporter is a stand-alone binary that allows you to extract data using Steampipe plugins without a database.
Turbot Pipes Turbot Pipes is the only intelligence, automation & security platform built specifically for DevOps. Pipes provide hosted Steampipe database instances, shared dashboards, snapshots, and more.

Developing

Prerequisites:

Clone:

git clone https://github.com/turbot/steampipe-plugin-terraform.git
cd steampipe-plugin-terraform

Build, which automatically installs the new version to your ~/.steampipe/plugins directory:

make

Configure the plugin:

cp config/* ~/.steampipe/config
vi ~/.steampipe/config/terraform.spc

Try it!

steampipe query
> .inspect terraform

Further reading:

Open Source & Contributing

This repository is published under the Apache 2.0 (source code) and CC BY-NC-ND (docs) licenses. Please see our code of conduct. We look forward to collaborating with you!

Steampipe is a product produced from this open source software, exclusively by Turbot HQ, Inc. It is distributed under our commercial terms. Others are allowed to make their own distribution of the software, but cannot use any of the Turbot trademarks, cloud services, etc. You can learn more in our Open Source FAQ.

Get Involved

Join #steampipe on Slack →

Want to help but don't know where to start? Pick up one of the help wanted issues:

steampipe-plugin-terraform's People

Contributors

bigdatasourav avatar cbruno10 avatar dependabot[bot] avatar e-gineer avatar judell avatar madhushreeray30 avatar misraved avatar rollwagen avatar subhajit97 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

steampipe-plugin-terraform's Issues

Add table terraform_variable

References
Add any related links that will help us understand the resource, including vendor documentation, related GitHub issues, and Go SDK documentation.

Add support to parse Terraform state files

Is your feature request related to a problem? Please describe.
I'd like to query resources with states as close to the deployed resources

Describe the solution you'd like
Parsing state files to get resource information would be helpful

Describe alternatives you've considered
Look at plan files or base TF config files

Additional context
N/A

Add information about go-getter support to `config/terraform.spc` example config file

Is your feature request related to a problem? Please describe.
When viewing config/terraform.spc (or the copy that gets created locally when installing the plugin), it's not clear I can insert GitHub or S3 URLs.

Describe the solution you'd like
It should include a brief mention and link to the Hub site (https://hub.steampipe.io/plugins/turbot/terraform#supported-path-formats).

Describe alternatives you've considered
Viewing https://hub.steampipe.io/plugins/turbot/terraform#supported-path-formats.

Additional context
Add any other context or screenshots about the feature request here.

Add `attributes` column to `terraform_resource` table

Is your feature request related to a problem? Please describe.
When running checks in the Terraform AWS Compliance mod, if I have resources from TF config files, plan files, and state files, checks for TF config and plan files work OK since the controls check in arguments, but state files have resource information in instances -> attributes.

Describe the solution you'd like
We could add a new column attributes that merges results from the arguments and instances -> attributes columns. This would allow users and mods to use a common column to check resource state.

I'm not sure if arguments and attribute property names are always the same and if all arguments are exported as attributes (I think this is the case).

Also, I'm not sure if instances -> attributes contains only the arguments passed in the resource's arguments, or if it contains all attributes.

Describe alternatives you've considered
Checking both columns in all controls.

Additional context
Add any other context or screenshots about the feature request here.

"arguments" column is always null in terraform_module table

Describe the bug
The arguments column seems to be always null when querying a terraform module.

Steampipe version (steampipe -v)
v0.19.5

Plugin version (steampipe plugin list)

hub.steampipe.io/plugins/turbot/terraform@latest           | 0.5.0   | terraform

To reproduce

SELECT name, split_part(path, '/', -1), arguments FROM terraform_module ORDER BY name;

in a directory with your terraform code (assuming your paths are configured for that). For example, here's a snippet from my working directory:

+------------------------------------+------------------------------------+-----------+
| name                               | split_part                         | arguments |
+------------------------------------+------------------------------------+-----------+
| accelerate-demo                    | mongodbatlas--cluster.tf           | <null>    |
| ansible_playbook_bucket            | aws-schlage-wss.tf                 | <null>    |
| ansible_playbook_bucket_logs       | aws-schlage-wss.tf                 | <null>    |
| assets_auth_lambda_edge            | main.tf                            | <null>    |
| assets_auth_req_forwarder_lambda   | main.tf                            | <null>    |
| cdn                                | main.tf                            | <null>    |
| chained_descriptors                | descriptors.tf                     | <null>    |

This is what the assets_auth_lambda_edge module looks like in the terraform code:

module "assets_auth_lambda_edge" {
  source           = "../../../modules/lambda"
  name             = format("%s-%s", var.environment, "assets-auth-lambda-edge")
  s3_bucket_name   = var.aws_lambdas_s3_bucket_name
  s3_bucket_key    = format("%s/%s", var.environment, "assets-auth-lambda-edge.zip")
  publish          = true
  role_name_prefix = "assets-auth-lambda-edge-role-"
  owner            = var.owner
  project          = var.project
  environment      = var.environment
}

Expected behavior
The arguments column should contain JSONB data of the key => value mappings of argument keys and values, much like the terraform_resource table.

In the example above, the arguments column should look like

{"source": "../../../modules/lambda", "owner": "var.owner", }

etc.

Additional context
From my very quick and cursory glance at the source code, it seems Arguments is not defined in the terraformModule struct nor built up in the buildModule func, but I didn't dig in very deep.

Crash when using default connection config and running in a directory without `tfplan.json`

Describe the bug
I installed the Terraform plugin, went to a blank directory, and then ran select * from terraform_resource and received the error Error: failed to get directory specified by the source tfplan.json: relative paths require a module with a pwd (SQLSTATE HV000)

Steampipe version (steampipe -v)
v0.20.9

Plugin version (steampipe plugin list)
v0.8.0

To reproduce
See above

Expected behavior
No rows should be returned

Additional context
Add any other context about the problem here.

Querying the `terraform_local` table sometimes results in a conversion error

Describe the bug
When querying any column from terraform_local, sometimes a conversion error is returned:

Error: Failed to convert value map[_kics__default:{16 []} _kics_array:{21 [map[_kics__default:{21 []}] map[_kics__default:{21 []}] map[_kics__default:{0 [map[_kics__default:{21 []}] map[_kics__default:{0 [map[_kics__default:{21 []}]]}]]}]]} _kics_faz:{17 []} _kics_faz1:{18 []} _kics_myInt:{19 []} _kics_myNum:{20 []} _kics_simple_array:{22 [map[_kics__default:{22 []}]]}] due to unknown type: map[string]model.LineObject (SQLSTATE HV000)

Steampipe version (steampipe -v)
v0.11.2

Plugin version (steampipe plugin list)
v0.0.2

To reproduce
Create a TF file with at least one locals block and run select * from terraform_local (may need to run multiple times, as this only occurs sometimes).

Expected behavior
Queries should not return an error

Additional context
Add any other context about the problem here.

Can we discover TF files in the current working directory by default?

Terraform users are used to the idea of running TF commands in the current working directory for their files. It's inconvenient and unintuitive that we require the paths to be configured first. Can we discover files in the current working directory by default?

The obvious snag is operation when in service mode - we'd need to think about that a bit more carefully as part of this.

Table `terraform_resource` column `arguments` not showing field named `type`

Below results should have the type inside the arguments json

> select name,jsonb_pretty(arguments) as arguments from terraform_resource where name = 'aws_iam_regional_access_analyzer_source'
+-----------------------------------------+-------------------------------------------------------------------------------------------------------------->
| name                                    | arguments                                                                                                    >
+-----------------------------------------+-------------------------------------------------------------------------------------------------------------->
| aws_iam_regional_access_analyzer_source | {                                                                                                            >
|                                         |     "note": "AWS CIS v3.0.0 - Controls: 1.20",                                                               >
|                                         |     "value": "resource \"aws_accessanalyzer_analyzer\" \"cis_access_analyzer\" {\n  analyzer_name = \"access_>
|                                         |     "resource": "${turbot_smart_folder.aws_cis_v300_s1_iam.id}"                                              >
|                                         | }                                                                                                            >
+-----------------------------------------+-------------------------------------------------------------------------------------------------------------->
resource "turbot_policy_setting" "aws_iam_regional_access_analyzer_source" {
  resource = turbot_smart_folder.aws_cis_v300_s1_iam.id
  type     = "tmod:@turbot/aws#/policy/types/regionStackSource"
  note     = "AWS CIS v3.0.0 - Controls: 1.20"
  value    = <<-EOT
    resource "aws_accessanalyzer_analyzer" "cis_access_analyzer" {
      analyzer_name = "access_analyzer"
      type          = "ACCOUNT"
    }
    EOT
}

Add File Watcher support for `paths` argument in terraform config

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Allow authentication via GitHub PAT when when working with private repositories

Is your feature request related to a problem? Please describe.
The paths configuration can include GitHub repository URLs as described in the documentation. However, the URLs will not be accessible if the repositories are private due to the call to GitHub being unauthenticated.

Describe the solution you'd like
Implement the optional use of GitHub PAT tokens akin to how it is performed in the Steampipe GitHub plugin. Either an explicit github_token config declaration or read from ENV VARs is sufficient for this use-case.

Describe alternatives you've considered
Not sure if it is a good solution, but consider using the authenticated session provided by GitHub cli aka. gh to authenticate when using the plugin. Or integrate with Steampipe GitHub plugin, but this I bet is less appealing.

Additional context
I want to access organizational private repositories, and I am not sure if such repositories differ from normal private repositories.

Query/Control hangs when terraform_resource refers data from JSON file

Describe the bug
A clear and concise description of what the bug is.

This is in the case when the Terraform file is referring to a data source, e.g. defining the policies in the aws_iam_policy_document, and the same is referred to in provisioning the aws_ecr_repository_policy.

Please look at the Note section below for other observations.

Control hangs

➜ steampipe check control.ecr_repository_policy_prohibit_public_access
⠴ Running 1 control. (0 complete, 1 running, 0 pending, 0 errors)
⠙ Running 1 control. (0 complete, 1 running, 0 pending, 0 errors)
⠴ Running 1 control. (0 complete, 1 running, 0 pending, 0 errors)
⠴ Running 1 control. (0 complete, 1 running, 0 pending, 0 errors)
CTRL + C
+ ECR repository policy should prohibit public access .......... 1 / 1 [          ]
  
  ERROR: execution cancelled

Steampipe version (steampipe -v)
Example: Steampipe v0.20.10

Plugin version (steampipe plugin list)
Example: 0.7.0 | terraform

To reproduce
Steps to reproduce the behaviour (please include relevant code and/or commands).

Create a TF file with the below.

### This works as no data source is provided to associate the policy (inbuilt)

resource "aws_ecr_repository_policy" "foobarworks" {
  repository = "foobar"

  policy = jsonencode(
    {
      Version = "2008-10-17",
      Statement = [
        {
          Effect    = "Allow",
          Principal = "*",
          Action = [
            "ecr:BatchGetImage",
            "ecr:BatchCheckLayerAvailability",
            "ecr:DescribeImages",
            "ecr:DescribeRepositories",
            "ecr:GetDownloadUrlForLayer",
            "ecr:ListImages"
          ],
          Condition = {
            "StringEquals" = {
              "aws:PrincipalOrgID" = "something"
            }
          }
        }
      ]
    }
  )
}

### CHECKING POLICY AS DATA INPUT (Fails when referring to the data source)
resource "aws_ecr_repository" "foo" {
  name = "bar"
}

data "aws_iam_policy_document" "foopolicy" {
  statement {
    sid    = "new policy"
    effect = "Allow"

    principals {
      type        = "AWS"
      identifiers = ["123456789012"]
    }

    actions = [
      "ecr:GetDownloadUrlForLayer",
      "ecr:BatchGetImage",
      "ecr:BatchCheckLayerAvailability",
      "ecr:PutImage",
      "ecr:InitiateLayerUpload",
      "ecr:UploadLayerPart",
      "ecr:CompleteLayerUpload",
      "ecr:DescribeRepositories",
      "ecr:GetRepositoryPolicy",
      "ecr:ListImages",
      "ecr:DeleteRepository",
      "ecr:BatchDeleteImage",
      "ecr:SetRepositoryPolicy",
      "ecr:DeleteRepositoryPolicy",
    ]
  }
}

resource "aws_ecr_repository_policy" "foopolicy" {
  repository = aws_ecr_repository.foo.name
  policy     = data.aws_iam_policy_document.foopolicy.json
}

The query to execute

with policy_statement as (
select
  distinct (type || ' ' || name ) as name
from
  terraform_resource ,
  jsonb_array_elements(
    case when ((arguments ->> 'policy') = '')
      then null
      else ((arguments ->> 'policy')::jsonb -> 'Statement') end
) as s
where
  type = 'aws_ecr_repository_policy'
  and (
    (s ->> 'Principal' = '*')
    and ((s ->> 'Condition') is null)
   )
)
select
  type || ' ' || r.name as resource,
  case
    when (arguments ->> 'policy') = ''  then 'ok'
    when s.name is null then 'ok'
    else 'alarm'
  end status,
  case
     when (arguments ->> 'policy') = '' then ' no policy defined'
    when s.name is null then ' not public'
    else ' public'
  end || '.' reason
from
  terraform_resource as r
  left join policy_statement as s on s.name = concat(r.type || ' ' || r.name)
where
  type = 'aws_ecr_repository_policy'

Note-

  1. Interesting thing is when I execute .cache off & .cache clear, the query renders as expected
  2. For the first time, the query works (not the control), but when we re-run it, it hangs.
  3. Now same way if the control is hanging for this case, it halts the execution of entire controls across the services
  4. Control & query with CTE block mostly hangs
  5. It may be very much related to cache, as it works when we execute the query with .cache off. The moment the cache is on it hangs

Error from plugin log

2023-08-22 10:39:38.535 UTC [TRACE] steampipe-plugin-terraform.plugin: [TRACE] 1692700778380: IndexItem) SatisfiesRequest: satisfiedColumns true satisfiesLimit true satisfiesQuals true
2023-08-22 10:39:38.535 UTC [TRACE] steampipe-plugin-terraform.plugin: [TRACE] 1692700778380: found pending index item to satisfy columns for_each,depends_on,lifecycle,start_line,source,arguments,count,count_src,path,end_line,name,type,provider,_ctx, limit -1, quals: NONE (terraform-1692700778380)
2023-08-22 10:39:38.535 UTC [TRACE] steampipe-plugin-terraform.plugin: [TRACE] 1692700778380: getPendingResultItem returning &{0xc0006de8a0 <nil> terraform-1692700646836 0xc015043100}
2023-08-22 10:39:38.535 UTC [INFO]  steampipe-plugin-terraform.plugin: [INFO]  1692700778380: found pending item [terraform-1692700646836] - subscribing to its data (terraform-1692700778380)
2023-08-22 10:39:38.535 UTC [INFO]  steampipe-plugin-terraform.plugin: [INFO]  1692700778380: stream all data already cached

Expected behaviour
A clear and concise description of what you expected to happen.

Additional context
Add any other context about the problem here.

Add support for parsing Terraform plans and pull them into existing tables

Currently, the Terraform plugin only scans .tf files and pulls the data into the table format. But the parser can also parse the TF plan files, not just .tf files.

Describe the solution you'd like
Update the plugin to support Terraform plan parsing and pulling the data into the existing tables.

support azurerm backend storage for tfstate files

A number of the terraform stacks I work with are configured with azurerm backend values, which doesn't currently appear to be supported, based on documentation and examples.

I'd like to request support for azurerm-backed terraform be built in; there's already support for s3 at this point, so this seems like a logical step, and equivalent functionality would be a boon for those of us working in azure storage accounts & containers for tfstate management.

Thanks.

Plugin crashes with Error: rpc error: code = Unavailable desc = error reading from server: EOF (SQLSTATE HV000)

Describe the bug
A clear and concise description of what the bug is.
Continuous execution of any query on any TF plugin tables crashes
Error: rpc error: code = Unavailable desc = error reading from server: EOF (SQLSTATE HV000)

Steampipe version (steampipe -v)
Example: v0.12.2

Plugin version (steampipe plugin list)
Example: v0.0.3

To reproduce
Steps to reproduce the behavior (please include relevant code and/or commands).

Run multiple times the below query
> select * from terraform_resource
Error: rpc error: code = Unavailable desc = error reading from server: EOF (SQLSTATE HV000)
Expected behavior
A clear and concise description of what you expected to happen.

Additional context
Add any other context about the problem here.

runtime error: invalid memory address when plan.json exists

Given this TF:

variable "allowed_ssh_cidrs" {
  description = "List of CIDRs that can SSH into instances"
  type        = list(string)
  default     = ["10.0.0.1/32"]
}

resource "aws_security_group" "example_sg" {
  name        = "example_sg"
  description = "Example Security Group"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = var.allowed_ssh_cidrs
  }
}
> select path from terraform_resource
+----------------------------------------------------+
| path                                               |
+----------------------------------------------------+
| /home/jon/terraform-examples/dynamic.tf |
+----------------------------------------------------+

Now:

terraform plan -var 'allowed_ssh_cidrs=["0.0.0.0/0"]' -out=plan.tfplan
terraform show -json plan.tfplan > plan.json
> select path from terraform_resource

Error: runtime error: invalid memory address or nil pointer dereference (SQLSTATE HV000)

+----------------------------------------------------+
| path                                               |
+----------------------------------------------------+
| /home/jon/terraform-examples/dynamic.tf |
+----------------------------------------------------+

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.