Skip to content

Commit f4faf94

Browse files
committed
add source code
1 parent 881b2f4 commit f4faf94

File tree

113 files changed

+1415352
-0
lines changed

Some content is hidden

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

113 files changed

+1415352
-0
lines changed

.gitignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Default ignored files
2+
/shelf/
3+
/workspace.xml
4+
# Editor-based HTTP Client requests
5+
/httpRequests/
6+
# Datasource local storage ignored files
7+
/dataSources/
8+
/dataSources.local.xml
9+
10+
*.iml
11+
.idea/

.goreleaser.yml

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
project_name: log4j-detect
2+
3+
release:
4+
prerelease: auto
5+
name_template: "{{.ProjectName}}-v{{.Version}}"
6+
7+
github:
8+
owner: whitesource
9+
name: log4j-detect-distribution
10+
11+
changelog:
12+
skip: true
13+
14+
before:
15+
hooks:
16+
- go mod tidy
17+
18+
checksum:
19+
algorithm: sha256
20+
# Disable the generation/upload of the checksum file.
21+
disable: false
22+
23+
builds:
24+
- <<: &build_defaults
25+
binary: log4j-detect
26+
main: ./main.go
27+
28+
id: linux
29+
goos: [ linux ]
30+
goarch: [ amd64, arm64 ]
31+
32+
- <<: *build_defaults
33+
id: windows
34+
goos: [ windows ]
35+
goarch: [ amd64, arm64 ]
36+
37+
- <<: *build_defaults
38+
id: macos
39+
goos: [ darwin ]
40+
goarch: [ amd64, arm64 ]
41+
42+
43+
archives:
44+
- <<: &archive_defaults
45+
name_template: "{{ .ProjectName }}-{{ .Version }}-{{ .Os }}-{{ .Arch }}"
46+
wrap_in_directory: "false"
47+
files:
48+
- none*
49+
id: unix
50+
builds: [ linux, macos ]
51+
format: tar.gz
52+
53+
- <<: *archive_defaults
54+
id: windows
55+
builds: [ windows ]
56+
format: zip
57+
58+
nfpms:
59+
- maintainer: WhiteSource
60+
vendor: WhiteSource
61+
homepage: https://github.com/whitesource/icu-log4j-distribution
62+
description: Tool for discovering "log4shell" exploit
63+
bindir: /usr/bin
64+
formats:
65+
- deb
66+
- rpm

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 nabeel
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

cmd/clioptions/options.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package clioptions
2+
3+
import (
4+
"io"
5+
"os"
6+
)
7+
8+
// IOStreams provides the standard names for iostreams.
9+
type IOStreams struct {
10+
// In think, os.Stdin
11+
In io.Reader
12+
// Out think, os.Stdout
13+
Out io.Writer
14+
// ErrOut think, os.Stderr
15+
ErrOut io.Writer
16+
}
17+
18+
// StandardIOStreams returns an IOStreams from os.Stdin, os.Stdout
19+
func StandardIOStreams() IOStreams {
20+
return IOStreams{
21+
In: os.Stdin,
22+
Out: os.Stdout,
23+
ErrOut: os.Stderr,
24+
}
25+
}

cmd/clioptions/settings/fs.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package settings
2+
3+
import (
4+
"github.com/go-logr/logr"
5+
"github.com/whitesource/log4j-detect/fs"
6+
"github.com/whitesource/log4j-detect/operations"
7+
fsop "github.com/whitesource/log4j-detect/operations/fs"
8+
rc "github.com/whitesource/log4j-detect/records"
9+
fsscreen "github.com/whitesource/log4j-detect/screening/fs"
10+
"github.com/whitesource/log4j-detect/utils/exec"
11+
)
12+
13+
type FilesystemResolver struct {
14+
Disabled bool
15+
}
16+
17+
func (r FilesystemResolver) Queries() map[rc.Organ]*fs.Query {
18+
if r.Disabled {
19+
return nil
20+
}
21+
22+
return map[rc.Organ]*fs.Query{rc.OFS: fsscreen.Query()}
23+
}
24+
25+
func (r FilesystemResolver) Surgeons(logger logr.Logger, commander exec.Commander) map[rc.Organ]operations.Surgeon {
26+
if r.Disabled {
27+
return nil
28+
}
29+
30+
return map[rc.Organ]operations.Surgeon{
31+
rc.OFS: fsop.NewSurgeon(logger, commander),
32+
}
33+
}

