From e74ca03cb56c36726e19855ef016199537cf46b2 Mon Sep 17 00:00:00 2001 From: Fabio Berchtold Date: Mon, 14 Nov 2022 10:28:36 +0100 Subject: [PATCH] avoid URI parsing errors for elasticsearch bindings --- Makefile | 2 +- _fixtures/env | 4 ++-- service/elasticsearch/backup.go | 4 +++- service/elasticsearch/binding.go | 8 ++++++++ service/elasticsearch/restore.go | 4 +++- service/service.go | 5 +++-- service/vcap_services.go | 3 +++ 7 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index daca42f..df963a3 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ run: .PHONY: gin ## gin: runs main.go via gin (hot reloading) gin: - source _fixtures/env; source _fixtures/env_private; gin --all --immediate run main.go + source _fixtures/env; source _fixtures/env_private; gin --all --immediate run main.go .PHONY: build ## build: builds the application diff --git a/_fixtures/env b/_fixtures/env index 294ae2c..f283176 100644 --- a/_fixtures/env +++ b/_fixtures/env @@ -87,9 +87,9 @@ export VCAP_SERVICES='{ "elasticsearch":[{ "binding_name": null, "credentials": { - "full_access_password": "yolo", + "full_access_password": "0Really-Yes%5-xyz(+abc", "full_access_username": "yolo", - "host": "https://yolo.elasticsearch.lyra-836.appcloud.swisscom.com", + "host": "https://0c061730-1b19-424b-8efd-349fd40957a0.yolo.elasticsearch.lyra-836.appcloud.swisscom.com:443", "kibana_read_only_password": "yolo", "kibana_read_only_username": "yolo", "kibana_system_password": "yolo", diff --git a/service/elasticsearch/backup.go b/service/elasticsearch/backup.go index 7a7f965..8d54d4a 100644 --- a/service/elasticsearch/backup.go +++ b/service/elasticsearch/backup.go @@ -30,7 +30,9 @@ func Backup(ctx context.Context, s3 *s3.Client, service config.Service, filename state.BackupStart(service, filename) - u, _ := url.Parse(service.Binding.Host) + u, _ := url.Parse(service.Binding.URI) + log.Debugf("elasticsearch: preparing connectstring for [%s://%s]", u.Scheme, u.Host) + connectstring := fmt.Sprintf("%s://%s:%s@%s", u.Scheme, url.PathEscape(service.Binding.Username), url.PathEscape(service.Binding.Password), u.Host) objectPath := fmt.Sprintf("%s/%s/%s", service.Binding.Type, service.Name, filename) diff --git a/service/elasticsearch/binding.go b/service/elasticsearch/binding.go index 8c24d93..246d039 100644 --- a/service/elasticsearch/binding.go +++ b/service/elasticsearch/binding.go @@ -12,6 +12,14 @@ func VerifyBinding(service config.Service) bool { log.Errorf("service binding for [%s] is missing property: [host]", service.Name) valid = false } + if service.Binding.Port == 0 { + log.Errorf("service binding for [%s] is missing property: [port]", service.Name) + valid = false + } + if len(service.Binding.URI) == 0 { + log.Errorf("service binding for [%s] is missing property: [uri]", service.Name) + valid = false + } if len(service.Binding.Username) == 0 { log.Errorf("service binding for [%s] is missing property: [username]", service.Name) valid = false diff --git a/service/elasticsearch/restore.go b/service/elasticsearch/restore.go index ff496c8..0d72a0f 100644 --- a/service/elasticsearch/restore.go +++ b/service/elasticsearch/restore.go @@ -27,7 +27,9 @@ func Restore(ctx context.Context, s3 *s3.Client, service config.Service, target filename := filepath.Base(objectPath) state.RestoreStart(service, filename) - u, _ := url.Parse(target.Binding.Host) + u, _ := url.Parse(target.Binding.URI) + log.Debugf("elasticsearch: preparing connectstring for [%s://%s]", u.Scheme, u.Host) + connectstring := fmt.Sprintf("%s://%s:%s@%s", u.Scheme, url.PathEscape(target.Binding.Username), diff --git a/service/service.go b/service/service.go index 9918e07..a6e7203 100644 --- a/service/service.go +++ b/service/service.go @@ -57,6 +57,9 @@ func validateServices() { // enrich service bindings, try to figure out additional properties by parsing URI service.Binding = enrichBinding(service.Binding) + // service name must be the same as map-key + service.Name = serviceName + // validate binding credentials for known service types // each of them knows best themselves what they need or require validBinding := true @@ -78,8 +81,6 @@ func validateServices() { continue } - service.Name = serviceName // service name must be the same as map-key - // read timeout for service if service.Timeout.Seconds() <= 1 { service.Timeout.Duration = 1 * time.Hour // default diff --git a/service/vcap_services.go b/service/vcap_services.go index 14e237b..d662fd4 100644 --- a/service/vcap_services.go +++ b/service/vcap_services.go @@ -145,6 +145,9 @@ func parseVCAPServices() ([]config.Service, error) { if len(binding.Host) == 0 { binding.Host = binding.URI } + if len(binding.URI) == 0 { + binding.URI = binding.Host + } case config.MongoDB: port, _ := vcapService.CredentialString("port")