From f281df6605e51f60ece5d85b36cbd211d058f8fc Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Mon, 21 Jan 2019 03:13:48 -0800 Subject: [PATCH] Initial implementation (#1) * Initial implementation --- .editorconfig | 8 ++ .gitignore | 22 +++ .travis.yml | 32 +++++ LICENSE | 202 +++++++++++++++++++++++++++ Makefile | 9 ++ README.md | 368 +++++++++++++++++++++++++++++++++++++++++++++++++- README.yaml | 204 ++++++++++++++++++++++++++++ glide.yaml | 0 main.go | 65 +++++++++ 9 files changed, 908 insertions(+), 2 deletions(-) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.yaml create mode 100644 glide.yaml create mode 100644 main.go diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..7928ca4 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,8 @@ +# Override for Makefile +[{Makefile, makefile, GNUmakefile}] +indent_style = tab +indent_size = 4 + +[Makefile.*] +indent_style = tab +indent_size = 4 \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9c4be69 --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +.idea +*.iml +dist/bin/* +release/* +github-status-updater +.build-harness +build-harness + +# Binaries for programs and plugins +*.exe +*.dll +*.so +*.dylib + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Project-local glide cache, RE: /~https://github.com/Masterminds/glide/issues/736 +.glide/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4a8847d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,32 @@ +sudo: required +language: go +go: + - 1.11.x +addons: + apt: + packages: + - git + - make + - curl + +install: +- make init +- make go/deps-build +- make go/deps-dev + +script: +- make go/deps +- make go/test +- make go/lint +- make go/build-all +- ls -l release/ + +deploy: + provider: releases + api_key: "$GITHUB_API_KEY" + file_glob: true + file: "release/*" + skip_cleanup: true + on: + tags: true + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d179a54 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2018 Cloud Posse, LLC + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3005441 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +SHELL = /bin/bash + +PATH:=$(PATH):$(GOPATH)/bin + +-include $(shell curl -sSL -o .build-harness "https://git.io/build-harness"; echo .build-harness) + +build: go/build + @exit 0 + diff --git a/README.md b/README.md index de32667..395c02a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,366 @@ -# tfmask -Terraform utility to mask select output from `terraform plan` + +[![README Header][readme_header_img]][readme_header_link] + +[![Cloud Posse][logo]](https://cpco.io/homepage) + +# tfmask [![Build Status](https://travis-ci.org/cloudposse/tfmask.svg?branch=master)](https://travis-ci.org/cloudposse/tfmask) [![Latest Release](https://img.shields.io/github/release/cloudposse/tfmask.svg)](/~https://github.com/cloudposse/tfmask/releases/latest) [![Slack Community](https://slack.cloudposse.com/badge.svg)](https://slack.cloudposse.com) + + +Command line utility to mask sensitive output from a `transform plan` or `terraform apply`. + + +--- + +This project is part of our comprehensive ["SweetOps"](https://cpco.io/sweetops) approach towards DevOps. +[][share_email] +[][share_googleplus] +[][share_facebook] +[][share_reddit] +[][share_linkedin] +[][share_twitter] + + + + +It's 100% Open Source and licensed under the [APACHE2](LICENSE). + + + + + + + + + + + + +## Introduction + + +If you answer "yes" to any of these questions, then look no further! + +* Have you ever wished you could easily filter sensitive output from a `terraform plan` or `terraform apply`? +* Do you use terraform providers that leak sensitive data to `stdout` (e.g. `terraform-github-provider`)? + +**Yes?** Great! Then this utility is for you. + +The `tfmask` utility will replace the "old value" and the "new value" with the masking character (e.g. `*`). + +__NOTE__: `tfmask` will preserve the name of the nodes in the graph + +## Usage + + +__NOTE__: The utility supports a number of configuration settings which can be passed via environment variables. + +| Environment Variable | Description | Default | +|----------------------|----------------------------------------------|------------| +| `TFMASK_CHAR` | Character used to mask all output | `*` | +| `TFMASK_REGEX` | Regular expression used to match graph nodes | [see code] | + +The basic usage looks like this. We're going to run `terraform plan` and filter it through `tfmask`: + +```sh +terraform plan | tfmask +``` + +### Direnv + +You can use `tfmask` with [`direnv`](https://direnv.net/) to set the defaults. + +Example `.envrc`: + +```sh +# Export terraform environment +export TFMASK_CHAR="#" +export TFMASK_REGEX="(?i)^.*(secret|password|oauth|token|key).*$" +``` + +
+ Example of Masked Output + + ```sh + Terraform will perform the following actions: + + ~ module.atlantis.module.web_app.module.ecs_codepipeline.aws_codepipeline.source_build_deploy + stage.0.action.0.configuration.%: "4" => "5" + stage.0.action.0.configuration.OAuthToken: "" => "*******************************************" + + Plan: 0 to add, 1 to change, 0 to destroy. + ``` +
+ + + + +## Examples + + +### Compiling the Binary + +```sh +make go/build +``` + + ### Use with Terraform Plan + + Many terraform providers unintentionally leak sensitive information when running `terraform plan`. **This is very bad.** + +
+ Example of a Leaked Secret from a Terraform Plan + + ```sh + An execution plan has been generated and is shown below. + Resource actions are indicated with the following symbols: + ~ update in-place + + Terraform will perform the following actions: + + ~ module.example.aws_codepipeline.source_build_deploy + stage.0.action.0.configuration.%: "4" => "5" + stage.0.action.0.configuration.OAuthToken: "" => "efba05dbe9b94ba18ae3737a6d6de16eefba05dbe9b9" + Plan: 0 to add, 1 to change, 0 to destroy. + ``` + __NOTE:__ This `OAuthToken` is just an example and not a valid token. +
+ + +Using `tfmask`, the output from `terraform plan` will be masked like this: + +```sh +terraform plan | tfmask +``` + +
+ Example of Masked Terraform Plan Output + + ``` + An execution plan has been generated and is shown below. + Resource actions are indicated with the following symbols: + ~ update in-place + + Terraform will perform the following actions: + + ~ module.example.aws_codepipeline.source_build_deploy + stage.0.action.0.configuration.%: "4" => "5" + stage.0.action.0.configuration.OAuthToken: "" => ********************************************" + Plan: 0 to add, 1 to change, 0 to destroy. + ``` +
+ +### Use with Terraform Apply + +Many terraform providers unintentionally leak sensitive information when running `terraform apply`. **This is very bad.** + +
+ Example of a Leaked Secret from a Terraform Apply + + ```sh + terraform apply + module.example.aws_codepipeline.source_build_deploy: Modifying... (ID: example-codepipeline) + stage.0.action.0.configuration.%: "4" => "5" + stage.0.action.0.configuration.OAuthToken: "" => "efba05dbe9b94ba18ae3737a6d6de16eefba05dbe9b9" + module.example.aws_codepipeline.source_build_deploy: Modifications complete after 1s (ID: example-codepipeline) + + Apply complete! Resources: 0 added, 1 changed, 0 destroyed. + ``` + + __NOTE:__ This `OAuthToken` is just an example and not a valid token. +
+ +Using `tfmask`, the output from `terraform apply` will be masked like this: + +```sh +terraform apply | tfmask +``` +
+ Example of Masked Terraform Apply Output + + ```sh + module.example.aws_codepipeline.source_build_deploy: Modifying... (ID: example-codepipeline) + stage.0.action.0.configuration.%: "4" => "5" + stage.0.action.0.configuration.OAuthToken: "" => "********************************************" + module.example.aws_codepipeline.source_build_deploy: Modifications complete after 1s (ID: example-codepipeline) + + Apply complete! Resources: 0 added, 1 changed, 0 destroyed. + ``` +
+ + + + + +## Share the Love + +Like this project? Please give it a ★ on [our GitHub](/~https://github.com/cloudposse/tfmask)! (it helps us **a lot**) + +Are you using this project or any of our other projects? Consider [leaving a testimonial][testimonial]. =) + + +## Related Projects + +Check out these related projects. + +- [Packages](/~https://github.com/cloudposse/packages) - Cloud Posse installer and distribution of native apps +- [build-harness](/~https://github.com/cloudposse/build-harness) - Collection of Makefiles to facilitate building Golang projects, Dockerfiles, Helm charts, and more +- [geodesic](/~https://github.com/cloudposse/geodesic) - Geodesic is the fastest way to get up and running with a rock solid, production grade cloud platform built on strictly Open Source tools. +- [direnv](https://direnv.net/) - Unclutter your .profile with an environment switcher for the shell +- [tfenv](/~https://github.com/cloudposse/tfenv) - Transform environment variables for use with Terraform (e.g. `HOSTNAME` ⇨ `TF_VAR_hostname`) + + + +## Help + +**Got a question?** + +File a GitHub [issue](/~https://github.com/cloudposse/tfmask/issues), send us an [email][email] or join our [Slack Community][slack]. + +[![README Commercial Support][readme_commercial_support_img]][readme_commercial_support_link] + +## Commercial Support + +Work directly with our team of DevOps experts via email, slack, and video conferencing. + +We provide [*commercial support*][commercial_support] for all of our [Open Source][github] projects. As a *Dedicated Support* customer, you have access to our team of subject matter experts at a fraction of the cost of a full-time engineer. + +[![E-Mail](https://img.shields.io/badge/email-hello@cloudposse.com-blue.svg)][email] + +- **Questions.** We'll use a Shared Slack channel between your team and ours. +- **Troubleshooting.** We'll help you triage why things aren't working. +- **Code Reviews.** We'll review your Pull Requests and provide constructive feedback. +- **Bug Fixes.** We'll rapidly work to fix any bugs in our projects. +- **Build New Terraform Modules.** We'll [develop original modules][module_development] to provision infrastructure. +- **Cloud Architecture.** We'll assist with your cloud strategy and design. +- **Implementation.** We'll provide hands-on support to implement our reference architectures. + + + + +## Slack Community + +Join our [Open Source Community][slack] on Slack. It's **FREE** for everyone! Our "SweetOps" community is where you get to talk with others who share a similar vision for how to rollout and manage infrastructure. This is the best place to talk shop, ask questions, solicit feedback, and work together as a community to build totally *sweet* infrastructure. + +## Newsletter + +Signup for [our newsletter][newsletter] that covers everything on our technology radar. Receive updates on what we're up to on GitHub as well as awesome new projects we discover. + +## Contributing + +### Bug Reports & Feature Requests + +Please use the [issue tracker](/~https://github.com/cloudposse/tfmask/issues) to report any bugs or file feature requests. + +### Developing + +If you are interested in being a contributor and want to get involved in developing this project or [help out](https://cpco.io/help-out) with our other projects, we would love to hear from you! Shoot us an [email][email]. + +In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow. + + 1. **Fork** the repo on GitHub + 2. **Clone** the project to your own machine + 3. **Commit** changes to your own branch + 4. **Push** your work back up to your fork + 5. Submit a **Pull Request** so that we can review your changes + +**NOTE:** Be sure to merge the latest changes from "upstream" before making a pull request! + + +## Copyright + +Copyright © 2017-2019 [Cloud Posse, LLC](https://cpco.io/copyright) + + + +## License + +[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) + +See [LICENSE](LICENSE) for full details. + + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + + + + + + + + + +## Trademarks + +All other trademarks referenced herein are the property of their respective owners. + +## About + +This project is maintained and funded by [Cloud Posse, LLC][website]. Like it? Please let us know by [leaving a testimonial][testimonial]! + +[![Cloud Posse][logo]][website] + +We're a [DevOps Professional Services][hire] company based in Los Angeles, CA. We ❤️ [Open Source Software][we_love_open_source]. + +We offer [paid support][commercial_support] on all of our projects. + +Check out [our other projects][github], [follow us on twitter][twitter], [apply for a job][jobs], or [hire us][hire] to help with your cloud strategy and implementation. + + + +### Contributors + +| [![Erik Osterman][osterman_avatar]][osterman_homepage]
[Erik Osterman][osterman_homepage] | +|---| + + + [osterman_homepage]: /~https://github.com/osterman + [osterman_avatar]: /~https://github.com/osterman.png?size=150 + + + +[![README Footer][readme_footer_img]][readme_footer_link] +[![Beacon][beacon]][website] + + [logo]: https://cloudposse.com/logo-300x69.svg + [docs]: https://cpco.io/docs + [website]: https://cpco.io/homepage + [github]: https://cpco.io/github + [jobs]: https://cpco.io/jobs + [hire]: https://cpco.io/hire + [slack]: https://cpco.io/slack + [linkedin]: https://cpco.io/linkedin + [twitter]: https://cpco.io/twitter + [testimonial]: https://cpco.io/leave-testimonial + [newsletter]: https://cpco.io/newsletter + [email]: https://cpco.io/email + [commercial_support]: https://cpco.io/commercial-support + [we_love_open_source]: https://cpco.io/we-love-open-source + [module_development]: https://cpco.io/module-development + [terraform_modules]: https://cpco.io/terraform-modules + [readme_header_img]: https://cloudposse.com/readme/header/img?repo=cloudposse/tfmask + [readme_header_link]: https://cloudposse.com/readme/header/link?repo=cloudposse/tfmask + [readme_footer_img]: https://cloudposse.com/readme/footer/img?repo=cloudposse/tfmask + [readme_footer_link]: https://cloudposse.com/readme/footer/link?repo=cloudposse/tfmask + [readme_commercial_support_img]: https://cloudposse.com/readme/commercial-support/img?repo=cloudposse/tfmask + [readme_commercial_support_link]: https://cloudposse.com/readme/commercial-support/link?repo=cloudposse/tfmask + [share_twitter]: https://twitter.com/intent/tweet/?text=tfmask&url=/~https://github.com/cloudposse/tfmask + [share_linkedin]: https://www.linkedin.com/shareArticle?mini=true&title=tfmask&url=/~https://github.com/cloudposse/tfmask + [share_reddit]: https://reddit.com/submit/?url=/~https://github.com/cloudposse/tfmask + [share_facebook]: https://facebook.com/sharer/sharer.php?u=/~https://github.com/cloudposse/tfmask + [share_googleplus]: https://plus.google.com/share?url=/~https://github.com/cloudposse/tfmask + [share_email]: mailto:?subject=tfmask&body=/~https://github.com/cloudposse/tfmask + [beacon]: https://ga-beacon.cloudposse.com/UA-76589703-4/cloudposse/tfmask?pixel&cs=github&cm=readme&an=tfmask diff --git a/README.yaml b/README.yaml new file mode 100644 index 0000000..873da81 --- /dev/null +++ b/README.yaml @@ -0,0 +1,204 @@ +--- +# +# This is the canonical configuration for the `README.md` +# Run `make readme` to rebuild the `README.md` +# + +# Name of this project +name: tfmask + +# Logo for this project +#logo: docs/logo.png + +# License of this project +license: "APACHE2" + +# Canonical GitHub repo +github_repo: cloudposse/tfmask + +# Badges to display +badges: + - name: "Build Status" + image: "https://travis-ci.org/cloudposse/tfmask.svg?branch=master" + url: "https://travis-ci.org/cloudposse/tfmask" + - name: "Latest Release" + image: "https://img.shields.io/github/release/cloudposse/tfmask.svg" + url: "/~https://github.com/cloudposse/tfmask/releases/latest" + - name: "Slack Community" + image: "https://slack.cloudposse.com/badge.svg" + url: "https://slack.cloudposse.com" + +# Short description of this project +description: |- + Command line utility to mask sensitive output from a `transform plan` or `terraform apply`. + +introduction: |- + + If you answer "yes" to any of these questions, then look no further! + + * Have you ever wished you could easily filter sensitive output from a `terraform plan` or `terraform apply`? + * Do you use terraform providers that leak sensitive data to `stdout` (e.g. `terraform-github-provider`)? + + **Yes?** Great! Then this utility is for you. + + The `tfmask` utility will replace the "old value" and the "new value" with the masking character (e.g. `*`). + + __NOTE__: `tfmask` will preserve the name of the nodes in the graph + +# How to use this project +usage: |- + + __NOTE__: The utility supports a number of configuration settings which can be passed via environment variables. + + | Environment Variable | Description | Default | + |----------------------|----------------------------------------------|------------| + | `TFMASK_CHAR` | Character used to mask all output | `*` | + | `TFMASK_REGEX` | Regular expression used to match graph nodes | [see code] | + + The basic usage looks like this. We're going to run `terraform plan` and filter it through `tfmask`: + + ```sh + terraform plan | tfmask + ``` + + ### Direnv + + You can use `tfmask` with [`direnv`](https://direnv.net/) to set the defaults. + + Example `.envrc`: + + ```sh + # Export terraform environment + export TFMASK_CHAR="#" + export TFMASK_REGEX="(?i)^.*(secret|password|oauth|token|key).*$" + ``` + +
+ Example of Masked Output + + ```sh + Terraform will perform the following actions: + + ~ module.atlantis.module.web_app.module.ecs_codepipeline.aws_codepipeline.source_build_deploy + stage.0.action.0.configuration.%: "4" => "5" + stage.0.action.0.configuration.OAuthToken: "" => "*******************************************" + + Plan: 0 to add, 1 to change, 0 to destroy. + ``` +
+ + +related: + - name: "Packages" + description: "Cloud Posse installer and distribution of native apps" + url: "/~https://github.com/cloudposse/packages" + - name: "build-harness" + description: "Collection of Makefiles to facilitate building Golang projects, Dockerfiles, Helm charts, and more" + url: "/~https://github.com/cloudposse/build-harness" + - name: "geodesic" + description: "Geodesic is the fastest way to get up and running with a rock solid, production grade cloud platform built on strictly Open Source tools." + url: "/~https://github.com/cloudposse/geodesic" + - name: "direnv" + description: "Unclutter your .profile with an environment switcher for the shell" + url: "https://direnv.net/" + - name: "tfenv" + description: "Transform environment variables for use with Terraform (e.g. `HOSTNAME` ⇨ `TF_VAR_hostname`)" + url: "/~https://github.com/cloudposse/tfenv" + +examples: |- + + ### Compiling the Binary + + ```sh + make go/build + ``` + + ### Use with Terraform Plan + + Many terraform providers unintentionally leak sensitive information when running `terraform plan`. **This is very bad.** + +
+ Example of a Leaked Secret from a Terraform Plan + + ```sh + An execution plan has been generated and is shown below. + Resource actions are indicated with the following symbols: + ~ update in-place + + Terraform will perform the following actions: + + ~ module.example.aws_codepipeline.source_build_deploy + stage.0.action.0.configuration.%: "4" => "5" + stage.0.action.0.configuration.OAuthToken: "" => "efba05dbe9b94ba18ae3737a6d6de16eefba05dbe9b9" + Plan: 0 to add, 1 to change, 0 to destroy. + ``` + __NOTE:__ This `OAuthToken` is just an example and not a valid token. +
+ + + Using `tfmask`, the output from `terraform plan` will be masked like this: + + ```sh + terraform plan | tfmask + ``` + +
+ Example of Masked Terraform Plan Output + + ``` + An execution plan has been generated and is shown below. + Resource actions are indicated with the following symbols: + ~ update in-place + + Terraform will perform the following actions: + + ~ module.example.aws_codepipeline.source_build_deploy + stage.0.action.0.configuration.%: "4" => "5" + stage.0.action.0.configuration.OAuthToken: "" => ********************************************" + Plan: 0 to add, 1 to change, 0 to destroy. + ``` +
+ + ### Use with Terraform Apply + + Many terraform providers unintentionally leak sensitive information when running `terraform apply`. **This is very bad.** + +
+ Example of a Leaked Secret from a Terraform Apply + + ```sh + terraform apply + module.example.aws_codepipeline.source_build_deploy: Modifying... (ID: example-codepipeline) + stage.0.action.0.configuration.%: "4" => "5" + stage.0.action.0.configuration.OAuthToken: "" => "efba05dbe9b94ba18ae3737a6d6de16eefba05dbe9b9" + module.example.aws_codepipeline.source_build_deploy: Modifications complete after 1s (ID: example-codepipeline) + + Apply complete! Resources: 0 added, 1 changed, 0 destroyed. + ``` + + __NOTE:__ This `OAuthToken` is just an example and not a valid token. +
+ + Using `tfmask`, the output from `terraform apply` will be masked like this: + + ```sh + terraform apply | tfmask + ``` +
+ Example of Masked Terraform Apply Output + + ```sh + module.example.aws_codepipeline.source_build_deploy: Modifying... (ID: example-codepipeline) + stage.0.action.0.configuration.%: "4" => "5" + stage.0.action.0.configuration.OAuthToken: "" => "********************************************" + module.example.aws_codepipeline.source_build_deploy: Modifications complete after 1s (ID: example-codepipeline) + + Apply complete! Resources: 0 added, 1 changed, 0 destroyed. + ``` +
+ +# Contributors to this project +contributors: + - name: "Erik Osterman" + homepage: "/~https://github.com/osterman" + github: "osterman" diff --git a/glide.yaml b/glide.yaml new file mode 100644 index 0000000..e69de29 diff --git a/main.go b/main.go new file mode 100644 index 0000000..7dff9f8 --- /dev/null +++ b/main.go @@ -0,0 +1,65 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "regexp" + "runtime" + "strings" + "unicode/utf8" +) + +func init() { + // make sure we only have one process and that it runs on the main thread + // (so that ideally, when we Exec, we keep our user switches and stuff) + runtime.GOMAXPROCS(1) + runtime.LockOSThread() +} + +func getEnv(key, fallback string) string { + if value, ok := os.LookupEnv(key); ok { + return value + } + return fallback +} + +func main() { + log.SetFlags(0) // no timestamps on our logs + + // Character used to mask sensitive output + var tfmaskChar = getEnv("TFMASK_CHAR", "*") + + // Pattern representing sensitive output + var tfmaskRegex = getEnv("TFMASK_REGEX", "(?i)^.*(oauth|secret|token|password|key).*$") + + // stage.0.action.0.configuration.OAuthToken: "" => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + reTfPlanLine := regexp.MustCompile("^( +)([a-zA-Z0-9%._-]+):( +)\"(.*?)\" +=> +\"(.*?)\"") + reTfSensitive := regexp.MustCompile(tfmaskRegex) + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + line := scanner.Text() + if reTfPlanLine.MatchString(line) { + match := reTfPlanLine.FindStringSubmatch(line) + leadingWhitespace := match[1] + property := match[2] + trailingWhitespace := match[3] + + if reTfSensitive.MatchString(property) { + oldValue := strings.Repeat(tfmaskChar, utf8.RuneCountInString(match[4])) + newValue := strings.Repeat(tfmaskChar, utf8.RuneCountInString(match[5])) + fmt.Printf("%v%v:%v\"%v\" => \"%v\"\n", leadingWhitespace, property, trailingWhitespace, oldValue, newValue) + } else { + fmt.Println(line) + } + } else { + fmt.Println(line) + } + } + + if err := scanner.Err(); err != nil { + fmt.Fprintln(os.Stderr, "error:", err) + os.Exit(1) + } +} \ No newline at end of file