From e95521f05a802db374201441f68067bf99551cf4 Mon Sep 17 00:00:00 2001 From: Nedim Akar Date: Tue, 17 Oct 2023 14:55:22 +0200 Subject: [PATCH 1/8] feat: add errorz package to combine errors as enum in there --- src/jetstream/datastore/datastore.go | 7 +++++-- src/jetstream/errorz/errorz.go | 8 ++++++++ src/jetstream/middleware.go | 4 +++- src/jetstream/plugins/kubernetes/main.go | 5 +++-- src/jetstream/setup_console.go | 5 +++-- 5 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 src/jetstream/errorz/errorz.go diff --git a/src/jetstream/datastore/datastore.go b/src/jetstream/datastore/datastore.go index 37f0aaa620..1b4ee5b4b8 100644 --- a/src/jetstream/datastore/datastore.go +++ b/src/jetstream/datastore/datastore.go @@ -2,6 +2,7 @@ package datastore import ( "database/sql" + "errors" "fmt" "os" "path" @@ -9,6 +10,8 @@ import ( "strings" "time" + "github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz" + goosedbversion "github.com/cloudfoundry-incubator/stratos/src/jetstream/repository/goose-db-version" "github.com/govau/cf-common/env" log "github.com/sirupsen/logrus" @@ -301,9 +304,9 @@ func WaitForMigrations(db *sql.DB) error { databaseVersionRec, err := dbVersionRepo.GetCurrentVersion() if err != nil { var errorMsg = err.Error() - if strings.Contains(err.Error(), "no such table") { + if errors.Is(err, errorz.ErrNoSuchTable) { errorMsg = "Waiting for versions table to be created" - } else if strings.Contains(err.Error(), "No database versions found") { + } else if errors.Is(err, errorz.ErrNoDatabaseVersionsFound) { errorMsg = "Versions table is empty - waiting for migrations" } log.Infof("Database schema check: %s", errorMsg) diff --git a/src/jetstream/errorz/errorz.go b/src/jetstream/errorz/errorz.go new file mode 100644 index 0000000000..0dc53d2f79 --- /dev/null +++ b/src/jetstream/errorz/errorz.go @@ -0,0 +1,8 @@ +package errorz + +import "errors" + +var ErrDialTcp = errors.New("dial tcp") +var ErrNoSuchTable = errors.New("no such table") +var ErrNoDatabaseVersionsFound = errors.New("no database versions found") +var Err509Certificate = errors.New("x509: certificate") diff --git a/src/jetstream/middleware.go b/src/jetstream/middleware.go index 41d2dd6ad0..0bfa7f9288 100644 --- a/src/jetstream/middleware.go +++ b/src/jetstream/middleware.go @@ -10,6 +10,8 @@ import ( "strings" "time" + "github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz" + "github.com/gorilla/context" "github.com/govau/cf-common/env" "github.com/labstack/echo/v4" @@ -39,7 +41,7 @@ const APIKeyAuthScheme = "Bearer" func handleSessionError(config api.PortalConfig, c echo.Context, err error, doNotLog bool, msg string) error { log.Debug("handleSessionError") - if strings.Contains(err.Error(), "dial tcp") { + if errors.Is(err, errorz.ErrDialTcp) { return api.NewHTTPShadowError( http.StatusServiceUnavailable, "Service is currently unavailable", diff --git a/src/jetstream/plugins/kubernetes/main.go b/src/jetstream/plugins/kubernetes/main.go index 4026dd49dd..19616ef35b 100644 --- a/src/jetstream/plugins/kubernetes/main.go +++ b/src/jetstream/plugins/kubernetes/main.go @@ -7,10 +7,11 @@ import ( "net/http" "net/url" "strconv" - "strings" "errors" + "github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz" + "github.com/cloudfoundry-incubator/stratos/src/jetstream/api" "github.com/labstack/echo/v4" log "github.com/sirupsen/logrus" @@ -288,7 +289,7 @@ func (c *KubernetesSpecification) RequiresCert(ec echo.Context) error { Message string } if err != nil { - if strings.Contains(err.Error(), "x509: certificate") { + if errors.Is(err, errorz.Err509Certificate) { response.Status = http.StatusOK response.Required = true } else { diff --git a/src/jetstream/setup_console.go b/src/jetstream/setup_console.go index 5416b0b805..120980dd37 100644 --- a/src/jetstream/setup_console.go +++ b/src/jetstream/setup_console.go @@ -7,7 +7,8 @@ import ( "net/url" "regexp" "strconv" - "strings" + + "github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz" "github.com/govau/cf-common/env" "github.com/labstack/echo/v4" @@ -93,7 +94,7 @@ func (p *portalProxy) setupGetAvailableScopes(c echo.Context) error { errInfo, ok := err.(api.ErrHTTPRequest) if ok { if errInfo.Status == 0 { - if strings.Contains(errInfo.Error(), "x509: certificate") { + if errors.Is(errInfo, errorz.Err509Certificate) { return api.NewHTTPShadowError( http.StatusBadRequest, "Could not connect to the UAA - Certificate error - check Skip SSL validation setting", From 55c1b33edfb47bfe588b55b0506eb90a7a9d4057 Mon Sep 17 00:00:00 2001 From: Nedim Akar Date: Tue, 17 Oct 2023 15:43:13 +0200 Subject: [PATCH 2/8] feat: missed duplicated error --- .../repository/goose-db-version/pgsql_goose_db_version.go | 4 ++-- .../goose-db-version/pgsql_goose_db_version_test.go | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/jetstream/repository/goose-db-version/pgsql_goose_db_version.go b/src/jetstream/repository/goose-db-version/pgsql_goose_db_version.go index ef279f03d8..3008139556 100644 --- a/src/jetstream/repository/goose-db-version/pgsql_goose_db_version.go +++ b/src/jetstream/repository/goose-db-version/pgsql_goose_db_version.go @@ -2,12 +2,12 @@ package goosedbversion import ( "database/sql" - "errors" "fmt" log "github.com/sirupsen/logrus" "github.com/cloudfoundry-incubator/stratos/src/jetstream/api" + "github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz" ) const ( @@ -37,7 +37,7 @@ func (p *PostgresGooseDBVersionRepository) GetCurrentVersion() (api.GooseDBVersi switch { case err == sql.ErrNoRows: - return api.GooseDBVersionRecord{}, errors.New("No database versions found") + return api.GooseDBVersionRecord{}, errorz.ErrNoDatabaseVersionsFound case err != nil: return api.GooseDBVersionRecord{}, fmt.Errorf("Error trying to get current database version: %v", err) default: diff --git a/src/jetstream/repository/goose-db-version/pgsql_goose_db_version_test.go b/src/jetstream/repository/goose-db-version/pgsql_goose_db_version_test.go index c45f1799d1..db8dc65c5e 100644 --- a/src/jetstream/repository/goose-db-version/pgsql_goose_db_version_test.go +++ b/src/jetstream/repository/goose-db-version/pgsql_goose_db_version_test.go @@ -10,6 +10,7 @@ import ( . "github.com/smartystreets/goconvey/convey" "github.com/cloudfoundry-incubator/stratos/src/jetstream/api" + "github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz" ) func TestPgSQLGooseDB(t *testing.T) { @@ -63,9 +64,6 @@ func TestPgSQLGooseDB(t *testing.T) { }) Convey("if one doesn't exist", func() { - - expectedErrorMessage := "No database versions found" - // Database setup rs := sqlmock.NewRows(rowFieldsForVersionID) mock.ExpectQuery(selectFromDBVersionWhere). @@ -74,7 +72,7 @@ func TestPgSQLGooseDB(t *testing.T) { Convey("there should be an error", func() { repository, _ := NewPostgresGooseDBVersionRepository(db) _, err := repository.GetCurrentVersion() - So(err, ShouldResemble, errors.New(expectedErrorMessage)) + So(err, ShouldResemble, errorz.ErrNoDatabaseVersionsFound) dberr := mock.ExpectationsWereMet() So(dberr, ShouldBeNil) From 7ebe0bf532f16622e622be3a7ee228993f403080 Mon Sep 17 00:00:00 2001 From: Nedim Akar Date: Wed, 18 Oct 2023 14:18:26 +0200 Subject: [PATCH 3/8] fix: not worked error checks --- src/jetstream/errorz/errorz.go | 15 +++++++++++---- src/jetstream/middleware.go | 16 ++++++++++------ src/jetstream/plugins/kubernetes/main.go | 6 +++--- src/jetstream/setup_console.go | 6 +++--- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/jetstream/errorz/errorz.go b/src/jetstream/errorz/errorz.go index 0dc53d2f79..441ec2cf3a 100644 --- a/src/jetstream/errorz/errorz.go +++ b/src/jetstream/errorz/errorz.go @@ -2,7 +2,14 @@ package errorz import "errors" -var ErrDialTcp = errors.New("dial tcp") -var ErrNoSuchTable = errors.New("no such table") -var ErrNoDatabaseVersionsFound = errors.New("no database versions found") -var Err509Certificate = errors.New("x509: certificate") +const ( + ERR_DIAL_TCP = "dial" + ERR_NO_DATABASE_VERSIONS_FOUND = "no database versions found" + ERR_NO_SUCH_TABLE = "no such table" + ERR_X509_CERTIFICATE = "x509: certificate" +) + +var ErrDialTcp = errors.New(ERR_DIAL_TCP) +var ErrNoDatabaseVersionsFound = errors.New(ERR_NO_DATABASE_VERSIONS_FOUND) +var ErrNoSuchTable = errors.New(ERR_NO_SUCH_TABLE) +var Err509Certificate = errors.New(ERR_X509_CERTIFICATE) diff --git a/src/jetstream/middleware.go b/src/jetstream/middleware.go index 0bfa7f9288..f9ab85c47c 100644 --- a/src/jetstream/middleware.go +++ b/src/jetstream/middleware.go @@ -5,6 +5,7 @@ import ( "database/sql" "errors" "fmt" + "net" "net/http" "os" "strings" @@ -41,12 +42,15 @@ const APIKeyAuthScheme = "Bearer" func handleSessionError(config api.PortalConfig, c echo.Context, err error, doNotLog bool, msg string) error { log.Debug("handleSessionError") - if errors.Is(err, errorz.ErrDialTcp) { - return api.NewHTTPShadowError( - http.StatusServiceUnavailable, - "Service is currently unavailable", - "Service is currently unavailable: %v", err, - ) + var netOpErr *net.OpError + if errors.As(err, &netOpErr) { + if netOpErr.Op == errorz.ERR_DIAL_TCP { + return api.NewHTTPShadowError( + http.StatusServiceUnavailable, + "Service is currently unavailable", + "Service is currently unavailable: %v", err, + ) + } } if doNotLog { diff --git a/src/jetstream/plugins/kubernetes/main.go b/src/jetstream/plugins/kubernetes/main.go index 19616ef35b..2ab3accf64 100644 --- a/src/jetstream/plugins/kubernetes/main.go +++ b/src/jetstream/plugins/kubernetes/main.go @@ -7,12 +7,12 @@ import ( "net/http" "net/url" "strconv" + "strings" "errors" - "github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz" - "github.com/cloudfoundry-incubator/stratos/src/jetstream/api" + "github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz" "github.com/labstack/echo/v4" log "github.com/sirupsen/logrus" @@ -289,7 +289,7 @@ func (c *KubernetesSpecification) RequiresCert(ec echo.Context) error { Message string } if err != nil { - if errors.Is(err, errorz.Err509Certificate) { + if strings.Contains(err.Error(), errorz.ERR_X509_CERTIFICATE) { response.Status = http.StatusOK response.Required = true } else { diff --git a/src/jetstream/setup_console.go b/src/jetstream/setup_console.go index 120980dd37..6d73268821 100644 --- a/src/jetstream/setup_console.go +++ b/src/jetstream/setup_console.go @@ -7,8 +7,7 @@ import ( "net/url" "regexp" "strconv" - - "github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz" + "strings" "github.com/govau/cf-common/env" "github.com/labstack/echo/v4" @@ -18,6 +17,7 @@ import ( "github.com/cloudfoundry-incubator/stratos/src/jetstream/api" "github.com/cloudfoundry-incubator/stratos/src/jetstream/api/config" "github.com/cloudfoundry-incubator/stratos/src/jetstream/crypto" + "github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz" "github.com/cloudfoundry-incubator/stratos/src/jetstream/repository/console_config" "github.com/cloudfoundry-incubator/stratos/src/jetstream/repository/localusers" ) @@ -94,7 +94,7 @@ func (p *portalProxy) setupGetAvailableScopes(c echo.Context) error { errInfo, ok := err.(api.ErrHTTPRequest) if ok { if errInfo.Status == 0 { - if errors.Is(errInfo, errorz.Err509Certificate) { + if strings.Contains(errInfo.Error(), errorz.ERR_X509_CERTIFICATE) { return api.NewHTTPShadowError( http.StatusBadRequest, "Could not connect to the UAA - Certificate error - check Skip SSL validation setting", From b8cebbf11ee88e7c90287b80c4c424af05c83093 Mon Sep 17 00:00:00 2001 From: Nedim Akar Date: Wed, 18 Oct 2023 14:21:03 +0200 Subject: [PATCH 4/8] fix: not worked error checks --- src/jetstream/datastore/datastore.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jetstream/datastore/datastore.go b/src/jetstream/datastore/datastore.go index 1b4ee5b4b8..9de928cb34 100644 --- a/src/jetstream/datastore/datastore.go +++ b/src/jetstream/datastore/datastore.go @@ -304,7 +304,7 @@ func WaitForMigrations(db *sql.DB) error { databaseVersionRec, err := dbVersionRepo.GetCurrentVersion() if err != nil { var errorMsg = err.Error() - if errors.Is(err, errorz.ErrNoSuchTable) { + if strings.Contains(err.Error(), errorz.ERR_NO_SUCH_TABLE) { errorMsg = "Waiting for versions table to be created" } else if errors.Is(err, errorz.ErrNoDatabaseVersionsFound) { errorMsg = "Versions table is empty - waiting for migrations" From f6c07441fbde3a15c3e94e532b9607d335830e97 Mon Sep 17 00:00:00 2001 From: Nedim Akar Date: Tue, 31 Oct 2023 11:31:08 +0100 Subject: [PATCH 5/8] rename error package --- .../{errorz/errorz.go => custom_error/custom_error.go} | 2 +- src/jetstream/datastore/datastore.go | 6 +++--- src/jetstream/middleware.go | 4 ++-- src/jetstream/plugins/kubernetes/main.go | 4 ++-- .../repository/goose-db-version/pgsql_goose_db_version.go | 4 ++-- .../goose-db-version/pgsql_goose_db_version_test.go | 4 ++-- src/jetstream/setup_console.go | 4 ++-- 7 files changed, 14 insertions(+), 14 deletions(-) rename src/jetstream/{errorz/errorz.go => custom_error/custom_error.go} (95%) diff --git a/src/jetstream/errorz/errorz.go b/src/jetstream/custom_error/custom_error.go similarity index 95% rename from src/jetstream/errorz/errorz.go rename to src/jetstream/custom_error/custom_error.go index 441ec2cf3a..7c533c506a 100644 --- a/src/jetstream/errorz/errorz.go +++ b/src/jetstream/custom_error/custom_error.go @@ -1,4 +1,4 @@ -package errorz +package custom_error import "errors" diff --git a/src/jetstream/datastore/datastore.go b/src/jetstream/datastore/datastore.go index 9de928cb34..98a7983888 100644 --- a/src/jetstream/datastore/datastore.go +++ b/src/jetstream/datastore/datastore.go @@ -10,7 +10,7 @@ import ( "strings" "time" - "github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz" + "github.com/cloudfoundry-incubator/stratos/src/jetstream/custom_error" goosedbversion "github.com/cloudfoundry-incubator/stratos/src/jetstream/repository/goose-db-version" "github.com/govau/cf-common/env" @@ -304,9 +304,9 @@ func WaitForMigrations(db *sql.DB) error { databaseVersionRec, err := dbVersionRepo.GetCurrentVersion() if err != nil { var errorMsg = err.Error() - if strings.Contains(err.Error(), errorz.ERR_NO_SUCH_TABLE) { + if strings.Contains(err.Error(), custom_error.ERR_NO_SUCH_TABLE) { errorMsg = "Waiting for versions table to be created" - } else if errors.Is(err, errorz.ErrNoDatabaseVersionsFound) { + } else if errors.Is(err, custom_error.ErrNoDatabaseVersionsFound) { errorMsg = "Versions table is empty - waiting for migrations" } log.Infof("Database schema check: %s", errorMsg) diff --git a/src/jetstream/middleware.go b/src/jetstream/middleware.go index f9ab85c47c..c610f43048 100644 --- a/src/jetstream/middleware.go +++ b/src/jetstream/middleware.go @@ -11,7 +11,7 @@ import ( "strings" "time" - "github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz" + "github.com/cloudfoundry-incubator/stratos/src/jetstream/custom_error" "github.com/gorilla/context" "github.com/govau/cf-common/env" @@ -44,7 +44,7 @@ func handleSessionError(config api.PortalConfig, c echo.Context, err error, doNo var netOpErr *net.OpError if errors.As(err, &netOpErr) { - if netOpErr.Op == errorz.ERR_DIAL_TCP { + if netOpErr.Op == custom_error.ERR_DIAL_TCP { return api.NewHTTPShadowError( http.StatusServiceUnavailable, "Service is currently unavailable", diff --git a/src/jetstream/plugins/kubernetes/main.go b/src/jetstream/plugins/kubernetes/main.go index 2ab3accf64..b417345fc6 100644 --- a/src/jetstream/plugins/kubernetes/main.go +++ b/src/jetstream/plugins/kubernetes/main.go @@ -12,7 +12,7 @@ import ( "errors" "github.com/cloudfoundry-incubator/stratos/src/jetstream/api" - "github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz" + "github.com/cloudfoundry-incubator/stratos/src/jetstream/custom_error" "github.com/labstack/echo/v4" log "github.com/sirupsen/logrus" @@ -289,7 +289,7 @@ func (c *KubernetesSpecification) RequiresCert(ec echo.Context) error { Message string } if err != nil { - if strings.Contains(err.Error(), errorz.ERR_X509_CERTIFICATE) { + if strings.Contains(err.Error(), custom_error.ERR_X509_CERTIFICATE) { response.Status = http.StatusOK response.Required = true } else { diff --git a/src/jetstream/repository/goose-db-version/pgsql_goose_db_version.go b/src/jetstream/repository/goose-db-version/pgsql_goose_db_version.go index 3008139556..9c52ca33ac 100644 --- a/src/jetstream/repository/goose-db-version/pgsql_goose_db_version.go +++ b/src/jetstream/repository/goose-db-version/pgsql_goose_db_version.go @@ -7,7 +7,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/cloudfoundry-incubator/stratos/src/jetstream/api" - "github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz" + "github.com/cloudfoundry-incubator/stratos/src/jetstream/custom_error" ) const ( @@ -37,7 +37,7 @@ func (p *PostgresGooseDBVersionRepository) GetCurrentVersion() (api.GooseDBVersi switch { case err == sql.ErrNoRows: - return api.GooseDBVersionRecord{}, errorz.ErrNoDatabaseVersionsFound + return api.GooseDBVersionRecord{}, custom_error.ErrNoDatabaseVersionsFound case err != nil: return api.GooseDBVersionRecord{}, fmt.Errorf("Error trying to get current database version: %v", err) default: diff --git a/src/jetstream/repository/goose-db-version/pgsql_goose_db_version_test.go b/src/jetstream/repository/goose-db-version/pgsql_goose_db_version_test.go index db8dc65c5e..665c55f054 100644 --- a/src/jetstream/repository/goose-db-version/pgsql_goose_db_version_test.go +++ b/src/jetstream/repository/goose-db-version/pgsql_goose_db_version_test.go @@ -10,7 +10,7 @@ import ( . "github.com/smartystreets/goconvey/convey" "github.com/cloudfoundry-incubator/stratos/src/jetstream/api" - "github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz" + "github.com/cloudfoundry-incubator/stratos/src/jetstream/custom_error" ) func TestPgSQLGooseDB(t *testing.T) { @@ -72,7 +72,7 @@ func TestPgSQLGooseDB(t *testing.T) { Convey("there should be an error", func() { repository, _ := NewPostgresGooseDBVersionRepository(db) _, err := repository.GetCurrentVersion() - So(err, ShouldResemble, errorz.ErrNoDatabaseVersionsFound) + So(err, ShouldResemble, custom_error.ErrNoDatabaseVersionsFound) dberr := mock.ExpectationsWereMet() So(dberr, ShouldBeNil) diff --git a/src/jetstream/setup_console.go b/src/jetstream/setup_console.go index 6d73268821..c426317d78 100644 --- a/src/jetstream/setup_console.go +++ b/src/jetstream/setup_console.go @@ -17,7 +17,7 @@ import ( "github.com/cloudfoundry-incubator/stratos/src/jetstream/api" "github.com/cloudfoundry-incubator/stratos/src/jetstream/api/config" "github.com/cloudfoundry-incubator/stratos/src/jetstream/crypto" - "github.com/cloudfoundry-incubator/stratos/src/jetstream/errorz" + "github.com/cloudfoundry-incubator/stratos/src/jetstream/custom_error" "github.com/cloudfoundry-incubator/stratos/src/jetstream/repository/console_config" "github.com/cloudfoundry-incubator/stratos/src/jetstream/repository/localusers" ) @@ -94,7 +94,7 @@ func (p *portalProxy) setupGetAvailableScopes(c echo.Context) error { errInfo, ok := err.(api.ErrHTTPRequest) if ok { if errInfo.Status == 0 { - if strings.Contains(errInfo.Error(), errorz.ERR_X509_CERTIFICATE) { + if strings.Contains(errInfo.Error(), custom_error.ERR_X509_CERTIFICATE) { return api.NewHTTPShadowError( http.StatusBadRequest, "Could not connect to the UAA - Certificate error - check Skip SSL validation setting", From 18325a57cb05f50583a1822797604bb6648e37f2 Mon Sep 17 00:00:00 2001 From: Nedim Akar Date: Tue, 31 Oct 2023 11:39:12 +0100 Subject: [PATCH 6/8] applied JRA's suggestions --- src/jetstream/cnsi.go | 21 ++++++++++----- src/jetstream/custom_error/custom_error.go | 15 ----------- .../custom_errors/constants/constants.go | 7 +++++ src/jetstream/custom_errors/custom_errors.go | 26 +++++++++++++++++++ src/jetstream/datastore/datastore.go | 6 ++--- src/jetstream/middleware.go | 4 +-- src/jetstream/plugins/kubernetes/main.go | 5 ++-- .../pgsql_goose_db_version.go | 20 +++++++------- .../pgsql_goose_db_version_test.go | 4 +-- src/jetstream/setup_console.go | 6 ++--- 10 files changed, 69 insertions(+), 45 deletions(-) delete mode 100644 src/jetstream/custom_error/custom_error.go create mode 100644 src/jetstream/custom_errors/constants/constants.go create mode 100644 src/jetstream/custom_errors/custom_errors.go diff --git a/src/jetstream/cnsi.go b/src/jetstream/cnsi.go index d5ac2c8e09..1fe0e72e95 100644 --- a/src/jetstream/cnsi.go +++ b/src/jetstream/cnsi.go @@ -3,6 +3,7 @@ package main import ( "crypto/x509" "encoding/json" + "errors" "fmt" "net/http" "net/url" @@ -23,15 +24,21 @@ import ( const dbReferenceError = "unable to establish a database reference: '%v'" func isSSLRelatedError(err error) (bool, string) { - if urlErr, ok := err.(*url.Error); ok { - if x509Err, ok := urlErr.Err.(x509.UnknownAuthorityError); ok { - return true, x509Err.Error() + var urlError *url.Error + if errors.As(err, &urlError) { + var ( + certInvalidError *x509.CertificateInvalidError + unknownAuthorityError *x509.UnknownAuthorityError + hostnameError *x509.HostnameError + ) + if errors.As(urlError.Err, unknownAuthorityError) { + return true, unknownAuthorityError.Error() } - if x509Err, ok := urlErr.Err.(x509.HostnameError); ok { - return true, x509Err.Error() + if errors.As(urlError.Err, hostnameError) { + return true, hostnameError.Error() } - if x509Err, ok := urlErr.Err.(x509.CertificateInvalidError); ok { - return true, x509Err.Error() + if errors.As(urlError.Err, certInvalidError) { + return true, certInvalidError.Error() } } return false, "" diff --git a/src/jetstream/custom_error/custom_error.go b/src/jetstream/custom_error/custom_error.go deleted file mode 100644 index 7c533c506a..0000000000 --- a/src/jetstream/custom_error/custom_error.go +++ /dev/null @@ -1,15 +0,0 @@ -package custom_error - -import "errors" - -const ( - ERR_DIAL_TCP = "dial" - ERR_NO_DATABASE_VERSIONS_FOUND = "no database versions found" - ERR_NO_SUCH_TABLE = "no such table" - ERR_X509_CERTIFICATE = "x509: certificate" -) - -var ErrDialTcp = errors.New(ERR_DIAL_TCP) -var ErrNoDatabaseVersionsFound = errors.New(ERR_NO_DATABASE_VERSIONS_FOUND) -var ErrNoSuchTable = errors.New(ERR_NO_SUCH_TABLE) -var Err509Certificate = errors.New(ERR_X509_CERTIFICATE) diff --git a/src/jetstream/custom_errors/constants/constants.go b/src/jetstream/custom_errors/constants/constants.go new file mode 100644 index 0000000000..f7dcacc278 --- /dev/null +++ b/src/jetstream/custom_errors/constants/constants.go @@ -0,0 +1,7 @@ +package constants + +const ( + ERR_GOOSE_DB_NO_DATABASE_VERSIONS_FOUND = "pgsql_goosedb: no database versions found" + ERR_GOOSE_DB_NO_SUCH_TABLE = "pgsql_goosedb: no such table" + ERR_GOOSE_DB_FAILED_GETTING_CURRENT_DATABASE_VERSION = "pgsql_goosedb: error trying to get current database version: %w" +) diff --git a/src/jetstream/custom_errors/custom_errors.go b/src/jetstream/custom_errors/custom_errors.go new file mode 100644 index 0000000000..a6e35a0e79 --- /dev/null +++ b/src/jetstream/custom_errors/custom_errors.go @@ -0,0 +1,26 @@ +package custom_errors + +import ( + "fmt" + + "github.com/cloudfoundry-incubator/stratos/src/jetstream/custom_error/constants" +) + +type GooseDBNoDatabaseVersionsFoundError struct{} + +func (e GooseDBNoDatabaseVersionsFoundError) Error() string { + return constants.ERR_GOOSE_DB_NO_DATABASE_VERSIONS_FOUND +} + +type GooseDBNoSuchTableError struct{} + +func (e GooseDBNoSuchTableError) Error() string { + return constants.ERR_GOOSE_DB_NO_SUCH_TABLE +} + +func ErrGettingCurrentVersion(err error) error { + return fmt.Errorf(constants.ERR_GOOSE_DB_FAILED_GETTING_CURRENT_DATABASE_VERSION, err) +} + +var ErrNoDatabaseVersionsFound = GooseDBNoDatabaseVersionsFoundError{} +var ErrNoSuchTable = GooseDBNoSuchTableError{} diff --git a/src/jetstream/datastore/datastore.go b/src/jetstream/datastore/datastore.go index 98a7983888..17030c102c 100644 --- a/src/jetstream/datastore/datastore.go +++ b/src/jetstream/datastore/datastore.go @@ -10,7 +10,7 @@ import ( "strings" "time" - "github.com/cloudfoundry-incubator/stratos/src/jetstream/custom_error" + "github.com/cloudfoundry-incubator/stratos/src/jetstream/custom_errors" goosedbversion "github.com/cloudfoundry-incubator/stratos/src/jetstream/repository/goose-db-version" "github.com/govau/cf-common/env" @@ -304,9 +304,9 @@ func WaitForMigrations(db *sql.DB) error { databaseVersionRec, err := dbVersionRepo.GetCurrentVersion() if err != nil { var errorMsg = err.Error() - if strings.Contains(err.Error(), custom_error.ERR_NO_SUCH_TABLE) { + if errors.Is(err, custom_errors.ErrNoSuchTable) { errorMsg = "Waiting for versions table to be created" - } else if errors.Is(err, custom_error.ErrNoDatabaseVersionsFound) { + } else if errors.Is(err, custom_errors.ErrNoDatabaseVersionsFound) { errorMsg = "Versions table is empty - waiting for migrations" } log.Infof("Database schema check: %s", errorMsg) diff --git a/src/jetstream/middleware.go b/src/jetstream/middleware.go index c610f43048..2b4cfdd48d 100644 --- a/src/jetstream/middleware.go +++ b/src/jetstream/middleware.go @@ -11,8 +11,6 @@ import ( "strings" "time" - "github.com/cloudfoundry-incubator/stratos/src/jetstream/custom_error" - "github.com/gorilla/context" "github.com/govau/cf-common/env" "github.com/labstack/echo/v4" @@ -44,7 +42,7 @@ func handleSessionError(config api.PortalConfig, c echo.Context, err error, doNo var netOpErr *net.OpError if errors.As(err, &netOpErr) { - if netOpErr.Op == custom_error.ERR_DIAL_TCP { + if netOpErr.Op == "dial" && netOpErr.Net == "tcp" { return api.NewHTTPShadowError( http.StatusServiceUnavailable, "Service is currently unavailable", diff --git a/src/jetstream/plugins/kubernetes/main.go b/src/jetstream/plugins/kubernetes/main.go index b417345fc6..36ad4d201e 100644 --- a/src/jetstream/plugins/kubernetes/main.go +++ b/src/jetstream/plugins/kubernetes/main.go @@ -1,18 +1,17 @@ package kubernetes import ( + "crypto/x509" "encoding/json" "fmt" "io/ioutil" "net/http" "net/url" "strconv" - "strings" "errors" "github.com/cloudfoundry-incubator/stratos/src/jetstream/api" - "github.com/cloudfoundry-incubator/stratos/src/jetstream/custom_error" "github.com/labstack/echo/v4" log "github.com/sirupsen/logrus" @@ -289,7 +288,7 @@ func (c *KubernetesSpecification) RequiresCert(ec echo.Context) error { Message string } if err != nil { - if strings.Contains(err.Error(), custom_error.ERR_X509_CERTIFICATE) { + if errors.Is(err, new(x509.CertificateInvalidError)) { response.Status = http.StatusOK response.Required = true } else { diff --git a/src/jetstream/repository/goose-db-version/pgsql_goose_db_version.go b/src/jetstream/repository/goose-db-version/pgsql_goose_db_version.go index 9c52ca33ac..7a340a5b17 100644 --- a/src/jetstream/repository/goose-db-version/pgsql_goose_db_version.go +++ b/src/jetstream/repository/goose-db-version/pgsql_goose_db_version.go @@ -2,12 +2,14 @@ package goosedbversion import ( "database/sql" + "errors" "fmt" + "strings" log "github.com/sirupsen/logrus" "github.com/cloudfoundry-incubator/stratos/src/jetstream/api" - "github.com/cloudfoundry-incubator/stratos/src/jetstream/custom_error" + "github.com/cloudfoundry-incubator/stratos/src/jetstream/custom_errors" ) const ( @@ -35,15 +37,15 @@ func (p *PostgresGooseDBVersionRepository) GetCurrentVersion() (api.GooseDBVersi err := p.db.QueryRow(getCurrentVersion).Scan(&dbVersion.VersionID) - switch { - case err == sql.ErrNoRows: - return api.GooseDBVersionRecord{}, custom_error.ErrNoDatabaseVersionsFound - case err != nil: - return api.GooseDBVersionRecord{}, fmt.Errorf("Error trying to get current database version: %v", err) - default: - // do nothing + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return api.GooseDBVersionRecord{}, custom_errors.ErrNoDatabaseVersionsFound + } else if strings.Contains(err.Error(), "no such table") { + return api.GooseDBVersionRecord{}, custom_errors.ErrNoSuchTable + } else { + return api.GooseDBVersionRecord{}, custom_errors.ErrGettingCurrentVersion(err) + } } - return *dbVersion, nil } diff --git a/src/jetstream/repository/goose-db-version/pgsql_goose_db_version_test.go b/src/jetstream/repository/goose-db-version/pgsql_goose_db_version_test.go index 665c55f054..e7b344ba01 100644 --- a/src/jetstream/repository/goose-db-version/pgsql_goose_db_version_test.go +++ b/src/jetstream/repository/goose-db-version/pgsql_goose_db_version_test.go @@ -10,7 +10,7 @@ import ( . "github.com/smartystreets/goconvey/convey" "github.com/cloudfoundry-incubator/stratos/src/jetstream/api" - "github.com/cloudfoundry-incubator/stratos/src/jetstream/custom_error" + "github.com/cloudfoundry-incubator/stratos/src/jetstream/custom_errors" ) func TestPgSQLGooseDB(t *testing.T) { @@ -72,7 +72,7 @@ func TestPgSQLGooseDB(t *testing.T) { Convey("there should be an error", func() { repository, _ := NewPostgresGooseDBVersionRepository(db) _, err := repository.GetCurrentVersion() - So(err, ShouldResemble, custom_error.ErrNoDatabaseVersionsFound) + So(err, ShouldResemble, custom_errors.ErrNoDatabaseVersionsFound) dberr := mock.ExpectationsWereMet() So(dberr, ShouldBeNil) diff --git a/src/jetstream/setup_console.go b/src/jetstream/setup_console.go index c426317d78..fca48fad3f 100644 --- a/src/jetstream/setup_console.go +++ b/src/jetstream/setup_console.go @@ -1,13 +1,13 @@ package main import ( + "crypto/x509" "errors" "fmt" "net/http" "net/url" "regexp" "strconv" - "strings" "github.com/govau/cf-common/env" "github.com/labstack/echo/v4" @@ -17,7 +17,6 @@ import ( "github.com/cloudfoundry-incubator/stratos/src/jetstream/api" "github.com/cloudfoundry-incubator/stratos/src/jetstream/api/config" "github.com/cloudfoundry-incubator/stratos/src/jetstream/crypto" - "github.com/cloudfoundry-incubator/stratos/src/jetstream/custom_error" "github.com/cloudfoundry-incubator/stratos/src/jetstream/repository/console_config" "github.com/cloudfoundry-incubator/stratos/src/jetstream/repository/localusers" ) @@ -94,7 +93,8 @@ func (p *portalProxy) setupGetAvailableScopes(c echo.Context) error { errInfo, ok := err.(api.ErrHTTPRequest) if ok { if errInfo.Status == 0 { - if strings.Contains(errInfo.Error(), custom_error.ERR_X509_CERTIFICATE) { + var certError *x509.CertificateInvalidError + if errors.As(err, certError) { return api.NewHTTPShadowError( http.StatusBadRequest, "Could not connect to the UAA - Certificate error - check Skip SSL validation setting", From 9f4e75e7db7df7d47050a2908ffeb2def194d460 Mon Sep 17 00:00:00 2001 From: Nedim Akar Date: Tue, 31 Oct 2023 11:48:56 +0100 Subject: [PATCH 7/8] applied JRA's suggestions --- src/jetstream/custom_errors/custom_errors.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jetstream/custom_errors/custom_errors.go b/src/jetstream/custom_errors/custom_errors.go index a6e35a0e79..961b646bae 100644 --- a/src/jetstream/custom_errors/custom_errors.go +++ b/src/jetstream/custom_errors/custom_errors.go @@ -3,7 +3,7 @@ package custom_errors import ( "fmt" - "github.com/cloudfoundry-incubator/stratos/src/jetstream/custom_error/constants" + "github.com/cloudfoundry-incubator/stratos/src/jetstream/custom_errors/constants" ) type GooseDBNoDatabaseVersionsFoundError struct{} From bfb15e015df972686a1f5b9d51b4af08b7b3346e Mon Sep 17 00:00:00 2001 From: Jan-Robin Aumann Date: Tue, 31 Oct 2023 16:02:18 +0100 Subject: [PATCH 8/8] fix error in test suite --- .../goose-db-version/pgsql_goose_db_version_test.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/jetstream/repository/goose-db-version/pgsql_goose_db_version_test.go b/src/jetstream/repository/goose-db-version/pgsql_goose_db_version_test.go index e7b344ba01..de588f3830 100644 --- a/src/jetstream/repository/goose-db-version/pgsql_goose_db_version_test.go +++ b/src/jetstream/repository/goose-db-version/pgsql_goose_db_version_test.go @@ -2,7 +2,6 @@ package goosedbversion import ( "errors" - "fmt" "testing" "gopkg.in/DATA-DOG/go-sqlmock.v1" @@ -80,9 +79,6 @@ func TestPgSQLGooseDB(t *testing.T) { }) Convey("if there is a problem talking to the database", func() { - - expectedErrorMessage := fmt.Sprintf("Error trying to get current database version: %s", "error") - // Database setup mock.ExpectQuery(selectFromDBVersionWhere). WillReturnError(errors.New("error")) @@ -90,7 +86,7 @@ func TestPgSQLGooseDB(t *testing.T) { Convey("there should be an error", func() { repository, _ := NewPostgresGooseDBVersionRepository(db) _, err := repository.GetCurrentVersion() - So(err, ShouldResemble, errors.New(expectedErrorMessage)) + So(err, ShouldResemble, custom_errors.ErrGettingCurrentVersion(errors.New("error"))) dberr := mock.ExpectationsWereMet() So(dberr, ShouldBeNil)