Skip to content

Commit

Permalink
fix(support-bundle): correct user-agent bevahiour
Browse files Browse the repository at this point in the history
Only kots.io requires a user agent when downloading troubleshoot specs
  • Loading branch information
banjoh committed Nov 29, 2023
1 parent d4623d9 commit d4556d4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
30 changes: 17 additions & 13 deletions internal/specs/specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,26 @@ func LoadFromCLIArgs(ctx context.Context, client kubernetes.Interface, args []st
return nil, types.NewExitCodeError(constants.EXIT_CODE_SPEC_ISSUES, fmt.Errorf("%s is not a URL and was not found (err %s)", v, err))
}

// Download preflight specs
rawSpec, err := downloadFromHttpURL(ctx, v, map[string]string{"User-Agent": "Replicated_Preflight/v1beta2"})
parsedURL, err := url.ParseRequestURI(v)
if err != nil {
return nil, err
return nil, types.NewExitCodeError(constants.EXIT_CODE_SPEC_ISSUES, err)
}
rawSpecs = append(rawSpecs, rawSpec)

// Download support bundle specs
rawSpec, err = downloadFromHttpURL(ctx, v, map[string]string{
"User-Agent": "Replicated_Troubleshoot/v1beta1",
"Bundle-Upload-Host": fmt.Sprintf("%s://%s", u.Scheme, u.Host),
})
if err != nil {
return nil, err
if parsedURL.Host == "kots.io" {
// To download specs from kots.io, we need to set the User-Agent header
rawSpec, err := downloadFromHttpURL(ctx, v, map[string]string{
"User-Agent": "Replicated_Troubleshoot/v1beta1",
})
if err != nil {
return nil, err
}
rawSpecs = append(rawSpecs, rawSpec)
} else {
rawSpec, err := downloadFromHttpURL(ctx, v, nil)
if err != nil {
return nil, err
}
rawSpecs = append(rawSpecs, rawSpec)
}
rawSpecs = append(rawSpecs, rawSpec)
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions internal/specs/specs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package specs
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"

"github.com/google/uuid"
"github.com/replicatedhq/troubleshoot/internal/testutils"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"github.com/replicatedhq/troubleshoot/pkg/loader"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -201,3 +204,23 @@ spec:
},
}
}

func TestLoadFromURI(t *testing.T) {
m := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`apiVersion: troubleshoot.sh/v1beta2
apiVersion: troubleshoot.sh/v1beta2
kind: HostCollector
metadata:
name: cpu
spec:
collectors:
- cpu: {}
`))
}))
defer m.Close()

client := testclient.NewSimpleClientset()
specs, err := LoadFromCLIArgs(context.Background(), client, []string{m.URL}, viper.New())
require.NoError(t, err)
require.Len(t, specs.HostCollectorsV1Beta2, 1)
}

0 comments on commit d4556d4

Please sign in to comment.