Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add User-agent for repo-plugins api calls (#2722) #2731

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading