Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
EVEREST-271 RFC-1035 error messages (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
oksana-grishchenko authored Sep 14, 2023
1 parent 22e3b49 commit 72a120c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion api-tests/tests/backup-storages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ test('create backup storage failures', async ({ request }) => {
accessKey: 'ssdssd',
secretKey: 'ssdssdssdssd',
},
errorText: '\'name\' is not RFC 1123 compatible',
errorText: '\'name\' is not RFC 1035 compatible',
},
{
payload: {
Expand Down
4 changes: 2 additions & 2 deletions api/database_cluster_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
// ListDatabaseClusterBackups returns list of the created database cluster backups on the specified kubernetes cluster.
func (e *EverestServer) ListDatabaseClusterBackups(ctx echo.Context, kubernetesID string, name string) error {
req := ctx.Request()
if err := validateRFC1123(name, "name"); err != nil {
return ctx.JSON(http.StatusBadRequest, Error{Message: pointer.ToString("Cluster name is not RFC 1123 compatible")})
if err := validateRFC1035(name, "name"); err != nil {
return ctx.JSON(http.StatusBadRequest, Error{Message: pointer.ToString(err.Error())})
}
val := url.Values{}
val.Add("labelSelector", fmt.Sprintf("clusterName=%s", name))
Expand Down
4 changes: 2 additions & 2 deletions api/database_cluster_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
// ListDatabaseClusterRestores List of the created database cluster restores on the specified kubernetes cluster.
func (e *EverestServer) ListDatabaseClusterRestores(ctx echo.Context, kubernetesID string, name string) error {
req := ctx.Request()
if err := validateRFC1123(name, "name"); err != nil {
return ctx.JSON(http.StatusBadRequest, Error{Message: pointer.ToString("Cluster name is not RFC 1123 compatible")})
if err := validateRFC1035(name, "name"); err != nil {
return ctx.JSON(http.StatusBadRequest, Error{Message: pointer.ToString(err.Error())})
}
val := url.Values{}
val.Add("labelSelector", fmt.Sprintf("clusterName=%s", name))
Expand Down
26 changes: 14 additions & 12 deletions api/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ var (
errDBCNameWrongFormat = errors.New("DatabaseCluster's metadata.name should be a string")
)

// ErrNameNotRFC1123Compatible when the given fieldName doesn't contain RFC 1123 compatible string.
func ErrNameNotRFC1123Compatible(fieldName string) error {
return errors.Errorf("'%s' is not RFC 1123 compatible. Please use only lowercase alphanumeric characters or '-'", fieldName)
// ErrNameNotRFC1035Compatible when the given fieldName doesn't contain RFC 1035 compatible string.
func ErrNameNotRFC1035Compatible(fieldName string) error {
return errors.Errorf(`'%s' is not RFC 1035 compatible. The name should contain only lowercase alphanumeric characters or '-', start with an alphabetic character, end with an alphanumeric character`,
fieldName,
)
}

// ErrNameTooLong when the given fieldName is longer than expected.
Expand All @@ -67,18 +69,18 @@ func ErrInvalidURL(fieldName string) error {
return errors.Errorf("'%s' is an invalid URL", fieldName)
}

// validates names to be RFC-1123 compatible https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-label-names
func validateRFC1123(s, name string) error {
// We are diverging from the RFC1123 spec in regards to the length of the
// validates names to be RFC-1035 compatible https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#rfc-1035-label-names
func validateRFC1035(s, name string) error {
// We are diverging from the RFC1035 spec in regards to the length of the
// name because the PXC operator limits the name of the cluster to 22.
if len(s) > 22 {
return ErrNameTooLong(name)
}

rfc1123Regex := "^[a-z]([-a-z0-9]{0,61}[a-z0-9])?$"
re := regexp.MustCompile(rfc1123Regex)
rfc1035Regex := "^[a-z]([-a-z0-9]{0,61}[a-z0-9])?$"
re := regexp.MustCompile(rfc1035Regex)
if !re.MatchString(s) {
return ErrNameNotRFC1123Compatible(name)
return ErrNameNotRFC1035Compatible(name)
}

return nil
Expand Down Expand Up @@ -192,7 +194,7 @@ func validateCreateBackupStorageRequest(ctx echo.Context, l *zap.SugaredLogger)
return nil, err
}

if err := validateRFC1123(params.Name, "name"); err != nil {
if err := validateRFC1035(params.Name, "name"); err != nil {
return nil, err
}

Expand All @@ -218,7 +220,7 @@ func validateCreateMonitoringInstanceRequest(ctx echo.Context) (*CreateMonitorin
return nil, err
}

if err := validateRFC1123(params.Name, "name"); err != nil {
if err := validateRFC1035(params.Name, "name"); err != nil {
return nil, err
}

Expand Down Expand Up @@ -297,7 +299,7 @@ func validateCreateDatabaseClusterRequest(dbc DatabaseCluster) error {
return errDBCNameWrongFormat
}

return validateRFC1123(strName, "metadata.name")
return validateRFC1035(strName, "metadata.name")
}

func (e *EverestServer) validateDBClusterAccess(ctx echo.Context, kubernetesID, dbClusterName string) error {
Expand Down
12 changes: 6 additions & 6 deletions api/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestValidateRFC1123(t *testing.T) {
func TestValidateRFC1035(t *testing.T) {
t.Parallel()
type testCase struct {
value string
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestValidateRFC1123(t *testing.T) {
c := tc
t.Run(c.value, func(t *testing.T) {
t.Parallel()
require.Equal(t, c.valid, validateRFC1123(c.value, "") == nil)
require.Equal(t, c.valid, validateRFC1035(c.value, "") == nil)
})
}
}
Expand Down Expand Up @@ -107,28 +107,28 @@ func TestValidateCreateDatabaseClusterRequest(t *testing.T) {
value: DatabaseCluster{Metadata: &map[string]interface{}{
"name": "",
}},
err: ErrNameNotRFC1123Compatible("metadata.name"),
err: ErrNameNotRFC1035Compatible("metadata.name"),
},
{
name: "starts with -",
value: DatabaseCluster{Metadata: &map[string]interface{}{
"name": "-sdfasa",
}},
err: ErrNameNotRFC1123Compatible("metadata.name"),
err: ErrNameNotRFC1035Compatible("metadata.name"),
},
{
name: "ends with -",
value: DatabaseCluster{Metadata: &map[string]interface{}{
"name": "sdfasa-",
}},
err: ErrNameNotRFC1123Compatible("metadata.name"),
err: ErrNameNotRFC1035Compatible("metadata.name"),
},
{
name: "contains uppercase",
value: DatabaseCluster{Metadata: &map[string]interface{}{
"name": "AAsdf",
}},
err: ErrNameNotRFC1123Compatible("metadata.name"),
err: ErrNameNotRFC1035Compatible("metadata.name"),
},
{
name: "valid",
Expand Down

0 comments on commit 72a120c

Please sign in to comment.