From 70ef9b349574ffd3dc8f121c48aa5b24915a0d68 Mon Sep 17 00:00:00 2001 From: yy Date: Fri, 20 Oct 2023 11:19:41 +0800 Subject: [PATCH] fix license lint. --- controllers/license/cmd/manager/main.go | 4 ++-- .../internal/controller/license_controller.go | 10 ++++++---- .../license/internal/controller/license_recorder.go | 12 ++++++------ controllers/license/internal/util/account/account.go | 5 +---- controllers/license/internal/util/claims/claims.go | 3 ++- .../license/internal/util/database/database.go | 1 - .../license/internal/util/database/database_test.go | 10 +--------- controllers/license/internal/util/errors/errors.go | 4 ++-- .../license/internal/util/license/validate.go | 3 ++- .../license/internal/util/license/validate_test.go | 4 +++- deploy/cloud/manifests/free-license.yaml | 3 ++- 11 files changed, 27 insertions(+), 32 deletions(-) diff --git a/controllers/license/cmd/manager/main.go b/controllers/license/cmd/manager/main.go index 253cff632df..d0e47857b9c 100644 --- a/controllers/license/cmd/manager/main.go +++ b/controllers/license/cmd/manager/main.go @@ -19,8 +19,6 @@ package main import ( "context" "flag" - accountv1 "github.com/labring/sealos/controllers/account/api/v1" - "github.com/labring/sealos/controllers/license/internal/util/database" "os" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) @@ -34,8 +32,10 @@ import ( "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/log/zap" + accountv1 "github.com/labring/sealos/controllers/account/api/v1" licensev1 "github.com/labring/sealos/controllers/license/api/v1" "github.com/labring/sealos/controllers/license/internal/controller" + "github.com/labring/sealos/controllers/license/internal/util/database" //+kubebuilder:scaffold:imports ) diff --git a/controllers/license/internal/controller/license_controller.go b/controllers/license/internal/controller/license_controller.go index 28d01846192..984cd7fe9e3 100644 --- a/controllers/license/internal/controller/license_controller.go +++ b/controllers/license/internal/controller/license_controller.go @@ -18,14 +18,16 @@ package controller import ( "context" - accountutil "github.com/labring/sealos/controllers/license/internal/util/account" - "github.com/labring/sealos/controllers/license/internal/util/database" - "github.com/labring/sealos/controllers/license/internal/util/key" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "time" "github.com/go-logr/logr" + licensev1 "github.com/labring/sealos/controllers/license/api/v1" + accountutil "github.com/labring/sealos/controllers/license/internal/util/account" + "github.com/labring/sealos/controllers/license/internal/util/database" + "github.com/labring/sealos/controllers/license/internal/util/key" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" diff --git a/controllers/license/internal/controller/license_recorder.go b/controllers/license/internal/controller/license_recorder.go index c1d7c144fbc..2454a119cc7 100644 --- a/controllers/license/internal/controller/license_recorder.go +++ b/controllers/license/internal/controller/license_recorder.go @@ -3,9 +3,11 @@ package controller import ( "context" "errors" + v1 "github.com/labring/sealos/controllers/license/api/v1" "github.com/labring/sealos/controllers/license/internal/util/database" "github.com/labring/sealos/controllers/license/internal/util/meta" + "go.mongodb.org/mongo-driver/mongo" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -26,12 +28,10 @@ func (r *LicenseRecorder) Get(ctx context.Context, license *v1.License) (*meta.M func (r *LicenseRecorder) Find(ctx context.Context, license *v1.License) (bool, error) { _, err := r.Get(ctx, license) - if err != nil { - if errors.Is(err, mongo.ErrNoDocuments) { - return false, nil - } else { - return false, err - } + if err != nil && errors.Is(err, mongo.ErrNoDocuments) { + return false, nil + } else if err != nil { + return false, err } return true, nil } diff --git a/controllers/license/internal/util/account/account.go b/controllers/license/internal/util/account/account.go index fef21ac8ac0..f077ca034a0 100644 --- a/controllers/license/internal/util/account/account.go +++ b/controllers/license/internal/util/account/account.go @@ -47,10 +47,7 @@ func Recharge(ctx context.Context, client client.Client, license *licensev1.Lice if err := crypto.RechargeBalance(account.Status.EncryptBalance, data.Amount*count.CurrencyUnit); err != nil { return err } - if err := client.Status().Update(ctx, account); err != nil { - return err - } - return nil + return client.Status().Update(ctx, account) } func GetNameByNameSpace(ns string) string { diff --git a/controllers/license/internal/util/claims/claims.go b/controllers/license/internal/util/claims/claims.go index 65ff66a2f17..2e86ac75455 100644 --- a/controllers/license/internal/util/claims/claims.go +++ b/controllers/license/internal/util/claims/claims.go @@ -2,8 +2,9 @@ package claims import ( "github.com/golang-jwt/jwt/v4" - "github.com/labring/sealos/controllers/license/api/v1" "github.com/mitchellh/mapstructure" + + v1 "github.com/labring/sealos/controllers/license/api/v1" ) type Claims struct { diff --git a/controllers/license/internal/util/database/database.go b/controllers/license/internal/util/database/database.go index f1b05d0b075..2e55df17271 100644 --- a/controllers/license/internal/util/database/database.go +++ b/controllers/license/internal/util/database/database.go @@ -56,5 +56,4 @@ func (db *DataBase) Disconnect(ctx context.Context) { logger.Error(err, "disconnect from database failed") return } - return } diff --git a/controllers/license/internal/util/database/database_test.go b/controllers/license/internal/util/database/database_test.go index 46363df3a6d..3e3b9e1a861 100644 --- a/controllers/license/internal/util/database/database_test.go +++ b/controllers/license/internal/util/database/database_test.go @@ -1,11 +1,3 @@ package database -import "testing" - -func TestDataBase_GetLicenseMeta(t *testing.T) { - -} - -func TestDataBase_StoreLicenseMeta(t *testing.T) { - -} +// TODO Add tests diff --git a/controllers/license/internal/util/errors/errors.go b/controllers/license/internal/util/errors/errors.go index dcb38020a54..bcaaf92174d 100644 --- a/controllers/license/internal/util/errors/errors.go +++ b/controllers/license/internal/util/errors/errors.go @@ -4,5 +4,5 @@ import ( "fmt" ) -var LicenseInvalidError = fmt.Errorf("the license provided appears to be invalid") -var ClaimsConventError = fmt.Errorf("the claims data provided appears to be invalid") +var ErrLicenseInvalid = fmt.Errorf("the license provided appears to be invalid") +var ErrClaimsConvent = fmt.Errorf("the claims data provided appears to be invalid") diff --git a/controllers/license/internal/util/license/validate.go b/controllers/license/internal/util/license/validate.go index 403fb444fc8..988c852ac95 100644 --- a/controllers/license/internal/util/license/validate.go +++ b/controllers/license/internal/util/license/validate.go @@ -2,6 +2,7 @@ package license import ( "encoding/base64" + "github.com/golang-jwt/jwt/v4" licensev1 "github.com/labring/sealos/controllers/license/api/v1" @@ -37,7 +38,7 @@ func GetClaims(license *licensev1.License) (*utilclaims.Claims, error) { } claims, ok := token.Claims.(*utilclaims.Claims) if !ok { - return nil, errors.ClaimsConventError + return nil, errors.ErrClaimsConvent } return claims, nil } diff --git a/controllers/license/internal/util/license/validate_test.go b/controllers/license/internal/util/license/validate_test.go index ff5ce80ae8f..59eae042d7e 100644 --- a/controllers/license/internal/util/license/validate_test.go +++ b/controllers/license/internal/util/license/validate_test.go @@ -1,10 +1,12 @@ package license import ( + "testing" + "github.com/golang-jwt/jwt/v4" + licensev1 "github.com/labring/sealos/controllers/license/api/v1" utilclaims "github.com/labring/sealos/controllers/license/internal/util/claims" - "testing" ) func TestIsLicenseValid(t *testing.T) { diff --git a/deploy/cloud/manifests/free-license.yaml b/deploy/cloud/manifests/free-license.yaml index aeae0c08043..654e63abb72 100644 --- a/deploy/cloud/manifests/free-license.yaml +++ b/deploy/cloud/manifests/free-license.yaml @@ -6,4 +6,5 @@ metadata: namespace: ns-admin spec: type: Account - token: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJTZWFsb3MiLCJpYXQiOjE2OTA0NDU5NjIsImFtdCI6MjAwMH0.ZzZfPRbNiNvRBLMn5FGJeKitRPHmUHZ1qvnGdJUIbIH1L5mQ4yECzvvsa5S8-OTqF6HXmrw9QmFcQOjoz5GpqjqrqXdH2H-JDXFGNNAib2J9UmLFmtV1BVm3zReucfK-bOY5NiWOr5wplEVwkoUKNPHLY5Mw142y9J62vELE-XW-hb3xcmWjLTPVRYgMqk0KEi7Z7cQ_rS0QgJh1Rqb2WS6AKz2ILE5J8XUhhhUva0nCEyLzE-I8oZtV6kugQy8YjWI-SjfneFOLI8-Pg40vry6DZZ-_J_9QmjkUlZx0YNMRiRA5yg2yWeMEzVnam9L310TJgu6Od-bEUijsfOcZyw" \ No newline at end of file + # nosemgrep: generic.secrets.security.detected-jwt-token.detected-jwt-token + token: "" \ No newline at end of file