func TestAllProvidersNoMetadata(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) })) defer server.Close() // Make a transport that reroutes all traffic to the example server transport := utilnet.SetTransportDefaults(&http.Transport{ Proxy: func(req *http.Request) (*url.URL, error) { return url.Parse(server.URL + req.URL.Path) }, }) providers := []credentialprovider.DockerConfigProvider{ &dockerConfigKeyProvider{ metadataProvider{Client: &http.Client{Transport: transport}}, }, &dockerConfigUrlKeyProvider{ metadataProvider{Client: &http.Client{Transport: transport}}, }, &containerRegistryProvider{ metadataProvider{Client: &http.Client{Transport: transport}}, }, } for _, provider := range providers { if provider.Enabled() { t.Errorf("Provider %s is unexpectedly enabled", reflect.TypeOf(provider).String()) } } }
func TestComputePlatformScopeSubstitutesStorageScope(t *testing.T) { const ( defaultEndpoint = "/computeMetadata/v1/instance/service-accounts/default/" scopeEndpoint = defaultEndpoint + "scopes" ) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Only serve the URL key and the value endpoint if scopeEndpoint == r.URL.Path { w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") fmt.Fprint(w, `["https://www.googleapis.com/auth/compute.read_write","https://www.googleapis.com/auth/cloud-platform.read-only"]`) } else { w.WriteHeader(http.StatusNotFound) } })) defer server.Close() // Make a transport that reroutes all traffic to the example server transport := utilnet.SetTransportDefaults(&http.Transport{ Proxy: func(req *http.Request) (*url.URL, error) { return url.Parse(server.URL + req.URL.Path) }, }) provider := &containerRegistryProvider{ metadataProvider{Client: &http.Client{Transport: transport}}, } if !provider.Enabled() { t.Errorf("Provider is unexpectedly disabled") } }
func newMesosClient( md detector.Master, mesosHttpClientTimeout, stateCacheTTL time.Duration) (*mesosClient, error) { tr := utilnet.SetTransportDefaults(&http.Transport{}) httpClient := &http.Client{ Transport: tr, Timeout: mesosHttpClientTimeout, } return createMesosClient(md, httpClient, tr, stateCacheTTL) }
func (l *SSHTunnelList) healthCheck(e sshTunnelEntry) error { // GET the healthcheck path using the provided tunnel's dial function. transport := utilnet.SetTransportDefaults(&http.Transport{ Dial: e.Tunnel.Dial, // TODO(cjcullen): Plumb real TLS options through. TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, }) client := &http.Client{Transport: transport} resp, err := client.Get(l.healthCheckURL.String()) if err != nil { return err } resp.Body.Close() return nil }
func (c *tlsTransportCache) get(config *Config) (http.RoundTripper, error) { key, err := tlsConfigKey(config) if err != nil { return nil, err } // Ensure we only create a single transport for the given TLS options c.mu.Lock() defer c.mu.Unlock() // See if we already have a custom transport for this config if t, ok := c.transports[key]; ok { return t, nil } // Get the TLS options for this client config tlsConfig, err := TLSConfigFor(config) if err != nil { return nil, err } // The options didn't require a custom TLS config if tlsConfig == nil { return http.DefaultTransport, nil } // Cache a single transport for these options c.transports[key] = utilnet.SetTransportDefaults(&http.Transport{ Proxy: http.ProxyFromEnvironment, TLSHandshakeTimeout: 10 * time.Second, TLSClientConfig: tlsConfig, MaxIdleConnsPerHost: idleConnsPerHost, Dial: (&net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, }).Dial, }) return c.transports[key], nil }
func newTransportForETCD2(certFile, keyFile, caFile string) (*http.Transport, error) { info := transport.TLSInfo{ CertFile: certFile, KeyFile: keyFile, CAFile: caFile, } cfg, err := info.ClientConfig() if err != nil { return nil, err } // Copied from etcd.DefaultTransport declaration. // TODO: Determine if transport needs optimization tr := utilnet.SetTransportDefaults(&http.Transport{ Proxy: http.ProxyFromEnvironment, Dial: (&net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, }).Dial, TLSHandshakeTimeout: 10 * time.Second, MaxIdleConnsPerHost: 500, TLSClientConfig: cfg, }) return tr, nil }
func TestDockerKeyringFromGoogleDockerConfigMetadata(t *testing.T) { registryUrl := "hello.kubernetes.io" email := "*****@*****.**" username := "******" password := "******" auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password))) sampleDockerConfig := fmt.Sprintf(`{ "https://%s": { "email": %q, "auth": %q } }`, registryUrl, email, auth) const probeEndpoint = "/computeMetadata/v1/" server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Only serve the one metadata key. if probeEndpoint == r.URL.Path { w.WriteHeader(http.StatusOK) } else if strings.HasSuffix(dockerConfigKey, r.URL.Path) { w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") fmt.Fprintln(w, sampleDockerConfig) } else { w.WriteHeader(http.StatusNotFound) } })) defer server.Close() // Make a transport that reroutes all traffic to the example server transport := utilnet.SetTransportDefaults(&http.Transport{ Proxy: func(req *http.Request) (*url.URL, error) { return url.Parse(server.URL + req.URL.Path) }, }) keyring := &credentialprovider.BasicDockerKeyring{} provider := &dockerConfigKeyProvider{ metadataProvider{Client: &http.Client{Transport: transport}}, } if !provider.Enabled() { t.Errorf("Provider is unexpectedly disabled") } keyring.Add(provider.Provide()) creds, ok := keyring.Lookup(registryUrl) if !ok { t.Errorf("Didn't find expected URL: %s", registryUrl) return } if len(creds) > 1 { t.Errorf("Got more hits than expected: %s", creds) } val := creds[0] if username != val.Username { t.Errorf("Unexpected username value, want: %s, got: %s", username, val.Username) } if password != val.Password { t.Errorf("Unexpected password value, want: %s, got: %s", password, val.Password) } if email != val.Email { t.Errorf("Unexpected email value, want: %s, got: %s", email, val.Email) } }
func TestContainerRegistryBasics(t *testing.T) { registryUrl := "container.cloud.google.com" email := "*****@*****.**" token := &tokenBlob{AccessToken: "ya26.lots-of-indiscernible-garbage"} const ( defaultEndpoint = "/computeMetadata/v1/instance/service-accounts/default/" scopeEndpoint = defaultEndpoint + "scopes" emailEndpoint = defaultEndpoint + "email" tokenEndpoint = defaultEndpoint + "token" ) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Only serve the URL key and the value endpoint if scopeEndpoint == r.URL.Path { w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") fmt.Fprintf(w, `["%s.read_write"]`, storageScopePrefix) } else if emailEndpoint == r.URL.Path { w.WriteHeader(http.StatusOK) fmt.Fprint(w, email) } else if tokenEndpoint == r.URL.Path { w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") bytes, err := json.Marshal(token) if err != nil { t.Fatalf("unexpected error: %v", err) } fmt.Fprintln(w, string(bytes)) } else { w.WriteHeader(http.StatusNotFound) } })) defer server.Close() // Make a transport that reroutes all traffic to the example server transport := utilnet.SetTransportDefaults(&http.Transport{ Proxy: func(req *http.Request) (*url.URL, error) { return url.Parse(server.URL + req.URL.Path) }, }) keyring := &credentialprovider.BasicDockerKeyring{} provider := &containerRegistryProvider{ metadataProvider{Client: &http.Client{Transport: transport}}, } if !provider.Enabled() { t.Errorf("Provider is unexpectedly disabled") } keyring.Add(provider.Provide()) creds, ok := keyring.Lookup(registryUrl) if !ok { t.Errorf("Didn't find expected URL: %s", registryUrl) return } if len(creds) > 1 { t.Errorf("Got more hits than expected: %s", creds) } val := creds[0] if "_token" != val.Username { t.Errorf("Unexpected username value, want: %s, got: %s", "_token", val.Username) } if token.AccessToken != val.Password { t.Errorf("Unexpected password value, want: %s, got: %s", token.AccessToken, val.Password) } if email != val.Email { t.Errorf("Unexpected email value, want: %s, got: %s", email, val.Email) } }
func New() HTTPProber { tlsConfig := &tls.Config{InsecureSkipVerify: true} transport := utilnet.SetTransportDefaults(&http.Transport{TLSClientConfig: tlsConfig, DisableKeepAlives: true}) return httpProber{transport} }