Skip to content

Commit 834cef1

Browse files
Kieron BrowneFelisiaM
andcommitted
Moved OriginatingIdentityHeader to its own package and added tests.
[#161523486] Signed-off-by: Felisia Martini <[email protected]> Co-authored-by: Felisia Martini <[email protected]>
1 parent 867d4b3 commit 834cef1

File tree

6 files changed

+997
-24
lines changed

6 files changed

+997
-24
lines changed

README.md

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,65 @@
11
# brokerapi
22

3-
[![Build Status](https://travis-ci.org/pivotal-cf/brokerapi.svg?branch=master)](https://travis-ci.org/pivotal-cf/brokerapi)
3+
[![Build
4+
Status](https://travis-ci.org/pivotal-cf/brokerapi.svg?branch=master)](https://travis-ci.org/pivotal-cf/brokerapi)
45

5-
A Go package for building [V2 Open Service Broker API](https://github.com/openservicebrokerapi/servicebroker/) compliant Service Brokers.
6+
A Go package for building [V2 Open Service Broker
7+
API](https://github.com/openservicebrokerapi/servicebroker/) compliant Service
8+
Brokers.
69

710
## [Docs](https://godoc.org/github.com/pivotal-cf/brokerapi)
811

912
## Dependencies
1013

1114
- Go 1.7+
1215
- [lager](https://github.com/cloudfoundry/lager)
13-
- [gorilla/mux](https://github.com/gorilla/mux)
16+
- [gorilla/mux v1.6.1+](https://github.com/gorilla/mux)
1417

15-
We use [dep](https://github.com/golang/dep) to manager our dependencies. Use `dep ensure` in order to download the required packages.
18+
We use [dep](https://github.com/golang/dep) to manager our dependencies. Use
19+
`dep ensure` in order to download the required packages.
1620

1721
## Usage
1822

19-
`brokerapi` defines a [`ServiceBroker`](https://godoc.org/github.com/pivotal-cf/brokerapi#ServiceBroker) interface. Pass an implementation of this to [`brokerapi.New`](https://godoc.org/github.com/pivotal-cf/brokerapi#New), which returns an `http.Handler` that you can use to serve handle HTTP requests.
23+
`brokerapi` defines a
24+
[`ServiceBroker`](https://godoc.org/github.com/pivotal-cf/brokerapi#ServiceBroker)
25+
interface. Pass an implementation of this to
26+
[`brokerapi.New`](https://godoc.org/github.com/pivotal-cf/brokerapi#New), which
27+
returns an `http.Handler` that you can use to serve handle HTTP requests.
2028

21-
Alternatively, if you already have a `*mux.Router` that you want to attach service broker routes to, you can use [`brokerapi.AttachRoutes`](https://godoc.org/github.com/pivotal-cf/brokerapi#AttachRoutes).
29+
Alternatively, if you already have a `*mux.Router` that you want to attach
30+
service broker routes to, you can use
31+
[`brokerapi.AttachRoutes`](https://godoc.org/github.com/pivotal-cf/brokerapi#AttachRoutes).
32+
Note in this case, the Basic Authentication and Originating Identity middleware
33+
will not be set up, so you will have to attach them manually if required.
2234

2335
## Error types
2436

25-
`brokerapi` defines a handful of error types in `service_broker.go` for some common error cases that your service broker may encounter. Return these from your `ServiceBroker` methods where appropriate, and `brokerapi` will do the "right thing" (™), and give Cloud Foundry an appropriate status code, as per the [Service Broker API specification](https://docs.cloudfoundry.org/services/api.html).
37+
`brokerapi` defines a handful of error types in `service_broker.go` for some
38+
common error cases that your service broker may encounter. Return these from
39+
your `ServiceBroker` methods where appropriate, and `brokerapi` will do the
40+
"right thing" (™), and give Cloud Foundry an appropriate status code, as per
41+
the [Service Broker API
42+
specification](https://docs.cloudfoundry.org/services/api.html).
2643

2744
### Custom Errors
2845

29-
`NewFailureResponse()` allows you to return a custom error from any of the `ServiceBroker` interface methods which return an error. Within this you must define an error, a HTTP response status code and a logging key. You can also use the `NewFailureResponseBuilder()` to add a custom `Error:` value in the response, or indicate that the broker should return an empty response rather than the error message.
46+
`NewFailureResponse()` allows you to return a custom error from any of the
47+
`ServiceBroker` interface methods which return an error. Within this you must
48+
define an error, a HTTP response status code and a logging key. You can also
49+
use the `NewFailureResponseBuilder()` to add a custom `Error:` value in the
50+
response, or indicate that the broker should return an empty response rather
51+
than the error message.
3052

3153
## Originating Identity
3254

33-
The request context for every request contains the unparsed `X-Broker-API-Originating-Identity` header under the key `originatingIdentityKey`.
34-
More details on how the Open Service Broker API manages request originating identity is available [here](https://github.com/openservicebrokerapi/servicebroker/blob/master/spec.md#originating-identity).
55+
The request context for every request contains the unparsed
56+
`X-Broker-API-Originating-Identity` header under the key
57+
`originatingIdentityKey`. More details on how the Open Service Broker API
58+
manages request originating identity is available
59+
[here](https://github.com/openservicebrokerapi/servicebroker/blob/master/spec.md#originating-identity).
3560

3661
## Example Service Broker
3762

38-
You can see the [cf-redis](https://github.com/pivotal-cf/cf-redis-broker/blob/2f0e9a8ebb1012a9be74bbef2d411b0b3b60352f/broker/broker.go) service broker uses the BrokerAPI package to create a service broker for Redis.
63+
You can see the
64+
[cf-redis](https://github.com/pivotal-cf/cf-redis-broker/blob/2f0e9a8ebb1012a9be74bbef2d411b0b3b60352f/broker/broker.go)
65+
service broker uses the BrokerAPI package to create a service broker for Redis.

api.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
package brokerapi
1717

1818
import (
19-
"context"
2019
"encoding/json"
2120
"errors"
2221
"fmt"
22+
"github.com/pivotal-cf/brokerapi/middlewares/originating_identity_header"
2323
"net/http"
2424
"strconv"
2525

@@ -40,8 +40,6 @@ const (
4040
lastBindingOperationLogKey = "lastBindingOperation"
4141
catalogLogKey = "catalog"
4242

43-
originatingIdentityKey = "originatingIdentity"
44-
4543
instanceIDLogKey = "instance-id"
4644
instanceDetailsLogKey = "instance-details"
4745
bindingIDLogKey = "binding-id"
@@ -85,7 +83,9 @@ func New(serviceBroker ServiceBroker, logger lager.Logger, brokerCredentials Bro
8583
AttachRoutes(router, serviceBroker, logger)
8684

8785
authMiddleware := auth.NewWrapper(brokerCredentials.Username, brokerCredentials.Password).Wrap
88-
router.Use(authMiddleware, brokerApiOriginatingIdentityMiddleware)
86+
router.Use(authMiddleware)
87+
router.Use(originating_identity_header.AddToContext)
88+
8989
return router
9090
}
9191

@@ -797,12 +797,4 @@ func checkBrokerAPIVersionHdr(req *http.Request) (brokerVersion, error) {
797797
return version, errors.New("X-Broker-API-Version Header must be 2.x")
798798
}
799799
return version, nil
800-
}
801-
802-
func brokerApiOriginatingIdentityMiddleware(next http.Handler) http.Handler {
803-
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
804-
originatingIdentity := req.Header.Get("X-Broker-API-Originating-Identity")
805-
newCtx := context.WithValue(req.Context(), originatingIdentityKey, originatingIdentity)
806-
next.ServeHTTP(w, req.WithContext(newCtx))
807-
})
808-
}
800+
}

api_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,56 @@ var _ = Describe("Service Broker API", func() {
251251
})
252252
})
253253

254+
Describe("OriginatingIdentityHeader", func(){
255+
256+
var (
257+
fakeServiceBroker *fakes.AutoFakeServiceBroker
258+
req *http.Request
259+
testServer *httptest.Server
260+
)
261+
262+
BeforeEach(func() {
263+
fakeServiceBroker = new(fakes.AutoFakeServiceBroker)
264+
brokerAPI = brokerapi.New(fakeServiceBroker, brokerLogger, credentials)
265+
266+
testServer = httptest.NewServer(brokerAPI)
267+
var err error
268+
req, err = http.NewRequest("GET", testServer.URL+"/v2/catalog", nil)
269+
Expect(err).NotTo(HaveOccurred())
270+
req.Header.Add("X-Broker-API-Version", "2.14")
271+
req.SetBasicAuth(credentials.Username, credentials.Password)
272+
})
273+
274+
AfterEach(func() {
275+
testServer.Close()
276+
})
277+
278+
When("X-Broker-API-Originating-Identity is passed", func(){
279+
It("Adds it to the context", func(){
280+
originatingIdentity := "Originating Identity Name"
281+
req.Header.Add("X-Broker-API-Originating-Identity", originatingIdentity)
282+
283+
_, err := http.DefaultClient.Do(req)
284+
Expect(err).NotTo(HaveOccurred())
285+
286+
Expect(fakeServiceBroker.ServicesCallCount()).To(Equal(1), "Services was not called")
287+
ctx := fakeServiceBroker.ServicesArgsForCall(0)
288+
Expect(ctx.Value("originatingIdentity")).To(Equal(originatingIdentity))
289+
290+
})
291+
})
292+
When("X-Broker-API-Originating-Identity is not passed", func(){
293+
It("Adds empty originatingIdentity to the context", func(){
294+
_, err := http.DefaultClient.Do(req)
295+
Expect(err).NotTo(HaveOccurred())
296+
297+
Expect(fakeServiceBroker.ServicesCallCount()).To(Equal(1), "Services was not called")
298+
ctx := fakeServiceBroker.ServicesArgsForCall(0)
299+
Expect(ctx.Value("originatingIdentity")).To(Equal(""))
300+
})
301+
})
302+
})
303+
254304
Describe("catalog endpoint", func() {
255305
makeCatalogRequest := func(apiVersion string, fail bool) *httptest.ResponseRecorder {
256306
recorder := httptest.NewRecorder()

0 commit comments

Comments
 (0)