Skip to content

Commit eb5be1b

Browse files
bradeglerverbanicm
authored andcommitted
updates from review comments
1 parent af7bc94 commit eb5be1b

File tree

16 files changed

+304
-266
lines changed

16 files changed

+304
-266
lines changed

.github/actions/secure-setup-terraform/action.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ runs:
3838
tar xf secure-setup-terraform_${{env.release_version}}_linux_amd64.tar.gz
3939
4040
# Recursively search for terraform files in the current repo and run a linter that fails when it finds calls to 'local-exec'
41-
- name: 'lint-local-exec'
41+
- name: 'lint-terraform'
4242
shell: 'bash'
43-
run: ./lint-local-exec ./
43+
run: ./lint-terraform ./
4444

4545
# Search the .github/workflows for this project and run a linter that fails if it finds a direct call to the 'hashicorp/setup-terraform' action
46-
- name: 'lint-setup-terraform'
46+
- name: 'lint-action'
4747
shell: 'bash'
48-
run: ./lint-setup-terraform ./.github/workflows
48+
run: ./lint-action ./.github/workflows
4949

5050
- name: 'setup-terraform'
5151
uses: 'hashicorp/setup-terraform@v2'

.goreleaser.yaml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ before:
2727

2828
builds:
2929
-
30-
id: 'lint-local-exec'
31-
main: './cmd/lint-local-exec'
32-
binary: 'lint-local-exec'
30+
id: 'lint-terraform'
31+
main: './cmd/lint-terraform'
32+
binary: 'lint-terraform'
3333
mod_timestamp: '{{ .CommitTimestamp }}'
3434
flags:
3535
- '-a'
3636
- '-trimpath'
3737
ldflags:
3838
- '-s'
3939
- '-w'
40-
- '-X={{ .ModulePath }}/cmd/lint-local-exec/version.Name=lint-local-exec'
41-
- '-X={{ .ModulePath }}/cmd/lint-local-exec/version.Version={{ .Version }}'
42-
- '-X={{ .ModulePath }}/cmd/lint-local-exec/version.Commit={{ .Commit }}'
40+
- '-X={{ .ModulePath }}/pkg/internal/version.Name=lint-terraform'
41+
- '-X={{ .ModulePath }}/pkg/internal/version.Version={{ .Version }}'
42+
- '-X={{ .ModulePath }}/pkg/internal/version.Commit={{ .Commit }}'
4343
- '-extldflags=-static'
4444
goos:
4545
- 'darwin'
@@ -48,19 +48,19 @@ builds:
4848
- 'amd64'
4949
- 'arm64'
5050
-
51-
id: 'lint-setup-terraform'
52-
main: './cmd/lint-setup-terraform'
53-
binary: 'lint-setup-terraform'
51+
id: 'lint-action'
52+
main: './cmd/lint-action'
53+
binary: 'lint-action'
5454
mod_timestamp: '{{ .CommitTimestamp }}'
5555
flags:
5656
- '-a'
5757
- '-trimpath'
5858
ldflags:
5959
- '-s'
6060
- '-w'
61-
- '-X={{ .ModulePath }}/cmd/lint-setup-terraform/version.Name=lint-setup-terraform'
62-
- '-X={{ .ModulePath }}/cmd/lint-setup-terraform/version.Version={{ .Version }}'
63-
- '-X={{ .ModulePath }}/cmd/lint-setup-terraform/version.Commit={{ .Commit }}'
61+
- '-X={{ .ModulePath }}/pkg/internal/version.Name=lint-action'
62+
- '-X={{ .ModulePath }}/pkg/internal/version.Version={{ .Version }}'
63+
- '-X={{ .ModulePath }}/pkg/internal/version.Commit={{ .Commit }}'
6464
- '-extldflags=-static'
6565
goos:
6666
- 'darwin'

Dockerfile

Lines changed: 0 additions & 29 deletions
This file was deleted.

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Secure Setup Terraform
22

3-
This repository contains a composite GitHub action + two linters that are built to meet the requirements set out to lightly secure the usage of Hashicorp's Terraform product from a GitHub action.
3+
This repository contains a composite GitHub Action and two linters that are built to meet the requirements set out to lightly secure the usage of HashiCorp's Terraform product from a GitHub Action.
44

55
## Linters
66

@@ -40,11 +40,12 @@ jobs:
4040
```
4141

4242
## Building the linters
43+
4344
```sh
4445
# Linter to find calls to the 'local-exec' terraform provider
45-
go build -o lint-local-exec ./cmd/lint-local-exec
46+
go build ./cmd/lint-local-exec
4647

