From 177a83c58f7b92bc41a58fd5e8e89c77b57da63d Mon Sep 17 00:00:00 2001 From: Richard87 Date: Tue, 8 Oct 2024 09:37:50 +0200 Subject: [PATCH] Remove unused token/impersonation from accounts --- api/alerting/handler_test.go | 3 +-- api/environments/environment_controller_test.go | 3 +-- api/jobs/job_handler_test.go | 3 +-- api/middleware/auth/authentication.go | 2 +- api/middleware/cors/cors.go | 2 +- api/middleware/logger/middleware.go | 6 +++--- api/middleware/recovery/recovery.go | 2 +- api/router/api.go | 12 ++++++------ api/router/metrics.go | 8 ++++---- api/utils/radix_middleware.go | 16 +--------------- models/accounts.go | 10 +--------- 11 files changed, 21 insertions(+), 46 deletions(-) diff --git a/api/alerting/handler_test.go b/api/alerting/handler_test.go index 2d6c5cea..032d7720 100644 --- a/api/alerting/handler_test.go +++ b/api/alerting/handler_test.go @@ -8,7 +8,6 @@ import ( certclientfake "github.com/cert-manager/cert-manager/pkg/client/clientset/versioned/fake" alertModels "github.com/equinor/radix-api/api/alerting/models" "github.com/equinor/radix-api/models" - radixmodels "github.com/equinor/radix-common/models" operatoralert "github.com/equinor/radix-operator/pkg/apis/alert" "github.com/equinor/radix-operator/pkg/apis/kube" radixv1 "github.com/equinor/radix-operator/pkg/apis/radix/v1" @@ -34,7 +33,7 @@ func (s *HandlerTestSuite) SetupTest() { kedaClient := kedafake.NewSimpleClientset() secretProviderClient := secretproviderfake.NewSimpleClientset() certClient := certclientfake.NewSimpleClientset() - s.accounts = models.NewAccounts(kubeClient, radixClient, kedaClient, secretProviderClient, nil, certClient, kubeClient, radixClient, kedaClient, secretProviderClient, nil, certClient, "", radixmodels.Impersonation{}) + s.accounts = models.NewAccounts(kubeClient, radixClient, kedaClient, secretProviderClient, nil, certClient, kubeClient, radixClient, kedaClient, secretProviderClient, nil, certClient) } func TestHandlerTestSuite(t *testing.T) { diff --git a/api/environments/environment_controller_test.go b/api/environments/environment_controller_test.go index 453ee939..ec156e8c 100644 --- a/api/environments/environment_controller_test.go +++ b/api/environments/environment_controller_test.go @@ -22,7 +22,6 @@ import ( "github.com/equinor/radix-api/api/utils" authnmock "github.com/equinor/radix-api/api/utils/token/mock" "github.com/equinor/radix-api/models" - radixmodels "github.com/equinor/radix-common/models" radixhttp "github.com/equinor/radix-common/net/http" radixutils "github.com/equinor/radix-common/utils" "github.com/equinor/radix-common/utils/numbers" @@ -2709,7 +2708,7 @@ func initHandler(client kubernetes.Interface, secretproviderclient secretsstorevclient.Interface, certClient certclient.Interface, handlerConfig ...EnvironmentHandlerOptions) EnvironmentHandler { - accounts := models.NewAccounts(client, radixclient, kedaClient, secretproviderclient, nil, certClient, client, radixclient, kedaClient, secretproviderclient, nil, certClient, "", radixmodels.Impersonation{}) + accounts := models.NewAccounts(client, radixclient, kedaClient, secretproviderclient, nil, certClient, client, radixclient, kedaClient, secretproviderclient, nil, certClient) options := []EnvironmentHandlerOptions{WithAccounts(accounts)} options = append(options, handlerConfig...) return Init(options...) diff --git a/api/jobs/job_handler_test.go b/api/jobs/job_handler_test.go index 25045e95..1acb13cd 100644 --- a/api/jobs/job_handler_test.go +++ b/api/jobs/job_handler_test.go @@ -10,7 +10,6 @@ import ( deploymentModels "github.com/equinor/radix-api/api/deployments/models" jobModels "github.com/equinor/radix-api/api/jobs/models" "github.com/equinor/radix-api/models" - radixmodels "github.com/equinor/radix-common/models" radixutils "github.com/equinor/radix-common/utils" radixv1 "github.com/equinor/radix-operator/pkg/apis/radix/v1" "github.com/equinor/radix-operator/pkg/apis/utils" @@ -75,7 +74,7 @@ func (s *JobHandlerTestSuite) SetupTest() { func (s *JobHandlerTestSuite) setupTest() { s.kubeClient, s.radixClient, s.kedaClient, s.secretProviderClient, s.certClient = s.getUtils() - accounts := models.NewAccounts(s.kubeClient, s.radixClient, s.kedaClient, s.secretProviderClient, nil, s.certClient, s.kubeClient, s.radixClient, s.kedaClient, s.secretProviderClient, nil, s.certClient, "", radixmodels.Impersonation{}) + accounts := models.NewAccounts(s.kubeClient, s.radixClient, s.kedaClient, s.secretProviderClient, nil, s.certClient, s.kubeClient, s.radixClient, s.kedaClient, s.secretProviderClient, nil, s.certClient) s.accounts = accounts } diff --git a/api/middleware/auth/authentication.go b/api/middleware/auth/authentication.go index 4557fc92..05d0d212 100644 --- a/api/middleware/auth/authentication.go +++ b/api/middleware/auth/authentication.go @@ -14,7 +14,7 @@ import ( type ctxUserKey struct{} type ctxImpersonationKey struct{} -func CreateAuthenticationMiddleware(validator token.ValidatorInterface) negroni.HandlerFunc { +func NewAuthenticationMiddleware(validator token.ValidatorInterface) negroni.HandlerFunc { return func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { ctx := r.Context() logger := log.Ctx(ctx) diff --git a/api/middleware/cors/cors.go b/api/middleware/cors/cors.go index e1b7d596..215b7c52 100644 --- a/api/middleware/cors/cors.go +++ b/api/middleware/cors/cors.go @@ -8,7 +8,7 @@ import ( "github.com/rs/zerolog/log" ) -func CreateMiddleware(clusterName, radixDNSZone string) *cors.Cors { +func NewMiddleware(clusterName, radixDNSZone string) *cors.Cors { corsOptions := cors.Options{ AllowedOrigins: []string{ diff --git a/api/middleware/logger/middleware.go b/api/middleware/logger/middleware.go index e0ff33a1..c8be393b 100644 --- a/api/middleware/logger/middleware.go +++ b/api/middleware/logger/middleware.go @@ -11,7 +11,7 @@ import ( "github.com/urfave/negroni/v3" ) -func CreateZerologRequestLoggerMiddleware() negroni.HandlerFunc { +func NewZerologResponseLoggerMiddleware() negroni.HandlerFunc { return func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { m := httpsnoop.CaptureMetrics(next, w, r) @@ -35,7 +35,7 @@ func CreateZerologRequestLoggerMiddleware() negroni.HandlerFunc { } } -func CreateZerologRequestIdMiddleware() negroni.HandlerFunc { +func NewZerologRequestIdMiddleware() negroni.HandlerFunc { return func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { logger := log.Ctx(r.Context()).With().Str("request_id", xid.New().String()).Logger() r = r.WithContext(logger.WithContext(r.Context())) @@ -43,7 +43,7 @@ func CreateZerologRequestIdMiddleware() negroni.HandlerFunc { next(w, r) } } -func CreateZerologRequestDetailsMiddleware() negroni.HandlerFunc { +func NewZerologRequestDetailsMiddleware() negroni.HandlerFunc { return func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { remoteIp, _, _ := net.SplitHostPort(r.RemoteAddr) logger := log.Ctx(r.Context()).With(). diff --git a/api/middleware/recovery/recovery.go b/api/middleware/recovery/recovery.go index e7e44515..ba778a5d 100644 --- a/api/middleware/recovery/recovery.go +++ b/api/middleware/recovery/recovery.go @@ -5,7 +5,7 @@ import ( "github.com/urfave/negroni/v3" ) -func CreateMiddleware() *negroni.Recovery { +func NewMiddleware() *negroni.Recovery { rec := negroni.NewRecovery() rec.PrintStack = false rec.Logger = &log.Logger diff --git a/api/router/api.go b/api/router/api.go index dbac7183..b6d9e48d 100644 --- a/api/router/api.go +++ b/api/router/api.go @@ -27,12 +27,12 @@ func NewAPIHandler(clusterName string, validator token.ValidatorInterface, radix serveMux.Handle("/api/", createApiRouter(kubeUtil, controllers)) n := negroni.New( - recovery.CreateMiddleware(), - logger.CreateZerologRequestIdMiddleware(), - cors.CreateMiddleware(clusterName, radixDNSZone), - logger.CreateZerologRequestDetailsMiddleware(), - auth.CreateAuthenticationMiddleware(validator), - logger.CreateZerologRequestLoggerMiddleware(), + recovery.NewMiddleware(), + logger.NewZerologRequestIdMiddleware(), + cors.NewMiddleware(clusterName, radixDNSZone), + logger.NewZerologRequestDetailsMiddleware(), + auth.NewAuthenticationMiddleware(validator), + logger.NewZerologResponseLoggerMiddleware(), ) n.UseHandler(serveMux) diff --git a/api/router/metrics.go b/api/router/metrics.go index 3f2ccf15..c303f2d7 100644 --- a/api/router/metrics.go +++ b/api/router/metrics.go @@ -15,10 +15,10 @@ func NewMetricsHandler() http.Handler { serveMux.Handle("GET /metrics", promhttp.Handler()) n := negroni.New( - recovery.CreateMiddleware(), - logger.CreateZerologRequestIdMiddleware(), - logger.CreateZerologRequestDetailsMiddleware(), - logger.CreateZerologRequestLoggerMiddleware(), + recovery.NewMiddleware(), + logger.NewZerologRequestIdMiddleware(), + logger.NewZerologRequestDetailsMiddleware(), + logger.NewZerologResponseLoggerMiddleware(), ) n.UseHandler(serveMux) diff --git a/api/utils/radix_middleware.go b/api/utils/radix_middleware.go index 5a78005b..906be29d 100644 --- a/api/utils/radix_middleware.go +++ b/api/utils/radix_middleware.go @@ -65,21 +65,7 @@ func (handler *RadixMiddleware) handleAuthorization(w http.ResponseWriter, r *ht inClusterClient, inClusterRadixClient, inClusterKedaClient, inClusterSecretProviderClient, inClusterTektonClient, inClusterCertManagerClient := handler.kubeUtil.GetServerKubernetesClient(restOptions...) outClusterClient, outClusterRadixClient, outClusterKedaClient, outClusterSecretProviderClient, outClusterTektonClient, outClusterCertManagerClient := handler.kubeUtil.GetUserKubernetesClient(token, impersonation, restOptions...) - accounts := models.NewAccounts( - inClusterClient, - inClusterRadixClient, - inClusterKedaClient, - inClusterSecretProviderClient, - inClusterTektonClient, - inClusterCertManagerClient, - outClusterClient, - outClusterRadixClient, - outClusterKedaClient, - outClusterSecretProviderClient, - outClusterTektonClient, - outClusterCertManagerClient, - token, - impersonation) + accounts := models.NewAccounts(inClusterClient, inClusterRadixClient, inClusterKedaClient, inClusterSecretProviderClient, inClusterTektonClient, inClusterCertManagerClient, outClusterClient, outClusterRadixClient, outClusterKedaClient, outClusterSecretProviderClient, outClusterTektonClient, outClusterCertManagerClient) // Check if registration of application exists for application-specific requests if appName, exists := mux.Vars(r)["appName"]; exists { diff --git a/models/accounts.go b/models/accounts.go index 02564307..cb308041 100644 --- a/models/accounts.go +++ b/models/accounts.go @@ -5,17 +5,13 @@ import ( tektonclient "github.com/tektoncd/pipeline/pkg/client/clientset/versioned" certclient "github.com/cert-manager/cert-manager/pkg/client/clientset/versioned" - radixmodels "github.com/equinor/radix-common/models" radixclient "github.com/equinor/radix-operator/pkg/client/clientset/versioned" "k8s.io/client-go/kubernetes" secretProviderClient "sigs.k8s.io/secrets-store-csi-driver/pkg/client/clientset/versioned" ) // NewAccounts creates a new Accounts struct -func NewAccounts( - inClusterClient kubernetes.Interface, inClusterRadixClient radixclient.Interface, inClusterKedaClient kedav2.Interface, inClusterSecretProviderClient secretProviderClient.Interface, inClusterTektonClient tektonclient.Interface, inClusterCertManagerClient certclient.Interface, - outClusterClient kubernetes.Interface, outClusterRadixClient radixclient.Interface, outClusterKedaClient kedav2.Interface, outClusterSecretProviderClient secretProviderClient.Interface, outClusterTektonClient tektonclient.Interface, outClusterCertManagerClient certclient.Interface, - token string, impersonation radixmodels.Impersonation) Accounts { +func NewAccounts(inClusterClient kubernetes.Interface, inClusterRadixClient radixclient.Interface, inClusterKedaClient kedav2.Interface, inClusterSecretProviderClient secretProviderClient.Interface, inClusterTektonClient tektonclient.Interface, inClusterCertManagerClient certclient.Interface, outClusterClient kubernetes.Interface, outClusterRadixClient radixclient.Interface, outClusterKedaClient kedav2.Interface, outClusterSecretProviderClient secretProviderClient.Interface, outClusterTektonClient tektonclient.Interface, outClusterCertManagerClient certclient.Interface) Accounts { return Accounts{ UserAccount: Account{ @@ -34,8 +30,6 @@ func NewAccounts( TektonClient: inClusterTektonClient, CertManagerClient: inClusterCertManagerClient, }, - token: token, - impersonation: impersonation, } } @@ -54,6 +48,4 @@ func NewServiceAccount(inClusterClient kubernetes.Interface, inClusterRadixClien type Accounts struct { UserAccount Account ServiceAccount Account - token string - impersonation radixmodels.Impersonation }