Skip to content

Commit

Permalink
avoid URI parsing errors for elasticsearch bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesClonk committed Nov 14, 2022
1 parent ceea3cb commit e74ca03
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions _fixtures/env
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion service/elasticsearch/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 8 additions & 0 deletions service/elasticsearch/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion service/elasticsearch/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
5 changes: 3 additions & 2 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions service/vcap_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit e74ca03

Please sign in to comment.