4748
# Linter to find calls to the 'setup-terraform' GitHub
4849
# action from Hashicopr
49-
go build -o lint-setup-terraform ./cmd/lint-setup-terraform
50+
go build ./cmd/lint-setup-terraform
5051
```

cmd/lint-setup-terraform/linter/lint.go renamed to cmd/lint-action/linter/lint.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ import (
2020
"io"
2121
"strings"
2222

23-
"github.com/bradegler/secure-setup-terraform/cmd/lint-setup-terraform/version"
2423
"github.com/bradegler/secure-setup-terraform/pkg/lint"
2524
"gopkg.in/yaml.v3"
2625
)
2726

28-
const violationType = "setup-terraform"
27+
const tokenSetupTerraform = "setup-terraform"
2928

3029
var selectors []string = []string{".yml", ".yaml"}
3130

@@ -34,7 +33,7 @@ type GitHubActionLinter struct{}
3433
// FindViolations inspects a set of bytes that represent a YAML document that defines
3534
// a GitHub action workflow looking for steps that use the 'hashicorp/setup-terraform'
3635
// action.
37-
func (tfl *GitHubActionLinter) FindViolations(content []byte, path string) ([]lint.ViolationInstance, error) {
36+
func (tfl *GitHubActionLinter) FindViolations(content []byte, path string) ([]*lint.ViolationInstance, error) {
3837
reader := bytes.NewReader(content)
3938
node, err := parseYAML(reader)
4039
if err != nil {
@@ -46,7 +45,8 @@ func (tfl *GitHubActionLinter) FindViolations(content []byte, path string) ([]li
4645
if node.Kind != yaml.DocumentNode {
4746
return nil, fmt.Errorf("expected document node, got %v", node.Kind)
4847
}
49-
violations := []lint.ViolationInstance{}
48+
49+
var violations []*lint.ViolationInstance
5050
// Top-level object map
5151
for _, docMap := range node.Content {
5252
_ = docMap
@@ -85,7 +85,7 @@ func (tfl *GitHubActionLinter) FindViolations(content []byte, path string) ([]li
8585
uses := step.Content[k+1]
8686
// Looking for the specific 'hashicorp/setup-terraform' action
8787
if strings.HasPrefix(uses.Value, "hashicorp/setup-terraform") {
88-
violations = append(violations, lint.ViolationInstance{Path: path, Line: uses.Line})
88+
violations = append(violations, &lint.ViolationInstance{ViolationType: tokenSetupTerraform, Path: path, Line: uses.Line})
8989
}
9090
}
9191
}
@@ -101,9 +101,7 @@ func (tfl *GitHubActionLinter) FindViolations(content []byte, path string) ([]li
101101
return violations, nil
102102
}
103103

104-
func (tfl *GitHubActionLinter) Selectors() []string { return selectors }
105-
func (tfl *GitHubActionLinter) ViolationType() string { return violationType }
106-
func (tfl *GitHubActionLinter) Version() string { return version.HumanVersion }
104+
func (tfl *GitHubActionLinter) Selectors() []string { return selectors }
107105

108106
// parseYAML parses the given reader as a yaml node.
109107
func parseYAML(r io.Reader) (*yaml.Node, error) {

cmd/lint-setup-terraform/linter/lint_test.go renamed to cmd/lint-action/linter/lint_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,34 +93,35 @@ jobs:
9393
filename string
9494
content string
9595
expectCount int
96-
expect []lint.ViolationInstance
96+
expect []*lint.ViolationInstance
9797
wantError bool
9898
}{
9999
{
100100
name: "yaml without setup-terraform action",
101101
filename: "/test/myfile1",
102102
content: withoutSetupTerraform,
103103
expectCount: 0,
104-
expect: []lint.ViolationInstance{},
104+
expect: nil,
105105
wantError: false,
106106
},
107107
{
108108
name: "yaml with setup-terraform action",
109109
filename: "/test/myfile2",
110110
content: withSetupTerraform,
111111
expectCount: 1,
112-
expect: []lint.ViolationInstance{
112+
expect: []*lint.ViolationInstance{
113113
{
114-
Path: "/test/myfile2",
115-
Line: 31,
114+
ViolationType: "setup-terraform",
115+
Path: "/test/myfile2",
116+
Line: 31,
116117
},
117118
},
118119
wantError: false,
119120
},
120121
}
121122

122123
for _, tc := range cases {
123-
tc := tc // IMPORTANT: don't shadow the test case
124+
tc := tc
124125

125126
t.Run(tc.name, func(t *testing.T) {
126127
t.Parallel()

cmd/lint-action/main.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package main
16+
17+
import (
18+
"context"
19+
"flag"
20+
"fmt"
21+
"os"
22+
"os/signal"
23+
"strings"
24+
"syscall"
25+
26+
"github.com/bradegler/secure-setup-terraform/cmd/lint-action/linter"
27+
"github.com/bradegler/secure-setup-terraform/pkg/lint"
28+
"github.com/bradegler/secure-setup-terraform/pkg/version"
29+
)
30+
31+
const lintCommandHelp = `
32+
The "lint" command
33+
EXAMPLES
34+
lint-action <file1> <file2> <directory>
35+
FLAGS
36+
`
37+
38+
func main() {
39+
if err := realMain(); err != nil {
40+
fmt.Fprintln(os.Stderr, err.Error())
41+
os.Exit(1)
42+
}
43+
}
44+
45+
func realMain() error {
46+
ctx, done := signal.NotifyContext(context.Background(),
47+
syscall.SIGINT, syscall.SIGTERM)
48+
defer done()
49+
50+
f := flag.NewFlagSet("", flag.ExitOnError)
51+
f.Usage = func() {
52+
fmt.Fprintf(os.Stderr, "%s\n\n", strings.TrimSpace(lintCommandHelp))
53+
f.PrintDefaults()
54+
}
55+
showVersion := f.Bool("version", false, "display version information")
56+
57+
if err := f.Parse(os.Args[1:]); err != nil {
58+
return fmt.Errorf("failed to parse flags: %w", err)
59+
}
60+
if *showVersion {
61+
fmt.Fprintln(os.Stderr, version.HumanVersion)
62+
return nil
63+
}
64+
65+
// The linter needs at least one file or directory
66+
args := f.Args()
67+
if got := len(args); got < 1 {
68+
return fmt.Errorf("expected atleast one argument, got %d", got)
69+
}
70+
71+
return lint.RunLinter(ctx, args, &linter.GitHubActionLinter{})
72+
}

cmd/lint-local-exec/main.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

cmd/lint-setup-terraform/main.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)