Skip to content

Commit

Permalink
Add User-agent for repo-plugins api calls (#2722)
Browse files Browse the repository at this point in the history
* Add user agent info for plugin api calls

* Remove comment

* Edit error message

* Use same user agent as api/plugin/client.go

- would be nice to use command.config but we couldn't see an easy way to thread it

Co-authored-by: david alvarado <alvaradoda@vmware.com>

* Add test

- The reason it is checking NotTo(Equal(...)) is because the default user agent "Go-http-client/1.1" is being blocked by the webserver

Co-authored-by: david alvarado <alvaradoda@vmware.com>

* Clean up deprecation and some warnings and regenerate fake_plugin_repo as we made changes

---------

Co-authored-by: M. Oleske <moleske@pivotal.io>
Co-authored-by: david alvarado <alvaradoda@vmware.com>
Co-authored-by: Al Berez <a-b@users.noreply.github.com>
  • Loading branch information
4 people authored Jan 9, 2024
1 parent 2fe0102 commit 8d19f9f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
33 changes: 28 additions & 5 deletions cf/actors/pluginrepo/plugin_repo.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package pluginrepo

import (
"code.cloudfoundry.org/cli/version"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"

clipr "code.cloudfoundry.org/cli-plugin-repo/web"
Expand All @@ -27,18 +31,37 @@ func NewPluginRepo() PluginRepo {

func (r pluginRepo) GetPlugins(repos []models.PluginRepo) (map[string][]clipr.Plugin, []string) {
var pluginList clipr.PluginsJson
repoError := []string{}
var repoError []string
repoPlugins := make(map[string][]clipr.Plugin)

for _, repo := range repos {
resp, err := http.Get(getListEndpoint(repo.URL))
client := &http.Client{}
req, err := http.NewRequest("GET", getListEndpoint(repo.URL), nil)
if err != nil {
repoError = append(repoError, fmt.Sprintf(T("Error creating a request")+" '%s' - %s", repo.Name, err.Error()))
continue
}
userAgent := fmt.Sprintf("%s/%s (%s; %s %s)",
filepath.Base(os.Args[0]),
version.VersionString(),
runtime.Version(),
runtime.GOARCH,
runtime.GOOS,
)
req.Header.Set("User-Agent", userAgent)
resp, err := client.Do(req)
if err != nil {
repoError = append(repoError, fmt.Sprintf(T("Error requesting from")+" '%s' - %s", repo.Name, err.Error()))
continue
} else {
defer resp.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
repoError = append(repoError, fmt.Sprintf(T("Error closing body")+" '%s' - %s", repo.Name, err.Error()))
}
}(resp.Body)

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
repoError = append(repoError, fmt.Sprintf(T("Error reading response from")+" '%s' - %s ", repo.Name, err.Error()))
continue
Expand Down
10 changes: 10 additions & 0 deletions cf/actors/pluginrepo/plugin_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ var _ = Describe("PluginRepo", func() {
BeforeEach(func() {
testServer1CallCount = 0
h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
testServer1CallCount++
fmt.Fprintln(w, `{"plugins":[]}`)
Expect(r.Header.Get("User-Agent")).NotTo(Equal("Go-http-client/1.1"))
})
testServer1 = httptest.NewServer(h1)

testServer2CallCount = 0
h2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
testServer2CallCount++
fmt.Fprintln(w, `{"plugins":[]}`)
Expect(r.Header.Get("User-Agent")).NotTo(Equal("Go-http-client/1.1"))
})
testServer2 = httptest.NewServer(h2)

Expand Down Expand Up @@ -87,6 +91,7 @@ var _ = Describe("PluginRepo", func() {
Context("When data is valid", func() {
BeforeEach(func() {
h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
fmt.Fprintln(w, `{"plugins":[
{
"name":"plugin1",
Expand All @@ -111,6 +116,7 @@ var _ = Describe("PluginRepo", func() {
]
}]
}`)
Expect(r.Header.Get("User-Agent")).NotTo(Equal("Go-http-client/1.1"))
})
testServer1 = httptest.NewServer(h1)

Expand Down Expand Up @@ -146,7 +152,9 @@ var _ = Describe("PluginRepo", func() {
Context("json is invalid", func() {
BeforeEach(func() {
h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
fmt.Fprintln(w, `"plugins":[]}`)
Expect(r.Header.Get("User-Agent")).NotTo(Equal("Go-http-client/1.1"))
})
testServer1 = httptest.NewServer(h1)
})
Expand Down Expand Up @@ -174,7 +182,9 @@ var _ = Describe("PluginRepo", func() {
Context("when data is valid json, but not valid plugin repo data", func() {
BeforeEach(func() {
h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
fmt.Fprintln(w, `{"bad_plugin_tag":[]}`)
Expect(r.Header.Get("User-Agent")).NotTo(Equal("Go-http-client/1.1"))
})
testServer1 = httptest.NewServer(h1)
})
Expand Down
7 changes: 4 additions & 3 deletions cf/actors/pluginrepo/pluginrepofakes/fake_plugin_repo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8d19f9f

Please sign in to comment.