cmd/clioptions/settings/gradle.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package settings
2+
3+
import (
4+
"github.com/go-logr/logr"
5+
"github.com/whitesource/log4j-detect/fs"
6+
"github.com/whitesource/log4j-detect/operations"
7+
gradleS "github.com/whitesource/log4j-detect/operations/gradle"
8+
rc "github.com/whitesource/log4j-detect/records"
9+
gradleQ "github.com/whitesource/log4j-detect/screening/gradle"
10+
"github.com/whitesource/log4j-detect/utils/exec"
11+
)
12+
13+
type GradleResolver struct {
14+
Disabled bool
15+
AdditionalArgs []string
16+
Configurations struct {
17+
Include []string
18+
Exclude []string
19+
}
20+
}
21+
22+
func (r GradleResolver) Queries() map[rc.Organ]*fs.Query {
23+
if r.Disabled {
24+
return nil
25+
}
26+
27+
return map[rc.Organ]*fs.Query{rc.OGradle: gradleQ.Query()}
28+
}
29+
30+
func (r GradleResolver) Surgeons(logger logr.Logger, commander exec.Commander) map[rc.Organ]operations.Surgeon {
31+
if r.Disabled {
32+
return nil
33+
}
34+
35+
return map[rc.Organ]operations.Surgeon{
36+
rc.OGradle: gradleS.NewSurgeon(logger, commander, r.AdditionalArgs, r.Configurations.Include, r.Configurations.Exclude),
37+
}
38+
}

cmd/clioptions/settings/maven.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package settings
2+
3+
import (
4+
"github.com/go-logr/logr"
5+
"github.com/whitesource/log4j-detect/fs"
6+
"github.com/whitesource/log4j-detect/operations"
7+
mavenS "github.com/whitesource/log4j-detect/operations/maven"
8+
rc "github.com/whitesource/log4j-detect/records"
9+
mavenQ "github.com/whitesource/log4j-detect/screening/maven"
10+
"github.com/whitesource/log4j-detect/utils/exec"
11+
)
12+
13+
type MavenResolver struct {
14+
Disabled bool
15+
AdditionalArgs []string
16+
Scopes struct {
17+
Include []string
18+
Exclude []string
19+
}
20+
}
21+
22+
func (r MavenResolver) Queries() map[rc.Organ]*fs.Query {
23+
if r.Disabled {
24+
return nil
25+
}
26+
27+
return map[rc.Organ]*fs.Query{rc.OMaven: mavenQ.Query()}
28+
}
29+
30+
func (r MavenResolver) Surgeons(logger logr.Logger, commander exec.Commander) map[rc.Organ]operations.Surgeon {
31+
if r.Disabled {
32+
return nil
33+
}
34+
35+
return map[rc.Organ]operations.Surgeon{
36+
rc.OMaven: mavenS.NewSurgeon(logger, commander, r.AdditionalArgs, r.Scopes.Include, r.Scopes.Exclude),
37+
}
38+
}

