Skip to content

Commit 2628c77

Browse files
author
OpenShift Bot
authored
Merge pull request #13751 from bparees/clear_proxy
Merged by openshift-bot
2 parents 5b5f19b + fb9a4bd commit 2628c77

Some content is hidden

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

42 files changed

+704
-5058
lines changed

Godeps/Godeps.json

+23-23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/build/builder/cmd/builder.go

+21-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/openshift/origin/pkg/build/api/validation"
2424
bld "github.com/openshift/origin/pkg/build/builder"
2525
"github.com/openshift/origin/pkg/build/builder/cmd/scmauth"
26+
"github.com/openshift/origin/pkg/build/util"
2627
"github.com/openshift/origin/pkg/client"
2728
"github.com/openshift/origin/pkg/generate/git"
2829
"github.com/openshift/origin/pkg/version"
@@ -47,17 +48,32 @@ func newBuilderConfigFromEnvironment(out io.Writer) (*builderConfig, error) {
4748

4849
cfg.out = out
4950

50-
// build (BUILD)
5151
buildStr := os.Getenv("BUILD")
52-
glog.V(4).Infof("$BUILD env var is %s \n", buildStr)
5352
cfg.build = &api.Build{}
54-
if err := runtime.DecodeInto(kapi.Codecs.UniversalDecoder(), []byte(buildStr), cfg.build); err != nil {
55-
return nil, fmt.Errorf("unable to parse build: %v", err)
53+
54+
obj, groupVersionKind, err := kapi.Codecs.UniversalDecoder().Decode([]byte(buildStr), nil, nil)
55+
if err != nil {
56+
return nil, fmt.Errorf("unable to parse build string: %v", err)
57+
}
58+
ok := false
59+
cfg.build, ok = obj.(*api.Build)
60+
if !ok {
61+
return nil, fmt.Errorf("build string is not a build: %v", err)
62+
}
63+
if glog.V(4) {
64+
redactedBuild := util.SafeForLoggingBuild(cfg.build)
65+
if err != nil {
66+
return nil, fmt.Errorf("unable to strip proxy credentials from build: %v", err)
67+
}
68+
bytes, err := runtime.Encode(kapi.Codecs.LegacyCodec(groupVersionKind.GroupVersion()), redactedBuild)
69+
if err != nil {
70+
return nil, fmt.Errorf("unable to serialize build: %v", err)
71+
}
72+
glog.V(4).Infof("redacted build: %v", string(bytes))
5673
}
5774
if errs := validation.ValidateBuild(cfg.build); len(errs) > 0 {
5875
return nil, errors.NewInvalid(schema.GroupKind{Kind: "Build"}, cfg.build.Name, errs)
5976
}
60-
glog.V(4).Infof("Build: %#v", cfg.build)
6177

6278
masterVersion := os.Getenv(api.OriginVersion)
6379
thisVersion := version.Get().String()

pkg/build/builder/dockerutil.go

+30-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ import (
1616
dockertypes "github.com/docker/engine-api/types"
1717
docker "github.com/fsouza/go-dockerclient"
1818
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
19-
"k8s.io/kubernetes/pkg/credentialprovider"
20-
"k8s.io/kubernetes/pkg/util/interrupt"
2119

2220
"github.com/openshift/source-to-image/pkg/tar"
21+
s2iutil "github.com/openshift/source-to-image/pkg/util"
22+
23+
"k8s.io/kubernetes/pkg/credentialprovider"
24+
"k8s.io/kubernetes/pkg/util/interrupt"
2325

2426
"github.com/openshift/imagebuilder"
2527
"github.com/openshift/imagebuilder/dockerclient"
@@ -281,7 +283,12 @@ func tagImage(dockerClient DockerClient, image, name string) error {
281283
// removed after it terminates.
282284
func dockerRun(client DockerClient, createOpts docker.CreateContainerOptions, attachOpts docker.AttachToContainerOptions) error {
283285
// Create a new container.
284-
glog.V(4).Infof("Creating container with options {Name:%q Config:%+v HostConfig:%+v} ...", createOpts.Name, createOpts.Config, createOpts.HostConfig)
286+
// First strip any inlined proxy credentials from the *proxy* env variables,
287+
// before logging the env variables.
288+
if glog.Is(4) {
289+
redactedOpts := SafeForLoggingDockerCreateOptions(&createOpts)
290+
glog.V(4).Infof("Creating container with options {Name:%q Config:%+v HostConfig:%+v} ...", redactedOpts.Name, redactedOpts.Config, redactedOpts.HostConfig)
291+
}
285292
c, err := client.CreateContainer(createOpts)
286293
if err != nil {
287294
return fmt.Errorf("create container %q: %v", createOpts.Name, err)
@@ -483,3 +490,23 @@ func GetDockerClient() (client *docker.Client, endpoint string, err error) {
483490
}
484491
return
485492
}
493+
494+
// SafeForLoggingDockerConfig returns a copy of a docker config struct
495+
// where any proxy credentials in the env section of the config
496+
// have been redacted.
497+
func SafeForLoggingDockerConfig(config *docker.Config) *docker.Config {
498+
origEnv := config.Env
499+
newConfig := *config
500+
newConfig.Env = s2iutil.SafeForLoggingEnv(origEnv)
501+
return &newConfig
502+
}
503+
504+
// SafeForLoggingDockerCreateOptions returns a copy of a docker
505+
// create container options struct where any proxy credentials in the env section of
506+
// the config have been redacted.
507+
func SafeForLoggingDockerCreateOptions(opts *docker.CreateContainerOptions) *docker.CreateContainerOptions {
508+
origConfig := opts.Config
509+
newOpts := *opts
510+
newOpts.Config = SafeForLoggingDockerConfig(origConfig)
511+
return &newOpts
512+
}

pkg/build/builder/dockerutil_test.go

+42-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"strings"
1313
"testing"
1414

15-
"github.com/fsouza/go-dockerclient"
15+
docker "github.com/fsouza/go-dockerclient"
1616
)
1717

1818
type FakeDocker struct {
@@ -522,3 +522,44 @@ func TestSimpleProgress(t *testing.T) {
522522
}
523523
}
524524
}
525+
526+
var credsRegex = regexp.MustCompile("user:password")
527+
var redactedRegex = regexp.MustCompile("redacted")
528+
529+
func TestSafeForLoggingDockerCreateOptions(t *testing.T) {
530+
opts := &docker.CreateContainerOptions{
531+
Config: &docker.Config{
532+
533+
Env: []string{
534+
"http_proxy=http://user:[email protected]",
535+
"ignore=http://user:[email protected]",
536+
},
537+
},
538+
}
539+
stripped := SafeForLoggingDockerCreateOptions(opts)
540+
if credsRegex.MatchString(stripped.Config.Env[0]) {
541+
t.Errorf("stripped proxy variable %s should not contain credentials", stripped.Config.Env[0])
542+
}
543+
if !redactedRegex.MatchString(stripped.Config.Env[0]) {
544+
t.Errorf("stripped proxy variable %s should contain redacted", stripped.Config.Env[0])
545+
}
546+
if !credsRegex.MatchString(stripped.Config.Env[1]) {
547+
t.Errorf("stripped other variable %s should contain credentials", stripped.Config.Env[1])
548+
}
549+
if redactedRegex.MatchString(stripped.Config.Env[1]) {
550+
t.Errorf("stripped other variable %s should not contain redacted", stripped.Config.Env[1])
551+
}
552+
553+
if !credsRegex.MatchString(opts.Config.Env[0]) {
554+
t.Errorf("original proxy variable %s should contain credentials", opts.Config.Env[0])
555+
}
556+
if redactedRegex.MatchString(opts.Config.Env[0]) {
557+
t.Errorf("original proxy variable %s should not contain redacted", opts.Config.Env[0])
558+
}
559+
if !credsRegex.MatchString(opts.Config.Env[1]) {
560+
t.Errorf("original other variable %s should contain credentials", opts.Config.Env[1])
561+
}
562+
if redactedRegex.MatchString(opts.Config.Env[1]) {
563+
t.Errorf("original other variable %s should not contain redacted", opts.Config.Env[1])
564+
}
565+
}

pkg/build/builder/sti.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/openshift/origin/pkg/build/builder/cmd/dockercfg"
2424
"github.com/openshift/origin/pkg/build/builder/timing"
2525
"github.com/openshift/origin/pkg/build/controller/strategy"
26+
"github.com/openshift/origin/pkg/build/util"
2627
"github.com/openshift/origin/pkg/client"
2728
"github.com/openshift/origin/pkg/generate/git"
2829

@@ -192,8 +193,9 @@ func (s *S2IBuilder) Build() error {
192193
}
193194
if scriptDownloadProxyConfig != nil {
194195
glog.V(0).Infof("Using HTTP proxy %v and HTTPS proxy %v for script download",
195-
scriptDownloadProxyConfig.HTTPProxy,
196-
scriptDownloadProxyConfig.HTTPSProxy)
196+
util.SafeForLoggingURL(scriptDownloadProxyConfig.HTTPProxy),
197+
util.SafeForLoggingURL(scriptDownloadProxyConfig.HTTPSProxy),
198+
)
197199
}
198200

199201
var incremental bool
@@ -283,7 +285,10 @@ func (s *S2IBuilder) Build() error {
283285
if err != nil {
284286
return err
285287
}
286-
glog.V(4).Infof("Creating a new S2I builder with build config: %#v\n", describe.Config(client, config))
288+
if glog.Is(4) {
289+
redactedConfig := util.SafeForLoggingS2IConfig(config)
290+
glog.V(4).Infof("Creating a new S2I builder with config: %#v\n", describe.Config(client, redactedConfig))
291+
}
287292
builder, buildInfo, err := s.builder.Builder(config, s2ibuild.Overrides{Downloader: nil})
288293
if err != nil {
289294
s.build.Status.Phase = api.BuildPhaseFailed

0 commit comments

Comments
 (0)