Skip to content

Commit 893b87b

Browse files
committed
Replace gas with gosec everywhere in the project
1 parent da26f64 commit 893b87b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+387
-390
lines changed

.github/issue_template.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### Steps to reproduce the behavior
44

5-
### Gas version
5+
### gosec version
66

77
### Go version (output of 'go version')
88

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ install:
1111
- go get -u github.com/onsi/ginkgo/ginkgo
1212
- go get -u github.com/onsi/gomega
1313
- go get -u golang.org/x/crypto/ssh
14-
- go get -u github.com/securego/gas/cmd/gas/...
14+
- go get -u github.com/securego/gosec/cmd/gosec/...
1515
- go get -v -t ./...
1616
- export PATH=$PATH:$HOME/gopath/bin
1717

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM golang:1.9.4-alpine3.7
22

3-
ENV BIN=gas
3+
ENV BIN=gosec
44

55
COPY build/*-linux-amd64 /go/bin/$BIN
66
COPY docker-entrypoint.sh /usr/local/bin

Makefile

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
GIT_TAG?= $(shell git describe --always --tags)
22
BUILD_DATE = $(shell date +%Y-%m-%d)
3-
BIN = gas
4-
BUILD_CMD = go build -ldflags "-X main.Version=${VERSION} -X main.GitTag=${GIT_TAG} -X main.BuildDate=${BUILD_DATE}" -o build/$(BIN)-$(VERSION)-$${GOOS}-$${GOARCH} ./cmd/gas/ &
3+
BIN = gosec
4+
BUILD_CMD = go build -ldflags "-X main.Version=${VERSION} -X main.GitTag=${GIT_TAG} -X main.BuildDate=${BUILD_DATE}" -o build/$(BIN)-$(VERSION)-$${GOOS}-$${GOARCH} ./cmd/gosec/ &
55
FMT_CMD = $(gofmt -s -l -w $(find . -type f -name '*.go' -not -path './vendor/*') | tee /dev/stderr)
66
IMAGE_REPO = docker.io
77

@@ -13,12 +13,12 @@ test: bootstrap
1313
test -z '$(FMT_CMD)'
1414
go vet $(go list ./... | grep -v /vendor/)
1515
golint -set_exit_status $(shell go list ./... | grep -v vendor)
16-
gas ./...
16+
gosec ./...
1717
ginkgo -r -v
1818
bootstrap:
1919
dep ensure
2020
build:
21-
go build -o $(BIN) ./cmd/gas/
21+
go build -o $(BIN) ./cmd/gosec/
2222
clean:
2323
rm -rf build vendor
2424
rm -f release image bootstrap $(BIN)

analyzer.go

+49-49
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
// Package gas holds the central scanning logic used by GAS
16-
package gas
15+
// Package gosec holds the central scanning logic used by gosec security scanner
16+
package gosec
1717

1818
import (
1919
"go/ast"
@@ -55,7 +55,7 @@ type Metrics struct {
5555
NumFound int `json:"found"`
5656
}
5757

58-
// Analyzer object is the main object of GAS. It has methods traverse an AST
58+
// Analyzer object is the main object of gosec. It has methods traverse an AST
5959
// and invoke the correct checking rules as on each node as required.
6060
type Analyzer struct {
6161
ignoreNosec bool
@@ -74,7 +74,7 @@ func NewAnalyzer(conf Config, logger *log.Logger) *Analyzer {
7474
ignoreNoSec = setting == "true" || setting == "enabled"
7575
}
7676
if logger == nil {
77-
logger = log.New(os.Stderr, "[gas]", log.LstdFlags)
77+
logger = log.New(os.Stderr, "[gosec]", log.LstdFlags)
7878
}
7979
return &Analyzer{
8080
ignoreNosec: ignoreNoSec,
@@ -89,15 +89,15 @@ func NewAnalyzer(conf Config, logger *log.Logger) *Analyzer {
8989

9090
// LoadRules instantiates all the rules to be used when analyzing source
9191
// packages
92-
func (gas *Analyzer) LoadRules(ruleDefinitions map[string]RuleBuilder) {
92+
func (gosec *Analyzer) LoadRules(ruleDefinitions map[string]RuleBuilder) {
9393
for id, def := range ruleDefinitions {
94-
r, nodes := def(id, gas.config)
95-
gas.ruleset.Register(r, nodes...)
94+
r, nodes := def(id, gosec.config)
95+
gosec.ruleset.Register(r, nodes...)
9696
}
9797
}
9898

9999
// Process kicks off the analysis process for a given package
100-
func (gas *Analyzer) Process(buildTags []string, packagePaths ...string) error {
100+
func (gosec *Analyzer) Process(buildTags []string, packagePaths ...string) error {
101101
ctx := build.Default
102102
ctx.BuildTags = append(ctx.BuildTags, buildTags...)
103103
packageConfig := loader.Config{
@@ -111,10 +111,10 @@ func (gas *Analyzer) Process(buildTags []string, packagePaths ...string) error {
111111
return err
112112
}
113113
if _, err := os.Stat(abspath); os.IsNotExist(err) {
114-
gas.logger.Printf("Skipping: %s. Path doesn't exist.", abspath)
114+
gosec.logger.Printf("Skipping: %s. Path doesn't exist.", abspath)
115115
continue
116116
}
117-
gas.logger.Println("Searching directory:", abspath)
117+
gosec.logger.Println("Searching directory:", abspath)
118118

119119
basePackage, err := build.Default.ImportDir(packagePath, build.ImportComment)
120120
if err != nil {
@@ -135,31 +135,31 @@ func (gas *Analyzer) Process(buildTags []string, packagePaths ...string) error {
135135
}
136136

137137
for _, pkg := range builtPackage.Created {
138-
gas.logger.Println("Checking package:", pkg.String())
138+
gosec.logger.Println("Checking package:", pkg.String())
139139
for _, file := range pkg.Files {
140-
gas.logger.Println("Checking file:", builtPackage.Fset.File(file.Pos()).Name())
141-
gas.context.FileSet = builtPackage.Fset
142-
gas.context.Config = gas.config
143-
gas.context.Comments = ast.NewCommentMap(gas.context.FileSet, file, file.Comments)
144-
gas.context.Root = file
145-
gas.context.Info = &pkg.Info
146-
gas.context.Pkg = pkg.Pkg
147-
gas.context.Imports = NewImportTracker()
148-
gas.context.Imports.TrackPackages(gas.context.Pkg.Imports()...)
149-
ast.Walk(gas, file)
150-
gas.stats.NumFiles++
151-
gas.stats.NumLines += builtPackage.Fset.File(file.Pos()).LineCount()
140+
gosec.logger.Println("Checking file:", builtPackage.Fset.File(file.Pos()).Name())
141+
gosec.context.FileSet = builtPackage.Fset
142+
gosec.context.Config = gosec.config
143+
gosec.context.Comments = ast.NewCommentMap(gosec.context.FileSet, file, file.Comments)
144+
gosec.context.Root = file
145+
gosec.context.Info = &pkg.Info
146+
gosec.context.Pkg = pkg.Pkg
147+
gosec.context.Imports = NewImportTracker()
148+
gosec.context.Imports.TrackPackages(gosec.context.Pkg.Imports()...)
149+
ast.Walk(gosec, file)
150+
gosec.stats.NumFiles++
151+
gosec.stats.NumLines += builtPackage.Fset.File(file.Pos()).LineCount()
152152
}
153153
}
154154
return nil
155155
}
156156

157157
// ignore a node (and sub-tree) if it is tagged with a "#nosec" comment
158-
func (gas *Analyzer) ignore(n ast.Node) ([]string, bool) {
159-
if groups, ok := gas.context.Comments[n]; ok && !gas.ignoreNosec {
158+
func (gosec *Analyzer) ignore(n ast.Node) ([]string, bool) {
159+
if groups, ok := gosec.context.Comments[n]; ok && !gosec.ignoreNosec {
160160
for _, group := range groups {
161161
if strings.Contains(group.Text(), "#nosec") {
162-
gas.stats.NumNosec++
162+
gosec.stats.NumNosec++
163163

164164
// Pull out the specific rules that are listed to be ignored.
165165
re := regexp.MustCompile("(G\\d{3})")
@@ -182,27 +182,27 @@ func (gas *Analyzer) ignore(n ast.Node) ([]string, bool) {
182182
return nil, false
183183
}
184184

185-
// Visit runs the GAS visitor logic over an AST created by parsing go code.
185+
// Visit runs the gosec visitor logic over an AST created by parsing go code.
186186
// Rule methods added with AddRule will be invoked as necessary.
187-
func (gas *Analyzer) Visit(n ast.Node) ast.Visitor {
187+
func (gosec *Analyzer) Visit(n ast.Node) ast.Visitor {
188188
// If we've reached the end of this branch, pop off the ignores stack.
189189
if n == nil {
190-
if len(gas.context.Ignores) > 0 {
191-
gas.context.Ignores = gas.context.Ignores[1:]
190+
if len(gosec.context.Ignores) > 0 {
191+
gosec.context.Ignores = gosec.context.Ignores[1:]
192192
}
193-
return gas
193+
return gosec
194194
}
195195

196196
// Get any new rule exclusions.
197-
ignoredRules, ignoreAll := gas.ignore(n)
197+
ignoredRules, ignoreAll := gosec.ignore(n)
198198
if ignoreAll {
199199
return nil
200200
}
201201

202202
// Now create the union of exclusions.
203203
ignores := make(map[string]bool, 0)
204-
if len(gas.context.Ignores) > 0 {
205-
for k, v := range gas.context.Ignores[0] {
204+
if len(gosec.context.Ignores) > 0 {
205+
for k, v := range gosec.context.Ignores[0] {
206206
ignores[k] = v
207207
}
208208
}
@@ -212,37 +212,37 @@ func (gas *Analyzer) Visit(n ast.Node) ast.Visitor {
212212
}
213213

214214
// Push the new set onto the stack.
215-
gas.context.Ignores = append([]map[string]bool{ignores}, gas.context.Ignores...)
215+
gosec.context.Ignores = append([]map[string]bool{ignores}, gosec.context.Ignores...)
216216

217217
// Track aliased and initialization imports
218-
gas.context.Imports.TrackImport(n)
218+
gosec.context.Imports.TrackImport(n)
219219

220-
for _, rule := range gas.ruleset.RegisteredFor(n) {
220+
for _, rule := range gosec.ruleset.RegisteredFor(n) {
221221
if _, ok := ignores[rule.ID()]; ok {
222222
continue
223223
}
224-
issue, err := rule.Match(n, gas.context)
224+
issue, err := rule.Match(n, gosec.context)
225225
if err != nil {
226-
file, line := GetLocation(n, gas.context)
226+
file, line := GetLocation(n, gosec.context)
227227
file = path.Base(file)
228-
gas.logger.Printf("Rule error: %v => %s (%s:%d)\n", reflect.TypeOf(rule), err, file, line)
228+
gosec.logger.Printf("Rule error: %v => %s (%s:%d)\n", reflect.TypeOf(rule), err, file, line)
229229
}
230230
if issue != nil {
231-
gas.issues = append(gas.issues, issue)
232-
gas.stats.NumFound++
231+
gosec.issues = append(gosec.issues, issue)
232+
gosec.stats.NumFound++
233233
}
234234
}
235-
return gas
235+
return gosec
236236
}
237237

238238
// Report returns the current issues discovered and the metrics about the scan
239-
func (gas *Analyzer) Report() ([]*Issue, *Metrics) {
240-
return gas.issues, gas.stats
239+
func (gosec *Analyzer) Report() ([]*Issue, *Metrics) {
240+
return gosec.issues, gosec.stats
241241
}
242242

243243
// Reset clears state such as context, issues and metrics from the configured analyzer
244-
func (gas *Analyzer) Reset() {
245-
gas.context = &Context{}
246-
gas.issues = make([]*Issue, 0, 16)
247-
gas.stats = &Metrics{}
244+
func (gosec *Analyzer) Reset() {
245+
gosec.context = &Context{}
246+
gosec.issues = make([]*Issue, 0, 16)
247+
gosec.stats = &Metrics{}
248248
}

analyzer_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
package gas_test
1+
package gosec_test
22

33
import (
44
"io/ioutil"
55
"log"
66
"os"
77
"strings"
88

9-
"github.com/securego/gas"
10-
"github.com/securego/gas/rules"
9+
"github.com/securego/gosec"
10+
"github.com/securego/gosec/rules"
1111

1212
. "github.com/onsi/ginkgo"
1313
. "github.com/onsi/gomega"
14-
"github.com/securego/gas/testutils"
14+
"github.com/securego/gosec/testutils"
1515
)
1616

1717
var _ = Describe("Analyzer", func() {
1818

1919
var (
20-
analyzer *gas.Analyzer
20+
analyzer *gosec.Analyzer
2121
logger *log.Logger
2222
buildTags []string
2323
)
2424
BeforeEach(func() {
2525
logger, _ = testutils.NewLogger()
26-
analyzer = gas.NewAnalyzer(nil, logger)
26+
analyzer = gosec.NewAnalyzer(nil, logger)
2727
})
2828

2929
Context("when processing a package", func() {
@@ -200,9 +200,9 @@ var _ = Describe("Analyzer", func() {
200200
source := sample.Code
201201

202202
// overwrite nosec option
203-
nosecIgnoreConfig := gas.NewConfig()
203+
nosecIgnoreConfig := gosec.NewConfig()
204204
nosecIgnoreConfig.SetGlobal("nosec", "true")
205-
customAnalyzer := gas.NewAnalyzer(nosecIgnoreConfig, logger)
205+
customAnalyzer := gosec.NewAnalyzer(nosecIgnoreConfig, logger)
206206
customAnalyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
207207

208208
nosecPackage := testutils.NewTestPackage()

call_list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
package gas
14+
package gosec
1515

1616
import (
1717
"go/ast"

call_list_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
package gas_test
1+
package gosec_test
22

33
import (
44
"go/ast"
55

66
. "github.com/onsi/ginkgo"
77
. "github.com/onsi/gomega"
8-
"github.com/securego/gas"
9-
"github.com/securego/gas/testutils"
8+
"github.com/securego/gosec"
9+
"github.com/securego/gosec/testutils"
1010
)
1111

1212
var _ = Describe("call list", func() {
1313
var (
14-
calls gas.CallList
14+
calls gosec.CallList
1515
)
1616
BeforeEach(func() {
17-
calls = gas.NewCallList()
17+
calls = gosec.NewCallList()
1818
})
1919

2020
It("should not return any matches when empty", func() {
@@ -72,7 +72,7 @@ var _ = Describe("call list", func() {
7272
matched := 0
7373
v := testutils.NewMockVisitor()
7474
v.Context = ctx
75-
v.Callback = func(n ast.Node, ctx *gas.Context) bool {
75+
v.Callback = func(n ast.Node, ctx *gosec.Context) bool {
7676
if _, ok := n.(*ast.CallExpr); ok && calls.ContainsCallExpr(n, ctx) != nil {
7777
matched++
7878
}
File renamed without changes.

0 commit comments

Comments
 (0)