Skip to content

Commit c1054f4

Browse files
Merge pull request #20401 from deads2k/build-02-split
scrub build utils to split into proper binaries
2 parents 6f8c120 + c878987 commit c1054f4

File tree

13 files changed

+562
-543
lines changed

13 files changed

+562
-543
lines changed

pkg/build/builder/cmd/builder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func newBuilderConfigFromEnvironment(out io.Writer, needsDocker bool) (*builderC
7474
return nil, fmt.Errorf("build string %s is not a build: %#v", buildStr, obj)
7575
}
7676
if glog.V(4) {
77-
redactedBuild := buildutil.SafeForLoggingBuild(cfg.build)
77+
redactedBuild := builderutil.SafeForLoggingBuild(cfg.build)
7878
bytes, err := runtime.Encode(buildEnvVarJSONCodec, redactedBuild)
7979
if err != nil {
8080
glog.V(4).Infof("unable to print debug line: %v", err)

pkg/build/builder/sti.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ func (s *S2IBuilder) Build() error {
170170
}
171171
if scriptDownloadProxyConfig != nil {
172172
glog.V(0).Infof("Using HTTP proxy %v and HTTPS proxy %v for script download",
173-
buildutil.SafeForLoggingURL(scriptDownloadProxyConfig.HTTPProxy),
174-
buildutil.SafeForLoggingURL(scriptDownloadProxyConfig.HTTPSProxy),
173+
builderutil.SafeForLoggingURL(scriptDownloadProxyConfig.HTTPProxy),
174+
builderutil.SafeForLoggingURL(scriptDownloadProxyConfig.HTTPSProxy),
175175
)
176176
}
177177

pkg/build/builder/util.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
buildapiv1 "github.com/openshift/api/build/v1"
1616
builderutil "github.com/openshift/origin/pkg/build/builder/util"
17-
buildutil "github.com/openshift/origin/pkg/build/util"
1817
s2iapi "github.com/openshift/source-to-image/pkg/api"
1918
s2iutil "github.com/openshift/source-to-image/pkg/util"
2019
)
@@ -179,11 +178,11 @@ func SafeForLoggingS2IConfig(config *s2iapi.Config) *s2iapi.Config {
179178
newProxy := *config.ScriptDownloadProxyConfig
180179
newConfig.ScriptDownloadProxyConfig = &newProxy
181180
if newConfig.ScriptDownloadProxyConfig.HTTPProxy != nil {
182-
newConfig.ScriptDownloadProxyConfig.HTTPProxy = buildutil.SafeForLoggingURL(newConfig.ScriptDownloadProxyConfig.HTTPProxy)
181+
newConfig.ScriptDownloadProxyConfig.HTTPProxy = builderutil.SafeForLoggingURL(newConfig.ScriptDownloadProxyConfig.HTTPProxy)
183182
}
184183

185184
if newConfig.ScriptDownloadProxyConfig.HTTPProxy != nil {
186-
newConfig.ScriptDownloadProxyConfig.HTTPSProxy = buildutil.SafeForLoggingURL(newConfig.ScriptDownloadProxyConfig.HTTPProxy)
185+
newConfig.ScriptDownloadProxyConfig.HTTPSProxy = builderutil.SafeForLoggingURL(newConfig.ScriptDownloadProxyConfig.HTTPProxy)
187186
}
188187
}
189188
newConfig.ScriptsURL, _ = s2iutil.SafeForLoggingURL(newConfig.ScriptsURL)

pkg/build/builder/util/logging.go

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package util
2+
3+
import (
4+
"net/url"
5+
"regexp"
6+
7+
s2iapi "github.com/openshift/source-to-image/pkg/api"
8+
s2iutil "github.com/openshift/source-to-image/pkg/util"
9+
10+
buildapiv1 "github.com/openshift/api/build/v1"
11+
corev1 "k8s.io/api/core/v1"
12+
)
13+
14+
var (
15+
proxyRegex = regexp.MustCompile("(?i)proxy")
16+
)
17+
18+
// SafeForLoggingURL removes the user:password section of
19+
// a url if present. If not present the value is returned unchanged.
20+
func SafeForLoggingURL(u *url.URL) *url.URL {
21+
if u == nil {
22+
return nil
23+
}
24+
newURL, err := url.Parse(u.String())
25+
if err != nil {
26+
return nil
27+
}
28+
if newURL.User != nil {
29+
if _, passwordSet := newURL.User.Password(); passwordSet {
30+
newURL.User = url.User("redacted")
31+
}
32+
}
33+
return newURL
34+
}
35+
36+
// SafeForLoggingEnvVar returns a copy of an EnvVar array with
37+
// proxy credential values redacted.
38+
func SafeForLoggingEnvVar(env []corev1.EnvVar) []corev1.EnvVar {
39+
newEnv := make([]corev1.EnvVar, len(env))
40+
copy(newEnv, env)
41+
for i, env := range newEnv {
42+
if proxyRegex.MatchString(env.Name) {
43+
newEnv[i].Value, _ = s2iutil.SafeForLoggingURL(env.Value)
44+
}
45+
}
46+
return newEnv
47+
}
48+
49+
// SafeForLoggingBuildCommonSpec returns a copy of a CommonSpec with
50+
// proxy credential env variable values redacted.
51+
func SafeForLoggingBuildCommonSpec(spec *buildapiv1.CommonSpec) *buildapiv1.CommonSpec {
52+
newSpec := spec.DeepCopy()
53+
if newSpec.Source.Git != nil {
54+
if newSpec.Source.Git.HTTPProxy != nil {
55+
s, _ := s2iutil.SafeForLoggingURL(*newSpec.Source.Git.HTTPProxy)
56+
newSpec.Source.Git.HTTPProxy = &s
57+
}
58+
59+
if newSpec.Source.Git.HTTPSProxy != nil {
60+
s, _ := s2iutil.SafeForLoggingURL(*newSpec.Source.Git.HTTPSProxy)
61+
newSpec.Source.Git.HTTPSProxy = &s
62+
}
63+
}
64+
65+
if newSpec.Strategy.SourceStrategy != nil {
66+
newSpec.Strategy.SourceStrategy.Env = SafeForLoggingEnvVar(newSpec.Strategy.SourceStrategy.Env)
67+
}
68+
if newSpec.Strategy.DockerStrategy != nil {
69+
newSpec.Strategy.DockerStrategy.Env = SafeForLoggingEnvVar(newSpec.Strategy.DockerStrategy.Env)
70+
}
71+
if newSpec.Strategy.CustomStrategy != nil {
72+
newSpec.Strategy.CustomStrategy.Env = SafeForLoggingEnvVar(newSpec.Strategy.CustomStrategy.Env)
73+
}
74+
if newSpec.Strategy.JenkinsPipelineStrategy != nil {
75+
newSpec.Strategy.JenkinsPipelineStrategy.Env = SafeForLoggingEnvVar(newSpec.Strategy.JenkinsPipelineStrategy.Env)
76+
}
77+
return newSpec
78+
}
79+
80+
// SafeForLoggingBuild returns a copy of a Build with
81+
// proxy credentials redacted.
82+
func SafeForLoggingBuild(build *buildapiv1.Build) *buildapiv1.Build {
83+
newBuild := *build
84+
newSpec := SafeForLoggingBuildCommonSpec(&build.Spec.CommonSpec)
85+
newBuild.Spec.CommonSpec = *newSpec
86+
return &newBuild
87+
}
88+
89+
// SafeForLoggingEnvironmentList returns a copy of an s2i EnvironmentList array with
90+
// proxy credential values redacted.
91+
func SafeForLoggingEnvironmentList(env s2iapi.EnvironmentList) s2iapi.EnvironmentList {
92+
newEnv := make(s2iapi.EnvironmentList, len(env))
93+
copy(newEnv, env)
94+
proxyRegex := regexp.MustCompile("(?i)proxy")
95+
for i, env := range newEnv {
96+
if proxyRegex.MatchString(env.Name) {
97+
newEnv[i].Value, _ = s2iutil.SafeForLoggingURL(env.Value)
98+
}
99+
}
100+
return newEnv
101+
}
102+
103+
// SafeForLoggingS2IConfig returns a copy of an s2i Config with
104+
// proxy credentials redacted.
105+
func SafeForLoggingS2IConfig(config *s2iapi.Config) *s2iapi.Config {
106+
newConfig := *config
107+
newConfig.Environment = SafeForLoggingEnvironmentList(config.Environment)
108+
if config.ScriptDownloadProxyConfig != nil {
109+
newProxy := *config.ScriptDownloadProxyConfig
110+
newConfig.ScriptDownloadProxyConfig = &newProxy
111+
if newConfig.ScriptDownloadProxyConfig.HTTPProxy != nil {
112+
newConfig.ScriptDownloadProxyConfig.HTTPProxy = SafeForLoggingURL(newConfig.ScriptDownloadProxyConfig.HTTPProxy)
113+
}
114+
115+
if newConfig.ScriptDownloadProxyConfig.HTTPProxy != nil {
116+
newConfig.ScriptDownloadProxyConfig.HTTPSProxy = SafeForLoggingURL(newConfig.ScriptDownloadProxyConfig.HTTPProxy)
117+
}
118+
}
119+
newConfig.ScriptsURL, _ = s2iutil.SafeForLoggingURL(newConfig.ScriptsURL)
120+
return &newConfig
121+
}

0 commit comments

Comments
 (0)