diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index 4324bf5a8..4b58737c3 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -94,6 +94,10 @@ body: description: We only provide support for current versions of Portainer as per the lifecycle policy linked above. If you are on an older version of Portainer we recommend [updating first](https://docs.portainer.io/start/upgrade) in case your bug has already been fixed. multiple: false options: + - '2.31.3' + - '2.31.2' + - '2.31.1' + - '2.31.0' - '2.30.1' - '2.30.0' - '2.29.2' @@ -101,6 +105,9 @@ body: - '2.29.0' - '2.28.1' - '2.28.0' + - '2.27.9' + - '2.27.8' + - '2.27.7' - '2.27.6' - '2.27.5' - '2.27.4' diff --git a/api/cli/cli.go b/api/cli/cli.go index f6035f298..0722c0b2e 100644 --- a/api/cli/cli.go +++ b/api/cli/cli.go @@ -61,6 +61,8 @@ func CLIFlags() *portainer.CLIFlags { LogMode: kingpin.Flag("log-mode", "Set the logging output mode").Default("PRETTY").Enum("NOCOLOR", "PRETTY", "JSON"), KubectlShellImage: kingpin.Flag("kubectl-shell-image", "Kubectl shell image").Envar(portainer.KubectlShellImageEnvVar).Default(portainer.DefaultKubectlShellImage).String(), PullLimitCheckDisabled: kingpin.Flag("pull-limit-check-disabled", "Pull limit check").Envar(portainer.PullLimitCheckDisabledEnvVar).Default(defaultPullLimitCheckDisabled).Bool(), + TrustedOrigins: kingpin.Flag("trusted-origins", "List of trusted origins for CSRF protection. Separate multiple origins with a comma.").Envar(portainer.TrustedOriginsEnvVar).String(), + CSP: kingpin.Flag("csp", "Content Security Policy (CSP) header").Envar(portainer.CSPEnvVar).Default("true").Bool(), } } diff --git a/api/cmd/portainer/main.go b/api/cmd/portainer/main.go index 6261efbd9..30493a3da 100644 --- a/api/cmd/portainer/main.go +++ b/api/cmd/portainer/main.go @@ -52,6 +52,7 @@ import ( "github.com/portainer/portainer/pkg/libhelm" libhelmtypes "github.com/portainer/portainer/pkg/libhelm/types" "github.com/portainer/portainer/pkg/libstack/compose" + "github.com/portainer/portainer/pkg/validate" "github.com/gofrs/uuid" "github.com/rs/zerolog/log" @@ -330,6 +331,18 @@ func buildServer(flags *portainer.CLIFlags) portainer.Server { featureflags.Parse(*flags.FeatureFlags, portainer.SupportedFeatureFlags) } + trustedOrigins := []string{} + if *flags.TrustedOrigins != "" { + // validate if the trusted origins are valid urls + for _, origin := range strings.Split(*flags.TrustedOrigins, ",") { + if !validate.IsTrustedOrigin(origin) { + log.Fatal().Str("trusted_origin", origin).Msg("invalid url for trusted origin. Please check the trusted origins flag.") + } + + trustedOrigins = append(trustedOrigins, origin) + } + } + fileService := initFileService(*flags.Data) encryptionKey := loadEncryptionSecretKey(*flags.SecretKeyName) if encryptionKey == nil { @@ -370,7 +383,8 @@ func buildServer(flags *portainer.CLIFlags) portainer.Server { gitService := git.NewService(shutdownCtx) - openAMTService := openamt.NewService() + // Setting insecureSkipVerify to true to preserve the old behaviour. + openAMTService := openamt.NewService(true) cryptoService := &crypto.Service{} @@ -545,6 +559,7 @@ func buildServer(flags *portainer.CLIFlags) portainer.Server { Status: applicationStatus, BindAddress: *flags.Addr, BindAddressHTTPS: *flags.AddrHTTPS, + CSP: *flags.CSP, HTTPEnabled: sslDBSettings.HTTPEnabled, AssetsPath: *flags.Assets, DataStore: dataStore, @@ -578,6 +593,7 @@ func buildServer(flags *portainer.CLIFlags) portainer.Server { PendingActionsService: pendingActionsService, PlatformService: platformService, PullLimitCheckDisabled: *flags.PullLimitCheckDisabled, + TrustedOrigins: trustedOrigins, } } diff --git a/api/database/boltdb/db.go b/api/database/boltdb/db.go index a0db7f4e0..32a1b55c3 100644 --- a/api/database/boltdb/db.go +++ b/api/database/boltdb/db.go @@ -138,6 +138,8 @@ func (connection *DbConnection) Open() error { db, err := bolt.Open(databasePath, 0600, &bolt.Options{ Timeout: 1 * time.Second, InitialMmapSize: connection.InitialMmapSize, + FreelistType: bolt.FreelistMapType, + NoFreelistSync: true, }) if err != nil { return err diff --git a/api/dataservices/base.go b/api/dataservices/base.go index 04af70b02..18839b60f 100644 --- a/api/dataservices/base.go +++ b/api/dataservices/base.go @@ -10,7 +10,7 @@ type BaseCRUD[T any, I constraints.Integer] interface { Create(element *T) error Read(ID I) (*T, error) Exists(ID I) (bool, error) - ReadAll() ([]T, error) + ReadAll(predicates ...func(T) bool) ([]T, error) Update(ID I, element *T) error Delete(ID I) error } @@ -56,12 +56,13 @@ func (service BaseDataService[T, I]) Exists(ID I) (bool, error) { return exists, err } -func (service BaseDataService[T, I]) ReadAll() ([]T, error) { +// ReadAll retrieves all the elements that satisfy all the provided predicates. +func (service BaseDataService[T, I]) ReadAll(predicates ...func(T) bool) ([]T, error) { var collection = make([]T, 0) return collection, service.Connection.ViewTx(func(tx portainer.Transaction) error { var err error - collection, err = service.Tx(tx).ReadAll() + collection, err = service.Tx(tx).ReadAll(predicates...) return err }) diff --git a/api/dataservices/base_test.go b/api/dataservices/base_test.go new file mode 100644 index 000000000..e97a09963 --- /dev/null +++ b/api/dataservices/base_test.go @@ -0,0 +1,92 @@ +package dataservices + +import ( + "strconv" + "testing" + + portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/slicesx" + + "github.com/stretchr/testify/require" +) + +type testObject struct { + ID int + Value int +} + +type mockConnection struct { + store map[int]testObject + + portainer.Connection +} + +func (m mockConnection) UpdateObject(bucket string, key []byte, value interface{}) error { + obj := value.(*testObject) + + m.store[obj.ID] = *obj + + return nil +} + +func (m mockConnection) GetAll(bucketName string, obj any, appendFn func(o any) (any, error)) error { + for _, v := range m.store { + if _, err := appendFn(&v); err != nil { + return err + } + } + + return nil +} + +func (m mockConnection) UpdateTx(fn func(portainer.Transaction) error) error { + return fn(m) +} + +func (m mockConnection) ViewTx(fn func(portainer.Transaction) error) error { + return fn(m) +} + +func (m mockConnection) ConvertToKey(v int) []byte { + return []byte(strconv.Itoa(v)) +} + +func TestReadAll(t *testing.T) { + service := BaseDataService[testObject, int]{ + Bucket: "testBucket", + Connection: mockConnection{store: make(map[int]testObject)}, + } + + data := []testObject{ + {ID: 1, Value: 1}, + {ID: 2, Value: 2}, + {ID: 3, Value: 3}, + {ID: 4, Value: 4}, + {ID: 5, Value: 5}, + } + + for _, item := range data { + err := service.Update(item.ID, &item) + require.NoError(t, err) + } + + // ReadAll without predicates + result, err := service.ReadAll() + require.NoError(t, err) + + expected := append([]testObject{}, data...) + + require.ElementsMatch(t, expected, result) + + // ReadAll with predicates + hasLowID := func(obj testObject) bool { return obj.ID < 3 } + isEven := func(obj testObject) bool { return obj.Value%2 == 0 } + + result, err = service.ReadAll(hasLowID, isEven) + require.NoError(t, err) + + expected = slicesx.Filter(expected, hasLowID) + expected = slicesx.Filter(expected, isEven) + + require.ElementsMatch(t, expected, result) +} diff --git a/api/dataservices/base_tx.go b/api/dataservices/base_tx.go index d9915b64c..5d7e7eee0 100644 --- a/api/dataservices/base_tx.go +++ b/api/dataservices/base_tx.go @@ -34,13 +34,32 @@ func (service BaseDataServiceTx[T, I]) Exists(ID I) (bool, error) { return service.Tx.KeyExists(service.Bucket, identifier) } -func (service BaseDataServiceTx[T, I]) ReadAll() ([]T, error) { +// ReadAll retrieves all the elements that satisfy all the provided predicates. +func (service BaseDataServiceTx[T, I]) ReadAll(predicates ...func(T) bool) ([]T, error) { var collection = make([]T, 0) + if len(predicates) == 0 { + return collection, service.Tx.GetAll( + service.Bucket, + new(T), + AppendFn(&collection), + ) + } + + filterFn := func(element T) bool { + for _, p := range predicates { + if !p(element) { + return false + } + } + + return true + } + return collection, service.Tx.GetAll( service.Bucket, new(T), - AppendFn(&collection), + FilterFn(&collection, filterFn), ) } diff --git a/api/dataservices/endpointrelation/endpointrelation.go b/api/dataservices/endpointrelation/endpointrelation.go index a81c258b9..556a046bb 100644 --- a/api/dataservices/endpointrelation/endpointrelation.go +++ b/api/dataservices/endpointrelation/endpointrelation.go @@ -112,13 +112,13 @@ func (service *Service) UpdateEndpointRelation(endpointID portainer.EndpointID, } func (service *Service) AddEndpointRelationsForEdgeStack(endpointIDs []portainer.EndpointID, edgeStackID portainer.EdgeStackID) error { - return service.connection.ViewTx(func(tx portainer.Transaction) error { + return service.connection.UpdateTx(func(tx portainer.Transaction) error { return service.Tx(tx).AddEndpointRelationsForEdgeStack(endpointIDs, edgeStackID) }) } func (service *Service) RemoveEndpointRelationsForEdgeStack(endpointIDs []portainer.EndpointID, edgeStackID portainer.EdgeStackID) error { - return service.connection.ViewTx(func(tx portainer.Transaction) error { + return service.connection.UpdateTx(func(tx portainer.Transaction) error { return service.Tx(tx).RemoveEndpointRelationsForEdgeStack(endpointIDs, edgeStackID) }) } diff --git a/api/datastore/migrator/migrate_2_32_0.go b/api/datastore/migrator/migrate_2_32_0.go new file mode 100644 index 000000000..c32a63cad --- /dev/null +++ b/api/datastore/migrator/migrate_2_32_0.go @@ -0,0 +1,33 @@ +package migrator + +import ( + "github.com/pkg/errors" + portainer "github.com/portainer/portainer/api" + perrors "github.com/portainer/portainer/api/dataservices/errors" + "github.com/portainer/portainer/api/internal/endpointutils" +) + +func (m *Migrator) addEndpointRelationForEdgeAgents_2_32_0() error { + endpoints, err := m.endpointService.Endpoints() + if err != nil { + return err + } + + for _, endpoint := range endpoints { + if endpointutils.IsEdgeEndpoint(&endpoint) { + _, err := m.endpointRelationService.EndpointRelation(endpoint.ID) + if err != nil && errors.Is(err, perrors.ErrObjectNotFound) { + relation := &portainer.EndpointRelation{ + EndpointID: endpoint.ID, + EdgeStacks: make(map[portainer.EdgeStackID]bool), + } + + if err := m.endpointRelationService.Create(relation); err != nil { + return err + } + } + } + } + + return nil +} diff --git a/api/datastore/migrator/migrator.go b/api/datastore/migrator/migrator.go index 992dd0b9d..4c2a50e59 100644 --- a/api/datastore/migrator/migrator.go +++ b/api/datastore/migrator/migrator.go @@ -249,6 +249,8 @@ func (m *Migrator) initMigrations() { m.addMigrations("2.31.0", m.migrateEdgeStacksStatuses_2_31_0) + m.addMigrations("2.32.0", m.addEndpointRelationForEdgeAgents_2_32_0) + // Add new migrations above... // One function per migration, each versions migration funcs in the same file. } diff --git a/api/datastore/test_data/output_24_to_latest.json b/api/datastore/test_data/output_24_to_latest.json index 41a1b49df..5e8b0eefa 100644 --- a/api/datastore/test_data/output_24_to_latest.json +++ b/api/datastore/test_data/output_24_to_latest.json @@ -121,6 +121,10 @@ "Ecr": { "Region": "" }, + "Github": { + "OrganisationName": "", + "UseOrganisation": false + }, "Gitlab": { "InstanceURL": "", "ProjectId": 0, @@ -611,7 +615,7 @@ "RequiredPasswordLength": 12 }, "KubeconfigExpiry": "0", - "KubectlShellImage": "portainer/kubectl-shell:2.31.0", + "KubectlShellImage": "portainer/kubectl-shell:2.32.0", "LDAPSettings": { "AnonymousMode": true, "AutoCreateUsers": true, @@ -776,6 +780,7 @@ "ImageCount": 9, "IsPodman": false, "NodeCount": 0, + "PerformanceMetrics": null, "RunningContainerCount": 5, "ServiceCount": 0, "StackCount": 2, @@ -939,7 +944,7 @@ } ], "version": { - "VERSION": "{\"SchemaVersion\":\"2.31.0\",\"MigratorCount\":1,\"Edition\":1,\"InstanceID\":\"463d5c47-0ea5-4aca-85b1-405ceefee254\"}" + "VERSION": "{\"SchemaVersion\":\"2.32.0\",\"MigratorCount\":1,\"Edition\":1,\"InstanceID\":\"463d5c47-0ea5-4aca-85b1-405ceefee254\"}" }, "webhooks": null } \ No newline at end of file diff --git a/api/hostmanagement/openamt/openamt.go b/api/hostmanagement/openamt/openamt.go index b27b78878..5843c1bdb 100644 --- a/api/hostmanagement/openamt/openamt.go +++ b/api/hostmanagement/openamt/openamt.go @@ -32,9 +32,9 @@ type Service struct { } // NewService initializes a new service. -func NewService() *Service { +func NewService(insecureSkipVerify bool) *Service { tlsConfig := crypto.CreateTLSConfiguration() - tlsConfig.InsecureSkipVerify = true + tlsConfig.InsecureSkipVerify = insecureSkipVerify return &Service{ httpsClient: &http.Client{ diff --git a/api/http/csrf/csrf.go b/api/http/csrf/csrf.go index 857d72c8b..6205c9290 100644 --- a/api/http/csrf/csrf.go +++ b/api/http/csrf/csrf.go @@ -2,6 +2,7 @@ package csrf import ( "crypto/rand" + "errors" "fmt" "net/http" "os" @@ -9,7 +10,8 @@ import ( "github.com/portainer/portainer/api/http/security" httperror "github.com/portainer/portainer/pkg/libhttp/error" - gorillacsrf "github.com/gorilla/csrf" + gcsrf "github.com/gorilla/csrf" + "github.com/rs/zerolog/log" "github.com/urfave/negroni" ) @@ -19,7 +21,7 @@ func SkipCSRFToken(w http.ResponseWriter) { w.Header().Set(csrfSkipHeader, "1") } -func WithProtect(handler http.Handler) (http.Handler, error) { +func WithProtect(handler http.Handler, trustedOrigins []string) (http.Handler, error) { // IsDockerDesktopExtension is used to check if we should skip csrf checks in the request bouncer (ShouldSkipCSRFCheck) // DOCKER_EXTENSION is set to '1' in build/docker-extension/docker-compose.yml isDockerDesktopExtension := false @@ -34,10 +36,12 @@ func WithProtect(handler http.Handler) (http.Handler, error) { return nil, fmt.Errorf("failed to generate CSRF token: %w", err) } - handler = gorillacsrf.Protect( + handler = gcsrf.Protect( token, - gorillacsrf.Path("/"), - gorillacsrf.Secure(false), + gcsrf.Path("/"), + gcsrf.Secure(false), + gcsrf.TrustedOrigins(trustedOrigins), + gcsrf.ErrorHandler(withErrorHandler(trustedOrigins)), )(handler) return withSkipCSRF(handler, isDockerDesktopExtension), nil @@ -55,7 +59,7 @@ func withSendCSRFToken(handler http.Handler) http.Handler { } if statusCode := sw.Status(); statusCode >= 200 && statusCode < 300 { - sw.Header().Set("X-CSRF-Token", gorillacsrf.Token(r)) + sw.Header().Set("X-CSRF-Token", gcsrf.Token(r)) } }) @@ -73,9 +77,33 @@ func withSkipCSRF(handler http.Handler, isDockerDesktopExtension bool) http.Hand } if skip { - r = gorillacsrf.UnsafeSkipCheck(r) + r = gcsrf.UnsafeSkipCheck(r) } handler.ServeHTTP(w, r) }) } + +func withErrorHandler(trustedOrigins []string) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + err := gcsrf.FailureReason(r) + + if errors.Is(err, gcsrf.ErrBadOrigin) || errors.Is(err, gcsrf.ErrBadReferer) || errors.Is(err, gcsrf.ErrNoReferer) { + log.Error().Err(err). + Str("request_url", r.URL.String()). + Str("host", r.Host). + Str("x_forwarded_proto", r.Header.Get("X-Forwarded-Proto")). + Str("forwarded", r.Header.Get("Forwarded")). + Str("origin", r.Header.Get("Origin")). + Str("referer", r.Header.Get("Referer")). + Strs("trusted_origins", trustedOrigins). + Msg("Failed to validate Origin or Referer") + } + + http.Error( + w, + http.StatusText(http.StatusForbidden)+" - "+err.Error(), + http.StatusForbidden, + ) + }) +} diff --git a/api/http/handler/auth/authenticate.go b/api/http/handler/auth/authenticate.go index 989949daa..4df31c92c 100644 --- a/api/http/handler/auth/authenticate.go +++ b/api/http/handler/auth/authenticate.go @@ -2,6 +2,7 @@ package auth import ( "net/http" + "strconv" "strings" portainer "github.com/portainer/portainer/api" @@ -82,6 +83,11 @@ func (handler *Handler) authenticate(rw http.ResponseWriter, r *http.Request) *h } } + // Clear any existing user caches + if user != nil { + handler.KubernetesClientFactory.ClearUserClientCache(strconv.Itoa(int(user.ID))) + } + if user != nil && isUserInitialAdmin(user) || settings.AuthenticationMethod == portainer.AuthenticationInternal { return handler.authenticateInternal(rw, user, payload.Password) } diff --git a/api/http/handler/auth/handler.go b/api/http/handler/auth/handler.go index 3b7210fbf..035ceabf8 100644 --- a/api/http/handler/auth/handler.go +++ b/api/http/handler/auth/handler.go @@ -8,6 +8,7 @@ import ( "github.com/portainer/portainer/api/http/proxy" "github.com/portainer/portainer/api/http/proxy/factory/kubernetes" "github.com/portainer/portainer/api/http/security" + "github.com/portainer/portainer/api/kubernetes/cli" httperror "github.com/portainer/portainer/pkg/libhttp/error" "github.com/gorilla/mux" @@ -23,16 +24,18 @@ type Handler struct { OAuthService portainer.OAuthService ProxyManager *proxy.Manager KubernetesTokenCacheManager *kubernetes.TokenCacheManager + KubernetesClientFactory *cli.ClientFactory passwordStrengthChecker security.PasswordStrengthChecker bouncer security.BouncerService } // NewHandler creates a handler to manage authentication operations. -func NewHandler(bouncer security.BouncerService, rateLimiter *security.RateLimiter, passwordStrengthChecker security.PasswordStrengthChecker) *Handler { +func NewHandler(bouncer security.BouncerService, rateLimiter *security.RateLimiter, passwordStrengthChecker security.PasswordStrengthChecker, kubernetesClientFactory *cli.ClientFactory) *Handler { h := &Handler{ Router: mux.NewRouter(), passwordStrengthChecker: passwordStrengthChecker, bouncer: bouncer, + KubernetesClientFactory: kubernetesClientFactory, } h.Handle("/auth/oauth/validate", diff --git a/api/http/handler/auth/logout.go b/api/http/handler/auth/logout.go index 977fafa69..73288565d 100644 --- a/api/http/handler/auth/logout.go +++ b/api/http/handler/auth/logout.go @@ -2,6 +2,7 @@ package auth import ( "net/http" + "strconv" "github.com/portainer/portainer/api/http/security" "github.com/portainer/portainer/api/logoutcontext" @@ -23,6 +24,7 @@ func (handler *Handler) logout(w http.ResponseWriter, r *http.Request) *httperro if tokenData != nil { handler.KubernetesTokenCacheManager.RemoveUserFromCache(tokenData.ID) + handler.KubernetesClientFactory.ClearUserClientCache(strconv.Itoa(int(tokenData.ID))) logoutcontext.Cancel(tokenData.Token) } diff --git a/api/http/handler/customtemplates/customtemplate_list.go b/api/http/handler/customtemplates/customtemplate_list.go index 581b219ae..c96d61523 100644 --- a/api/http/handler/customtemplates/customtemplate_list.go +++ b/api/http/handler/customtemplates/customtemplate_list.go @@ -71,7 +71,7 @@ func (handler *Handler) customTemplateList(w http.ResponseWriter, r *http.Reques customTemplates = filterByType(customTemplates, templateTypes) if edge != nil { - customTemplates = slicesx.Filter(customTemplates, func(customTemplate portainer.CustomTemplate) bool { + customTemplates = slicesx.FilterInPlace(customTemplates, func(customTemplate portainer.CustomTemplate) bool { return customTemplate.EdgeTemplate == *edge }) } diff --git a/api/http/handler/edgegroups/edgegroup_update.go b/api/http/handler/edgegroups/edgegroup_update.go index 7831b634e..a51ae33d4 100644 --- a/api/http/handler/edgegroups/edgegroup_update.go +++ b/api/http/handler/edgegroups/edgegroup_update.go @@ -163,7 +163,7 @@ func (handler *Handler) edgeGroupUpdate(w http.ResponseWriter, r *http.Request) func (handler *Handler) updateEndpointStacks(tx dataservices.DataStoreTx, endpoint *portainer.Endpoint, edgeGroups []portainer.EdgeGroup, edgeStacks []portainer.EdgeStack) error { relation, err := tx.EndpointRelation().EndpointRelation(endpoint.ID) - if err != nil && !handler.DataStore.IsErrObjectNotFound(err) { + if err != nil { return err } @@ -179,12 +179,6 @@ func (handler *Handler) updateEndpointStacks(tx dataservices.DataStoreTx, endpoi edgeStackSet[edgeStackID] = true } - if relation == nil { - relation = &portainer.EndpointRelation{ - EndpointID: endpoint.ID, - EdgeStacks: make(map[portainer.EdgeStackID]bool), - } - } relation.EdgeStacks = edgeStackSet return tx.EndpointRelation().UpdateEndpointRelation(endpoint.ID, relation) diff --git a/api/http/handler/edgestacks/edgestack_list.go b/api/http/handler/edgestacks/edgestack_list.go index b0df238c3..1ea991c4b 100644 --- a/api/http/handler/edgestacks/edgestack_list.go +++ b/api/http/handler/edgestacks/edgestack_list.go @@ -3,10 +3,39 @@ package edgestacks import ( "net/http" + portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/dataservices" + "github.com/portainer/portainer/api/slicesx" httperror "github.com/portainer/portainer/pkg/libhttp/error" + "github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/response" ) +type aggregatedStatusesMap map[portainer.EdgeStackStatusType]int + +type SummarizedStatus string + +const ( + sumStatusUnavailable SummarizedStatus = "Unavailable" + sumStatusDeploying SummarizedStatus = "Deploying" + sumStatusFailed SummarizedStatus = "Failed" + sumStatusPaused SummarizedStatus = "Paused" + sumStatusPartiallyRunning SummarizedStatus = "PartiallyRunning" + sumStatusCompleted SummarizedStatus = "Completed" + sumStatusRunning SummarizedStatus = "Running" +) + +type edgeStackStatusSummary struct { + AggregatedStatus aggregatedStatusesMap + Status SummarizedStatus + Reason string +} + +type edgeStackListResponseItem struct { + portainer.EdgeStack + StatusSummary edgeStackStatusSummary +} + // @id EdgeStackList // @summary Fetches the list of EdgeStacks // @description **Access policy**: administrator @@ -14,22 +43,122 @@ import ( // @security ApiKeyAuth // @security jwt // @produce json +// @param summarizeStatuses query boolean false "will summarize the statuses" // @success 200 {array} portainer.EdgeStack // @failure 500 // @failure 400 // @failure 503 "Edge compute features are disabled" // @router /edge_stacks [get] func (handler *Handler) edgeStackList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { + summarizeStatuses, _ := request.RetrieveBooleanQueryParameter(r, "summarizeStatuses", true) + edgeStacks, err := handler.DataStore.EdgeStack().EdgeStacks() if err != nil { return httperror.InternalServerError("Unable to retrieve edge stacks from the database", err) } + res := make([]edgeStackListResponseItem, len(edgeStacks)) + for i := range edgeStacks { - if err := fillEdgeStackStatus(handler.DataStore, &edgeStacks[i]); err != nil { + res[i].EdgeStack = edgeStacks[i] + + if summarizeStatuses { + if err := fillStatusSummary(handler.DataStore, &res[i]); err != nil { + return handlerDBErr(err, "Unable to retrieve edge stack status from the database") + } + } else if err := fillEdgeStackStatus(handler.DataStore, &res[i].EdgeStack); err != nil { return handlerDBErr(err, "Unable to retrieve edge stack status from the database") } } - return response.JSON(w, edgeStacks) + return response.JSON(w, res) +} + +func fillStatusSummary(tx dataservices.DataStoreTx, edgeStack *edgeStackListResponseItem) error { + statuses, err := tx.EdgeStackStatus().ReadAll(edgeStack.ID) + if err != nil { + return err + } + + aggregated := make(aggregatedStatusesMap) + + for _, envStatus := range statuses { + for _, status := range envStatus.Status { + aggregated[status.Type]++ + } + } + + status, reason := SummarizeStatuses(statuses, edgeStack.NumDeployments) + + edgeStack.StatusSummary = edgeStackStatusSummary{ + AggregatedStatus: aggregated, + Status: status, + Reason: reason, + } + + edgeStack.Status = map[portainer.EndpointID]portainer.EdgeStackStatus{} + + return nil +} + +func SummarizeStatuses(statuses []portainer.EdgeStackStatusForEnv, numDeployments int) (SummarizedStatus, string) { + if numDeployments == 0 { + return sumStatusUnavailable, "Your edge stack is currently unavailable due to the absence of an available environment in your edge group" + } + + allStatuses := slicesx.FlatMap(statuses, func(x portainer.EdgeStackStatusForEnv) []portainer.EdgeStackDeploymentStatus { + return x.Status + }) + + lastStatuses := slicesx.Map( + slicesx.Filter( + statuses, + func(s portainer.EdgeStackStatusForEnv) bool { + return len(s.Status) > 0 + }, + ), + func(x portainer.EdgeStackStatusForEnv) portainer.EdgeStackDeploymentStatus { + return x.Status[len(x.Status)-1] + }, + ) + + if len(lastStatuses) == 0 { + return sumStatusDeploying, "" + } + + if allFailed := slicesx.Every(lastStatuses, func(s portainer.EdgeStackDeploymentStatus) bool { + return s.Type == portainer.EdgeStackStatusError + }); allFailed { + return sumStatusFailed, "" + } + + if hasPaused := slicesx.Some(allStatuses, func(s portainer.EdgeStackDeploymentStatus) bool { + return s.Type == portainer.EdgeStackStatusPausedDeploying + }); hasPaused { + return sumStatusPaused, "" + } + + if len(lastStatuses) < numDeployments { + return sumStatusDeploying, "" + } + + hasDeploying := slicesx.Some(lastStatuses, func(s portainer.EdgeStackDeploymentStatus) bool { return s.Type == portainer.EdgeStackStatusDeploying }) + hasRunning := slicesx.Some(lastStatuses, func(s portainer.EdgeStackDeploymentStatus) bool { return s.Type == portainer.EdgeStackStatusRunning }) + hasFailed := slicesx.Some(lastStatuses, func(s portainer.EdgeStackDeploymentStatus) bool { return s.Type == portainer.EdgeStackStatusError }) + + if hasRunning && hasFailed && !hasDeploying { + return sumStatusPartiallyRunning, "" + } + + if allCompleted := slicesx.Every(lastStatuses, func(s portainer.EdgeStackDeploymentStatus) bool { return s.Type == portainer.EdgeStackStatusCompleted }); allCompleted { + return sumStatusCompleted, "" + } + + if allRunning := slicesx.Every(lastStatuses, func(s portainer.EdgeStackDeploymentStatus) bool { + return s.Type == portainer.EdgeStackStatusRunning + }); allRunning { + return sumStatusRunning, "" + } + + return sumStatusDeploying, "" } diff --git a/api/http/handler/edgestacks/edgestack_status_update.go b/api/http/handler/edgestacks/edgestack_status_update.go index 4f99e7ab3..0ff6a9eff 100644 --- a/api/http/handler/edgestacks/edgestack_status_update.go +++ b/api/http/handler/edgestacks/edgestack_status_update.go @@ -133,7 +133,9 @@ func (handler *Handler) updateEdgeStackStatus(tx dataservices.DataStoreTx, stack } environmentStatus, err := tx.EdgeStackStatus().Read(stackID, payload.EndpointID) - if err != nil { + if err != nil && !tx.IsErrObjectNotFound(err) { + return err + } else if tx.IsErrObjectNotFound(err) { environmentStatus = &portainer.EdgeStackStatusForEnv{ EndpointID: payload.EndpointID, Status: []portainer.EdgeStackDeploymentStatus{}, diff --git a/api/http/handler/endpointedge/endpointedge_status_inspect.go b/api/http/handler/endpointedge/endpointedge_status_inspect.go index 4d6368493..9bd341561 100644 --- a/api/http/handler/endpointedge/endpointedge_status_inspect.go +++ b/api/http/handler/endpointedge/endpointedge_status_inspect.go @@ -264,9 +264,6 @@ func (handler *Handler) buildSchedules(tx dataservices.DataStoreTx, endpointID p func (handler *Handler) buildEdgeStacks(tx dataservices.DataStoreTx, endpointID portainer.EndpointID) ([]stackStatusResponse, *httperror.HandlerError) { relation, err := tx.EndpointRelation().EndpointRelation(endpointID) if err != nil { - if tx.IsErrObjectNotFound(err) { - return nil, nil - } return nil, httperror.InternalServerError("Unable to retrieve relation object from the database", err) } diff --git a/api/http/handler/endpointgroups/endpoints.go b/api/http/handler/endpointgroups/endpoints.go index b34032d9e..8b420f2a6 100644 --- a/api/http/handler/endpointgroups/endpoints.go +++ b/api/http/handler/endpointgroups/endpoints.go @@ -21,17 +21,10 @@ func (handler *Handler) updateEndpointRelations(tx dataservices.DataStoreTx, end } endpointRelation, err := tx.EndpointRelation().EndpointRelation(endpoint.ID) - if err != nil && !tx.IsErrObjectNotFound(err) { + if err != nil { return err } - if endpointRelation == nil { - endpointRelation = &portainer.EndpointRelation{ - EndpointID: endpoint.ID, - EdgeStacks: make(map[portainer.EdgeStackID]bool), - } - } - edgeGroups, err := tx.EdgeGroup().ReadAll() if err != nil { return err diff --git a/api/http/handler/endpoints/endpoint_create.go b/api/http/handler/endpoints/endpoint_create.go index 1c6415023..3cfe934bc 100644 --- a/api/http/handler/endpoints/endpoint_create.go +++ b/api/http/handler/endpoints/endpoint_create.go @@ -563,6 +563,10 @@ func (handler *Handler) saveEndpointAndUpdateAuthorizations(tx dataservices.Data return err } + if err := endpointutils.InitializeEdgeEndpointRelation(endpoint, tx); err != nil { + return err + } + for _, tagID := range endpoint.TagIDs { if err := tx.Tag().UpdateTagFunc(tagID, func(tag *portainer.Tag) { tag.Endpoints[endpoint.ID] = true diff --git a/api/http/handler/endpoints/endpoint_list.go b/api/http/handler/endpoints/endpoint_list.go index 86f1b1d3c..43b14ad6a 100644 --- a/api/http/handler/endpoints/endpoint_list.go +++ b/api/http/handler/endpoints/endpoint_list.go @@ -95,12 +95,11 @@ func (handler *Handler) endpointList(w http.ResponseWriter, r *http.Request) *ht return httperror.BadRequest("Invalid query parameters", err) } - filteredEndpoints := security.FilterEndpoints(endpoints, endpointGroups, securityContext) - - filteredEndpoints, totalAvailableEndpoints, err := handler.filterEndpointsByQuery(filteredEndpoints, query, endpointGroups, edgeGroups, settings) + filteredEndpoints, totalAvailableEndpoints, err := handler.filterEndpointsByQuery(endpoints, query, endpointGroups, edgeGroups, settings, securityContext) if err != nil { return httperror.InternalServerError("Unable to filter endpoints", err) } + filteredEndpoints = security.FilterEndpoints(filteredEndpoints, endpointGroups, securityContext) sortEnvironmentsByField(filteredEndpoints, endpointGroups, getSortKey(sortField), sortOrder == "desc") diff --git a/api/http/handler/endpoints/endpoint_registries_list.go b/api/http/handler/endpoints/endpoint_registries_list.go index e81bc34a9..5bc4a930d 100644 --- a/api/http/handler/endpoints/endpoint_registries_list.go +++ b/api/http/handler/endpoints/endpoint_registries_list.go @@ -75,7 +75,7 @@ func (handler *Handler) listRegistries(tx dataservices.DataStoreTx, r *http.Requ return nil, httperror.InternalServerError("Unable to retrieve registries from the database", err) } - registries, handleError := handler.filterRegistriesByAccess(r, registries, endpoint, user, securityContext.UserMemberships) + registries, handleError := handler.filterRegistriesByAccess(tx, r, registries, endpoint, user, securityContext.UserMemberships) if handleError != nil { return nil, handleError } @@ -87,15 +87,15 @@ func (handler *Handler) listRegistries(tx dataservices.DataStoreTx, r *http.Requ return registries, err } -func (handler *Handler) filterRegistriesByAccess(r *http.Request, registries []portainer.Registry, endpoint *portainer.Endpoint, user *portainer.User, memberships []portainer.TeamMembership) ([]portainer.Registry, *httperror.HandlerError) { +func (handler *Handler) filterRegistriesByAccess(tx dataservices.DataStoreTx, r *http.Request, registries []portainer.Registry, endpoint *portainer.Endpoint, user *portainer.User, memberships []portainer.TeamMembership) ([]portainer.Registry, *httperror.HandlerError) { if !endpointutils.IsKubernetesEndpoint(endpoint) { return security.FilterRegistries(registries, user, memberships, endpoint.ID), nil } - return handler.filterKubernetesEndpointRegistries(r, registries, endpoint, user, memberships) + return handler.filterKubernetesEndpointRegistries(tx, r, registries, endpoint, user, memberships) } -func (handler *Handler) filterKubernetesEndpointRegistries(r *http.Request, registries []portainer.Registry, endpoint *portainer.Endpoint, user *portainer.User, memberships []portainer.TeamMembership) ([]portainer.Registry, *httperror.HandlerError) { +func (handler *Handler) filterKubernetesEndpointRegistries(tx dataservices.DataStoreTx, r *http.Request, registries []portainer.Registry, endpoint *portainer.Endpoint, user *portainer.User, memberships []portainer.TeamMembership) ([]portainer.Registry, *httperror.HandlerError) { namespaceParam, _ := request.RetrieveQueryParameter(r, "namespace", true) isAdmin, err := security.IsAdmin(r) if err != nil { @@ -116,7 +116,7 @@ func (handler *Handler) filterKubernetesEndpointRegistries(r *http.Request, regi return registries, nil } - return handler.filterKubernetesRegistriesByUserRole(r, registries, endpoint, user) + return handler.filterKubernetesRegistriesByUserRole(tx, r, registries, endpoint, user) } func (handler *Handler) isNamespaceAuthorized(endpoint *portainer.Endpoint, namespace string, userId portainer.UserID, memberships []portainer.TeamMembership, isAdmin bool) (bool, error) { @@ -169,7 +169,7 @@ func registryAccessPoliciesContainsNamespace(registryAccess portainer.RegistryAc return false } -func (handler *Handler) filterKubernetesRegistriesByUserRole(r *http.Request, registries []portainer.Registry, endpoint *portainer.Endpoint, user *portainer.User) ([]portainer.Registry, *httperror.HandlerError) { +func (handler *Handler) filterKubernetesRegistriesByUserRole(tx dataservices.DataStoreTx, r *http.Request, registries []portainer.Registry, endpoint *portainer.Endpoint, user *portainer.User) ([]portainer.Registry, *httperror.HandlerError) { err := handler.requestBouncer.AuthorizedEndpointOperation(r, endpoint) if errors.Is(err, security.ErrAuthorizationRequired) { return nil, httperror.Forbidden("User is not authorized", err) @@ -178,7 +178,7 @@ func (handler *Handler) filterKubernetesRegistriesByUserRole(r *http.Request, re return nil, httperror.InternalServerError("Unable to retrieve info from request context", err) } - userNamespaces, err := handler.userNamespaces(endpoint, user) + userNamespaces, err := handler.userNamespaces(tx, endpoint, user) if err != nil { return nil, httperror.InternalServerError("unable to retrieve user namespaces", err) } @@ -186,7 +186,7 @@ func (handler *Handler) filterKubernetesRegistriesByUserRole(r *http.Request, re return filterRegistriesByNamespaces(registries, endpoint.ID, userNamespaces), nil } -func (handler *Handler) userNamespaces(endpoint *portainer.Endpoint, user *portainer.User) ([]string, error) { +func (handler *Handler) userNamespaces(tx dataservices.DataStoreTx, endpoint *portainer.Endpoint, user *portainer.User) ([]string, error) { kcl, err := handler.K8sClientFactory.GetPrivilegedKubeClient(endpoint) if err != nil { return nil, err @@ -197,7 +197,7 @@ func (handler *Handler) userNamespaces(endpoint *portainer.Endpoint, user *porta return nil, err } - userMemberships, err := handler.DataStore.TeamMembership().TeamMembershipsByUserID(user.ID) + userMemberships, err := tx.TeamMembership().TeamMembershipsByUserID(user.ID) if err != nil { return nil, err } diff --git a/api/http/handler/endpoints/filter.go b/api/http/handler/endpoints/filter.go index 9b6004d1c..7cf7b9760 100644 --- a/api/http/handler/endpoints/filter.go +++ b/api/http/handler/endpoints/filter.go @@ -11,6 +11,7 @@ import ( portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/http/handler/edgegroups" + "github.com/portainer/portainer/api/http/security" "github.com/portainer/portainer/api/internal/edge" "github.com/portainer/portainer/api/internal/endpointutils" "github.com/portainer/portainer/api/slicesx" @@ -140,6 +141,7 @@ func (handler *Handler) filterEndpointsByQuery( groups []portainer.EndpointGroup, edgeGroups []portainer.EdgeGroup, settings *portainer.Settings, + context *security.RestrictedRequestContext, ) ([]portainer.Endpoint, int, error) { totalAvailableEndpoints := len(filteredEndpoints) @@ -181,11 +183,16 @@ func (handler *Handler) filterEndpointsByQuery( } // filter edge environments by trusted/untrusted + // only portainer admins are allowed to see untrusted environments filteredEndpoints = filter(filteredEndpoints, func(endpoint portainer.Endpoint) bool { if !endpointutils.IsEdgeEndpoint(&endpoint) { return true } + if query.edgeDeviceUntrusted { + return !endpoint.UserTrusted && context.IsAdmin + } + return endpoint.UserTrusted == !query.edgeDeviceUntrusted }) @@ -290,7 +297,9 @@ func filterEndpointsByEdgeStack(endpoints []portainer.Endpoint, edgeStackId port n := 0 for _, envId := range envIds { edgeStackStatus, err := datastore.EdgeStackStatus().Read(edgeStackId, envId) - if err != nil { + if dataservices.IsErrObjectNotFound(err) { + continue + } else if err != nil { return nil, errors.WithMessagef(err, "Unable to retrieve edge stack status for environment %d", envId) } diff --git a/api/http/handler/endpoints/filter_test.go b/api/http/handler/endpoints/filter_test.go index f19d0a276..8abc76fb7 100644 --- a/api/http/handler/endpoints/filter_test.go +++ b/api/http/handler/endpoints/filter_test.go @@ -6,6 +6,7 @@ import ( portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/datastore" + "github.com/portainer/portainer/api/http/security" "github.com/portainer/portainer/api/internal/testhelpers" "github.com/portainer/portainer/api/slicesx" @@ -263,6 +264,7 @@ func runTest(t *testing.T, test filterTest, handler *Handler, endpoints []portai []portainer.EndpointGroup{}, []portainer.EdgeGroup{}, &portainer.Settings{}, + &security.RestrictedRequestContext{IsAdmin: true}, ) is.NoError(err) diff --git a/api/http/handler/endpoints/update_edge_relations.go b/api/http/handler/endpoints/update_edge_relations.go index 1390c9fd4..c487519f0 100644 --- a/api/http/handler/endpoints/update_edge_relations.go +++ b/api/http/handler/endpoints/update_edge_relations.go @@ -17,17 +17,7 @@ func (handler *Handler) updateEdgeRelations(tx dataservices.DataStoreTx, endpoin relation, err := tx.EndpointRelation().EndpointRelation(endpoint.ID) if err != nil { - if !tx.IsErrObjectNotFound(err) { - return errors.WithMessage(err, "Unable to retrieve environment relation inside the database") - } - - relation = &portainer.EndpointRelation{ - EndpointID: endpoint.ID, - EdgeStacks: map[portainer.EdgeStackID]bool{}, - } - if err := tx.EndpointRelation().Create(relation); err != nil { - return errors.WithMessage(err, "Unable to create environment relation inside the database") - } + return errors.WithMessage(err, "Unable to retrieve environment relation inside the database") } endpointGroup, err := tx.EndpointGroup().Read(endpoint.GroupID) diff --git a/api/http/handler/file/handler.go b/api/http/handler/file/handler.go index 66f81b64a..9e57478c8 100644 --- a/api/http/handler/file/handler.go +++ b/api/http/handler/file/handler.go @@ -17,12 +17,12 @@ type Handler struct { } // NewHandler creates a handler to serve static files. -func NewHandler(assetPublicPath string, wasInstanceDisabled func() bool) *Handler { +func NewHandler(assetPublicPath string, csp bool, wasInstanceDisabled func() bool) *Handler { h := &Handler{ Handler: security.MWSecureHeaders( gzhttp.GzipHandler(http.FileServer(http.Dir(assetPublicPath))), featureflags.IsEnabled("hsts"), - featureflags.IsEnabled("csp"), + csp, ), wasInstanceDisabled: wasInstanceDisabled, } @@ -36,6 +36,7 @@ func isHTML(acceptContent []string) bool { return true } } + return false } @@ -43,11 +44,13 @@ func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if handler.wasInstanceDisabled() { if r.RequestURI == "/" || r.RequestURI == "/index.html" { http.Redirect(w, r, "/timeout.html", http.StatusTemporaryRedirect) + return } } else { if strings.HasPrefix(r.RequestURI, "/timeout.html") { http.Redirect(w, r, "/", http.StatusTemporaryRedirect) + return } } diff --git a/api/http/handler/handler.go b/api/http/handler/handler.go index 1ae55a002..1704eb316 100644 --- a/api/http/handler/handler.go +++ b/api/http/handler/handler.go @@ -81,7 +81,7 @@ type Handler struct { } // @title PortainerCE API -// @version 2.31.0 +// @version 2.32.0 // @description.markdown api-description.md // @termsOfService diff --git a/api/http/handler/kubernetes/client.go b/api/http/handler/kubernetes/client.go index 6612ab7f4..a7f2485e3 100644 --- a/api/http/handler/kubernetes/client.go +++ b/api/http/handler/kubernetes/client.go @@ -2,8 +2,10 @@ package kubernetes import ( "net/http" + "strconv" "github.com/portainer/portainer/api/http/middlewares" + "github.com/portainer/portainer/api/http/security" "github.com/portainer/portainer/api/kubernetes/cli" httperror "github.com/portainer/portainer/pkg/libhttp/error" "github.com/rs/zerolog/log" @@ -25,7 +27,13 @@ func (handler *Handler) prepareKubeClient(r *http.Request) (*cli.KubeClient, *ht return nil, httperror.NotFound("Unable to find the Kubernetes endpoint associated to the request.", err) } - pcli, err := handler.KubernetesClientFactory.GetPrivilegedKubeClient(endpoint) + tokenData, err := security.RetrieveTokenData(r) + if err != nil { + log.Error().Err(err).Str("context", "prepareKubeClient").Msg("Unable to retrieve token data associated to the request.") + return nil, httperror.InternalServerError("Unable to retrieve token data associated to the request.", err) + } + + pcli, err := handler.KubernetesClientFactory.GetPrivilegedUserKubeClient(endpoint, strconv.Itoa(int(tokenData.ID))) if err != nil { log.Error().Err(err).Str("context", "prepareKubeClient").Msg("Unable to get a privileged Kubernetes client for the user.") return nil, httperror.InternalServerError("Unable to get a privileged Kubernetes client for the user.", err) diff --git a/api/http/handler/kubernetes/event.go b/api/http/handler/kubernetes/event.go index 0e226d5ec..25f024303 100644 --- a/api/http/handler/kubernetes/event.go +++ b/api/http/handler/kubernetes/event.go @@ -20,7 +20,7 @@ import ( // @param id path int true "Environment identifier" // @param namespace path string true "The namespace name the events are associated to" // @param resourceId query string false "The resource id of the involved kubernetes object" example:"e5b021b6-4bce-4c06-bd3b-6cca906797aa" -// @success 200 {object} models.Event[] "Success" +// @success 200 {object} []kubernetes.K8sEvent "Success" // @failure 400 "Invalid request payload, such as missing required fields or fields not meeting validation criteria." // @failure 401 "Unauthorized access - the user is not authenticated or does not have the necessary permissions. Ensure that you have provided a valid API key or JWT token, and that you have the required permissions." // @failure 403 "Permission denied - the user is authenticated but does not have the necessary permissions to access the requested resource or perform the specified operation. Check your user roles and permissions." @@ -68,7 +68,7 @@ func (handler *Handler) getKubernetesEventsForNamespace(w http.ResponseWriter, r // @produce json // @param id path int true "Environment identifier" // @param resourceId query string false "The resource id of the involved kubernetes object" example:"e5b021b6-4bce-4c06-bd3b-6cca906797aa" -// @success 200 {object} models.Event[] "Success" +// @success 200 {object} []kubernetes.K8sEvent "Success" // @failure 400 "Invalid request payload, such as missing required fields or fields not meeting validation criteria." // @failure 401 "Unauthorized access - the user is not authenticated or does not have the necessary permissions. Ensure that you have provided a valid API key or JWT token, and that you have the required permissions." // @failure 403 "Permission denied - the user is authenticated but does not have the necessary permissions to access the requested resource or perform the specified operation. Check your user roles and permissions." diff --git a/api/http/handler/kubernetes/handler.go b/api/http/handler/kubernetes/handler.go index 07f6bdf3f..a8a5898c8 100644 --- a/api/http/handler/kubernetes/handler.go +++ b/api/http/handler/kubernetes/handler.go @@ -146,7 +146,7 @@ func (h *Handler) getProxyKubeClient(r *http.Request) (portainer.KubeClient, *ht return nil, httperror.Forbidden(fmt.Sprintf("an error occurred during the getProxyKubeClient operation, permission denied to access the environment /api/kubernetes/%d. Error: ", endpointID), err) } - cli, ok := h.KubernetesClientFactory.GetProxyKubeClient(strconv.Itoa(endpointID), tokenData.Token) + cli, ok := h.KubernetesClientFactory.GetProxyKubeClient(strconv.Itoa(endpointID), strconv.Itoa(int(tokenData.ID))) if !ok { return nil, httperror.InternalServerError("an error occurred during the getProxyKubeClient operation,failed to get proxy KubeClient", nil) } @@ -179,7 +179,7 @@ func (handler *Handler) kubeClientMiddleware(next http.Handler) http.Handler { } // Check if we have a kubeclient against this auth token already, otherwise generate a new one - _, ok := handler.KubernetesClientFactory.GetProxyKubeClient(strconv.Itoa(endpointID), tokenData.Token) + _, ok := handler.KubernetesClientFactory.GetProxyKubeClient(strconv.Itoa(endpointID), strconv.Itoa(int(tokenData.ID))) if ok { next.ServeHTTP(w, r) return @@ -269,7 +269,7 @@ func (handler *Handler) kubeClientMiddleware(next http.Handler) http.Handler { return } - handler.KubernetesClientFactory.SetProxyKubeClient(strconv.Itoa(int(endpoint.ID)), tokenData.Token, kubeCli) + handler.KubernetesClientFactory.SetProxyKubeClient(strconv.Itoa(int(endpoint.ID)), strconv.Itoa(int(tokenData.ID)), kubeCli) next.ServeHTTP(w, r) }) } diff --git a/api/http/handler/kubernetes/namespaces.go b/api/http/handler/kubernetes/namespaces.go index 2efde3b85..75dae9e69 100644 --- a/api/http/handler/kubernetes/namespaces.go +++ b/api/http/handler/kubernetes/namespaces.go @@ -22,6 +22,7 @@ import ( // @produce json // @param id path int true "Environment identifier" // @param withResourceQuota query boolean true "When set to true, include the resource quota information as part of the Namespace information. Default is false" +// @param withUnhealthyEvents query boolean true "When set to true, include the unhealthy events information as part of the Namespace information. Default is false" // @success 200 {array} portainer.K8sNamespaceInfo "Success" // @failure 400 "Invalid request payload, such as missing required fields or fields not meeting validation criteria." // @failure 401 "Unauthorized access - the user is not authenticated or does not have the necessary permissions. Ensure that you have provided a valid API key or JWT token, and that you have the required permissions." @@ -36,6 +37,12 @@ func (handler *Handler) getKubernetesNamespaces(w http.ResponseWriter, r *http.R return httperror.BadRequest("an error occurred during the GetKubernetesNamespaces operation, invalid query parameter withResourceQuota. Error: ", err) } + withUnhealthyEvents, err := request.RetrieveBooleanQueryParameter(r, "withUnhealthyEvents", true) + if err != nil { + log.Error().Err(err).Str("context", "GetKubernetesNamespaces").Msg("Invalid query parameter withUnhealthyEvents") + return httperror.BadRequest("an error occurred during the GetKubernetesNamespaces operation, invalid query parameter withUnhealthyEvents. Error: ", err) + } + cli, httpErr := handler.prepareKubeClient(r) if httpErr != nil { log.Error().Err(httpErr).Str("context", "GetKubernetesNamespaces").Msg("Unable to get a Kubernetes client for the user") @@ -48,6 +55,14 @@ func (handler *Handler) getKubernetesNamespaces(w http.ResponseWriter, r *http.R return httperror.InternalServerError("an error occurred during the GetKubernetesNamespaces operation, unable to retrieve namespaces from the Kubernetes cluster. Error: ", err) } + if withUnhealthyEvents { + namespaces, err = cli.CombineNamespacesWithUnhealthyEvents(namespaces) + if err != nil { + log.Error().Err(err).Str("context", "GetKubernetesNamespaces").Msg("Unable to combine namespaces with unhealthy events") + return httperror.InternalServerError("an error occurred during the GetKubernetesNamespaces operation, unable to combine namespaces with unhealthy events. Error: ", err) + } + } + if withResourceQuota { return cli.CombineNamespacesWithResourceQuotas(namespaces, w) } diff --git a/api/http/handler/registries/handler.go b/api/http/handler/registries/handler.go index dee14885e..026039833 100644 --- a/api/http/handler/registries/handler.go +++ b/api/http/handler/registries/handler.go @@ -5,10 +5,10 @@ import ( portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices" + httperrors "github.com/portainer/portainer/api/http/errors" "github.com/portainer/portainer/api/http/proxy" "github.com/portainer/portainer/api/http/security" - "github.com/portainer/portainer/api/internal/endpointutils" - "github.com/portainer/portainer/api/kubernetes" + "github.com/portainer/portainer/api/internal/registryutils/access" "github.com/portainer/portainer/api/kubernetes/cli" "github.com/portainer/portainer/api/pendingactions" httperror "github.com/portainer/portainer/pkg/libhttp/error" @@ -17,6 +17,7 @@ import ( "github.com/gorilla/mux" "github.com/pkg/errors" + "github.com/rs/zerolog/log" ) func hideFields(registry *portainer.Registry, hideAccesses bool) { @@ -56,17 +57,20 @@ func newHandler(bouncer security.BouncerService) *Handler { func (handler *Handler) initRouter(bouncer accessGuard) { adminRouter := handler.NewRoute().Subrouter() adminRouter.Use(bouncer.AdminAccess) - - authenticatedRouter := handler.NewRoute().Subrouter() - authenticatedRouter.Use(bouncer.AuthenticatedAccess) - adminRouter.Handle("/registries", httperror.LoggerHandler(handler.registryList)).Methods(http.MethodGet) adminRouter.Handle("/registries", httperror.LoggerHandler(handler.registryCreate)).Methods(http.MethodPost) adminRouter.Handle("/registries/{id}", httperror.LoggerHandler(handler.registryUpdate)).Methods(http.MethodPut) adminRouter.Handle("/registries/{id}/configure", httperror.LoggerHandler(handler.registryConfigure)).Methods(http.MethodPost) adminRouter.Handle("/registries/{id}", httperror.LoggerHandler(handler.registryDelete)).Methods(http.MethodDelete) - authenticatedRouter.Handle("/registries/{id}", httperror.LoggerHandler(handler.registryInspect)).Methods(http.MethodGet) + // Use registry-specific access bouncer for inspect and repositories endpoints + registryAccessRouter := handler.NewRoute().Subrouter() + registryAccessRouter.Use(bouncer.AuthenticatedAccess, handler.RegistryAccess) + registryAccessRouter.Handle("/registries/{id}", httperror.LoggerHandler(handler.registryInspect)).Methods(http.MethodGet) + + // Keep the gitlab proxy on the regular authenticated router as it doesn't require specific registry access + authenticatedRouter := handler.NewRoute().Subrouter() + authenticatedRouter.Use(bouncer.AuthenticatedAccess) authenticatedRouter.PathPrefix("/registries/proxies/gitlab").Handler(httperror.LoggerHandler(handler.proxyRequestsToGitlabAPIWithoutRegistry)) } @@ -88,9 +92,7 @@ func (handler *Handler) registriesHaveSameURLAndCredentials(r1, r2 *portainer.Re } // this function validates that -// // 1. user has the appropriate authorizations to perform the request -// // 2. user has a direct or indirect access to the registry func (handler *Handler) userHasRegistryAccess(r *http.Request, registry *portainer.Registry) (hasAccess bool, isAdmin bool, err error) { securityContext, err := security.RetrieveRestrictedRequestContext(r) @@ -98,11 +100,6 @@ func (handler *Handler) userHasRegistryAccess(r *http.Request, registry *portain return false, false, err } - user, err := handler.DataStore.User().Read(securityContext.UserID) - if err != nil { - return false, false, err - } - // Portainer admins always have access to everything if securityContext.IsAdmin { return true, true, nil @@ -128,47 +125,68 @@ func (handler *Handler) userHasRegistryAccess(r *http.Request, registry *portain return false, false, err } - memberships, err := handler.DataStore.TeamMembership().TeamMembershipsByUserID(user.ID) + // Use the enhanced registry access utility function that includes namespace validation + _, err = access.GetAccessibleRegistry( + handler.DataStore, + handler.K8sClientFactory, + securityContext.UserID, + endpointId, + registry.ID, + ) if err != nil { - return false, false, nil + return false, false, nil // No access } - // validate access for kubernetes namespaces (leverage registry.RegistryAccesses[endpointId].Namespaces) - if endpointutils.IsKubernetesEndpoint(endpoint) { - kcl, err := handler.K8sClientFactory.GetPrivilegedKubeClient(endpoint) - if err != nil { - return false, false, errors.Wrap(err, "unable to retrieve kubernetes client to validate registry access") - } - accessPolicies, err := kcl.GetNamespaceAccessPolicies() - if err != nil { - return false, false, errors.Wrap(err, "unable to retrieve environment's namespaces policies to validate registry access") - } - - authorizedNamespaces := registry.RegistryAccesses[endpointId].Namespaces - - for _, namespace := range authorizedNamespaces { - // when the default namespace is authorized to use a registry, all users have the ability to use it - // unless the default namespace is restricted: in this case continue to search for other potential accesses authorizations - if namespace == kubernetes.DefaultNamespace && !endpoint.Kubernetes.Configuration.RestrictDefaultNamespace { - return true, false, nil - } - - namespacePolicy := accessPolicies[namespace] - if security.AuthorizedAccess(user.ID, memberships, namespacePolicy.UserAccessPolicies, namespacePolicy.TeamAccessPolicies) { - return true, false, nil - } - } - return false, false, nil - } - - // validate access for docker environments - // leverage registry.RegistryAccesses[endpointId].UserAccessPolicies (direct access) - // and registry.RegistryAccesses[endpointId].TeamAccessPolicies (indirect access via his teams) - if security.AuthorizedRegistryAccess(registry, user, memberships, endpoint.ID) { - return true, false, nil - } - - // when user has no access via their role, direct grant or indirect grant - // then they don't have access to the registry - return false, false, nil + return true, false, nil +} + +// RegistryAccess defines a security check for registry-specific API endpoints. +// Authentication is required to access these endpoints. +// The user must have direct or indirect access to the specific registry being requested. +// This bouncer validates registry access using the userHasRegistryAccess logic. +func (handler *Handler) RegistryAccess(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // First ensure the user is authenticated + tokenData, err := security.RetrieveTokenData(r) + if err != nil { + httperror.WriteError(w, http.StatusUnauthorized, "Authentication required", httperrors.ErrUnauthorized) + return + } + + // Extract registry ID from the route + registryID, err := request.RetrieveNumericRouteVariableValue(r, "id") + if err != nil { + httperror.WriteError(w, http.StatusBadRequest, "Invalid registry identifier route variable", err) + return + } + + // Get the registry from the database + registry, err := handler.DataStore.Registry().Read(portainer.RegistryID(registryID)) + if handler.DataStore.IsErrObjectNotFound(err) { + httperror.WriteError(w, http.StatusNotFound, "Unable to find a registry with the specified identifier inside the database", err) + return + } else if err != nil { + httperror.WriteError(w, http.StatusInternalServerError, "Unable to find a registry with the specified identifier inside the database", err) + return + } + + // Check if user has access to this registry + hasAccess, _, err := handler.userHasRegistryAccess(r, registry) + if err != nil { + httperror.WriteError(w, http.StatusInternalServerError, "Unable to retrieve info from request context", err) + return + } + if !hasAccess { + log.Debug(). + Int("registry_id", registryID). + Str("registry_name", registry.Name). + Int("user_id", int(tokenData.ID)). + Str("context", "RegistryAccessBouncer"). + Msg("User access denied to registry") + httperror.WriteError(w, http.StatusForbidden, "Access denied to resource", httperrors.ErrResourceAccessDenied) + return + } + + next.ServeHTTP(w, r) + }) } diff --git a/api/http/handler/registries/registry_access_test.go b/api/http/handler/registries/registry_access_test.go new file mode 100644 index 000000000..8231f4d66 --- /dev/null +++ b/api/http/handler/registries/registry_access_test.go @@ -0,0 +1,89 @@ +package registries + +import ( + "net/http" + "net/http/httptest" + "testing" + + portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/datastore" + "github.com/portainer/portainer/api/http/security" + "github.com/portainer/portainer/api/internal/testhelpers" + + "github.com/gorilla/mux" + "github.com/stretchr/testify/assert" +) + +func Test_RegistryAccess_RequiresAuthentication(t *testing.T) { + _, store := datastore.MustNewTestStore(t, true, true) + registry := &portainer.Registry{ + ID: 1, + Name: "test-registry", + URL: "https://registry.test.com", + } + err := store.Registry().Create(registry) + assert.NoError(t, err) + handler := &Handler{ + DataStore: store, + } + req := httptest.NewRequest(http.MethodGet, "/registries/1", nil) + req = mux.SetURLVars(req, map[string]string{"id": "1"}) + rr := httptest.NewRecorder() + testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }) + bouncer := handler.RegistryAccess(testHandler) + bouncer.ServeHTTP(rr, req) + assert.Equal(t, http.StatusUnauthorized, rr.Code) +} + +func Test_RegistryAccess_InvalidRegistryID(t *testing.T) { + _, store := datastore.MustNewTestStore(t, true, true) + user := &portainer.User{ID: 1, Username: "test", Role: portainer.StandardUserRole} + err := store.User().Create(user) + assert.NoError(t, err) + + handler := &Handler{ + DataStore: store, + } + req := httptest.NewRequest(http.MethodGet, "/registries/invalid", nil) + req = mux.SetURLVars(req, map[string]string{"id": "invalid"}) + tokenData := &portainer.TokenData{ID: 1, Role: portainer.StandardUserRole} + req = req.WithContext(security.StoreTokenData(req, tokenData)) + + rr := httptest.NewRecorder() + + testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + bouncer := handler.RegistryAccess(testHandler) + bouncer.ServeHTTP(rr, req) + assert.Equal(t, http.StatusBadRequest, rr.Code) +} + +func Test_RegistryAccess_RegistryNotFound(t *testing.T) { + _, store := datastore.MustNewTestStore(t, true, true) + user := &portainer.User{ID: 1, Username: "test", Role: portainer.StandardUserRole} + err := store.User().Create(user) + assert.NoError(t, err) + + handler := &Handler{ + DataStore: store, + requestBouncer: testhelpers.NewTestRequestBouncer(), + } + req := httptest.NewRequest(http.MethodGet, "/registries/999", nil) + req = mux.SetURLVars(req, map[string]string{"id": "999"}) + tokenData := &portainer.TokenData{ID: 1, Role: portainer.StandardUserRole} + req = req.WithContext(security.StoreTokenData(req, tokenData)) + + rr := httptest.NewRecorder() + + testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + bouncer := handler.RegistryAccess(testHandler) + bouncer.ServeHTTP(rr, req) + assert.Equal(t, http.StatusNotFound, rr.Code) +} diff --git a/api/http/handler/registries/registry_inspect.go b/api/http/handler/registries/registry_inspect.go index a1f0bd9c5..f606a953e 100644 --- a/api/http/handler/registries/registry_inspect.go +++ b/api/http/handler/registries/registry_inspect.go @@ -4,10 +4,12 @@ import ( "net/http" portainer "github.com/portainer/portainer/api" - httperrors "github.com/portainer/portainer/api/http/errors" + "github.com/portainer/portainer/api/http/security" httperror "github.com/portainer/portainer/pkg/libhttp/error" "github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/response" + + "github.com/rs/zerolog/log" ) // @id RegistryInspect @@ -31,6 +33,11 @@ func (handler *Handler) registryInspect(w http.ResponseWriter, r *http.Request) return httperror.BadRequest("Invalid registry identifier route variable", err) } + log.Debug(). + Int("registry_id", registryID). + Str("context", "RegistryInspectHandler"). + Msg("Starting registry inspection") + registry, err := handler.DataStore.Registry().Read(portainer.RegistryID(registryID)) if handler.DataStore.IsErrObjectNotFound(err) { return httperror.NotFound("Unable to find a registry with the specified identifier inside the database", err) @@ -38,14 +45,12 @@ func (handler *Handler) registryInspect(w http.ResponseWriter, r *http.Request) return httperror.InternalServerError("Unable to find a registry with the specified identifier inside the database", err) } - hasAccess, isAdmin, err := handler.userHasRegistryAccess(r, registry) + // Check if user is admin to determine if we should hide sensitive fields + securityContext, err := security.RetrieveRestrictedRequestContext(r) if err != nil { return httperror.InternalServerError("Unable to retrieve info from request context", err) } - if !hasAccess { - return httperror.Forbidden("Access denied to resource", httperrors.ErrResourceAccessDenied) - } - hideFields(registry, !isAdmin) + hideFields(registry, !securityContext.IsAdmin) return response.JSON(w, registry) } diff --git a/api/http/handler/tags/tag_delete.go b/api/http/handler/tags/tag_delete.go index 4f8554faf..f8f1b7786 100644 --- a/api/http/handler/tags/tag_delete.go +++ b/api/http/handler/tags/tag_delete.go @@ -8,6 +8,7 @@ import ( portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/internal/edge" + "github.com/portainer/portainer/api/internal/endpointutils" httperror "github.com/portainer/portainer/pkg/libhttp/error" "github.com/portainer/portainer/pkg/libhttp/request" "github.com/portainer/portainer/pkg/libhttp/response" @@ -58,6 +59,9 @@ func deleteTag(tx dataservices.DataStoreTx, tagID portainer.TagID) error { for endpointID := range tag.Endpoints { endpoint, err := tx.Endpoint().Endpoint(endpointID) + if tx.IsErrObjectNotFound(err) { + continue + } if err != nil { return httperror.InternalServerError("Unable to retrieve environment from the database", err) } @@ -103,15 +107,10 @@ func deleteTag(tx dataservices.DataStoreTx, tagID portainer.TagID) error { return httperror.InternalServerError("Unable to retrieve edge stacks from the database", err) } - for _, endpoint := range endpoints { - if (tag.Endpoints[endpoint.ID] || tag.EndpointGroups[endpoint.GroupID]) && (endpoint.Type == portainer.EdgeAgentOnDockerEnvironment || endpoint.Type == portainer.EdgeAgentOnKubernetesEnvironment) { - err = updateEndpointRelations(tx, endpoint, edgeGroups, edgeStacks) - if err != nil { - return httperror.InternalServerError("Unable to update environment relations in the database", err) - } - } + edgeJobs, err := tx.EdgeJob().ReadAll() + if err != nil { + return httperror.InternalServerError("Unable to retrieve edge job configurations from the database", err) } - for _, edgeGroup := range edgeGroups { edgeGroup.TagIDs = slices.DeleteFunc(edgeGroup.TagIDs, func(t portainer.TagID) bool { return t == tagID @@ -123,6 +122,16 @@ func deleteTag(tx dataservices.DataStoreTx, tagID portainer.TagID) error { } } + for _, endpoint := range endpoints { + if (!tag.Endpoints[endpoint.ID] && !tag.EndpointGroups[endpoint.GroupID]) || !endpointutils.IsEdgeEndpoint(&endpoint) { + continue + } + + if err := updateEndpointRelations(tx, endpoint, edgeGroups, edgeStacks, edgeJobs); err != nil { + return httperror.InternalServerError("Unable to update environment relations in the database", err) + } + } + err = tx.Tag().Delete(tagID) if err != nil { return httperror.InternalServerError("Unable to remove the tag from the database", err) @@ -131,19 +140,12 @@ func deleteTag(tx dataservices.DataStoreTx, tagID portainer.TagID) error { return nil } -func updateEndpointRelations(tx dataservices.DataStoreTx, endpoint portainer.Endpoint, edgeGroups []portainer.EdgeGroup, edgeStacks []portainer.EdgeStack) error { +func updateEndpointRelations(tx dataservices.DataStoreTx, endpoint portainer.Endpoint, edgeGroups []portainer.EdgeGroup, edgeStacks []portainer.EdgeStack, edgeJobs []portainer.EdgeJob) error { endpointRelation, err := tx.EndpointRelation().EndpointRelation(endpoint.ID) - if err != nil && !tx.IsErrObjectNotFound(err) { + if err != nil { return err } - if endpointRelation == nil { - endpointRelation = &portainer.EndpointRelation{ - EndpointID: endpoint.ID, - EdgeStacks: make(map[portainer.EdgeStackID]bool), - } - } - endpointGroup, err := tx.EndpointGroup().Read(endpoint.GroupID) if err != nil { return err @@ -157,5 +159,25 @@ func updateEndpointRelations(tx dataservices.DataStoreTx, endpoint portainer.End endpointRelation.EdgeStacks = stacksSet - return tx.EndpointRelation().UpdateEndpointRelation(endpoint.ID, endpointRelation) + if err := tx.EndpointRelation().UpdateEndpointRelation(endpoint.ID, endpointRelation); err != nil { + return err + } + + for _, edgeJob := range edgeJobs { + endpoints, err := edge.GetEndpointsFromEdgeGroups(edgeJob.EdgeGroups, tx) + if err != nil { + return err + } + if slices.Contains(endpoints, endpoint.ID) { + continue + } + + delete(edgeJob.GroupLogsCollection, endpoint.ID) + + if err := tx.EdgeJob().Update(edgeJob.ID, &edgeJob); err != nil { + return err + } + } + + return nil } diff --git a/api/http/handler/tags/tag_delete_test.go b/api/http/handler/tags/tag_delete_test.go index cabf20963..ecced7c0d 100644 --- a/api/http/handler/tags/tag_delete_test.go +++ b/api/http/handler/tags/tag_delete_test.go @@ -1,6 +1,7 @@ package tags import ( + "github.com/portainer/portainer/api/dataservices" "net/http" "net/http/httptest" "strconv" @@ -8,23 +9,18 @@ import ( "testing" portainer "github.com/portainer/portainer/api" + portainerDsErrors "github.com/portainer/portainer/api/dataservices/errors" "github.com/portainer/portainer/api/datastore" "github.com/portainer/portainer/api/internal/testhelpers" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestTagDeleteEdgeGroupsConcurrently(t *testing.T) { const tagsCount = 100 - _, store := datastore.MustNewTestStore(t, true, false) - - user := &portainer.User{ID: 2, Username: "admin", Role: portainer.AdministratorRole} - if err := store.User().Create(user); err != nil { - t.Fatal("could not create admin user:", err) - } - - handler := NewHandler(testhelpers.NewTestRequestBouncer()) - handler.DataStore = store - + handler, store := setUpHandler(t) // Create all the tags and add them to the same edge group var tagIDs []portainer.TagID @@ -84,3 +80,132 @@ func TestTagDeleteEdgeGroupsConcurrently(t *testing.T) { t.Fatal("the edge group is not consistent") } } + +func TestHandler_tagDelete(t *testing.T) { + t.Run("should delete tag and update related endpoints and edge groups", func(t *testing.T) { + handler, store := setUpHandler(t) + + tag := &portainer.Tag{ + ID: 1, + Name: "tag-1", + Endpoints: make(map[portainer.EndpointID]bool), + EndpointGroups: make(map[portainer.EndpointGroupID]bool), + } + require.NoError(t, store.Tag().Create(tag)) + + endpointGroup := &portainer.EndpointGroup{ + ID: 2, + Name: "endpoint-group-1", + TagIDs: []portainer.TagID{tag.ID}, + } + require.NoError(t, store.EndpointGroup().Create(endpointGroup)) + + endpoint1 := &portainer.Endpoint{ + ID: 1, + Name: "endpoint-1", + GroupID: endpointGroup.ID, + } + require.NoError(t, store.Endpoint().Create(endpoint1)) + + endpoint2 := &portainer.Endpoint{ + ID: 2, + Name: "endpoint-2", + TagIDs: []portainer.TagID{tag.ID}, + } + require.NoError(t, store.Endpoint().Create(endpoint2)) + + tag.Endpoints[endpoint2.ID] = true + tag.EndpointGroups[endpointGroup.ID] = true + require.NoError(t, store.Tag().Update(tag.ID, tag)) + + dynamicEdgeGroup := &portainer.EdgeGroup{ + ID: 1, + Name: "edgegroup-1", + TagIDs: []portainer.TagID{tag.ID}, + Dynamic: true, + } + require.NoError(t, store.EdgeGroup().Create(dynamicEdgeGroup)) + + staticEdgeGroup := &portainer.EdgeGroup{ + ID: 2, + Name: "edgegroup-2", + Endpoints: []portainer.EndpointID{endpoint2.ID}, + } + require.NoError(t, store.EdgeGroup().Create(staticEdgeGroup)) + + req, err := http.NewRequest(http.MethodDelete, "/tags/"+strconv.Itoa(int(tag.ID)), nil) + if err != nil { + t.Fail() + + return + } + + rec := httptest.NewRecorder() + handler.ServeHTTP(rec, req) + + require.Equal(t, http.StatusNoContent, rec.Code) + + // Check that the tag is deleted + _, err = store.Tag().Read(tag.ID) + require.ErrorIs(t, err, portainerDsErrors.ErrObjectNotFound) + + // Check that the endpoints are updated + endpoint1, err = store.Endpoint().Endpoint(endpoint1.ID) + require.NoError(t, err) + assert.Len(t, endpoint1.TagIDs, 0, "endpoint-1 should not have any tags") + assert.Equal(t, endpoint1.GroupID, endpointGroup.ID, "endpoint-1 should still belong to the endpoint group") + + endpoint2, err = store.Endpoint().Endpoint(endpoint2.ID) + require.NoError(t, err) + assert.Len(t, endpoint2.TagIDs, 0, "endpoint-2 should not have any tags") + + // Check that the dynamic edge group is updated + dynamicEdgeGroup, err = store.EdgeGroup().Read(dynamicEdgeGroup.ID) + require.NoError(t, err) + assert.Len(t, dynamicEdgeGroup.TagIDs, 0, "dynamic edge group should not have any tags") + assert.Len(t, dynamicEdgeGroup.Endpoints, 0, "dynamic edge group should not have any endpoints") + + // Check that the static edge group is not updated + staticEdgeGroup, err = store.EdgeGroup().Read(staticEdgeGroup.ID) + require.NoError(t, err) + assert.Len(t, staticEdgeGroup.TagIDs, 0, "static edge group should not have any tags") + assert.Len(t, staticEdgeGroup.Endpoints, 1, "static edge group should have one endpoint") + assert.Equal(t, endpoint2.ID, staticEdgeGroup.Endpoints[0], "static edge group should have the endpoint-2") + }) + + // Test the tx.IsErrObjectNotFound logic when endpoint is not found during cleanup + t.Run("should continue gracefully when endpoint not found during cleanup", func(t *testing.T) { + _, store := setUpHandler(t) + // Create a tag with a reference to a non-existent endpoint + tag := &portainer.Tag{ + ID: 1, + Name: "test-tag", + Endpoints: map[portainer.EndpointID]bool{999: true}, // Non-existent endpoint + EndpointGroups: make(map[portainer.EndpointGroupID]bool), + } + + err := store.Tag().Create(tag) + if err != nil { + t.Fatal("could not create tag:", err) + } + + err = deleteTag(store, 1) + if err != nil { + t.Fatal("could not delete tag:", err) + } + }) +} + +func setUpHandler(t *testing.T) (*Handler, dataservices.DataStore) { + _, store := datastore.MustNewTestStore(t, true, false) + + user := &portainer.User{ID: 2, Username: "admin", Role: portainer.AdministratorRole} + if err := store.User().Create(user); err != nil { + t.Fatal("could not create admin user:", err) + } + + handler := NewHandler(testhelpers.NewTestRequestBouncer()) + handler.DataStore = store + + return handler, store +} diff --git a/api/http/handler/templates/utils_fetch_templates.go b/api/http/handler/templates/utils_fetch_templates.go index 73f9bad56..fc5c97125 100644 --- a/api/http/handler/templates/utils_fetch_templates.go +++ b/api/http/handler/templates/utils_fetch_templates.go @@ -40,11 +40,13 @@ func (handler *Handler) fetchTemplates() (*listResponse, *httperror.HandlerError } defer resp.Body.Close() - err = json.NewDecoder(resp.Body).Decode(&body) - if err != nil { + if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { return nil, httperror.InternalServerError("Unable to parse template file", err) } + for i := range body.Templates { + body.Templates[i].ID = portainer.TemplateID(i + 1) + } return body, nil } diff --git a/api/http/handler/webhooks/webhook_create.go b/api/http/handler/webhooks/webhook_create.go index b69e93db3..d7edde333 100644 --- a/api/http/handler/webhooks/webhook_create.go +++ b/api/http/handler/webhooks/webhook_create.go @@ -80,7 +80,7 @@ func (handler *Handler) webhookCreate(w http.ResponseWriter, r *http.Request) *h return httperror.InternalServerError("Unable to retrieve user authentication token", err) } - _, err = access.GetAccessibleRegistry(handler.DataStore, tokenData.ID, endpointID, payload.RegistryID) + _, err = access.GetAccessibleRegistry(handler.DataStore, nil, tokenData.ID, endpointID, payload.RegistryID) if err != nil { return httperror.Forbidden("Permission deny to access registry", err) } diff --git a/api/http/handler/webhooks/webhook_update.go b/api/http/handler/webhooks/webhook_update.go index 7a026fcd7..94133c49a 100644 --- a/api/http/handler/webhooks/webhook_update.go +++ b/api/http/handler/webhooks/webhook_update.go @@ -69,7 +69,7 @@ func (handler *Handler) webhookUpdate(w http.ResponseWriter, r *http.Request) *h return httperror.InternalServerError("Unable to retrieve user authentication token", err) } - _, err = access.GetAccessibleRegistry(handler.DataStore, tokenData.ID, webhook.EndpointID, payload.RegistryID) + _, err = access.GetAccessibleRegistry(handler.DataStore, nil, tokenData.ID, webhook.EndpointID, payload.RegistryID) if err != nil { return httperror.Forbidden("Permission deny to access registry", err) } diff --git a/api/http/middlewares/endpoint.go b/api/http/middlewares/endpoint.go index c88731dd3..0050e4300 100644 --- a/api/http/middlewares/endpoint.go +++ b/api/http/middlewares/endpoint.go @@ -25,12 +25,12 @@ type key int const contextEndpoint key = 0 func WithEndpoint(endpointService dataservices.EndpointService, endpointIDParam string) mux.MiddlewareFunc { + if endpointIDParam == "" { + endpointIDParam = "id" + } + return func(next http.Handler) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, request *http.Request) { - if endpointIDParam == "" { - endpointIDParam = "id" - } - endpointID, err := requesthelpers.RetrieveNumericRouteVariableValue(request, endpointIDParam) if err != nil { httperror.WriteError(rw, http.StatusBadRequest, "Invalid environment identifier route variable", err) @@ -51,7 +51,6 @@ func WithEndpoint(endpointService dataservices.EndpointService, endpointIDParam ctx := context.WithValue(request.Context(), contextEndpoint, endpoint) next.ServeHTTP(rw, request.WithContext(ctx)) - }) } } diff --git a/api/http/middlewares/plaintext_http_request.go b/api/http/middlewares/plaintext_http_request.go index 668346098..e746fd819 100644 --- a/api/http/middlewares/plaintext_http_request.go +++ b/api/http/middlewares/plaintext_http_request.go @@ -3,6 +3,7 @@ package middlewares import ( "net/http" "slices" + "strings" "github.com/gorilla/csrf" ) @@ -16,6 +17,45 @@ type plainTextHTTPRequestHandler struct { next http.Handler } +// parseForwardedHeaderProto parses the Forwarded header and extracts the protocol. +// The Forwarded header format supports: +// - Single proxy: Forwarded: by=;for=;host=;proto= +// - Multiple proxies: Forwarded: for=192.0.2.43, for=198.51.100.17 +// We take the first (leftmost) entry as it represents the original client +func parseForwardedHeaderProto(forwarded string) string { + if forwarded == "" { + return "" + } + + // Parse the first part (leftmost proxy, closest to original client) + firstPart, _, _ := strings.Cut(forwarded, ",") + firstPart = strings.TrimSpace(firstPart) + + // Split by semicolon to get key-value pairs within this proxy entry + // Format: key=value;key=value;key=value + pairs := strings.Split(firstPart, ";") + for _, pair := range pairs { + // Split by equals sign to separate key and value + key, value, found := strings.Cut(pair, "=") + if !found { + continue + } + + if strings.EqualFold(strings.TrimSpace(key), "proto") { + return strings.Trim(strings.TrimSpace(value), `"'`) + } + } + + return "" +} + +// isHTTPSRequest checks if the original request was made over HTTPS +// by examining both X-Forwarded-Proto and Forwarded headers +func isHTTPSRequest(r *http.Request) bool { + return strings.EqualFold(r.Header.Get("X-Forwarded-Proto"), "https") || + strings.EqualFold(parseForwardedHeaderProto(r.Header.Get("Forwarded")), "https") +} + func (h *plainTextHTTPRequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if slices.Contains(safeMethods, r.Method) { h.next.ServeHTTP(w, r) @@ -24,7 +64,7 @@ func (h *plainTextHTTPRequestHandler) ServeHTTP(w http.ResponseWriter, r *http.R req := r // If original request was HTTPS (via proxy), keep CSRF checks. - if xfproto := r.Header.Get("X-Forwarded-Proto"); xfproto != "https" { + if !isHTTPSRequest(r) { req = csrf.PlaintextHTTPRequest(r) } diff --git a/api/http/middlewares/plaintext_http_request_test.go b/api/http/middlewares/plaintext_http_request_test.go new file mode 100644 index 000000000..33912be80 --- /dev/null +++ b/api/http/middlewares/plaintext_http_request_test.go @@ -0,0 +1,173 @@ +package middlewares + +import ( + "testing" +) + +var tests = []struct { + name string + forwarded string + expected string +}{ + { + name: "empty header", + forwarded: "", + expected: "", + }, + { + name: "single proxy with proto=https", + forwarded: "proto=https", + expected: "https", + }, + { + name: "single proxy with proto=http", + forwarded: "proto=http", + expected: "http", + }, + { + name: "single proxy with multiple directives", + forwarded: "for=192.0.2.60;proto=https;by=203.0.113.43", + expected: "https", + }, + { + name: "single proxy with proto in middle", + forwarded: "for=192.0.2.60;proto=https;host=example.com", + expected: "https", + }, + { + name: "single proxy with proto at end", + forwarded: "for=192.0.2.60;host=example.com;proto=https", + expected: "https", + }, + { + name: "multiple proxies - takes first", + forwarded: "proto=https, proto=http", + expected: "https", + }, + { + name: "multiple proxies with complex format", + forwarded: "for=192.0.2.43;proto=https, for=198.51.100.17;proto=http", + expected: "https", + }, + { + name: "multiple proxies with for directive only", + forwarded: "for=192.0.2.43, for=198.51.100.17", + expected: "", + }, + { + name: "multiple proxies with proto only in second", + forwarded: "for=192.0.2.43, proto=https", + expected: "", + }, + { + name: "multiple proxies with proto only in first", + forwarded: "proto=https, for=198.51.100.17", + expected: "https", + }, + { + name: "quoted protocol value", + forwarded: "proto=\"https\"", + expected: "https", + }, + { + name: "single quoted protocol value", + forwarded: "proto='https'", + expected: "https", + }, + { + name: "mixed case protocol", + forwarded: "proto=HTTPS", + expected: "HTTPS", + }, + { + name: "no proto directive", + forwarded: "for=192.0.2.60;by=203.0.113.43", + expected: "", + }, + { + name: "empty proto value", + forwarded: "proto=", + expected: "", + }, + { + name: "whitespace around values", + forwarded: " proto = https ", + expected: "https", + }, + { + name: "whitespace around semicolons", + forwarded: "for=192.0.2.60 ; proto=https ; by=203.0.113.43", + expected: "https", + }, + { + name: "whitespace around commas", + forwarded: "proto=https , proto=http", + expected: "https", + }, + { + name: "IPv6 address in for directive", + forwarded: "for=\"[2001:db8:cafe::17]:4711\";proto=https", + expected: "https", + }, + { + name: "complex multiple proxies with IPv6", + forwarded: "for=192.0.2.43;proto=https, for=\"[2001:db8:cafe::17]\";proto=http", + expected: "https", + }, + { + name: "obfuscated identifiers", + forwarded: "for=_mdn;proto=https", + expected: "https", + }, + { + name: "unknown identifier", + forwarded: "for=unknown;proto=https", + expected: "https", + }, + { + name: "malformed key-value pair", + forwarded: "proto", + expected: "", + }, + { + name: "malformed key-value pair with equals", + forwarded: "proto=", + expected: "", + }, + { + name: "multiple equals signs", + forwarded: "proto=https=extra", + expected: "https=extra", + }, + { + name: "mixed case directive name", + forwarded: "PROTO=https", + expected: "https", + }, + { + name: "mixed case directive name with spaces", + forwarded: " Proto = https ", + expected: "https", + }, +} + +func TestParseForwardedHeaderProto(t *testing.T) { + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := parseForwardedHeaderProto(tt.forwarded) + if result != tt.expected { + t.Errorf("parseForwardedHeader(%q) = %q, want %q", tt.forwarded, result, tt.expected) + } + }) + } +} + +func FuzzParseForwardedHeaderProto(f *testing.F) { + for _, t := range tests { + f.Add(t.forwarded) + } + + f.Fuzz(func(t *testing.T, forwarded string) { + parseForwardedHeaderProto(forwarded) + }) +} diff --git a/api/http/models/kubernetes/application.go b/api/http/models/kubernetes/application.go index fcb49b23d..4759d9214 100644 --- a/api/http/models/kubernetes/application.go +++ b/api/http/models/kubernetes/application.go @@ -38,14 +38,30 @@ type K8sApplication struct { Labels map[string]string `json:"Labels,omitempty"` Resource K8sApplicationResource `json:"Resource,omitempty"` HorizontalPodAutoscaler *autoscalingv2.HorizontalPodAutoscaler `json:"HorizontalPodAutoscaler,omitempty"` + CustomResourceMetadata CustomResourceMetadata `json:"CustomResourceMetadata,omitempty"` } type Metadata struct { Labels map[string]string `json:"labels"` } +type CustomResourceMetadata struct { + Kind string `json:"kind"` + APIVersion string `json:"apiVersion"` + Plural string `json:"plural"` +} + type Pod struct { - Status string `json:"Status"` + Name string `json:"Name"` + ContainerName string `json:"ContainerName"` + Image string `json:"Image"` + ImagePullPolicy string `json:"ImagePullPolicy"` + Status string `json:"Status"` + NodeName string `json:"NodeName"` + PodIP string `json:"PodIP"` + UID string `json:"Uid"` + Resource K8sApplicationResource `json:"Resource,omitempty"` + CreationDate time.Time `json:"CreationDate"` } type Configuration struct { @@ -72,8 +88,8 @@ type TLSInfo struct { // Existing types type K8sApplicationResource struct { - CPURequest float64 `json:"CpuRequest"` - CPULimit float64 `json:"CpuLimit"` - MemoryRequest int64 `json:"MemoryRequest"` - MemoryLimit int64 `json:"MemoryLimit"` + CPURequest float64 `json:"CpuRequest,omitempty"` + CPULimit float64 `json:"CpuLimit,omitempty"` + MemoryRequest int64 `json:"MemoryRequest,omitempty"` + MemoryLimit int64 `json:"MemoryLimit,omitempty"` } diff --git a/api/http/proxy/factory/docker/access_control.go b/api/http/proxy/factory/docker/access_control.go index e945d38da..ac25a7b7a 100644 --- a/api/http/proxy/factory/docker/access_control.go +++ b/api/http/proxy/factory/docker/access_control.go @@ -35,7 +35,7 @@ type ( func getUniqueElements(items string) []string { xs := strings.Split(items, ",") xs = slicesx.Map(xs, strings.TrimSpace) - xs = slicesx.Filter(xs, func(x string) bool { return len(x) > 0 }) + xs = slicesx.FilterInPlace(xs, func(x string) bool { return len(x) > 0 }) return slicesx.Unique(xs) } diff --git a/api/http/proxy/factory/docker/registry.go b/api/http/proxy/factory/docker/registry.go index ecf7935f1..7036853c7 100644 --- a/api/http/proxy/factory/docker/registry.go +++ b/api/http/proxy/factory/docker/registry.go @@ -55,12 +55,13 @@ func createRegistryAuthenticationHeader( return } - if err = registryutils.EnsureRegTokenValid(dataStore, matchingRegistry); err != nil { + if err = registryutils.PrepareRegistryCredentials(dataStore, matchingRegistry); err != nil { return } authenticationHeader.Serveraddress = matchingRegistry.URL - authenticationHeader.Username, authenticationHeader.Password, err = registryutils.GetRegEffectiveCredential(matchingRegistry) + authenticationHeader.Username = matchingRegistry.Username + authenticationHeader.Password = matchingRegistry.Password return } diff --git a/api/http/proxy/factory/github/client.go b/api/http/proxy/factory/github/client.go new file mode 100644 index 000000000..74dcfb994 --- /dev/null +++ b/api/http/proxy/factory/github/client.go @@ -0,0 +1,108 @@ +package github + +import ( + "context" + "fmt" + "io" + "net/http" + "strings" + "time" + + "github.com/segmentio/encoding/json" + "oras.land/oras-go/v2/registry/remote/retry" +) + +const GitHubAPIHost = "https://api.github.com" + +// Package represents a GitHub container package +type Package struct { + Name string `json:"name"` + Owner struct { + Login string `json:"login"` + } `json:"owner"` +} + +// Client represents a GitHub API client +type Client struct { + httpClient *http.Client + baseURL string +} + +// NewClient creates a new GitHub API client +func NewClient(token string) *Client { + return &Client{ + httpClient: NewHTTPClient(token), + baseURL: GitHubAPIHost, + } +} + +// GetContainerPackages fetches container packages for the configured namespace +// It's a small http client wrapper instead of using the github client because listing repositories is the only known operation that isn't directly supported by oras +func (c *Client) GetContainerPackages(ctx context.Context, useOrganisation bool, organisationName string) ([]string, error) { + // Determine the namespace (user or organisation) for the request + namespace := "user" + if useOrganisation { + namespace = "orgs/" + organisationName + } + + // Build the full URL for listing container packages + url := fmt.Sprintf("%s/%s/packages?package_type=container", c.baseURL, namespace) + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return nil, fmt.Errorf("failed to create request: %w", err) + } + + resp, err := c.httpClient.Do(req) + if err != nil { + return nil, fmt.Errorf("failed to execute request: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("GitHub API returned status %d: %s", resp.StatusCode, resp.Status) + } + + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + + var packages []Package + if err := json.Unmarshal(body, &packages); err != nil { + return nil, fmt.Errorf("failed to parse response: %w", err) + } + + // Extract repository names in the form "owner/name" + repositories := make([]string, len(packages)) + for i, pkg := range packages { + repositories[i] = fmt.Sprintf("%s/%s", strings.ToLower(pkg.Owner.Login), strings.ToLower(pkg.Name)) + } + + return repositories, nil +} + +// NewHTTPClient creates a new HTTP client configured for GitHub API requests +func NewHTTPClient(token string) *http.Client { + return &http.Client{ + Transport: &tokenTransport{ + token: token, + transport: retry.NewTransport(&http.Transport{}), // Use ORAS retry transport for consistent rate limiting and error handling + }, + Timeout: 1 * time.Minute, + } +} + +// tokenTransport automatically adds the Bearer token header to requests +type tokenTransport struct { + token string + transport http.RoundTripper +} + +func (t *tokenTransport) RoundTrip(req *http.Request) (*http.Response, error) { + if t.token != "" { + req.Header.Set("Authorization", "Bearer "+t.token) + req.Header.Set("Accept", "application/vnd.github+json") + } + return t.transport.RoundTrip(req) +} diff --git a/api/http/proxy/factory/gitlab/client.go b/api/http/proxy/factory/gitlab/client.go new file mode 100644 index 000000000..13d07e18b --- /dev/null +++ b/api/http/proxy/factory/gitlab/client.go @@ -0,0 +1,130 @@ +package gitlab + +import ( + "context" + "errors" + "fmt" + "io" + "net/http" + "time" + + "github.com/segmentio/encoding/json" + "oras.land/oras-go/v2/registry/remote/retry" +) + +// Repository represents a GitLab registry repository +type Repository struct { + ID int `json:"id"` + Name string `json:"name"` + Path string `json:"path"` + ProjectID int `json:"project_id"` + Location string `json:"location"` + CreatedAt string `json:"created_at"` + Status string `json:"status"` +} + +// Client represents a GitLab API client +type Client struct { + httpClient *http.Client + baseURL string +} + +// NewClient creates a new GitLab API client +// it currently is an http client because only GetRegistryRepositoryNames is needed (oras supports other commands). +// if we need to support other commands, consider using the gitlab client library. +func NewClient(baseURL, token string) *Client { + return &Client{ + httpClient: NewHTTPClient(token), + baseURL: baseURL, + } +} + +// GetRegistryRepositoryNames fetches registry repository names for a given project. +// It's a small http client wrapper instead of using the gitlab client library because listing repositories is the only known operation that isn't directly supported by oras +func (c *Client) GetRegistryRepositoryNames(ctx context.Context, projectID int) ([]string, error) { + url := fmt.Sprintf("%s/api/v4/projects/%d/registry/repositories", c.baseURL, projectID) + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return nil, fmt.Errorf("failed to create request: %w", err) + } + + resp, err := c.httpClient.Do(req) + if err != nil { + return nil, fmt.Errorf("failed to execute request: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("GitLab API returned status %d: %s", resp.StatusCode, resp.Status) + } + + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + + var repositories []Repository + if err := json.Unmarshal(body, &repositories); err != nil { + return nil, fmt.Errorf("failed to parse response: %w", err) + } + + // Extract repository names + names := make([]string, len(repositories)) + for i, repo := range repositories { + // the full path is required for further repo operations + names[i] = repo.Path + } + + return names, nil +} + +type Transport struct { + httpTransport *http.Transport +} + +// NewTransport returns a pointer to a new instance of Transport that implements the HTTP Transport +// interface for proxying requests to the Gitlab API. +func NewTransport() *Transport { + return &Transport{ + httpTransport: &http.Transport{}, + } +} + +// RoundTrip is the implementation of the http.RoundTripper interface +func (transport *Transport) RoundTrip(request *http.Request) (*http.Response, error) { + token := request.Header.Get("Private-Token") + if token == "" { + return nil, errors.New("no gitlab token provided") + } + + r, err := http.NewRequest(request.Method, request.URL.String(), request.Body) + if err != nil { + return nil, err + } + + r.Header.Set("Private-Token", token) + return transport.httpTransport.RoundTrip(r) +} + +// NewHTTPClient creates a new HTTP client configured for GitLab API requests +func NewHTTPClient(token string) *http.Client { + return &http.Client{ + Transport: &tokenTransport{ + token: token, + transport: retry.NewTransport(&http.Transport{}), // Use ORAS retry transport for consistent rate limiting and error handling + }, + Timeout: 1 * time.Minute, + } +} + +// tokenTransport automatically adds the Private-Token header to requests +type tokenTransport struct { + token string + transport http.RoundTripper +} + +func (t *tokenTransport) RoundTrip(req *http.Request) (*http.Response, error) { + req.Header.Set("Private-Token", t.token) + return t.transport.RoundTrip(req) +} diff --git a/api/http/proxy/factory/gitlab/transport.go b/api/http/proxy/factory/gitlab/transport.go deleted file mode 100644 index 7e1804c45..000000000 --- a/api/http/proxy/factory/gitlab/transport.go +++ /dev/null @@ -1,34 +0,0 @@ -package gitlab - -import ( - "errors" - "net/http" -) - -type Transport struct { - httpTransport *http.Transport -} - -// NewTransport returns a pointer to a new instance of Transport that implements the HTTP Transport -// interface for proxying requests to the Gitlab API. -func NewTransport() *Transport { - return &Transport{ - httpTransport: &http.Transport{}, - } -} - -// RoundTrip is the implementation of the http.RoundTripper interface -func (transport *Transport) RoundTrip(request *http.Request) (*http.Response, error) { - token := request.Header.Get("Private-Token") - if token == "" { - return nil, errors.New("no gitlab token provided") - } - - r, err := http.NewRequest(request.Method, request.URL.String(), request.Body) - if err != nil { - return nil, err - } - - r.Header.Set("Private-Token", token) - return transport.httpTransport.RoundTrip(r) -} diff --git a/api/http/proxy/factory/kubernetes/transport.go b/api/http/proxy/factory/kubernetes/transport.go index 76e9daa68..ddab7a096 100644 --- a/api/http/proxy/factory/kubernetes/transport.go +++ b/api/http/proxy/factory/kubernetes/transport.go @@ -58,6 +58,7 @@ func (transport *baseTransport) proxyKubernetesRequest(request *http.Request) (* switch { case strings.EqualFold(requestPath, "/namespaces/portainer/configmaps/portainer-config") && (request.Method == "PUT" || request.Method == "POST"): + transport.k8sClientFactory.ClearClientCache() defer transport.tokenManager.UpdateUserServiceAccountsForEndpoint(portainer.EndpointID(endpointID)) return transport.executeKubernetesRequest(request) case strings.EqualFold(requestPath, "/namespaces"): diff --git a/api/http/security/bouncer.go b/api/http/security/bouncer.go index eb240692d..fa7360f4c 100644 --- a/api/http/security/bouncer.go +++ b/api/http/security/bouncer.go @@ -35,6 +35,7 @@ type ( JWTAuthLookup(*http.Request) (*portainer.TokenData, error) TrustedEdgeEnvironmentAccess(dataservices.DataStoreTx, *portainer.Endpoint) error RevokeJWT(string) + DisableCSP() } // RequestBouncer represents an entity that manages API request accesses @@ -72,7 +73,7 @@ func NewRequestBouncer(dataStore dataservices.DataStore, jwtService portainer.JW jwtService: jwtService, apiKeyService: apiKeyService, hsts: featureflags.IsEnabled("hsts"), - csp: featureflags.IsEnabled("csp"), + csp: true, } go b.cleanUpExpiredJWT() @@ -80,6 +81,11 @@ func NewRequestBouncer(dataStore dataservices.DataStore, jwtService portainer.JW return b } +// DisableCSP disables Content Security Policy +func (bouncer *RequestBouncer) DisableCSP() { + bouncer.csp = false +} + // PublicAccess defines a security check for public API endpoints. // No authentication is required to access these endpoints. func (bouncer *RequestBouncer) PublicAccess(h http.Handler) http.Handler { @@ -528,7 +534,7 @@ func MWSecureHeaders(next http.Handler, hsts, csp bool) http.Handler { } if csp { - w.Header().Set("Content-Security-Policy", "script-src 'self' cdn.matomo.cloud") + w.Header().Set("Content-Security-Policy", "script-src 'self' cdn.matomo.cloud; frame-ancestors 'none';") } w.Header().Set("X-Content-Type-Options", "nosniff") diff --git a/api/http/security/bouncer_test.go b/api/http/security/bouncer_test.go index 4d84dcfee..3dd42fdc5 100644 --- a/api/http/security/bouncer_test.go +++ b/api/http/security/bouncer_test.go @@ -530,3 +530,34 @@ func TestJWTRevocation(t *testing.T) { require.Equal(t, 1, revokeLen()) } + +func TestCSPHeaderDefault(t *testing.T) { + b := NewRequestBouncer(nil, nil, nil) + + srv := httptest.NewServer( + b.PublicAccess(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})), + ) + defer srv.Close() + + resp, err := http.Get(srv.URL + "/") + require.NoError(t, err) + defer resp.Body.Close() + + require.Contains(t, resp.Header, "Content-Security-Policy") +} + +func TestCSPHeaderDisabled(t *testing.T) { + b := NewRequestBouncer(nil, nil, nil) + b.DisableCSP() + + srv := httptest.NewServer( + b.PublicAccess(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})), + ) + defer srv.Close() + + resp, err := http.Get(srv.URL + "/") + require.NoError(t, err) + defer resp.Body.Close() + + require.NotContains(t, resp.Header, "Content-Security-Policy") +} diff --git a/api/http/server.go b/api/http/server.go index 88d131650..8f073ce58 100644 --- a/api/http/server.go +++ b/api/http/server.go @@ -77,6 +77,7 @@ type Server struct { AuthorizationService *authorization.Service BindAddress string BindAddressHTTPS string + CSP bool HTTPEnabled bool AssetsPath string Status *portainer.Status @@ -113,6 +114,7 @@ type Server struct { PendingActionsService *pendingactions.PendingActionsService PlatformService platform.Service PullLimitCheckDisabled bool + TrustedOrigins []string } // Start starts the HTTP server @@ -120,13 +122,16 @@ func (server *Server) Start() error { kubernetesTokenCacheManager := server.KubernetesTokenCacheManager requestBouncer := security.NewRequestBouncer(server.DataStore, server.JWTService, server.APIKeyService) + if !server.CSP { + requestBouncer.DisableCSP() + } rateLimiter := security.NewRateLimiter(10, 1*time.Second, 1*time.Hour) offlineGate := offlinegate.NewOfflineGate() passwordStrengthChecker := security.NewPasswordStrengthChecker(server.DataStore.Settings()) - var authHandler = auth.NewHandler(requestBouncer, rateLimiter, passwordStrengthChecker) + var authHandler = auth.NewHandler(requestBouncer, rateLimiter, passwordStrengthChecker, server.KubernetesClientFactory) authHandler.DataStore = server.DataStore authHandler.CryptoService = server.CryptoService authHandler.JWTService = server.JWTService @@ -199,7 +204,7 @@ func (server *Server) Start() error { var dockerHandler = dockerhandler.NewHandler(requestBouncer, server.AuthorizationService, server.DataStore, server.DockerClientFactory, containerService) - var fileHandler = file.NewHandler(filepath.Join(server.AssetsPath, "public"), adminMonitor.WasInstanceDisabled) + var fileHandler = file.NewHandler(filepath.Join(server.AssetsPath, "public"), server.CSP, adminMonitor.WasInstanceDisabled) var endpointHelmHandler = helm.NewHandler(requestBouncer, server.DataStore, server.JWTService, server.KubernetesDeployer, server.HelmPackageManager, server.KubeClusterAccessService) @@ -336,7 +341,7 @@ func (server *Server) Start() error { handler = middlewares.WithPanicLogger(middlewares.WithSlowRequestsLogger(handler)) - handler, err := csrf.WithProtect(handler) + handler, err := csrf.WithProtect(handler, server.TrustedOrigins) if err != nil { return errors.Wrap(err, "failed to create CSRF middleware") } diff --git a/api/internal/edge/edgestacks/service.go b/api/internal/edge/edgestacks/service.go index 5932a5ec8..c0ecb5caf 100644 --- a/api/internal/edge/edgestacks/service.go +++ b/api/internal/edge/edgestacks/service.go @@ -129,9 +129,6 @@ func (service *Service) updateEndpointRelations(tx dataservices.DataStoreTx, edg for _, endpointID := range relatedEndpointIds { relation, err := endpointRelationService.EndpointRelation(endpointID) if err != nil { - if tx.IsErrObjectNotFound(err) { - continue - } return fmt.Errorf("unable to find endpoint relation in database: %w", err) } diff --git a/api/internal/endpointutils/endpointutils.go b/api/internal/endpointutils/endpointutils.go index 6b7eb1c2d..f596ae0d5 100644 --- a/api/internal/endpointutils/endpointutils.go +++ b/api/internal/endpointutils/endpointutils.go @@ -249,3 +249,19 @@ func getEndpointCheckinInterval(endpoint *portainer.Endpoint, settings *portaine return defaultInterval } + +func InitializeEdgeEndpointRelation(endpoint *portainer.Endpoint, tx dataservices.DataStoreTx) error { + if !IsEdgeEndpoint(endpoint) { + return nil + } + + relation := &portainer.EndpointRelation{ + EndpointID: endpoint.ID, + EdgeStacks: make(map[portainer.EdgeStackID]bool), + } + + if err := tx.EndpointRelation().Create(relation); err != nil { + return err + } + return nil +} diff --git a/api/internal/registryutils/access/access.go b/api/internal/registryutils/access/access.go index 0d14cba39..bfa5181c0 100644 --- a/api/internal/registryutils/access/access.go +++ b/api/internal/registryutils/access/access.go @@ -2,40 +2,82 @@ package access import ( "errors" + "fmt" portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/http/security" + "github.com/portainer/portainer/api/internal/endpointutils" + "github.com/portainer/portainer/api/kubernetes" + "github.com/portainer/portainer/api/kubernetes/cli" ) func hasPermission( dataStore dataservices.DataStore, + k8sClientFactory *cli.ClientFactory, userID portainer.UserID, endpointID portainer.EndpointID, registry *portainer.Registry, ) (hasPermission bool, err error) { user, err := dataStore.User().Read(userID) if err != nil { - return + return false, err } if user.Role == portainer.AdministratorRole { - return true, err + return true, nil + } + + endpoint, err := dataStore.Endpoint().Endpoint(endpointID) + if err != nil { + return false, err } teamMemberships, err := dataStore.TeamMembership().TeamMembershipsByUserID(userID) if err != nil { - return + return false, err } + // validate access for kubernetes namespaces (leverage registry.RegistryAccesses[endpointId].Namespaces) + if endpointutils.IsKubernetesEndpoint(endpoint) && k8sClientFactory != nil { + kcl, err := k8sClientFactory.GetPrivilegedKubeClient(endpoint) + if err != nil { + return false, fmt.Errorf("unable to retrieve kubernetes client to validate registry access: %w", err) + } + accessPolicies, err := kcl.GetNamespaceAccessPolicies() + if err != nil { + return false, fmt.Errorf("unable to retrieve environment's namespaces policies to validate registry access: %w", err) + } + + authorizedNamespaces := registry.RegistryAccesses[endpointID].Namespaces + + for _, namespace := range authorizedNamespaces { + // when the default namespace is authorized to use a registry, all users have the ability to use it + // unless the default namespace is restricted: in this case continue to search for other potential accesses authorizations + if namespace == kubernetes.DefaultNamespace && !endpoint.Kubernetes.Configuration.RestrictDefaultNamespace { + return true, nil + } + + namespacePolicy := accessPolicies[namespace] + if security.AuthorizedAccess(user.ID, teamMemberships, namespacePolicy.UserAccessPolicies, namespacePolicy.TeamAccessPolicies) { + return true, nil + } + } + return false, nil + } + + // validate access for docker environments + // leverage registry.RegistryAccesses[endpointId].UserAccessPolicies (direct access) + // and registry.RegistryAccesses[endpointId].TeamAccessPolicies (indirect access via his teams) hasPermission = security.AuthorizedRegistryAccess(registry, user, teamMemberships, endpointID) - return + return hasPermission, nil } // GetAccessibleRegistry get the registry if the user has permission func GetAccessibleRegistry( dataStore dataservices.DataStore, + k8sClientFactory *cli.ClientFactory, userID portainer.UserID, endpointID portainer.EndpointID, registryID portainer.RegistryID, @@ -46,7 +88,7 @@ func GetAccessibleRegistry( return } - hasPermission, err := hasPermission(dataStore, userID, endpointID, registry) + hasPermission, err := hasPermission(dataStore, k8sClientFactory, userID, endpointID, registry) if err != nil { return } diff --git a/api/internal/registryutils/ecr_reg_token.go b/api/internal/registryutils/ecr_reg_token.go index cbcceb982..6e9a754bf 100644 --- a/api/internal/registryutils/ecr_reg_token.go +++ b/api/internal/registryutils/ecr_reg_token.go @@ -62,3 +62,26 @@ func GetRegEffectiveCredential(registry *portainer.Registry) (username, password return } + +// PrepareRegistryCredentials consolidates the common pattern of ensuring valid ECR token +// and setting effective credentials on the registry when authentication is enabled. +// This function modifies the registry in-place by setting Username and Password to the effective values. +func PrepareRegistryCredentials(tx dataservices.DataStoreTx, registry *portainer.Registry) error { + if !registry.Authentication { + return nil + } + + if err := EnsureRegTokenValid(tx, registry); err != nil { + return err + } + + username, password, err := GetRegEffectiveCredential(registry) + if err != nil { + return err + } + + registry.Username = username + registry.Password = password + + return nil +} diff --git a/api/internal/testhelpers/datastore.go b/api/internal/testhelpers/datastore.go index 392f21e97..19254f540 100644 --- a/api/internal/testhelpers/datastore.go +++ b/api/internal/testhelpers/datastore.go @@ -7,6 +7,7 @@ import ( "github.com/portainer/portainer/api/database" "github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/dataservices/errors" + "github.com/portainer/portainer/api/slicesx" ) var _ dataservices.DataStore = &testDatastore{} @@ -152,8 +153,17 @@ type stubUserService struct { users []portainer.User } -func (s *stubUserService) BucketName() string { return "users" } -func (s *stubUserService) ReadAll() ([]portainer.User, error) { return s.users, nil } +func (s *stubUserService) BucketName() string { return "users" } +func (s *stubUserService) ReadAll(predicates ...func(portainer.User) bool) ([]portainer.User, error) { + filtered := s.users + + for _, p := range predicates { + filtered = slicesx.Filter(filtered, p) + } + + return filtered, nil +} + func (s *stubUserService) UsersByRole(role portainer.UserRole) ([]portainer.User, error) { return s.users, nil } @@ -171,8 +181,16 @@ type stubEdgeJobService struct { jobs []portainer.EdgeJob } -func (s *stubEdgeJobService) BucketName() string { return "edgejobs" } -func (s *stubEdgeJobService) ReadAll() ([]portainer.EdgeJob, error) { return s.jobs, nil } +func (s *stubEdgeJobService) BucketName() string { return "edgejobs" } +func (s *stubEdgeJobService) ReadAll(predicates ...func(portainer.EdgeJob) bool) ([]portainer.EdgeJob, error) { + filtered := s.jobs + + for _, p := range predicates { + filtered = slicesx.Filter(filtered, p) + } + + return filtered, nil +} // WithEdgeJobs option will instruct testDatastore to return provided jobs func WithEdgeJobs(js []portainer.EdgeJob) datastoreOption { @@ -362,8 +380,14 @@ func (s *stubStacksService) Read(ID portainer.StackID) (*portainer.Stack, error) return nil, errors.ErrObjectNotFound } -func (s *stubStacksService) ReadAll() ([]portainer.Stack, error) { - return s.stacks, nil +func (s *stubStacksService) ReadAll(predicates ...func(portainer.Stack) bool) ([]portainer.Stack, error) { + filtered := s.stacks + + for _, p := range predicates { + filtered = slicesx.Filter(filtered, p) + } + + return filtered, nil } func (s *stubStacksService) StacksByEndpointID(endpointID portainer.EndpointID) ([]portainer.Stack, error) { diff --git a/api/internal/testhelpers/request_bouncer.go b/api/internal/testhelpers/request_bouncer.go index b89154549..0586dffef 100644 --- a/api/internal/testhelpers/request_bouncer.go +++ b/api/internal/testhelpers/request_bouncer.go @@ -60,6 +60,8 @@ func (testRequestBouncer) JWTAuthLookup(r *http.Request) (*portainer.TokenData, func (testRequestBouncer) RevokeJWT(jti string) {} +func (testRequestBouncer) DisableCSP() {} + // AddTestSecurityCookie adds a security cookie to the request func AddTestSecurityCookie(r *http.Request, jwt string) { r.AddCookie(&http.Cookie{ diff --git a/api/kubernetes/cli/client.go b/api/kubernetes/cli/client.go index a40a865f1..550ade1d3 100644 --- a/api/kubernetes/cli/client.go +++ b/api/kubernetes/cli/client.go @@ -77,9 +77,26 @@ func (factory *ClientFactory) ClearClientCache() { factory.endpointProxyClients.Flush() } +// ClearClientCache removes all cached kube clients for a userId +func (factory *ClientFactory) ClearUserClientCache(userID string) { + for key := range factory.endpointProxyClients.Items() { + if strings.HasSuffix(key, "."+userID) { + factory.endpointProxyClients.Delete(key) + } + } +} + // Remove the cached kube client so a new one can be created func (factory *ClientFactory) RemoveKubeClient(endpointID portainer.EndpointID) { factory.endpointProxyClients.Delete(strconv.Itoa(int(endpointID))) + + endpointPrefix := strconv.Itoa(int(endpointID)) + "." + + for key := range factory.endpointProxyClients.Items() { + if strings.HasPrefix(key, endpointPrefix) { + factory.endpointProxyClients.Delete(key) + } + } } func (factory *ClientFactory) GetAddrHTTPS() string { @@ -104,6 +121,24 @@ func (factory *ClientFactory) GetPrivilegedKubeClient(endpoint *portainer.Endpoi return kcl, nil } +// GetPrivilegedUserKubeClient checks if an existing admin client is already registered for the environment(endpoint) and user and returns it if one is found. +// If no client is registered, it will create a new client, register it, and returns it. +func (factory *ClientFactory) GetPrivilegedUserKubeClient(endpoint *portainer.Endpoint, userID string) (*KubeClient, error) { + key := strconv.Itoa(int(endpoint.ID)) + ".admin." + userID + pcl, ok := factory.endpointProxyClients.Get(key) + if ok { + return pcl.(*KubeClient), nil + } + + kcl, err := factory.createCachedPrivilegedKubeClient(endpoint) + if err != nil { + return nil, err + } + + factory.endpointProxyClients.Set(key, kcl, cache.DefaultExpiration) + return kcl, nil +} + // GetProxyKubeClient retrieves a KubeClient from the cache. You should be // calling SetProxyKubeClient before first. It is normally, called the // kubernetes middleware. @@ -156,8 +191,9 @@ func (factory *ClientFactory) createCachedPrivilegedKubeClient(endpoint *portain } return &KubeClient{ - cli: cli, - instanceID: factory.instanceID, + cli: cli, + instanceID: factory.instanceID, + IsKubeAdmin: true, }, nil } diff --git a/api/kubernetes/cli/client_test.go b/api/kubernetes/cli/client_test.go new file mode 100644 index 000000000..993a966e3 --- /dev/null +++ b/api/kubernetes/cli/client_test.go @@ -0,0 +1,22 @@ +package cli + +import ( + "testing" +) + +func TestClearUserClientCache(t *testing.T) { + factory, _ := NewClientFactory(nil, nil, nil, "", "", "") + kcl := &KubeClient{} + factory.endpointProxyClients.Set("12.1", kcl, 0) + factory.endpointProxyClients.Set("12.12", kcl, 0) + factory.endpointProxyClients.Set("12", kcl, 0) + + factory.ClearUserClientCache("12") + + if len(factory.endpointProxyClients.Items()) != 2 { + t.Errorf("Incorrect clients cached after clearUserClientCache;\ngot=\n%d\nwant=\n%d", len(factory.endpointProxyClients.Items()), 2) + } + if _, ok := factory.GetProxyKubeClient("12", "12"); ok { + t.Errorf("Expected not to find client cache for user after clear") + } +} diff --git a/api/kubernetes/cli/namespace.go b/api/kubernetes/cli/namespace.go index 11307d651..560b91e75 100644 --- a/api/kubernetes/cli/namespace.go +++ b/api/kubernetes/cli/namespace.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "sort" "strconv" "time" @@ -351,6 +352,34 @@ func (kcl *KubeClient) DeleteNamespace(namespaceName string) (*corev1.Namespace, return namespace, nil } +// CombineNamespacesWithUnhealthyEvents combines namespaces with unhealthy events across all namespaces +func (kcl *KubeClient) CombineNamespacesWithUnhealthyEvents(namespaces map[string]portainer.K8sNamespaceInfo) (map[string]portainer.K8sNamespaceInfo, error) { + allEvents, err := kcl.GetEvents("", "") + if err != nil && !k8serrors.IsNotFound(err) { + log.Error(). + Str("context", "CombineNamespacesWithUnhealthyEvents"). + Err(err). + Msg("unable to retrieve unhealthy events from the Kubernetes for an admin user") + return nil, err + } + + unhealthyEventCounts := make(map[string]int) + for _, event := range allEvents { + if event.Type == "Warning" { + unhealthyEventCounts[event.Namespace]++ + } + } + + for namespaceName, namespace := range namespaces { + if count, exists := unhealthyEventCounts[namespaceName]; exists { + namespace.UnhealthyEventCount = count + namespaces[namespaceName] = namespace + } + } + + return namespaces, nil +} + // CombineNamespacesWithResourceQuotas combines namespaces with resource quotas where matching is based on "portainer-rq-"+namespace.Name func (kcl *KubeClient) CombineNamespacesWithResourceQuotas(namespaces map[string]portainer.K8sNamespaceInfo, w http.ResponseWriter) *httperror.HandlerError { resourceQuotas, err := kcl.GetResourceQuotas("") @@ -409,5 +438,10 @@ func (kcl *KubeClient) ConvertNamespaceMapToSlice(namespaces map[string]portaine namespaceSlice = append(namespaceSlice, namespace) } + // Sort namespaces by name + sort.Slice(namespaceSlice, func(i, j int) bool { + return namespaceSlice[i].Name < namespaceSlice[j].Name + }) + return namespaceSlice } diff --git a/api/portainer.go b/api/portainer.go index b3b917a08..8172f08fa 100644 --- a/api/portainer.go +++ b/api/portainer.go @@ -110,6 +110,7 @@ type ( AdminPassword *string AdminPasswordFile *string Assets *string + CSP *bool Data *string FeatureFlags *[]string EnableEdgeComputeFeatures *bool @@ -139,6 +140,7 @@ type ( LogMode *string KubectlShellImage *string PullLimitCheckDisabled *bool + TrustedOrigins *string } // CustomTemplateVariableDefinition @@ -213,26 +215,34 @@ type ( // DockerSnapshot represents a snapshot of a specific Docker environment(endpoint) at a specific time DockerSnapshot struct { - Time int64 `json:"Time"` - DockerVersion string `json:"DockerVersion"` - Swarm bool `json:"Swarm"` - TotalCPU int `json:"TotalCPU"` - TotalMemory int64 `json:"TotalMemory"` - ContainerCount int `json:"ContainerCount"` - RunningContainerCount int `json:"RunningContainerCount"` - StoppedContainerCount int `json:"StoppedContainerCount"` - HealthyContainerCount int `json:"HealthyContainerCount"` - UnhealthyContainerCount int `json:"UnhealthyContainerCount"` - VolumeCount int `json:"VolumeCount"` - ImageCount int `json:"ImageCount"` - ServiceCount int `json:"ServiceCount"` - StackCount int `json:"StackCount"` - SnapshotRaw DockerSnapshotRaw `json:"DockerSnapshotRaw"` - NodeCount int `json:"NodeCount"` - GpuUseAll bool `json:"GpuUseAll"` - GpuUseList []string `json:"GpuUseList"` - IsPodman bool `json:"IsPodman"` - DiagnosticsData *DiagnosticsData `json:"DiagnosticsData"` + Time int64 `json:"Time"` + DockerVersion string `json:"DockerVersion"` + Swarm bool `json:"Swarm"` + TotalCPU int `json:"TotalCPU"` + TotalMemory int64 `json:"TotalMemory"` + ContainerCount int `json:"ContainerCount"` + RunningContainerCount int `json:"RunningContainerCount"` + StoppedContainerCount int `json:"StoppedContainerCount"` + HealthyContainerCount int `json:"HealthyContainerCount"` + UnhealthyContainerCount int `json:"UnhealthyContainerCount"` + VolumeCount int `json:"VolumeCount"` + ImageCount int `json:"ImageCount"` + ServiceCount int `json:"ServiceCount"` + StackCount int `json:"StackCount"` + SnapshotRaw DockerSnapshotRaw `json:"DockerSnapshotRaw"` + NodeCount int `json:"NodeCount"` + GpuUseAll bool `json:"GpuUseAll"` + GpuUseList []string `json:"GpuUseList"` + IsPodman bool `json:"IsPodman"` + DiagnosticsData *DiagnosticsData `json:"DiagnosticsData"` + PerformanceMetrics *PerformanceMetrics `json:"PerformanceMetrics"` + } + + // PerformanceMetrics represents the performance metrics of a Docker, Swarm, Podman, and Kubernetes environments + PerformanceMetrics struct { + CPUUsage float64 `json:"CPUUsage,omitempty"` + MemoryUsage float64 `json:"MemoryUsage,omitempty"` + NetworkUsage float64 `json:"NetworkUsage,omitempty"` } // DockerContainerSnapshot is an extent of Docker's Container struct @@ -593,6 +603,12 @@ type ( ProjectPath string `json:"ProjectPath"` } + // GithubRegistryData represents data required for Github registry to work + GithubRegistryData struct { + UseOrganisation bool `json:"UseOrganisation"` + OrganisationName string `json:"OrganisationName"` + } + HelmUserRepositoryID int // HelmUserRepositories stores a Helm repository URL for the given user @@ -620,15 +636,16 @@ type ( JobType int K8sNamespaceInfo struct { - Id string `json:"Id"` - Name string `json:"Name"` - Status corev1.NamespaceStatus `json:"Status"` - Annotations map[string]string `json:"Annotations"` - CreationDate string `json:"CreationDate"` - NamespaceOwner string `json:"NamespaceOwner"` - IsSystem bool `json:"IsSystem"` - IsDefault bool `json:"IsDefault"` - ResourceQuota *corev1.ResourceQuota `json:"ResourceQuota"` + Id string `json:"Id"` + Name string `json:"Name"` + Status corev1.NamespaceStatus `json:"Status"` + Annotations map[string]string `json:"Annotations"` + CreationDate string `json:"CreationDate"` + UnhealthyEventCount int `json:"UnhealthyEventCount"` + NamespaceOwner string `json:"NamespaceOwner"` + IsSystem bool `json:"IsSystem"` + IsDefault bool `json:"IsDefault"` + ResourceQuota *corev1.ResourceQuota `json:"ResourceQuota"` } K8sNodeLimits struct { @@ -660,12 +677,13 @@ type ( // KubernetesSnapshot represents a snapshot of a specific Kubernetes environment(endpoint) at a specific time KubernetesSnapshot struct { - Time int64 `json:"Time"` - KubernetesVersion string `json:"KubernetesVersion"` - NodeCount int `json:"NodeCount"` - TotalCPU int64 `json:"TotalCPU"` - TotalMemory int64 `json:"TotalMemory"` - DiagnosticsData *DiagnosticsData `json:"DiagnosticsData"` + Time int64 `json:"Time"` + KubernetesVersion string `json:"KubernetesVersion"` + NodeCount int `json:"NodeCount"` + TotalCPU int64 `json:"TotalCPU"` + TotalMemory int64 `json:"TotalMemory"` + DiagnosticsData *DiagnosticsData `json:"DiagnosticsData"` + PerformanceMetrics *PerformanceMetrics `json:"PerformanceMetrics"` } // KubernetesConfiguration represents the configuration of a Kubernetes environment(endpoint) @@ -811,6 +829,7 @@ type ( Password string `json:"Password,omitempty" example:"registry_password"` ManagementConfiguration *RegistryManagementConfiguration `json:"ManagementConfiguration"` Gitlab GitlabRegistryData `json:"Gitlab"` + Github GithubRegistryData `json:"Github"` Quay QuayRegistryData `json:"Quay"` Ecr EcrData `json:"Ecr"` RegistryAccesses RegistryAccesses `json:"RegistryAccesses"` @@ -1728,7 +1747,7 @@ type ( const ( // APIVersion is the version number of the Portainer API - APIVersion = "2.31.0" + APIVersion = "2.32.0" // Support annotation for the API version ("STS" for Short-Term Support or "LTS" for Long-Term Support) APIVersionSupport = "STS" // Edition is what this edition of Portainer is called @@ -1787,6 +1806,10 @@ const ( LicenseServerBaseURL = "https://api.portainer.io" // URL to validate licenses along with system metadata. LicenseCheckInURL = LicenseServerBaseURL + "/licenses/checkin" + // TrustedOriginsEnvVar is the environment variable used to set the trusted origins for CSRF protection + TrustedOriginsEnvVar = "TRUSTED_ORIGINS" + // CSPEnvVar is the environment variable used to enable/disable the Content Security Policy + CSPEnvVar = "CSP" ) // List of supported features @@ -1956,6 +1979,8 @@ const ( DockerHubRegistry // EcrRegistry represents an ECR registry EcrRegistry + // Github container registry + GithubRegistry ) const ( diff --git a/api/slicesx/filter.go b/api/slicesx/filter.go new file mode 100644 index 000000000..13dc12105 --- /dev/null +++ b/api/slicesx/filter.go @@ -0,0 +1,28 @@ +package slicesx + +// Iterates over elements of collection, returning an array of all elements predicate returns truthy for. +// +// Note: Unlike `FilterInPlace`, this method returns a new array. +func Filter[T any](input []T, predicate func(T) bool) []T { + result := make([]T, 0) + for i := range input { + if predicate(input[i]) { + result = append(result, input[i]) + } + } + return result +} + +// Filter in place all elements from input that predicate returns truthy for and returns an array of the removed elements. +// +// Note: Unlike `Filter`, this method mutates input. +func FilterInPlace[T any](input []T, predicate func(T) bool) []T { + n := 0 + for _, v := range input { + if predicate(v) { + input[n] = v + n++ + } + } + return input[:n] +} diff --git a/api/slicesx/filter_test.go b/api/slicesx/filter_test.go new file mode 100644 index 000000000..36f97fa10 --- /dev/null +++ b/api/slicesx/filter_test.go @@ -0,0 +1,96 @@ +package slicesx_test + +import ( + "testing" + + "github.com/portainer/portainer/api/slicesx" +) + +func Test_Filter(t *testing.T) { + test(t, slicesx.Filter, "Filter even numbers", + []int{1, 2, 3, 4, 5, 6, 7, 8, 9}, + []int{2, 4, 6, 8}, + func(x int) bool { return x%2 == 0 }, + ) + test(t, slicesx.Filter, "Filter odd numbers", + []int{1, 2, 3, 4, 5, 6, 7, 8, 9}, + []int{1, 3, 5, 7, 9}, + func(x int) bool { return x%2 == 1 }, + ) + test(t, slicesx.Filter, "Filter strings starting with 'A'", + []string{"Apple", "Banana", "Avocado", "Grapes", "Apricot"}, + []string{"Apple", "Avocado", "Apricot"}, + func(s string) bool { return s[0] == 'A' }, + ) + test(t, slicesx.Filter, "Filter strings longer than 5 chars", + []string{"Apple", "Banana", "Avocado", "Grapes", "Apricot"}, + []string{"Banana", "Avocado", "Grapes", "Apricot"}, + func(s string) bool { return len(s) > 5 }, + ) +} + +func Test_Retain(t *testing.T) { + test(t, slicesx.FilterInPlace, "Filter even numbers", + []int{1, 2, 3, 4, 5, 6, 7, 8, 9}, + []int{2, 4, 6, 8}, + func(x int) bool { return x%2 == 0 }, + ) + test(t, slicesx.FilterInPlace, "Filter odd numbers", + []int{1, 2, 3, 4, 5, 6, 7, 8, 9}, + []int{1, 3, 5, 7, 9}, + func(x int) bool { return x%2 == 1 }, + ) + test(t, slicesx.FilterInPlace, "Filter strings starting with 'A'", + []string{"Apple", "Banana", "Avocado", "Grapes", "Apricot"}, + []string{"Apple", "Avocado", "Apricot"}, + func(s string) bool { return s[0] == 'A' }, + ) + test(t, slicesx.FilterInPlace, "Filter strings longer than 5 chars", + []string{"Apple", "Banana", "Avocado", "Grapes", "Apricot"}, + []string{"Banana", "Avocado", "Grapes", "Apricot"}, + func(s string) bool { return len(s) > 5 }, + ) +} + +func Benchmark_Filter(b *testing.B) { + n := 100000 + + source := make([]int, n) + for i := range source { + source[i] = i + } + + b.ResetTimer() + for range b.N { + e := slicesx.Filter(source, func(x int) bool { return x%2 == 0 }) + if len(e) != n/2 { + b.FailNow() + } + } +} + +func Benchmark_FilterInPlace(b *testing.B) { + n := 100000 + + source := make([]int, n) + for i := range source { + source[i] = i + } + + // Preallocate all copies before timing + // because FilterInPlace mutates the original slice + copies := make([][]int, b.N) + for i := range b.N { + buf := make([]int, len(source)) + copy(buf, source) + copies[i] = buf + } + + b.ResetTimer() + for i := range b.N { + e := slicesx.FilterInPlace(copies[i], func(x int) bool { return x%2 == 0 }) + if len(e) != n/2 { + b.FailNow() + } + } +} diff --git a/api/slicesx/flatten.go b/api/slicesx/flatten.go new file mode 100644 index 000000000..56a77f3e9 --- /dev/null +++ b/api/slicesx/flatten.go @@ -0,0 +1,7 @@ +package slicesx + +import "slices" + +func Flatten[T any](input [][]T) []T { + return slices.Concat(input...) +} diff --git a/api/slicesx/flatten_test.go b/api/slicesx/flatten_test.go new file mode 100644 index 000000000..6875c4e6b --- /dev/null +++ b/api/slicesx/flatten_test.go @@ -0,0 +1,19 @@ +package slicesx_test + +import ( + "testing" + + "github.com/portainer/portainer/api/slicesx" + "github.com/stretchr/testify/assert" +) + +func Test_Flatten(t *testing.T) { + t.Run("Flatten an array of arrays", func(t *testing.T) { + is := assert.New(t) + + source := [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} + expected := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} + is.ElementsMatch(slicesx.Flatten(source), expected) + + }) +} diff --git a/api/slicesx/includes.go b/api/slicesx/includes.go new file mode 100644 index 000000000..377a54215 --- /dev/null +++ b/api/slicesx/includes.go @@ -0,0 +1,17 @@ +package slicesx + +import "slices" + +// Checks if predicate returns truthy for any element of input. Iteration is stopped once predicate returns truthy. +func Some[T any](input []T, predicate func(T) bool) bool { + return slices.ContainsFunc(input, predicate) +} + +// Checks if predicate returns truthy for all elements of input. Iteration is stopped once predicate returns falsey. +// +// Note: This method returns true for empty collections because everything is true of elements of empty collections. +// https://en.wikipedia.org/wiki/Vacuous_truth +func Every[T any](input []T, predicate func(T) bool) bool { + // if the slice doesn't contain an inverted predicate then all items follow the predicate + return !slices.ContainsFunc(input, func(t T) bool { return !predicate(t) }) +} diff --git a/api/slicesx/includes_test.go b/api/slicesx/includes_test.go new file mode 100644 index 000000000..a3f074c1c --- /dev/null +++ b/api/slicesx/includes_test.go @@ -0,0 +1,76 @@ +package slicesx_test + +import ( + "testing" + + "github.com/portainer/portainer/api/slicesx" +) + +func Test_Every(t *testing.T) { + test(t, slicesx.Every, "All start with an A (ok)", + []string{"Apple", "Avocado", "Apricot"}, + true, + func(s string) bool { return s[0] == 'A' }, + ) + test(t, slicesx.Every, "All start with an A (ko = some don't start with A)", + []string{"Apple", "Avocado", "Banana"}, + false, + func(s string) bool { return s[0] == 'A' }, + ) + test(t, slicesx.Every, "All are under 5 (ok)", + []int{1, 2, 3}, + true, + func(i int) bool { return i < 5 }, + ) + test(t, slicesx.Every, "All are under 5 (ko = some above 10)", + []int{1, 2, 10}, + false, + func(i int) bool { return i < 5 }, + ) + test(t, slicesx.Every, "All are true (ok)", + []struct{ x bool }{{x: true}, {x: true}, {x: true}}, + true, + func(s struct{ x bool }) bool { return s.x }) + test(t, slicesx.Every, "All are true (ko = some are false)", + []struct{ x bool }{{x: true}, {x: true}, {x: false}}, + false, + func(s struct{ x bool }) bool { return s.x }) + test(t, slicesx.Every, "Must be true on empty slice", + []int{}, + true, + func(i int) bool { return i%2 == 0 }, + ) +} + +func Test_Some(t *testing.T) { + test(t, slicesx.Some, "Some start with an A (ok)", + []string{"Apple", "Avocado", "Banana"}, + true, + func(s string) bool { return s[0] == 'A' }, + ) + test(t, slicesx.Some, "Some start with an A (ko = all don't start with A)", + []string{"Banana", "Cherry", "Peach"}, + false, + func(s string) bool { return s[0] == 'A' }, + ) + test(t, slicesx.Some, "Some are under 5 (ok)", + []int{1, 2, 30}, + true, + func(i int) bool { return i < 5 }, + ) + test(t, slicesx.Some, "Some are under 5 (ko = all above 5)", + []int{10, 11, 12}, + false, + func(i int) bool { return i < 5 }, + ) + test(t, slicesx.Some, "Some are true (ok)", + []struct{ x bool }{{x: true}, {x: true}, {x: false}}, + true, + func(s struct{ x bool }) bool { return s.x }, + ) + test(t, slicesx.Some, "Some are true (ko = all are false)", + []struct{ x bool }{{x: false}, {x: false}, {x: false}}, + false, + func(s struct{ x bool }) bool { return s.x }, + ) +} diff --git a/api/slicesx/map.go b/api/slicesx/map.go new file mode 100644 index 000000000..7e24bdd0d --- /dev/null +++ b/api/slicesx/map.go @@ -0,0 +1,15 @@ +package slicesx + +// Map applies the given function to each element of the slice and returns a new slice with the results +func Map[T, U any](s []T, f func(T) U) []U { + result := make([]U, len(s)) + for i, v := range s { + result[i] = f(v) + } + return result +} + +// FlatMap applies the given function to each element of the slice and returns a new slice with the flattened results +func FlatMap[T, U any](s []T, f func(T) []U) []U { + return Flatten(Map(s, f)) +} diff --git a/api/slicesx/map_test.go b/api/slicesx/map_test.go new file mode 100644 index 000000000..a2cd2256d --- /dev/null +++ b/api/slicesx/map_test.go @@ -0,0 +1,43 @@ +package slicesx_test + +import ( + "strconv" + "testing" + + "github.com/portainer/portainer/api/slicesx" +) + +func Test_Map(t *testing.T) { + test(t, slicesx.Map, "Map integers to strings", + []int{1, 2, 3, 4, 5}, + []string{"1", "2", "3", "4", "5"}, + strconv.Itoa, + ) + test(t, slicesx.Map, "Map strings to integers", + []string{"1", "2", "3", "4", "5"}, + []int{1, 2, 3, 4, 5}, + func(s string) int { + n, _ := strconv.Atoi(s) + return n + }, + ) +} + +func Test_FlatMap(t *testing.T) { + test(t, slicesx.FlatMap, "Map integers to strings and flatten", + []int{1, 2, 3, 4, 5}, + []string{"1", "1", "2", "2", "3", "3", "4", "4", "5", "5"}, + func(i int) []string { + x := strconv.Itoa(i) + return []string{x, x} + }, + ) + test(t, slicesx.FlatMap, "Map strings to integers and flatten", + []string{"1", "2", "3", "4", "5"}, + []int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5}, + func(s string) []int { + n, _ := strconv.Atoi(s) + return []int{n, n} + }, + ) +} diff --git a/api/slicesx/slices_test.go b/api/slicesx/slices_test.go deleted file mode 100644 index d75f9b559..000000000 --- a/api/slicesx/slices_test.go +++ /dev/null @@ -1,127 +0,0 @@ -package slicesx - -import ( - "strconv" - "testing" - - "github.com/stretchr/testify/assert" -) - -type filterTestCase[T any] struct { - name string - input []T - expected []T - predicate func(T) bool -} - -func TestFilter(t *testing.T) { - intTestCases := []filterTestCase[int]{ - { - name: "Filter even numbers", - input: []int{1, 2, 3, 4, 5, 6, 7, 8, 9}, - expected: []int{2, 4, 6, 8}, - - predicate: func(n int) bool { - return n%2 == 0 - }, - }, - { - name: "Filter odd numbers", - input: []int{1, 2, 3, 4, 5, 6, 7, 8, 9}, - expected: []int{1, 3, 5, 7, 9}, - - predicate: func(n int) bool { - return n%2 != 0 - }, - }, - } - - runTestCases(t, intTestCases) - - stringTestCases := []filterTestCase[string]{ - { - name: "Filter strings starting with 'A'", - input: []string{"Apple", "Banana", "Avocado", "Grapes", "Apricot"}, - expected: []string{"Apple", "Avocado", "Apricot"}, - predicate: func(s string) bool { - return s[0] == 'A' - }, - }, - { - name: "Filter strings longer than 5 characters", - input: []string{"Apple", "Banana", "Avocado", "Grapes", "Apricot"}, - expected: []string{"Banana", "Avocado", "Grapes", "Apricot"}, - predicate: func(s string) bool { - return len(s) > 5 - }, - }, - } - - runTestCases(t, stringTestCases) -} - -func runTestCases[T any](t *testing.T, testCases []filterTestCase[T]) { - for _, testCase := range testCases { - t.Run(testCase.name, func(t *testing.T) { - is := assert.New(t) - result := Filter(testCase.input, testCase.predicate) - - is.Equal(len(testCase.expected), len(result)) - is.ElementsMatch(testCase.expected, result) - }) - } -} - -func TestMap(t *testing.T) { - intTestCases := []struct { - name string - input []int - expected []string - mapper func(int) string - }{ - { - name: "Map integers to strings", - input: []int{1, 2, 3, 4, 5}, - expected: []string{"1", "2", "3", "4", "5"}, - mapper: strconv.Itoa, - }, - } - - runMapTestCases(t, intTestCases) - - stringTestCases := []struct { - name string - input []string - expected []int - mapper func(string) int - }{ - { - name: "Map strings to integers", - input: []string{"1", "2", "3", "4", "5"}, - expected: []int{1, 2, 3, 4, 5}, - mapper: func(s string) int { - n, _ := strconv.Atoi(s) - return n - }, - }, - } - - runMapTestCases(t, stringTestCases) -} - -func runMapTestCases[T, U any](t *testing.T, testCases []struct { - name string - input []T - expected []U - mapper func(T) U -}) { - for _, testCase := range testCases { - t.Run(testCase.name, func(t *testing.T) { - is := assert.New(t) - result := Map(testCase.input, testCase.mapper) - - is.Equal(len(testCase.expected), len(result)) - is.ElementsMatch(testCase.expected, result) - }) - } -} diff --git a/api/slicesx/slicesx_test.go b/api/slicesx/slicesx_test.go new file mode 100644 index 000000000..1bb8a76fe --- /dev/null +++ b/api/slicesx/slicesx_test.go @@ -0,0 +1,29 @@ +package slicesx_test + +import ( + "reflect" + "testing" + + "github.com/stretchr/testify/assert" +) + +type libFunc[T, U, V any] func([]T, func(T) U) V +type predicateFunc[T, U any] func(T) U + +func test[T, U, V any](t *testing.T, libFn libFunc[T, U, V], name string, input []T, expected V, predicate predicateFunc[T, U]) { + t.Helper() + + t.Run(name, func(t *testing.T) { + is := assert.New(t) + + result := libFn(input, predicate) + + switch reflect.TypeOf(result).Kind() { + case reflect.Slice, reflect.Array: + is.Equal(expected, result) + is.ElementsMatch(expected, result) + default: + is.Equal(expected, result) + } + }) +} diff --git a/api/slicesx/slices.go b/api/slicesx/unique.go similarity index 51% rename from api/slicesx/slices.go rename to api/slicesx/unique.go index b7e0aa0ef..8659b0778 100644 --- a/api/slicesx/slices.go +++ b/api/slicesx/unique.go @@ -1,27 +1,5 @@ package slicesx -// Map applies the given function to each element of the slice and returns a new slice with the results -func Map[T, U any](s []T, f func(T) U) []U { - result := make([]U, len(s)) - for i, v := range s { - result[i] = f(v) - } - return result -} - -// Filter returns a new slice containing only the elements of the slice for which the given predicate returns true -func Filter[T any](s []T, predicate func(T) bool) []T { - n := 0 - for _, v := range s { - if predicate(v) { - s[n] = v - n++ - } - } - - return s[:n] -} - func Unique[T comparable](items []T) []T { return UniqueBy(items, func(item T) T { return item diff --git a/api/slicesx/unique_test.go b/api/slicesx/unique_test.go new file mode 100644 index 000000000..8ff967ca6 --- /dev/null +++ b/api/slicesx/unique_test.go @@ -0,0 +1,46 @@ +package slicesx_test + +import ( + "testing" + + "github.com/portainer/portainer/api/slicesx" + "github.com/stretchr/testify/assert" +) + +func Test_Unique(t *testing.T) { + is := assert.New(t) + t.Run("Should extract unique numbers", func(t *testing.T) { + + source := []int{1, 1, 2, 3, 4, 4, 5, 4, 6, 7, 8, 9, 1} + result := slicesx.Unique(source) + expected := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} + + is.ElementsMatch(result, expected) + }) + + t.Run("Should return empty array", func(t *testing.T) { + source := []int{} + result := slicesx.Unique(source) + expected := []int{} + is.ElementsMatch(result, expected) + }) +} + +func Test_UniqueBy(t *testing.T) { + is := assert.New(t) + t.Run("Should extract unique numbers by property", func(t *testing.T) { + + source := []struct{ int }{{1}, {1}, {2}, {3}, {4}, {4}, {5}, {4}, {6}, {7}, {8}, {9}, {1}} + result := slicesx.UniqueBy(source, func(item struct{ int }) int { return item.int }) + expected := []struct{ int }{{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}} + + is.ElementsMatch(result, expected) + }) + + t.Run("Should return empty array", func(t *testing.T) { + source := []int{} + result := slicesx.UniqueBy(source, func(x int) int { return x }) + expected := []int{} + is.ElementsMatch(result, expected) + }) +} diff --git a/app/assets/css/button.css b/app/assets/css/button.css index 0aece2a15..547d9fdc0 100644 --- a/app/assets/css/button.css +++ b/app/assets/css/button.css @@ -29,43 +29,79 @@ fieldset[disabled] .btn { } .btn.btn-primary { - @apply border-blue-8 bg-blue-8 text-white; - @apply hover:border-blue-9 hover:bg-blue-9 hover:text-white; - @apply th-dark:hover:border-blue-7 th-dark:hover:bg-blue-7; + @apply border-graphite-700 bg-graphite-700 text-mist-100; + @apply hover:border-graphite-700/90 hover:bg-graphite-700/90 hover:text-mist-100; + @apply focus:border-blue-5 focus:shadow-graphite-700/80 focus:text-mist-100; + + @apply th-dark:border-mist-100 th-dark:bg-mist-100 th-dark:text-graphite-700; + @apply th-dark:hover:border-mist-100/90 th-dark:hover:bg-mist-100/90 th-dark:hover:text-graphite-700; + @apply th-dark:focus:border-blue-5 th-dark:focus:shadow-white/80 th-dark:focus:text-graphite-700; + + @apply th-highcontrast:border-mist-100 th-highcontrast:bg-mist-100 th-highcontrast:text-graphite-700; + @apply th-highcontrast:hover:border-mist-100/90 th-highcontrast:hover:bg-mist-100/90 th-highcontrast:hover:text-graphite-700; + @apply th-highcontrast:focus:border-blue-5 th-highcontrast:focus:shadow-white/80 th-highcontrast:focus:text-graphite-700; +} + +/* Sidebar background is always dark, so we need to override the primary button styles */ +.btn.btn-primary.sidebar { + @apply border-mist-100 bg-mist-100 text-graphite-700; + @apply hover:border-mist-100/90 hover:bg-mist-100/90 hover:text-graphite-700; + @apply focus:border-blue-5 focus:shadow-white/80 focus:text-graphite-700; } .btn.btn-primary:active, .btn.btn-primary.active, .open > .dropdown-toggle.btn-primary { - @apply border-blue-5 bg-blue-9; + @apply border-graphite-700/80 bg-graphite-700 text-mist-100; + @apply th-dark:border-white/80 th-dark:bg-mist-100 th-dark:text-graphite-700; + @apply th-highcontrast:border-white/80 th-highcontrast:bg-mist-100 th-highcontrast:text-graphite-700; } .nav-pills > li.active > a, .nav-pills > li.active > a:hover, .nav-pills > li.active > a:focus { - @apply bg-blue-8; + @apply bg-graphite-700 text-mist-100; + @apply th-dark:bg-mist-100 th-dark:text-graphite-700; + @apply th-highcontrast:bg-mist-100 th-highcontrast:text-graphite-700; } /* Button Secondary */ .btn.btn-secondary { @apply border border-solid; - @apply border-blue-8 bg-blue-2 text-blue-9; - @apply hover:bg-blue-3; + @apply border-graphite-700 bg-mist-100 text-graphite-700; + @apply hover:border-graphite-700 hover:bg-graphite-700/10 hover:text-graphite-700; + @apply focus:border-blue-5 focus:shadow-graphite-700/20 focus:text-graphite-700; - @apply th-dark:border-blue-7 th-dark:bg-gray-10 th-dark:text-blue-3; - @apply th-dark:hover:bg-blue-11; + @apply th-dark:border-mist-100 th-dark:bg-graphite-700 th-dark:text-mist-100; + @apply th-dark:hover:border-mist-100 th-dark:hover:bg-mist-100/20 th-dark:hover:text-mist-100; + @apply th-dark:focus:border-blue-5 th-dark:focus:shadow-white/80 th-dark:focus:text-mist-100; + + @apply th-highcontrast:border-mist-100 th-highcontrast:bg-graphite-700 th-highcontrast:text-mist-100; + @apply th-highcontrast:hover:border-mist-100 th-highcontrast:hover:bg-mist-100/20 th-highcontrast:hover:text-mist-100; + @apply th-highcontrast:focus:border-blue-5 th-highcontrast:focus:shadow-white/80 th-highcontrast:focus:text-mist-100; +} + +.btn.btn-secondary:active, +.btn.btn-secondary.active, +.open > .dropdown-toggle.btn-secondary { + @apply border-graphite-700 bg-graphite-700/10 text-graphite-700; + @apply th-dark:border-mist-100 th-dark:bg-mist-100/20 th-dark:text-mist-100; + @apply th-highcontrast:border-mist-100 th-highcontrast:bg-mist-100/20 th-highcontrast:text-mist-100; } .btn.btn-danger { @apply border-error-8 bg-error-8; @apply hover:border-error-7 hover:bg-error-7 hover:text-white; + @apply focus:border-blue-5 focus:shadow-error-8/20 focus:text-white; + @apply th-dark:focus:border-blue-5 th-dark:focus:shadow-white/80 th-dark:focus:text-white; + @apply th-highcontrast:focus:border-blue-5 th-highcontrast:focus:shadow-white/80 th-highcontrast:focus:text-white; } .btn.btn-danger:active, .btn.btn-danger.active, .open > .dropdown-toggle.btn-danger { - @apply border-blue-5 bg-error-8 text-white; + @apply border-error-5 bg-error-8 text-white; } .btn.btn-dangerlight { @@ -74,6 +110,10 @@ fieldset[disabled] .btn { @apply hover:bg-error-2 th-dark:hover:bg-error-11; @apply border-error-5 th-highcontrast:border-error-7 th-dark:border-error-7; @apply border border-solid; + + @apply focus:border-blue-5 focus:shadow-error-8/20 focus:text-error-9; + @apply th-dark:focus:border-blue-5 th-dark:focus:shadow-white/80 th-dark:focus:text-white; + @apply th-highcontrast:focus:border-blue-5 th-highcontrast:focus:shadow-white/80; } .btn.btn-icon.btn-dangerlight { @apply hover:text-error-11 th-dark:hover:text-error-7; @@ -90,15 +130,18 @@ fieldset[disabled] .btn { /* secondary-grey */ .btn.btn-default, .btn.btn-light { - @apply border-gray-5 bg-white text-gray-7; - @apply hover:border-gray-5 hover:bg-gray-3 hover:text-gray-9; + @apply border-gray-5 bg-white text-gray-8; + @apply hover:border-gray-5 hover:bg-gray-3 hover:text-gray-10; + @apply focus:border-blue-5 focus:shadow-graphite-700/20 focus:text-gray-8; /* dark mode */ @apply th-dark:border-gray-warm-7 th-dark:bg-gray-iron-10 th-dark:text-gray-warm-4; @apply th-dark:hover:border-gray-6 th-dark:hover:bg-gray-iron-9 th-dark:hover:text-gray-warm-4; + @apply th-dark:focus:border-blue-5 th-dark:focus:shadow-white/80 th-dark:focus:text-gray-warm-4; @apply th-highcontrast:border-gray-2 th-highcontrast:bg-black th-highcontrast:text-white; @apply th-highcontrast:hover:border-gray-6 th-highcontrast:hover:bg-gray-9 th-highcontrast:hover:text-gray-warm-4; + @apply th-highcontrast:focus:border-blue-5 th-highcontrast:focus:shadow-white/80 th-highcontrast:focus:text-white; } .btn.btn-light:active, @@ -119,42 +162,17 @@ fieldset[disabled] .btn { .input-group-btn .btn.active, .btn-group .btn.active { - @apply border-blue-5 bg-blue-2 text-blue-10; - @apply th-dark:border-blue-9 th-dark:bg-blue-11 th-dark:text-blue-2; -} - -/* focus */ - -.btn.btn-primary:focus, -.btn.btn-secondary:focus, -.btn.btn-light:focus { - @apply border-blue-5; -} - -.btn.btn-danger:focus, -.btn.btn-dangerlight:focus { - @apply border-blue-6; -} - -.btn.btn-primary:focus, -.btn.btn-secondary:focus, -.btn.btn-light:focus, -.btn.btn-danger:focus, -.btn.btn-dangerlight:focus { - --btn-focus-color: var(--ui-blue-3); - box-shadow: 0px 0px 0px 4px var(--btn-focus-color); + @apply border-graphite-700/80 bg-graphite-700 text-mist-100; + @apply th-dark:border-white/80 th-dark:bg-mist-100 th-dark:text-graphite-700; + @apply th-highcontrast:border-white/80 th-highcontrast:bg-mist-100 th-highcontrast:text-graphite-700; } .btn.btn-icon:focus { box-shadow: none !important; } -[theme='dark'] .btn.btn-primary:focus, -[theme='dark'] .btn.btn-secondary:focus, -[theme='dark'] .btn.btn-light:focus, -[theme='dark'] .btn.btn-danger:focus, -[theme='dark'] .btn.btn-dangerlight:focus { - --btn-focus-color: var(--ui-blue-11); +.btn:focus { + box-shadow: 0px 0px 0px 2px var(--tw-shadow-color); } a.no-link, diff --git a/app/assets/css/colors.json b/app/assets/css/colors.json index 55f2922e5..94d3c2015 100644 --- a/app/assets/css/colors.json +++ b/app/assets/css/colors.json @@ -1,6 +1,31 @@ { "black": "#000000", "white": "#ffffff", + "graphite": { + "10": "#f5f5f6", + "50": "#e5e6e8", + "100": "#ced0d3", + "200": "#abafb5", + "300": "#7b8089", + "400": "#5c6066", + "500": "#484a4e", + "600": "#3a3b3f", + "700": "#2e2f33", + "800": "#222326", + "900": "#161719" + }, + "mist": { + "50": "#fcfbfa", + "100": "#f7f6f3", + "200": "#f0f0ec", + "300": "#e8e7e2", + "400": "#e2e1db", + "500": "#d9d8d2", + "600": "#ceccc4", + "700": "#bebcb4", + "800": "#a7a6a0", + "900": "#8b8983" + }, "gray": { "1": "#fcfcfd", "2": "#f9fafb", diff --git a/app/assets/css/react-datetime-picker-override.css b/app/assets/css/react-datetime-picker-override.css index acd26fb58..dbbea4766 100644 --- a/app/assets/css/react-datetime-picker-override.css +++ b/app/assets/css/react-datetime-picker-override.css @@ -12,35 +12,40 @@ /* Extending Calendar.css from react-daterange-picker__calendar */ -.react-daterange-picker__calendar .react-calendar { +.react-calendar { background: var(--bg-calendar-color); color: var(--text-main-color); + @apply th-dark:bg-gray-iron-10; } /* calendar nav buttons */ -.react-daterange-picker__calendar .react-calendar__navigation button:disabled { +.react-calendar__navigation button:disabled { background: var(--bg-calendar-color); @apply opacity-60; @apply brightness-95 th-dark:brightness-110; + @apply th-dark:bg-gray-iron-7; } -.react-daterange-picker__calendar .react-calendar__navigation button:enabled:hover, -.react-daterange-picker__calendar .react-calendar__navigation button:enabled:focus { +.react-calendar__navigation button:enabled:hover, +.react-calendar__navigation button:enabled:focus { background: var(--bg-daterangepicker-color); + @apply th-dark:bg-gray-iron-7; } /* date tile */ -.react-daterange-picker__calendar .react-calendar__tile:disabled { - background: var(--bg-calendar-color); +.react-calendar__tile:disabled { @apply opacity-60; @apply brightness-95 th-dark:brightness-110; + @apply th-dark:bg-gray-iron-7; } -.react-daterange-picker__calendar .react-calendar__tile:enabled:hover, -.react-daterange-picker__calendar .react-calendar__tile:enabled:focus { + +.react-calendar__tile:enabled:hover, +.react-calendar__tile:enabled:focus { background: var(--bg-daterangepicker-hover); + @apply th-dark:bg-gray-iron-7; } /* today's date tile */ -.react-daterange-picker__calendar .react-calendar__tile--now { +.react-calendar__tile--now { @apply th-highcontrast:text-[color:var(--bg-calendar-color)] th-dark:text-[color:var(--bg-calendar-color)]; border-radius: 0.25rem !important; } @@ -48,23 +53,27 @@ .react-daterange-picker__calendar .react-calendar__tile--now:enabled:focus { background: var(--bg-daterangepicker-hover); color: var(--text-daterangepicker-hover); + @apply th-dark:bg-gray-iron-7; } /* probably date tile in range */ -.react-daterange-picker__calendar .react-calendar__tile--hasActive { +.react-calendar__tile--hasActive { background: var(--bg-daterangepicker-end-date); color: var(--text-daterangepicker-end-date); + @apply th-dark:bg-gray-iron-7; } -.react-daterange-picker__calendar .react-calendar__tile--hasActive:enabled:hover, -.react-daterange-picker__calendar .react-calendar__tile--hasActive:enabled:focus { +.react-calendar__tile--hasActive:enabled:hover, +.react-calendar__tile--hasActive:enabled:focus { background: var(--bg-daterangepicker-hover); color: var(--text-daterangepicker-hover); + @apply th-dark:bg-gray-iron-7; } -.react-daterange-picker__calendar .react-calendar__tile--active:enabled:hover, -.react-daterange-picker__calendar .react-calendar__tile--active:enabled:focus { +.react-calendar__tile--active:enabled:hover, +.react-calendar__tile--active:enabled:focus { background: var(--bg-daterangepicker-hover); color: var(--text-daterangepicker-hover); + @apply th-dark:bg-gray-iron-7; } .react-daterange-picker__calendar @@ -75,9 +84,10 @@ } /* on range select hover */ -.react-daterange-picker__calendar .react-calendar--selectRange .react-calendar__tile--hover { +.react-calendar--selectRange .react-calendar__tile--hover { background: var(--bg-daterangepicker-in-range); color: var(--text-daterangepicker-in-range); + @apply th-dark:bg-gray-iron-7; } /* @@ -111,4 +121,5 @@ .react-calendar__tile--active.react-calendar__month-view__days__day--weekend { color: var(--text-daterangepicker-active); + @apply th-dark:bg-gray-iron-7; } diff --git a/app/assets/css/theme.css b/app/assets/css/theme.css index eb2d36882..318e0d9e4 100644 --- a/app/assets/css/theme.css +++ b/app/assets/css/theme.css @@ -3,6 +3,16 @@ --black-color: var(--ui-black); --white-color: var(--ui-white); + --graphite-600: #3a3b3f; + --graphite-700: #2e2f33; + --graphite-800: #222326; + --graphite-900: #161719; + + --mist-50: #fcfbfa; + --mist-100: #f7f6f3; + --mist-200: #f0f0ec; + --mist-300: #e8e7e2; + --grey-1: #212121; --grey-2: #181818; --grey-3: #383838; @@ -58,6 +68,8 @@ --grey-58: #ebf4f8; --grey-59: #e6e6e6; --grey-61: rgb(231, 231, 231); + --grey-62: #fdfdfd; + --grey-63: #121212; --blue-1: #219; --blue-2: #337ab7; @@ -99,17 +111,16 @@ /* Default Theme */ --bg-card-color: var(--white-color); --bg-main-color: var(--white-color); - --bg-body-color: var(--grey-9); + --bg-body-color: var(--grey-62); --bg-checkbox-border-color: var(--grey-49); - --bg-sidebar-color: var(--ui-blue-10); - --bg-sidebar-nav-color: var(--ui-blue-11); + --bg-sidebar-color: var(--graphite-700); + --bg-sidebar-nav-color: var(--graphite-600); --bg-widget-color: var(--white-color); --bg-widget-header-color: var(--grey-10); --bg-widget-table-color: var(--ui-gray-3); --bg-header-color: var(--white-color); --bg-hover-table-color: var(--grey-14); --bg-input-group-addon-color: var(--ui-gray-3); - --bg-btn-default-color: var(--ui-blue-10); --bg-blocklist-hover-color: var(--ui-blue-2); --bg-table-color: var(--white-color); --bg-md-checkbox-color: var(--grey-12); @@ -128,7 +139,8 @@ --border-pagination-color: var(--ui-white); --bg-pagination-span-color: var(--white-color); --bg-pagination-hover-color: var(--ui-blue-3); - --bg-motd-body-color: var(--grey-20); + --bg-motd-body-color: var(--mist-50); + --bg-motd-btn-color: var(--graphite-700); --bg-item-highlighted-color: var(--grey-21); --bg-item-highlighted-null-color: var(--grey-14); --bg-panel-body-color: var(--white-color); @@ -144,8 +156,6 @@ --bg-daterangepicker-in-range: var(--grey-58); --bg-daterangepicker-active: var(--blue-14); --bg-input-autofill-color: var(--bg-inputbox); - --bg-btn-default-hover-color: var(--ui-blue-9); - --bg-btn-focus: var(--grey-59); --bg-small-select-color: var(--white-color); --bg-stepper-item-active: var(--white-color); --bg-stepper-item-counter: var(--grey-61); @@ -177,7 +187,6 @@ --text-navtabs-color: var(--grey-7); --text-navtabs-hover-color: var(--grey-6); --text-nav-tab-active-color: var(--grey-25); - --text-dropdown-menu-color: var(--grey-6); --text-log-viewer-color: var(--black-color); --text-json-tree-color: var(--blue-3); @@ -189,6 +198,8 @@ --text-pagination-color: var(--grey-26); --text-pagination-span-color: var(--grey-3); --text-pagination-span-hover-color: var(--grey-3); + --text-motd-body-color: var(--black-color); + --text-motd-btn-color: var(--mist-100); --text-summary-color: var(--black-color); --text-tooltip-color: var(--white-color); --text-rzslider-color: var(--grey-36); @@ -203,6 +214,7 @@ --text-button-group-color: var(--ui-gray-9); --text-button-dangerlight-color: var(--ui-error-5); --text-stepper-active-color: var(--ui-blue-8); + --border-color: var(--grey-42); --border-widget-color: var(--grey-43); --border-sidebar-color: var(--ui-blue-9); @@ -218,7 +230,8 @@ --border-pre-color: var(--grey-43); --border-pagination-span-color: var(--ui-white); --border-pagination-hover-color: var(--ui-white); - --border-panel-color: var(--white-color); + --border-motd-body-color: var(--mist-300); + --border-panel-color: var(--mist-300); --border-input-sm-color: var(--grey-47); --border-daterangepicker-color: var(--grey-19); --border-calendar-table: var(--white-color); @@ -265,8 +278,7 @@ --text-log-viewer-color-json-red: var(--text-log-viewer-color); --text-log-viewer-color-json-blue: var(--text-log-viewer-color); - --bg-body-color: var(--grey-2); - --bg-btn-default-color: var(--grey-3); + --bg-body-color: var(--grey-63); --bg-blocklist-hover-color: var(--ui-gray-iron-10); --bg-blocklist-item-selected-color: var(--ui-gray-iron-10); --bg-card-color: var(--grey-1); @@ -274,8 +286,6 @@ --bg-code-color: var(--grey-2); --bg-dropdown-menu-color: var(--ui-gray-warm-8); --bg-main-color: var(--grey-2); - --bg-sidebar-color: var(--grey-1); - --bg-sidebar-nav-color: var(--grey-2); --bg-widget-color: var(--grey-1); --bg-widget-header-color: var(--grey-3); --bg-widget-table-color: var(--grey-3); @@ -296,7 +306,8 @@ --bg-pagination-color: var(--grey-3); --bg-pagination-span-color: var(--grey-1); --bg-pagination-hover-color: var(--grey-3); - --bg-motd-body-color: var(--grey-1); + --bg-motd-body-color: var(--graphite-800); + --bg-motd-btn-color: var(--mist-100); --bg-item-highlighted-color: var(--grey-2); --bg-item-highlighted-null-color: var(--grey-2); --bg-panel-body-color: var(--grey-1); @@ -316,8 +327,6 @@ --bg-daterangepicker-in-range: var(--ui-gray-warm-11); --bg-daterangepicker-active: var(--blue-14); --bg-input-autofill-color: var(--bg-inputbox); - --bg-btn-default-hover-color: var(--grey-4); - --bg-btn-focus: var(--grey-3); --bg-small-select-color: var(--grey-2); --bg-stepper-item-active: var(--grey-1); --bg-stepper-item-counter: var(--grey-7); @@ -348,7 +357,6 @@ --text-navtabs-color: var(--grey-8); --text-navtabs-hover-color: var(--grey-9); --text-nav-tab-active-color: var(--white-color); - --text-dropdown-menu-color: var(--white-color); --text-log-viewer-color: var(--white-color); --text-json-tree-color: var(--grey-40); @@ -360,6 +368,8 @@ --text-pagination-color: var(--white-color); --text-pagination-span-color: var(--ui-white); --text-pagination-span-hover-color: var(--ui-white); + --text-motd-body-color: var(--mist-100); + --text-motd-btn-color: var(--graphite-700); --text-summary-color: var(--white-color); --text-tooltip-color: var(--white-color); --text-rzslider-color: var(--white-color); @@ -374,6 +384,7 @@ --text-button-group-color: var(--ui-white); --text-button-dangerlight-color: var(--ui-error-7); --text-stepper-active-color: var(--ui-white); + --border-color: var(--grey-3); --border-widget-color: var(--grey-1); --border-sidebar-color: var(--ui-gray-8); @@ -391,6 +402,7 @@ --border-blocklist-item-selected-color: var(--grey-31); --border-pagination-span-color: var(--grey-1); --border-pagination-hover-color: var(--grey-3); + --border-motd-body-color: var(--graphite-800); --border-panel-color: var(--grey-2); --border-input-sm-color: var(--grey-3); --border-daterangepicker-color: var(--grey-3); @@ -450,6 +462,7 @@ --bg-panel-body-color: var(--black-color); --bg-dropdown-menu-color: var(--ui-gray-warm-8); --bg-motd-body-color: var(--black-color); + --bg-motd-btn-color: var(--white-color); --bg-blocklist-hover-color: var(--black-color); --bg-blocklist-item-selected-color: var(--black-color); --bg-input-group-addon-color: var(--grey-3); @@ -481,11 +494,8 @@ --bg-navtabs-hover-color: var(--grey-3); --bg-nav-tab-active-color: var(--ui-black); - --bg-btn-default-color: var(--black-color); --bg-input-autofill-color: var(--bg-inputbox); --bg-code-color: var(--ui-black); - --bg-btn-default-hover-color: var(--grey-4); - --bg-btn-focus: var(--black-color); --bg-small-select-color: var(--black-color); --bg-stepper-item-active: var(--black-color); --bg-stepper-item-counter: var(--grey-3); @@ -523,6 +533,8 @@ --text-daterangepicker-end-date: var(--ui-white); --text-daterangepicker-in-range: var(--white-color); --text-daterangepicker-active: var(--white-color); + --text-motd-body-color: var(--white-color); + --text-motd-btn-color: var(--black-color); --text-json-tree-color: var(--white-color); --text-json-tree-leaf-color: var(--white-color); --text-json-tree-branch-preview-color: var(--white-color); @@ -553,6 +565,7 @@ --border-input-sm-color: var(--white-color); --border-pagination-color: var(--grey-1); --border-pagination-span-color: var(--grey-1); + --border-motd-body-color: var(--white-color); --border-daterangepicker-color: var(--white-color); --border-calendar-table: var(--black-color); --border-daterangepicker: var(--black-color); diff --git a/app/assets/css/vendor-override.css b/app/assets/css/vendor-override.css index 74fa94d4e..12e0fc947 100644 --- a/app/assets/css/vendor-override.css +++ b/app/assets/css/vendor-override.css @@ -201,8 +201,18 @@ pre { background-color: var(--bg-progress-color); } -.motd-body { - background-color: var(--bg-motd-body-color) !important; +.widget-body.motd-body { + border: 1px solid var(--border-motd-body-color); + color: var(--text-motd-body-color); + background: var(--bg-motd-body-color) url(../images/purple-gradient.svg) top right / 40% no-repeat; +} + +.widget-body.motd-body .btn.btn-link, +.widget-body.motd-body .btn.btn-link:hover { + padding: 0 5px 0 4px; + border-radius: 4px; + background-color: var(--bg-motd-btn-color); + color: var(--text-motd-btn-color); } .panel-body { @@ -408,14 +418,10 @@ input:-webkit-autofill { } .sidebar.tippy-box[data-placement^='right'] > .tippy-arrow:before { - border-right: 8px solid var(--ui-blue-9); + border-right: 8px solid var(--graphite-600); border-width: 6px 8px 6px 0; } -[theme='dark'] .sidebar.tippy-box[data-placement^='right'] > .tippy-arrow:before { - border-right: 8px solid var(--ui-gray-true-9); -} - [theme='highcontrast'] .sidebar.tippy-box[data-placement^='right'] > .tippy-arrow:before { border-right: 8px solid var(--ui-white); } diff --git a/app/assets/ico/android-chrome-192x192.png b/app/assets/ico/android-chrome-192x192.png index 8f31e405a..236db0e2b 100644 Binary files a/app/assets/ico/android-chrome-192x192.png and b/app/assets/ico/android-chrome-192x192.png differ diff --git a/app/assets/ico/android-chrome-256x256.png b/app/assets/ico/android-chrome-256x256.png index cc95d0044..52848e019 100644 Binary files a/app/assets/ico/android-chrome-256x256.png and b/app/assets/ico/android-chrome-256x256.png differ diff --git a/app/assets/ico/apple-touch-icon.png b/app/assets/ico/apple-touch-icon.png index aeea31ce8..f05e9c161 100644 Binary files a/app/assets/ico/apple-touch-icon.png and b/app/assets/ico/apple-touch-icon.png differ diff --git a/app/assets/ico/favicon-16x16.png b/app/assets/ico/favicon-16x16.png index f7a26b564..8c60e5d9f 100644 Binary files a/app/assets/ico/favicon-16x16.png and b/app/assets/ico/favicon-16x16.png differ diff --git a/app/assets/ico/favicon-32x32.png b/app/assets/ico/favicon-32x32.png index d1ccc9cea..8735718a2 100644 Binary files a/app/assets/ico/favicon-32x32.png and b/app/assets/ico/favicon-32x32.png differ diff --git a/app/assets/ico/favicon.ico b/app/assets/ico/favicon.ico index 28ed661f9..066969400 100644 Binary files a/app/assets/ico/favicon.ico and b/app/assets/ico/favicon.ico differ diff --git a/app/assets/ico/logomark.svg b/app/assets/ico/logomark.svg index b7679d482..140c1b494 100644 --- a/app/assets/ico/logomark.svg +++ b/app/assets/ico/logomark.svg @@ -1,35 +1,12 @@ - - - - - - - - - - - - - - - - - + + + + + - - - - - - - - - - - - - - + + + diff --git a/app/assets/ico/mstile-150x150.png b/app/assets/ico/mstile-150x150.png index 5e7eb6873..f48374538 100644 Binary files a/app/assets/ico/mstile-150x150.png and b/app/assets/ico/mstile-150x150.png differ diff --git a/app/assets/ico/safari-pinned-tab.svg b/app/assets/ico/safari-pinned-tab.svg index 79ce7b6fa..d0509a572 100644 --- a/app/assets/ico/safari-pinned-tab.svg +++ b/app/assets/ico/safari-pinned-tab.svg @@ -1 +1,6 @@ - \ No newline at end of file + + + + + + \ No newline at end of file diff --git a/app/assets/images/logo.png b/app/assets/images/logo.png deleted file mode 100644 index 2e46594f2..000000000 Binary files a/app/assets/images/logo.png and /dev/null differ diff --git a/app/assets/images/logo_alt.png b/app/assets/images/logo_alt.png deleted file mode 100644 index a6c6707ca..000000000 Binary files a/app/assets/images/logo_alt.png and /dev/null differ diff --git a/app/assets/images/logo_alt.svg b/app/assets/images/logo_alt.svg index 90e164ca1..8d254e4e5 100644 --- a/app/assets/images/logo_alt.svg +++ b/app/assets/images/logo_alt.svg @@ -1,60 +1,14 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + diff --git a/app/assets/images/logo_alt_black.svg b/app/assets/images/logo_alt_black.svg new file mode 100644 index 000000000..d9243b464 --- /dev/null +++ b/app/assets/images/logo_alt_black.svg @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/app/assets/images/logo_ico.png b/app/assets/images/logo_ico.png deleted file mode 100644 index b4bfd2924..000000000 Binary files a/app/assets/images/logo_ico.png and /dev/null differ diff --git a/app/assets/images/logo_small.png b/app/assets/images/logo_small.png deleted file mode 100644 index 76d3a46b0..000000000 Binary files a/app/assets/images/logo_small.png and /dev/null differ diff --git a/app/assets/images/logo_small_alt.png b/app/assets/images/logo_small_alt.png deleted file mode 100644 index a5bc64771..000000000 Binary files a/app/assets/images/logo_small_alt.png and /dev/null differ diff --git a/app/assets/images/purple-gradient.svg b/app/assets/images/purple-gradient.svg new file mode 100644 index 000000000..0b3bc7160 --- /dev/null +++ b/app/assets/images/purple-gradient.svg @@ -0,0 +1,522 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/docker/views/images/edit/image.html b/app/docker/views/images/edit/image.html index 7bee83a2b..af37cd0e2 100644 --- a/app/docker/views/images/edit/image.html +++ b/app/docker/views/images/edit/image.html @@ -16,19 +16,19 @@ - + - + - + diff --git a/app/index.html b/app/index.html index 370070b48..52b9b5d10 100644 --- a/app/index.html +++ b/app/index.html @@ -20,7 +20,7 @@ - + @@ -47,7 +47,10 @@
- +
+ + +
diff --git a/app/kubernetes/react/components/index.ts b/app/kubernetes/react/components/index.ts index 27aa04444..cfb103823 100644 --- a/app/kubernetes/react/components/index.ts +++ b/app/kubernetes/react/components/index.ts @@ -92,6 +92,7 @@ export const ngModule = angular 'onChange', 'placeholder', 'value', + 'allowSelectAll', ]) ) .component( diff --git a/app/kubernetes/registries/kube-registry-access-view/kube-registry-access-view.html b/app/kubernetes/registries/kube-registry-access-view/kube-registry-access-view.html index 11184ae0f..5c5e68255 100644 --- a/app/kubernetes/registries/kube-registry-access-view/kube-registry-access-view.html +++ b/app/kubernetes/registries/kube-registry-access-view/kube-registry-access-view.html @@ -19,6 +19,7 @@ namespaces="$ctrl.resourcePools" placeholder="'Select one or more namespaces'" on-change="($ctrl.onChangeResourcePools)" + allow-select-all="true" >
diff --git a/app/kubernetes/views/deploy/deploy.html b/app/kubernetes/views/deploy/deploy.html index d57d0caa7..60e7b0144 100644 --- a/app/kubernetes/views/deploy/deploy.html +++ b/app/kubernetes/views/deploy/deploy.html @@ -40,17 +40,15 @@
- +
- + is-disabled="ctrl.formValues.namespace_toggle && ctrl.state.BuildMethod !== ctrl.BuildMethods.HELM || ctrl.state.isNamespaceInputDisabled" + value="ctrl.formValues.Namespace" + on-change="(ctrl.onChangeNamespace)" + options="ctrl.namespaceOptions" + > Namespaces specified in the manifest will be used @@ -186,7 +184,6 @@
-
Selected Helm chart
diff --git a/app/kubernetes/views/deploy/deployController.js b/app/kubernetes/views/deploy/deployController.js index 89f416ac3..b44d3d7bb 100644 --- a/app/kubernetes/views/deploy/deployController.js +++ b/app/kubernetes/views/deploy/deployController.js @@ -101,9 +101,10 @@ class KubernetesDeployController { this.onChangeNamespace = this.onChangeNamespace.bind(this); } - onChangeNamespace() { + onChangeNamespace(namespaceName) { return this.$async(async () => { - const applications = await this.KubernetesApplicationService.get(this.formValues.Namespace); + this.formValues.Namespace = namespaceName; + const applications = await this.KubernetesApplicationService.get(namespaceName); const stacks = _.map(applications, (item) => item.StackName).filter((item) => item !== ''); this.stacks = _.uniq(stacks); }); @@ -371,6 +372,10 @@ class KubernetesDeployController { if (this.namespaces.length > 0) { this.formValues.Namespace = this.namespaces[0].Name; } + this.namespaceOptions = _.map(namespaces, (namespace) => ({ + label: namespace.Name, + value: namespace.Name, + })); } catch (err) { this.Notifications.error('Failure', err, 'Unable to load namespaces data'); } @@ -404,7 +409,8 @@ class KubernetesDeployController { } } - this.onChangeNamespace(); + this.onChangeNamespace(this.formValues.Namespace); + this.state.viewReady = true; this.$window.onbeforeunload = () => { diff --git a/app/portainer/react/components/index.ts b/app/portainer/react/components/index.ts index 4b1c03608..9b2f7325d 100644 --- a/app/portainer/react/components/index.ts +++ b/app/portainer/react/components/index.ts @@ -9,6 +9,7 @@ import { withFormValidation } from '@/react-tools/withFormValidation'; import { GroupAssociationTable } from '@/react/portainer/environments/environment-groups/components/GroupAssociationTable'; import { AssociatedEnvironmentsSelector } from '@/react/portainer/environments/environment-groups/components/AssociatedEnvironmentsSelector'; import { withControlledInput } from '@/react-tools/withControlledInput'; +import { NamespacePortainerSelect } from '@/react/kubernetes/applications/components/NamespaceSelector/NamespaceSelector'; import { EnvironmentVariablesFieldset, @@ -199,11 +200,22 @@ export const ngModule = angular 'onChange', 'options', 'isMulti', + 'filterOption', 'isClearable', 'components', 'isLoading', 'noOptionsMessage', 'aria-label', + 'loadingMessage', + ]) + ) + .component( + 'namespacePortainerSelect', + r2a(NamespacePortainerSelect, [ + 'value', + 'onChange', + 'isDisabled', + 'options', ]) ) .component( diff --git a/app/portainer/views/auth/auth.html b/app/portainer/views/auth/auth.html index 09dadf050..2504141a7 100644 --- a/app/portainer/views/auth/auth.html +++ b/app/portainer/views/auth/auth.html @@ -4,7 +4,10 @@
- +
+ + +
diff --git a/app/portainer/views/init/admin/initAdmin.html b/app/portainer/views/init/admin/initAdmin.html index b5cfcfeb4..afff165b2 100644 --- a/app/portainer/views/init/admin/initAdmin.html +++ b/app/portainer/views/init/admin/initAdmin.html @@ -5,7 +5,10 @@
- +
+ + +
diff --git a/app/portainer/views/logout/logout.html b/app/portainer/views/logout/logout.html index fe9b2513d..95299d5d0 100644 --- a/app/portainer/views/logout/logout.html +++ b/app/portainer/views/logout/logout.html @@ -4,7 +4,10 @@
- +
+ + +
diff --git a/app/react-tools/test-mocks.ts b/app/react-tools/test-mocks.ts index 20fe7dee3..d9c7d273c 100644 --- a/app/react-tools/test-mocks.ts +++ b/app/react-tools/test-mocks.ts @@ -10,7 +10,7 @@ import { export function createMockUsers( count: number, - roles: Role | Role[] | ((id: UserId) => Role) = () => _.random(1, 3) + roles: Role | Role[] | ((id: UserId) => Role) ): User[] { return _.range(1, count + 1).map((value) => ({ Id: value, @@ -40,7 +40,14 @@ function getRoles( return roles; } - return roles[id]; + // Roles is an array + if (roles.length === 0) { + throw new Error('No roles provided'); + } + + // The number of roles is not necessarily the same length as the number of users + // so we need to distribute the roles evenly and consistently + return roles[(id - 1) % roles.length]; } export function createMockTeams(count: number): Team[] { diff --git a/app/react/components/ExternalLink.tsx b/app/react/components/ExternalLink.tsx index ef16dcb66..1bd839cad 100644 --- a/app/react/components/ExternalLink.tsx +++ b/app/react/components/ExternalLink.tsx @@ -1,20 +1,20 @@ -import { ExternalLink as ExternalLinkIcon } from 'lucide-react'; +import { ArrowUpRight } from 'lucide-react'; import { PropsWithChildren } from 'react'; import clsx from 'clsx'; import { AutomationTestingProps } from '@/types'; -import { Icon } from '@@/Icon'; - interface Props { to: string; className?: string; + showIcon?: boolean; } export function ExternalLink({ to, className, children, + showIcon = true, 'data-cy': dataCy, }: PropsWithChildren) { return ( @@ -23,10 +23,10 @@ export function ExternalLink({ target="_blank" rel="noreferrer" data-cy={dataCy} - className={clsx('inline-flex items-center gap-1', className)} + className={clsx('inline-flex align-baseline', className)} > - - {children} + {children} + {showIcon && } ); } diff --git a/app/react/components/FallbackImage.tsx b/app/react/components/FallbackImage.tsx index ee6956f24..eaa4f1272 100644 --- a/app/react/components/FallbackImage.tsx +++ b/app/react/components/FallbackImage.tsx @@ -27,5 +27,5 @@ export function FallbackImage({ src, fallbackIcon, alt, className }: Props) { } // fallback icon if there is an error loading the image - return <>{fallbackIcon}; + return
{fallbackIcon}
; } diff --git a/app/react/components/InformationPanel.tsx b/app/react/components/InformationPanel.tsx index b5c9dafc9..f25afadba 100644 --- a/app/react/components/InformationPanel.tsx +++ b/app/react/components/InformationPanel.tsx @@ -19,7 +19,7 @@ export function InformationPanel({ children, }: PropsWithChildren) { return ( - +
{title && ( diff --git a/app/react/components/ViewLoading/ViewLoading.tsx b/app/react/components/ViewLoading/ViewLoading.tsx index 5ade961bb..139c67637 100644 --- a/app/react/components/ViewLoading/ViewLoading.tsx +++ b/app/react/components/ViewLoading/ViewLoading.tsx @@ -1,7 +1,4 @@ import clsx from 'clsx'; -import { Settings } from 'lucide-react'; - -import { Icon } from '@@/Icon'; import styles from './ViewLoading.module.css'; @@ -18,12 +15,7 @@ export function ViewLoading({ message }: Props) {
- {message && ( - - {message} - - - )} + {message && {message}}
); } diff --git a/app/react/components/form-components/PortainerSelect.tsx b/app/react/components/form-components/PortainerSelect.tsx index 9ddf234da..6800d0013 100644 --- a/app/react/components/form-components/PortainerSelect.tsx +++ b/app/react/components/form-components/PortainerSelect.tsx @@ -5,15 +5,25 @@ import { } from 'react-select'; import _ from 'lodash'; import { AriaAttributes } from 'react'; +import { FilterOptionOption } from 'react-select/dist/declarations/src/filters'; import { AutomationTestingProps } from '@/types'; -import { Select as ReactSelect } from '@@/form-components/ReactSelect'; +import { + Creatable, + Select as ReactSelect, +} from '@@/form-components/ReactSelect'; export interface Option { value: TValue; label: string; disabled?: boolean; + [key: string]: unknown; +} + +export interface GroupOption { + label: string; + options: Option[]; } type Options = OptionsOrGroups< @@ -21,7 +31,7 @@ type Options = OptionsOrGroups< GroupBase> >; -interface SharedProps +interface SharedProps extends AutomationTestingProps, Pick { name?: string; @@ -32,9 +42,14 @@ interface SharedProps bindToBody?: boolean; isLoading?: boolean; noOptionsMessage?: () => string; + loadingMessage?: () => string; + filterOption?: ( + option: FilterOptionOption>, + rawInput: string + ) => boolean; } -interface MultiProps extends SharedProps { +interface MultiProps extends SharedProps { value: readonly TValue[]; onChange(value: TValue[]): void; options: Options; @@ -44,9 +59,12 @@ interface MultiProps extends SharedProps { true, GroupBase> >; + formatCreateLabel?: (input: string) => string; + onCreateOption?: (input: string) => void; + isCreatable?: boolean; } -interface SingleProps extends SharedProps { +interface SingleProps extends SharedProps { value: TValue; onChange(value: TValue | null): void; options: Options; @@ -58,9 +76,13 @@ interface SingleProps extends SharedProps { >; } -type Props = MultiProps | SingleProps; +export type PortainerSelectProps = + | MultiProps + | SingleProps; -export function PortainerSelect(props: Props) { +export function PortainerSelect( + props: PortainerSelectProps +) { return isMultiProps(props) ? ( // eslint-disable-next-line react/jsx-props-no-spreading @@ -71,7 +93,7 @@ export function PortainerSelect(props: Props) { } function isMultiProps( - props: Props + props: PortainerSelectProps ): props is MultiProps { return 'isMulti' in props && !!props.isMulti; } @@ -87,9 +109,11 @@ export function SingleSelect({ placeholder, isClearable, bindToBody, + filterOption, components, isLoading, noOptionsMessage, + loadingMessage, isMulti, ...aria }: SingleProps) { @@ -116,9 +140,11 @@ export function SingleSelect({ placeholder={placeholder} isDisabled={disabled} menuPortalTarget={bindToBody ? document.body : undefined} + filterOption={filterOption} components={components} isLoading={isLoading} noOptionsMessage={noOptionsMessage} + loadingMessage={loadingMessage} // eslint-disable-next-line react/jsx-props-no-spreading {...aria} /> @@ -159,14 +185,20 @@ export function MultiSelect({ disabled, isClearable, bindToBody, + filterOption, components, isLoading, noOptionsMessage, + loadingMessage, + formatCreateLabel, + onCreateOption, + isCreatable, ...aria }: Omit, 'isMulti'>) { const selectedOptions = findSelectedOptions(options, value); + const SelectComponent = isCreatable ? Creatable : ReactSelect; return ( - ({ placeholder={placeholder} isDisabled={disabled} menuPortalTarget={bindToBody ? document.body : undefined} + filterOption={filterOption} components={components} isLoading={isLoading} noOptionsMessage={noOptionsMessage} + loadingMessage={loadingMessage} + formatCreateLabel={formatCreateLabel} + onCreateOption={onCreateOption} // eslint-disable-next-line react/jsx-props-no-spreading {...aria} /> diff --git a/app/react/components/form-components/ReactSelect.tsx b/app/react/components/form-components/ReactSelect.tsx index a9f4e6282..c7ea47366 100644 --- a/app/react/components/form-components/ReactSelect.tsx +++ b/app/react/components/form-components/ReactSelect.tsx @@ -5,12 +5,14 @@ import ReactSelectAsync, { AsyncProps as ReactSelectAsyncProps, } from 'react-select/async'; import ReactSelect, { + components, GroupBase, + InputProps, OptionsOrGroups, Props as ReactSelectProps, } from 'react-select'; import clsx from 'clsx'; -import { RefAttributes, useMemo } from 'react'; +import { RefAttributes, useMemo, useCallback } from 'react'; import ReactSelectType from 'react-select/dist/declarations/src/Select'; import './ReactSelect.css'; @@ -52,6 +54,9 @@ type Props< | CreatableProps | RegularProps; +/** + * DO NOT use this component directly, use PortainerSelect instead. + */ export function Select< Option = DefaultOption, IsMulti extends boolean = false, @@ -68,24 +73,37 @@ export function Select< id: string; }) { const Component = isCreatable ? ReactSelectCreatable : ReactSelect; - const { options } = props; + const { + options, + 'data-cy': dataCy, + components: componentsProp, + ...rest + } = props; + + const memoizedComponents = useMemoizedSelectComponents< + Option, + IsMulti, + Group + >(dataCy, componentsProp); if ((options?.length || 0) > 1000) { return ( ); } return ( ); } @@ -94,13 +112,25 @@ export function Creatable< Option = DefaultOption, IsMulti extends boolean = false, Group extends GroupBase
- + > + Install + ); } diff --git a/app/react/kubernetes/helm/HelmTemplates/HelmTemplates.tsx b/app/react/kubernetes/helm/HelmTemplates/HelmTemplates.tsx index ffd122cd2..cca219fbc 100644 --- a/app/react/kubernetes/helm/HelmTemplates/HelmTemplates.tsx +++ b/app/react/kubernetes/helm/HelmTemplates/HelmTemplates.tsx @@ -1,15 +1,20 @@ import { useState } from 'react'; -import { compact } from 'lodash'; import { useCurrentUser } from '@/react/hooks/useUser'; -import { Chart } from '../types'; -import { useHelmChartList } from '../queries/useHelmChartList'; -import { useHelmRegistries } from '../queries/useHelmRegistries'; +import { FormSection } from '@@/form-components/FormSection'; + +import { useHelmHTTPChartList } from '../queries/useHelmChartList'; +import { Chart } from '../types'; +import { + HelmRegistrySelect, + RepoValue, +} from '../components/HelmRegistrySelect'; +import { useHelmRepoOptions } from '../queries/useHelmRepositories'; -import { HelmTemplatesList } from './HelmTemplatesList'; -import { HelmTemplatesSelectedItem } from './HelmTemplatesSelectedItem'; import { HelmInstallForm } from './HelmInstallForm'; +import { HelmTemplatesSelectedItem } from './HelmTemplatesSelectedItem'; +import { HelmTemplatesList } from './HelmTemplatesList'; interface Props { onSelectHelmChart: (chartName: string) => void; @@ -19,11 +24,60 @@ interface Props { export function HelmTemplates({ onSelectHelmChart, namespace, name }: Props) { const [selectedChart, setSelectedChart] = useState(null); - const [selectedRegistry, setSelectedRegistry] = useState(null); - + const [selectedRepo, setSelectedRepo] = useState(null); const { user } = useCurrentUser(); - const helmReposQuery = useHelmRegistries(); - const chartListQuery = useHelmChartList(user.Id, compact([selectedRegistry])); + const chartListQuery = useHelmHTTPChartList( + user.Id, + selectedRepo?.repoUrl ?? '', + !!selectedRepo?.repoUrl + ); + const repoOptionsQuery = useHelmRepoOptions(); + const isRepoAvailable = + !!repoOptionsQuery.data && repoOptionsQuery.data.length > 0; + + return ( +
+
+ + {selectedChart ? ( + <> + + + + ) : ( + <> + + {selectedRepo && ( + + )} + + )} + +
+
+ ); + function clearHelmChart() { setSelectedChart(null); onSelectHelmChart(''); @@ -33,33 +87,4 @@ export function HelmTemplates({ onSelectHelmChart, namespace, name }: Props) { setSelectedChart(chart); onSelectHelmChart(chart.name); } - - return ( -
-
- {selectedChart ? ( - <> - - - - ) : ( - - )} -
-
- ); } diff --git a/app/react/kubernetes/helm/HelmTemplates/HelmTemplatesList.test.tsx b/app/react/kubernetes/helm/HelmTemplates/HelmTemplatesList.test.tsx index 98b96e9a4..bfaa6e7e8 100644 --- a/app/react/kubernetes/helm/HelmTemplates/HelmTemplatesList.test.tsx +++ b/app/react/kubernetes/helm/HelmTemplates/HelmTemplatesList.test.tsx @@ -46,25 +46,63 @@ const mockCharts: Chart[] = [ const selectActionMock = vi.fn(); +const mockUseEnvironmentId = vi.fn(() => 1); + +vi.mock('@/react/hooks/useEnvironmentId', () => ({ + useEnvironmentId: () => mockUseEnvironmentId(), +})); + +// Mock the helm registries query +vi.mock('../queries/useHelmRegistries', () => ({ + useHelmRegistries: vi.fn(() => ({ + data: ['https://example.com', 'https://example.com/2'], + isInitialLoading: false, + isError: false, + })), +})); + +// Mock the environment registries query +vi.mock( + '@/react/portainer/environments/queries/useEnvironmentRegistries', + () => ({ + useEnvironmentRegistries: vi.fn(() => ({ + data: [ + { Id: 1, URL: 'https://registry.example.com' }, + { Id: 2, URL: 'https://registry2.example.com' }, + ], + isInitialLoading: false, + isError: false, + })), + }) +); + function renderComponent({ loading = false, charts = mockCharts, selectAction = selectActionMock, - selectedRegistry = '', + selectedRegistry = { + repoUrl: 'https://example.com', + name: 'Test Registry', + }, +}: { + loading?: boolean; + charts?: Chart[]; + selectAction?: (chart: Chart) => void; + selectedRegistry?: { + repoUrl?: string; + name?: string; + } | null; } = {}) { const user = new UserViewModel({ Username: 'user' }); - const registries = ['https://example.com', 'https://example.com/2']; const Wrapped = withTestQueryProvider( withUserProvider( withTestRouter(() => ( {}} /> )), user @@ -81,8 +119,10 @@ describe('HelmTemplatesList', () => { it('should display title and charts list', async () => { renderComponent(); - // Check for the title - expect(screen.getByText('Helm chart')).toBeInTheDocument(); + // Check for the title with registry name + expect( + screen.getByText('Select a helm chart from Test Registry') + ).toBeInTheDocument(); // Check for charts expect(screen.getByText('test-chart-1')).toBeInTheDocument(); @@ -160,21 +200,27 @@ describe('HelmTemplatesList', () => { }); it('should show empty message when no charts are available and a registry is selected', async () => { - renderComponent({ charts: [], selectedRegistry: 'https://example.com' }); + renderComponent({ + charts: [], + selectedRegistry: { + repoUrl: 'https://example.com', + name: 'Test Registry', + }, + }); // Check for empty message expect( - screen.getByText('No helm charts available in this registry.') + screen.getByText('No helm charts available in this repository.') ).toBeInTheDocument(); }); it("should show 'select registry' message when no charts are available and no registry is selected", async () => { - renderComponent({ charts: [] }); + renderComponent({ charts: [], selectedRegistry: null }); // Check for message expect( screen.getByText( - 'Please select a registry to view available Helm charts.' + 'Please select a repository to view available Helm charts.' ) ).toBeInTheDocument(); }); diff --git a/app/react/kubernetes/helm/HelmTemplates/HelmTemplatesList.tsx b/app/react/kubernetes/helm/HelmTemplates/HelmTemplatesList.tsx index 1b02bed1d..3d9e86578 100644 --- a/app/react/kubernetes/helm/HelmTemplates/HelmTemplatesList.tsx +++ b/app/react/kubernetes/helm/HelmTemplates/HelmTemplatesList.tsx @@ -1,59 +1,47 @@ import { useState, useMemo } from 'react'; -import { components, OptionProps } from 'react-select'; -import { - PortainerSelect, - Option, -} from '@/react/components/form-components/PortainerSelect'; -import { Link } from '@/react/components/Link'; +import { PortainerSelect } from '@/react/components/form-components/PortainerSelect'; -import { InsightsBox } from '@@/InsightsBox'; import { SearchBar } from '@@/datatables/SearchBar'; import { InlineLoader } from '@@/InlineLoader'; import { Chart } from '../types'; +import { RepoValue } from '../components/HelmRegistrySelect'; import { HelmTemplatesListItem } from './HelmTemplatesListItem'; interface Props { - isLoading: boolean; + isLoadingCharts: boolean; charts?: Chart[]; selectAction: (chart: Chart) => void; - registries: string[]; - selectedRegistry: string | null; - setSelectedRegistry: (registry: string | null) => void; + selectedRegistry: RepoValue | null; } export function HelmTemplatesList({ - isLoading, + isLoadingCharts, charts = [], selectAction, - registries, selectedRegistry, - setSelectedRegistry, }: Props) { const [textFilter, setTextFilter] = useState(''); const [selectedCategory, setSelectedCategory] = useState(null); const categories = useMemo(() => getCategories(charts), [charts]); - const registryOptions = useMemo( - () => - registries.map((registry) => ({ - label: registry, - value: registry, - })), - [registries] - ); const filteredCharts = useMemo( () => getFilteredCharts(charts, textFilter, selectedCategory), [charts, textFilter, selectedCategory] ); + const isSelectedRegistryEmpty = + !isLoadingCharts && charts.length === 0 && selectedRegistry; + return (
-
-
Helm chart
+
+
+ Select a helm chart from {selectedRegistry?.name} +
-
- -
- -
+
-
-
- Select the Helm chart to use. Bring further Helm charts into your - selection list via{' '} - - User settings - Helm repositories - - . -
- - - At present Portainer does not support OCI format Helm charts. - Support for OCI charts will be available in a future release. -
- If you would like to provide feedback on OCI support or get access - to early releases to test this functionality,{' '} - - please get in touch - - . - - } - /> -
{filteredCharts.map((chart) => ( @@ -138,7 +77,7 @@ export function HelmTemplatesList({
No Helm charts found
)} - {isLoading && ( + {isLoadingCharts && (
Loading helm charts... @@ -151,15 +90,15 @@ export function HelmTemplatesList({
)} - {!isLoading && charts.length === 0 && selectedRegistry && ( + {isSelectedRegistryEmpty && (
- No helm charts available in this registry. + No helm charts available in this repository.
)} {!selectedRegistry && (
- Please select a registry to view available Helm charts. + Please select a repository to view available Helm charts.
)}
@@ -167,20 +106,6 @@ export function HelmTemplatesList({ ); } -// truncate the registry text, because some registry names are urls, which are too long -function RegistryOption(props: OptionProps>) { - const { data: registry } = props; - - return ( -
- {/* eslint-disable-next-line react/jsx-props-no-spreading */} - - {registry.value} - -
- ); -} - /** * Get categories from charts * @param charts - The charts to get the categories from diff --git a/app/react/kubernetes/helm/HelmTemplates/HelmTemplatesSelectedItem.tsx b/app/react/kubernetes/helm/HelmTemplates/HelmTemplatesSelectedItem.tsx index d6685f3c6..cc85170e2 100644 --- a/app/react/kubernetes/helm/HelmTemplates/HelmTemplatesSelectedItem.tsx +++ b/app/react/kubernetes/helm/HelmTemplates/HelmTemplatesSelectedItem.tsx @@ -26,7 +26,7 @@ export function HelmTemplatesSelectedItem({
diff --git a/app/react/kubernetes/helm/HelmTemplates/types.ts b/app/react/kubernetes/helm/HelmTemplates/types.ts index df0b09374..61a8451c9 100644 --- a/app/react/kubernetes/helm/HelmTemplates/types.ts +++ b/app/react/kubernetes/helm/HelmTemplates/types.ts @@ -1,4 +1,5 @@ export type HelmInstallFormValues = { values: string; version: string; + repo: string; }; diff --git a/app/react/kubernetes/helm/components/HelmRegistrySelect.test.tsx b/app/react/kubernetes/helm/components/HelmRegistrySelect.test.tsx new file mode 100644 index 000000000..5934e75ba --- /dev/null +++ b/app/react/kubernetes/helm/components/HelmRegistrySelect.test.tsx @@ -0,0 +1,242 @@ +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { vi } from 'vitest'; + +import selectEvent from '@/react/test-utils/react-select'; +import { withTestQueryProvider } from '@/react/test-utils/withTestQuery'; +import { withUserProvider } from '@/react/test-utils/withUserProvider'; +import { withTestRouter } from '@/react/test-utils/withRouter'; +import { UserViewModel } from '@/portainer/models/user'; +import { RegistryTypes } from '@/react/portainer/registries/types/registry'; +import { useCurrentUser } from '@/react/hooks/useUser'; +import { User, Role } from '@/portainer/users/types'; + +import { HelmRegistrySelect, RepoValue } from './HelmRegistrySelect'; + +// Mock the hooks with factory functions - preserve other exports +vi.mock('@/react/hooks/useUser', async () => { + const actual = await vi.importActual('@/react/hooks/useUser'); + return { + ...actual, + useCurrentUser: vi.fn(), + }; +}); + +const mockOnRegistryChange = vi.fn(); + +const defaultProps = { + selectedRegistry: null, + onRegistryChange: mockOnRegistryChange, + isRepoAvailable: true, + isLoading: false, + isError: false, + repoOptions: [], +}; + +const mockRepoOptions = [ + { + value: { + repoUrl: 'https://charts.bitnami.com/bitnami', + name: 'Bitnami', + type: RegistryTypes.CUSTOM, + }, + label: 'Bitnami', + }, + { + value: { + repoUrl: 'https://kubernetes-charts.storage.googleapis.com', + name: 'Stable', + type: RegistryTypes.CUSTOM, + }, + label: 'Stable', + }, +]; + +interface MockUserHookReturn { + user: User; + isPureAdmin: boolean; +} + +interface UserProps { + isPureAdmin?: boolean; +} + +// Get the mocked functions +const mockUseCurrentUser = vi.mocked(useCurrentUser); + +function renderComponent(props = {}, userProps: UserProps = {}) { + const userResult: MockUserHookReturn = { + user: { + Id: 1, + Username: 'admin', + Role: Role.Admin, + EndpointAuthorizations: {}, + UseCache: false, + ThemeSettings: { + color: 'auto', + }, + }, + isPureAdmin: userProps.isPureAdmin || false, + }; + + mockUseCurrentUser.mockReturnValue(userResult); + + const Component = withTestQueryProvider( + withUserProvider( + withTestRouter(HelmRegistrySelect), + new UserViewModel({ Username: 'admin', Role: 1 }) + ) + ); + + return render(); +} + +describe('HelmRegistrySelect', () => { + beforeEach(() => { + vi.clearAllMocks(); + mockUseCurrentUser.mockClear(); + }); + + describe('Basic rendering', () => { + it('should render with default placeholder', () => { + renderComponent(); + expect(screen.getByText('Select a repository')).toBeInTheDocument(); + }); + + it('should render with custom placeholder', () => { + renderComponent({ placeholder: 'Custom placeholder' }); + expect(screen.getByText('Custom placeholder')).toBeInTheDocument(); + }); + + it('should render loading state', () => { + renderComponent({ isLoading: true }); + expect(screen.getByRole('combobox')).toBeInTheDocument(); + }); + + it('should render error state', () => { + renderComponent({ isError: true }); + expect( + screen.getByText('Unable to load registry options.') + ).toBeInTheDocument(); + }); + }); + + describe('Repository options', () => { + it('should display repository options', async () => { + const user = userEvent.setup(); + renderComponent({ repoOptions: mockRepoOptions }); + + const select = screen.getByRole('combobox'); + await user.click(select); + + expect(screen.getByText('Bitnami')).toBeInTheDocument(); + expect(screen.getByText('Stable')).toBeInTheDocument(); + }); + + it.skip('should call onRegistryChange when option is selected', async () => { + // Skipping this test due to react-select testing complexity + // The onChange functionality is covered by integration tests + renderComponent({ repoOptions: mockRepoOptions }); + + const select = screen.getByRole('combobox'); + await selectEvent.select(select, 'Bitnami'); + + expect(mockOnRegistryChange).toHaveBeenCalledWith({ + repoUrl: 'https://charts.bitnami.com/bitnami', + name: 'Bitnami', + type: RegistryTypes.CUSTOM, + }); + }); + + it('should show selected repository value', () => { + const selectedRegistry: RepoValue = { + repoUrl: 'https://charts.bitnami.com/bitnami', + name: 'Bitnami', + type: RegistryTypes.CUSTOM, + }; + + renderComponent({ + selectedRegistry, + repoOptions: mockRepoOptions, + }); + + // Since the component uses PortainerSelect which manages the display value, + // we verify the props are correctly passed by checking the select element exists + expect(screen.getByRole('combobox')).toBeInTheDocument(); + }); + }); + + describe('No repositories warning', () => { + it('should show no repositories warning when no repos are available', () => { + renderComponent({ + isRepoAvailable: false, + namespace: 'test-namespace', + }); + + expect( + screen.getByText(/There are no repositories available./) + ).toBeInTheDocument(); + }); + + it('should not show warning when loading', () => { + renderComponent({ + isRepoAvailable: false, + namespace: 'test-namespace', + isLoading: true, + }); + + expect( + screen.queryByText('There are no repositories available.') + ).not.toBeInTheDocument(); + }); + + it('should not show warning when no namespace is provided', () => { + renderComponent({ + isRepoAvailable: false, + }); + + expect( + screen.queryByText('There are no repositories available.') + ).not.toBeInTheDocument(); + }); + }); + + describe('Tooltip content', () => { + it('should render the component with label and tooltip', () => { + renderComponent({}, { isPureAdmin: true }); + + // Verify that the component renders the main label + expect(screen.getByText('Helm chart source')).toBeInTheDocument(); + + expect(screen.getByRole('combobox')).toBeInTheDocument(); + }); + }); + + describe('Loading and error states', () => { + it('should not show no repos warning when loading', () => { + renderComponent({ + isLoading: true, + isRepoAvailable: false, + repoOptions: [], + namespace: 'test-namespace', + }); + + expect( + screen.queryByText('There are no repositories available.') + ).not.toBeInTheDocument(); + }); + + it('should show error when API fails', () => { + renderComponent({ + isLoading: false, + isError: true, + isRepoAvailable: false, + namespace: 'test-namespace', + }); + + expect( + screen.getByText('Unable to load registry options.') + ).toBeInTheDocument(); + }); + }); +}); diff --git a/app/react/kubernetes/helm/components/HelmRegistrySelect.tsx b/app/react/kubernetes/helm/components/HelmRegistrySelect.tsx new file mode 100644 index 000000000..55569f900 --- /dev/null +++ b/app/react/kubernetes/helm/components/HelmRegistrySelect.tsx @@ -0,0 +1,156 @@ +import { GroupBase } from 'react-select'; + +import { + PortainerSelect, + Option, +} from '@/react/components/form-components/PortainerSelect'; +import { useCurrentUser } from '@/react/hooks/useUser'; +import { RegistryTypes } from '@/react/portainer/registries/types/registry'; + +import { FormControl } from '@@/form-components/FormControl'; +import { Alert } from '@@/Alert'; +import { Link } from '@@/Link'; +import { TextTip } from '@@/Tip/TextTip'; + +export type RepoValue = { + repoUrl?: string; // set for traditional https helm repos + name?: string; + type?: RegistryTypes; +}; + +interface Props { + selectedRegistry: RepoValue | null; + onRegistryChange: (registry: RepoValue | null) => void; + namespace?: string; + placeholder?: string; + 'data-cy'?: string; + isRepoAvailable: boolean; + isLoading: boolean; + isError: boolean; + repoOptions: GroupBase>[]; +} + +export function HelmRegistrySelect({ + selectedRegistry, + onRegistryChange, + namespace, + placeholder = 'Select a repository', + 'data-cy': dataCy = 'helm-registry-select', + isRepoAvailable, + isLoading, + isError, + repoOptions, +}: Props) { + const { isPureAdmin } = useCurrentUser(); + + return ( + } + > + + placeholder={placeholder} + value={selectedRegistry ?? {}} + options={repoOptions} + isLoading={isLoading} + onChange={onRegistryChange} + isClearable + bindToBody + data-cy={dataCy} + /> + + {isError && Unable to load registry options.} + + ); +} + +function HelmChartSourceTooltip({ isPureAdmin }: { isPureAdmin: boolean }) { + if (isPureAdmin) { + return ( + <> + +
+ + + ); + } + + // Non-admin + return ; +} + +function NoReposWarning({ + hasNoRepos, + isLoading, + namespace, + isPureAdmin, +}: { + hasNoRepos: boolean; + isLoading: boolean; + namespace?: string; + isPureAdmin: boolean; +}) { + if (!hasNoRepos || isLoading || !namespace) { + return null; + } + + return ( + + There are no repositories available. + + + ); +} + +function CreateRepoMessage({ isPureAdmin }: { isPureAdmin: boolean }) { + if (isPureAdmin) { + return ( + <> + +
+ + + ); + } + + // Non-admin + return ; +} + +function CreateUserRepoMessage() { + return ( + <> + You can define repositories in the{' '} + + User settings - Helm repositories + + . + + ); +} + +function CreateGlobalRepoMessage() { + return ( + <> + You can also define repositories in the{' '} + + Portainer settings + + . + + ); +} diff --git a/app/react/kubernetes/helm/queries/useHelmChartList.ts b/app/react/kubernetes/helm/queries/useHelmChartList.ts index 5824236bc..b2cc82b9d 100644 --- a/app/react/kubernetes/helm/queries/useHelmChartList.ts +++ b/app/react/kubernetes/helm/queries/useHelmChartList.ts @@ -1,6 +1,5 @@ -import { useQueries } from '@tanstack/react-query'; -import { compact, flatMap } from 'lodash'; -import { useMemo } from 'react'; +import { compact } from 'lodash'; +import { useQuery } from '@tanstack/react-query'; import axios from '@/portainer/services/axios'; import { withGlobalError } from '@/react-tools/react-query'; @@ -8,45 +7,28 @@ import { withGlobalError } from '@/react-tools/react-query'; import { Chart, HelmChartsResponse } from '../types'; /** - * React hook to fetch helm charts from the provided repositories - * Charts from each repository are loaded independently, allowing the UI - * to show charts as they become available instead of waiting for all - * repositories to load + * React hook to fetch helm charts from the provided HTTP repository. + * Charts are loaded from the specified repository URL. * * @param userId User ID - * @param repositories List of repository URLs to fetch charts from + * @param repository Repository URL to fetch charts from + * @param enabled Flag indicating if the query should be enabled + * @returns Query result containing helm charts */ -export function useHelmChartList(userId: number, repositories: string[] = []) { - // Fetch charts from each repository in parallel as separate queries - const chartQueries = useQueries({ - queries: useMemo( - () => - repositories.map((repo) => ({ - queryKey: [userId, repo, 'helm-charts'], - queryFn: () => getChartsFromRepo(repo), - enabled: !!userId && repositories.length > 0, - // one request takes a long time, so fail early to get feedback to the user faster - retries: false, - ...withGlobalError(`Unable to retrieve Helm charts from ${repo}`), - })), - [repositories, userId] - ), +export function useHelmHTTPChartList( + userId: number, + repository: string, + enabled: boolean +) { + return useQuery({ + queryKey: [userId, repository, 'helm-charts'], + queryFn: () => getChartsFromRepo(repository), + enabled: !!userId && !!repository && enabled, + // one request takes a long time, so fail early to get feedback to the user faster + retry: false, + ...withGlobalError(`Unable to retrieve Helm charts from ${repository}`), + cacheTime: 1000 * 60 * 60 * 8, // 8 hours so that the chart list populates faster (keep stale time the same to always revalidate) }); - - // Combine the results for easier consumption by components - const allCharts = useMemo( - () => flatMap(compact(chartQueries.map((q) => q.data))), - [chartQueries] - ); - - return { - // Data from all repositories that have loaded so far - data: allCharts, - // Overall loading state - isInitialLoading: chartQueries.some((q) => q.isInitialLoading), - // Overall error state - isError: chartQueries.some((q) => q.isError), - }; } async function getChartsFromRepo(repo: string): Promise { diff --git a/app/react/kubernetes/helm/queries/useHelmChartValues.ts b/app/react/kubernetes/helm/queries/useHelmChartValues.ts index 2c1a95995..81e9cd9c2 100644 --- a/app/react/kubernetes/helm/queries/useHelmChartValues.ts +++ b/app/react/kubernetes/helm/queries/useHelmChartValues.ts @@ -4,8 +4,11 @@ import axios, { parseAxiosError } from '@/portainer/services/axios'; import { withGlobalError } from '@/react-tools/react-query'; type Params = { + /** The name of the chart to get the values for */ chart: string; + /** The repository URL or registry ID */ repo: string; + /** The version of the chart to get the values for */ version?: string; }; @@ -16,18 +19,26 @@ async function getHelmChartValues(params: Params) { }); return response.data; } catch (err) { - throw parseAxiosError(err as Error, 'Unable to get Helm chart values'); + throw parseAxiosError(err, 'Unable to get Helm chart values'); } } -export function useHelmChartValues(params: Params) { +export function useHelmChartValues(params: Params, isLatestVersion = false) { + const hasValidRepoUrl = !!params.repo; return useQuery({ - queryKey: ['helm-chart-values', params.repo, params.chart, params.version], + queryKey: [ + 'helm-chart-values', + params.repo, + params.chart, + // if the latest version is fetched, use the latest version key to cache the latest version + isLatestVersion ? 'latest' : params.version, + ], queryFn: () => getHelmChartValues(params), - enabled: !!params.chart && !!params.repo, + enabled: !!params.chart && hasValidRepoUrl, select: (data) => ({ values: data, }), + retry: 1, staleTime: 60 * 1000 * 20, // 60 minutes, because values are not expected to change often ...withGlobalError('Unable to get Helm chart values'), }); diff --git a/app/react/kubernetes/helm/queries/useHelmRegistries.ts b/app/react/kubernetes/helm/queries/useHelmRegistries.ts deleted file mode 100644 index f48fb72fa..000000000 --- a/app/react/kubernetes/helm/queries/useHelmRegistries.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { useQuery } from '@tanstack/react-query'; -import { compact } from 'lodash'; - -import axios, { parseAxiosError } from '@/portainer/services/axios'; -import { UserId } from '@/portainer/users/types'; -import { withGlobalError } from '@/react-tools/react-query'; -import { useCurrentUser } from '@/react/hooks/useUser'; - -import { HelmRegistriesResponse } from '../types'; - -/** - * Hook to fetch all Helm registries for the current user - */ -export function useHelmRegistries() { - const { user } = useCurrentUser(); - return useQuery( - ['helm', 'registries'], - async () => getHelmRegistries(user.Id), - { - enabled: !!user.Id, - ...withGlobalError('Unable to retrieve helm registries'), - } - ); -} - -/** - * Get Helm registries for user - */ -async function getHelmRegistries(userId: UserId) { - try { - const { data } = await axios.get( - `users/${userId}/helm/repositories` - ); - const repos = compact([ - // compact will remove the global repository if it's empty - data.GlobalRepository.toLowerCase(), - ...data.UserRepositories.map((repo) => repo.URL.toLowerCase()), - ]); - return [...new Set(repos)]; - } catch (err) { - throw parseAxiosError(err, 'Unable to retrieve helm repositories for user'); - } -} diff --git a/app/react/kubernetes/helm/queries/useHelmRepoVersions.ts b/app/react/kubernetes/helm/queries/useHelmRepoVersions.ts index 006b74599..98b7c3c99 100644 --- a/app/react/kubernetes/helm/queries/useHelmRepoVersions.ts +++ b/app/react/kubernetes/helm/queries/useHelmRepoVersions.ts @@ -10,14 +10,21 @@ interface HelmSearch { } interface Entries { - [key: string]: { version: string }[]; + [key: string]: { version: string; appVersion: string }[]; } export interface ChartVersion { + Chart?: string; Repo: string; + Label?: string; Version: string; + AppVersion?: string; } +type RepoSource = { + repo?: string; +}; + /** * React hook to get a list of available versions for a chart from specified repositories * @@ -29,21 +36,21 @@ export interface ChartVersion { export function useHelmRepoVersions( chart: string, staleTime: number, - repositories: string[] = [], + repoSources: RepoSource[] = [], useCache: boolean = true ) { // Fetch versions from each repository in parallel as separate queries const versionQueries = useQueries({ queries: useMemo( () => - repositories.map((repo) => ({ + repoSources.map(({ repo }) => ({ queryKey: ['helm', 'repositories', chart, repo, useCache], - queryFn: () => getSearchHelmRepo(repo, chart, useCache), - enabled: !!chart && repositories.length > 0, + queryFn: () => getSearchHelmRepo({ repo, chart, useCache }), + enabled: !!chart && repoSources.length > 0, staleTime, ...withGlobalError(`Unable to retrieve versions from ${repo}`), })), - [repositories, chart, staleTime, useCache] + [repoSources, chart, staleTime, useCache] ), }); @@ -55,30 +62,37 @@ export function useHelmRepoVersions( return { data: allVersions, - isInitialLoading: versionQueries.some((q) => q.isLoading), + isInitialLoading: versionQueries.some((q) => q.isInitialLoading), isError: versionQueries.some((q) => q.isError), isFetching: versionQueries.some((q) => q.isFetching), refetch: () => Promise.all(versionQueries.map((q) => q.refetch())), }; } +type SearchRepoParams = { + repo?: string; + chart: string; + useCache?: boolean; +}; + /** * Get Helm repositories for user */ async function getSearchHelmRepo( - repo: string, - chart: string, - useCache: boolean = true + params: SearchRepoParams ): Promise { try { const { data } = await axios.get(`templates/helm`, { - params: { repo, chart, useCache }, + params, }); - const versions = data.entries[chart]; + // if separated by '/', take the last part + const chartKey = params.chart.split('/').pop() || params.chart; + const versions = data.entries[chartKey]; return ( versions?.map((v) => ({ - Repo: repo, + Repo: params.repo ?? '', Version: v.version, + AppVersion: v.appVersion, })) ?? [] ); } catch (err) { diff --git a/app/react/kubernetes/helm/queries/useHelmRepositories.ts b/app/react/kubernetes/helm/queries/useHelmRepositories.ts new file mode 100644 index 000000000..32a054325 --- /dev/null +++ b/app/react/kubernetes/helm/queries/useHelmRepositories.ts @@ -0,0 +1,84 @@ +import { useQuery } from '@tanstack/react-query'; +import { compact } from 'lodash'; + +import axios, { parseAxiosError } from '@/portainer/services/axios'; +import { UserId } from '@/portainer/users/types'; +import { withGlobalError } from '@/react-tools/react-query'; +import { useCurrentUser } from '@/react/hooks/useUser'; +import { Option } from '@/react/components/form-components/PortainerSelect'; + +import { HelmRegistriesResponse } from '../types'; +import { RepoValue } from '../components/HelmRegistrySelect'; + +/** + * Hook to fetch all Helm registries for the current user + */ +export function useUserHelmRepositories({ + select, +}: { + select?: (registries: string[]) => T; +} = {}) { + const { user } = useCurrentUser(); + return useQuery( + ['helm', 'registries'], + async () => getUserHelmRepositories(user.Id), + { + enabled: !!user.Id, + select, + ...withGlobalError('Unable to retrieve helm registries'), + } + ); +} + +export function useHelmRepoOptions() { + return useUserHelmRepositories({ + select: (registries) => { + const repoOptions = registries + .map>((registry) => ({ + label: registry, + value: { + repoUrl: registry, + isOCI: false, + name: registry, + }, + })) + .sort((a, b) => a.label.localeCompare(b.label)); + return [ + { + label: 'Helm Repositories', + options: repoOptions, + }, + { + label: 'OCI Registries', + options: [ + { + label: + 'Installing from an OCI registry is a Portainer Business Feature', + value: {}, + disabled: true, + }, + ], + }, + ]; + }, + }); +} + +/** + * Get Helm repositories for user + */ +async function getUserHelmRepositories(userId: UserId) { + try { + const { data } = await axios.get( + `users/${userId}/helm/repositories` + ); + // compact will remove the global repository if it's empty + const repos = compact([ + data.GlobalRepository.toLowerCase(), + ...data.UserRepositories.map((repo) => repo.URL.toLowerCase()), + ]); + return [...new Set(repos)]; + } catch (err) { + throw parseAxiosError(err, 'Unable to retrieve helm repositories for user'); + } +} diff --git a/app/react/kubernetes/helm/types.ts b/app/react/kubernetes/helm/types.ts index 3094f84b2..208745944 100644 --- a/app/react/kubernetes/helm/types.ts +++ b/app/react/kubernetes/helm/types.ts @@ -91,7 +91,7 @@ export interface HelmChartResponse { versions: string[]; } -export interface HelmRepositoryResponse { +export interface HelmRegistryResponse { Id: number; UserId: number; URL: string; @@ -99,7 +99,7 @@ export interface HelmRepositoryResponse { export interface HelmRegistriesResponse { GlobalRepository: string; - UserRepositories: HelmRepositoryResponse[]; + UserRepositories: HelmRegistryResponse[]; } export interface HelmChartsResponse { @@ -108,21 +108,13 @@ export interface HelmChartsResponse { generated: string; } -export interface InstallChartPayload { - Name: string; - Repo: string; - Chart: string; - Values: string; - Namespace: string; - Version?: string; -} - export interface UpdateHelmReleasePayload { namespace: string; values?: string; - repo?: string; + repo: string; name: string; chart: string; + appVersion?: string; version?: string; atomic?: boolean; } diff --git a/app/react/kubernetes/more-resources/ClusterRolesView/ClusterRolesDatatable/queries/useClusterRoles.ts b/app/react/kubernetes/more-resources/ClusterRolesView/ClusterRolesDatatable/queries/useClusterRoles.ts index dbacc4f4c..f27ffe642 100644 --- a/app/react/kubernetes/more-resources/ClusterRolesView/ClusterRolesDatatable/queries/useClusterRoles.ts +++ b/app/react/kubernetes/more-resources/ClusterRolesView/ClusterRolesDatatable/queries/useClusterRoles.ts @@ -21,7 +21,7 @@ export function useClusterRoles( }, { ...withGlobalError('Unable to get cluster roles'), - ...options, + refetchInterval: options?.autoRefreshRate, } ); } diff --git a/app/react/kubernetes/more-resources/JobsView/CronJobsDatatable/columns/command.tsx b/app/react/kubernetes/more-resources/JobsView/CronJobsDatatable/columns/command.tsx index 04e6e155f..2d1977a0a 100644 --- a/app/react/kubernetes/more-resources/JobsView/CronJobsDatatable/columns/command.tsx +++ b/app/react/kubernetes/more-resources/JobsView/CronJobsDatatable/columns/command.tsx @@ -3,5 +3,5 @@ import { columnHelper } from './helper'; export const command = columnHelper.accessor((row) => row.Command, { header: 'Command', id: 'command', - cell: ({ getValue }) => getValue(), + cell: ({ getValue }) => getValue() ?? '', }); diff --git a/app/react/kubernetes/more-resources/JobsView/CronJobsDatatable/columns/schedule.tsx b/app/react/kubernetes/more-resources/JobsView/CronJobsDatatable/columns/schedule.tsx index 60a673eb0..f979240e8 100644 --- a/app/react/kubernetes/more-resources/JobsView/CronJobsDatatable/columns/schedule.tsx +++ b/app/react/kubernetes/more-resources/JobsView/CronJobsDatatable/columns/schedule.tsx @@ -3,5 +3,5 @@ import { columnHelper } from './helper'; export const schedule = columnHelper.accessor((row) => row.Schedule, { header: 'Schedule', id: 'schedule', - cell: ({ getValue }) => getValue(), + cell: ({ getValue }) => getValue() ?? '', }); diff --git a/app/react/kubernetes/more-resources/JobsView/CronJobsDatatable/columns/timezone.tsx b/app/react/kubernetes/more-resources/JobsView/CronJobsDatatable/columns/timezone.tsx index 54bb35a79..3b262b93b 100644 --- a/app/react/kubernetes/more-resources/JobsView/CronJobsDatatable/columns/timezone.tsx +++ b/app/react/kubernetes/more-resources/JobsView/CronJobsDatatable/columns/timezone.tsx @@ -3,5 +3,5 @@ import { columnHelper } from './helper'; export const timezone = columnHelper.accessor((row) => row.Timezone, { header: 'Timezone', id: 'timezone', - cell: ({ getValue }) => getValue(), + cell: ({ getValue }) => getValue() ?? '', }); diff --git a/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/command.tsx b/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/command.tsx index 04e6e155f..2d1977a0a 100644 --- a/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/command.tsx +++ b/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/command.tsx @@ -3,5 +3,5 @@ import { columnHelper } from './helper'; export const command = columnHelper.accessor((row) => row.Command, { header: 'Command', id: 'command', - cell: ({ getValue }) => getValue(), + cell: ({ getValue }) => getValue() ?? '', }); diff --git a/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/duration.tsx b/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/duration.tsx index 23cfcaf34..b189d5a7a 100644 --- a/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/duration.tsx +++ b/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/duration.tsx @@ -3,5 +3,5 @@ import { columnHelper } from './helper'; export const duration = columnHelper.accessor((row) => row.Duration, { header: 'Duration', id: 'duration', - cell: ({ getValue }) => getValue(), + cell: ({ getValue }) => getValue() ?? '', }); diff --git a/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/finished.tsx b/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/finished.tsx index b3bcb4e0f..cf8e5b5e3 100644 --- a/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/finished.tsx +++ b/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/finished.tsx @@ -3,5 +3,5 @@ import { columnHelper } from './helper'; export const finished = columnHelper.accessor((row) => row.FinishTime, { header: 'Finished', id: 'finished', - cell: ({ getValue }) => getValue(), + cell: ({ getValue }) => getValue() ?? '', }); diff --git a/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/started.tsx b/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/started.tsx index eff7f2465..8ea383c64 100644 --- a/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/started.tsx +++ b/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/started.tsx @@ -7,6 +7,6 @@ export const started = columnHelper.accessor( { header: 'Started', id: 'started', - cell: ({ getValue }) => getValue(), + cell: ({ getValue }) => getValue() ?? '', } ); diff --git a/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/status.tsx b/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/status.tsx index 2f6ad1768..93ecb66a2 100644 --- a/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/status.tsx +++ b/app/react/kubernetes/more-resources/JobsView/JobsDatatable/columns/status.tsx @@ -26,7 +26,7 @@ function Cell({ row: { original: item } }: CellContext) { }, ])} /> - {item.Status} + {item.Status ?? ''} {item.Status === 'Failed' && ( remainingNamespaces ); diff --git a/app/react/kubernetes/namespaces/ListView/columns/useColumns.tsx b/app/react/kubernetes/namespaces/ListView/columns/useColumns.tsx index 541c2315f..ba5eaf159 100644 --- a/app/react/kubernetes/namespaces/ListView/columns/useColumns.tsx +++ b/app/react/kubernetes/namespaces/ListView/columns/useColumns.tsx @@ -1,13 +1,17 @@ import _ from 'lodash'; import { useMemo } from 'react'; +import { AlertTriangle } from 'lucide-react'; import { isoDate } from '@/portainer/filters/filters'; import { useAuthorizations } from '@/react/hooks/useUser'; +import { pluralize } from '@/portainer/helpers/strings'; import { Link } from '@@/Link'; import { StatusBadge } from '@@/StatusBadge'; import { Badge } from '@@/Badge'; import { SystemBadge } from '@@/Badge/SystemBadge'; +import { TooltipWithChildren } from '@@/Tip/TooltipWithChildren'; +import { Icon } from '@@/Icon'; import { helper } from './helper'; import { actions } from './actions'; @@ -45,12 +49,34 @@ export function useColumns() { }), helper.accessor('Status', { header: 'Status', - cell({ getValue }) { + cell({ getValue, row: { original: item } }) { const status = getValue(); return ( - - {status.phase} - +
+ + {status.phase} + + {item.UnhealthyEventCount > 0 && ( + + + + + + {item.UnhealthyEventCount}{' '} + {pluralize(item.UnhealthyEventCount, 'warning')} + + + + + )} +
); function getColor(status?: string) { diff --git a/app/react/kubernetes/namespaces/queries/queryKeys.ts b/app/react/kubernetes/namespaces/queries/queryKeys.ts index ecfe4ea58..6c0a04676 100644 --- a/app/react/kubernetes/namespaces/queries/queryKeys.ts +++ b/app/react/kubernetes/namespaces/queries/queryKeys.ts @@ -5,7 +5,7 @@ import { EnvironmentId } from '@/react/portainer/environments/types'; export const queryKeys = { list: ( environmentId: EnvironmentId, - options?: { withResourceQuota?: boolean } + options?: { withResourceQuota?: boolean; withUnhealthyEvents?: boolean } ) => compact([ 'environments', @@ -13,6 +13,7 @@ export const queryKeys = { 'kubernetes', 'namespaces', options?.withResourceQuota, + options?.withUnhealthyEvents, ]), namespace: (environmentId: EnvironmentId, namespace: string) => [ diff --git a/app/react/kubernetes/namespaces/queries/useNamespacesQuery.ts b/app/react/kubernetes/namespaces/queries/useNamespacesQuery.ts index 6e521e0aa..e7977db84 100644 --- a/app/react/kubernetes/namespaces/queries/useNamespacesQuery.ts +++ b/app/react/kubernetes/namespaces/queries/useNamespacesQuery.ts @@ -8,20 +8,32 @@ import { PortainerNamespace } from '../types'; import { queryKeys } from './queryKeys'; -export function useNamespacesQuery( +export function useNamespacesQuery( environmentId: EnvironmentId, - options?: { autoRefreshRate?: number; withResourceQuota?: boolean } + options?: { + autoRefreshRate?: number; + withResourceQuota?: boolean; + withUnhealthyEvents?: boolean; + select?: (namespaces: PortainerNamespace[]) => T; + } ) { return useQuery( queryKeys.list(environmentId, { withResourceQuota: !!options?.withResourceQuota, + withUnhealthyEvents: !!options?.withUnhealthyEvents, }), - async () => getNamespaces(environmentId, options?.withResourceQuota), + async () => + getNamespaces( + environmentId, + options?.withResourceQuota, + options?.withUnhealthyEvents + ), { ...withGlobalError('Unable to get namespaces.'), refetchInterval() { return options?.autoRefreshRate ?? false; }, + select: options?.select, } ); } @@ -29,9 +41,13 @@ export function useNamespacesQuery( // getNamespaces is used to retrieve namespaces using the Portainer backend with caching export async function getNamespaces( environmentId: EnvironmentId, - withResourceQuota?: boolean + withResourceQuota?: boolean, + withUnhealthyEvents?: boolean ) { - const params = withResourceQuota ? { withResourceQuota } : {}; + const params = { + withResourceQuota, + withUnhealthyEvents, + }; try { const { data: namespaces } = await axios.get( `kubernetes/${environmentId}/namespaces`, diff --git a/app/react/kubernetes/namespaces/types.ts b/app/react/kubernetes/namespaces/types.ts index ba4abb744..f3626e675 100644 --- a/app/react/kubernetes/namespaces/types.ts +++ b/app/react/kubernetes/namespaces/types.ts @@ -10,6 +10,7 @@ export interface PortainerNamespace { Id: string; Name: string; Status: NamespaceStatus; + UnhealthyEventCount: number; Annotations: Record | null; CreationDate: string; NamespaceOwner: string; diff --git a/app/react/kubernetes/services/ServicesView/ServicesDatatable/ServicesDatatable.test.tsx b/app/react/kubernetes/services/ServicesView/ServicesDatatable/ServicesDatatable.test.tsx new file mode 100644 index 000000000..e8539826c --- /dev/null +++ b/app/react/kubernetes/services/ServicesView/ServicesDatatable/ServicesDatatable.test.tsx @@ -0,0 +1,145 @@ +import { render, screen, waitFor } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { HttpResponse } from 'msw'; + +import { withTestRouter } from '@/react/test-utils/withRouter'; +import { withUserProvider } from '@/react/test-utils/withUserProvider'; +import { withTestQueryProvider } from '@/react/test-utils/withTestQuery'; +import { createMockUsers } from '@/react-tools/test-mocks'; +import { server, http } from '@/setup-tests/server'; + +import { ServicesDatatable } from './ServicesDatatable'; + +vi.mock('@/react/hooks/useEnvironmentId', () => ({ + useEnvironmentId: () => 1, +})); + +vi.mock('@/portainer/services/notifications', () => ({ + notifyError: vi.fn(), + notifySuccess: vi.fn(), +})); +function createMockServices(count: number) { + return Array.from({ length: count }, (_, i) => { + let namespace = 'default'; + if (i % 3 === 0) { + namespace = 'kube-system'; + } else if (i % 2 !== 0) { + namespace = 'my-namespace'; + } + + let type = 'ClusterIP'; + if (i % 4 === 1) { + type = 'NodePort'; + } else if (i % 4 === 2) { + type = 'LoadBalancer'; + } else if (i % 4 === 3) { + type = 'ExternalName'; + } + + return { + UID: `service-${i}`, + Name: `service-${i}`, + Namespace: namespace, + Type: type, + Ports: [{ Port: 80 + i, TargetPort: 8080 + i, Protocol: 'TCP' }], + Selector: { app: `app-${i}` }, + CreationTimestamp: new Date(Date.now() - i * 1000 * 60).toISOString(), + ApplicationOwner: '', + Applications: [{ Name: `app-${i}` }], + }; + }); +} + +const mockServices = createMockServices(4); + +const mockNamespaces = [ + { + Name: 'default', + IsSystem: false, + Status: 'Active', + CreationTimestamp: '2024-01-01T00:00:00Z', + }, + { + Name: 'kube-system', + IsSystem: true, + Status: 'Active', + CreationTimestamp: '2024-01-01T00:00:00Z', + }, + { + Name: 'my-namespace', + IsSystem: false, + Status: 'Active', + CreationTimestamp: '2024-01-01T00:00:00Z', + }, +]; + +beforeEach(() => { + server.use( + http.get('/api/kubernetes/1/services', () => + HttpResponse.json(mockServices) + ), + http.get('/api/kubernetes/1/namespaces', () => + HttpResponse.json(mockNamespaces) + ) + ); +}); +const mockUser = { + // Admin user + ...createMockUsers(1, 1)[0], +}; + +function createTestComponent() { + return withTestRouter( + withUserProvider(withTestQueryProvider(ServicesDatatable), mockUser), + { + route: '/kubernetes/services', + stateConfig: [ + { + name: 'kubernetes.services', + url: '/kubernetes/services', + params: { endpointId: '1' }, + }, + ], + } + ); +} + +describe('ServicesDatatable', () => { + it('renders services data correctly', async () => { + const TestComponent = createTestComponent(); + render(); + + expect(await screen.findByText('service-1')).toBeInTheDocument(); + expect(screen.getByText('service-2')).toBeInTheDocument(); + }); + + it('should filter system resources correctly when toggled', async () => { + const TestComponent = createTestComponent(); + render(); + + const settingsButton = screen.getByRole('button', { name: /settings/i }); + await userEvent.click(settingsButton); + + await waitFor(() => { + expect(screen.queryByText('service-0')).not.toBeInTheDocument(); + }); + + const systemToggle = await screen.findByTestId('show-system-resources'); + await userEvent.click(systemToggle); + + await waitFor(() => { + expect(screen.queryByText('service-0')).toBeInTheDocument(); + }); + + expect(screen.getByText('service-3')).toBeInTheDocument(); + expect(screen.getByText('service-1')).toBeInTheDocument(); + expect(screen.getByText('service-2')).toBeInTheDocument(); + }); + + it('should show loading state when data is loading', async () => { + const TestComponent = createTestComponent(); + render(); + + expect(screen.getByText('Loading...')).toBeInTheDocument(); + }); +}); diff --git a/app/react/kubernetes/services/ServicesView/ServicesDatatable/ServicesDatatable.tsx b/app/react/kubernetes/services/ServicesView/ServicesDatatable/ServicesDatatable.tsx index 4786eaf2f..265808564 100644 --- a/app/react/kubernetes/services/ServicesView/ServicesDatatable/ServicesDatatable.tsx +++ b/app/react/kubernetes/services/ServicesView/ServicesDatatable/ServicesDatatable.tsx @@ -16,6 +16,7 @@ import { DefaultDatatableSettings } from '@/react/kubernetes/datatables/DefaultD import { SystemResourceDescription } from '@/react/kubernetes/datatables/SystemResourceDescription'; import { useNamespacesQuery } from '@/react/kubernetes/namespaces/queries/useNamespacesQuery'; import { CreateFromManifestButton } from '@/react/kubernetes/components/CreateFromManifestButton'; +import { createStore } from '@/react/kubernetes/datatables/default-kube-datatable-store'; import { Datatable, Table, TableSettingsMenu } from '@@/datatables'; import { useTableState } from '@@/datatables/useTableState'; @@ -25,7 +26,6 @@ import { useMutationDeleteServices, useClusterServices } from '../../service'; import { Service } from '../../types'; import { columns } from './columns'; -import { createStore } from './datatable-store'; import { ServiceRowData } from './types'; const storageKey = 'k8sServicesDatatable'; @@ -34,52 +34,53 @@ const settingsStore = createStore(storageKey); export function ServicesDatatable() { const tableState = useTableState(settingsStore, storageKey); const environmentId = useEnvironmentId(); - const { data: namespacesArray, ...namespacesQuery } = - useNamespacesQuery(environmentId); + const { data: namespaces, ...namespacesQuery } = useNamespacesQuery( + environmentId, + { + select: (namespacesArray) => + namespacesArray?.reduce>( + (acc, namespace) => { + acc[namespace.Name] = namespace; + return acc; + }, + {} + ), + } + ); + const { authorized: canWrite } = useAuthorizations(['K8sServiceW']); + const { authorized: canAccessSystemResources } = useAuthorizations( + 'K8sAccessSystemNamespaces' + ); const { data: services, ...servicesQuery } = useClusterServices( environmentId, { autoRefreshRate: tableState.autoRefreshRate * 1000, withApplications: true, + select: (services) => + services?.filter( + (service) => + (canAccessSystemResources && tableState.showSystemResources) || + !namespaces?.[service.Namespace]?.IsSystem + ), } ); - const namespaces: Record = {}; - if (Array.isArray(namespacesArray)) { - for (let i = 0; i < namespacesArray.length; i++) { - const namespace = namespacesArray[i]; - namespaces[namespace.Name] = namespace; - } - } - - const { authorized: canWrite } = useAuthorizations(['K8sServiceW']); - const readOnly = !canWrite; - const { authorized: canAccessSystemResources } = useAuthorizations( - 'K8sAccessSystemNamespaces' - ); - const filteredServices = services?.filter( - (service) => - (canAccessSystemResources && tableState.showSystemResources) || - !namespaces?.[service.Namespace]?.IsSystem - ); - - const servicesWithIsSystem = useServicesRowData( - filteredServices || [], - namespaces - ); + const servicesWithIsSystem = useServicesRowData(services || [], namespaces); return ( row.UID} isRowSelectable={(row) => !namespaces?.[row.original.Namespace]?.IsSystem} - disableSelect={readOnly} + disableSelect={!canWrite} renderTableActions={(selectedRows) => ( )} diff --git a/app/react/kubernetes/services/service.ts b/app/react/kubernetes/services/service.ts index a36f25db9..a7cdbc7f2 100644 --- a/app/react/kubernetes/services/service.ts +++ b/app/react/kubernetes/services/service.ts @@ -23,18 +23,21 @@ export const queryKeys = { * * @returns The result of the query. */ -export function useClusterServices( +export function useClusterServices( environmentId: EnvironmentId, - options?: { autoRefreshRate?: number; withApplications?: boolean } + options?: { + autoRefreshRate?: number; + withApplications?: boolean; + select?: (services: Service[]) => T; + } ) { return useQuery( queryKeys.clusterServices(environmentId), async () => getClusterServices(environmentId, options?.withApplications), { ...withGlobalError('Unable to get services.'), - refetchInterval() { - return options?.autoRefreshRate ?? false; - }, + refetchInterval: options?.autoRefreshRate, + select: options?.select, } ); } diff --git a/app/react/kubernetes/volumes/ListView/VolumesDatatable.tsx b/app/react/kubernetes/volumes/ListView/VolumesDatatable.tsx index b81f60e9a..032fe63aa 100644 --- a/app/react/kubernetes/volumes/ListView/VolumesDatatable.tsx +++ b/app/react/kubernetes/volumes/ListView/VolumesDatatable.tsx @@ -16,12 +16,17 @@ import { } from '../../datatables/DefaultDatatableSettings'; import { SystemResourceDescription } from '../../datatables/SystemResourceDescription'; import { useNamespacesQuery } from '../../namespaces/queries/useNamespacesQuery'; -import { useAllVolumesQuery } from '../queries/useVolumesQuery'; +import { + convertToVolumeViewModels, + useAllVolumesQuery, +} from '../queries/useVolumesQuery'; import { isSystemNamespace } from '../../namespaces/queries/useIsSystemNamespace'; import { useDeleteVolumes } from '../queries/useDeleteVolumes'; import { isVolumeUsed } from '../utils'; +import { K8sVolumeInfo } from '../types'; import { columns } from './columns'; +import { VolumeViewModel } from './types'; export function VolumesDatatable() { const tableState = useTableStateWithStorage( @@ -45,21 +50,15 @@ export function VolumesDatatable() { const namespaces = namespaceListQuery.data ?? []; const volumesQuery = useAllVolumesQuery(envId, { refetchInterval: tableState.autoRefreshRate * 1000, + select: transformAndFilterVolumes, }); const volumes = volumesQuery.data ?? []; - const filteredVolumes = tableState.showSystemResources - ? volumes - : volumes.filter( - (volume) => - !isSystemNamespace(volume.ResourcePool.Namespace.Name, namespaces) - ); - return ( ); + + function transformAndFilterVolumes( + volumes: K8sVolumeInfo[] + ): VolumeViewModel[] { + const transformedVolumes = convertToVolumeViewModels(volumes); + return transformedVolumes.filter( + (volume) => + tableState.showSystemResources || + !isSystemNamespace(volume.ResourcePool.Namespace.Name, namespaces) + ); + } } diff --git a/app/react/kubernetes/volumes/queries/useDeleteVolumes.ts b/app/react/kubernetes/volumes/queries/useDeleteVolumes.ts index 78d5a295d..7732c2b58 100644 --- a/app/react/kubernetes/volumes/queries/useDeleteVolumes.ts +++ b/app/react/kubernetes/volumes/queries/useDeleteVolumes.ts @@ -36,7 +36,7 @@ export function useDeleteVolumes(environmentId: EnvironmentId) { ); } queryClient.invalidateQueries(queryKeys.storages(environmentId)); - queryClient.invalidateQueries(queryKeys.volumes(environmentId)); + return queryClient.invalidateQueries(queryKeys.volumes(environmentId)); }, ...withGlobalError('Unable to remove volumes'), }); diff --git a/app/react/kubernetes/volumes/queries/useNamespaceVolumes.ts b/app/react/kubernetes/volumes/queries/useNamespaceVolumes.ts deleted file mode 100644 index cdf6b6ee0..000000000 --- a/app/react/kubernetes/volumes/queries/useNamespaceVolumes.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { useQuery } from '@tanstack/react-query'; - -import { EnvironmentId } from '@/react/portainer/environments/types'; -import { withGlobalError } from '@/react-tools/react-query'; -import axios, { parseAxiosError } from '@/portainer/services/axios'; - -import { K8sVolumeInfo } from '../types'; - -import { queryKeys } from './query-keys'; -import { convertToVolumeViewModels } from './useVolumesQuery'; - -// useQuery to get a list of all volumes in a cluster -export function useNamespaceVolumes( - environmentId: EnvironmentId, - namespace: string, - queryOptions?: { - refetchInterval?: number; - withApplications?: boolean; - } -) { - return useQuery( - queryKeys.volumes(environmentId), - () => - getNamespaceVolumes(environmentId, namespace, { - withApplications: queryOptions?.withApplications ?? false, - }), - { - enabled: !!namespace, - refetchInterval: queryOptions?.refetchInterval, - select: convertToVolumeViewModels, - ...withGlobalError('Unable to retrieve volumes'), - } - ); -} - -// get all volumes in a cluster -async function getNamespaceVolumes( - environmentId: EnvironmentId, - namespace: string, - params?: { withApplications: boolean } -) { - try { - const { data } = await axios.get( - `/kubernetes/${environmentId}/namespaces/${namespace}/volumes`, - { params } - ); - return data; - } catch (e) { - throw parseAxiosError(e, 'Unable to retrieve volumes'); - } -} diff --git a/app/react/kubernetes/volumes/queries/useVolumesQuery.ts b/app/react/kubernetes/volumes/queries/useVolumesQuery.ts index 94b30345b..646065758 100644 --- a/app/react/kubernetes/volumes/queries/useVolumesQuery.ts +++ b/app/react/kubernetes/volumes/queries/useVolumesQuery.ts @@ -14,10 +14,11 @@ import { appOwnerLabel } from '../../applications/constants'; import { queryKeys } from './query-keys'; // useQuery to get a list of all volumes in a cluster -export function useAllVolumesQuery( +export function useAllVolumesQuery( environmentId: EnvironmentId, queryOptions?: { refetchInterval?: number; + select?: (volumes: K8sVolumeInfo[]) => T[]; } ) { return useQuery( @@ -25,7 +26,7 @@ export function useAllVolumesQuery( () => getAllVolumes(environmentId, { withApplications: true }), { refetchInterval: queryOptions?.refetchInterval, - select: convertToVolumeViewModels, + select: queryOptions?.select, ...withGlobalError('Unable to retrieve volumes'), } ); diff --git a/app/react/portainer/HomeView/EnvironmentList/KubeconfigButton/KubeconfigButton.tsx b/app/react/portainer/HomeView/EnvironmentList/KubeconfigButton/KubeconfigButton.tsx index f2b20857d..393c55f9c 100644 --- a/app/react/portainer/HomeView/EnvironmentList/KubeconfigButton/KubeconfigButton.tsx +++ b/app/react/portainer/HomeView/EnvironmentList/KubeconfigButton/KubeconfigButton.tsx @@ -7,6 +7,7 @@ import { trackEvent } from '@/angulartics.matomo/analytics-services'; import { Query } from '@/react/portainer/environments/queries/useEnvironmentList'; import { Button } from '@@/buttons'; +import { TooltipWithChildren } from '@@/Tip/TooltipWithChildren'; import { KubeconfigPrompt } from './KubeconfigPrompt'; @@ -23,23 +24,41 @@ export function KubeconfigButton({ environments, envQueryParams }: Props) { isKubernetesEnvironment(env.Type) ); - if (!isKubeconfigButtonVisible()) { - return null; + const isHttp = window.location.protocol === 'http:'; + const noKubeEnvs = kubeEnvs.length === 0; + const isDisabled = noKubeEnvs || isHttp; + + let tooltipMessage = ''; + if (isHttp) { + tooltipMessage = + 'Kubeconfig download is not available when Portainer is accessed via HTTP. Please use HTTPS'; + } else if (noKubeEnvs) { + tooltipMessage = 'No Kubernetes environments detected'; } + const button = ( + + ); + return ( <> - + {isDisabled ? ( + + {button} + + ) : ( + button + )} {prompt()} ); @@ -60,10 +79,6 @@ export function KubeconfigButton({ environments, envQueryParams }: Props) { setIsOpen(false); } - function isKubeconfigButtonVisible() { - return window.location.protocol === 'https:'; - } - function prompt() { return ( isOpen && ( diff --git a/app/react/portainer/access-control/AccessControlPanel/AccessControlPanelDetails.tsx b/app/react/portainer/access-control/AccessControlPanel/AccessControlPanelDetails.tsx index 725e115cd..f53015cea 100644 --- a/app/react/portainer/access-control/AccessControlPanel/AccessControlPanelDetails.tsx +++ b/app/react/portainer/access-control/AccessControlPanel/AccessControlPanelDetails.tsx @@ -201,8 +201,10 @@ function InheritanceMessage({ return ( - - {children} +
+ + {children} +
diff --git a/app/react/portainer/account/AccountView/HelmRepositoryDatatable/HelmRepositoryDatatable.tsx b/app/react/portainer/account/AccountView/HelmRepositoryDatatable/HelmRepositoryDatatable.tsx index 7ed2f4651..bad68c293 100644 --- a/app/react/portainer/account/AccountView/HelmRepositoryDatatable/HelmRepositoryDatatable.tsx +++ b/app/react/portainer/account/AccountView/HelmRepositoryDatatable/HelmRepositoryDatatable.tsx @@ -81,21 +81,25 @@ export function HelmRepositoryDatatable() { function HelmDatatableDescription({ isAdmin }: { isAdmin: boolean }) { return ( - Adding a Helm repo here only makes it available in your own user - account's Portainer UI. Helm charts are pulled down from these repos - (plus the{' '} - {isAdmin ? ( - - globally-set Helm repo - - ) : ( - globally-set Helm repo +

+ Adding a Helm repository here makes it available only in your Portainer + user account. The Helm charts from these repositories (along with the + globally set Helm repository) are shown in the 'Create from + Code' screen. +

+ {isAdmin && ( + <> + To manage your helm repositories globally, navigate to{' '} + + Settings > General + + . + )} - ) and shown in the Create from code screen's Helm charts list.
); } diff --git a/app/react/portainer/account/AccountView/theme-options.tsx b/app/react/portainer/account/AccountView/theme-options.tsx index c76ddaa2f..21c1d00d9 100644 --- a/app/react/portainer/account/AccountView/theme-options.tsx +++ b/app/react/portainer/account/AccountView/theme-options.tsx @@ -7,28 +7,24 @@ export const options = [ id: 'light', icon: , label: 'Light Theme', - description: 'Default color mode', value: 'light', }, { id: 'dark', icon: , label: 'Dark Theme', - description: 'Dark color mode', value: 'dark', }, { id: 'highcontrast', icon: , label: 'High Contrast', - description: 'High contrast color mode', value: 'highcontrast', }, { id: 'auto', icon: , - label: 'Auto', - description: 'Sync with system theme', + label: 'System Theme', value: 'auto', }, ]; diff --git a/app/react/portainer/environments/queries/query-keys.ts b/app/react/portainer/environments/queries/query-keys.ts index ed0600c19..5cb21a348 100644 --- a/app/react/portainer/environments/queries/query-keys.ts +++ b/app/react/portainer/environments/queries/query-keys.ts @@ -3,6 +3,11 @@ import { EnvironmentId } from '../types'; export const environmentQueryKeys = { base: () => ['environments'] as const, item: (id: EnvironmentId) => [...environmentQueryKeys.base(), id] as const, - registries: (environmentId: EnvironmentId) => - [...environmentQueryKeys.base(), environmentId, 'registries'] as const, + registries: (environmentId: EnvironmentId, namespace?: string) => + [ + ...environmentQueryKeys.base(), + environmentId, + 'registries', + namespace, + ] as const, }; diff --git a/app/react/portainer/environments/queries/useEnvironmentRegistries.ts b/app/react/portainer/environments/queries/useEnvironmentRegistries.ts index 19588b5ca..c25ffa157 100644 --- a/app/react/portainer/environments/queries/useEnvironmentRegistries.ts +++ b/app/react/portainer/environments/queries/useEnvironmentRegistries.ts @@ -14,17 +14,28 @@ export function useEnvironmentRegistries>( environmentId: EnvironmentId, queryOptions: GenericRegistriesQueryOptions = {} ) { + const { namespace } = queryOptions; return useGenericRegistriesQuery( - environmentQueryKeys.registries(environmentId), - () => getEnvironmentRegistries(environmentId), + environmentQueryKeys.registries(environmentId, namespace), + () => getEnvironmentRegistries(environmentId, { namespace }), queryOptions ); } -async function getEnvironmentRegistries(environmentId: EnvironmentId) { +type Params = { + namespace?: string; +}; + +async function getEnvironmentRegistries( + environmentId: EnvironmentId, + params: Params +) { try { const { data } = await axios.get>( - buildUrl(environmentId, 'registries') + buildUrl(environmentId, 'registries'), + { + params, + } ); return data; } catch (err) { diff --git a/app/react/portainer/registries/CreateView/options.tsx b/app/react/portainer/registries/CreateView/options.tsx index b6802e60b..984eaa892 100644 --- a/app/react/portainer/registries/CreateView/options.tsx +++ b/app/react/portainer/registries/CreateView/options.tsx @@ -1,62 +1,56 @@ -import { Edit } from 'lucide-react'; - -import Docker from '@/assets/ico/vendor/docker.svg?c'; -import Ecr from '@/assets/ico/vendor/ecr.svg?c'; -import Quay from '@/assets/ico/vendor/quay.svg?c'; -import Proget from '@/assets/ico/vendor/proget.svg?c'; -import Azure from '@/assets/ico/vendor/azure.svg?c'; -import Gitlab from '@/assets/ico/vendor/gitlab.svg?c'; - import { BadgeIcon } from '@@/BadgeIcon'; +import { RegistryTypes } from '../types/registry'; +import { registryIconMap, registryLabelMap } from '../utils/constants'; + export const options = [ { id: 'registry_dockerhub', - icon: Docker, - label: 'DockerHub', + icon: registryIconMap[RegistryTypes.DOCKERHUB], + label: registryLabelMap[RegistryTypes.DOCKERHUB], description: 'DockerHub authenticated account', - value: '6', + value: String(RegistryTypes.DOCKERHUB), }, { id: 'registry_aws_ecr', - icon: Ecr, - label: 'AWS ECR', + icon: registryIconMap[RegistryTypes.ECR], + label: registryLabelMap[RegistryTypes.ECR], description: 'Amazon elastic container registry', - value: '7', + value: String(RegistryTypes.ECR), }, { id: 'registry_quay', - icon: Quay, - label: 'Quay.io', + icon: registryIconMap[RegistryTypes.QUAY], + label: registryLabelMap[RegistryTypes.QUAY], description: 'Quay container registry', - value: '1', + value: String(RegistryTypes.QUAY), }, { id: 'registry_proget', - icon: Proget, - label: 'ProGet', + icon: registryIconMap[RegistryTypes.PROGET], + label: registryLabelMap[RegistryTypes.PROGET], description: 'ProGet container registry', - value: '5', + value: String(RegistryTypes.PROGET), }, { id: 'registry_azure', - icon: Azure, - label: 'Azure', + icon: registryIconMap[RegistryTypes.AZURE], + label: registryLabelMap[RegistryTypes.AZURE], description: 'Azure container registry', - value: '2', + value: String(RegistryTypes.AZURE), }, { id: 'registry_gitlab', - icon: Gitlab, - label: 'GitLab', + icon: registryIconMap[RegistryTypes.GITLAB], + label: registryLabelMap[RegistryTypes.GITLAB], description: 'GitLab container registry', - value: '4', + value: String(RegistryTypes.GITLAB), }, { id: 'registry_custom', - icon: , - label: 'Custom registry', + icon: , + label: registryLabelMap[RegistryTypes.CUSTOM], description: 'Define your own registry', - value: '3', + value: String(RegistryTypes.CUSTOM), }, ]; diff --git a/app/react/portainer/registries/queries/build-url.ts b/app/react/portainer/registries/queries/build-url.ts index 3f76215bd..f490ec406 100644 --- a/app/react/portainer/registries/queries/build-url.ts +++ b/app/react/portainer/registries/queries/build-url.ts @@ -1,13 +1,17 @@ import { RegistryId } from '../types/registry'; -export function buildUrl(registryId: RegistryId) { - const base = '/registries'; +export function buildUrl(registryId: RegistryId, resource?: 'repositories') { + let url = '/registries'; if (registryId) { - return `${base}/${registryId}`; + url += `/${registryId}`; } - return base; + if (resource) { + url += `/${resource}`; + } + + return url; } export function buildProxyUrl(registryId: RegistryId) { diff --git a/app/react/portainer/registries/queries/useRegistries.ts b/app/react/portainer/registries/queries/useRegistries.ts index 007ab70cb..4ea5aad6e 100644 --- a/app/react/portainer/registries/queries/useRegistries.ts +++ b/app/react/portainer/registries/queries/useRegistries.ts @@ -24,6 +24,8 @@ export type GenericRegistriesQueryOptions = { onSuccess?: (data: T) => void; /** is used to hide the default registry from the list of registries, regardless of the user's settings. Kubernetes views use this. */ hideDefault?: boolean; + /** is used to filter the registries by namespace. Kubernetes views use this. */ + namespace?: string; }; export function useGenericRegistriesQuery( diff --git a/app/react/portainer/registries/repositories/ItemView/TagsDatatable/TagsDatatable.test.tsx b/app/react/portainer/registries/repositories/ItemView/TagsDatatable/TagsDatatable.test.tsx new file mode 100644 index 000000000..6ec8599e1 --- /dev/null +++ b/app/react/portainer/registries/repositories/ItemView/TagsDatatable/TagsDatatable.test.tsx @@ -0,0 +1,169 @@ +import { render, screen } from '@testing-library/react'; +import { vi } from 'vitest'; + +import { withTestQueryProvider } from '@/react/test-utils/withTestQuery'; +import { withTestRouter } from '@/react/test-utils/withRouter'; + +import { TagsDatatable } from './TagsDatatable'; +import { Tag } from './types'; +import { RepositoryTagViewModel } from './view-model'; + +// Mock the necessary hooks +const mockUseCurrentStateAndParams = vi.fn(); + +vi.mock('@uirouter/react', async (importOriginal: () => Promise) => ({ + ...(await importOriginal()), + useCurrentStateAndParams: () => mockUseCurrentStateAndParams(), +})); + +// Mock the Link component to capture route parameters and generate proper hrefs +vi.mock('@@/Link', () => ({ + Link: ({ + children, + params, + 'data-cy': dataCy, + title, + }: { + children: React.ReactNode; + params?: Record; + 'data-cy'?: string; + title?: string; + }) => { + // Simulate href generation based on route and params + // For 'portainer.registries.registry.repository.tag' route + const baseParams = { + endpointId: '1', + id: '1', + repository: 'test-repo', + ...params, + }; + + const tag = (baseParams as Record).tag || ''; + const href = `/endpoints/${baseParams.endpointId}/registries/${baseParams.id}/repositories/${baseParams.repository}/tags/${tag}`; + + return ( + + {children} + + ); + }, +})); + +vi.mock('../../queries/useTagDetails', () => ({ + useTagDetails: vi.fn( + ( + params, + { select }: { select?: (data: RepositoryTagViewModel) => string } = {} + ) => { + const data: RepositoryTagViewModel = { + Name: params.tag, + Os: 'linux', + Architecture: 'amd64', + ImageId: `sha256:${params.tag}123`, + Size: 1024, + ImageDigest: '', + ManifestV2: { + digest: `sha256:${params.tag}123`, + schemaVersion: 2, + mediaType: 'application/vnd.docker.distribution.manifest.v2+json', + config: { + digest: `sha256:${params.tag}123`, + mediaType: 'application/vnd.docker.container.image.v1+json', + size: 1024, + }, + layers: [], + }, + History: [], + }; + + return { + data: select?.(data) || data, + isLoading: false, + error: null, + }; + } + ), +})); + +// Create mock data +const mockTags: Tag[] = [ + { Name: 'latest' }, + { Name: 'v1.0.0' }, + { Name: 'dev-branch' }, + { Name: 'feature/new-ui' }, +]; + +const defaultProps = { + dataset: mockTags, + advancedFeaturesAvailable: true, + onRemove: vi.fn(), + onRetag: vi.fn().mockResolvedValue(undefined), +}; + +function renderComponent() { + const Wrapped = withTestQueryProvider( + withTestRouter(() => ) + ); + return render(); +} + +describe('TagsDatatable', () => { + beforeEach(() => { + // Set up default mock values + mockUseCurrentStateAndParams.mockReturnValue({ + params: { + endpointId: '1', + id: '1', + repository: 'test-repo', + }, + }); + }); + + it('renders basic table structure', () => { + renderComponent(); + expect(screen.getByText('Tags')).toBeInTheDocument(); + expect(screen.getByPlaceholderText('Search...')).toBeInTheDocument(); + }); + + it('renders tag data in table', () => { + renderComponent(); + + // Check that our mock tags are rendered somewhere in the table + expect(screen.getByText('latest')).toBeInTheDocument(); + expect(screen.getByText('v1.0.0')).toBeInTheDocument(); + expect(screen.getByText('dev-branch')).toBeInTheDocument(); + expect(screen.getByText('feature/new-ui')).toBeInTheDocument(); + }); + + it('creates correct hrefs for tag name links', () => { + renderComponent(); + + // Get the links by their data-cy attributes + const latestLink = screen.getByTestId( + 'registry-tag-name_latest' + ) as HTMLAnchorElement; + const v100Link = screen.getByTestId( + 'registry-tag-name_v1.0.0' + ) as HTMLAnchorElement; + const devBranchLink = screen.getByTestId( + 'registry-tag-name_dev-branch' + ) as HTMLAnchorElement; + const featureLink = screen.getByTestId( + 'registry-tag-name_feature/new-ui' + ) as HTMLAnchorElement; + + // Verify the exact path portion of the href + expect(new URL(latestLink.href).pathname).toBe( + '/endpoints/1/registries/1/repositories/test-repo/tags/latest' + ); + expect(new URL(v100Link.href).pathname).toBe( + '/endpoints/1/registries/1/repositories/test-repo/tags/v1.0.0' + ); + expect(new URL(devBranchLink.href).pathname).toBe( + '/endpoints/1/registries/1/repositories/test-repo/tags/dev-branch' + ); + expect(new URL(featureLink.href).pathname).toBe( + '/endpoints/1/registries/1/repositories/test-repo/tags/feature/new-ui' + ); + }); +}); diff --git a/app/react/portainer/registries/repositories/ItemView/TagsDatatable/columns/useColumns.ts b/app/react/portainer/registries/repositories/ItemView/TagsDatatable/columns/useColumns.ts index 4f07b5bec..bcedccb47 100644 --- a/app/react/portainer/registries/repositories/ItemView/TagsDatatable/columns/useColumns.ts +++ b/app/react/portainer/registries/repositories/ItemView/TagsDatatable/columns/useColumns.ts @@ -4,7 +4,7 @@ import _ from 'lodash'; import { humanize } from '@/portainer/filters/filters'; import { trimSHA } from '@/docker/filters/utils'; -import { buildNameColumn } from '@@/datatables/buildNameColumn'; +import { buildNameColumnFromObject } from '@@/datatables/buildNameColumn'; import { Tag } from '../types'; @@ -13,13 +13,13 @@ import { buildCell } from './buildCell'; import { actions } from './actions'; const columns = [ - buildNameColumn( - 'Name', - 'portainer.registries.registry.repository.tag', - 'tag', - 'registry-tag-name', - (item) => item.Name - ), + buildNameColumnFromObject({ + nameKey: 'Name', + path: 'portainer.registries.registry.repository.tag', + dataCy: 'registry-tag-name', + idParam: 'tag', + idGetter: (item) => item.Name, + }), helper.display({ header: 'OS/Architecture', cell: buildCell((model) => `${model.Os}/${model.Architecture}`), diff --git a/app/react/portainer/registries/utils/constants.tsx b/app/react/portainer/registries/utils/constants.tsx new file mode 100644 index 000000000..e20ee285f --- /dev/null +++ b/app/react/portainer/registries/utils/constants.tsx @@ -0,0 +1,35 @@ +import { Edit } from 'lucide-react'; + +import Docker from '@/assets/ico/vendor/docker.svg?c'; +import Ecr from '@/assets/ico/vendor/ecr.svg?c'; +import Quay from '@/assets/ico/vendor/quay.svg?c'; +import Proget from '@/assets/ico/vendor/proget.svg?c'; +import Azure from '@/assets/ico/vendor/azure.svg?c'; +import Gitlab from '@/assets/ico/vendor/gitlab.svg?c'; + +import { RegistryTypes } from '../types/registry'; + +export const registryLabelMap: Record = { + [RegistryTypes.ANONYMOUS]: 'Anonymous', + [RegistryTypes.DOCKERHUB]: 'DockerHub', + [RegistryTypes.ECR]: 'AWS ECR', + [RegistryTypes.QUAY]: 'Quay.io', + [RegistryTypes.PROGET]: 'ProGet', + [RegistryTypes.AZURE]: 'Azure', + [RegistryTypes.GITLAB]: 'GitLab', + [RegistryTypes.CUSTOM]: 'Custom registry', + [RegistryTypes.GITHUB]: 'GitHub', +}; + +export const registryIconMap = { + [RegistryTypes.DOCKERHUB]: Docker, + [RegistryTypes.ECR]: Ecr, + [RegistryTypes.QUAY]: Quay, + [RegistryTypes.PROGET]: Proget, + [RegistryTypes.AZURE]: Azure, + [RegistryTypes.GITLAB]: Gitlab, + [RegistryTypes.CUSTOM]: Edit, + // github and anonymous don't have an icon + [RegistryTypes.GITHUB]: null, + [RegistryTypes.ANONYMOUS]: null, +}; diff --git a/app/react/portainer/settings/SettingsView/KubeSettingsPanel/HelmSection.tsx b/app/react/portainer/settings/SettingsView/KubeSettingsPanel/HelmSection.tsx index e5d519174..0c21140aa 100644 --- a/app/react/portainer/settings/SettingsView/KubeSettingsPanel/HelmSection.tsx +++ b/app/react/portainer/settings/SettingsView/KubeSettingsPanel/HelmSection.tsx @@ -4,7 +4,7 @@ import { TextTip } from '@@/Tip/TextTip'; import { FormControl } from '@@/form-components/FormControl'; import { FormSection } from '@@/form-components/FormSection'; import { Input } from '@@/form-components/Input'; -import { InsightsBox } from '@@/InsightsBox'; +import { ExternalLink } from '@@/ExternalLink'; export function HelmSection() { const [{ name }, { error }] = useField('helmRepositoryUrl'); @@ -13,39 +13,17 @@ export function HelmSection() {
- You can specify the URL to your own Helm repository here. See the{' '} - - official documentation - {' '} - for more details. + Helm repository + {' '} + here.
- - At present Portainer does not support OCI format Helm charts. - Support for OCI charts will be available in a future release. If you - would like to provide feedback on OCI support or get access to early - releases to test this functionality,{' '} - - please get in touch - - . - - } - className="block w-fit mt-2 mb-1" - /> - { const inputElement = screen.getByDisplayValue(value.VAR1); await user.clear(inputElement); - expect(onChange).toHaveBeenCalledWith({ VAR1: '' }); + expect(onChange).toHaveBeenCalledWith({ VAR1: undefined }); const newValue = 'New Value'; await user.type(inputElement, newValue); @@ -107,11 +107,14 @@ test('validates env vars fieldset', () => { ]); const validData = { VAR1: 'Value 1', VAR2: 'Value 2' }; - const invalidData = { VAR1: '', VAR2: 'Value 2' }; + const emptyData = { VAR1: '', VAR2: 'Value 2' }; + const undefinedData = { VAR1: undefined, VAR2: 'Value 2' }; const validResult = schema.isValidSync(validData); - const invalidResult = schema.isValidSync(invalidData); + const emptyResult = schema.isValidSync(emptyData); + const undefinedResult = schema.isValidSync(undefinedData); expect(validResult).toBe(true); - expect(invalidResult).toBe(false); + expect(emptyResult).toBe(true); + expect(undefinedResult).toBe(true); }); diff --git a/app/react/portainer/templates/app-templates/DeployFormWidget/EnvVarsFieldset.tsx b/app/react/portainer/templates/app-templates/DeployFormWidget/EnvVarsFieldset.tsx index dad33af07..a1281772f 100644 --- a/app/react/portainer/templates/app-templates/DeployFormWidget/EnvVarsFieldset.tsx +++ b/app/react/portainer/templates/app-templates/DeployFormWidget/EnvVarsFieldset.tsx @@ -6,7 +6,7 @@ import { TemplateEnv } from '@/react/portainer/templates/app-templates/types'; import { FormControl } from '@@/form-components/FormControl'; import { Input, Select } from '@@/form-components/Input'; -type Value = Record; +type Value = Record; export { type Value as EnvVarsValue }; @@ -27,7 +27,7 @@ export function EnvVarsFieldset({ handleChange(env.name, value)} errors={errors?.[env.name]} /> @@ -36,7 +36,7 @@ export function EnvVarsFieldset({ ); function handleChange(name: string, envValue: string) { - onChange({ ...values, [name]: envValue }); + onChange({ ...values, [name]: envValue || undefined }); } } @@ -55,7 +55,7 @@ function Item({ return ( @@ -101,7 +101,9 @@ export function envVarsFieldsetValidation( ): SchemaOf { return object( Object.fromEntries( - definitions.map((v) => [v.name, string().required('Required')]) + definitions + .filter((v) => !v.preset) + .map((v) => [v.name, string().optional()]) ) ); } diff --git a/app/react/portainer/templates/app-templates/DeployFormWidget/StackDeployForm/types.ts b/app/react/portainer/templates/app-templates/DeployFormWidget/StackDeployForm/types.ts index d94a2412e..f40d79c56 100644 --- a/app/react/portainer/templates/app-templates/DeployFormWidget/StackDeployForm/types.ts +++ b/app/react/portainer/templates/app-templates/DeployFormWidget/StackDeployForm/types.ts @@ -2,6 +2,6 @@ import { AccessControlFormData } from '@/react/portainer/access-control/types'; export interface FormValues { name: string; - envVars: Record; + envVars: Record; accessControl: AccessControlFormData; } diff --git a/app/react/portainer/templates/app-templates/view-model.ts b/app/react/portainer/templates/app-templates/view-model.ts index 94ea24bc3..3619caa20 100644 --- a/app/react/portainer/templates/app-templates/view-model.ts +++ b/app/react/portainer/templates/app-templates/view-model.ts @@ -88,10 +88,8 @@ function setTemplatesV3(this: TemplateViewModel, template: AppTemplate) { this.Id = template.id; } -let templateV2ID = 0; - function setTemplatesV2(this: TemplateViewModel, template: AppTemplate) { - this.Id = templateV2ID++; + this.Id = template.id; this.Title = template.title; this.Type = template.type; this.Description = template.description; diff --git a/app/react/portainer/users/teams/ItemView/TeamAssociationSelector/TeamAssociationSelector.stories.tsx b/app/react/portainer/users/teams/ItemView/TeamAssociationSelector/TeamAssociationSelector.stories.tsx index d4d4142f7..9c7fa8a32 100644 --- a/app/react/portainer/users/teams/ItemView/TeamAssociationSelector/TeamAssociationSelector.stories.tsx +++ b/app/react/portainer/users/teams/ItemView/TeamAssociationSelector/TeamAssociationSelector.stories.tsx @@ -2,7 +2,7 @@ import { Meta } from '@storybook/react'; import { useMemo, useState } from 'react'; import { createMockUsers } from '@/react-tools/test-mocks'; -import { Role, User } from '@/portainer/users/types'; +import { Role } from '@/portainer/users/types'; import { UserViewModel } from '@/portainer/models/user'; import { UserContext } from '@/react/hooks/useUser'; @@ -28,7 +28,7 @@ function Example({ userRole }: Args) { () => ({ user: new UserViewModel({ Role: userRole }) }), [userRole] ); - const [users] = useState(createMockUsers(20) as User[]); + const [users] = useState(createMockUsers(20, Role.Standard)); const [memberships] = useState[]>( users diff --git a/app/react/portainer/users/teams/ItemView/TeamAssociationSelector/TeamMembersList/TeamMembersList.stories.tsx b/app/react/portainer/users/teams/ItemView/TeamAssociationSelector/TeamMembersList/TeamMembersList.stories.tsx index 6f2e53f1e..efe4cc908 100644 --- a/app/react/portainer/users/teams/ItemView/TeamAssociationSelector/TeamMembersList/TeamMembersList.stories.tsx +++ b/app/react/portainer/users/teams/ItemView/TeamAssociationSelector/TeamMembersList/TeamMembersList.stories.tsx @@ -28,7 +28,7 @@ function Example({ userRole }: Args) { [userRole] ); - const [users] = useState(createMockUsers(20)); + const [users] = useState(createMockUsers(20, Role.Standard)); const [roles] = useState( Object.fromEntries( users.map((user) => [ diff --git a/app/react/portainer/users/teams/ItemView/TeamAssociationSelector/UsersList/UsersList.stories.tsx b/app/react/portainer/users/teams/ItemView/TeamAssociationSelector/UsersList/UsersList.stories.tsx index bbbcc6498..8966d6fad 100644 --- a/app/react/portainer/users/teams/ItemView/TeamAssociationSelector/UsersList/UsersList.stories.tsx +++ b/app/react/portainer/users/teams/ItemView/TeamAssociationSelector/UsersList/UsersList.stories.tsx @@ -1,6 +1,10 @@ import { Meta } from '@storybook/react'; +import { useMemo } from 'react'; import { createMockUsers } from '@/react-tools/test-mocks'; +import { Role } from '@/portainer/users/types'; +import { UserContext } from '@/react/hooks/useUser'; +import { UserViewModel } from '@/portainer/models/user'; import { UsersList } from './UsersList'; @@ -13,10 +17,25 @@ export default meta; export { Example }; -function Example() { - const users = createMockUsers(20); - - return ; +interface Args { + userRole: Role; } -Example.args = {}; +function Example({ userRole }: Args) { + const userProviderState = useMemo( + () => ({ user: new UserViewModel({ Role: userRole }) }), + [userRole] + ); + + const users = createMockUsers(20, Role.Standard); + + return ( + + + + ); +} + +Example.args = { + userRole: Role.Admin, +}; diff --git a/app/react/sidebar/EnvironmentSidebar.module.css b/app/react/sidebar/EnvironmentSidebar.module.css index 56165048c..2400d635a 100644 --- a/app/react/sidebar/EnvironmentSidebar.module.css +++ b/app/react/sidebar/EnvironmentSidebar.module.css @@ -1,12 +1,11 @@ .root { background-color: var(--bg-sidebar-nav-color); - border-color: var(--border-sidebar-color); } .closeBtn { - background-color: var(--bg-btn-default-color); + background-color: transparent; } .closeBtn:hover { - background-color: var(--bg-btn-default-hover-color); + background-color: var(--graphite-500); } diff --git a/app/react/sidebar/EnvironmentSidebar.tsx b/app/react/sidebar/EnvironmentSidebar.tsx index 2e51b449c..bea5f86c1 100644 --- a/app/react/sidebar/EnvironmentSidebar.tsx +++ b/app/react/sidebar/EnvironmentSidebar.tsx @@ -36,7 +36,7 @@ export function EnvironmentSidebar() { } return ( -
+
{environment ? ( ) : ( @@ -151,7 +151,7 @@ function Title({ environment, onClear }: TitleProps) { onClick={onClear} className={clsx( styles.closeBtn, - 'ml-auto mr-2 flex h-5 w-5 items-center justify-center rounded border-0 p-1 text-sm text-gray-5 transition-colors duration-200 hover:text-white be:text-gray-6 be:hover:text-white' + 'ml-auto mr-2 flex h-5 w-5 items-center justify-center rounded border-0 p-1 text-sm text-white transition-colors duration-200' )} > diff --git a/app/react/sidebar/Footer/Footer.tsx b/app/react/sidebar/Footer/Footer.tsx index 0c6636a3e..7374857c5 100644 --- a/app/react/sidebar/Footer/Footer.tsx +++ b/app/react/sidebar/Footer/Footer.tsx @@ -7,7 +7,6 @@ import { UpdateNotification } from './UpdateNotifications'; import { BuildInfoModalButton } from './BuildInfoModal'; import '@reach/dialog/styles.css'; import styles from './Footer.module.css'; -import Logo from './portainer_logo.svg?c'; export function Footer() { return isBE ? : ; @@ -19,7 +18,6 @@ function CEFooter() { - Community Edition @@ -43,7 +41,7 @@ function BEFooter() { function FooterContent({ children }: PropsWithChildren) { return ( -
+
{children}
); diff --git a/app/react/sidebar/Footer/portainer_logo.svg b/app/react/sidebar/Footer/portainer_logo.svg deleted file mode 100644 index 1bf390370..000000000 --- a/app/react/sidebar/Footer/portainer_logo.svg +++ /dev/null @@ -1,36 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app/react/sidebar/Header.module.css b/app/react/sidebar/Header.module.css index 002dad966..82a002e8a 100644 --- a/app/react/sidebar/Header.module.css +++ b/app/react/sidebar/Header.module.css @@ -1,9 +1,5 @@ .logo { display: inline; max-height: 55px; - max-width: min(100%, 230px); -} - -.collapseBtn:hover { - background-color: var(--bg-btn-default-hover-color); + max-width: min(100%, 220px); } diff --git a/app/react/sidebar/Header.tsx b/app/react/sidebar/Header.tsx index 60b24b883..b69aa7d3a 100644 --- a/app/react/sidebar/Header.tsx +++ b/app/react/sidebar/Header.tsx @@ -2,12 +2,13 @@ import { ChevronsLeft, ChevronsRight } from 'lucide-react'; import clsx from 'clsx'; import { isBE } from '@/react/portainer/feature-flags/feature-flags.service'; -import smallLogo from '@/assets/ico/logomark.svg'; import { Link } from '@@/Link'; import fullLogoBE from './portainer_logo-BE.svg'; import fullLogoCE from './portainer_logo-CE.svg'; +import smallLogoBE from './logomark-BE.svg'; +import smallLogoCE from './logomark-CE.svg'; import { useSidebarState } from './useSidebarState'; import styles from './Header.module.css'; @@ -20,7 +21,7 @@ export function Header({ logo: customLogo }: Props) { return (
-
+
); diff --git a/app/react/sidebar/SidebarItem/SidebarItem.tsx b/app/react/sidebar/SidebarItem/SidebarItem.tsx index 96fd5cbc8..7208516f3 100644 --- a/app/react/sidebar/SidebarItem/SidebarItem.tsx +++ b/app/react/sidebar/SidebarItem/SidebarItem.tsx @@ -59,7 +59,7 @@ export function SidebarItem({ return ( +
{title} -
+
{children}
diff --git a/app/react/sidebar/SidebarItem/SidebarTooltip.tsx b/app/react/sidebar/SidebarItem/SidebarTooltip.tsx index b9a2d11d4..ec25ffd99 100644 --- a/app/react/sidebar/SidebarItem/SidebarTooltip.tsx +++ b/app/react/sidebar/SidebarItem/SidebarTooltip.tsx @@ -8,7 +8,7 @@ type Props = { export function SidebarTooltip({ children, content }: Props) { return ( > = {}, options: TransitionOptions = {}, pathOptions: PathOptions = { diff --git a/app/react/sidebar/logomark-BE.svg b/app/react/sidebar/logomark-BE.svg new file mode 100644 index 000000000..beead5989 --- /dev/null +++ b/app/react/sidebar/logomark-BE.svg @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/app/react/sidebar/logomark-CE.svg b/app/react/sidebar/logomark-CE.svg new file mode 100644 index 000000000..1ed2ea259 --- /dev/null +++ b/app/react/sidebar/logomark-CE.svg @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/app/react/sidebar/portainer_logo-BE.svg b/app/react/sidebar/portainer_logo-BE.svg index ed7ab076d..ad9762aa8 100644 --- a/app/react/sidebar/portainer_logo-BE.svg +++ b/app/react/sidebar/portainer_logo-BE.svg @@ -1,51 +1,37 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + diff --git a/app/react/sidebar/portainer_logo-CE.svg b/app/react/sidebar/portainer_logo-CE.svg index 7b6c83a00..707c7b1a7 100644 --- a/app/react/sidebar/portainer_logo-CE.svg +++ b/app/react/sidebar/portainer_logo-CE.svg @@ -1,68 +1,38 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - + + diff --git a/app/setup-tests/setup-handlers/users.ts b/app/setup-tests/setup-handlers/users.ts index 67eb6debb..be8c3a5e3 100644 --- a/app/setup-tests/setup-handlers/users.ts +++ b/app/setup-tests/setup-handlers/users.ts @@ -2,9 +2,12 @@ import { http, HttpResponse } from 'msw'; import { TeamMembership } from '@/react/portainer/users/teams/types'; import { createMockUsers } from '@/react-tools/test-mocks'; +import { Role } from '@/portainer/users/types'; export const userHandlers = [ - http.get('/api/users', async () => HttpResponse.json(createMockUsers(10))), + http.get('/api/users', async () => + HttpResponse.json(createMockUsers(10, Role.Standard)) + ), http.get( '/api/users/:userId/memberships', () => HttpResponse.json([]) diff --git a/binary-version.json b/binary-version.json index 3f190e5d3..7a275ec82 100644 --- a/binary-version.json +++ b/binary-version.json @@ -1,5 +1,4 @@ { - "docker": "v27.5.1", - "kubectl": "v1.32.2", + "docker": "v28.3.0", "mingit": "2.49.0.1" } diff --git a/dev/run_container.sh b/dev/run_container.sh index 56b00edd4..56dc1ceda 100755 --- a/dev/run_container.sh +++ b/dev/run_container.sh @@ -16,6 +16,7 @@ docker run -d \ -v /var/run/docker.sock:/var/run/docker.sock:z \ -v /var/run/docker.sock:/var/run/alternative.sock:z \ -v /tmp:/tmp \ + -e CSP=false \ --name portainer \ portainer/base \ /app/portainer $PORTAINER_FLAGS diff --git a/dev/run_container_podman.sh b/dev/run_container_podman.sh index 1893efd0b..03e2fb270 100755 --- a/dev/run_container_podman.sh +++ b/dev/run_container_podman.sh @@ -16,6 +16,7 @@ sudo podman run -d \ -v "$PORTAINER_DATA:/data" \ -v /run/podman/podman.sock:/var/run/docker.sock \ -v /tmp:/tmp \ + -e CSP=false \ --privileged \ --name portainer \ portainer/base \ diff --git a/go.mod b/go.mod index 3a2ba0bf5..a7e925466 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/portainer/portainer -go 1.23.8 +go 1.24.4 require ( github.com/Masterminds/semver v1.5.0 @@ -28,7 +28,7 @@ require ( github.com/google/uuid v1.6.0 github.com/gorilla/csrf v1.7.3 github.com/gorilla/mux v1.8.1 - github.com/gorilla/websocket v1.5.0 + github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 github.com/hashicorp/go-version v1.7.0 github.com/hashicorp/golang-lru v0.5.4 github.com/joho/godotenv v1.4.0 @@ -36,6 +36,7 @@ require ( github.com/klauspost/compress v1.18.0 github.com/koding/websocketproxy v0.0.0-20181220232114-7ed82d81a28c github.com/opencontainers/go-digest v1.0.0 + github.com/opencontainers/image-spec v1.1.1 github.com/orcaman/concurrent-map v1.0.0 github.com/patrickmn/go-cache v2.1.0+incompatible github.com/pkg/errors v0.9.1 @@ -47,21 +48,23 @@ require ( github.com/urfave/negroni v1.0.0 github.com/viney-shih/go-lock v1.1.1 go.etcd.io/bbolt v1.4.0 - golang.org/x/crypto v0.37.0 + golang.org/x/crypto v0.39.0 golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 - golang.org/x/mod v0.24.0 + golang.org/x/mod v0.25.0 golang.org/x/oauth2 v0.29.0 - golang.org/x/sync v0.14.0 + golang.org/x/sync v0.15.0 gopkg.in/alecthomas/kingpin.v2 v2.2.6 gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v3 v3.0.1 - helm.sh/helm/v3 v3.17.3 - k8s.io/api v0.32.3 - k8s.io/apimachinery v0.32.3 - k8s.io/cli-runtime v0.32.2 - k8s.io/client-go v0.32.3 - k8s.io/kubectl v0.32.2 - k8s.io/metrics v0.32.2 + helm.sh/helm/v3 v3.18.4 + k8s.io/api v0.33.2 + k8s.io/apimachinery v0.33.2 + k8s.io/cli-runtime v0.33.2 + k8s.io/client-go v0.33.2 + k8s.io/kubectl v0.33.2 + k8s.io/kubelet v0.33.2 + k8s.io/metrics v0.33.2 + oras.land/oras-go/v2 v2.6.0 software.sslmate.com/src/go-pkcs12 v0.0.0-20210415151418-c5206de65a78 ) @@ -69,11 +72,10 @@ require github.com/gorilla/securecookie v1.1.2 // indirect require ( dario.cat/mergo v1.0.1 // indirect - github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 // indirect github.com/AlecAivazis/survey/v2 v2.3.7 // indirect github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c // indirect github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c // indirect - github.com/BurntSushi/toml v1.4.0 // indirect + github.com/BurntSushi/toml v1.5.0 // indirect github.com/DefangLabs/secret-detector v0.0.0-20250403165618-22662109213e // indirect github.com/MakeNowJust/heredoc v1.0.0 // indirect github.com/Masterminds/goutils v1.1.1 // indirect @@ -81,6 +83,7 @@ require ( github.com/Masterminds/sprig/v3 v3.3.0 // indirect github.com/Masterminds/squirrel v1.5.4 // indirect github.com/ProtonMail/go-crypto v1.1.3 // indirect + github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d // indirect github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect @@ -107,7 +110,7 @@ require ( github.com/cloudflare/cfssl v1.6.4 // indirect github.com/cloudflare/circl v1.3.7 // indirect github.com/containerd/console v1.0.4 // indirect - github.com/containerd/containerd v1.7.24 // indirect + github.com/containerd/containerd v1.7.27 // indirect github.com/containerd/containerd/api v1.9.0 // indirect github.com/containerd/containerd/v2 v2.1.1 // indirect github.com/containerd/continuity v0.4.5 // indirect @@ -120,7 +123,7 @@ require ( github.com/containers/libtrust v0.0.0-20230121012942-c1716e8a8d01 // indirect github.com/containers/ocicrypt v1.2.1 // indirect github.com/containers/storage v1.53.0 // indirect - github.com/cyphar/filepath-securejoin v0.3.6 // indirect + github.com/cyphar/filepath-securejoin v0.4.1 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/distribution/reference v0.6.0 // indirect github.com/docker/buildx v0.24.0 // indirect @@ -134,7 +137,7 @@ require ( github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203 // indirect github.com/emicklei/go-restful/v3 v3.11.0 // indirect github.com/emirpasic/gods v1.18.1 // indirect - github.com/evanphx/json-patch v5.9.0+incompatible // indirect + github.com/evanphx/json-patch v5.9.11+incompatible // indirect github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f // indirect github.com/fatih/camelcase v1.0.0 // indirect github.com/fatih/color v1.15.0 // indirect @@ -160,9 +163,8 @@ require ( github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/golang/snappy v0.0.4 // indirect - github.com/google/btree v1.0.1 // indirect - github.com/google/gnostic-models v0.6.8 // indirect - github.com/google/gofuzz v1.2.0 // indirect + github.com/google/btree v1.1.3 // indirect + github.com/google/gnostic-models v0.6.9 // indirect github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect github.com/gosuri/uitable v0.0.4 // indirect github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect @@ -225,7 +227,6 @@ require ( github.com/morikuni/aec v1.0.0 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect - github.com/opencontainers/image-spec v1.1.1 // indirect github.com/opencontainers/runtime-spec v1.2.1 // indirect github.com/pelletier/go-toml v1.9.5 // indirect github.com/peterbourgon/diskv v2.0.1+incompatible // indirect @@ -237,7 +238,7 @@ require ( github.com/prometheus/common v0.62.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect github.com/rivo/uniseg v0.4.4 // indirect - github.com/rubenv/sql-migrate v1.7.1 // indirect + github.com/rubenv/sql-migrate v1.8.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/secure-systems-lab/go-securesystemslib v0.8.0 // indirect github.com/segmentio/asm v1.1.3 // indirect @@ -274,8 +275,8 @@ require ( go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.56.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect go.opentelemetry.io/otel v1.35.0 // indirect - go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.31.0 // indirect - go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.31.0 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.32.0 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.32.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.35.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.35.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.35.0 // indirect @@ -285,10 +286,10 @@ require ( go.opentelemetry.io/otel/trace v1.35.0 // indirect go.opentelemetry.io/proto/otlp v1.5.0 // indirect go.uber.org/mock v0.5.2 // indirect - golang.org/x/net v0.39.0 // indirect + golang.org/x/net v0.40.0 // indirect golang.org/x/sys v0.33.0 // indirect - golang.org/x/term v0.31.0 // indirect - golang.org/x/text v0.24.0 // indirect + golang.org/x/term v0.32.0 // indirect + golang.org/x/text v0.26.0 // indirect golang.org/x/time v0.11.0 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20250218202821-56aae31c358a // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20250218202821-56aae31c358a // indirect @@ -298,17 +299,18 @@ require ( gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect - k8s.io/apiextensions-apiserver v0.32.2 // indirect - k8s.io/apiserver v0.32.3 // indirect - k8s.io/component-base v0.32.3 // indirect + k8s.io/apiextensions-apiserver v0.33.2 // indirect + k8s.io/apiserver v0.33.2 // indirect + k8s.io/component-base v0.33.2 // indirect + k8s.io/component-helpers v0.33.2 // indirect k8s.io/klog/v2 v2.130.1 // indirect - k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f // indirect + k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff // indirect k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect - oras.land/oras-go v1.2.5 // indirect sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect - sigs.k8s.io/kustomize/api v0.18.0 // indirect - sigs.k8s.io/kustomize/kyaml v0.18.1 // indirect - sigs.k8s.io/structured-merge-diff/v4 v4.4.2 // indirect + sigs.k8s.io/kustomize/api v0.19.0 // indirect + sigs.k8s.io/kustomize/kyaml v0.19.0 // indirect + sigs.k8s.io/randfill v1.0.0 // indirect + sigs.k8s.io/structured-merge-diff/v4 v4.6.0 // indirect sigs.k8s.io/yaml v1.4.0 // indirect tags.cncf.io/container-device-interface v1.0.1 // indirect ) diff --git a/go.sum b/go.sum index b79c1d644..ad8e48908 100644 --- a/go.sum +++ b/go.sum @@ -11,8 +11,8 @@ github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c/go.mod h1:xomTg6 github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c h1:/IBSNwUN8+eKzUzbJPqhK839ygXJ82sde8x3ogr6R28= github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= -github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= +github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg= +github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU= github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU= github.com/DefangLabs/secret-detector v0.0.0-20250403165618-22662109213e h1:rd4bOvKmDIx0WeTv9Qz+hghsgyjikFiPrseXHlKepO0= @@ -136,8 +136,8 @@ github.com/containerd/cgroups/v3 v3.0.5 h1:44na7Ud+VwyE7LIoJ8JTNQOa549a8543BmzaJ github.com/containerd/cgroups/v3 v3.0.5/go.mod h1:SA5DLYnXO8pTGYiAHXz94qvLQTKfVM5GEVisn4jpins= github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro= github.com/containerd/console v1.0.4/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk= -github.com/containerd/containerd v1.7.24 h1:zxszGrGjrra1yYJW/6rhm9cJ1ZQ8rkKBR48brqsa7nA= -github.com/containerd/containerd v1.7.24/go.mod h1:7QUzfURqZWCZV7RLNEn1XjUCQLEf0bkaK4GjUaZehxw= +github.com/containerd/containerd v1.7.27 h1:yFyEyojddO3MIGVER2xJLWoCIn+Up4GaHFquP7hsFII= +github.com/containerd/containerd v1.7.27/go.mod h1:xZmPnl75Vc+BLGt4MIfu6bp+fy03gdHAn9bz+FreFR0= github.com/containerd/containerd/api v1.9.0 h1:HZ/licowTRazus+wt9fM6r/9BQO7S0vD5lMcWspGIg0= github.com/containerd/containerd/api v1.9.0/go.mod h1:GhghKFmTR3hNtyznBoQ0EMWr9ju5AqHjcZPsSpTKutI= github.com/containerd/containerd/v2 v2.1.1 h1:znnkm7Ajz8lg8BcIPMhc/9yjBRN3B+OkNKqKisKfwwM= @@ -176,13 +176,15 @@ github.com/containers/storage v1.53.0/go.mod h1:pujcoOSc+upx15Jirdkebhtd8uJiLwbS github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4= github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec= github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs= +github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s= github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE= -github.com/cyphar/filepath-securejoin v0.3.6 h1:4d9N5ykBnSp5Xn2JkhocYDkOpURL/18CYMpo6xB9uWM= -github.com/cyphar/filepath-securejoin v0.3.6/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGLDGQL7h7bg04C/+u9jI= +github.com/cyphar/filepath-securejoin v0.4.1 h1:JyxxyPEaktOD+GAnqIqTf9A8tHyAG22rowi7HkoSU1s= +github.com/cyphar/filepath-securejoin v0.4.1/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGLDGQL7h7bg04C/+u9jI= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= @@ -190,8 +192,10 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8Yc github.com/dchest/uniuri v0.0.0-20200228104902-7aecb25e1fe5 h1:RAV05c0xOkJ3dZGS0JFybxFKZ2WMLabgx3uXnd7rpGs= github.com/dchest/uniuri v0.0.0-20200228104902-7aecb25e1fe5/go.mod h1:GgB8SF9nRG+GqaDtLcwJZsQFhcogVCJ79j4EdT0c2V4= github.com/denisenkom/go-mssqldb v0.0.0-20191128021309-1d7a30a10f73/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= -github.com/distribution/distribution/v3 v3.0.0-20221208165359-362910506bc2 h1:aBfCb7iqHmDEIp6fBvC/hQUddQfg+3qdYjwzaiP9Hnc= -github.com/distribution/distribution/v3 v3.0.0-20221208165359-362910506bc2/go.mod h1:WHNsWjnIn2V1LYOrME7e8KxSeKunYHsxEm4am0BUtcI= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/distribution/distribution/v3 v3.0.0 h1:q4R8wemdRQDClzoNNStftB2ZAfqOiN6UX90KJc4HjyM= +github.com/distribution/distribution/v3 v3.0.0/go.mod h1:tRNuFoZsUdyRVegq8xGNeds4KLjwLCRin/tTo6i1DhU= github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E= github.com/docker/buildx v0.24.0 h1:qiD+xktY+Fs3R79oz8M+7pbhip78qGLx6LBuVmyb+64= @@ -233,8 +237,8 @@ github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRr github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= -github.com/evanphx/json-patch v5.9.0+incompatible h1:fBXyNpNMuTTDdquAq/uisOr2lShz4oaXpDTX2bLe7ls= -github.com/evanphx/json-patch v5.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= +github.com/evanphx/json-patch v5.9.11+incompatible h1:ixHHqfcGvxhWkniF1tWxBHA0yb4Z+d1UQi45df52xW8= +github.com/evanphx/json-patch v5.9.11+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f h1:Wl78ApPPB2Wvf/TIe2xdyJxTlb6obmF18d8QdkxNDu4= github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f/go.mod h1:OSYXu++VVOHnXeitef/D8n/6y4QV8uLHSFXX4NeXMGc= github.com/fatih/camelcase v1.0.0 h1:hxNvNX/xYBp0ovncs8WyWZrOrpBNub/JfaMvbURyft8= @@ -326,15 +330,13 @@ github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= -github.com/gomodule/redigo v1.8.2 h1:H5XSIre1MB5NbPYFp+i1NBbb5qN1W8Y8YAQoAYbkm8k= -github.com/gomodule/redigo v1.8.2/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0= -github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4= -github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9uaxA= +github.com/google/btree v1.1.3 h1:CVpQJjYgC4VbzxeGVHfvZrv1ctoYCAI8vbl07Fcxlyg= +github.com/google/btree v1.1.3/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4= github.com/google/certificate-transparency-go v1.0.10-0.20180222191210-5ab67e519c93/go.mod h1:QeJfpSbVSfYc7RgB3gJFj9cbuQMMchQxrWXz8Ruopmg= github.com/google/certificate-transparency-go v1.1.4 h1:hCyXHDbtqlr/lMXU0D4WgbalXL0Zk4dSWWMbPV8VrqY= github.com/google/certificate-transparency-go v1.1.4/go.mod h1:D6lvbfwckhNrbM9WVl1EVeMOyzC19mpIjMOI4nxBHtQ= -github.com/google/gnostic-models v0.6.8 h1:yo/ABAfM5IMRsS1VnXjTBvUb61tFIHozhlYvRgGre9I= -github.com/google/gnostic-models v0.6.8/go.mod h1:5n7qKqH0f5wFt+aWF8CW6pZLLNOfYuF5OpfBSENuI8U= +github.com/google/gnostic-models v0.6.9 h1:MU/8wDLif2qCXZmzncUQ/BOfxWfthHi63KqpoNbWqVw= +github.com/google/gnostic-models v0.6.9/go.mod h1:CiWsm0s6BSQd1hRn8/QmxqB6BesYcbSZxsz9b0KuDBw= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= @@ -350,15 +352,15 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/csrf v1.7.3 h1:BHWt6FTLZAb2HtWT5KDBf6qgpZzvtbp9QWDRKZMXJC0= github.com/gorilla/csrf v1.7.3/go.mod h1:F1Fj3KG23WYHE6gozCmBAezKookxbIvUJT+121wTuLk= -github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4= -github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q= +github.com/gorilla/handlers v1.5.2 h1:cLTUSsNkgcwhgRqvCNmdbRWG0A3N4F+M2nWKdScwyEE= +github.com/gorilla/handlers v1.5.2/go.mod h1:dX+xVpaxdSw+q0Qek8SSsl3dfMk3jNddUkMzo0GtH0w= github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA= github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo= -github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= -github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 h1:JeSE6pjso5THxAzdVpqr6/geYxZytqFMBCOtn/ujyeo= +github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674/go.mod h1:r4w70xmWCQKmi1ONH4KIaBptdivuRPyosB9RmPlGEwA= github.com/gosuri/uitable v0.0.4 h1:IG2xLKRvErL3uhY6e1BylFzG+aJiwQviDDTfOKeKTpY= github.com/gosuri/uitable v0.0.4/go.mod h1:tKR86bXuXPZazfOTG1FIzvjIdXzd0mo4Vtn16vt0PJo= github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 h1:+ngKgrYPPJrOjhax5N+uePQ0Fh1Z7PheYoUI/0nzkPA= @@ -378,6 +380,10 @@ github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKe github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/golang-lru/arc/v2 v2.0.5 h1:l2zaLDubNhW4XO3LnliVj0GXO3+/CGNJAg1dcN2Fpfw= +github.com/hashicorp/golang-lru/arc/v2 v2.0.5/go.mod h1:ny6zBSQZi2JxIeYcv7kt2sH2PXJtirBN7RDhRpxPkxU= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog= github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= @@ -615,6 +621,12 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT github.com/prometheus/procfs v0.0.3/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDaekg4FpcdQ= github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc= github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk= +github.com/redis/go-redis/extra/rediscmd/v9 v9.0.5 h1:EaDatTxkdHG+U3Bk4EUr+DZ7fOGwTfezUiUJMaIcaho= +github.com/redis/go-redis/extra/rediscmd/v9 v9.0.5/go.mod h1:fyalQWdtzDBECAQFBJuQe5bzQ02jGd5Qcbgb97Flm7U= +github.com/redis/go-redis/extra/redisotel/v9 v9.0.5 h1:EfpWLLCyXw8PSM2/XNJLjI3Pb27yVE+gIAfeqp8LUCc= +github.com/redis/go-redis/extra/redisotel/v9 v9.0.5/go.mod h1:WZjPDy7VNzn77AAfnAfVjZNvfJTYfPetfZk5yoSTLaQ= +github.com/redis/go-redis/v9 v9.7.3 h1:YpPyAayJV+XErNsatSElgRZZVCwXX9QzkKYNvO7x0wM= +github.com/redis/go-redis/v9 v9.7.3/go.mod h1:bGUrSggJ9X9GUmZpZNEOQKaANxSGgOEBRltRTZHSvrA= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= @@ -625,8 +637,8 @@ github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWN github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w= github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0= -github.com/rubenv/sql-migrate v1.7.1 h1:f/o0WgfO/GqNuVg+6801K/KW3WdDSupzSjDYODmiUq4= -github.com/rubenv/sql-migrate v1.7.1/go.mod h1:Ob2Psprc0/3ggbM6wCzyYVFFuc6FyZrb2AS+ezLDFb4= +github.com/rubenv/sql-migrate v1.8.0 h1:dXnYiJk9k3wetp7GfQbKJcPHjVJL6YK19tKj8t2Ns0o= +github.com/rubenv/sql-migrate v1.8.0/go.mod h1:F2bGFBwCU+pnmbtNYDeKvSuvL6lBVtXDXUUv5t+u1qw= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/secure-systems-lab/go-securesystemslib v0.8.0 h1:mr5An6X45Kb2nddcFlbmfHkLguCE9laoZCUzEEpIZXA= @@ -732,12 +744,6 @@ github.com/xlab/treeprint v1.2.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43 h1:+lm10QQTNSBd8DVTNGHx7o/IKu9HYDvLMffDhbyLccI= -github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43/go.mod h1:aX5oPXxHm3bOH+xeAttToC8pqch2ScQN/JoXYupl6xs= -github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50 h1:hlE8//ciYMztlGpl/VA+Zm1AcTPHYkHJPbHqE6WJUXE= -github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50/go.mod h1:NUSPSUX/bi6SeDMUh6brw0nXpxHnc96TguQh0+r/ssA= -github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f h1:ERexzlUfuTvpE74urLSbIQW0Z/6hF9t8U4NsJLaioAY= -github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f/go.mod h1:GlGEuHIJweS1mbCqG+7vt2nvWLzLLnRHbXz5JKd/Qbg= github.com/zclconf/go-cty v1.16.2 h1:LAJSwc3v81IRBZyUVQDUdZ7hs3SYs9jv0eZJDWHD/70= github.com/zclconf/go-cty v1.16.2/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE= github.com/zmap/zcrypto v0.0.0-20210511125630-18f1e0152cfc h1:zkGwegkOW709y0oiAraH/3D8njopUR/pARHv4tZZ6pw= @@ -750,6 +756,10 @@ go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA= go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A= +go.opentelemetry.io/contrib/bridges/prometheus v0.57.0 h1:UW0+QyeyBVhn+COBec3nGhfnFe5lwB0ic1JBVjzhk0w= +go.opentelemetry.io/contrib/bridges/prometheus v0.57.0/go.mod h1:ppciCHRLsyCio54qbzQv0E4Jyth/fLWDTJYfvWpcSVk= +go.opentelemetry.io/contrib/exporters/autoexport v0.57.0 h1:jmTVJ86dP60C01K3slFQa2NQ/Aoi7zA+wy7vMOKD9H4= +go.opentelemetry.io/contrib/exporters/autoexport v0.57.0/go.mod h1:EJBheUMttD/lABFyLXhce47Wr6DPWYReCzaZiXadH7g= go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.60.0 h1:x7wzEgXfnzJcHDwStJT+mxOz4etr2EcexjqhBvmoakw= go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.60.0/go.mod h1:rg+RlpR5dKwaS95IyyZqj5Wd4E13lk/msnTS0Xl9lJM= go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.56.0 h1:4BZHA+B1wXEQoGNHxW8mURaLhcdGwvRnmhGbm+odRbc= @@ -758,20 +768,36 @@ go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 h1:sbiXRND go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0/go.mod h1:69uWxva0WgAA/4bu2Yy70SLDBwZXuQ6PbBpbsa5iZrQ= go.opentelemetry.io/otel v1.35.0 h1:xKWKPxrxB6OtMCbmMY021CqC45J+3Onta9MqjhnusiQ= go.opentelemetry.io/otel v1.35.0/go.mod h1:UEqy8Zp11hpkUrL73gSlELM0DupHoiq72dR+Zqel/+Y= -go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.31.0 h1:FZ6ei8GFW7kyPYdxJaV2rgI6M+4tvZzhYsQ2wgyVC08= -go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.31.0/go.mod h1:MdEu/mC6j3D+tTEfvI15b5Ci2Fn7NneJ71YMoiS3tpI= -go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.31.0 h1:ZsXq73BERAiNuuFXYqP4MR5hBrjXfMGSO+Cx7qoOZiM= -go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.31.0/go.mod h1:hg1zaDMpyZJuUzjFxFsRYBoccE86tM9Uf4IqNMUxvrY= +go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.8.0 h1:WzNab7hOOLzdDF/EoWCt4glhrbMPVMOO5JYTmpz36Ls= +go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.8.0/go.mod h1:hKvJwTzJdp90Vh7p6q/9PAOd55dI6WA6sWj62a/JvSs= +go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.8.0 h1:S+LdBGiQXtJdowoJoQPEtI52syEP/JYBUpjO49EQhV8= +go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.8.0/go.mod h1:5KXybFvPGds3QinJWQT7pmXf+TN5YIa7CNYObWRkj50= +go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.32.0 h1:j7ZSD+5yn+lo3sGV69nW04rRR0jhYnBwjuX3r0HvnK0= +go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.32.0/go.mod h1:WXbYJTUaZXAbYd8lbgGuvih0yuCfOFC5RJoYnoLcGz8= +go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.32.0 h1:t/Qur3vKSkUCcDVaSumWF2PKHt85pc7fRvFuoVT8qFU= +go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.32.0/go.mod h1:Rl61tySSdcOJWoEgYZVtmnKdA0GeKrSqkHC1t+91CH8= go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.35.0 h1:1fTNlAIJZGWLP5FVu0fikVry1IsiUnXjf7QFvoNN3Xw= go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.35.0/go.mod h1:zjPK58DtkqQFn+YUMbx0M2XV3QgKU0gS9LeGohREyK4= go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.35.0 h1:m639+BofXTvcY1q8CGs4ItwQarYtJPOWmVobfM1HpVI= go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.35.0/go.mod h1:LjReUci/F4BUyv+y4dwnq3h/26iNOeC3wAIqgvTIZVo= go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.35.0 h1:xJ2qHD0C1BeYVTLLR9sX12+Qb95kfeD/byKj6Ky1pXg= go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.35.0/go.mod h1:u5BF1xyjstDowA1R5QAO9JHzqK+ublenEW/dyqTjBVk= +go.opentelemetry.io/otel/exporters/prometheus v0.54.0 h1:rFwzp68QMgtzu9PgP3jm9XaMICI6TsofWWPcBDKwlsU= +go.opentelemetry.io/otel/exporters/prometheus v0.54.0/go.mod h1:QyjcV9qDP6VeK5qPyKETvNjmaaEc7+gqjh4SS0ZYzDU= +go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.8.0 h1:CHXNXwfKWfzS65yrlB2PVds1IBZcdsX8Vepy9of0iRU= +go.opentelemetry.io/otel/exporters/stdout/stdoutlog v0.8.0/go.mod h1:zKU4zUgKiaRxrdovSS2amdM5gOc59slmo/zJwGX+YBg= +go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.32.0 h1:SZmDnHcgp3zwlPBS2JX2urGYe/jBKEIT6ZedHRUyCz8= +go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.32.0/go.mod h1:fdWW0HtZJ7+jNpTKUR0GpMEDP69nR8YBJQxNiVCE3jk= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.32.0 h1:cC2yDI3IQd0Udsux7Qmq8ToKAx1XCilTQECZ0KDZyTw= +go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.32.0/go.mod h1:2PD5Ex6z8CFzDbTdOlwyNIUywRr1DN0ospafJM1wJ+s= +go.opentelemetry.io/otel/log v0.8.0 h1:egZ8vV5atrUWUbnSsHn6vB8R21G2wrKqNiDt3iWertk= +go.opentelemetry.io/otel/log v0.8.0/go.mod h1:M9qvDdUTRCopJcGRKg57+JSQ9LgLBrwwfC32epk5NX8= go.opentelemetry.io/otel/metric v1.35.0 h1:0znxYu2SNyuMSQT4Y9WDWej0VpcsxkuklLa4/siN90M= go.opentelemetry.io/otel/metric v1.35.0/go.mod h1:nKVFgxBZ2fReX6IlyW28MgZojkoAkJGaE8CpgeAU3oE= go.opentelemetry.io/otel/sdk v1.35.0 h1:iPctf8iprVySXSKJffSS79eOjl9pvxV9ZqOWT0QejKY= go.opentelemetry.io/otel/sdk v1.35.0/go.mod h1:+ga1bZliga3DxJ3CQGg3updiaAJoNECOgJREo9KHGQg= +go.opentelemetry.io/otel/sdk/log v0.8.0 h1:zg7GUYXqxk1jnGF/dTdLPrK06xJdrXgqgFLnI4Crxvs= +go.opentelemetry.io/otel/sdk/log v0.8.0/go.mod h1:50iXr0UVwQrYS45KbruFrEt4LvAdCaWWgIrsN3ZQggo= go.opentelemetry.io/otel/sdk/metric v1.35.0 h1:1RriWBmCKgkeHEhM7a2uMjMUfP7MsOF5JpUCaEqEI9o= go.opentelemetry.io/otel/sdk/metric v1.35.0/go.mod h1:is6XYCUMpcKi+ZsOvfluY5YstFnhW0BidkR+gL+qN+w= go.opentelemetry.io/otel/trace v1.35.0 h1:dPpEfJu1sDIqruz7BHFG3c7528f6ddfSWfFDVt/xgMs= @@ -793,15 +819,15 @@ golang.org/x/crypto v0.0.0-20201117144127-c1f2f97bffc9/go.mod h1:jdWPYTVW3xRLrWP golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE= -golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc= +golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM= +golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U= golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 h1:R84qjqJb5nVJMxqWYb3np9L5ZsaDtB+a39EqjV0JSUM= golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0/go.mod h1:S9Xr4PYopiDyqSyp5NjCrhFrqg6A5zA2E/iPHPhqnS8= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU= -golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww= +golang.org/x/mod v0.25.0 h1:n7a+ZbQKQA/Ysbyb0/6IbB1H/X41mKgbhfv7AfG/44w= +golang.org/x/mod v0.25.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= @@ -812,8 +838,8 @@ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwY golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY= -golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E= +golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY= +golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds= golang.org/x/oauth2 v0.29.0 h1:WdYw2tdTK1S8olAzWHdgeqfy+Mtm9XNhv/xJsY65d98= golang.org/x/oauth2 v0.29.0/go.mod h1:onh5ek6nERTohokkhCD/y2cV4Do3fxFHFuAejCkRWT8= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -824,8 +850,8 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ= -golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8= +golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -858,15 +884,15 @@ golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.31.0 h1:erwDkOK1Msy6offm1mOgvspSkslFnIGsFnxOKoufg3o= -golang.org/x/term v0.31.0/go.mod h1:R4BeIy7D95HzImkxGkTW1UQTtP54tio2RyHz7PwK0aw= +golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg= +golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0= -golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU= +golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M= +golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA= golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0= golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -874,8 +900,8 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.32.0 h1:Q7N1vhpkQv7ybVzLFtTjvQya2ewbwNDZzUgfXGqtMWU= -golang.org/x/tools v0.32.0/go.mod h1:ZxrU41P/wAbZD8EDa6dDCa6XfpkhJ7HFMjHJXfBDu8s= +golang.org/x/tools v0.33.0 h1:4qz2S3zmRxbGIhDIAgjxvFutSvH5EfnsYrRBj0UI0bc= +golang.org/x/tools v0.33.0/go.mod h1:CIJMaWEY88juyUfo7UbgPqbC8rU2OqfAV1h2Qp0oMYI= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -924,42 +950,49 @@ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q= gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA= -helm.sh/helm/v3 v3.17.3 h1:3n5rW3D0ArjFl0p4/oWO8IbY/HKaNNwJtOQFdH2AZHg= -helm.sh/helm/v3 v3.17.3/go.mod h1:+uJKMH/UiMzZQOALR3XUf3BLIoczI2RKKD6bMhPh4G8= -k8s.io/api v0.32.3 h1:Hw7KqxRusq+6QSplE3NYG4MBxZw1BZnq4aP4cJVINls= -k8s.io/api v0.32.3/go.mod h1:2wEDTXADtm/HA7CCMD8D8bK4yuBUptzaRhYcYEEYA3k= -k8s.io/apiextensions-apiserver v0.32.2 h1:2YMk285jWMk2188V2AERy5yDwBYrjgWYggscghPCvV4= -k8s.io/apiextensions-apiserver v0.32.2/go.mod h1:GPwf8sph7YlJT3H6aKUWtd0E+oyShk/YHWQHf/OOgCA= -k8s.io/apimachinery v0.32.3 h1:JmDuDarhDmA/Li7j3aPrwhpNBA94Nvk5zLeOge9HH1U= -k8s.io/apimachinery v0.32.3/go.mod h1:GpHVgxoKlTxClKcteaeuF1Ul/lDVb74KpZcxcmLDElE= -k8s.io/apiserver v0.32.3 h1:kOw2KBuHOA+wetX1MkmrxgBr648ksz653j26ESuWNY8= -k8s.io/apiserver v0.32.3/go.mod h1:q1x9B8E/WzShF49wh3ADOh6muSfpmFL0I2t+TG0Zdgc= -k8s.io/cli-runtime v0.32.2 h1:aKQR4foh9qeyckKRkNXUccP9moxzffyndZAvr+IXMks= -k8s.io/cli-runtime v0.32.2/go.mod h1:a/JpeMztz3xDa7GCyyShcwe55p8pbcCVQxvqZnIwXN8= -k8s.io/client-go v0.32.3 h1:RKPVltzopkSgHS7aS98QdscAgtgah/+zmpAogooIqVU= -k8s.io/client-go v0.32.3/go.mod h1:3v0+3k4IcT9bXTc4V2rt+d2ZPPG700Xy6Oi0Gdl2PaY= -k8s.io/component-base v0.32.3 h1:98WJvvMs3QZ2LYHBzvltFSeJjEx7t5+8s71P7M74u8k= -k8s.io/component-base v0.32.3/go.mod h1:LWi9cR+yPAv7cu2X9rZanTiFKB2kHA+JjmhkKjCZRpI= +helm.sh/helm/v3 v3.18.4 h1:pNhnHM3nAmDrxz6/UC+hfjDY4yeDATQCka2/87hkZXQ= +helm.sh/helm/v3 v3.18.4/go.mod h1:WVnwKARAw01iEdjpEkP7Ii1tT1pTPYfM1HsakFKM3LI= +k8s.io/api v0.33.2 h1:YgwIS5jKfA+BZg//OQhkJNIfie/kmRsO0BmNaVSimvY= +k8s.io/api v0.33.2/go.mod h1:fhrbphQJSM2cXzCWgqU29xLDuks4mu7ti9vveEnpSXs= +k8s.io/apiextensions-apiserver v0.33.2 h1:6gnkIbngnaUflR3XwE1mCefN3YS8yTD631JXQhsU6M8= +k8s.io/apiextensions-apiserver v0.33.2/go.mod h1:IvVanieYsEHJImTKXGP6XCOjTwv2LUMos0YWc9O+QP8= +k8s.io/apimachinery v0.33.2 h1:IHFVhqg59mb8PJWTLi8m1mAoepkUNYmptHsV+Z1m5jY= +k8s.io/apimachinery v0.33.2/go.mod h1:BHW0YOu7n22fFv/JkYOEfkUYNRN0fj0BlvMFWA7b+SM= +k8s.io/apiserver v0.33.2 h1:KGTRbxn2wJagJowo29kKBp4TchpO1DRO3g+dB/KOJN4= +k8s.io/apiserver v0.33.2/go.mod h1:9qday04wEAMLPWWo9AwqCZSiIn3OYSZacDyu/AcoM/M= +k8s.io/cli-runtime v0.33.2 h1:koNYQKSDdq5AExa/RDudXMhhtFasEg48KLS2KSAU74Y= +k8s.io/cli-runtime v0.33.2/go.mod h1:gnhsAWpovqf1Zj5YRRBBU7PFsRc6NkEkwYNQE+mXL88= +k8s.io/client-go v0.33.2 h1:z8CIcc0P581x/J1ZYf4CNzRKxRvQAwoAolYPbtQes+E= +k8s.io/client-go v0.33.2/go.mod h1:9mCgT4wROvL948w6f6ArJNb7yQd7QsvqavDeZHvNmHo= +k8s.io/component-base v0.33.2 h1:sCCsn9s/dG3ZrQTX/Us0/Sx2R0G5kwa0wbZFYoVp/+0= +k8s.io/component-base v0.33.2/go.mod h1:/41uw9wKzuelhN+u+/C59ixxf4tYQKW7p32ddkYNe2k= +k8s.io/component-helpers v0.33.2 h1:AjCtYzst11NV8ensxV/2LEEXRwctqS7Bs44bje9Qcnw= +k8s.io/component-helpers v0.33.2/go.mod h1:PsPpiCk74n8pGWp1d6kjK/iSKBTyQfIacv02BNkMenU= k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk= k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE= -k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f h1:GA7//TjRY9yWGy1poLzYYJJ4JRdzg3+O6e8I+e+8T5Y= -k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f/go.mod h1:R/HEjbvWI0qdfb8viZUeVZm0X6IZnxAydC7YU42CMw4= -k8s.io/kubectl v0.32.2 h1:TAkag6+XfSBgkqK9I7ZvwtF0WVtUAvK8ZqTt+5zi1Us= -k8s.io/kubectl v0.32.2/go.mod h1:+h/NQFSPxiDZYX/WZaWw9fwYezGLISP0ud8nQKg+3g8= -k8s.io/metrics v0.32.2 h1:7t/rZzTHFrGa9f94XcgLlm3ToAuJtdlHANcJEHlYl9g= -k8s.io/metrics v0.32.2/go.mod h1:VL3nJpzcgB6L5nSljkkzoE0nilZhVgcjCfNRgoylaIQ= +k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff h1:/usPimJzUKKu+m+TE36gUyGcf03XZEP0ZIKgKj35LS4= +k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff/go.mod h1:5jIi+8yX4RIb8wk3XwBo5Pq2ccx4FP10ohkbSKCZoK8= +k8s.io/kubectl v0.33.2 h1:7XKZ6DYCklu5MZQzJe+CkCjoGZwD1wWl7t/FxzhMz7Y= +k8s.io/kubectl v0.33.2/go.mod h1:8rC67FB8tVTYraovAGNi/idWIK90z2CHFNMmGJZJ3KI= +k8s.io/kubelet v0.33.2 h1:wxEau5/563oJb3j3KfrCKlNWWx35YlSgDLOYUBCQ0pg= +k8s.io/kubelet v0.33.2/go.mod h1:way8VCDTUMiX1HTOvJv7M3xS/xNysJI6qh7TOqMe5KM= +k8s.io/metrics v0.33.2 h1:gNCBmtnUMDMCRg9Ly5ehxP3OdKISMsOnh1vzk01iCgE= +k8s.io/metrics v0.33.2/go.mod h1:yxoAosKGRsZisv3BGekC5W6T1J8XSV+PoUEevACRv7c= k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 h1:M3sRQVHv7vB20Xc2ybTt7ODCeFj6JSWYFzOFnYeS6Ro= k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= -oras.land/oras-go v1.2.5 h1:XpYuAwAb0DfQsunIyMfeET92emK8km3W4yEzZvUbsTo= -oras.land/oras-go v1.2.5/go.mod h1:PuAwRShRZCsZb7g8Ar3jKKQR/2A/qN+pkYxIOd/FAoo= +oras.land/oras-go/v2 v2.6.0 h1:X4ELRsiGkrbeox69+9tzTu492FMUu7zJQW6eJU+I2oc= +oras.land/oras-go/v2 v2.6.0/go.mod h1:magiQDfG6H1O9APp+rOsvCPcW1GD2MM7vgnKY0Y+u1o= sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 h1:/Rv+M11QRah1itp8VhT6HoVx1Ray9eB4DBr+K+/sCJ8= sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3/go.mod h1:18nIHnGi6636UCz6m8i4DhaJ65T6EruyzmoQqI2BVDo= -sigs.k8s.io/kustomize/api v0.18.0 h1:hTzp67k+3NEVInwz5BHyzc9rGxIauoXferXyjv5lWPo= -sigs.k8s.io/kustomize/api v0.18.0/go.mod h1:f8isXnX+8b+SGLHQ6yO4JG1rdkZlvhaCf/uZbLVMb0U= -sigs.k8s.io/kustomize/kyaml v0.18.1 h1:WvBo56Wzw3fjS+7vBjN6TeivvpbW9GmRaWZ9CIVmt4E= -sigs.k8s.io/kustomize/kyaml v0.18.1/go.mod h1:C3L2BFVU1jgcddNBE1TxuVLgS46TjObMwW5FT9FcjYo= -sigs.k8s.io/structured-merge-diff/v4 v4.4.2 h1:MdmvkGuXi/8io6ixD5wud3vOLwc1rj0aNqRlpuvjmwA= -sigs.k8s.io/structured-merge-diff/v4 v4.4.2/go.mod h1:N8f93tFZh9U6vpxwRArLiikrE5/2tiu1w1AGfACIGE4= +sigs.k8s.io/kustomize/api v0.19.0 h1:F+2HB2mU1MSiR9Hp1NEgoU2q9ItNOaBJl0I4Dlus5SQ= +sigs.k8s.io/kustomize/api v0.19.0/go.mod h1:/BbwnivGVcBh1r+8m3tH1VNxJmHSk1PzP5fkP6lbL1o= +sigs.k8s.io/kustomize/kyaml v0.19.0 h1:RFge5qsO1uHhwJsu3ipV7RNolC7Uozc0jUBC/61XSlA= +sigs.k8s.io/kustomize/kyaml v0.19.0/go.mod h1:FeKD5jEOH+FbZPpqUghBP8mrLjJ3+zD3/rf9NNu1cwY= +sigs.k8s.io/randfill v0.0.0-20250304075658-069ef1bbf016/go.mod h1:XeLlZ/jmk4i1HRopwe7/aU3H5n1zNUcX6TM94b3QxOY= +sigs.k8s.io/randfill v1.0.0 h1:JfjMILfT8A6RbawdsK2JXGBR5AQVfd+9TbzrlneTyrU= +sigs.k8s.io/randfill v1.0.0/go.mod h1:XeLlZ/jmk4i1HRopwe7/aU3H5n1zNUcX6TM94b3QxOY= +sigs.k8s.io/structured-merge-diff/v4 v4.6.0 h1:IUA9nvMmnKWcj5jl84xn+T5MnlZKThmUW1TdblaLVAc= +sigs.k8s.io/structured-merge-diff/v4 v4.6.0/go.mod h1:dDy58f92j70zLsuZVuUX5Wp9vtxXpaZnkPGWeqDfCps= sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E= sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY= software.sslmate.com/src/go-pkcs12 v0.0.0-20210415151418-c5206de65a78 h1:SqYE5+A2qvRhErbsXFfUEUmpWEKxxRSMgGLkvRAFOV4= diff --git a/package.json b/package.json index d9c180698..9a250005a 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Portainer.io", "name": "portainer", "homepage": "http://portainer.io", - "version": "2.31.0", + "version": "2.32.0", "repository": { "type": "git", "url": "git@github.com:portainer/portainer.git" @@ -20,7 +20,7 @@ "dev": "webpack-dev-server", "start": "webpack -w", "build": "webpack", - "format": "prettier --log-level warn --write \"**/*.{js,css,html,jsx,tsx,ts,json}\"", + "format": "prettier --log-level warn --write \"**/*.{js,css,html,jsx,tsx,ts}\"", "lint": "eslint --cache --fix './**/*.{js,jsx,ts,tsx}'", "test": "vitest run", "sb": "yarn storybook", diff --git a/pkg/libhelm/cache/cache.go b/pkg/libhelm/cache/cache.go new file mode 100644 index 000000000..e555bc090 --- /dev/null +++ b/pkg/libhelm/cache/cache.go @@ -0,0 +1,126 @@ +package cache + +import ( + "fmt" + "time" + + "github.com/patrickmn/go-cache" + portainer "github.com/portainer/portainer/api" + "github.com/rs/zerolog/log" + "helm.sh/helm/v3/pkg/registry" +) + +// Cache manages Helm registry clients with TTL-based expiration +// Registry clients are cached per registry ID rather than per user session +// to optimize rate limiting - one login per registry per Portainer instance +type Cache struct { + cache *cache.Cache +} + +// CachedRegistryClient wraps a registry client with metadata +type CachedRegistryClient struct { + Client *registry.Client + RegistryID portainer.RegistryID + CreatedAt time.Time +} + +// newCache creates a new Helm registry client cache with the specified timeout +func newCache(userSessionTimeout string) (*Cache, error) { + timeout, err := time.ParseDuration(userSessionTimeout) + if err != nil { + return nil, fmt.Errorf("invalid user session timeout: %w", err) + } + + return &Cache{ + cache: cache.New(timeout, timeout), + }, nil +} + +// getByRegistryID retrieves a cached registry client by registry ID +// Cache key strategy: use registryID for maximum efficiency against rate limits +// This means one login per registry per Portainer instance, regardless of user/environment +func (c *Cache) getByRegistryID(registryID portainer.RegistryID) (*registry.Client, bool) { + key := generateRegistryIDCacheKey(registryID) + + cachedClient, found := c.cache.Get(key) + if !found { + log.Debug(). + Str("cache_key", key). + Int("registry_id", int(registryID)). + Str("context", "HelmRegistryCache"). + Msg("Cache miss for registry client") + return nil, false + } + + client := cachedClient.(CachedRegistryClient) + + log.Debug(). + Str("cache_key", key). + Int("registry_id", int(registryID)). + Str("context", "HelmRegistryCache"). + Msg("Cache hit for registry client") + + return client.Client, true +} + +// setByRegistryID stores a registry client in the cache with registry ID context +func (c *Cache) setByRegistryID(registryID portainer.RegistryID, client *registry.Client) { + if client == nil { + log.Warn(). + Int("registry_id", int(registryID)). + Str("context", "HelmRegistryCache"). + Msg("Attempted to cache nil registry client") + return + } + + key := generateRegistryIDCacheKey(registryID) + + cachedClient := CachedRegistryClient{ + Client: client, + RegistryID: registryID, + CreatedAt: time.Now(), + } + + c.cache.Set(key, cachedClient, cache.DefaultExpiration) + + log.Debug(). + Str("cache_key", key). + Int("registry_id", int(registryID)). + Str("context", "HelmRegistryCache"). + Msg("Cached registry client") +} + +// flushRegistry removes cached registry client for a specific registry ID +// This should be called whenever registry credentials change +func (c *Cache) flushRegistry(registryID portainer.RegistryID) { + key := generateRegistryIDCacheKey(registryID) + + c.cache.Delete(key) + log.Info(). + Int("registry_id", int(registryID)). + Str("context", "HelmRegistryCache"). + Msg("Flushed registry client due to registry change") +} + +// flushAll removes all cached registry clients +func (c *Cache) flushAll() { + itemCount := c.cache.ItemCount() + c.cache.Flush() + + if itemCount > 0 { + log.Info(). + Int("cached_clients_removed", itemCount). + Str("context", "HelmRegistryCache"). + Msg("Flushed all registry clients") + } +} + +// generateRegistryIDCacheKey creates a cache key from registry ID +// Key strategy decision: Use registry ID instead of user sessions or URL+username +// This provides optimal rate limiting protection since each registry only gets +// logged into once per Portainer instance, regardless of how many users access it +// RBAC security is enforced before reaching this caching layer +// When a new user needs access, they reuse the same cached client +func generateRegistryIDCacheKey(registryID portainer.RegistryID) string { + return fmt.Sprintf("registry:%d", registryID) +} diff --git a/pkg/libhelm/cache/manager.go b/pkg/libhelm/cache/manager.go new file mode 100644 index 000000000..6520b314c --- /dev/null +++ b/pkg/libhelm/cache/manager.go @@ -0,0 +1,81 @@ +package cache + +import ( + "sync" + + portainer "github.com/portainer/portainer/api" + "github.com/rs/zerolog/log" + "helm.sh/helm/v3/pkg/registry" +) + +var ( + // Global singleton instance + instance *Cache + once sync.Once +) + +// Initialize creates and initializes the global cache instance +func Initialize(userSessionTimeout string) error { + var err error + once.Do(func() { + instance, err = newCache(userSessionTimeout) + if err != nil { + log.Error(). + Err(err). + Str("user_session_timeout", userSessionTimeout). + Msg("Failed to initialize Helm registry cache") + } else { + log.Info(). + Str("user_session_timeout", userSessionTimeout). + Msg("Helm registry cache initialized") + } + }) + return err +} + +// Registry-based cache management functions + +// GetCachedRegistryClientByID retrieves a cached registry client by registry ID +func GetCachedRegistryClientByID(registryID portainer.RegistryID) (*registry.Client, bool) { + if instance == nil { + log.Debug(). + Str("context", "HelmRegistryCache"). + Msg("Cache not initialized, returning nil") + return nil, false + } + return instance.getByRegistryID(registryID) +} + +// SetCachedRegistryClientByID stores a registry client in the cache by registry ID +func SetCachedRegistryClientByID(registryID portainer.RegistryID, client *registry.Client) { + if instance == nil { + log.Warn(). + Str("context", "HelmRegistryCache"). + Msg("Cannot set cache entry - cache not initialized") + return + } + instance.setByRegistryID(registryID, client) +} + +// FlushRegistryByID removes cached registry client for a specific registry ID +// This should be called whenever registry credentials change +func FlushRegistryByID(registryID portainer.RegistryID) { + if instance == nil { + log.Debug(). + Str("context", "HelmRegistryCache"). + Msg("Cache not initialized, nothing to flush") + return + } + instance.flushRegistry(registryID) +} + +// FlushAll removes all cached registry clients +func FlushAll() { + if instance == nil { + log.Debug(). + Str("context", "HelmRegistryCache"). + Msg("Cache not initialized, nothing to flush") + return + } + instance.flushAll() +} diff --git a/pkg/libhelm/options/chart_reference.go b/pkg/libhelm/options/chart_reference.go new file mode 100644 index 000000000..11b3daf53 --- /dev/null +++ b/pkg/libhelm/options/chart_reference.go @@ -0,0 +1,38 @@ +package options + +import ( + "strings" +) + +const ( + // OCIProtocolPrefix is the standard OCI protocol prefix + OCIProtocolPrefix = "oci://" +) + +// ConstructChartReference constructs the appropriate chart reference based on registry type +func ConstructChartReference(registryURL string, chartName string) string { + if registryURL == "" { + return chartName + } + + // Don't double-prefix if chart already contains the registry URL + if strings.HasPrefix(chartName, OCIProtocolPrefix) { + return chartName + } + + baseURL := ConstructOCIRegistryReference(registryURL) + + // Handle cases where chartName might already have a path separator + if strings.HasPrefix(chartName, "/") { + return baseURL + chartName + } + + return baseURL + "/" + chartName +} + +func ConstructOCIRegistryReference(registryURL string) string { + // Remove oci:// prefix if present to avoid duplication + registryURL = strings.TrimPrefix(registryURL, OCIProtocolPrefix) + // Ensure we have oci:// prefix for OCI registries + return OCIProtocolPrefix + registryURL +} diff --git a/pkg/libhelm/options/chart_reference_test.go b/pkg/libhelm/options/chart_reference_test.go new file mode 100644 index 000000000..06db8c26e --- /dev/null +++ b/pkg/libhelm/options/chart_reference_test.go @@ -0,0 +1,100 @@ +package options + +import ( + "testing" +) + +func TestConstructChartReference(t *testing.T) { + tests := []struct { + name string + registryURL string + chartName string + expected string + }{ + { + name: "empty registry URL returns chart name as-is", + registryURL: "", + chartName: "nginx", + expected: "nginx", + }, + { + name: "basic OCI registry with chart name", + registryURL: "registry.example.com", + chartName: "nginx", + expected: "oci://registry.example.com/nginx", + }, + { + name: "registry with project path", + registryURL: "harbor.example.com", + chartName: "library/nginx", + expected: "oci://harbor.example.com/library/nginx", + }, + { + name: "chart name already has oci prefix returns as-is", + registryURL: "registry.example.com", + chartName: "oci://registry.example.com/nginx", + expected: "oci://registry.example.com/nginx", + }, + { + name: "chart name with leading slash", + registryURL: "registry.example.com", + chartName: "/nginx", + expected: "oci://registry.example.com/nginx", + }, + { + name: "registry URL already has oci prefix", + registryURL: "oci://registry.example.com", + chartName: "nginx", + expected: "oci://registry.example.com/nginx", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := ConstructChartReference(tt.registryURL, tt.chartName) + if result != tt.expected { + t.Errorf("ConstructChartReference(%q, %q) = %q, want %q", + tt.registryURL, tt.chartName, result, tt.expected) + } + }) + } +} + +func TestConstructOCIRegistryReference(t *testing.T) { + tests := []struct { + name string + registryURL string + expected string + }{ + { + name: "simple registry URL", + registryURL: "registry.example.com", + expected: "oci://registry.example.com", + }, + { + name: "registry URL with oci prefix", + registryURL: "oci://registry.example.com", + expected: "oci://registry.example.com", + }, + { + name: "registry URL with port", + registryURL: "registry.example.com:5000", + expected: "oci://registry.example.com:5000", + }, + { + name: "empty registry URL", + registryURL: "", + expected: "oci://", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := ConstructOCIRegistryReference(tt.registryURL) + if result != tt.expected { + t.Errorf("ConstructOCIRegistryReference(%q) = %q, want %q", + tt.registryURL, result, tt.expected) + } + }) + } +} diff --git a/pkg/libhelm/options/install_options.go b/pkg/libhelm/options/install_options.go index 807862683..60b14cf0f 100644 --- a/pkg/libhelm/options/install_options.go +++ b/pkg/libhelm/options/install_options.go @@ -1,6 +1,10 @@ package options -import "time" +import ( + "time" + + portainer "github.com/portainer/portainer/api" +) type InstallOptions struct { Name string @@ -8,6 +12,7 @@ type InstallOptions struct { Version string Namespace string Repo string + Registry *portainer.Registry Wait bool ValuesFile string PostRenderer string diff --git a/pkg/libhelm/options/search_repo_options.go b/pkg/libhelm/options/search_repo_options.go index 0b35c0bbd..333c28c8b 100644 --- a/pkg/libhelm/options/search_repo_options.go +++ b/pkg/libhelm/options/search_repo_options.go @@ -1,10 +1,15 @@ package options -import "net/http" +import ( + "net/http" + + portainer "github.com/portainer/portainer/api" +) type SearchRepoOptions struct { Repo string `example:"https://charts.gitlab.io/"` Client *http.Client `example:"&http.Client{Timeout: time.Second * 10}"` Chart string `example:"my-chart"` UseCache bool `example:"false"` + Registry *portainer.Registry } diff --git a/pkg/libhelm/options/show_options.go b/pkg/libhelm/options/show_options.go index a715c6655..dadbab906 100644 --- a/pkg/libhelm/options/show_options.go +++ b/pkg/libhelm/options/show_options.go @@ -1,5 +1,7 @@ package options +import portainer "github.com/portainer/portainer/api" + // ShowOutputFormat is the format of the output of `helm show` type ShowOutputFormat string @@ -20,6 +22,6 @@ type ShowOptions struct { Chart string Repo string Version string - - Env []string + Env []string + Registry *portainer.Registry // Registry credentials for authentication } diff --git a/pkg/libhelm/release/release.go b/pkg/libhelm/release/release.go index 56888291c..bb49c0435 100644 --- a/pkg/libhelm/release/release.go +++ b/pkg/libhelm/release/release.go @@ -36,6 +36,8 @@ type Release struct { Manifest string `json:"manifest,omitempty"` // Hooks are all of the hooks declared for this release. Hooks []*Hook `json:"hooks,omitempty"` + // AppVersion is the app version of the release. + AppVersion string `json:"appVersion,omitempty"` // Version is an int which represents the revision of the release. Version int `json:"version,omitempty"` // Namespace is the kubernetes namespace of the release. @@ -43,6 +45,8 @@ type Release struct { // Labels of the release. // Disabled encoding into Json cause labels are stored in storage driver metadata field. Labels map[string]string `json:"-"` + // ChartReference are the labels that are used to identify the chart source. + ChartReference ChartReference `json:"chartReference,omitempty"` // Values are the values used to deploy the chart. Values Values `json:"values,omitempty"` } @@ -52,6 +56,12 @@ type Values struct { ComputedValues string `json:"computedValues,omitempty"` } +type ChartReference struct { + ChartPath string `json:"chartPath,omitempty"` + RepoURL string `json:"repoURL,omitempty"` + RegistryID int64 `json:"registryID,omitempty"` +} + // Chart is a helm package that contains metadata, a default config, zero or more // optionally parameterizable templates, and zero or more charts (dependencies). type Chart struct { diff --git a/pkg/libhelm/sdk/chartsources.go b/pkg/libhelm/sdk/chartsources.go new file mode 100644 index 000000000..c42196f3d --- /dev/null +++ b/pkg/libhelm/sdk/chartsources.go @@ -0,0 +1,297 @@ +package sdk + +// Helm Registry Client Caching Strategy +// +// This package implements a registry-based caching mechanism for Helm OCI registry clients +// to address rate limiting issues caused by repeated registry authentication. +// +// Key Design Decisions: +// +// 1. Cache Key Strategy: Registry ID +// - Uses portainer.RegistryID as the cache key instead of user sessions or URL+username +// - One cached client per registry per Portainer instance, regardless of users +// - Optimal for rate limiting: each registry only gets one login per Portainer instance +// - New users reuse existing cached clients rather than creating new ones +// +// 2. Cache Invalidation: Registry Change Events +// - Cache is flushed when registry credentials are updated (registryUpdate handler) +// - Cache is flushed when registry is reconfigured (registryConfigure handler) +// - Cache is flushed when registry is deleted (registryDelete handler) +// - Cache is flushed when registry authentication fails (show, install, upgrade) +// - No time-based expiration needed since registry credentials rarely change +// +// 3. Alternative Approaches NOT Used: +// - registry.ClientOptCredentialsFile(): Still requires token exchange on each client creation +// - User/session-based caching: Less efficient for rate limiting, creates unnecessary logins +// - URL+username caching: More complex, harder to invalidate, doesn't handle registry updates +// +// 4. Security Model: +// - RBAC security is enforced BEFORE reaching this caching layer (handler.getRegistryWithAccess) + +import ( + "strings" + + "github.com/pkg/errors" + portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/pkg/libhelm/cache" + "github.com/portainer/portainer/pkg/libhelm/options" + "github.com/rs/zerolog/log" + "helm.sh/helm/v3/pkg/action" + "helm.sh/helm/v3/pkg/registry" + "oras.land/oras-go/v2/registry/remote/retry" +) + +// IsOCIRegistry returns true if the registry is an OCI registry (not nil), false if it's an HTTP repository (nil) +func IsOCIRegistry(registry *portainer.Registry) bool { + return registry != nil +} + +// IsHTTPRepository returns true if it's an HTTP repository (registry is nil), false if it's an OCI registry +func IsHTTPRepository(registry *portainer.Registry) bool { + return registry == nil +} + +// parseChartRef parses chart and repo references based on the registry type +func parseChartRef(chart, repo string, registry *portainer.Registry) (string, string, error) { + if IsHTTPRepository(registry) { + return parseHTTPRepoChartRef(chart, repo) + } + return parseOCIChartRef(chart, registry) +} + +// parseOCIChartRef constructs the full OCI chart reference +func parseOCIChartRef(chart string, registry *portainer.Registry) (string, string, error) { + + chartRef := options.ConstructChartReference(registry.URL, chart) + + log.Debug(). + Str("context", "HelmClient"). + Str("chart_ref", chartRef). + Bool("authentication", registry.Authentication). + Msg("Constructed OCI chart reference") + + return chartRef, registry.URL, nil +} + +// parseHTTPRepoChartRef returns chart and repo as-is for HTTP repositories +func parseHTTPRepoChartRef(chart, repo string) (string, string, error) { + return chart, repo, nil +} + +// shouldFlushCacheOnError determines if a registry client should be removed from cache based on the error +// This helps handle cases where cached credentials have become invalid +func shouldFlushCacheOnError(err error, registryID portainer.RegistryID) bool { + if err == nil || registryID == 0 { + return false + } + + errorStr := strings.ToLower(err.Error()) + + // Authentication/authorization errors that indicate invalid cached credentials + authenticationErrors := []string{ + "unauthorized", + "authentication", + "login failed", + "invalid credentials", + "access denied", + "forbidden", + "401", + "403", + "token", + "auth", + } + + for _, authErr := range authenticationErrors { + if strings.Contains(errorStr, authErr) { + log.Info(). + Int("registry_id", int(registryID)). + Str("error", err.Error()). + Str("context", "HelmClient"). + Msg("Detected authentication error - will flush registry cache") + return true + } + } + + return false +} + +// authenticateChartSource handles both HTTP repositories and OCI registries +func authenticateChartSource(actionConfig *action.Configuration, registry *portainer.Registry) error { + // For HTTP repositories, no authentication needed (CE and EE) + if IsHTTPRepository(registry) { + return nil + } + + // If RegistryClient is already set, we're done + if actionConfig.RegistryClient != nil { + log.Debug(). + Str("context", "HelmClient"). + Msg("Registry client already set in action config") + return nil + } + + // Validate registry credentials first + err := validateRegistryCredentials(registry) + if err != nil { + log.Error(). + Str("context", "HelmClient"). + Err(err). + Msg("Registry credential validation failed") + return errors.Wrap(err, "registry credential validation failed") + } + + // No authentication required + if !registry.Authentication { + log.Debug(). + Str("context", "HelmClient"). + Msg("No OCI registry authentication required") + return nil + } + + // Cache Strategy Decision: Use registry ID as cache key + // This provides optimal rate limiting protection since each registry only gets + // logged into once per Portainer instance, regardless of how many users access it. + // RBAC security is enforced before reaching this caching layer. + // When a new user needs access, they reuse the same cached client. + // + // Alternative approach (NOT used): registry.ClientOptCredentialsFile() + // We don't use Helm's credential file approach because: + // 1. It still requires token exchange with registry on each new client creation + // 2. Rate limiting occurs during token exchange, not credential loading + // 3. Our caching approach reuses existing authenticated clients completely + // 4. Credential files add complexity without solving the core rate limiting issue + + // Try to get cached registry client (registry ID-based key) + if cachedClient, found := cache.GetCachedRegistryClientByID(registry.ID); found { + log.Debug(). + Int("registry_id", int(registry.ID)). + Str("registry_url", registry.URL). + Str("context", "HelmClient"). + Msg("Using cached registry client") + + actionConfig.RegistryClient = cachedClient + return nil + } + + // Cache miss - perform login and cache the result + log.Debug(). + Int("registry_id", int(registry.ID)). + Str("registry_url", registry.URL). + Str("context", "HelmClient"). + Msg("Cache miss - creating new registry client") + + registryClient, err := loginToOCIRegistry(registry) + if err != nil { + log.Error(). + Str("context", "HelmClient"). + Str("registry_url", registry.URL). + Err(err). + Msg("Failed to login to registry") + return errors.Wrap(err, "failed to login to registry") + } + + // Cache the client if login was successful (registry ID-based key) + if registryClient != nil { + cache.SetCachedRegistryClientByID(registry.ID, registryClient) + log.Debug(). + Int("registry_id", int(registry.ID)). + Str("registry_url", registry.URL). + Str("context", "HelmClient"). + Msg("Registry client cached successfully") + } + + actionConfig.RegistryClient = registryClient + return nil +} + +// configureChartPathOptions sets chart path options based on registry type +func configureChartPathOptions(chartPathOptions *action.ChartPathOptions, version, repo string, registry *portainer.Registry) error { + chartPathOptions.Version = version + // Set chart path options based on registry type + if IsHTTPRepository(registry) { + configureHTTPRepoChartPathOptions(chartPathOptions, repo) + } else { + configureOCIChartPathOptions(chartPathOptions, registry) + } + + return nil +} + +// configureHTTPRepoChartPathOptions sets chart path options for HTTP repositories +func configureHTTPRepoChartPathOptions(chartPathOptions *action.ChartPathOptions, repo string) { + chartPathOptions.RepoURL = repo +} + +// configureOCIChartPathOptions sets chart path options for OCI registries +func configureOCIChartPathOptions(chartPathOptions *action.ChartPathOptions, registry *portainer.Registry) { + if registry.Authentication { + chartPathOptions.Username = registry.Username + chartPathOptions.Password = registry.Password + } +} + +// loginToOCIRegistry performs registry login for OCI-based registries using Helm SDK +// Tries to get a cached registry client if available, otherwise creates and caches a new one +func loginToOCIRegistry(portainerRegistry *portainer.Registry) (*registry.Client, error) { + if IsHTTPRepository(portainerRegistry) || !portainerRegistry.Authentication { + return nil, nil // No authentication needed + } + + // Check cache first using registry ID-based key + if cachedClient, found := cache.GetCachedRegistryClientByID(portainerRegistry.ID); found { + return cachedClient, nil + } + + log.Debug(). + Str("context", "loginToRegistry"). + Int("registry_id", int(portainerRegistry.ID)). + Str("registry_url", portainerRegistry.URL). + Msg("Attempting to login to OCI registry") + + registryClient, err := registry.NewClient(registry.ClientOptHTTPClient(retry.DefaultClient)) + if err != nil { + return nil, errors.Wrap(err, "failed to create registry client") + } + + loginOpts := []registry.LoginOption{ + registry.LoginOptBasicAuth(portainerRegistry.Username, portainerRegistry.Password), + } + + err = registryClient.Login(portainerRegistry.URL, loginOpts...) + if err != nil { + return nil, errors.Wrapf(err, "failed to login to registry %s", portainerRegistry.URL) + } + + log.Debug(). + Str("context", "loginToRegistry"). + Int("registry_id", int(portainerRegistry.ID)). + Str("registry_url", portainerRegistry.URL). + Msg("Successfully logged in to OCI registry") + + // Cache using registry ID-based key + cache.SetCachedRegistryClientByID(portainerRegistry.ID, registryClient) + + return registryClient, nil +} + +// validateRegistryCredentials validates registry authentication settings +func validateRegistryCredentials(registry *portainer.Registry) error { + if IsHTTPRepository(registry) { + return nil // No registry means no validation needed + } + + if !registry.Authentication { + return nil // No authentication required + } + + // Authentication is enabled - validate credentials + if strings.TrimSpace(registry.Username) == "" { + return errors.New("username is required when registry authentication is enabled") + } + + if strings.TrimSpace(registry.Password) == "" { + return errors.New("password is required when registry authentication is enabled") + } + + return nil +} diff --git a/pkg/libhelm/sdk/chartsources_test.go b/pkg/libhelm/sdk/chartsources_test.go new file mode 100644 index 000000000..663601abc --- /dev/null +++ b/pkg/libhelm/sdk/chartsources_test.go @@ -0,0 +1,752 @@ +package sdk + +import ( + "strings" + "testing" + + "github.com/pkg/errors" + portainer "github.com/portainer/portainer/api" + helmregistrycache "github.com/portainer/portainer/pkg/libhelm/cache" + "github.com/stretchr/testify/assert" + "helm.sh/helm/v3/pkg/action" + "helm.sh/helm/v3/pkg/registry" +) + +func TestIsOCIRegistry(t *testing.T) { + t.Run("should return false for nil registry (HTTP repo)", func(t *testing.T) { + assert.False(t, IsOCIRegistry(nil)) + }) + + t.Run("should return true for non-nil registry (OCI registry)", func(t *testing.T) { + assert.True(t, IsOCIRegistry(&portainer.Registry{})) + }) +} + +func TestIsHTTPRepository(t *testing.T) { + t.Run("should return true for nil registry (HTTP repo)", func(t *testing.T) { + assert.True(t, IsHTTPRepository(nil)) + }) + + t.Run("should return false for non-nil registry (OCI registry)", func(t *testing.T) { + assert.False(t, IsHTTPRepository(&portainer.Registry{})) + }) +} + +func TestParseHTTPRepoChartRef(t *testing.T) { + is := assert.New(t) + + chartRef, repoURL, err := parseHTTPRepoChartRef("my-chart", "https://my.repo/charts") + + is.NoError(err) + is.Equal("my-chart", chartRef) + is.Equal("https://my.repo/charts", repoURL) +} + +func TestParseOCIChartRef(t *testing.T) { + is := assert.New(t) + + registry := &portainer.Registry{ + URL: "my-registry.io/my-namespace", + Authentication: true, + Username: "user", + Password: "pass", + } + + chartRef, repoURL, err := parseOCIChartRef("my-chart", registry) + + is.NoError(err) + is.Equal("oci://my-registry.io/my-namespace/my-chart", chartRef) + is.Equal("my-registry.io/my-namespace", repoURL) +} + +func TestParseOCIChartRef_GitLab(t *testing.T) { + is := assert.New(t) + + registry := &portainer.Registry{ + Type: portainer.GitlabRegistry, + URL: "registry.gitlab.com", + Authentication: true, + Username: "gitlab-ci-token", + Password: "glpat-xxxxxxxxxxxxxxxxxxxx", + Gitlab: portainer.GitlabRegistryData{ + ProjectID: 12345, + InstanceURL: "https://gitlab.com", + ProjectPath: "my-group/my-project", + }, + } + + chartRef, repoURL, err := parseOCIChartRef("my-chart", registry) + + is.NoError(err) + is.Equal("oci://registry.gitlab.com/my-chart", chartRef) + is.Equal("registry.gitlab.com", repoURL) +} + +func TestParseChartRef(t *testing.T) { + t.Run("should parse HTTP repo chart ref when registry is nil", func(t *testing.T) { + is := assert.New(t) + + chartRef, repoURL, err := parseChartRef("my-chart", "https://my.repo/charts", nil) + + is.NoError(err) + is.Equal("my-chart", chartRef) + is.Equal("https://my.repo/charts", repoURL) + }) + + t.Run("should parse OCI chart ref when registry is provided", func(t *testing.T) { + is := assert.New(t) + + registry := &portainer.Registry{ + URL: "my-registry.io/my-namespace", + Authentication: true, + Username: "user", + Password: "pass", + } + + chartRef, repoURL, err := parseChartRef("my-chart", "", registry) + + is.NoError(err) + is.Equal("oci://my-registry.io/my-namespace/my-chart", chartRef) + is.Equal("my-registry.io/my-namespace", repoURL) + }) +} + +func TestConfigureHTTPRepoChartPathOptions(t *testing.T) { + is := assert.New(t) + chartPathOptions := &action.ChartPathOptions{} + + configureHTTPRepoChartPathOptions(chartPathOptions, "https://my.repo/charts") + + is.Equal("https://my.repo/charts", chartPathOptions.RepoURL) +} + +func TestConfigureOCIChartPathOptions(t *testing.T) { + is := assert.New(t) + chartPathOptions := &action.ChartPathOptions{} + + registry := &portainer.Registry{ + URL: "my-registry.io/my-namespace", + Authentication: true, + Username: "user", + Password: "pass", + } + + configureOCIChartPathOptions(chartPathOptions, registry) + + is.Equal("user", chartPathOptions.Username) + is.Equal("pass", chartPathOptions.Password) +} + +func TestConfigureOCIChartPathOptions_NoAuth(t *testing.T) { + is := assert.New(t) + chartPathOptions := &action.ChartPathOptions{} + + registry := &portainer.Registry{ + URL: "my-registry.io/my-namespace", + Authentication: false, + } + + configureOCIChartPathOptions(chartPathOptions, registry) + + is.Empty(chartPathOptions.Username) + is.Empty(chartPathOptions.Password) +} + +func TestConfigureChartPathOptions(t *testing.T) { + t.Run("should configure HTTP repo when registry is nil", func(t *testing.T) { + is := assert.New(t) + chartPathOptions := &action.ChartPathOptions{} + + err := configureChartPathOptions(chartPathOptions, "1.0.0", "https://my.repo/charts", nil) + + is.NoError(err) + is.Equal("https://my.repo/charts", chartPathOptions.RepoURL) + is.Equal("1.0.0", chartPathOptions.Version) + }) + + t.Run("should configure OCI registry when registry is provided", func(t *testing.T) { + is := assert.New(t) + chartPathOptions := &action.ChartPathOptions{} + + registry := &portainer.Registry{ + URL: "my-registry.io/my-namespace", + Authentication: true, + Username: "user", + Password: "pass", + } + + err := configureChartPathOptions(chartPathOptions, "1.0.0", "", registry) + + is.NoError(err) + is.Equal("user", chartPathOptions.Username) + is.Equal("pass", chartPathOptions.Password) + is.Equal("1.0.0", chartPathOptions.Version) + }) +} + +func TestLoginToOCIRegistry(t *testing.T) { + is := assert.New(t) + + t.Run("should return nil for HTTP repository (nil registry)", func(t *testing.T) { + client, err := loginToOCIRegistry(nil) + is.NoError(err) + is.Nil(client) + }) + + t.Run("should return nil for registry with auth disabled", func(t *testing.T) { + registry := &portainer.Registry{ + URL: "my-registry.io", + Authentication: false, + } + client, err := loginToOCIRegistry(registry) + is.NoError(err) + is.Nil(client) + }) + + t.Run("should return error for invalid credentials", func(t *testing.T) { + registry := &portainer.Registry{ + URL: "my-registry.io", + Authentication: true, + Username: " ", + } + client, err := loginToOCIRegistry(registry) + is.Error(err) + is.Nil(client) + // The error might be a validation error or a login error, both are acceptable + is.True(err.Error() == "username is required when registry authentication is enabled" || + strings.Contains(err.Error(), "failed to login to registry")) + }) + + t.Run("should attempt login for valid credentials", func(t *testing.T) { + registry := &portainer.Registry{ + ID: 123, + URL: "my-registry.io", + Authentication: true, + Username: "user", + Password: "pass", + } + // this will fail because it can't connect to the registry, + // but it proves that the loginToOCIRegistry function is calling the login function. + client, err := loginToOCIRegistry(registry) + is.Error(err) + is.Nil(client) + is.Contains(err.Error(), "failed to login to registry") + }) + + t.Run("should attempt login for GitLab registry with valid credentials", func(t *testing.T) { + registry := &portainer.Registry{ + ID: 456, + Type: portainer.GitlabRegistry, + URL: "registry.gitlab.com", + Authentication: true, + Username: "gitlab-ci-token", + Password: "glpat-xxxxxxxxxxxxxxxxxxxx", + Gitlab: portainer.GitlabRegistryData{ + ProjectID: 12345, + InstanceURL: "https://gitlab.com", + ProjectPath: "my-group/my-project", + }, + } + // this will fail because it can't connect to the registry, + // but it proves that the loginToOCIRegistry function is calling the login function. + client, err := loginToOCIRegistry(registry) + is.Error(err) + is.Nil(client) + is.Contains(err.Error(), "failed to login to registry") + }) +} + +func TestAuthenticateChartSource(t *testing.T) { + t.Run("should do nothing for HTTP repo (nil registry)", func(t *testing.T) { + is := assert.New(t) + actionConfig := &action.Configuration{} + err := authenticateChartSource(actionConfig, nil) + is.NoError(err) + is.Nil(actionConfig.RegistryClient) + }) + + t.Run("should do nothing if registry client already set", func(t *testing.T) { + is := assert.New(t) + actionConfig := &action.Configuration{} + // Mock an existing registry client + existingClient := ®istry.Client{} + actionConfig.RegistryClient = existingClient + + registry := &portainer.Registry{ + ID: 123, + Authentication: true, + Username: "user", + Password: "pass", + } + + err := authenticateChartSource(actionConfig, registry) + is.NoError(err) + is.Equal(existingClient, actionConfig.RegistryClient) + }) + + t.Run("should authenticate OCI registry when registry is provided", func(t *testing.T) { + is := assert.New(t) + actionConfig := &action.Configuration{} + registry := &portainer.Registry{ + ID: 123, + Authentication: false, + } + err := authenticateChartSource(actionConfig, registry) + is.NoError(err) + }) + + t.Run("should return error for invalid registry credentials", func(t *testing.T) { + is := assert.New(t) + actionConfig := &action.Configuration{} + registry := &portainer.Registry{ + ID: 123, + Authentication: true, + Username: " ", // Invalid username + } + err := authenticateChartSource(actionConfig, registry) + is.Error(err) + is.Contains(err.Error(), "registry credential validation failed") + }) +} + +func TestGetRegistryClientFromCache(t *testing.T) { + // Initialize cache for testing + err := helmregistrycache.Initialize("24h") + if err != nil { + t.Fatalf("Failed to initialize cache: %v", err) + } + // Clear cache before each test + helmregistrycache.FlushAll() + + t.Run("should return nil for invalid registry ID", func(t *testing.T) { + is := assert.New(t) + client, found := helmregistrycache.GetCachedRegistryClientByID(0) + is.False(found) + is.Nil(client) + }) + + t.Run("should return nil for non-existent registry ID", func(t *testing.T) { + is := assert.New(t) + client, found := helmregistrycache.GetCachedRegistryClientByID(123) + is.False(found) + is.Nil(client) + }) + + t.Run("should return cached client for valid registry ID", func(t *testing.T) { + is := assert.New(t) + // Create a mock client + mockClient := ®istry.Client{} + + // Store in cache + helmregistrycache.SetCachedRegistryClientByID(123, mockClient) + + // Retrieve from cache + cachedClient, found := helmregistrycache.GetCachedRegistryClientByID(123) + is.True(found) + is.NotNil(cachedClient) + is.Equal(mockClient, cachedClient) + }) +} + +func TestSetRegistryClientInCache(t *testing.T) { + // Initialize cache for testing + err := helmregistrycache.Initialize("24h") + if err != nil { + t.Fatalf("Failed to initialize cache: %v", err) + } + // Clear cache before each test + helmregistrycache.FlushAll() + + t.Run("should store and retrieve client successfully", func(t *testing.T) { + is := assert.New(t) + // Create a mock client + client := ®istry.Client{} + + // Store in cache + helmregistrycache.SetCachedRegistryClientByID(123, client) + + // Verify the cache returns the client + cachedClient, found := helmregistrycache.GetCachedRegistryClientByID(123) + is.True(found) + is.NotNil(cachedClient) + is.Equal(client, cachedClient) + }) + + t.Run("should handle invalid parameters gracefully", func(t *testing.T) { + // Clear cache to start clean + helmregistrycache.FlushAll() + + // These should not panic + helmregistrycache.SetCachedRegistryClientByID(0, nil) // nil client should be rejected + helmregistrycache.SetCachedRegistryClientByID(999, ®istry.Client{}) // valid client with registry ID 999 should be accepted + helmregistrycache.SetCachedRegistryClientByID(123, nil) // nil client should be rejected + + // Verify that nil clients don't get stored, but valid clients do + is := assert.New(t) + + // Registry ID 999 with a valid client should be found (the second call above) + client, found := helmregistrycache.GetCachedRegistryClientByID(999) + is.True(found) + is.NotNil(client) + + // Registry ID 0 with nil client should not be found + client, found = helmregistrycache.GetCachedRegistryClientByID(0) + is.False(found) + is.Nil(client) + + // Registry ID 123 with nil client should not be found + client, found = helmregistrycache.GetCachedRegistryClientByID(123) + is.False(found) + is.Nil(client) + }) +} + +func TestFlushRegistryCache(t *testing.T) { + // Initialize cache for testing + err := helmregistrycache.Initialize("24h") + if err != nil { + t.Fatalf("Failed to initialize cache: %v", err) + } + // Clear cache before test + helmregistrycache.FlushAll() + + t.Run("should flush specific registry cache", func(t *testing.T) { + is := assert.New(t) + // Create mock clients + client1 := ®istry.Client{} + client2 := ®istry.Client{} + + // Store in cache + helmregistrycache.SetCachedRegistryClientByID(123, client1) + helmregistrycache.SetCachedRegistryClientByID(456, client2) + + // Verify both are cached + client, found := helmregistrycache.GetCachedRegistryClientByID(123) + is.True(found) + is.NotNil(client) + client, found = helmregistrycache.GetCachedRegistryClientByID(456) + is.True(found) + is.NotNil(client) + + // Flush only one + helmregistrycache.FlushRegistryByID(123) + + // Verify only one is flushed + client, found = helmregistrycache.GetCachedRegistryClientByID(123) + is.False(found) + is.Nil(client) + client, found = helmregistrycache.GetCachedRegistryClientByID(456) + is.True(found) + is.NotNil(client) + }) +} + +func TestFlushAllRegistryCache(t *testing.T) { + // Initialize cache for testing + err := helmregistrycache.Initialize("24h") + if err != nil { + t.Fatalf("Failed to initialize cache: %v", err) + } + + t.Run("should flush all registry cache", func(t *testing.T) { + is := assert.New(t) + // Create mock clients + client1 := ®istry.Client{} + client2 := ®istry.Client{} + + // Store in cache + helmregistrycache.SetCachedRegistryClientByID(123, client1) + helmregistrycache.SetCachedRegistryClientByID(456, client2) + + // Verify both are cached + client, found := helmregistrycache.GetCachedRegistryClientByID(123) + is.True(found) + is.NotNil(client) + client, found = helmregistrycache.GetCachedRegistryClientByID(456) + is.True(found) + is.NotNil(client) + + // Flush all + helmregistrycache.FlushAll() + + // Verify both are flushed + client, found = helmregistrycache.GetCachedRegistryClientByID(123) + is.False(found) + is.Nil(client) + client, found = helmregistrycache.GetCachedRegistryClientByID(456) + is.False(found) + is.Nil(client) + client, found = helmregistrycache.GetCachedRegistryClientByID(456) + is.False(found) + is.Nil(client) + }) +} + +func TestValidateRegistryCredentials(t *testing.T) { + tests := []struct { + name string + registry *portainer.Registry + expectError bool + errorMsg string + }{ + { + name: "nil registry should pass validation", + registry: nil, + expectError: false, + }, + { + name: "registry with authentication disabled should pass validation", + registry: &portainer.Registry{ + Authentication: false, + }, + expectError: false, + }, + { + name: "registry with authentication enabled and valid credentials should pass", + registry: &portainer.Registry{ + Authentication: true, + Username: "testuser", + Password: "testpass", + }, + expectError: false, + }, + { + name: "registry with authentication enabled but empty username should fail", + registry: &portainer.Registry{ + Authentication: true, + Username: "", + Password: "testpass", + }, + expectError: true, + errorMsg: "username is required when registry authentication is enabled", + }, + { + name: "registry with authentication enabled but whitespace username should fail", + registry: &portainer.Registry{ + Authentication: true, + Username: " ", + Password: "testpass", + }, + expectError: true, + errorMsg: "username is required when registry authentication is enabled", + }, + { + name: "registry with authentication enabled but empty password should fail", + registry: &portainer.Registry{ + Authentication: true, + Username: "testuser", + Password: "", + }, + expectError: true, + errorMsg: "password is required when registry authentication is enabled", + }, + { + name: "registry with authentication enabled but whitespace password should fail", + registry: &portainer.Registry{ + Authentication: true, + Username: "testuser", + Password: " ", + }, + expectError: true, + errorMsg: "password is required when registry authentication is enabled", + }, + { + name: "GitLab registry with authentication enabled and valid credentials should pass", + registry: &portainer.Registry{ + Type: portainer.GitlabRegistry, + Authentication: true, + Username: "gitlab-ci-token", + Password: "glpat-xxxxxxxxxxxxxxxxxxxx", + Gitlab: portainer.GitlabRegistryData{ + ProjectID: 12345, + InstanceURL: "https://gitlab.com", + ProjectPath: "my-group/my-project", + }, + }, + expectError: false, + }, + { + name: "GitLab registry with authentication enabled but empty username should fail", + registry: &portainer.Registry{ + Type: portainer.GitlabRegistry, + Authentication: true, + Username: "", + Password: "glpat-xxxxxxxxxxxxxxxxxxxx", + Gitlab: portainer.GitlabRegistryData{ + ProjectID: 12345, + InstanceURL: "https://gitlab.com", + ProjectPath: "my-group/my-project", + }, + }, + expectError: true, + errorMsg: "username is required when registry authentication is enabled", + }, + { + name: "GitLab registry with authentication enabled but empty password should fail", + registry: &portainer.Registry{ + Type: portainer.GitlabRegistry, + Authentication: true, + Username: "gitlab-ci-token", + Password: "", + Gitlab: portainer.GitlabRegistryData{ + ProjectID: 12345, + InstanceURL: "https://gitlab.com", + ProjectPath: "my-group/my-project", + }, + }, + expectError: true, + errorMsg: "password is required when registry authentication is enabled", + }, + { + name: "GitLab registry with authentication disabled should pass validation", + registry: &portainer.Registry{ + Type: portainer.GitlabRegistry, + Authentication: false, + Gitlab: portainer.GitlabRegistryData{ + ProjectID: 12345, + InstanceURL: "https://gitlab.com", + ProjectPath: "my-group/my-project", + }, + }, + expectError: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := validateRegistryCredentials(tt.registry) + + if tt.expectError { + assert.Error(t, err) + if err != nil { + assert.Equal(t, tt.errorMsg, err.Error()) + } + } else { + assert.NoError(t, err) + } + }) + } +} + +// Note: buildCacheKey function was removed since we now use registry ID-based caching +// instead of endpoint/session-based caching for better rate limiting protection + +func TestShouldFlushCacheOnError(t *testing.T) { + tests := []struct { + name string + err error + registryID portainer.RegistryID + shouldFlush bool + }{ + { + name: "nil error should not flush", + err: nil, + registryID: 123, + shouldFlush: false, + }, + { + name: "zero registry ID should not flush", + err: errors.New("some error"), + registryID: 0, + shouldFlush: false, + }, + { + name: "unauthorized error should flush", + err: errors.New("unauthorized access to registry"), + registryID: 123, + shouldFlush: true, + }, + { + name: "authentication failed error should flush", + err: errors.New("authentication failed"), + registryID: 123, + shouldFlush: true, + }, + { + name: "login failed error should flush", + err: errors.New("login failed for user"), + registryID: 123, + shouldFlush: true, + }, + { + name: "invalid credentials error should flush", + err: errors.New("invalid credentials provided"), + registryID: 123, + shouldFlush: true, + }, + { + name: "access denied error should flush", + err: errors.New("access denied to repository"), + registryID: 123, + shouldFlush: true, + }, + { + name: "forbidden error should flush", + err: errors.New("forbidden: insufficient permissions"), + registryID: 123, + shouldFlush: true, + }, + { + name: "401 error should flush", + err: errors.New("HTTP 401 Unauthorized"), + registryID: 123, + shouldFlush: true, + }, + { + name: "403 error should flush", + err: errors.New("HTTP 403 Forbidden"), + registryID: 123, + shouldFlush: true, + }, + { + name: "token error should flush", + err: errors.New("token expired or invalid"), + registryID: 123, + shouldFlush: true, + }, + { + name: "auth error should flush", + err: errors.New("auth validation failed"), + registryID: 123, + shouldFlush: true, + }, + { + name: "chart not found error should not flush", + err: errors.New("chart not found in repository"), + registryID: 123, + shouldFlush: false, + }, + { + name: "network error should not flush", + err: errors.New("connection timeout"), + registryID: 123, + shouldFlush: false, + }, + { + name: "helm validation error should not flush", + err: errors.New("invalid chart values"), + registryID: 123, + shouldFlush: false, + }, + { + name: "kubernetes error should not flush", + err: errors.New("namespace not found"), + registryID: 123, + shouldFlush: false, + }, + { + name: "case insensitive matching works", + err: errors.New("UNAUTHORIZED access denied"), + registryID: 123, + shouldFlush: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := shouldFlushCacheOnError(tt.err, tt.registryID) + is := assert.New(t) + is.Equal(tt.shouldFlush, result, "Expected shouldFlushCacheOnError to return %v for error: %v", tt.shouldFlush, tt.err) + }) + } +} diff --git a/pkg/libhelm/sdk/common.go b/pkg/libhelm/sdk/common.go index 2a51c7f8b..c831a69fa 100644 --- a/pkg/libhelm/sdk/common.go +++ b/pkg/libhelm/sdk/common.go @@ -1,24 +1,38 @@ package sdk import ( + "fmt" + "maps" + "net/url" "os" + "path/filepath" + "strconv" + "strings" "github.com/pkg/errors" + "github.com/portainer/portainer/pkg/libhelm/options" + "github.com/portainer/portainer/pkg/libhelm/release" "github.com/rs/zerolog/log" "helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/chart/loader" + "helm.sh/helm/v3/pkg/cli" "helm.sh/helm/v3/pkg/downloader" "helm.sh/helm/v3/pkg/getter" + "helm.sh/helm/v3/pkg/repo" +) + +// Helm chart reference label constants +const ( + ChartPathAnnotation = "portainer/chart-path" + RepoURLAnnotation = "portainer/repo-url" + RegistryIDAnnotation = "portainer/registry-id" ) // loadAndValidateChartWithPathOptions locates and loads the chart, and validates it. // it also checks for chart dependencies and updates them if necessary. // it returns the chart information. func (hspm *HelmSDKPackageManager) loadAndValidateChartWithPathOptions(chartPathOptions *action.ChartPathOptions, chartName, version string, repoURL string, dependencyUpdate bool, operation string) (*chart.Chart, error) { - // Locate and load the chart - chartPathOptions.RepoURL = repoURL - chartPathOptions.Version = version chartPath, err := chartPathOptions.LocateChart(chartName, hspm.settings) if err != nil { log.Error(). @@ -26,6 +40,11 @@ func (hspm *HelmSDKPackageManager) loadAndValidateChartWithPathOptions(chartPath Str("chart", chartName). Err(err). Msg("Failed to locate chart for helm " + operation) + + // For OCI charts, chartName already contains the full reference + if strings.HasPrefix(chartName, options.OCIProtocolPrefix) { + return nil, errors.Wrapf(err, "failed to find the helm chart: %s", chartName) + } return nil, errors.Wrapf(err, "failed to find the helm chart at the path: %s/%s", repoURL, chartName) } @@ -86,3 +105,186 @@ func (hspm *HelmSDKPackageManager) loadAndValidateChartWithPathOptions(chartPath return chartReq, nil } + +// parseRepoURL parses and validates a Helm repository URL using RFC 3986 standards. +// Used by search and show operations before downloading index.yaml files. +func parseRepoURL(repoURL string) (*url.URL, error) { + parsedURL, err := url.ParseRequestURI(repoURL) + if err != nil { + return nil, errors.Wrap(err, "invalid helm chart URL: "+repoURL) + } + return parsedURL, nil +} + +// getRepoNameFromURL generates a unique repository identifier from a URL. +// Combines hostname and path for uniqueness (e.g., "charts.helm.sh/stable" → "charts.helm.sh-stable"). +// Used for Helm's repositories.yaml entries, caching, and chart references. +func getRepoNameFromURL(urlStr string) (string, error) { + parsedURL, err := url.Parse(urlStr) + if err != nil { + return "", fmt.Errorf("failed to parse URL: %w", err) + } + + hostname := parsedURL.Hostname() + path := parsedURL.Path + path = strings.Trim(path, "/") + path = strings.ReplaceAll(path, "/", "-") + + if path == "" { + return hostname, nil + } + return fmt.Sprintf("%s-%s", hostname, path), nil +} + +// loadIndexFile loads and parses a Helm repository index.yaml file. +// Called after downloading from HTTP repos or generating from OCI registries. +// Contains chart metadata used for discovery, version resolution, and caching. +func loadIndexFile(indexPath string) (*repo.IndexFile, error) { + log.Debug(). + Str("context", "HelmClient"). + Str("index_path", indexPath). + Msg("Loading index file") + + indexFile, err := repo.LoadIndexFile(indexPath) + if err != nil { + log.Error(). + Str("context", "HelmClient"). + Str("index_path", indexPath). + Err(err). + Msg("Failed to load index file") + return nil, errors.Wrapf(err, "failed to load downloaded index file: %s", indexPath) + } + return indexFile, nil +} + +// ensureHelmDirectoriesExist creates required Helm directories and configuration files. +// Creates repository cache, config directories, and ensures repositories.yaml exists. +// Essential for Helm operations to function properly. +func ensureHelmDirectoriesExist(settings *cli.EnvSettings) error { + log.Debug(). + Str("context", "helm_sdk_dirs"). + Msg("Ensuring Helm directories exist") + + // List of directories to ensure exist + directories := []string{ + filepath.Dir(settings.RepositoryConfig), // Repository config directory + settings.RepositoryCache, // Repository cache directory + filepath.Dir(settings.RegistryConfig), // Registry config directory + settings.PluginsDirectory, // Plugins directory + } + + // Create each directory if it doesn't exist + for _, dir := range directories { + if dir == "" { + continue // Skip empty paths + } + + if _, err := os.Stat(dir); os.IsNotExist(err) { + if err := os.MkdirAll(dir, 0700); err != nil { + log.Error(). + Str("context", "helm_sdk_dirs"). + Str("directory", dir). + Err(err). + Msg("Failed to create directory") + return errors.Wrapf(err, "failed to create directory: %s", dir) + } + } + } + + // Ensure registry config file exists + if settings.RegistryConfig != "" { + if _, err := os.Stat(settings.RegistryConfig); os.IsNotExist(err) { + // Create the directory if it doesn't exist + dir := filepath.Dir(settings.RegistryConfig) + if err := os.MkdirAll(dir, 0700); err != nil { + log.Error(). + Str("context", "helm_sdk_dirs"). + Str("directory", dir). + Err(err). + Msg("Failed to create directory") + return errors.Wrapf(err, "failed to create directory: %s", dir) + } + + // Create an empty registry config file + if _, err := os.Create(settings.RegistryConfig); err != nil { + log.Error(). + Str("context", "helm_sdk_dirs"). + Str("file", settings.RegistryConfig). + Err(err). + Msg("Failed to create registry config file") + return errors.Wrapf(err, "failed to create registry config file: %s", settings.RegistryConfig) + } + } + } + + // Ensure repository config file exists + if settings.RepositoryConfig != "" { + if _, err := os.Stat(settings.RepositoryConfig); os.IsNotExist(err) { + // Create an empty repository config file with default yaml structure + f := repo.NewFile() + if err := f.WriteFile(settings.RepositoryConfig, 0644); err != nil { + log.Error(). + Str("context", "helm_sdk_dirs"). + Str("file", settings.RepositoryConfig). + Err(err). + Msg("Failed to create repository config file") + return errors.Wrapf(err, "failed to create repository config file: %s", settings.RepositoryConfig) + } + } + } + + log.Debug(). + Str("context", "helm_sdk_dirs"). + Msg("Successfully ensured all Helm directories exist") + + return nil +} + +// appendChartReferenceAnnotations encodes chart reference values for safe storage in Helm labels. +// It creates a new map with encoded values for specific chart reference labels. +// Preserves existing labels and handles edge cases gracefully. +func appendChartReferenceAnnotations(chartPath, repoURL string, registryID int, existingAnnotations map[string]string) map[string]string { + // Copy existing annotations + annotations := make(map[string]string) + maps.Copy(annotations, existingAnnotations) + + // delete the existing portainer specific labels, for a clean overwrite + delete(annotations, ChartPathAnnotation) + delete(annotations, RepoURLAnnotation) + delete(annotations, RegistryIDAnnotation) + + if chartPath != "" { + annotations[ChartPathAnnotation] = chartPath + } + + if repoURL != "" && registryID == 0 { + annotations[RepoURLAnnotation] = repoURL + } + + if registryID != 0 { + annotations[RegistryIDAnnotation] = strconv.Itoa(registryID) + } + + return annotations +} + +// extractChartReferenceAnnotations decodes chart reference labels for display purposes. +// It handles existing labels gracefully and only decodes known chart reference labels. +// If a chart reference label cannot be decoded, it is omitted entirely from the result. +// Returns a ChartReference struct with decoded values. +func extractChartReferenceAnnotations(annotations map[string]string) release.ChartReference { + if annotations == nil { + return release.ChartReference{} + } + + registryID, err := strconv.Atoi(annotations[RegistryIDAnnotation]) + if err != nil { + registryID = 0 + } + + return release.ChartReference{ + ChartPath: annotations[ChartPathAnnotation], + RepoURL: annotations[RepoURLAnnotation], + RegistryID: int64(registryID), + } +} diff --git a/pkg/libhelm/sdk/get.go b/pkg/libhelm/sdk/get.go index c15b65715..e252ce7a6 100644 --- a/pkg/libhelm/sdk/get.go +++ b/pkg/libhelm/sdk/get.go @@ -97,6 +97,7 @@ func convert(sdkRelease *sdkrelease.Release, values release.Values) *release.Rel AppVersion: sdkRelease.Chart.Metadata.AppVersion, }, }, - Values: values, + Values: values, + ChartReference: extractChartReferenceAnnotations(sdkRelease.Chart.Metadata.Annotations), } } diff --git a/pkg/libhelm/sdk/install.go b/pkg/libhelm/sdk/install.go index ca904dcca..08c563559 100644 --- a/pkg/libhelm/sdk/install.go +++ b/pkg/libhelm/sdk/install.go @@ -4,6 +4,7 @@ import ( "time" "github.com/pkg/errors" + "github.com/portainer/portainer/pkg/libhelm/cache" "github.com/portainer/portainer/pkg/libhelm/options" "github.com/portainer/portainer/pkg/libhelm/release" "github.com/rs/zerolog/log" @@ -42,6 +43,12 @@ func (hspm *HelmSDKPackageManager) install(installOpts options.InstallOptions) ( return nil, errors.Wrap(err, "failed to initialize helm configuration for helm release installation") } + // Setup chart source + err = authenticateChartSource(actionConfig, installOpts.Registry) + if err != nil { + return nil, errors.Wrap(err, "failed to setup chart source for helm release installation") + } + installClient, err := initInstallClient(actionConfig, installOpts) if err != nil { log.Error(). @@ -51,7 +58,7 @@ func (hspm *HelmSDKPackageManager) install(installOpts options.InstallOptions) ( return nil, errors.Wrap(err, "failed to initialize helm install client for helm release installation") } - values, err := hspm.GetHelmValuesFromFile(installOpts.ValuesFile) + values, err := hspm.getHelmValuesFromFile(installOpts.ValuesFile) if err != nil { log.Error(). Str("context", "HelmClient"). @@ -60,15 +67,36 @@ func (hspm *HelmSDKPackageManager) install(installOpts options.InstallOptions) ( return nil, errors.Wrap(err, "failed to get Helm values from file for helm release installation") } - chart, err := hspm.loadAndValidateChartWithPathOptions(&installClient.ChartPathOptions, installOpts.Chart, installOpts.Version, installOpts.Repo, installClient.DependencyUpdate, "release installation") + chartRef, repoURL, err := parseChartRef(installOpts.Chart, installOpts.Repo, installOpts.Registry) + if err != nil { + return nil, errors.Wrap(err, "failed to parse chart reference for helm release installation") + } + chart, err := hspm.loadAndValidateChartWithPathOptions(&installClient.ChartPathOptions, chartRef, installOpts.Version, repoURL, installClient.DependencyUpdate, "release installation") if err != nil { log.Error(). Str("context", "HelmClient"). Err(err). Msg("Failed to load and validate chart for helm release installation") + + // Check if this is an authentication error and flush cache if needed + if installOpts.Registry != nil && shouldFlushCacheOnError(err, installOpts.Registry.ID) { + cache.FlushRegistryByID(installOpts.Registry.ID) + log.Info(). + Int("registry_id", int(installOpts.Registry.ID)). + Str("context", "HelmClient"). + Msg("Flushed registry cache due to chart loading authentication error during install") + } + return nil, errors.Wrap(err, "failed to load and validate chart for helm release installation") } + // Add chart references to annotations + var registryID int + if installOpts.Registry != nil { + registryID = int(installOpts.Registry.ID) + } + chart.Metadata.Annotations = appendChartReferenceAnnotations(installOpts.Chart, installOpts.Repo, registryID, chart.Metadata.Annotations) + // Run the installation log.Info(). Str("context", "HelmClient"). @@ -76,7 +104,6 @@ func (hspm *HelmSDKPackageManager) install(installOpts options.InstallOptions) ( Str("name", installOpts.Name). Str("namespace", installOpts.Namespace). Msg("Running chart installation for helm release") - helmRelease, err := installClient.Run(chart, values) if err != nil { log.Error(). @@ -94,9 +121,10 @@ func (hspm *HelmSDKPackageManager) install(installOpts options.InstallOptions) ( Namespace: helmRelease.Namespace, Chart: release.Chart{ Metadata: &release.Metadata{ - Name: helmRelease.Chart.Metadata.Name, - Version: helmRelease.Chart.Metadata.Version, - AppVersion: helmRelease.Chart.Metadata.AppVersion, + Name: helmRelease.Chart.Metadata.Name, + Version: helmRelease.Chart.Metadata.Version, + AppVersion: helmRelease.Chart.Metadata.AppVersion, + Annotations: helmRelease.Chart.Metadata.Annotations, }, }, Labels: helmRelease.Labels, @@ -111,13 +139,17 @@ func initInstallClient(actionConfig *action.Configuration, installOpts options.I installClient := action.NewInstall(actionConfig) installClient.DependencyUpdate = true installClient.ReleaseName = installOpts.Name - installClient.ChartPathOptions.RepoURL = installOpts.Repo installClient.Wait = installOpts.Wait installClient.Timeout = installOpts.Timeout + installClient.Version = installOpts.Version + err := configureChartPathOptions(&installClient.ChartPathOptions, installOpts.Version, installOpts.Repo, installOpts.Registry) + if err != nil { + return nil, errors.Wrap(err, "failed to configure chart path options for helm release installation") + } // Set default values if not specified if installOpts.Timeout == 0 { - installClient.Timeout = 5 * time.Minute + installClient.Timeout = 15 * time.Minute // set a bigger timeout for large charts } else { installClient.Timeout = installOpts.Timeout } diff --git a/pkg/libhelm/sdk/search_repo.go b/pkg/libhelm/sdk/search_repo.go index 63015330c..42011d4ae 100644 --- a/pkg/libhelm/sdk/search_repo.go +++ b/pkg/libhelm/sdk/search_repo.go @@ -1,24 +1,30 @@ package sdk import ( - "net/url" - "os" + "context" + "fmt" + "io" "path/filepath" + "strings" "sync" "time" + ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/pkg/errors" + portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/pkg/libhelm/options" + "github.com/portainer/portainer/pkg/liboras" "github.com/rs/zerolog/log" "github.com/segmentio/encoding/json" + "helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/cli" "helm.sh/helm/v3/pkg/getter" "helm.sh/helm/v3/pkg/repo" + "oras.land/oras-go/v2/registry" ) var ( errRequiredSearchOptions = errors.New("repo is required") - errInvalidRepoURL = errors.New("the request failed since either the Helm repository was not found or the index.yaml is not valid") ) type RepoIndex struct { @@ -40,7 +46,6 @@ var ( // SearchRepo downloads the `index.yaml` file for specified repo, parses it and returns JSON to caller. func (hspm *HelmSDKPackageManager) SearchRepo(searchRepoOpts options.SearchRepoOptions) ([]byte, error) { - // Validate input options if err := validateSearchRepoOptions(searchRepoOpts); err != nil { log.Error(). Str("context", "HelmClient"). @@ -55,33 +60,8 @@ func (hspm *HelmSDKPackageManager) SearchRepo(searchRepoOpts options.SearchRepoO Str("repo", searchRepoOpts.Repo). Msg("Searching repository") - // Parse and validate the repository URL - repoURL, err := parseRepoURL(searchRepoOpts.Repo) - if err != nil { - log.Error(). - Str("context", "HelmClient"). - Str("repo", searchRepoOpts.Repo). - Err(err). - Msg("Invalid repository URL") - return nil, err - } - - // Check cache first - if searchRepoOpts.UseCache { - cacheMutex.RLock() - if cached, exists := indexCache[repoURL.String()]; exists { - if time.Since(cached.Timestamp) < cacheDuration { - cacheMutex.RUnlock() - return convertAndMarshalIndex(cached.Index, searchRepoOpts.Chart) - } - } - cacheMutex.RUnlock() - } - // Set up Helm CLI environment repoSettings := cli.New() - - // Ensure all required Helm directories exist if err := ensureHelmDirectoriesExist(repoSettings); err != nil { log.Error(). Str("context", "HelmClient"). @@ -90,70 +70,145 @@ func (hspm *HelmSDKPackageManager) SearchRepo(searchRepoOpts options.SearchRepoO return nil, errors.Wrap(err, "failed to ensure Helm directories exist") } - // Download the index file and update repository configuration - indexPath, err := downloadRepoIndex(repoURL.String(), repoSettings, searchRepoOpts.Repo) + // Try cache first for HTTP repos + if IsHTTPRepository(searchRepoOpts.Registry) && searchRepoOpts.UseCache { + if cachedResult := hspm.tryGetFromCache(searchRepoOpts.Repo, searchRepoOpts.Chart); cachedResult != nil { + return cachedResult, nil + } + } + + // Download index based on source type + indexFile, err := hspm.downloadRepoIndex(searchRepoOpts, repoSettings) if err != nil { - log.Error(). - Str("context", "HelmClient"). - Str("repo_url", repoURL.String()). - Err(err). - Msg("Failed to download repository index") return nil, err } - // Load and parse the index file - log.Debug(). - Str("context", "HelmClient"). - Str("index_path", indexPath). - Msg("Loading index file") - - indexFile, err := loadIndexFile(indexPath) - if err != nil { - log.Error(). - Str("context", "HelmClient"). - Str("index_path", indexPath). - Err(err). - Msg("Failed to load index file") - return nil, err + // Update cache for HTTP repos + if IsHTTPRepository(searchRepoOpts.Registry) { + hspm.updateCache(searchRepoOpts.Repo, indexFile) } - // Update cache and remove old entries + return convertAndMarshalIndex(indexFile, searchRepoOpts.Chart) +} + +// tryGetFromCache attempts to retrieve a cached index file and convert it to the response format +func (hspm *HelmSDKPackageManager) tryGetFromCache(repoURL, chartName string) []byte { + cacheMutex.RLock() + defer cacheMutex.RUnlock() + + if cached, exists := indexCache[repoURL]; exists { + if time.Since(cached.Timestamp) < cacheDuration { + result, err := convertAndMarshalIndex(cached.Index, chartName) + if err != nil { + log.Debug(). + Str("context", "HelmClient"). + Str("repo", repoURL). + Err(err). + Msg("Failed to convert cached index") + return nil + } + return result + } + } + return nil +} + +// updateCache updates the cache with the provided index file and cleans up expired entries +func (hspm *HelmSDKPackageManager) updateCache(repoURL string, indexFile *repo.IndexFile) { cacheMutex.Lock() - indexCache[searchRepoOpts.Repo] = RepoIndexCache{ + defer cacheMutex.Unlock() + + indexCache[repoURL] = RepoIndexCache{ Index: indexFile, Timestamp: time.Now(), } + + // Clean up expired entries for key, index := range indexCache { if time.Since(index.Timestamp) > cacheDuration { delete(indexCache, key) } } +} - cacheMutex.Unlock() +// downloadRepoIndex downloads the repository index based on the source type (HTTP or OCI) +func (hspm *HelmSDKPackageManager) downloadRepoIndex(opts options.SearchRepoOptions, repoSettings *cli.EnvSettings) (*repo.IndexFile, error) { + if IsOCIRegistry(opts.Registry) { + return hspm.downloadOCIRepoIndex(opts.Registry, repoSettings, opts.Chart) + } + return hspm.downloadHTTPRepoIndex(opts.Repo, repoSettings) +} - return convertAndMarshalIndex(indexFile, searchRepoOpts.Chart) +// downloadHTTPRepoIndex downloads and loads an index file from an HTTP repository +func (hspm *HelmSDKPackageManager) downloadHTTPRepoIndex(repoURL string, repoSettings *cli.EnvSettings) (*repo.IndexFile, error) { + parsedURL, err := parseRepoURL(repoURL) + if err != nil { + log.Error(). + Str("context", "HelmClient"). + Str("repo", repoURL). + Err(err). + Msg("Invalid repository URL") + return nil, err + } + + repoName, err := getRepoNameFromURL(parsedURL.String()) + if err != nil { + log.Error(). + Str("context", "HelmClient"). + Err(err). + Msg("Failed to get hostname from URL") + return nil, err + } + + indexPath, err := downloadRepoIndexFromHttpRepo(parsedURL.String(), repoSettings, repoName) + if err != nil { + log.Error(). + Str("context", "HelmClient"). + Str("repo_url", parsedURL.String()). + Err(err). + Msg("Failed to download repository index") + return nil, err + } + + return loadIndexFile(indexPath) +} + +// downloadOCIRepoIndex downloads and loads an index file from an OCI registry +func (hspm *HelmSDKPackageManager) downloadOCIRepoIndex(registry *portainer.Registry, repoSettings *cli.EnvSettings, chartPath string) (*repo.IndexFile, error) { + // Validate registry credentials first + if err := validateRegistryCredentials(registry); err != nil { + log.Error(). + Str("context", "HelmClient"). + Str("repo", registry.URL). + Err(err). + Msg("Registry credential validation failed for OCI search") + return nil, fmt.Errorf("registry credential validation failed: %w", err) + } + + indexPath, err := downloadRepoIndexFromOciRegistry(registry, repoSettings, chartPath) + if err != nil { + log.Error(). + Str("context", "HelmClient"). + Str("repo", registry.URL). + Err(err). + Msg("Failed to download repository index") + return nil, err + } + + return loadIndexFile(indexPath) } // validateSearchRepoOptions validates the required search repository options. func validateSearchRepoOptions(opts options.SearchRepoOptions) error { - if opts.Repo == "" { + if opts.Repo == "" && IsHTTPRepository(opts.Registry) { return errRequiredSearchOptions } return nil } -// parseRepoURL parses and validates the repository URL. -func parseRepoURL(repoURL string) (*url.URL, error) { - parsedURL, err := url.ParseRequestURI(repoURL) - if err != nil { - return nil, errors.Wrap(err, "invalid helm chart URL: "+repoURL) - } - return parsedURL, nil -} - -// downloadRepoIndex downloads the index.yaml file from the repository and updates +// downloadRepoIndexFromHttpRepo downloads the index.yaml file from the repository and updates // the repository configuration. -func downloadRepoIndex(repoURLString string, repoSettings *cli.EnvSettings, repoName string) (string, error) { +func downloadRepoIndexFromHttpRepo(repoURLString string, repoSettings *cli.EnvSettings, repoName string) (string, error) { log.Debug(). Str("context", "helm_sdk_repo_index"). Str("repo_url", repoURLString). @@ -163,7 +218,8 @@ func downloadRepoIndex(repoURLString string, repoSettings *cli.EnvSettings, repo // Create chart repository object rep, err := repo.NewChartRepository( &repo.Entry{ - URL: repoURLString, + Name: repoName, + URL: repoURLString, }, getter.All(repoSettings), ) @@ -173,7 +229,7 @@ func downloadRepoIndex(repoURLString string, repoSettings *cli.EnvSettings, repo Str("repo_url", repoURLString). Err(err). Msg("Failed to create chart repository object") - return "", errInvalidRepoURL + return "", errors.New("the request failed since either the Helm repository was not found or the index.yaml is not valid") } // Load repository configuration file @@ -229,13 +285,168 @@ func downloadRepoIndex(repoURLString string, repoSettings *cli.EnvSettings, repo return indexPath, nil } -// loadIndexFile loads the index file from the given path. -func loadIndexFile(indexPath string) (*repo.IndexFile, error) { - indexFile, err := repo.LoadIndexFile(indexPath) - if err != nil { - return nil, errors.Wrapf(err, "failed to load downloaded index file: %s", indexPath) +func downloadRepoIndexFromOciRegistry(registry *portainer.Registry, repoSettings *cli.EnvSettings, chartPath string) (string, error) { + if IsHTTPRepository(registry) { + return "", errors.New("registry information is required for OCI search") } - return indexFile, nil + + if chartPath == "" { + return "", errors.New("chart path is required for OCI search") + } + + ctx := context.Background() + + registryClient, err := liboras.CreateClient(*registry) + if err != nil { + log.Error(). + Str("context", "helm_sdk_repo_index_oci"). + Str("registry_url", registry.URL). + Err(err). + Msg("Failed to create ORAS registry client") + return "", errors.Wrap(err, "failed to create ORAS registry client") + } + + // Obtain repository handle for the specific chart path (relative to registry host) + repository, err := registryClient.Repository(ctx, chartPath) + if err != nil { + log.Error(). + Str("context", "helm_sdk_repo_index_oci"). + Str("repository", chartPath). + Err(err). + Msg("Failed to obtain repository handle") + return "", errors.Wrap(err, "failed to obtain repository handle") + } + + // List all tags for this chart repository + var tags []string + err = repository.Tags(ctx, "", func(t []string) error { + tags = append(tags, t...) + return nil + }) + if err != nil { + log.Error(). + Str("context", "helm_sdk_repo_index_oci"). + Str("repository", chartPath). + Err(err). + Msg("Failed to list tags") + return "", errors.Wrap(err, "failed to list tags for repository") + } + + if len(tags) == 0 { + return "", errors.Errorf("no tags found for repository %s", chartPath) + } + + // Build Helm index file in memory + indexFile := repo.NewIndexFile() + + const helmConfigMediaType = "application/vnd.cncf.helm.config.v1+json" + + for _, tag := range tags { + chartVersion, err := processOCITag(ctx, repository, registry, chartPath, tag, helmConfigMediaType) + if err != nil { + log.Debug(). + Str("context", "helm_sdk_repo_index_oci"). + Str("repository", chartPath). + Str("tag", tag). + Err(err). + Msg("Failed to process tag; skipping") + continue + } + + if chartVersion != nil { + indexFile.Entries[chartVersion.Name] = append(indexFile.Entries[chartVersion.Name], chartVersion) + } + } + + if len(indexFile.Entries) == 0 { + return "", errors.Errorf("no helm chart versions found for repository %s", chartPath) + } + + indexFile.SortEntries() + + fileNameSafe := strings.ReplaceAll(chartPath, "/", "-") + destPath := filepath.Join(repoSettings.RepositoryCache, fmt.Sprintf("%s-%d-index.yaml", fileNameSafe, time.Now().UnixNano())) + + if err := indexFile.WriteFile(destPath, 0644); err != nil { + return "", errors.Wrap(err, "failed to write OCI index file") + } + + log.Debug(). + Str("context", "helm_sdk_repo_index_oci"). + Str("dest_path", destPath). + Int("entries", len(indexFile.Entries)). + Msg("Successfully generated OCI index file") + + return destPath, nil +} + +// processOCITag processes a single OCI tag and returns a Helm chart version. +func processOCITag(ctx context.Context, repository registry.Repository, registry *portainer.Registry, chartPath string, tag string, helmConfigMediaType string) (*repo.ChartVersion, error) { + // Resolve tag to get descriptor + descriptor, err := repository.Resolve(ctx, tag) + if err != nil { + log.Debug(). + Str("context", "helm_sdk_repo_index_oci"). + Str("repository", chartPath). + Str("tag", tag). + Err(err). + Msg("Failed to resolve tag; skipping") + return nil, nil + } + + // Fetch manifest to validate media type and obtain config descriptor + manifestReader, err := repository.Manifests().Fetch(ctx, descriptor) + if err != nil { + log.Debug(). + Str("context", "helm_sdk_repo_index_oci"). + Str("repository", chartPath). + Str("tag", tag). + Err(err). + Msg("Failed to fetch manifest; skipping") + return nil, nil + } + + manifestContent, err := io.ReadAll(manifestReader) + manifestReader.Close() + if err != nil { + return nil, nil + } + + var manifest ocispec.Manifest + if err := json.Unmarshal(manifestContent, &manifest); err != nil { + return nil, nil + } + + // Ensure manifest config is Helm chart metadata + if manifest.Config.MediaType != helmConfigMediaType { + return nil, nil + } + + // Fetch config blob (chart metadata) + cfgReader, err := repository.Blobs().Fetch(ctx, manifest.Config) + if err != nil { + return nil, nil + } + cfgBytes, err := io.ReadAll(cfgReader) + cfgReader.Close() + if err != nil { + return nil, nil + } + + var metadata chart.Metadata + if err := json.Unmarshal(cfgBytes, &metadata); err != nil { + return nil, nil + } + + // Build chart version entry + chartVersion := &repo.ChartVersion{ + Metadata: &metadata, + URLs: []string{fmt.Sprintf("oci://%s/%s:%s", registry.URL, chartPath, tag)}, + Created: time.Now(), + Digest: descriptor.Digest.String(), + } + + return chartVersion, nil } // convertIndexToResponse converts the Helm index file to our response format. @@ -248,7 +459,7 @@ func convertIndexToResponse(indexFile *repo.IndexFile, chartName string) (RepoIn // Convert Helm SDK types to our response types for name, charts := range indexFile.Entries { - if chartName == "" || name == chartName { + if chartName == "" || strings.Contains(strings.ToLower(chartName), strings.ToLower(name)) { result.Entries[name] = convertChartsToChartInfo(charts) } } @@ -294,87 +505,6 @@ type ChartInfo struct { Annotations any `json:"annotations,omitempty"` } -// ensureHelmDirectoriesExist checks and creates required Helm directories if they don't exist -func ensureHelmDirectoriesExist(settings *cli.EnvSettings) error { - log.Debug(). - Str("context", "helm_sdk_dirs"). - Msg("Ensuring Helm directories exist") - - // List of directories to ensure exist - directories := []string{ - filepath.Dir(settings.RepositoryConfig), // Repository config directory - settings.RepositoryCache, // Repository cache directory - filepath.Dir(settings.RegistryConfig), // Registry config directory - settings.PluginsDirectory, // Plugins directory - } - - // Create each directory if it doesn't exist - for _, dir := range directories { - if dir == "" { - continue // Skip empty paths - } - - if _, err := os.Stat(dir); os.IsNotExist(err) { - if err := os.MkdirAll(dir, 0700); err != nil { - log.Error(). - Str("context", "helm_sdk_dirs"). - Str("directory", dir). - Err(err). - Msg("Failed to create directory") - return errors.Wrapf(err, "failed to create directory: %s", dir) - } - } - } - - // Ensure registry config file exists - if settings.RegistryConfig != "" { - if _, err := os.Stat(settings.RegistryConfig); os.IsNotExist(err) { - // Create the directory if it doesn't exist - dir := filepath.Dir(settings.RegistryConfig) - if err := os.MkdirAll(dir, 0700); err != nil { - log.Error(). - Str("context", "helm_sdk_dirs"). - Str("directory", dir). - Err(err). - Msg("Failed to create directory") - return errors.Wrapf(err, "failed to create directory: %s", dir) - } - - // Create an empty registry config file - if _, err := os.Create(settings.RegistryConfig); err != nil { - log.Error(). - Str("context", "helm_sdk_dirs"). - Str("file", settings.RegistryConfig). - Err(err). - Msg("Failed to create registry config file") - return errors.Wrapf(err, "failed to create registry config file: %s", settings.RegistryConfig) - } - } - } - - // Ensure repository config file exists - if settings.RepositoryConfig != "" { - if _, err := os.Stat(settings.RepositoryConfig); os.IsNotExist(err) { - // Create an empty repository config file with default yaml structure - f := repo.NewFile() - if err := f.WriteFile(settings.RepositoryConfig, 0644); err != nil { - log.Error(). - Str("context", "helm_sdk_dirs"). - Str("file", settings.RepositoryConfig). - Err(err). - Msg("Failed to create repository config file") - return errors.Wrapf(err, "failed to create repository config file: %s", settings.RepositoryConfig) - } - } - } - - log.Debug(). - Str("context", "helm_sdk_dirs"). - Msg("Successfully ensured all Helm directories exist") - - return nil -} - func convertAndMarshalIndex(indexFile *repo.IndexFile, chartName string) ([]byte, error) { // Convert the index file to our response format result, err := convertIndexToResponse(indexFile, chartName) diff --git a/pkg/libhelm/sdk/show.go b/pkg/libhelm/sdk/show.go index 4dc9c402d..70e97e364 100644 --- a/pkg/libhelm/sdk/show.go +++ b/pkg/libhelm/sdk/show.go @@ -2,20 +2,20 @@ package sdk import ( "fmt" - "os" "github.com/pkg/errors" + "github.com/portainer/portainer/pkg/libhelm/cache" "github.com/portainer/portainer/pkg/libhelm/options" "github.com/rs/zerolog/log" "helm.sh/helm/v3/pkg/action" ) -var errRequiredShowOptions = errors.New("chart, repo and output format are required") +var errRequiredShowOptions = errors.New("chart, output format and either repo or registry are required") // Show implements the HelmPackageManager interface by using the Helm SDK to show chart information. // It supports showing chart values, readme, and chart details based on the provided ShowOptions. func (hspm *HelmSDKPackageManager) Show(showOpts options.ShowOptions) ([]byte, error) { - if showOpts.Chart == "" || showOpts.Repo == "" || showOpts.OutputFormat == "" { + if showOpts.Chart == "" || (showOpts.Repo == "" && IsHTTPRepository(showOpts.Registry)) || showOpts.OutputFormat == "" { log.Error(). Str("context", "HelmClient"). Str("chart", showOpts.Chart). @@ -32,25 +32,12 @@ func (hspm *HelmSDKPackageManager) Show(showOpts options.ShowOptions) ([]byte, e Str("output_format", string(showOpts.OutputFormat)). Msg("Showing chart information") - // Initialize action configuration (no namespace or cluster access needed) actionConfig := new(action.Configuration) - err := hspm.initActionConfig(actionConfig, "", nil) + err := authenticateChartSource(actionConfig, showOpts.Registry) if err != nil { - // error is already logged in initActionConfig - return nil, fmt.Errorf("failed to initialize helm configuration: %w", err) + return nil, fmt.Errorf("failed to setup chart source: %w", err) } - // Create temporary directory for chart download - tempDir, err := os.MkdirTemp("", "helm-show-*") - if err != nil { - log.Error(). - Str("context", "HelmClient"). - Err(err). - Msg("Failed to create temp directory") - return nil, fmt.Errorf("failed to create temp directory: %w", err) - } - defer os.RemoveAll(tempDir) - // Create showClient action showClient, err := initShowClient(actionConfig, showOpts) if err != nil { @@ -61,21 +48,28 @@ func (hspm *HelmSDKPackageManager) Show(showOpts options.ShowOptions) ([]byte, e return nil, fmt.Errorf("failed to initialize helm show client: %w", err) } - // Locate and load the chart - log.Debug(). - Str("context", "HelmClient"). - Str("chart", showOpts.Chart). - Str("repo", showOpts.Repo). - Msg("Locating chart") - - chartPath, err := showClient.ChartPathOptions.LocateChart(showOpts.Chart, hspm.settings) + chartRef, _, err := parseChartRef(showOpts.Chart, showOpts.Repo, showOpts.Registry) + if err != nil { + return nil, fmt.Errorf("failed to parse chart reference: %w", err) + } + chartPath, err := showClient.ChartPathOptions.LocateChart(chartRef, hspm.settings) if err != nil { log.Error(). Str("context", "HelmClient"). - Str("chart", showOpts.Chart). + Str("chart", chartRef). Str("repo", showOpts.Repo). Err(err). Msg("Failed to locate chart") + + // Check if this is an authentication error and flush cache if needed + if showOpts.Registry != nil && shouldFlushCacheOnError(err, showOpts.Registry.ID) { + cache.FlushRegistryByID(showOpts.Registry.ID) + log.Info(). + Int("registry_id", int(showOpts.Registry.ID)). + Str("context", "HelmClient"). + Msg("Flushed registry cache due to chart registry authentication error") + } + return nil, fmt.Errorf("failed to locate chart: %w", err) } @@ -88,6 +82,16 @@ func (hspm *HelmSDKPackageManager) Show(showOpts options.ShowOptions) ([]byte, e Str("output_format", string(showOpts.OutputFormat)). Err(err). Msg("Failed to show chart info") + + // Check if this is an authentication error and flush cache if needed + if showOpts.Registry != nil && shouldFlushCacheOnError(err, showOpts.Registry.ID) { + cache.FlushRegistryByID(showOpts.Registry.ID) + log.Info(). + Int("registry_id", int(showOpts.Registry.ID)). + Str("context", "HelmClient"). + Msg("Flushed registry cache due to chart show authentication error") + } + return nil, fmt.Errorf("failed to show chart info: %w", err) } @@ -104,13 +108,13 @@ func (hspm *HelmSDKPackageManager) Show(showOpts options.ShowOptions) ([]byte, e // and return the show client. func initShowClient(actionConfig *action.Configuration, showOpts options.ShowOptions) (*action.Show, error) { showClient := action.NewShowWithConfig(action.ShowAll, actionConfig) - showClient.ChartPathOptions.RepoURL = showOpts.Repo - showClient.ChartPathOptions.Version = showOpts.Version // If version is "", it will use the latest version + err := configureChartPathOptions(&showClient.ChartPathOptions, showOpts.Version, showOpts.Repo, showOpts.Registry) + if err != nil { + return nil, fmt.Errorf("failed to configure chart path options: %w", err) + } // Set output type based on ShowOptions switch showOpts.OutputFormat { - case options.ShowAll: - showClient.OutputFormat = action.ShowAll case options.ShowChart: showClient.OutputFormat = action.ShowChart case options.ShowValues: diff --git a/pkg/libhelm/sdk/show_test.go b/pkg/libhelm/sdk/show_test.go index 302f188ca..26801eb93 100644 --- a/pkg/libhelm/sdk/show_test.go +++ b/pkg/libhelm/sdk/show_test.go @@ -28,7 +28,7 @@ func Test_Show(t *testing.T) { }) } - t.Run("show requires chart, repo and output format", func(t *testing.T) { + t.Run("show requires chart, output format and repo or registry", func(t *testing.T) { showOpts := options.ShowOptions{ Chart: "", Repo: "", @@ -36,7 +36,7 @@ func Test_Show(t *testing.T) { } _, err := hspm.Show(showOpts) is.Error(err, "should return error when required options are missing") - is.Contains(err.Error(), "chart, repo and output format are required", "error message should indicate required options") + is.Contains(err.Error(), "chart, output format and either repo or registry are required", "error message should indicate required options") }) t.Run("show chart values", func(t *testing.T) { diff --git a/pkg/libhelm/sdk/upgrade.go b/pkg/libhelm/sdk/upgrade.go index b47a439a9..1e2a1a5c2 100644 --- a/pkg/libhelm/sdk/upgrade.go +++ b/pkg/libhelm/sdk/upgrade.go @@ -4,6 +4,7 @@ import ( "time" "github.com/pkg/errors" + "github.com/portainer/portainer/pkg/libhelm/cache" "github.com/portainer/portainer/pkg/libhelm/options" "github.com/portainer/portainer/pkg/libhelm/release" "github.com/rs/zerolog/log" @@ -66,6 +67,12 @@ func (hspm *HelmSDKPackageManager) Upgrade(upgradeOpts options.InstallOptions) ( return nil, errors.Wrap(err, "failed to initialize helm configuration for helm release upgrade") } + // Setup chart source + err = authenticateChartSource(actionConfig, upgradeOpts.Registry) + if err != nil { + return nil, errors.Wrap(err, "failed to setup chart source for helm release upgrade") + } + upgradeClient, err := initUpgradeClient(actionConfig, upgradeOpts) if err != nil { log.Error(). @@ -75,7 +82,7 @@ func (hspm *HelmSDKPackageManager) Upgrade(upgradeOpts options.InstallOptions) ( return nil, errors.Wrap(err, "failed to initialize helm upgrade client for helm release upgrade") } - values, err := hspm.GetHelmValuesFromFile(upgradeOpts.ValuesFile) + values, err := hspm.getHelmValuesFromFile(upgradeOpts.ValuesFile) if err != nil { log.Error(). Str("context", "HelmClient"). @@ -84,15 +91,36 @@ func (hspm *HelmSDKPackageManager) Upgrade(upgradeOpts options.InstallOptions) ( return nil, errors.Wrap(err, "failed to get Helm values from file for helm release upgrade") } - chart, err := hspm.loadAndValidateChartWithPathOptions(&upgradeClient.ChartPathOptions, upgradeOpts.Chart, upgradeOpts.Version, upgradeOpts.Repo, upgradeClient.DependencyUpdate, "release upgrade") + chartRef, repoURL, err := parseChartRef(upgradeOpts.Chart, upgradeOpts.Repo, upgradeOpts.Registry) + if err != nil { + return nil, errors.Wrap(err, "failed to parse chart reference for helm release upgrade") + } + chart, err := hspm.loadAndValidateChartWithPathOptions(&upgradeClient.ChartPathOptions, chartRef, upgradeOpts.Version, repoURL, upgradeClient.DependencyUpdate, "release upgrade") if err != nil { log.Error(). Str("context", "HelmClient"). Err(err). Msg("Failed to load and validate chart for helm release upgrade") + + // Check if this is an authentication error and flush cache if needed + if upgradeOpts.Registry != nil && shouldFlushCacheOnError(err, upgradeOpts.Registry.ID) { + cache.FlushRegistryByID(upgradeOpts.Registry.ID) + log.Info(). + Int("registry_id", int(upgradeOpts.Registry.ID)). + Str("context", "HelmClient"). + Msg("Flushed registry cache due to chart loading authentication error during upgrade") + } + return nil, errors.Wrap(err, "failed to load and validate chart for helm release upgrade") } + // Add chart references to annotations + var registryID int + if upgradeOpts.Registry != nil { + registryID = int(upgradeOpts.Registry.ID) + } + chart.Metadata.Annotations = appendChartReferenceAnnotations(upgradeOpts.Chart, upgradeOpts.Repo, registryID, chart.Metadata.Annotations) + log.Info(). Str("context", "HelmClient"). Str("chart", upgradeOpts.Chart). @@ -117,9 +145,10 @@ func (hspm *HelmSDKPackageManager) Upgrade(upgradeOpts options.InstallOptions) ( Namespace: helmRelease.Namespace, Chart: release.Chart{ Metadata: &release.Metadata{ - Name: helmRelease.Chart.Metadata.Name, - Version: helmRelease.Chart.Metadata.Version, - AppVersion: helmRelease.Chart.Metadata.AppVersion, + Name: helmRelease.Chart.Metadata.Name, + Version: helmRelease.Chart.Metadata.Version, + AppVersion: helmRelease.Chart.Metadata.AppVersion, + Annotations: helmRelease.Chart.Metadata.Annotations, }, }, Labels: helmRelease.Labels, @@ -134,12 +163,20 @@ func initUpgradeClient(actionConfig *action.Configuration, upgradeOpts options.I upgradeClient := action.NewUpgrade(actionConfig) upgradeClient.DependencyUpdate = true upgradeClient.Atomic = upgradeOpts.Atomic - upgradeClient.ChartPathOptions.RepoURL = upgradeOpts.Repo upgradeClient.Wait = upgradeOpts.Wait + upgradeClient.Version = upgradeOpts.Version + err := configureChartPathOptions(&upgradeClient.ChartPathOptions, upgradeOpts.Version, upgradeOpts.Repo, upgradeOpts.Registry) + if err != nil { + return nil, errors.Wrap(err, "failed to configure chart path options for helm release upgrade") + } // Set default values if not specified if upgradeOpts.Timeout == 0 { - upgradeClient.Timeout = 5 * time.Minute + if upgradeClient.Atomic { + upgradeClient.Timeout = 30 * time.Minute // the atomic flag significantly increases the upgrade time + } else { + upgradeClient.Timeout = 15 * time.Minute + } } else { upgradeClient.Timeout = upgradeOpts.Timeout } diff --git a/pkg/libhelm/sdk/values.go b/pkg/libhelm/sdk/values.go index 8dc6325a3..7e3e5a07e 100644 --- a/pkg/libhelm/sdk/values.go +++ b/pkg/libhelm/sdk/values.go @@ -11,9 +11,9 @@ import ( "helm.sh/helm/v3/pkg/action" ) -// GetHelmValuesFromFile reads the values file and parses it into a map[string]any +// getHelmValuesFromFile reads the values file and parses it into a map[string]any // and returns the map. -func (hspm *HelmSDKPackageManager) GetHelmValuesFromFile(valuesFile string) (map[string]any, error) { +func (hspm *HelmSDKPackageManager) getHelmValuesFromFile(valuesFile string) (map[string]any, error) { var vals map[string]any if valuesFile != "" { log.Debug(). diff --git a/pkg/libkubectl/apply.go b/pkg/libkubectl/apply.go index 5dad5adc5..23619e532 100644 --- a/pkg/libkubectl/apply.go +++ b/pkg/libkubectl/apply.go @@ -6,18 +6,30 @@ import ( "fmt" "k8s.io/kubectl/pkg/cmd/apply" + cmdutil "k8s.io/kubectl/pkg/cmd/util" ) func (c *Client) Apply(ctx context.Context, manifests []string) (string, error) { buf := new(bytes.Buffer) + var fatalErr error + cmdutil.BehaviorOnFatal(func(msg string, code int) { + fatalErr = newKubectlFatalError(code, msg) + }) + defer cmdutil.DefaultBehaviorOnFatal() + cmd := apply.NewCmdApply("kubectl", c.factory, c.streams) cmd.SetArgs(resourcesToArgs(manifests)) cmd.SetOut(buf) - if err := cmd.ExecuteContext(ctx); err != nil { + err := cmd.ExecuteContext(ctx) + // check for the fatal error first so we don't return the error from the command execution + if fatalErr != nil { + return "", fatalErr + } + // if there is no fatal error, return the error from the command execution + if err != nil { return "", fmt.Errorf("error applying resources: %w", err) } - return buf.String(), nil } diff --git a/pkg/libkubectl/client.go b/pkg/libkubectl/client.go index f9d4137f6..0229c8167 100644 --- a/pkg/libkubectl/client.go +++ b/pkg/libkubectl/client.go @@ -3,6 +3,7 @@ package libkubectl import ( "bytes" "errors" + "fmt" "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/genericiooptions" @@ -61,3 +62,7 @@ func generateConfigFlags(token, server, namespace, kubeconfigPath string, insecu return configFlags, nil } + +func newKubectlFatalError(code int, msg string) error { + return fmt.Errorf("kubectl fatal error (exit code %d): %s", code, msg) +} diff --git a/pkg/libkubectl/delete.go b/pkg/libkubectl/delete.go index bb093ab4d..4544dfc76 100644 --- a/pkg/libkubectl/delete.go +++ b/pkg/libkubectl/delete.go @@ -6,17 +6,30 @@ import ( "fmt" "k8s.io/kubectl/pkg/cmd/delete" + cmdutil "k8s.io/kubectl/pkg/cmd/util" ) func (c *Client) Delete(ctx context.Context, manifests []string) (string, error) { buf := new(bytes.Buffer) + var fatalErr error + cmdutil.BehaviorOnFatal(func(msg string, code int) { + fatalErr = newKubectlFatalError(code, msg) + }) + defer cmdutil.DefaultBehaviorOnFatal() + cmd := delete.NewCmdDelete(c.factory, c.streams) cmd.SetArgs(resourcesToArgs(manifests)) cmd.Flags().Set("ignore-not-found", "true") cmd.SetOut(buf) - if err := cmd.ExecuteContext(ctx); err != nil { + err := cmd.ExecuteContext(ctx) + // check for the fatal error first so we don't return the error from the command execution + if fatalErr != nil { + return "", fatalErr + } + // if there is no fatal error, return the error from the command execution + if err != nil { return "", fmt.Errorf("error deleting resources: %w", err) } diff --git a/pkg/libkubectl/restart.go b/pkg/libkubectl/restart.go index fcfa7fd15..9ce7149aa 100644 --- a/pkg/libkubectl/restart.go +++ b/pkg/libkubectl/restart.go @@ -6,11 +6,18 @@ import ( "fmt" "k8s.io/kubectl/pkg/cmd/rollout" + cmdutil "k8s.io/kubectl/pkg/cmd/util" ) func (c *Client) RolloutRestart(ctx context.Context, manifests []string) (string, error) { buf := new(bytes.Buffer) + var fatalErr error + cmdutil.BehaviorOnFatal(func(msg string, code int) { + fatalErr = newKubectlFatalError(code, msg) + }) + defer cmdutil.DefaultBehaviorOnFatal() + cmd := rollout.NewCmdRollout(c.factory, c.streams) args := []string{"restart"} args = append(args, resourcesToArgs(manifests)...) @@ -18,7 +25,13 @@ func (c *Client) RolloutRestart(ctx context.Context, manifests []string) (string cmd.SetArgs(args) cmd.SetOut(buf) - if err := cmd.ExecuteContext(ctx); err != nil { + err := cmd.ExecuteContext(ctx) + // check for the fatal error first so we don't return the error from the command execution + if fatalErr != nil { + return "", fatalErr + } + // if there is no fatal error, return the error from the command execution + if err != nil { return "", fmt.Errorf("error restarting resources: %w", err) } diff --git a/pkg/liboras/generic_listrepo_client.go b/pkg/liboras/generic_listrepo_client.go new file mode 100644 index 000000000..a99587c0e --- /dev/null +++ b/pkg/liboras/generic_listrepo_client.go @@ -0,0 +1,47 @@ +package liboras + +import ( + "context" + "errors" + + portainer "github.com/portainer/portainer/api" + "oras.land/oras-go/v2/registry/remote" +) + +// GenericListRepoClient implements RepositoryListClient for standard OCI registries +// This client handles repository listing for registries that follow the standard OCI distribution spec +type GenericListRepoClient struct { + registry *portainer.Registry + registryClient *remote.Registry +} + +// NewGenericListRepoClient creates a new generic repository listing client +func NewGenericListRepoClient(registry *portainer.Registry) *GenericListRepoClient { + return &GenericListRepoClient{ + registry: registry, + // registryClient will be set when needed + } +} + +// SetRegistryClient sets the ORAS registry client for repository listing operations +func (c *GenericListRepoClient) SetRegistryClient(registryClient *remote.Registry) { + c.registryClient = registryClient +} + +// ListRepositories fetches repositories from a standard OCI registry using ORAS +func (c *GenericListRepoClient) ListRepositories(ctx context.Context) ([]string, error) { + if c.registryClient == nil { + return nil, errors.New("registry client not initialized for repository listing") + } + + var repositories []string + err := c.registryClient.Repositories(ctx, "", func(repos []string) error { + repositories = append(repositories, repos...) + return nil + }) + if err != nil { + return nil, errors.New("failed to list repositories") + } + + return repositories, nil +} diff --git a/pkg/liboras/github_listrepo_client.go b/pkg/liboras/github_listrepo_client.go new file mode 100644 index 000000000..e80789bec --- /dev/null +++ b/pkg/liboras/github_listrepo_client.go @@ -0,0 +1,57 @@ +package liboras + +import ( + "context" + "fmt" + + portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/http/proxy/factory/github" + "github.com/rs/zerolog/log" +) + +// GithubListRepoClient implements RepositoryListClient specifically for GitHub registries +// This client handles the GitHub Packages API's unique repository listing implementation +type GithubListRepoClient struct { + registry *portainer.Registry + client *github.Client +} + +// NewGithubListRepoClient creates a new GitHub repository listing client +func NewGithubListRepoClient(registry *portainer.Registry) *GithubListRepoClient { + // Prefer the management configuration credentials when available + token := registry.Password + if registry.ManagementConfiguration != nil && registry.ManagementConfiguration.Password != "" { + token = registry.ManagementConfiguration.Password + } + + client := github.NewClient(token) + + return &GithubListRepoClient{ + registry: registry, + client: client, + } +} + +// ListRepositories fetches repositories from a GitHub registry using the GitHub Packages API +func (c *GithubListRepoClient) ListRepositories(ctx context.Context) ([]string, error) { + repositories, err := c.client.GetContainerPackages( + ctx, + c.registry.Github.UseOrganisation, + c.registry.Github.OrganisationName, + ) + if err != nil { + log.Error(). + Str("registry_name", c.registry.Name). + Err(err). + Msg("Failed to list GitHub repositories") + return nil, fmt.Errorf("failed to list GitHub repositories: %w", err) + } + + log.Debug(). + Bool("use_organisation", c.registry.Github.UseOrganisation). + Str("organisation_name", c.registry.Github.OrganisationName). + Int("repository_count", len(repositories)). + Msg("Successfully listed GitHub repositories") + + return repositories, nil +} diff --git a/pkg/liboras/gitlab_listrepo_client.go b/pkg/liboras/gitlab_listrepo_client.go new file mode 100644 index 000000000..762d4984e --- /dev/null +++ b/pkg/liboras/gitlab_listrepo_client.go @@ -0,0 +1,47 @@ +package liboras + +import ( + "context" + "fmt" + + portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/http/proxy/factory/gitlab" + "github.com/rs/zerolog/log" +) + +// GitlabListRepoClient implements RepositoryListClient specifically for GitLab registries +// This client handles the GitLab Container Registry API's unique repository listing implementation +type GitlabListRepoClient struct { + registry *portainer.Registry + client *gitlab.Client +} + +// NewGitlabListRepoClient creates a new GitLab repository listing client +func NewGitlabListRepoClient(registry *portainer.Registry) *GitlabListRepoClient { + client := gitlab.NewClient(registry.Gitlab.InstanceURL, registry.Password) + + return &GitlabListRepoClient{ + registry: registry, + client: client, + } +} + +// ListRepositories fetches repositories from a GitLab registry using the GitLab API +func (c *GitlabListRepoClient) ListRepositories(ctx context.Context) ([]string, error) { + repositories, err := c.client.GetRegistryRepositoryNames(ctx, c.registry.Gitlab.ProjectID) + if err != nil { + log.Error(). + Str("registry_name", c.registry.Name). + Err(err). + Msg("Failed to list GitLab repositories") + return nil, fmt.Errorf("failed to list GitLab repositories: %w", err) + } + + log.Debug(). + Str("gitlab_url", c.registry.Gitlab.InstanceURL). + Int("project_id", c.registry.Gitlab.ProjectID). + Int("repository_count", len(repositories)). + Msg("Successfully listed GitLab repositories") + + return repositories, nil +} diff --git a/pkg/liboras/listrepo_client.go b/pkg/liboras/listrepo_client.go new file mode 100644 index 000000000..f3e066de4 --- /dev/null +++ b/pkg/liboras/listrepo_client.go @@ -0,0 +1,39 @@ +package liboras + +import ( + "context" + + portainer "github.com/portainer/portainer/api" + "oras.land/oras-go/v2/registry/remote" +) + +// RepositoryListClient provides an interface specifically for listing repositories +// This exists because listing repositories isn't a standard OCI operation, and we need to handle +// different registry types differently. +type RepositoryListClient interface { + // ListRepositories returns a list of repository names from the registry + ListRepositories(ctx context.Context) ([]string, error) +} + +// RepositoryListClientFactory creates repository listing clients based on registry type +type RepositoryListClientFactory struct{} + +// NewRepositoryListClientFactory creates a new factory instance +func NewRepositoryListClientFactory() *RepositoryListClientFactory { + return &RepositoryListClientFactory{} +} + +// CreateListClientWithRegistry creates a repository listing client based on the registry type +// and automatically configures it with the provided ORAS registry client for generic registries +func (f *RepositoryListClientFactory) CreateListClientWithRegistry(registry *portainer.Registry, registryClient *remote.Registry) (RepositoryListClient, error) { + switch registry.Type { + case portainer.GitlabRegistry: + return NewGitlabListRepoClient(registry), nil + case portainer.GithubRegistry: + return NewGithubListRepoClient(registry), nil + default: + genericClient := NewGenericListRepoClient(registry) + genericClient.SetRegistryClient(registryClient) + return genericClient, nil + } +} diff --git a/pkg/liboras/registry.go b/pkg/liboras/registry.go new file mode 100644 index 000000000..576f848ac --- /dev/null +++ b/pkg/liboras/registry.go @@ -0,0 +1,79 @@ +package liboras + +import ( + "strings" + + portainer "github.com/portainer/portainer/api" + "github.com/rs/zerolog/log" + "oras.land/oras-go/v2/registry/remote" + "oras.land/oras-go/v2/registry/remote/auth" + "oras.land/oras-go/v2/registry/remote/retry" +) + +func CreateClient(registry portainer.Registry) (*remote.Registry, error) { + registryClient, err := remote.NewRegistry(registry.URL) + if err != nil { + log.Error().Err(err).Str("registryUrl", registry.URL).Msg("Failed to create registry client") + return nil, err + } + // By default, oras sends multiple requests to get the full list of repos/tags/referrers. + // set a high page size limit for fewer round trips. + // e.g. https://github.com/oras-project/oras-go/blob/v2.6.0/registry/remote/registry.go#L129-L142 + registryClient.RepositoryListPageSize = 1000 + registryClient.TagListPageSize = 1000 + registryClient.ReferrerListPageSize = 1000 + + // Only apply authentication if explicitly enabled AND credentials are provided + if registry.Authentication && + strings.TrimSpace(registry.Username) != "" && + strings.TrimSpace(registry.Password) != "" { + + registryClient.Client = &auth.Client{ + Client: retry.DefaultClient, + Cache: auth.NewCache(), + Credential: auth.StaticCredential(registry.URL, auth.Credential{ + Username: registry.Username, + Password: registry.Password, + }), + } + + log.Debug(). + Str("registryURL", registry.URL). + Str("registryType", getRegistryTypeName(registry.Type)). + Bool("authentication", true). + Msg("Created ORAS registry client with authentication") + } else { + // Use default client for anonymous access + registryClient.Client = retry.DefaultClient + + log.Debug(). + Str("registryURL", registry.URL). + Str("registryType", getRegistryTypeName(registry.Type)). + Bool("authentication", false). + Msg("Created ORAS registry client for anonymous access") + } + + return registryClient, nil +} + +// getRegistryTypeName returns a human-readable name for the registry type +func getRegistryTypeName(registryType portainer.RegistryType) string { + switch registryType { + case portainer.QuayRegistry: + return "Quay" + case portainer.AzureRegistry: + return "Azure" + case portainer.CustomRegistry: + return "Custom" + case portainer.GitlabRegistry: + return "GitLab" + case portainer.ProGetRegistry: + return "ProGet" + case portainer.DockerHubRegistry: + return "DockerHub" + case portainer.EcrRegistry: + return "ECR" + default: + return "Unknown" + } +} diff --git a/pkg/liboras/registry_test.go b/pkg/liboras/registry_test.go new file mode 100644 index 000000000..78172f25d --- /dev/null +++ b/pkg/liboras/registry_test.go @@ -0,0 +1,252 @@ +package liboras + +import ( + "testing" + + portainer "github.com/portainer/portainer/api" + "github.com/stretchr/testify/assert" + "oras.land/oras-go/v2/registry/remote/auth" + "oras.land/oras-go/v2/registry/remote/retry" +) + +func TestCreateClient_AuthenticationScenarios(t *testing.T) { + tests := []struct { + name string + registry portainer.Registry + expectAuthenticated bool + description string + }{ + { + name: "authentication disabled should create anonymous client", + registry: portainer.Registry{ + URL: "registry.example.com", + Authentication: false, + Username: "testuser", + Password: "testpass", + }, + expectAuthenticated: false, + description: "Even with credentials present, authentication=false should result in anonymous access", + }, + { + name: "authentication enabled with valid credentials should create authenticated client", + registry: portainer.Registry{ + URL: "registry.example.com", + Authentication: true, + Username: "testuser", + Password: "testpass", + }, + expectAuthenticated: true, + description: "Valid credentials with authentication=true should result in authenticated access", + }, + { + name: "authentication enabled with empty username should create anonymous client", + registry: portainer.Registry{ + URL: "registry.example.com", + Authentication: true, + Username: "", + Password: "testpass", + }, + expectAuthenticated: false, + description: "Empty username should fallback to anonymous access", + }, + { + name: "authentication enabled with whitespace-only username should create anonymous client", + registry: portainer.Registry{ + URL: "registry.example.com", + Authentication: true, + Username: " ", + Password: "testpass", + }, + expectAuthenticated: false, + description: "Whitespace-only username should fallback to anonymous access", + }, + { + name: "authentication enabled with empty password should create anonymous client", + registry: portainer.Registry{ + URL: "registry.example.com", + Authentication: true, + Username: "testuser", + Password: "", + }, + expectAuthenticated: false, + description: "Empty password should fallback to anonymous access", + }, + { + name: "authentication enabled with whitespace-only password should create anonymous client", + registry: portainer.Registry{ + URL: "registry.example.com", + Authentication: true, + Username: "testuser", + Password: " ", + }, + expectAuthenticated: false, + description: "Whitespace-only password should fallback to anonymous access", + }, + { + name: "authentication enabled with both credentials empty should create anonymous client", + registry: portainer.Registry{ + URL: "registry.example.com", + Authentication: true, + Username: "", + Password: "", + }, + expectAuthenticated: false, + description: "Both credentials empty should fallback to anonymous access", + }, + { + name: "public registry with no authentication should create anonymous client", + registry: portainer.Registry{ + URL: "docker.io", + Authentication: false, + Username: "", + Password: "", + }, + expectAuthenticated: false, + description: "Public registries without authentication should use anonymous access", + }, + { + name: "GitLab registry with valid credentials should create authenticated client", + registry: portainer.Registry{ + Type: portainer.GitlabRegistry, + URL: "registry.gitlab.com", + Authentication: true, + Username: "gitlab-ci-token", + Password: "glpat-xxxxxxxxxxxxxxxxxxxx", + Gitlab: portainer.GitlabRegistryData{ + ProjectID: 12345, + InstanceURL: "https://gitlab.com", + ProjectPath: "my-group/my-project", + }, + }, + expectAuthenticated: true, + description: "GitLab registry with valid credentials should result in authenticated access", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + client, err := CreateClient(tt.registry) + + assert.NoError(t, err, "CreateClient should not return an error") + assert.NotNil(t, client, "Client should not be nil") + + // Check if the client has authentication configured + if tt.expectAuthenticated { + // Should have auth.Client with credentials + authClient, ok := client.Client.(*auth.Client) + assert.True(t, ok, "Expected auth.Client for authenticated access") + assert.NotNil(t, authClient, "Auth client should not be nil") + assert.NotNil(t, authClient.Credential, "Credential function should be set") + } else { + // Should use retry.DefaultClient (no authentication) + assert.Equal(t, retry.DefaultClient, client.Client, + "Expected retry.DefaultClient for anonymous access") + } + }) + } +} + +func TestCreateClient_RegistryTypes(t *testing.T) { + registryTypes := []struct { + name string + registryType portainer.RegistryType + expectedName string + }{ + {"DockerHub", portainer.DockerHubRegistry, "DockerHub"}, + {"Azure", portainer.AzureRegistry, "Azure"}, + {"Custom", portainer.CustomRegistry, "Custom"}, + {"GitLab", portainer.GitlabRegistry, "GitLab"}, + {"Quay", portainer.QuayRegistry, "Quay"}, + {"ProGet", portainer.ProGetRegistry, "ProGet"}, + {"ECR", portainer.EcrRegistry, "ECR"}, + } + + for _, rt := range registryTypes { + t.Run(rt.name, func(t *testing.T) { + registry := portainer.Registry{ + URL: "registry.example.com", + Type: rt.registryType, + Authentication: false, + } + + client, err := CreateClient(registry) + + assert.NoError(t, err, "CreateClient should not return an error") + assert.NotNil(t, client, "Client should not be nil") + + // Verify that getRegistryTypeName returns the expected name + typeName := getRegistryTypeName(rt.registryType) + assert.Equal(t, rt.expectedName, typeName, "Registry type name mismatch") + }) + } +} + +func TestGetRegistryTypeName(t *testing.T) { + tests := []struct { + registryType portainer.RegistryType + expectedName string + }{ + {portainer.QuayRegistry, "Quay"}, + {portainer.AzureRegistry, "Azure"}, + {portainer.CustomRegistry, "Custom"}, + {portainer.GitlabRegistry, "GitLab"}, + {portainer.ProGetRegistry, "ProGet"}, + {portainer.DockerHubRegistry, "DockerHub"}, + {portainer.EcrRegistry, "ECR"}, + {portainer.RegistryType(999), "Unknown"}, // Unknown type + } + + for _, tt := range tests { + t.Run(tt.expectedName, func(t *testing.T) { + result := getRegistryTypeName(tt.registryType) + assert.Equal(t, tt.expectedName, result, "Registry type name mismatch") + }) + } +} + +func TestCreateClient_ErrorHandling(t *testing.T) { + tests := []struct { + name string + registry portainer.Registry + expectError bool + }{ + { + name: "valid registry URL should not error", + registry: portainer.Registry{ + URL: "registry.example.com", + Authentication: false, + }, + expectError: false, + }, + { + name: "empty registry URL should error", + registry: portainer.Registry{ + URL: "", + Authentication: false, + }, + expectError: true, + }, + { + name: "invalid registry URL should error", + registry: portainer.Registry{ + URL: "://invalid-url", + Authentication: false, + }, + expectError: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + client, err := CreateClient(tt.registry) + + if tt.expectError { + assert.Error(t, err, "Expected an error but got none") + assert.Nil(t, client, "Client should be nil when error occurs") + } else { + assert.NoError(t, err, "Expected no error but got: %v", err) + assert.NotNil(t, client, "Client should not be nil") + } + }) + } +} diff --git a/pkg/liboras/repository.go b/pkg/liboras/repository.go new file mode 100644 index 000000000..2bd78f46a --- /dev/null +++ b/pkg/liboras/repository.go @@ -0,0 +1,126 @@ +package liboras + +import ( + "context" + "fmt" + "io" + "sort" + + ocispec "github.com/opencontainers/image-spec/specs-go/v1" + portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/concurrent" + "github.com/segmentio/encoding/json" + "golang.org/x/mod/semver" + "oras.land/oras-go/v2/registry" + "oras.land/oras-go/v2/registry/remote" +) + +// ListRepositories retrieves all repositories from a registry using specialized repository listing clients +// Each registry type has different repository listing implementations that require specific API calls +func ListRepositories(ctx context.Context, registry *portainer.Registry, registryClient *remote.Registry) ([]string, error) { + factory := NewRepositoryListClientFactory() + listClient, err := factory.CreateListClientWithRegistry(registry, registryClient) + if err != nil { + return nil, fmt.Errorf("failed to create repository list client: %w", err) + } + + return listClient.ListRepositories(ctx) +} + +// FilterRepositoriesByMediaType filters repositories to only include those with the expected media type +func FilterRepositoriesByMediaType(ctx context.Context, repositoryNames []string, registryClient *remote.Registry, expectedMediaType string) ([]string, error) { + // Run concurrently as this can take 10s+ to complete in serial + var tasks []concurrent.Func + for _, repoName := range repositoryNames { + name := repoName + task := func(ctx context.Context) (any, error) { + repository, err := registryClient.Repository(ctx, name) + if err != nil { + return nil, err + } + + if HasMediaType(ctx, repository, expectedMediaType) { + return name, nil + } + return nil, nil // not a repository with the expected media type + } + tasks = append(tasks, task) + } + + // 10 is a reasonable max concurrency limit + results, err := concurrent.Run(ctx, 10, tasks...) + if err != nil { + return nil, err + } + + // Collect repository names + var repositories []string + for _, result := range results { + if result.Result != nil { + if repoName, ok := result.Result.(string); ok { + repositories = append(repositories, repoName) + } + } + } + + return repositories, nil +} + +// HasMediaType checks if a repository has artifacts with the specified media type +func HasMediaType(ctx context.Context, repository registry.Repository, expectedMediaType string) bool { + // Check the first available tag + // Reasonable limitation - it won't work for repos where the latest tag is missing the expected media type but other tags have it + // This tradeoff is worth it for the performance benefits + var latestTag string + err := repository.Tags(ctx, "", func(tagList []string) error { + if len(tagList) > 0 { + // Order the taglist by latest semver, then get the latest tag + // e.g. ["1.0", "1.1"] -> ["1.1", "1.0"] -> "1.1" + sort.Slice(tagList, func(i, j int) bool { + return semver.Compare(tagList[i], tagList[j]) > 0 + }) + latestTag = tagList[0] + } + return nil + }) + + if err != nil { + return false + } + + if latestTag == "" { + return false + } + + descriptor, err := repository.Resolve(ctx, latestTag) + if err != nil { + return false + } + + return descriptorHasMediaType(ctx, repository, descriptor, expectedMediaType) +} + +// descriptorHasMediaType checks if a descriptor or its manifest contains the expected media type +func descriptorHasMediaType(ctx context.Context, repository registry.Repository, descriptor ocispec.Descriptor, expectedMediaType string) bool { + // Check if the descriptor indicates the expected media type + if descriptor.MediaType == expectedMediaType { + return true + } + + // Otherwise, look for the expected media type in the entire manifest content + manifestReader, err := repository.Manifests().Fetch(ctx, descriptor) + if err != nil { + return false + } + defer manifestReader.Close() + + content, err := io.ReadAll(manifestReader) + if err != nil { + return false + } + var manifest ocispec.Manifest + if err := json.Unmarshal(content, &manifest); err != nil { + return false + } + return manifest.Config.MediaType == expectedMediaType +} diff --git a/pkg/snapshot/docker.go b/pkg/snapshot/docker.go index deaabdb08..12f3a3089 100644 --- a/pkg/snapshot/docker.go +++ b/pkg/snapshot/docker.go @@ -100,7 +100,10 @@ func dockerSnapshotNodes(snapshot *portainer.DockerSnapshot, cli *client.Client) snapshot.TotalCPU = int(nanoCpus / 1e9) snapshot.TotalMemory = totalMem - snapshot.NodeCount = len(nodes) + snapshot.NodeCount = 1 + if snapshot.Swarm { + snapshot.NodeCount = len(nodes) + } return nil } diff --git a/pkg/snapshot/kubernetes.go b/pkg/snapshot/kubernetes.go index d8e550e84..77fda14bd 100644 --- a/pkg/snapshot/kubernetes.go +++ b/pkg/snapshot/kubernetes.go @@ -5,7 +5,9 @@ import ( "errors" "fmt" "io" + "math" "os" + "reflect" "strings" "time" @@ -19,11 +21,11 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" + statsapi "k8s.io/kubelet/pkg/apis/stats/v1alpha1" ) func CreateKubernetesSnapshot(cli *kubernetes.Clientset) (*portainer.KubernetesSnapshot, error) { kubernetesSnapshot := &portainer.KubernetesSnapshot{} - err := kubernetesSnapshotVersion(kubernetesSnapshot, cli) if err != nil { log.Warn().Err(err).Msg("unable to snapshot cluster version") @@ -54,10 +56,28 @@ func kubernetesSnapshotNodes(snapshot *portainer.KubernetesSnapshot, cli *kubern return err } + if len(nodeList.Items) == 0 { + return nil + } + var totalCPUs, totalMemory int64 + performanceMetrics := &portainer.PerformanceMetrics{ + CPUUsage: 0, + MemoryUsage: 0, + NetworkUsage: 0, + } + for _, node := range nodeList.Items { totalCPUs += node.Status.Capacity.Cpu().Value() totalMemory += node.Status.Capacity.Memory().Value() + + performanceMetrics, err = kubernetesSnapshotNodePerformanceMetrics(cli, node, performanceMetrics) + if err != nil { + return fmt.Errorf("failed to get node performance metrics: %w", err) + } + if performanceMetrics != nil { + snapshot.PerformanceMetrics = performanceMetrics + } } snapshot.TotalCPU = totalCPUs @@ -123,6 +143,40 @@ func kubernetesSnapshotPodErrorLogs(snapshot *portainer.KubernetesSnapshot, cli return nil } +func kubernetesSnapshotNodePerformanceMetrics(cli *kubernetes.Clientset, node corev1.Node, performanceMetrics *portainer.PerformanceMetrics) (*portainer.PerformanceMetrics, error) { + result := cli.RESTClient().Get().AbsPath(fmt.Sprintf("/api/v1/nodes/%s/proxy/stats/summary", node.Name)).Do(context.TODO()) + if result.Error() != nil { + return nil, fmt.Errorf("failed to get node performance metrics: %w", result.Error()) + } + + raw, err := result.Raw() + if err != nil { + return nil, fmt.Errorf("failed to get node performance metrics: %w", err) + } + + stats := statsapi.Summary{} + err = json.Unmarshal(raw, &stats) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal node performance metrics: %w", err) + } + + nodeStats := stats.Node + if reflect.DeepEqual(nodeStats, statsapi.NodeStats{}) { + return nil, nil + } + + if nodeStats.CPU != nil && nodeStats.CPU.UsageNanoCores != nil { + performanceMetrics.CPUUsage += math.Round(float64(*nodeStats.CPU.UsageNanoCores) / float64(node.Status.Capacity.Cpu().Value()*1000000000) * 100) + } + if nodeStats.Memory != nil && nodeStats.Memory.WorkingSetBytes != nil { + performanceMetrics.MemoryUsage += math.Round(float64(*nodeStats.Memory.WorkingSetBytes) / float64(node.Status.Capacity.Memory().Value()) * 100) + } + if nodeStats.Network != nil && nodeStats.Network.RxBytes != nil && nodeStats.Network.TxBytes != nil { + performanceMetrics.NetworkUsage += math.Round((float64(*nodeStats.Network.RxBytes) + float64(*nodeStats.Network.TxBytes)) / 1024 / 1024) // MB + } + return performanceMetrics, nil +} + // filterLogsByPattern filters the logs by the given patterns and returns a list of logs that match the patterns // the logs are returned as a list of maps with the keys "timestamp" and "message" func filterLogsByPattern(logBytes []byte, patterns []string) []map[string]string { diff --git a/pkg/validate/validate.go b/pkg/validate/validate.go index f647d1e17..8ad69df72 100644 --- a/pkg/validate/validate.go +++ b/pkg/validate/validate.go @@ -80,3 +80,32 @@ func IsDNSName(s string) bool { return !IsIP(s) && dnsNameRegex.MatchString(s) } + +func IsTrustedOrigin(s string) bool { + // Reject if a scheme is present + if strings.Contains(s, "://") { + return false + } + + // Prepend http:// for parsing + strTemp := "http://" + s + parsedOrigin, err := url.Parse(strTemp) + if err != nil { + return false + } + + // Validate host, and ensure no user, path, query, fragment, port, etc. + if parsedOrigin.Host == "" || + parsedOrigin.User != nil || + parsedOrigin.Path != "" || + parsedOrigin.RawQuery != "" || + parsedOrigin.Fragment != "" || + parsedOrigin.Opaque != "" || + parsedOrigin.RawFragment != "" || + parsedOrigin.RawPath != "" || + parsedOrigin.Port() != "" { + return false + } + + return true +} diff --git a/pkg/validate/validate_test.go b/pkg/validate/validate_test.go index f3cb6a01c..ca054190d 100644 --- a/pkg/validate/validate_test.go +++ b/pkg/validate/validate_test.go @@ -437,3 +437,64 @@ func Test_IsDNSName(t *testing.T) { }) } } + +func Test_IsTrustedOrigin(t *testing.T) { + f := func(s string, expected bool) { + t.Helper() + + result := IsTrustedOrigin(s) + if result != expected { + t.Fatalf("unexpected result for %q; got %t; want %t", s, result, expected) + } + } + + // Valid trusted origins - host only + f("localhost", true) + f("example.com", true) + f("192.168.1.1", true) + f("api.example.com", true) + f("subdomain.example.org", true) + + // Invalid trusted origins - host with port (no longer allowed) + f("localhost:8080", false) + f("example.com:3000", false) + f("192.168.1.1:443", false) + f("api.example.com:9000", false) + + // Invalid trusted origins - empty or malformed + f("", false) + f("invalid url", false) + f("://example.com", false) + + // Invalid trusted origins - with scheme + f("http://example.com", false) + f("https://localhost", false) + f("ftp://192.168.1.1", false) + + // Invalid trusted origins - with user info + f("user@example.com", false) + f("user:pass@localhost", false) + + // Invalid trusted origins - with path + f("example.com/path", false) + f("localhost/api", false) + f("192.168.1.1/static", false) + + // Invalid trusted origins - with query parameters + f("example.com?param=value", false) + f("localhost:8080?query=test", false) + + // Invalid trusted origins - with fragment + f("example.com#fragment", false) + f("localhost:3000#section", false) + + // Invalid trusted origins - with multiple invalid components + f("https://user@example.com/path?query=value#fragment", false) + f("http://localhost:8080/api/v1?param=test", false) + + // Edge cases - ports are no longer allowed + f("example.com:0", false) // port 0 is no longer valid + f("example.com:65535", false) // max port number is no longer valid + f("example.com:99999", false) // invalid port number + f("example.com:-1", false) // negative port +}