From 81e8f20591ad966d0d0c82167c55ed1e417b888f Mon Sep 17 00:00:00 2001 From: den-rgb Date: Fri, 22 Nov 2024 15:59:19 +0000 Subject: [PATCH] OCM-12442 | feat: Added github hostname validation --- provider/identityprovider/github.go | 28 +++++++++------------------- tests/e2e/idps_test.go | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/provider/identityprovider/github.go b/provider/identityprovider/github.go index c315b6a6..4838f6cb 100644 --- a/provider/identityprovider/github.go +++ b/provider/identityprovider/github.go @@ -3,7 +3,6 @@ package identityprovider import ( "context" "fmt" - "net/url" "strings" "github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" @@ -88,30 +87,21 @@ func githubTeamsFormatValidator() validator.String { func githubHostnameValidator() validator.String { return attrvalidators.NewStringValidator("hostname validator", func(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) { hostname := req.ConfigValue - // Validate hostname - if !hostname.IsUnknown() && !hostname.IsNull() && len(hostname.ValueString()) > 0 { - hostnameStr := hostname.ValueString() - if hostnameStr == "" { - return - } - if hostnameStr == "github.com" || strings.HasSuffix(hostnameStr, ".github.com") { - resp.Diagnostics.AddAttributeError(req.Path, "invalid hostname", + hostnameStr := hostname.ValueString() + if hostnameStr == "" { + return + } + if hostnameStr == "github.com" || strings.HasSuffix(hostnameStr, ".github.com") { + resp.Diagnostics.AddAttributeError(req.Path, "invalid hostname", fmt.Sprintf("'%s' hostname cannot be equal to [*.]github.com", hostnameStr), ) return - } - if !(len(validation.IsDNS1123Subdomain(hostnameStr)) == 0 || netutils.ParseIPSloppy(hostnameStr) != nil) { - resp.Diagnostics.AddAttributeError(req.Path, "invalid hostname", + } + if !(len(validation.IsDNS1123Subdomain(hostnameStr)) == 0 || netutils.ParseIPSloppy(hostnameStr) != nil) { + resp.Diagnostics.AddAttributeError(req.Path, "invalid hostname", fmt.Sprintf("'%s' hostname must be a valid DNS subdomain or IP address", hostnameStr), ) return - } - _, err := url.ParseRequestURI(hostnameStr) - if err != nil { - resp.Diagnostics.AddAttributeError(req.Path, "invalid hostname", - fmt.Sprintf("Expected a valid GitHub hostname. Got %v", hostnameStr), - ) - } } }) } diff --git a/tests/e2e/idps_test.go b/tests/e2e/idps_test.go index 11edc151..71612608 100644 --- a/tests/e2e/idps_test.go +++ b/tests/e2e/idps_test.go @@ -566,6 +566,11 @@ var _ = Describe("Identity Providers", ci.Day2, ci.FeatureIDP, func() { } } + validateIDPArgAgainstNoError := func(svc exec.IDPService, idpArgs *exec.IDPArgs) { + _, err := svc.Apply(idpArgs) + Expect(err).ToNot(HaveOccurred()) + } + It("the mandatory idp's attributes must be set - [id:68939]", ci.Medium, func() { var err error idpServices.htpasswd, err = profileHandler.Services().GetIDPService(constants.IDPHTPassword) @@ -695,6 +700,16 @@ var _ = Describe("Identity Providers", ci.Day2, ci.FeatureIDP, func() { args.HostedDomain = helper.StringPointer(" invalid hostname ") validateIDPArgAgainstErrorSubstrings(idpServices.github, args, "hostname must be a valid DNS subdomain or IP address") + By("Create github idp with hostname myhost.com/aa") + args = getDefaultGitHubArgs(idpName) + args.HostedDomain = helper.StringPointer("myhost.com/aa") + validateIDPArgAgainstNoError(idpServices.github, args) + + By("Create github idp with hostname example.com") + args = getDefaultGitHubArgs(idpName) + args.HostedDomain = helper.StringPointer("example.com") + validateIDPArgAgainstNoError(idpServices.github, args) + By("Create github idp with empty hostname") args = getDefaultGitHubArgs(idpName) args.HostedDomain = helper.EmptyStringPointer