From e2f7bd284f030be967619422a5b2cbef45d443af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20M=C3=BCller?= Date: Thu, 27 Oct 2022 14:12:47 +0200 Subject: [PATCH] Site accounts & Mentix updates (#3404) --- changelog/unreleased/siteacc-mentix-upd.md | 5 ++ .../exchangers/exporters/cs3api/normalize.go | 61 +++++++++++++++++++ .../exchangers/exporters/cs3api/query.go | 18 +++--- .../panels/account/contact/template.go | 1 - pkg/siteacc/panels/account/edit/template.go | 1 - pkg/siteacc/panels/account/login/template.go | 1 - .../panels/account/registration/template.go | 1 - .../panels/account/settings/template.go | 1 - pkg/siteacc/panels/account/sites/template.go | 1 - 9 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 changelog/unreleased/siteacc-mentix-upd.md create mode 100644 pkg/mentix/exchangers/exporters/cs3api/normalize.go diff --git a/changelog/unreleased/siteacc-mentix-upd.md b/changelog/unreleased/siteacc-mentix-upd.md new file mode 100644 index 0000000000..66c0a3506a --- /dev/null +++ b/changelog/unreleased/siteacc-mentix-upd.md @@ -0,0 +1,5 @@ +Enhancement: Site accounts & Mentix updates + +Some small improvements to the Site Accounts and Mentix services, including normalization of data exposed at the `/cs3` endpoint of Mentix. + +https://github.com/cs3org/reva/pull/3404 diff --git a/pkg/mentix/exchangers/exporters/cs3api/normalize.go b/pkg/mentix/exchangers/exporters/cs3api/normalize.go new file mode 100644 index 0000000000..2cc4999e91 --- /dev/null +++ b/pkg/mentix/exchangers/exporters/cs3api/normalize.go @@ -0,0 +1,61 @@ +// Copyright 2018-2021 CERN +// +// Licensed 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. +// +// In applying this license, CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +package cs3api + +import ( + "net/url" + "regexp" + + "github.com/rs/zerolog" +) + +func _normalizeAddress(addr string) (*url.URL, error) { + address := addr + + // See if the address includes a protocol; if not, add a default one + re := regexp.MustCompile(".+://.+") + if !re.MatchString(address) { + address = "https://" + address + } + + // Parse the address as a URL + addressURL, err := url.Parse(address) + if err != nil { + return nil, err + } + return addressURL, nil +} + +func normalizeHost(domain string, log *zerolog.Logger) string { + address, err := _normalizeAddress(domain) + if err != nil { + log.Error().Msgf("unable to parse host %v", domain) + return domain + } + return address.Hostname() +} + +func normalizeURLPath(path string, log *zerolog.Logger) string { + address, err := _normalizeAddress(path) + if err != nil { + log.Error().Msgf("unable to parse URL %v", path) + return path + } + return address.String() +} diff --git a/pkg/mentix/exchangers/exporters/cs3api/query.go b/pkg/mentix/exchangers/exporters/cs3api/query.go index 0c18a39ab3..6f7154e827 100755 --- a/pkg/mentix/exchangers/exporters/cs3api/query.go +++ b/pkg/mentix/exchangers/exporters/cs3api/query.go @@ -34,9 +34,9 @@ import ( ) // HandleDefaultQuery processes a basic query. -func HandleDefaultQuery(meshData *meshdata.MeshData, params url.Values, conf *config.Configuration, _ *zerolog.Logger) (int, []byte, error) { +func HandleDefaultQuery(meshData *meshdata.MeshData, params url.Values, conf *config.Configuration, log *zerolog.Logger) (int, []byte, error) { // Convert the mesh data - ocmData, err := convertMeshDataToOCMData(meshData, conf.Exporters.CS3API.ElevatedServiceTypes) + ocmData, err := convertMeshDataToOCMData(meshData, conf.Exporters.CS3API.ElevatedServiceTypes, log) if err != nil { return http.StatusBadRequest, []byte{}, fmt.Errorf("unable to convert the mesh data to OCM data structures: %v", err) } @@ -50,7 +50,7 @@ func HandleDefaultQuery(meshData *meshdata.MeshData, params url.Values, conf *co return http.StatusOK, data, nil } -func convertMeshDataToOCMData(meshData *meshdata.MeshData, elevatedServiceTypes []string) ([]*ocmprovider.ProviderInfo, error) { +func convertMeshDataToOCMData(meshData *meshdata.MeshData, elevatedServiceTypes []string, log *zerolog.Logger) ([]*ocmprovider.ProviderInfo, error) { // Convert the mesh data into the corresponding OCM data structures providers := make([]*ocmprovider.ProviderInfo, 0, len(meshData.Operators)*3) for _, op := range meshData.Operators { @@ -60,8 +60,8 @@ func convertMeshDataToOCMData(meshData *meshdata.MeshData, elevatedServiceTypes addService := func(host string, endpoint *meshdata.ServiceEndpoint, addEndpoints []*ocmprovider.ServiceEndpoint, apiVersion string) { services = append(services, &ocmprovider.Service{ - Host: host, - Endpoint: convertServiceEndpointToOCMData(endpoint), + Host: normalizeHost(host, log), + Endpoint: convertServiceEndpointToOCMData(endpoint, log), AdditionalEndpoints: addEndpoints, ApiVersion: apiVersion, }) @@ -77,7 +77,7 @@ func convertMeshDataToOCMData(meshData *meshdata.MeshData, elevatedServiceTypes endpointURL, _ := url.Parse(endpoint.URL) addService(endpointURL.Host, endpoint, nil, apiVersion) } else { - addEndpoints = append(addEndpoints, convertServiceEndpointToOCMData(endpoint)) + addEndpoints = append(addEndpoints, convertServiceEndpointToOCMData(endpoint, log)) } } @@ -90,7 +90,7 @@ func convertMeshDataToOCMData(meshData *meshdata.MeshData, elevatedServiceTypes FullName: site.FullName, Description: site.Description, Organization: site.Organization, - Domain: site.Domain, + Domain: normalizeHost(site.Domain, log), Homepage: site.Homepage, Email: site.Email, Services: services, @@ -103,14 +103,14 @@ func convertMeshDataToOCMData(meshData *meshdata.MeshData, elevatedServiceTypes return providers, nil } -func convertServiceEndpointToOCMData(endpoint *meshdata.ServiceEndpoint) *ocmprovider.ServiceEndpoint { +func convertServiceEndpointToOCMData(endpoint *meshdata.ServiceEndpoint, log *zerolog.Logger) *ocmprovider.ServiceEndpoint { return &ocmprovider.ServiceEndpoint{ Type: &ocmprovider.ServiceType{ Name: endpoint.Type.Name, Description: endpoint.Type.Description, }, Name: endpoint.Name, - Path: endpoint.URL, + Path: normalizeURLPath(endpoint.URL, log), IsMonitored: endpoint.IsMonitored, Properties: endpoint.Properties, } diff --git a/pkg/siteacc/panels/account/contact/template.go b/pkg/siteacc/panels/account/contact/template.go index ff53bf9854..d1456fc58b 100644 --- a/pkg/siteacc/panels/account/contact/template.go +++ b/pkg/siteacc/panels/account/contact/template.go @@ -100,7 +100,6 @@ const tplBody = ` Fields marked with * are mandatory.
-
diff --git a/pkg/siteacc/panels/account/edit/template.go b/pkg/siteacc/panels/account/edit/template.go index b82327b76e..e98cfaced4 100644 --- a/pkg/siteacc/panels/account/edit/template.go +++ b/pkg/siteacc/panels/account/edit/template.go @@ -147,7 +147,6 @@ const tplBody = ` Fields marked with * are mandatory.
-
diff --git a/pkg/siteacc/panels/account/login/template.go b/pkg/siteacc/panels/account/login/template.go index c47fe0edf2..a4363bc072 100644 --- a/pkg/siteacc/panels/account/login/template.go +++ b/pkg/siteacc/panels/account/login/template.go @@ -126,7 +126,6 @@ const tplBody = ` Fields marked with * are mandatory.
-
diff --git a/pkg/siteacc/panels/account/registration/template.go b/pkg/siteacc/panels/account/registration/template.go index 57832eb82c..16599fe1e8 100644 --- a/pkg/siteacc/panels/account/registration/template.go +++ b/pkg/siteacc/panels/account/registration/template.go @@ -175,7 +175,6 @@ const tplBody = ` Fields marked with * are mandatory.
-
diff --git a/pkg/siteacc/panels/account/settings/template.go b/pkg/siteacc/panels/account/settings/template.go index 75b1aaa4ad..9724108a37 100644 --- a/pkg/siteacc/panels/account/settings/template.go +++ b/pkg/siteacc/panels/account/settings/template.go @@ -82,7 +82,6 @@ const tplBody = `
-
diff --git a/pkg/siteacc/panels/account/sites/template.go b/pkg/siteacc/panels/account/sites/template.go index e390dcf153..355a2fe74c 100644 --- a/pkg/siteacc/panels/account/sites/template.go +++ b/pkg/siteacc/panels/account/sites/template.go @@ -126,7 +126,6 @@ const tplBody = ` Fields marked with * are mandatory.
-