Skip to content

Commit

Permalink
store, cmd/snapd: send a basic user-agent to the store
Browse files Browse the repository at this point in the history
  • Loading branch information
chipaca committed Jun 28, 2016
1 parent d57b958 commit af25c42
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/snapd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/snapcore/snapd/cmd"
"github.com/snapcore/snapd/daemon"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/store"
)

func init() {
Expand All @@ -46,6 +47,8 @@ func main() {
}

func run() error {
store.UserAgent = "snapd/" + cmd.Version

d, err := daemon.New()
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions store/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func RequestPackageAccessMacaroon() (string, error) {
if err != nil {
return "", fmt.Errorf(errorPrefix+"%v", err)
}
req.Header.Set("User-Agent", UserAgent)
req.Header.Set("accept", "application/json")
req.Header.Set("content-type", "application/json")

Expand Down Expand Up @@ -114,6 +115,7 @@ func DischargeAuthCaveat(username, password, macaroon, otp string) (string, erro
if err != nil {
return "", fmt.Errorf(errorPrefix+"%v", err)
}
req.Header.Set("User-Agent", UserAgent)
req.Header.Set("accept", "application/json")
req.Header.Set("content-type", "application/json")

Expand Down
6 changes: 6 additions & 0 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ const (
UbuntuCoreWireProtocol = "1"
)

// UserAgent to send
// xxx: this should actually be set per client request, and include the client user agent
var UserAgent = "unset"

func infoFromRemote(d snapDetails) *snap.Info {
info := &snap.Info{}
info.Architectures = d.Architectures
Expand Down Expand Up @@ -228,6 +232,7 @@ func (s *SnapUbuntuStoreRepository) setUbuntuStoreHeaders(req *http.Request, cha
auther.Authenticate(req)
}

req.Header.Set("User-Agent", UserAgent)
req.Header.Set("Accept", "application/hal+json,application/json")
req.Header.Set("X-Ubuntu-Architecture", string(arch.UbuntuArchitecture()))
req.Header.Set("X-Ubuntu-Release", release.Series)
Expand Down Expand Up @@ -737,6 +742,7 @@ func (s *SnapUbuntuStoreRepository) Assertion(assertType *asserts.AssertionType,
if auther != nil {
auther.Authenticate(req)
}
req.Header.Set("User-Agent", UserAgent)
req.Header.Set("Accept", asserts.MediaType)

resp, err := s.client.Do(req)
Expand Down
10 changes: 10 additions & 0 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (t *remoteRepoTestSuite) TestAuthenticatedDownloadDoesNotUseAnonURL(c *C) {
// check authorization is set
authorization := req.Header.Get("Authorization")
c.Check(authorization, Equals, "Authorization-details")
c.Check(req.UserAgent(), Equals, UserAgent)

c.Check(req.URL.String(), Equals, "AUTH-URL")
w.Write([]byte("I was downloaded"))
Expand Down Expand Up @@ -174,6 +175,8 @@ func (t *remoteRepoTestSuite) TestUbuntuStoreRepositoryHeaders(c *C) {

t.store.setUbuntuStoreHeaders(req, "", false, nil)

c.Check(req.UserAgent(), Equals, UserAgent)

c.Check(req.Header.Get("X-Ubuntu-Release"), Equals, "16")
c.Check(req.Header.Get("X-Ubuntu-Device-Channel"), Equals, "")
c.Check(req.Header.Get("X-Ubuntu-Confinement"), Equals, "")
Expand Down Expand Up @@ -301,6 +304,8 @@ const mockPurchaseJSON = `[

func (t *remoteRepoTestSuite) TestUbuntuStoreRepositoryDetails(c *C) {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.Check(r.UserAgent(), Equals, UserAgent)

// no store ID by default
storeID := r.Header.Get("X-Ubuntu-Store")
c.Check(storeID, Equals, "")
Expand Down Expand Up @@ -360,6 +365,7 @@ func (t *remoteRepoTestSuite) TestUbuntuStoreRepositoryDetailsSetsAuth(c *C) {
// check authorization is set
authorization := r.Header.Get("Authorization")
c.Check(authorization, Equals, "Authorization-details")
c.Check(r.UserAgent(), Equals, UserAgent)

io.WriteString(w, MockDetailsJSON)
}))
Expand All @@ -368,6 +374,7 @@ func (t *remoteRepoTestSuite) TestUbuntuStoreRepositoryDetailsSetsAuth(c *C) {
defer mockServer.Close()

mockPurchasesServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.Check(r.UserAgent(), Equals, UserAgent)
// check authorization is set
authorization := r.Header.Get("Authorization")
c.Check(authorization, Equals, "Authorization-details")
Expand Down Expand Up @@ -550,6 +557,7 @@ const MockSearchJSON = `{

func (t *remoteRepoTestSuite) TestUbuntuStoreFind(c *C) {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.Check(r.UserAgent(), Equals, UserAgent)
c.Check(r.URL.RawQuery, Equals, "q=hello")
w.Header().Set("Content-Type", "application/hal+json")
io.WriteString(w, MockSearchJSON)
Expand Down Expand Up @@ -643,6 +651,7 @@ func (t *remoteRepoTestSuite) TestUbuntuStoreFindBadBoody(c *C) {

func (t *remoteRepoTestSuite) TestUbuntuStoreFindSetsAuth(c *C) {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.Check(r.UserAgent(), Equals, UserAgent)
// check authorization is set
authorization := r.Header.Get("Authorization")
c.Check(authorization, Equals, "Authorization-details")
Expand All @@ -655,6 +664,7 @@ func (t *remoteRepoTestSuite) TestUbuntuStoreFindSetsAuth(c *C) {
defer mockServer.Close()

mockPurchasesServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.Check(r.UserAgent(), Equals, UserAgent)
authorization := r.Header.Get("Authorization")
c.Check(authorization, Equals, "Authorization-details")
c.Check(r.URL.Path, Equals, "/dev/api/snap-purchases/"+helloWorldSnapID+"/")
Expand Down

0 comments on commit af25c42

Please sign in to comment.