From e6cbefed288ac1310da96d9456172c60e49044e1 Mon Sep 17 00:00:00 2001 From: Andrew Fitzgerald Date: Sat, 27 Oct 2018 00:57:44 -0400 Subject: [PATCH 1/2] Add limited support for Originating Identity --- README.md | 5 +++++ api.go | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index abd7a9eb..23c23278 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,11 @@ Alternatively, if you already have a `*mux.Router` that you want to attach servi `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. +## Originating Identity + +The request context for every request contains the unparsed `X-Broker-API-Originating-Identity` header under the key `originatingIdentityKey`. +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). + ## Example Service Broker 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. diff --git a/api.go b/api.go index 170b7e01..8c5769a9 100644 --- a/api.go +++ b/api.go @@ -16,6 +16,7 @@ package brokerapi import ( + "context" "encoding/json" "errors" "fmt" @@ -39,6 +40,8 @@ const ( lastBindingOperationLogKey = "lastBindingOperation" catalogLogKey = "catalog" + originatingIdentityKey = "originatingIdentity" + instanceIDLogKey = "instance-id" instanceDetailsLogKey = "instance-details" bindingIDLogKey = "binding-id" @@ -79,7 +82,10 @@ type BrokerCredentials struct { func New(serviceBroker ServiceBroker, logger lager.Logger, brokerCredentials BrokerCredentials) http.Handler { router := mux.NewRouter() AttachRoutes(router, serviceBroker, logger) - return auth.NewWrapper(brokerCredentials.Username, brokerCredentials.Password).Wrap(router) + + authMiddleware := auth.NewWrapper(brokerCredentials.Username, brokerCredentials.Password).Wrap + router.Use(authMiddleware, brokerApiOriginatingIdentityMiddleware) + return router } func AttachRoutes(router *mux.Router, serviceBroker ServiceBroker, logger lager.Logger) { @@ -791,3 +797,11 @@ func checkBrokerAPIVersionHdr(req *http.Request) (brokerVersion, error) { } return version, nil } + +func brokerApiOriginatingIdentityMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + originatingIdentity := req.Header.Get("X-Broker-API-Originating-Identity") + newCtx := context.WithValue(req.Context(), originatingIdentityKey, originatingIdentity) + next.ServeHTTP(w, req.WithContext(newCtx)) + }) +} From 834cef108a6ac3a2e6219aad71243e84ddaebf42 Mon Sep 17 00:00:00 2001 From: Kieron Browne Date: Thu, 3 Jan 2019 16:01:33 +0000 Subject: [PATCH 2/2] Moved OriginatingIdentityHeader to its own package and added tests. [#161523486] Signed-off-by: Felisia Martini Co-authored-by: Felisia Martini --- README.md | 49 +- api.go | 18 +- api_test.go | 50 + fakes/auto_fake_service_broker.go | 868 ++++++++++++++++++ .../originating_identity_header.go | 34 + service_broker.go | 2 + 6 files changed, 997 insertions(+), 24 deletions(-) create mode 100644 fakes/auto_fake_service_broker.go create mode 100644 middlewares/originating_identity_header/originating_identity_header.go diff --git a/README.md b/README.md index 23c23278..a9333975 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,11 @@ # brokerapi -[![Build Status](https://travis-ci.org/pivotal-cf/brokerapi.svg?branch=master)](https://travis-ci.org/pivotal-cf/brokerapi) +[![Build +Status](https://travis-ci.org/pivotal-cf/brokerapi.svg?branch=master)](https://travis-ci.org/pivotal-cf/brokerapi) -A Go package for building [V2 Open Service Broker API](https://github.com/openservicebrokerapi/servicebroker/) compliant Service Brokers. +A Go package for building [V2 Open Service Broker +API](https://github.com/openservicebrokerapi/servicebroker/) compliant Service +Brokers. ## [Docs](https://godoc.org/github.com/pivotal-cf/brokerapi) @@ -10,29 +13,53 @@ A Go package for building [V2 Open Service Broker API](https://github.com/opense - Go 1.7+ - [lager](https://github.com/cloudfoundry/lager) -- [gorilla/mux](https://github.com/gorilla/mux) +- [gorilla/mux v1.6.1+](https://github.com/gorilla/mux) -We use [dep](https://github.com/golang/dep) to manager our dependencies. Use `dep ensure` in order to download the required packages. +We use [dep](https://github.com/golang/dep) to manager our dependencies. Use +`dep ensure` in order to download the required packages. ## Usage -`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. +`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. -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). +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). +Note in this case, the Basic Authentication and Originating Identity middleware +will not be set up, so you will have to attach them manually if required. ## Error types -`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). +`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). ### Custom Errors -`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. +`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. ## Originating Identity -The request context for every request contains the unparsed `X-Broker-API-Originating-Identity` header under the key `originatingIdentityKey`. -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). +The request context for every request contains the unparsed +`X-Broker-API-Originating-Identity` header under the key +`originatingIdentityKey`. 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). ## Example Service Broker -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. +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. diff --git a/api.go b/api.go index efd86294..93c728b9 100644 --- a/api.go +++ b/api.go @@ -16,10 +16,10 @@ package brokerapi import ( - "context" "encoding/json" "errors" "fmt" + "github.com/pivotal-cf/brokerapi/middlewares/originating_identity_header" "net/http" "strconv" @@ -40,8 +40,6 @@ const ( lastBindingOperationLogKey = "lastBindingOperation" catalogLogKey = "catalog" - originatingIdentityKey = "originatingIdentity" - instanceIDLogKey = "instance-id" instanceDetailsLogKey = "instance-details" bindingIDLogKey = "binding-id" @@ -85,7 +83,9 @@ func New(serviceBroker ServiceBroker, logger lager.Logger, brokerCredentials Bro AttachRoutes(router, serviceBroker, logger) authMiddleware := auth.NewWrapper(brokerCredentials.Username, brokerCredentials.Password).Wrap - router.Use(authMiddleware, brokerApiOriginatingIdentityMiddleware) + router.Use(authMiddleware) + router.Use(originating_identity_header.AddToContext) + return router } @@ -797,12 +797,4 @@ func checkBrokerAPIVersionHdr(req *http.Request) (brokerVersion, error) { return version, errors.New("X-Broker-API-Version Header must be 2.x") } return version, nil -} - -func brokerApiOriginatingIdentityMiddleware(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - originatingIdentity := req.Header.Get("X-Broker-API-Originating-Identity") - newCtx := context.WithValue(req.Context(), originatingIdentityKey, originatingIdentity) - next.ServeHTTP(w, req.WithContext(newCtx)) - }) -} +} \ No newline at end of file diff --git a/api_test.go b/api_test.go index 17aba6d2..19ca1bad 100644 --- a/api_test.go +++ b/api_test.go @@ -251,6 +251,56 @@ var _ = Describe("Service Broker API", func() { }) }) + Describe("OriginatingIdentityHeader", func(){ + + var ( + fakeServiceBroker *fakes.AutoFakeServiceBroker + req *http.Request + testServer *httptest.Server + ) + + BeforeEach(func() { + fakeServiceBroker = new(fakes.AutoFakeServiceBroker) + brokerAPI = brokerapi.New(fakeServiceBroker, brokerLogger, credentials) + + testServer = httptest.NewServer(brokerAPI) + var err error + req, err = http.NewRequest("GET", testServer.URL+"/v2/catalog", nil) + Expect(err).NotTo(HaveOccurred()) + req.Header.Add("X-Broker-API-Version", "2.14") + req.SetBasicAuth(credentials.Username, credentials.Password) + }) + + AfterEach(func() { + testServer.Close() + }) + + When("X-Broker-API-Originating-Identity is passed", func(){ + It("Adds it to the context", func(){ + originatingIdentity := "Originating Identity Name" + req.Header.Add("X-Broker-API-Originating-Identity", originatingIdentity) + + _, err := http.DefaultClient.Do(req) + Expect(err).NotTo(HaveOccurred()) + + Expect(fakeServiceBroker.ServicesCallCount()).To(Equal(1), "Services was not called") + ctx := fakeServiceBroker.ServicesArgsForCall(0) + Expect(ctx.Value("originatingIdentity")).To(Equal(originatingIdentity)) + + }) + }) + When("X-Broker-API-Originating-Identity is not passed", func(){ + It("Adds empty originatingIdentity to the context", func(){ + _, err := http.DefaultClient.Do(req) + Expect(err).NotTo(HaveOccurred()) + + Expect(fakeServiceBroker.ServicesCallCount()).To(Equal(1), "Services was not called") + ctx := fakeServiceBroker.ServicesArgsForCall(0) + Expect(ctx.Value("originatingIdentity")).To(Equal("")) + }) + }) + }) + Describe("catalog endpoint", func() { makeCatalogRequest := func(apiVersion string, fail bool) *httptest.ResponseRecorder { recorder := httptest.NewRecorder() diff --git a/fakes/auto_fake_service_broker.go b/fakes/auto_fake_service_broker.go new file mode 100644 index 00000000..99f3cf0f --- /dev/null +++ b/fakes/auto_fake_service_broker.go @@ -0,0 +1,868 @@ +// Code generated by counterfeiter. DO NOT EDIT. +package fakes + +import ( + context "context" + sync "sync" + + brokerapi "github.com/pivotal-cf/brokerapi" +) + +type AutoFakeServiceBroker struct { + BindStub func(context.Context, string, string, brokerapi.BindDetails, bool) (brokerapi.Binding, error) + bindMutex sync.RWMutex + bindArgsForCall []struct { + arg1 context.Context + arg2 string + arg3 string + arg4 brokerapi.BindDetails + arg5 bool + } + bindReturns struct { + result1 brokerapi.Binding + result2 error + } + bindReturnsOnCall map[int]struct { + result1 brokerapi.Binding + result2 error + } + DeprovisionStub func(context.Context, string, brokerapi.DeprovisionDetails, bool) (brokerapi.DeprovisionServiceSpec, error) + deprovisionMutex sync.RWMutex + deprovisionArgsForCall []struct { + arg1 context.Context + arg2 string + arg3 brokerapi.DeprovisionDetails + arg4 bool + } + deprovisionReturns struct { + result1 brokerapi.DeprovisionServiceSpec + result2 error + } + deprovisionReturnsOnCall map[int]struct { + result1 brokerapi.DeprovisionServiceSpec + result2 error + } + GetBindingStub func(context.Context, string, string) (brokerapi.GetBindingSpec, error) + getBindingMutex sync.RWMutex + getBindingArgsForCall []struct { + arg1 context.Context + arg2 string + arg3 string + } + getBindingReturns struct { + result1 brokerapi.GetBindingSpec + result2 error + } + getBindingReturnsOnCall map[int]struct { + result1 brokerapi.GetBindingSpec + result2 error + } + GetInstanceStub func(context.Context, string) (brokerapi.GetInstanceDetailsSpec, error) + getInstanceMutex sync.RWMutex + getInstanceArgsForCall []struct { + arg1 context.Context + arg2 string + } + getInstanceReturns struct { + result1 brokerapi.GetInstanceDetailsSpec + result2 error + } + getInstanceReturnsOnCall map[int]struct { + result1 brokerapi.GetInstanceDetailsSpec + result2 error + } + LastBindingOperationStub func(context.Context, string, string, brokerapi.PollDetails) (brokerapi.LastOperation, error) + lastBindingOperationMutex sync.RWMutex + lastBindingOperationArgsForCall []struct { + arg1 context.Context + arg2 string + arg3 string + arg4 brokerapi.PollDetails + } + lastBindingOperationReturns struct { + result1 brokerapi.LastOperation + result2 error + } + lastBindingOperationReturnsOnCall map[int]struct { + result1 brokerapi.LastOperation + result2 error + } + LastOperationStub func(context.Context, string, brokerapi.PollDetails) (brokerapi.LastOperation, error) + lastOperationMutex sync.RWMutex + lastOperationArgsForCall []struct { + arg1 context.Context + arg2 string + arg3 brokerapi.PollDetails + } + lastOperationReturns struct { + result1 brokerapi.LastOperation + result2 error + } + lastOperationReturnsOnCall map[int]struct { + result1 brokerapi.LastOperation + result2 error + } + ProvisionStub func(context.Context, string, brokerapi.ProvisionDetails, bool) (brokerapi.ProvisionedServiceSpec, error) + provisionMutex sync.RWMutex + provisionArgsForCall []struct { + arg1 context.Context + arg2 string + arg3 brokerapi.ProvisionDetails + arg4 bool + } + provisionReturns struct { + result1 brokerapi.ProvisionedServiceSpec + result2 error + } + provisionReturnsOnCall map[int]struct { + result1 brokerapi.ProvisionedServiceSpec + result2 error + } + ServicesStub func(context.Context) ([]brokerapi.Service, error) + servicesMutex sync.RWMutex + servicesArgsForCall []struct { + arg1 context.Context + } + servicesReturns struct { + result1 []brokerapi.Service + result2 error + } + servicesReturnsOnCall map[int]struct { + result1 []brokerapi.Service + result2 error + } + UnbindStub func(context.Context, string, string, brokerapi.UnbindDetails, bool) (brokerapi.UnbindSpec, error) + unbindMutex sync.RWMutex + unbindArgsForCall []struct { + arg1 context.Context + arg2 string + arg3 string + arg4 brokerapi.UnbindDetails + arg5 bool + } + unbindReturns struct { + result1 brokerapi.UnbindSpec + result2 error + } + unbindReturnsOnCall map[int]struct { + result1 brokerapi.UnbindSpec + result2 error + } + UpdateStub func(context.Context, string, brokerapi.UpdateDetails, bool) (brokerapi.UpdateServiceSpec, error) + updateMutex sync.RWMutex + updateArgsForCall []struct { + arg1 context.Context + arg2 string + arg3 brokerapi.UpdateDetails + arg4 bool + } + updateReturns struct { + result1 brokerapi.UpdateServiceSpec + result2 error + } + updateReturnsOnCall map[int]struct { + result1 brokerapi.UpdateServiceSpec + result2 error + } + invocations map[string][][]interface{} + invocationsMutex sync.RWMutex +} + +func (fake *AutoFakeServiceBroker) Bind(arg1 context.Context, arg2 string, arg3 string, arg4 brokerapi.BindDetails, arg5 bool) (brokerapi.Binding, error) { + fake.bindMutex.Lock() + ret, specificReturn := fake.bindReturnsOnCall[len(fake.bindArgsForCall)] + fake.bindArgsForCall = append(fake.bindArgsForCall, struct { + arg1 context.Context + arg2 string + arg3 string + arg4 brokerapi.BindDetails + arg5 bool + }{arg1, arg2, arg3, arg4, arg5}) + fake.recordInvocation("Bind", []interface{}{arg1, arg2, arg3, arg4, arg5}) + fake.bindMutex.Unlock() + if fake.BindStub != nil { + return fake.BindStub(arg1, arg2, arg3, arg4, arg5) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.bindReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *AutoFakeServiceBroker) BindCallCount() int { + fake.bindMutex.RLock() + defer fake.bindMutex.RUnlock() + return len(fake.bindArgsForCall) +} + +func (fake *AutoFakeServiceBroker) BindCalls(stub func(context.Context, string, string, brokerapi.BindDetails, bool) (brokerapi.Binding, error)) { + fake.bindMutex.Lock() + defer fake.bindMutex.Unlock() + fake.BindStub = stub +} + +func (fake *AutoFakeServiceBroker) BindArgsForCall(i int) (context.Context, string, string, brokerapi.BindDetails, bool) { + fake.bindMutex.RLock() + defer fake.bindMutex.RUnlock() + argsForCall := fake.bindArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4, argsForCall.arg5 +} + +func (fake *AutoFakeServiceBroker) BindReturns(result1 brokerapi.Binding, result2 error) { + fake.bindMutex.Lock() + defer fake.bindMutex.Unlock() + fake.BindStub = nil + fake.bindReturns = struct { + result1 brokerapi.Binding + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) BindReturnsOnCall(i int, result1 brokerapi.Binding, result2 error) { + fake.bindMutex.Lock() + defer fake.bindMutex.Unlock() + fake.BindStub = nil + if fake.bindReturnsOnCall == nil { + fake.bindReturnsOnCall = make(map[int]struct { + result1 brokerapi.Binding + result2 error + }) + } + fake.bindReturnsOnCall[i] = struct { + result1 brokerapi.Binding + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) Deprovision(arg1 context.Context, arg2 string, arg3 brokerapi.DeprovisionDetails, arg4 bool) (brokerapi.DeprovisionServiceSpec, error) { + fake.deprovisionMutex.Lock() + ret, specificReturn := fake.deprovisionReturnsOnCall[len(fake.deprovisionArgsForCall)] + fake.deprovisionArgsForCall = append(fake.deprovisionArgsForCall, struct { + arg1 context.Context + arg2 string + arg3 brokerapi.DeprovisionDetails + arg4 bool + }{arg1, arg2, arg3, arg4}) + fake.recordInvocation("Deprovision", []interface{}{arg1, arg2, arg3, arg4}) + fake.deprovisionMutex.Unlock() + if fake.DeprovisionStub != nil { + return fake.DeprovisionStub(arg1, arg2, arg3, arg4) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.deprovisionReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *AutoFakeServiceBroker) DeprovisionCallCount() int { + fake.deprovisionMutex.RLock() + defer fake.deprovisionMutex.RUnlock() + return len(fake.deprovisionArgsForCall) +} + +func (fake *AutoFakeServiceBroker) DeprovisionCalls(stub func(context.Context, string, brokerapi.DeprovisionDetails, bool) (brokerapi.DeprovisionServiceSpec, error)) { + fake.deprovisionMutex.Lock() + defer fake.deprovisionMutex.Unlock() + fake.DeprovisionStub = stub +} + +func (fake *AutoFakeServiceBroker) DeprovisionArgsForCall(i int) (context.Context, string, brokerapi.DeprovisionDetails, bool) { + fake.deprovisionMutex.RLock() + defer fake.deprovisionMutex.RUnlock() + argsForCall := fake.deprovisionArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4 +} + +func (fake *AutoFakeServiceBroker) DeprovisionReturns(result1 brokerapi.DeprovisionServiceSpec, result2 error) { + fake.deprovisionMutex.Lock() + defer fake.deprovisionMutex.Unlock() + fake.DeprovisionStub = nil + fake.deprovisionReturns = struct { + result1 brokerapi.DeprovisionServiceSpec + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) DeprovisionReturnsOnCall(i int, result1 brokerapi.DeprovisionServiceSpec, result2 error) { + fake.deprovisionMutex.Lock() + defer fake.deprovisionMutex.Unlock() + fake.DeprovisionStub = nil + if fake.deprovisionReturnsOnCall == nil { + fake.deprovisionReturnsOnCall = make(map[int]struct { + result1 brokerapi.DeprovisionServiceSpec + result2 error + }) + } + fake.deprovisionReturnsOnCall[i] = struct { + result1 brokerapi.DeprovisionServiceSpec + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) GetBinding(arg1 context.Context, arg2 string, arg3 string) (brokerapi.GetBindingSpec, error) { + fake.getBindingMutex.Lock() + ret, specificReturn := fake.getBindingReturnsOnCall[len(fake.getBindingArgsForCall)] + fake.getBindingArgsForCall = append(fake.getBindingArgsForCall, struct { + arg1 context.Context + arg2 string + arg3 string + }{arg1, arg2, arg3}) + fake.recordInvocation("GetBinding", []interface{}{arg1, arg2, arg3}) + fake.getBindingMutex.Unlock() + if fake.GetBindingStub != nil { + return fake.GetBindingStub(arg1, arg2, arg3) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.getBindingReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *AutoFakeServiceBroker) GetBindingCallCount() int { + fake.getBindingMutex.RLock() + defer fake.getBindingMutex.RUnlock() + return len(fake.getBindingArgsForCall) +} + +func (fake *AutoFakeServiceBroker) GetBindingCalls(stub func(context.Context, string, string) (brokerapi.GetBindingSpec, error)) { + fake.getBindingMutex.Lock() + defer fake.getBindingMutex.Unlock() + fake.GetBindingStub = stub +} + +func (fake *AutoFakeServiceBroker) GetBindingArgsForCall(i int) (context.Context, string, string) { + fake.getBindingMutex.RLock() + defer fake.getBindingMutex.RUnlock() + argsForCall := fake.getBindingArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3 +} + +func (fake *AutoFakeServiceBroker) GetBindingReturns(result1 brokerapi.GetBindingSpec, result2 error) { + fake.getBindingMutex.Lock() + defer fake.getBindingMutex.Unlock() + fake.GetBindingStub = nil + fake.getBindingReturns = struct { + result1 brokerapi.GetBindingSpec + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) GetBindingReturnsOnCall(i int, result1 brokerapi.GetBindingSpec, result2 error) { + fake.getBindingMutex.Lock() + defer fake.getBindingMutex.Unlock() + fake.GetBindingStub = nil + if fake.getBindingReturnsOnCall == nil { + fake.getBindingReturnsOnCall = make(map[int]struct { + result1 brokerapi.GetBindingSpec + result2 error + }) + } + fake.getBindingReturnsOnCall[i] = struct { + result1 brokerapi.GetBindingSpec + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) GetInstance(arg1 context.Context, arg2 string) (brokerapi.GetInstanceDetailsSpec, error) { + fake.getInstanceMutex.Lock() + ret, specificReturn := fake.getInstanceReturnsOnCall[len(fake.getInstanceArgsForCall)] + fake.getInstanceArgsForCall = append(fake.getInstanceArgsForCall, struct { + arg1 context.Context + arg2 string + }{arg1, arg2}) + fake.recordInvocation("GetInstance", []interface{}{arg1, arg2}) + fake.getInstanceMutex.Unlock() + if fake.GetInstanceStub != nil { + return fake.GetInstanceStub(arg1, arg2) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.getInstanceReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *AutoFakeServiceBroker) GetInstanceCallCount() int { + fake.getInstanceMutex.RLock() + defer fake.getInstanceMutex.RUnlock() + return len(fake.getInstanceArgsForCall) +} + +func (fake *AutoFakeServiceBroker) GetInstanceCalls(stub func(context.Context, string) (brokerapi.GetInstanceDetailsSpec, error)) { + fake.getInstanceMutex.Lock() + defer fake.getInstanceMutex.Unlock() + fake.GetInstanceStub = stub +} + +func (fake *AutoFakeServiceBroker) GetInstanceArgsForCall(i int) (context.Context, string) { + fake.getInstanceMutex.RLock() + defer fake.getInstanceMutex.RUnlock() + argsForCall := fake.getInstanceArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2 +} + +func (fake *AutoFakeServiceBroker) GetInstanceReturns(result1 brokerapi.GetInstanceDetailsSpec, result2 error) { + fake.getInstanceMutex.Lock() + defer fake.getInstanceMutex.Unlock() + fake.GetInstanceStub = nil + fake.getInstanceReturns = struct { + result1 brokerapi.GetInstanceDetailsSpec + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) GetInstanceReturnsOnCall(i int, result1 brokerapi.GetInstanceDetailsSpec, result2 error) { + fake.getInstanceMutex.Lock() + defer fake.getInstanceMutex.Unlock() + fake.GetInstanceStub = nil + if fake.getInstanceReturnsOnCall == nil { + fake.getInstanceReturnsOnCall = make(map[int]struct { + result1 brokerapi.GetInstanceDetailsSpec + result2 error + }) + } + fake.getInstanceReturnsOnCall[i] = struct { + result1 brokerapi.GetInstanceDetailsSpec + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) LastBindingOperation(arg1 context.Context, arg2 string, arg3 string, arg4 brokerapi.PollDetails) (brokerapi.LastOperation, error) { + fake.lastBindingOperationMutex.Lock() + ret, specificReturn := fake.lastBindingOperationReturnsOnCall[len(fake.lastBindingOperationArgsForCall)] + fake.lastBindingOperationArgsForCall = append(fake.lastBindingOperationArgsForCall, struct { + arg1 context.Context + arg2 string + arg3 string + arg4 brokerapi.PollDetails + }{arg1, arg2, arg3, arg4}) + fake.recordInvocation("LastBindingOperation", []interface{}{arg1, arg2, arg3, arg4}) + fake.lastBindingOperationMutex.Unlock() + if fake.LastBindingOperationStub != nil { + return fake.LastBindingOperationStub(arg1, arg2, arg3, arg4) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.lastBindingOperationReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *AutoFakeServiceBroker) LastBindingOperationCallCount() int { + fake.lastBindingOperationMutex.RLock() + defer fake.lastBindingOperationMutex.RUnlock() + return len(fake.lastBindingOperationArgsForCall) +} + +func (fake *AutoFakeServiceBroker) LastBindingOperationCalls(stub func(context.Context, string, string, brokerapi.PollDetails) (brokerapi.LastOperation, error)) { + fake.lastBindingOperationMutex.Lock() + defer fake.lastBindingOperationMutex.Unlock() + fake.LastBindingOperationStub = stub +} + +func (fake *AutoFakeServiceBroker) LastBindingOperationArgsForCall(i int) (context.Context, string, string, brokerapi.PollDetails) { + fake.lastBindingOperationMutex.RLock() + defer fake.lastBindingOperationMutex.RUnlock() + argsForCall := fake.lastBindingOperationArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4 +} + +func (fake *AutoFakeServiceBroker) LastBindingOperationReturns(result1 brokerapi.LastOperation, result2 error) { + fake.lastBindingOperationMutex.Lock() + defer fake.lastBindingOperationMutex.Unlock() + fake.LastBindingOperationStub = nil + fake.lastBindingOperationReturns = struct { + result1 brokerapi.LastOperation + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) LastBindingOperationReturnsOnCall(i int, result1 brokerapi.LastOperation, result2 error) { + fake.lastBindingOperationMutex.Lock() + defer fake.lastBindingOperationMutex.Unlock() + fake.LastBindingOperationStub = nil + if fake.lastBindingOperationReturnsOnCall == nil { + fake.lastBindingOperationReturnsOnCall = make(map[int]struct { + result1 brokerapi.LastOperation + result2 error + }) + } + fake.lastBindingOperationReturnsOnCall[i] = struct { + result1 brokerapi.LastOperation + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) LastOperation(arg1 context.Context, arg2 string, arg3 brokerapi.PollDetails) (brokerapi.LastOperation, error) { + fake.lastOperationMutex.Lock() + ret, specificReturn := fake.lastOperationReturnsOnCall[len(fake.lastOperationArgsForCall)] + fake.lastOperationArgsForCall = append(fake.lastOperationArgsForCall, struct { + arg1 context.Context + arg2 string + arg3 brokerapi.PollDetails + }{arg1, arg2, arg3}) + fake.recordInvocation("LastOperation", []interface{}{arg1, arg2, arg3}) + fake.lastOperationMutex.Unlock() + if fake.LastOperationStub != nil { + return fake.LastOperationStub(arg1, arg2, arg3) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.lastOperationReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *AutoFakeServiceBroker) LastOperationCallCount() int { + fake.lastOperationMutex.RLock() + defer fake.lastOperationMutex.RUnlock() + return len(fake.lastOperationArgsForCall) +} + +func (fake *AutoFakeServiceBroker) LastOperationCalls(stub func(context.Context, string, brokerapi.PollDetails) (brokerapi.LastOperation, error)) { + fake.lastOperationMutex.Lock() + defer fake.lastOperationMutex.Unlock() + fake.LastOperationStub = stub +} + +func (fake *AutoFakeServiceBroker) LastOperationArgsForCall(i int) (context.Context, string, brokerapi.PollDetails) { + fake.lastOperationMutex.RLock() + defer fake.lastOperationMutex.RUnlock() + argsForCall := fake.lastOperationArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3 +} + +func (fake *AutoFakeServiceBroker) LastOperationReturns(result1 brokerapi.LastOperation, result2 error) { + fake.lastOperationMutex.Lock() + defer fake.lastOperationMutex.Unlock() + fake.LastOperationStub = nil + fake.lastOperationReturns = struct { + result1 brokerapi.LastOperation + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) LastOperationReturnsOnCall(i int, result1 brokerapi.LastOperation, result2 error) { + fake.lastOperationMutex.Lock() + defer fake.lastOperationMutex.Unlock() + fake.LastOperationStub = nil + if fake.lastOperationReturnsOnCall == nil { + fake.lastOperationReturnsOnCall = make(map[int]struct { + result1 brokerapi.LastOperation + result2 error + }) + } + fake.lastOperationReturnsOnCall[i] = struct { + result1 brokerapi.LastOperation + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) Provision(arg1 context.Context, arg2 string, arg3 brokerapi.ProvisionDetails, arg4 bool) (brokerapi.ProvisionedServiceSpec, error) { + fake.provisionMutex.Lock() + ret, specificReturn := fake.provisionReturnsOnCall[len(fake.provisionArgsForCall)] + fake.provisionArgsForCall = append(fake.provisionArgsForCall, struct { + arg1 context.Context + arg2 string + arg3 brokerapi.ProvisionDetails + arg4 bool + }{arg1, arg2, arg3, arg4}) + fake.recordInvocation("Provision", []interface{}{arg1, arg2, arg3, arg4}) + fake.provisionMutex.Unlock() + if fake.ProvisionStub != nil { + return fake.ProvisionStub(arg1, arg2, arg3, arg4) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.provisionReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *AutoFakeServiceBroker) ProvisionCallCount() int { + fake.provisionMutex.RLock() + defer fake.provisionMutex.RUnlock() + return len(fake.provisionArgsForCall) +} + +func (fake *AutoFakeServiceBroker) ProvisionCalls(stub func(context.Context, string, brokerapi.ProvisionDetails, bool) (brokerapi.ProvisionedServiceSpec, error)) { + fake.provisionMutex.Lock() + defer fake.provisionMutex.Unlock() + fake.ProvisionStub = stub +} + +func (fake *AutoFakeServiceBroker) ProvisionArgsForCall(i int) (context.Context, string, brokerapi.ProvisionDetails, bool) { + fake.provisionMutex.RLock() + defer fake.provisionMutex.RUnlock() + argsForCall := fake.provisionArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4 +} + +func (fake *AutoFakeServiceBroker) ProvisionReturns(result1 brokerapi.ProvisionedServiceSpec, result2 error) { + fake.provisionMutex.Lock() + defer fake.provisionMutex.Unlock() + fake.ProvisionStub = nil + fake.provisionReturns = struct { + result1 brokerapi.ProvisionedServiceSpec + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) ProvisionReturnsOnCall(i int, result1 brokerapi.ProvisionedServiceSpec, result2 error) { + fake.provisionMutex.Lock() + defer fake.provisionMutex.Unlock() + fake.ProvisionStub = nil + if fake.provisionReturnsOnCall == nil { + fake.provisionReturnsOnCall = make(map[int]struct { + result1 brokerapi.ProvisionedServiceSpec + result2 error + }) + } + fake.provisionReturnsOnCall[i] = struct { + result1 brokerapi.ProvisionedServiceSpec + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) Services(arg1 context.Context) ([]brokerapi.Service, error) { + fake.servicesMutex.Lock() + ret, specificReturn := fake.servicesReturnsOnCall[len(fake.servicesArgsForCall)] + fake.servicesArgsForCall = append(fake.servicesArgsForCall, struct { + arg1 context.Context + }{arg1}) + fake.recordInvocation("Services", []interface{}{arg1}) + fake.servicesMutex.Unlock() + if fake.ServicesStub != nil { + return fake.ServicesStub(arg1) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.servicesReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *AutoFakeServiceBroker) ServicesCallCount() int { + fake.servicesMutex.RLock() + defer fake.servicesMutex.RUnlock() + return len(fake.servicesArgsForCall) +} + +func (fake *AutoFakeServiceBroker) ServicesCalls(stub func(context.Context) ([]brokerapi.Service, error)) { + fake.servicesMutex.Lock() + defer fake.servicesMutex.Unlock() + fake.ServicesStub = stub +} + +func (fake *AutoFakeServiceBroker) ServicesArgsForCall(i int) context.Context { + fake.servicesMutex.RLock() + defer fake.servicesMutex.RUnlock() + argsForCall := fake.servicesArgsForCall[i] + return argsForCall.arg1 +} + +func (fake *AutoFakeServiceBroker) ServicesReturns(result1 []brokerapi.Service, result2 error) { + fake.servicesMutex.Lock() + defer fake.servicesMutex.Unlock() + fake.ServicesStub = nil + fake.servicesReturns = struct { + result1 []brokerapi.Service + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) ServicesReturnsOnCall(i int, result1 []brokerapi.Service, result2 error) { + fake.servicesMutex.Lock() + defer fake.servicesMutex.Unlock() + fake.ServicesStub = nil + if fake.servicesReturnsOnCall == nil { + fake.servicesReturnsOnCall = make(map[int]struct { + result1 []brokerapi.Service + result2 error + }) + } + fake.servicesReturnsOnCall[i] = struct { + result1 []brokerapi.Service + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) Unbind(arg1 context.Context, arg2 string, arg3 string, arg4 brokerapi.UnbindDetails, arg5 bool) (brokerapi.UnbindSpec, error) { + fake.unbindMutex.Lock() + ret, specificReturn := fake.unbindReturnsOnCall[len(fake.unbindArgsForCall)] + fake.unbindArgsForCall = append(fake.unbindArgsForCall, struct { + arg1 context.Context + arg2 string + arg3 string + arg4 brokerapi.UnbindDetails + arg5 bool + }{arg1, arg2, arg3, arg4, arg5}) + fake.recordInvocation("Unbind", []interface{}{arg1, arg2, arg3, arg4, arg5}) + fake.unbindMutex.Unlock() + if fake.UnbindStub != nil { + return fake.UnbindStub(arg1, arg2, arg3, arg4, arg5) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.unbindReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *AutoFakeServiceBroker) UnbindCallCount() int { + fake.unbindMutex.RLock() + defer fake.unbindMutex.RUnlock() + return len(fake.unbindArgsForCall) +} + +func (fake *AutoFakeServiceBroker) UnbindCalls(stub func(context.Context, string, string, brokerapi.UnbindDetails, bool) (brokerapi.UnbindSpec, error)) { + fake.unbindMutex.Lock() + defer fake.unbindMutex.Unlock() + fake.UnbindStub = stub +} + +func (fake *AutoFakeServiceBroker) UnbindArgsForCall(i int) (context.Context, string, string, brokerapi.UnbindDetails, bool) { + fake.unbindMutex.RLock() + defer fake.unbindMutex.RUnlock() + argsForCall := fake.unbindArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4, argsForCall.arg5 +} + +func (fake *AutoFakeServiceBroker) UnbindReturns(result1 brokerapi.UnbindSpec, result2 error) { + fake.unbindMutex.Lock() + defer fake.unbindMutex.Unlock() + fake.UnbindStub = nil + fake.unbindReturns = struct { + result1 brokerapi.UnbindSpec + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) UnbindReturnsOnCall(i int, result1 brokerapi.UnbindSpec, result2 error) { + fake.unbindMutex.Lock() + defer fake.unbindMutex.Unlock() + fake.UnbindStub = nil + if fake.unbindReturnsOnCall == nil { + fake.unbindReturnsOnCall = make(map[int]struct { + result1 brokerapi.UnbindSpec + result2 error + }) + } + fake.unbindReturnsOnCall[i] = struct { + result1 brokerapi.UnbindSpec + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) Update(arg1 context.Context, arg2 string, arg3 brokerapi.UpdateDetails, arg4 bool) (brokerapi.UpdateServiceSpec, error) { + fake.updateMutex.Lock() + ret, specificReturn := fake.updateReturnsOnCall[len(fake.updateArgsForCall)] + fake.updateArgsForCall = append(fake.updateArgsForCall, struct { + arg1 context.Context + arg2 string + arg3 brokerapi.UpdateDetails + arg4 bool + }{arg1, arg2, arg3, arg4}) + fake.recordInvocation("Update", []interface{}{arg1, arg2, arg3, arg4}) + fake.updateMutex.Unlock() + if fake.UpdateStub != nil { + return fake.UpdateStub(arg1, arg2, arg3, arg4) + } + if specificReturn { + return ret.result1, ret.result2 + } + fakeReturns := fake.updateReturns + return fakeReturns.result1, fakeReturns.result2 +} + +func (fake *AutoFakeServiceBroker) UpdateCallCount() int { + fake.updateMutex.RLock() + defer fake.updateMutex.RUnlock() + return len(fake.updateArgsForCall) +} + +func (fake *AutoFakeServiceBroker) UpdateCalls(stub func(context.Context, string, brokerapi.UpdateDetails, bool) (brokerapi.UpdateServiceSpec, error)) { + fake.updateMutex.Lock() + defer fake.updateMutex.Unlock() + fake.UpdateStub = stub +} + +func (fake *AutoFakeServiceBroker) UpdateArgsForCall(i int) (context.Context, string, brokerapi.UpdateDetails, bool) { + fake.updateMutex.RLock() + defer fake.updateMutex.RUnlock() + argsForCall := fake.updateArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4 +} + +func (fake *AutoFakeServiceBroker) UpdateReturns(result1 brokerapi.UpdateServiceSpec, result2 error) { + fake.updateMutex.Lock() + defer fake.updateMutex.Unlock() + fake.UpdateStub = nil + fake.updateReturns = struct { + result1 brokerapi.UpdateServiceSpec + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) UpdateReturnsOnCall(i int, result1 brokerapi.UpdateServiceSpec, result2 error) { + fake.updateMutex.Lock() + defer fake.updateMutex.Unlock() + fake.UpdateStub = nil + if fake.updateReturnsOnCall == nil { + fake.updateReturnsOnCall = make(map[int]struct { + result1 brokerapi.UpdateServiceSpec + result2 error + }) + } + fake.updateReturnsOnCall[i] = struct { + result1 brokerapi.UpdateServiceSpec + result2 error + }{result1, result2} +} + +func (fake *AutoFakeServiceBroker) Invocations() map[string][][]interface{} { + fake.invocationsMutex.RLock() + defer fake.invocationsMutex.RUnlock() + fake.bindMutex.RLock() + defer fake.bindMutex.RUnlock() + fake.deprovisionMutex.RLock() + defer fake.deprovisionMutex.RUnlock() + fake.getBindingMutex.RLock() + defer fake.getBindingMutex.RUnlock() + fake.getInstanceMutex.RLock() + defer fake.getInstanceMutex.RUnlock() + fake.lastBindingOperationMutex.RLock() + defer fake.lastBindingOperationMutex.RUnlock() + fake.lastOperationMutex.RLock() + defer fake.lastOperationMutex.RUnlock() + fake.provisionMutex.RLock() + defer fake.provisionMutex.RUnlock() + fake.servicesMutex.RLock() + defer fake.servicesMutex.RUnlock() + fake.unbindMutex.RLock() + defer fake.unbindMutex.RUnlock() + fake.updateMutex.RLock() + defer fake.updateMutex.RUnlock() + copiedInvocations := map[string][][]interface{}{} + for key, value := range fake.invocations { + copiedInvocations[key] = value + } + return copiedInvocations +} + +func (fake *AutoFakeServiceBroker) recordInvocation(key string, args []interface{}) { + fake.invocationsMutex.Lock() + defer fake.invocationsMutex.Unlock() + if fake.invocations == nil { + fake.invocations = map[string][][]interface{}{} + } + if fake.invocations[key] == nil { + fake.invocations[key] = [][]interface{}{} + } + fake.invocations[key] = append(fake.invocations[key], args) +} + +var _ brokerapi.ServiceBroker = new(AutoFakeServiceBroker) diff --git a/middlewares/originating_identity_header/originating_identity_header.go b/middlewares/originating_identity_header/originating_identity_header.go new file mode 100644 index 00000000..e75a046f --- /dev/null +++ b/middlewares/originating_identity_header/originating_identity_header.go @@ -0,0 +1,34 @@ +// Copyright (C) 2015-Present Pivotal Software, Inc. All rights reserved. + +// This program and the accompanying materials are made available under +// the terms of the under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package originating_identity_header + +import ( + "context" + "net/http" +) + +const ( + originatingIdentityKey = "originatingIdentity" +) + +func AddToContext(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + originatingIdentity := req.Header.Get("X-Broker-API-Originating-Identity") + newCtx := context.WithValue(req.Context(), originatingIdentityKey, originatingIdentity) + next.ServeHTTP(w, req.WithContext(newCtx)) + }) +} + diff --git a/service_broker.go b/service_broker.go index 45ff091d..93af5f30 100644 --- a/service_broker.go +++ b/service_broker.go @@ -22,6 +22,8 @@ import ( "net/http" ) +//go:generate counterfeiter -o fakes/auto_fake_service_broker.go -fake-name AutoFakeServiceBroker . ServiceBroker + type ServiceBroker interface { Services(ctx context.Context) ([]Service, error)