-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial DB Table * Structures Extension * Linter * DAO Implementation * Removed Commented Out Consts * Unnecessary Filter * Linter * Attributes Correction * DAO Test * Typo Correction * Corrected Scripts Names and Added RuntimeID Index * Changed Runtime Id to Instance Id During code review I received input that instance id is the main identity value for SKRs in KEB. * Corrected Param Value in Example Token Request * Removed Old Retry * Formatting Corrections * Linter * Spacing Correction * Removed Unused Mocks Generation * Additional Test Cases * Removed Redundant Version Field * Corrected Test Case * Corrected Text to Varchar for Type Field * Corrected Type Field * Missed runtimeID Correction * Review Remarks * Naming Corrections * Review Remarks * Compilation Correction * Compilation Correction * ListByInstanceID * Linter * Corrected Const Name * Corrected Const Name --------- Co-authored-by: Marek Michali <56163696+MarekMichali@users.noreply.github.com> Co-authored-by: Piotr Miśkiewicz <piotr.miskiewicz@sap.com>
- Loading branch information
1 parent
d4ddae9
commit 1947428
Showing
15 changed files
with
492 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package dbmodel | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type BindingDTO struct { | ||
ID string | ||
InstanceID string | ||
|
||
CreatedAt time.Time | ||
|
||
Kubeconfig string | ||
ExpirationSeconds int64 | ||
GenerationMethod string | ||
BindingType string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package postsql | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/kyma-project/kyma-environment-broker/internal" | ||
"github.com/kyma-project/kyma-environment-broker/internal/storage/dberr" | ||
"github.com/kyma-project/kyma-environment-broker/internal/storage/dbmodel" | ||
"github.com/kyma-project/kyma-environment-broker/internal/storage/postsql" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type Binding struct { | ||
postsql.Factory | ||
cipher Cipher | ||
} | ||
|
||
func NewBinding(sess postsql.Factory, cipher Cipher) *Binding { | ||
return &Binding{ | ||
Factory: sess, | ||
cipher: cipher, | ||
} | ||
} | ||
|
||
func (s *Binding) GetByBindingID(bindingId string) (*internal.Binding, error) { | ||
sess := s.NewReadSession() | ||
bindingDTO := dbmodel.BindingDTO{} | ||
bindingDTO, lastErr := sess.GetBindingByID(bindingId) | ||
if lastErr != nil { | ||
if dberr.IsNotFound(lastErr) { | ||
return nil, dberr.NotFound("Binding with id %s not exist", bindingId) | ||
} | ||
log.Errorf("while getting instanceDTO by ID %s: %v", bindingId, lastErr) | ||
return nil, lastErr | ||
} | ||
binding, err := s.toBinding(bindingDTO) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &binding, nil | ||
} | ||
|
||
func (s *Binding) Insert(binding *internal.Binding) error { | ||
_, err := s.GetByBindingID(binding.ID) | ||
if err == nil { | ||
return dberr.AlreadyExists("instance with id %s already exist", binding.ID) | ||
} | ||
|
||
dto, err := s.toBindingDTO(binding) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
sess := s.NewWriteSession() | ||
err = sess.InsertBinding(dto) | ||
if err != nil { | ||
return fmt.Errorf("while saving binding with ID %s: %w", binding.ID, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *Binding) DeleteByBindingID(ID string) error { | ||
sess := s.NewWriteSession() | ||
return sess.DeleteBinding(ID) | ||
} | ||
|
||
func (s *Binding) ListByInstanceID(instanceID string) ([]internal.Binding, error) { | ||
dtos, err := s.NewReadSession().ListBindings(instanceID) | ||
if err != nil { | ||
return []internal.Binding{}, err | ||
} | ||
var bindings []internal.Binding | ||
for _, dto := range dtos { | ||
instance, err := s.toBinding(dto) | ||
if err != nil { | ||
return []internal.Binding{}, err | ||
} | ||
|
||
bindings = append(bindings, instance) | ||
} | ||
return bindings, err | ||
} | ||
|
||
func (s *Binding) toBindingDTO(binding *internal.Binding) (dbmodel.BindingDTO, error) { | ||
encrypted, err := s.cipher.Encrypt([]byte(binding.Kubeconfig)) | ||
if err != nil { | ||
return dbmodel.BindingDTO{}, fmt.Errorf("while encrypting kubeconfig: %w", err) | ||
} | ||
|
||
return dbmodel.BindingDTO{ | ||
Kubeconfig: string(encrypted), | ||
ID: binding.ID, | ||
InstanceID: binding.InstanceID, | ||
CreatedAt: binding.CreatedAt, | ||
ExpirationSeconds: binding.ExpirationSeconds, | ||
}, nil | ||
} | ||
|
||
func (s *Binding) toBinding(dto dbmodel.BindingDTO) (internal.Binding, error) { | ||
decrypted, err := s.cipher.Decrypt([]byte(dto.Kubeconfig)) | ||
if err != nil { | ||
return internal.Binding{}, fmt.Errorf("while decrypting kubeconfig: %w", err) | ||
} | ||
|
||
return internal.Binding{ | ||
Kubeconfig: string(decrypted), | ||
ID: dto.ID, | ||
InstanceID: dto.InstanceID, | ||
CreatedAt: dto.CreatedAt, | ||
ExpirationSeconds: dto.ExpirationSeconds, | ||
}, nil | ||
} |
Oops, something went wrong.