cmd/clioptions/settings/settings.go

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package settings
2+
3+
import (
4+
"fmt"
5+
"github.com/go-logr/logr"
6+
"github.com/spf13/cobra"
7+
"github.com/whitesource/log4j-detect/fs"
8+
"github.com/whitesource/log4j-detect/fs/match"
9+
"github.com/whitesource/log4j-detect/operations"
10+
"github.com/whitesource/log4j-detect/records"
11+
"github.com/whitesource/log4j-detect/utils/exec"
12+
"regexp"
13+
)
14+
15+
type Flags struct {
16+
mavenOnly bool
17+
gradleOnly bool
18+
}
19+
20+
func (f *Flags) ToSettings(logger logr.Logger) (*Settings, error) {
21+
if f.mavenOnly && f.gradleOnly {
22+
return nil, fmt.Errorf("bad")
23+
}
24+
25+
s := &Settings{
26+
Resolvers: Resolvers{
27+
Gradle: GradleResolver{
28+
Disabled: f.mavenOnly,
29+
},
30+
Maven: MavenResolver{
31+
Disabled: f.gradleOnly,
32+
},
33+
Fs: FilesystemResolver{
34+
Disabled: false,
35+
},
36+
},
37+
logger: logger,
38+
}
39+
return s, nil
40+
}
41+
42+
func AddFlags(cmd *cobra.Command, f *Flags) {
43+
cmd.Flags().BoolVarP(&f.mavenOnly, "maven-only", "m", false, "only scan for maven projects")
44+
cmd.Flags().BoolVarP(&f.gradleOnly, "gradle-only", "g", false, "only scan for gradle projects")
45+
}
46+
47+
// Settings represents all settings.
48+
// this includes logging parameters and other parameters that may modify the
49+
// behavior of resolvers for different package managers.
50+
// It does not contain authentication information - this should be stored in a profile or passed as arguments.
51+
type Settings struct {
52+
Resolvers Resolvers
53+
Excludes []string
54+
logger logr.Logger
55+
}
56+
57+
type Resolvers struct {
58+
Gradle GradleResolver
59+
Maven MavenResolver
60+
Fs FilesystemResolver
61+
}
62+
63+
type Resolver interface {
64+
Queries() map[records.Organ]*fs.Query
65+
Surgeons(logger logr.Logger, commander exec.Commander) map[records.Organ]operations.Surgeon
66+
}
67+
68+
var defaultExcludes = match.Or(
69+
match.FilenameRegex(regexp.MustCompile("^target$")),
70+
)
71+
72+
func (s *Settings) GlobalExcludes() match.Matcher {
73+
if s.Excludes == nil {
74+
return defaultExcludes
75+
}
76+
77+
if len(s.Excludes) == 0 {
78+
return nil
79+
}
80+
81+
var excludeMatchers []match.Matcher
82+
for _, e := range s.Excludes {
83+
if r, err := regexp.Compile(e); err == nil {
84+
excludeMatchers = append(excludeMatchers, match.FilenameRegex(r))
85+
} else {
86+
s.logger.Info("invalid regex syntax found in excludes", "regex", e)
87+
}
88+
}
89+
return match.Or(excludeMatchers...)
90+
}
91+
92+
func (r *Resolvers) ManifestQueries() map[records.Organ]*fs.Query {
93+
return mergeQueries(r.Maven, r.Gradle, r.Fs)
94+
}
95+
96+
func (r *Resolvers) Surgeons(logger logr.Logger, commander exec.Commander) map[records.Organ]operations.Surgeon {
97+
return mergeSurgeons(logger, commander, r.Maven, r.Gradle, r.Fs)
98+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package settings
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestSettings_GlobalExcludes(t *testing.T) {
9+
s := Settings{}
10+
assert.Equal(t, s.GlobalExcludes(), defaultExcludes)
11+
12+
s = Settings{
13+
Excludes: []string{},
14+
}
15+
assert.NotEqual(t, s.GlobalExcludes(), defaultExcludes)
16+
17+
s = Settings{
18+
Excludes: []string{"^\\.git$", "^node_modules$"},
19+
}
20+
exclude := s.GlobalExcludes()
21+
assert.True(t, exclude.Match(".git", "", 0))
22+
assert.True(t, exclude.Match("node_modules", "", 0))
23+
}

0 commit comments

Comments
 (0)