diff --git a/internal/broker/bind_create.go b/internal/broker/bind_create.go index 5b2889ad40..9fa3b34171 100644 --- a/internal/broker/bind_create.go +++ b/internal/broker/bind_create.go @@ -39,6 +39,33 @@ type BindEndpoint struct { log logrus.FieldLogger } +type BindingContext struct { + Email *string `json:"email,omitempty"` + Origin *string `json:"origin,omitempty"` +} + +func (b *BindingContext) CreatedBy() string { + if b.Email == nil && b.Origin == nil { + return "" + } + + email := "" + if b.Email != nil { + email = *b.Email + } + + origin := "" + if b.Origin != nil { + origin = *b.Origin + } + + if email != "" && origin != "" { + return email + " " + origin + } + + return email + origin +} + type BindingParams struct { ServiceAccount bool `json:"service_account,omit"` ExpirationSeconds int `json:"expiration_seconds,omit"` @@ -95,6 +122,15 @@ func (b *BindEndpoint) Bind(ctx context.Context, instanceID, bindingID string, d ).WithErrorKey("BindingNotSupported").Build() } + var bindingContext BindingContext + if len(details.RawContext) != 0 { + err = json.Unmarshal(details.RawContext, &bindingContext) + if err != nil { + message := fmt.Sprintf("failed to unmarshal context: %s", err) + return domain.Binding{}, apiresponses.NewFailureResponse(fmt.Errorf(message), http.StatusBadRequest, message) + } + } + var parameters BindingParams if len(details.RawParameters) != 0 { err = json.Unmarshal(details.RawParameters, ¶meters) @@ -127,6 +163,7 @@ func (b *BindEndpoint) Bind(ctx context.Context, instanceID, bindingID string, d UpdatedAt: time.Now(), ExpirationSeconds: int64(expirationSeconds), + CreatedBy: bindingContext.CreatedBy(), } if parameters.ServiceAccount { // get kubeconfig for the instance diff --git a/internal/broker/bind_create_test.go b/internal/broker/bind_create_test.go index f0db01a22f..88a7f8ccf2 100644 --- a/internal/broker/bind_create_test.go +++ b/internal/broker/bind_create_test.go @@ -192,6 +192,10 @@ func TestCreateBindingEndpoint(t *testing.T) { "plan_id": "%s", "parameters": { "service_account": true + }, + "context": { + "email": "john.smith@email.com", + "origin": "origin" } }`, fixture.PlanId), t) defer response.Body.Close() @@ -380,6 +384,59 @@ func TestCreateBindingEndpoint(t *testing.T) { }) } +func TestCreatedBy(t *testing.T) { + emptyStr := "" + email := "john.smith@email.com" + origin := "origin" + tests := []struct { + name string + context BindingContext + expected string + }{ + { + name: "Both Email and Origin are nil", + context: BindingContext{Email: nil, Origin: nil}, + expected: "", + }, + { + name: "Both Email and Origin are empty", + context: BindingContext{Email: &emptyStr, Origin: &emptyStr}, + expected: "", + }, + { + name: "Origin is nil", + context: BindingContext{Email: &email, Origin: nil}, + expected: "john.smith@email.com", + }, + { + name: "Origin is empty", + context: BindingContext{Email: &email, Origin: &emptyStr}, + expected: "john.smith@email.com", + }, + { + name: "Email is nil", + context: BindingContext{Email: nil, Origin: &origin}, + expected: "origin", + }, + { + name: "Email is empty", + context: BindingContext{Email: &emptyStr, Origin: &origin}, + expected: "origin", + }, + { + name: "Both Email and Origin are set", + context: BindingContext{Email: &email, Origin: &origin}, + expected: "john.smith@email.com origin", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.expected, tt.context.CreatedBy()) + }) + } +} + func assertClusterAccess(t *testing.T, response *http.Response, controlSecretName string, binding domain.Binding) { credentials, ok := binding.Credentials.(map[string]interface{}) diff --git a/internal/fixture/fixture.go b/internal/fixture/fixture.go index 4fe317a902..fc0ed67fbc 100644 --- a/internal/fixture/fixture.go +++ b/internal/fixture/fixture.go @@ -360,6 +360,7 @@ func FixBindingWithInstanceID(bindingID string, instanceID string) internal.Bind Kubeconfig: "kubeconfig", ExpirationSeconds: 600, BindingType: internal.BINDING_TYPE_SERVICE_ACCOUNT, + CreatedBy: "john.smith@email.com", } } diff --git a/internal/model.go b/internal/model.go index 8db21cdfcd..6922c3d3d4 100644 --- a/internal/model.go +++ b/internal/model.go @@ -577,4 +577,5 @@ type Binding struct { Kubeconfig string ExpirationSeconds int64 BindingType string + CreatedBy string } diff --git a/internal/storage/dbmodel/binding.go b/internal/storage/dbmodel/binding.go index 68fbce0846..8f8f7a6aa5 100644 --- a/internal/storage/dbmodel/binding.go +++ b/internal/storage/dbmodel/binding.go @@ -14,4 +14,5 @@ type BindingDTO struct { Kubeconfig string ExpirationSeconds int64 BindingType string + CreatedBy string } diff --git a/internal/storage/driver/postsql/binding.go b/internal/storage/driver/postsql/binding.go index 7699424bbb..53fb0978ae 100644 --- a/internal/storage/driver/postsql/binding.go +++ b/internal/storage/driver/postsql/binding.go @@ -91,6 +91,7 @@ func (s *Binding) toBindingDTO(binding *internal.Binding) (dbmodel.BindingDTO, e InstanceID: binding.InstanceID, CreatedAt: binding.CreatedAt, ExpirationSeconds: binding.ExpirationSeconds, + CreatedBy: binding.CreatedBy, }, nil } @@ -106,5 +107,6 @@ func (s *Binding) toBinding(dto dbmodel.BindingDTO) (internal.Binding, error) { InstanceID: dto.InstanceID, CreatedAt: dto.CreatedAt, ExpirationSeconds: dto.ExpirationSeconds, + CreatedBy: dto.CreatedBy, }, nil } diff --git a/internal/storage/driver/postsql/binding_test.go b/internal/storage/driver/postsql/binding_test.go index e784486256..f4094221fa 100644 --- a/internal/storage/driver/postsql/binding_test.go +++ b/internal/storage/driver/postsql/binding_test.go @@ -40,6 +40,7 @@ func TestBinding(t *testing.T) { assert.Equal(t, fixedBinding.ExpirationSeconds, createdBinding.ExpirationSeconds) assert.NotNil(t, createdBinding.Kubeconfig) assert.Equal(t, fixedBinding.Kubeconfig, createdBinding.Kubeconfig) + assert.Equal(t, fixedBinding.CreatedBy, createdBinding.CreatedBy) // when err = brokerStorage.Bindings().DeleteByBindingID(testBindingId) diff --git a/internal/storage/postsql/write.go b/internal/storage/postsql/write.go index 0d67b1116f..5a5f9b337b 100644 --- a/internal/storage/postsql/write.go +++ b/internal/storage/postsql/write.go @@ -41,6 +41,7 @@ func (ws writeSession) InsertBinding(binding dbmodel.BindingDTO) dberr.Error { Pair("kubeconfig", binding.Kubeconfig). Pair("expiration_seconds", binding.ExpirationSeconds). Pair("binding_type", binding.BindingType). + Pair("created_by", binding.CreatedBy). Exec() if err != nil { diff --git a/resources/keb/migrations/202410111300_add_created_by_to_binding.down.sql b/resources/keb/migrations/202410111300_add_created_by_to_binding.down.sql new file mode 100644 index 0000000000..a21a811963 --- /dev/null +++ b/resources/keb/migrations/202410111300_add_created_by_to_binding.down.sql @@ -0,0 +1 @@ +ALTER TABLE bindings DROP COLUMN created_by; diff --git a/resources/keb/migrations/202410111300_add_created_by_to_binding.up.sql b/resources/keb/migrations/202410111300_add_created_by_to_binding.up.sql new file mode 100644 index 0000000000..d51c7ba306 --- /dev/null +++ b/resources/keb/migrations/202410111300_add_created_by_to_binding.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE bindings + ADD COLUMN created_by VARCHAR(255);