Skip to content

Commit

Permalink
backport proxy support. Includes fixes for both devfile/api#926 and d…
Browse files Browse the repository at this point in the history
…evfile/api#897 (#139)

Signed-off-by: Kim Tsao <ktsao@redhat.com>

Signed-off-by: Kim Tsao <ktsao@redhat.com>
  • Loading branch information
kim-tsao authored Sep 19, 2022
1 parent db88e58 commit 3f85c85
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 42 deletions.
37 changes: 22 additions & 15 deletions registry-library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,36 @@ Devfile registry library is used for interacting with devfile registry, consumer

## How to use it
1. Import devfile registry library
```go
import (
registryLibrary "github.com/devfile/registry-support/registry-library/library"
)
```
```go
import (
registryLibrary "github.com/devfile/registry-support/registry-library/library"
)
```
2. Invoke devfile registry library

a. Get the index of devfile registry for various devfile types
```go
registryIndex, err := registryLibrary.GetRegistryIndex(registryURL, options, StackDevfileType)
if err != nil {
return err
}
if err != nil {
return err
}
```
b. Get the indices of multiple devfile registries for various devfile types
```go
registryList := GetMultipleRegistryIndices(registryURLs, options, StackDevfileType)
```
c. Download the stack devfile from devfile registry
```go
err := registryLibrary.PullStackByMediaTypesFromRegistry(registry, stack, registryLibrary.DevfileMediaTypeList, destDir, options)
if err != nil {
return err
}
err := registryLibrary.PullStackByMediaTypesFromRegistry(registry, stack, registryLibrary.DevfileMediaTypeList, destDir, options)
if err != nil {
return err
}
```
d. Download the whole stack from devfile registry
```go
err := registryLibrary.PullStackFromRegistry(registry, stack, destDir, options)
if err != nil {
return err
}
return err
}
```
e. Specify Options
```go
Expand All @@ -54,3 +53,11 @@ import (
},
}
```
f. Override the HTTP request and response timeout values
```go
customTimeout := 20
options := registryLibrary.RegistryOptions{
HTTPTimeout: &customTimeout
}
```

77 changes: 50 additions & 27 deletions registry-library/library/library.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
//
// Copyright (c) 2020 Red Hat, Inc.
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
// Contributors:
// Red Hat, Inc. - initial API and implementation
/* Copyright 2020-2022 Red Hat, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package library

Expand Down Expand Up @@ -47,8 +51,7 @@ const (
DevfilePNGLogoMediaType = "image/png"
DevfileArchiveMediaType = "application/x-tar"

httpRequestTimeout = 30 * time.Second // httpRequestTimeout configures timeout of all HTTP requests
responseHeaderTimeout = 30 * time.Second // responseHeaderTimeout is the timeout to retrieve the server's response headers
httpRequestResponseTimeout = 30 * time.Second // httpRequestTimeout configures timeout of all HTTP requests
)

var (
Expand All @@ -73,9 +76,15 @@ type TelemetryData struct {
}

type RegistryOptions struct {
// SkipTLSVerify is false by default which is the recommended setting for a devfile registry deployed in production. SkipTLSVerify should only be set to true
// if you are testing a devfile registry or proxy server that is set up with self-signed certificates in a pre-production environment.
SkipTLSVerify bool
Telemetry TelemetryData
Filter RegistryFilter
// Telemetry allows clients to send telemetry data to the community Devfile Registry
Telemetry TelemetryData
// Filter allows clients to specify which architectures they want to filter their devfiles on
Filter RegistryFilter
// HTTPTimeout overrides the request and response timeout values for the custom HTTP clients set by the registry library. If unset or a negative value is specified, the default timeout of 30s will be used.
HTTPTimeout *int
}

type RegistryFilter struct {
Expand Down Expand Up @@ -140,13 +149,8 @@ func GetRegistryIndex(registryURL string, options RegistryOptions, devfileTypes

setHeaders(&req.Header, options)

httpClient := &http.Client{
Transport: &http.Transport{
ResponseHeaderTimeout: responseHeaderTimeout,
TLSClientConfig: &tls.Config{InsecureSkipVerify: options.SkipTLSVerify},
},
Timeout: httpRequestTimeout,
}
httpClient := getHTTPClient(options)

resp, err := httpClient.Do(req)
if err != nil {
return nil, err
Expand All @@ -162,7 +166,7 @@ func GetRegistryIndex(registryURL string, options RegistryOptions, devfileTypes
return registryIndex, nil
}

// GetMultipleRegistryIndices returns returns the list of stacks and/or samples of multiple registries
// GetMultipleRegistryIndices returns the list of stacks and/or samples of multiple registries
func GetMultipleRegistryIndices(registryURLs []string, options RegistryOptions, devfileTypes ...indexSchema.DevfileType) []Registry {
registryList := make([]Registry, len(registryURLs))
registryContentsChannel := make(chan []indexSchema.Schema)
Expand Down Expand Up @@ -242,11 +246,9 @@ func PullStackByMediaTypesFromRegistry(registry string, stack string, allowedMed
if urlObj.Scheme == "https" {
plainHTTP = false
}
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: options.SkipTLSVerify},
},
}

httpClient := getHTTPClient(options)

headers := make(http.Header)
setHeaders(&headers, options)

Expand Down Expand Up @@ -344,3 +346,24 @@ func setHeaders(headers *http.Header, options RegistryOptions) {
headers.Add("Locale", t.Locale)
}
}

//getHTTPClient returns a new http client object
func getHTTPClient(options RegistryOptions) *http.Client {

overriddenTimeout := httpRequestResponseTimeout
timeout := options.HTTPTimeout
//if value is invalid or unspecified, the default will be used
if timeout != nil && *timeout > 0 {
//convert timeout to seconds
overriddenTimeout = time.Duration(*timeout) * time.Second
}

return &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
ResponseHeaderTimeout: overriddenTimeout,
TLSClientConfig: &tls.Config{InsecureSkipVerify: options.SkipTLSVerify},
},
Timeout: overriddenTimeout,
}
}
26 changes: 26 additions & 0 deletions registry-library/library/library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

func TestGetRegistryIndex(t *testing.T) {
const serverIP = "127.0.0.1:8080"
invalidHTTPTimeout := -1
validHTTPTimeout := 10
archFilteredIndex := []indexSchema.Schema{
{
Name: "archindex1",
Expand Down Expand Up @@ -135,6 +137,30 @@ func TestGetRegistryIndex(t *testing.T) {
url: serverIP,
wantErr: true,
},
{
name: "Get Registry Index with invalid httpTimeout value",
url: "http://" + serverIP,
options: RegistryOptions{
HTTPTimeout: &invalidHTTPTimeout,
Filter: RegistryFilter{
Architectures: []string{"amd64", "arm64"},
},
},
devfileTypes: []indexSchema.DevfileType{indexSchema.SampleDevfileType},
wantSchemas: archFilteredIndex,
},
{
name: "Get Registry Index with valid httpTimeout value",
url: "http://" + serverIP,
options: RegistryOptions{
HTTPTimeout: &validHTTPTimeout,
Filter: RegistryFilter{
Architectures: []string{"amd64", "arm64"},
},
},
devfileTypes: []indexSchema.DevfileType{indexSchema.SampleDevfileType},
wantSchemas: archFilteredIndex,
},
}

for _, test := range tests {
Expand Down

0 comments on commit 3f85c85

Please sign in to comment.