Skip to content

Commit 8e39ed0

Browse files
authored
Merge pull request #21252 from github/mbg/go/private-registry-diagnostic
Go: Add diagnostic for private registry usage
2 parents 6fbf727 + d5c4a19 commit 8e39ed0

File tree

8 files changed

+121
-6
lines changed

8 files changed

+121
-6
lines changed

go/extractor/diagnostics/diagnostics.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,3 +568,25 @@ func EmitExtractionFailedForProjects(path []string) {
568568
noLocation,
569569
)
570570
}
571+
572+
func EmitPrivateRegistryUsed(writer DiagnosticsWriter, configs []string) {
573+
n := len(configs)
574+
lines := make([]string, n)
575+
576+
for i := range configs {
577+
lines[i] = fmt.Sprintf("* %s", configs[i])
578+
}
579+
580+
emitDiagnosticTo(
581+
writer,
582+
"go/autobuilder/analysis-using-private-registries",
583+
"Go extraction used private package registries",
584+
fmt.Sprintf(
585+
"Go was extracted using the following private package registr%s:\n\n%s\n",
586+
plural(n, "y", "ies"),
587+
strings.Join(lines, "\n")),
588+
severityNote,
589+
fullVisibility,
590+
noLocation,
591+
)
592+
}

go/extractor/diagnostics/diagnostics_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,46 @@ func Test_EmitCannotFindPackages_Actions(t *testing.T) {
8383
// Custom build command suggestion
8484
assert.Contains(t, d.MarkdownMessage, "If any of the packages are already present in the repository")
8585
}
86+
87+
func Test_EmitPrivateRegistryUsed_Single(t *testing.T) {
88+
writer := newMemoryDiagnosticsWriter()
89+
90+
testItems := []string{
91+
"https://github.com/github/example (Git Source)",
92+
}
93+
94+
EmitPrivateRegistryUsed(writer, testItems)
95+
96+
assert.Len(t, writer.diagnostics, 1, "Expected one diagnostic to be emitted")
97+
98+
d := writer.diagnostics[0]
99+
assert.Equal(t, d.Source.Id, "go/autobuilder/analysis-using-private-registries")
100+
assert.Equal(t, d.Severity, string(severityNote))
101+
assert.Contains(t, d.MarkdownMessage, "following private package registry")
102+
103+
for i := range testItems {
104+
assert.Contains(t, d.MarkdownMessage, testItems[i])
105+
}
106+
}
107+
108+
func Test_EmitPrivateRegistryUsed_Multiple(t *testing.T) {
109+
writer := newMemoryDiagnosticsWriter()
110+
111+
testItems := []string{
112+
"https://github.com/github/example (Git Source)",
113+
"https://example.com/goproxy (GOPROXY Server)",
114+
}
115+
116+
EmitPrivateRegistryUsed(writer, testItems)
117+
118+
assert.Len(t, writer.diagnostics, 1, "Expected one diagnostic to be emitted")
119+
120+
d := writer.diagnostics[0]
121+
assert.Equal(t, d.Source.Id, "go/autobuilder/analysis-using-private-registries")
122+
assert.Equal(t, d.Severity, string(severityNote))
123+
assert.Contains(t, d.MarkdownMessage, "following private package registries")
124+
125+
for i := range testItems {
126+
assert.Contains(t, d.MarkdownMessage, testItems[i])
127+
}
128+
}

go/extractor/registries/BUILD.bazel

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package util
1+
package registries
22

33
import (
44
"encoding/json"
@@ -8,6 +8,8 @@ import (
88
"os"
99
"os/exec"
1010
"strings"
11+
12+
"github.com/github/codeql-go/extractor/diagnostics"
1113
)
1214

1315
const PROXY_HOST = "CODEQL_PROXY_HOST"
@@ -22,6 +24,19 @@ type RegistryConfig struct {
2224
URL string `json:"url"`
2325
}
2426

27+
func (config *RegistryConfig) Pretty() string {
28+
pretty_type := "other"
29+
30+
switch config.Type {
31+
case GIT_SOURCE:
32+
pretty_type = "Git Source"
33+
case GOPROXY_SERVER:
34+
pretty_type = "GOPROXY Server"
35+
}
36+
37+
return fmt.Sprintf("`%s` (%s)", config.URL, pretty_type)
38+
}
39+
2540
// The address of the proxy including protocol and port (e.g. http://localhost:1234)
2641
var proxy_address string
2742

@@ -97,24 +112,40 @@ func getEnvVars() []string {
97112
if err != nil {
98113
slog.Error("Unable to parse proxy configurations", slog.String("error", err.Error()))
99114
} else {
115+
activeConfigs := make([]RegistryConfig, 0, len(val))
116+
100117
// We only care about private registry configurations that are relevant to Go and
101118
// filter others out at this point.
102119
for _, cfg := range val {
103120
if cfg.Type == GOPROXY_SERVER {
104121
goproxy_servers = append(goproxy_servers, cfg.URL)
105122
slog.Info("Found GOPROXY server", slog.String("url", cfg.URL))
123+
activeConfigs = append(activeConfigs, cfg)
106124
} else if cfg.Type == GIT_SOURCE {
107125
parsed, err := url.Parse(cfg.URL)
108126
if err == nil && parsed.Hostname() != "" {
109127
git_source := parsed.Hostname() + parsed.Path + "*"
110128
git_sources = append(git_sources, git_source)
111129
slog.Info("Found Git source", slog.String("source", git_source))
130+
activeConfigs = append(activeConfigs, cfg)
112131
} else {
113132
slog.Warn("Not a valid URL for Git source", slog.String("url", cfg.URL))
114133
}
115134
}
116135
}
117136

137+
// Emit a diagnostic to make it easy for users to see that private registry
138+
// configurations were picked up by the Go analysis.
139+
if len(activeConfigs) > 0 {
140+
prettyConfigs := []string{}
141+
for i := range activeConfigs {
142+
prettyConfigs = append(prettyConfigs, activeConfigs[i].Pretty())
143+
}
144+
145+
diagnostics.EmitPrivateRegistryUsed(diagnostics.DefaultWriter, prettyConfigs)
146+
}
147+
148+
// Assemble environment variables for Go.
118149
goprivate := []string{}
119150

120151
if len(goproxy_servers) > 0 {

go/extractor/util/registryproxy_test.go renamed to go/extractor/registries/registryproxy_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package util
1+
package registries
22

33
import (
44
"testing"

go/extractor/toolchain/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/extractor/toolchain/toolchain.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"path/filepath"
1111
"strings"
1212

13+
"github.com/github/codeql-go/extractor/registries"
1314
"github.com/github/codeql-go/extractor/util"
1415
)
1516

@@ -140,7 +141,7 @@ func SupportsWorkspaces() bool {
140141
// Constructs a `*exec.Cmd` for `go` with the specified arguments.
141142
func GoCommand(arg ...string) *exec.Cmd {
142143
cmd := exec.Command("go", arg...)
143-
util.ApplyProxyEnvVars(cmd)
144+
registries.ApplyProxyEnvVars(cmd)
144145
return cmd
145146
}
146147

go/extractor/util/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)