From efa57c0a13264e8f072c0f25260014b0c3d02865 Mon Sep 17 00:00:00 2001 From: Derrick <76881293+D-B-Hawk@users.noreply.github.com> Date: Fri, 19 Jan 2024 14:02:42 -0800 Subject: [PATCH] =?UTF-8?q?add=20post=20route=20to=20validate=20digital=20?= =?UTF-8?q?ocean=20domain.=20re-creates=20checks=20us=E2=80=A6=20(#279)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add post route to validate digital ocean domain. re-creates checks used in cluster controller. run go fmt on project * replace civo response with digital ocean response --- internal/router/api/v1/digitalocean.go | 52 ++++++++++++++++++++++++++ internal/router/api/v1/secrets.go | 6 +-- internal/router/router.go | 6 +-- internal/types/validation.go | 2 +- pkg/constants/cloudProviderDefaults.go | 2 +- pkg/k8s/secret.go | 4 +- pkg/k8s/types.go | 2 +- 7 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 internal/router/api/v1/digitalocean.go diff --git a/internal/router/api/v1/digitalocean.go b/internal/router/api/v1/digitalocean.go new file mode 100644 index 00000000..0a9abf45 --- /dev/null +++ b/internal/router/api/v1/digitalocean.go @@ -0,0 +1,52 @@ +/* +Copyright (C) 2021-2024, Kubefirst + +This program is licensed under MIT. +See the LICENSE file for more details. +*/ +package api + +import ( + "context" + "net/http" + + "github.com/gin-gonic/gin" + "github.com/kubefirst/kubefirst-api/internal/types" + "github.com/kubefirst/runtime/pkg/digitalocean" +) + +func PostValidateDigitalOceanDomain(c *gin.Context) { + domainName, exists := c.Params.Get("domain") + if !exists { + c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ + Message: ":domain parameter not provided in request", + }) + return + } + + var request types.DigitalOceanDomainValidationRequest + err := c.Bind(&request) + if err != nil { + c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ + Message: err.Error(), + }) + return + } + + digitaloceanConf := digitalocean.DigitaloceanConfiguration{ + Client: digitalocean.NewDigitalocean(request.Token), + Context: context.Background(), + } + + validated := digitaloceanConf.TestDomainLiveness(domainName) + if !validated { + c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ + Message: "domain validation failed", + }) + return + } + + c.JSON(http.StatusOK, types.DigitalOceanDomainValidationResponse{ + Validated: validated, + }) +} diff --git a/internal/router/api/v1/secrets.go b/internal/router/api/v1/secrets.go index 252442da..55295b46 100644 --- a/internal/router/api/v1/secrets.go +++ b/internal/router/api/v1/secrets.go @@ -16,7 +16,6 @@ import ( "github.com/kubefirst/kubefirst-api/pkg/k8s" ) - func GetClusterSecret(c *gin.Context) { clusterName, param := c.Params.Get("cluster_name") if !param { @@ -46,7 +45,7 @@ func GetClusterSecret(c *gin.Context) { kcfg := k8s.CreateKubeConfig(false, fmt.Sprintf("%s/kubeconfig", clusterDir)) - secrets, err := k8s.ReadSecretV2(kcfg.Clientset,"kubefirst",secret) + secrets, err := k8s.ReadSecretV2(kcfg.Clientset, "kubefirst", secret) if err != nil { c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ Message: err.Error(), @@ -95,7 +94,7 @@ func UpdateClusterSecret(c *gin.Context) { kcfg := k8s.CreateKubeConfig(false, fmt.Sprintf("%s/kubeconfig", clusterDir)) - err = k8s.UpdateSecretV2(kcfg.Clientset,"kubefirst",secret, clusterSecretUpdates) + err = k8s.UpdateSecretV2(kcfg.Clientset, "kubefirst", secret, clusterSecretUpdates) if err != nil { c.JSON(http.StatusBadRequest, types.JSONFailureResponse{ Message: err.Error(), @@ -107,4 +106,3 @@ func UpdateClusterSecret(c *gin.Context) { Message: "cluster secret updated", }) } - diff --git a/internal/router/router.go b/internal/router/router.go index 2da13e49..c4570830 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -54,7 +54,7 @@ func SetupRouter() *gin.Engine { v1.GET("/cluster/:cluster_name/export", middleware.ValidateAPIKey(), router.GetExportCluster) v1.POST("/cluster/:cluster_name/reset_progress", middleware.ValidateAPIKey(), router.PostResetClusterProgress) v1.POST("/cluster/:cluster_name/:cloud_provider/kubeconfig", middleware.ValidateAPIKey(), router.GetClusterKubeconfig) - + // Cluster Secret v1.GET("/secret/:cluster_name/:secret", router.GetClusterSecret) v1.POST("/secret/:cluster_name/:secret", router.UpdateClusterSecret) @@ -72,7 +72,7 @@ func SetupRouter() *gin.Engine { v1.POST("/domain/:dns_provider", middleware.ValidateAPIKey(), router.PostDomains) v1.GET("/domain/validate/aws/:domain", middleware.ValidateAPIKey(), router.GetValidateAWSDomain) v1.GET("/domain/validate/civo/:domain", middleware.ValidateAPIKey(), router.GetValidateCivoDomain) - // v1.GET("/domain/validate/digitalocean/:domain", middleware.ValidateAPIKey(), router.GetValidateDigitalOceanDomain) + v1.POST("/domain/validate/digitalocean/:domain", middleware.ValidateAPIKey(), router.PostValidateDigitalOceanDomain) // v1.GET("/domain/validate/vultr/:domain", middleware.ValidateAPIKey(), router.GetValidateVultrDomain) // v1.GET("/domain/validate/google/:domain", middleware.ValidateAPIKey(), router.GetValidateGoogleDomain) // Regions @@ -99,8 +99,6 @@ func SetupRouter() *gin.Engine { // Event streaming v1.GET("/stream", router.GetLogs) - - // Telemetry v1.POST("/telemetry/:cluster_name", middleware.ValidateAPIKey(), router.PostTelemetry) } diff --git a/internal/types/validation.go b/internal/types/validation.go index aa8e3eb2..0cc09126 100644 --- a/internal/types/validation.go +++ b/internal/types/validation.go @@ -28,7 +28,7 @@ type CivoDomainValidationResponse struct { // DigitalOceanDomainValidationRequest /digitalocean/domain/validate required parameters type DigitalOceanDomainValidationRequest struct { - CloudRegion string `json:"cloud_region"` + Token string `json:"token"` } // DigitalOceanDomainValidationResponse is the response for the /digitalocean/domain/validate route diff --git a/pkg/constants/cloudProviderDefaults.go b/pkg/constants/cloudProviderDefaults.go index 523e9e3c..94fedffa 100644 --- a/pkg/constants/cloudProviderDefaults.go +++ b/pkg/constants/cloudProviderDefaults.go @@ -13,4 +13,4 @@ var cloudProviderDefaults = types.CloudProviderDefaults{ func GetCloudDefaults() types.CloudProviderDefaults { return cloudProviderDefaults -} \ No newline at end of file +} diff --git a/pkg/k8s/secret.go b/pkg/k8s/secret.go index 834c487a..5b49cf7d 100644 --- a/pkg/k8s/secret.go +++ b/pkg/k8s/secret.go @@ -57,13 +57,13 @@ func UpdateSecretV2(clientset *kubernetes.Clientset, namespace string, secretNam // create map to iterate over values to change secretsToUpdateMap := make(map[string]string) - err = json.Unmarshal(secretsToUpdate,&secretsToUpdateMap) + err = json.Unmarshal(secretsToUpdate, &secretsToUpdateMap) if err != nil { return err } currentSecret, err := clientset.CoreV1().Secrets(namespace).Get(context.Background(), secretName, metav1.GetOptions{}) - if err != nil{ + if err != nil { return err } diff --git a/pkg/k8s/types.go b/pkg/k8s/types.go index 92a943c5..63cb4ac9 100644 --- a/pkg/k8s/types.go +++ b/pkg/k8s/types.go @@ -13,4 +13,4 @@ type KubernetesClient struct { Clientset *kubernetes.Clientset RestConfig *rest.Config KubeConfigPath string -} \ No newline at end of file +}