mirror of
https://github.com/portainer/portainer.git
synced 2025-07-22 06:49:40 +02:00
* feat(access-token): Multi-auth middleware support EE-1891 (#5936) * AnyAuth middleware initial implementation with tests * using mux.MiddlewareFunc instead of custom definition * removed redundant comments * - ExtractBearerToken bouncer func made private - changed helm token handling functionality to use jwt service to convert token to jwt string - updated tests - fixed helm list broken test due to missing token in request context * rename mwCheckAuthentication -> mwCheckJWTAuthentication * - introduce initial api-key auth support using X-API-KEY header - added tests to validate x-api-key request header presence * updated core mwAuthenticatedUser middleware to support multiple auth paradigms * - simplified anyAuth middleware - enforcing authmiddleware to implement verificationFunc interface - created tests for middleware * simplify bouncer Co-authored-by: Dmitry Salakhov <to@dimasalakhov.com> * feat(api-key): user-access-token generation endpoint EE-1889 EE-1888 EE-1895 (#6012) * user-access-token generation endpoint * fix comment * - introduction of apikey service - seperation of repository from service logic - called in handler * fixed tests * - fixed api key prefix - added tests * added another test for digest matching * updated swagger spec for access token creation * api key response returns raw key and struct - easing testability * test for api key prefix length * added another TODO to middleware * - api-key prefix rune -> string (rune does not auto-encode when response sent back to client) - digest -> pointer as we want to allow nil values and omit digest in responses (when nil) * - updated apikey struct - updated apikey service to support all common operations - updated apikey repo - integration of apikey service into bouncer - added test for all apikey service functions - boilerplate code for apikey service integration * - user access token generation tests - apiKeyLookup updated to support query params - added api-key tests for query params - added api-key tests for apiKeyLookup * get and remove access token handlers * get and remove access token handler tests * - delete user deletes all associated api keys - tests for this functionality * removed redundant []byte cast * automatic api-key eviction set within cache for 1 hour * fixed bug with loop var using final value * fixed service comment * ignore bolt error responses * case-insensitive query param check * simplified query var assignment * - added GetAPIKey func to get by unique id - updated DeleteAPIKey func to not require user ID - updated tests * GenerateRandomKey helper func from github.com/gorilla/securecookie moved to codebase * json response casing for api-keys fixed * updating api-key will update the cache * updated golang LRU cache * using hashicorps golang-LRU cache for api keys * simplified jwt check in create user access token * fixed api-key update logic on cache miss * Prefix generated api-keys with `ptr_` (#6067) * prefix api-keys with 'ptr_' * updated apikey description * refactor Co-authored-by: Dmitry Salakhov <to@dimasalakhov.com> * helm list test refactor * fixed user delete test * reduce test nil pointer errors * using correct http 201 created status code for token creation; updated tests * fixed swagger doc user id path param for user access token based endpoints * added api-key security openapi spec to existing jwt secured endpoints (#6091) * fixed flaky test * apikey datecreated and lastused attrs converted to unix timestamp * feat(user): added access token datatable. (#6124) * feat(user): added access token datatable. * feat(tokens): only display lastUsed time when it is not the default date * Update app/portainer/views/account/accountController.js Co-authored-by: zees-dev <63374656+zees-dev@users.noreply.github.com> * Update app/portainer/views/account/accountController.js Co-authored-by: zees-dev <63374656+zees-dev@users.noreply.github.com> * Update app/portainer/views/account/accountController.js Co-authored-by: zees-dev <63374656+zees-dev@users.noreply.github.com> * Update app/portainer/components/datatables/access-tokens-datatable/accessTokensDatatableController.js Co-authored-by: zees-dev <63374656+zees-dev@users.noreply.github.com> * Update app/portainer/services/api/userService.js Co-authored-by: zees-dev <63374656+zees-dev@users.noreply.github.com> * feat(improvements): proposed datatable improvements to speed up dev time (#6138) * modal code update * updated datatable filenames, updated controller to be default class export * fix(access-token): code improvement. Co-authored-by: zees-dev <63374656+zees-dev@users.noreply.github.com> * feat(apikeys): create access token view initial implementation EE-1886 (#6129) * CopyButton implementation * Code component implementation * ToolTip component migration to another folder * TextTip component implementation - continued * form Heading component * Button component updated to be more dynamic * copybutton - small size * form control pass tip error * texttip small text * CreateAccessToken react feature initial implementation * create user access token angularjs view implementation * registration of CreateAccessToken component in AngularJS * user token generation API request moved to angular service, method passed down instead * consistent naming of access token operations; clustered similar code together * any user can add access token * create access token page routing * moved code component to the correct location * removed isadmin check as all functionality applicable to all users * create access token angular view moved up a level * fixed PR issues, updated PR * addressed PR issues/improvements * explicit hr for horizontal line * fixed merge conflict storybook build breaking * - apikey test - cache test * addressed testing issues: - description validations - remove token description link on table * fix(api-keys): user role change evicts user keys in cache EE-2113 (#6168) * user role change evicts user api keys in cache * EvictUserKeyCache -> InvalidateUserKeyCache * godoc for InvalidateUserKeyCache func * additional test line * disable add access token button after adding token to prevent spam Co-authored-by: Dmitry Salakhov <to@dimasalakhov.com> Co-authored-by: fhanportainer <79428273+fhanportainer@users.noreply.github.com>
232 lines
8.4 KiB
Go
232 lines
8.4 KiB
Go
package settings
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/asaskevich/govalidator"
|
|
"github.com/pkg/errors"
|
|
"github.com/portainer/libhelm"
|
|
httperror "github.com/portainer/libhttp/error"
|
|
"github.com/portainer/libhttp/request"
|
|
"github.com/portainer/libhttp/response"
|
|
portainer "github.com/portainer/portainer/api"
|
|
"github.com/portainer/portainer/api/filesystem"
|
|
)
|
|
|
|
type settingsUpdatePayload struct {
|
|
// URL to a logo that will be displayed on the login page as well as on top of the sidebar. Will use default Portainer logo when value is empty string
|
|
LogoURL *string `example:"https://mycompany.mydomain.tld/logo.png"`
|
|
// A list of label name & value that will be used to hide containers when querying containers
|
|
BlackListedLabels []portainer.Pair
|
|
// Active authentication method for the Portainer instance. Valid values are: 1 for internal, 2 for LDAP, or 3 for oauth
|
|
AuthenticationMethod *int `example:"1"`
|
|
LDAPSettings *portainer.LDAPSettings `example:""`
|
|
OAuthSettings *portainer.OAuthSettings `example:""`
|
|
// The interval in which environment(endpoint) snapshots are created
|
|
SnapshotInterval *string `example:"5m"`
|
|
// URL to the templates that will be displayed in the UI when navigating to App Templates
|
|
TemplatesURL *string `example:"https://raw.githubusercontent.com/portainer/templates/master/templates.json"`
|
|
// The default check in interval for edge agent (in seconds)
|
|
EdgeAgentCheckinInterval *int `example:"5"`
|
|
// Whether edge compute features are enabled
|
|
EnableEdgeComputeFeatures *bool `example:"true"`
|
|
// The duration of a user session
|
|
UserSessionTimeout *string `example:"5m"`
|
|
// The expiry of a Kubeconfig
|
|
KubeconfigExpiry *string `example:"24h" default:"0"`
|
|
// Whether telemetry is enabled
|
|
EnableTelemetry *bool `example:"false"`
|
|
// Helm repository URL
|
|
HelmRepositoryURL *string `example:"https://charts.bitnami.com/bitnami"`
|
|
// Kubectl Shell Image
|
|
KubectlShellImage *string `example:"portainer/kubectl-shell:latest"`
|
|
}
|
|
|
|
func (payload *settingsUpdatePayload) Validate(r *http.Request) error {
|
|
if payload.AuthenticationMethod != nil && *payload.AuthenticationMethod != 1 && *payload.AuthenticationMethod != 2 && *payload.AuthenticationMethod != 3 {
|
|
return errors.New("Invalid authentication method value. Value must be one of: 1 (internal), 2 (LDAP/AD) or 3 (OAuth)")
|
|
}
|
|
if payload.LogoURL != nil && *payload.LogoURL != "" && !govalidator.IsURL(*payload.LogoURL) {
|
|
return errors.New("Invalid logo URL. Must correspond to a valid URL format")
|
|
}
|
|
if payload.TemplatesURL != nil && *payload.TemplatesURL != "" && !govalidator.IsURL(*payload.TemplatesURL) {
|
|
return errors.New("Invalid external templates URL. Must correspond to a valid URL format")
|
|
}
|
|
if payload.HelmRepositoryURL != nil && *payload.HelmRepositoryURL != "" && !govalidator.IsURL(*payload.HelmRepositoryURL) {
|
|
return errors.New("Invalid Helm repository URL. Must correspond to a valid URL format")
|
|
}
|
|
if payload.UserSessionTimeout != nil {
|
|
_, err := time.ParseDuration(*payload.UserSessionTimeout)
|
|
if err != nil {
|
|
return errors.New("Invalid user session timeout")
|
|
}
|
|
}
|
|
if payload.KubeconfigExpiry != nil {
|
|
_, err := time.ParseDuration(*payload.KubeconfigExpiry)
|
|
if err != nil {
|
|
return errors.New("Invalid Kubeconfig Expiry")
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// @id SettingsUpdate
|
|
// @summary Update Portainer settings
|
|
// @description Update Portainer settings.
|
|
// @description **Access policy**: administrator
|
|
// @tags settings
|
|
// @security ApiKeyAuth
|
|
// @security jwt
|
|
// @accept json
|
|
// @produce json
|
|
// @param body body settingsUpdatePayload true "New settings"
|
|
// @success 200 {object} portainer.Settings "Success"
|
|
// @failure 400 "Invalid request"
|
|
// @failure 500 "Server error"
|
|
// @router /settings [put]
|
|
func (handler *Handler) settingsUpdate(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
var payload settingsUpdatePayload
|
|
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
|
if err != nil {
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err}
|
|
}
|
|
|
|
settings, err := handler.DataStore.Settings().Settings()
|
|
if err != nil {
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve the settings from the database", err}
|
|
}
|
|
|
|
if payload.AuthenticationMethod != nil {
|
|
settings.AuthenticationMethod = portainer.AuthenticationMethod(*payload.AuthenticationMethod)
|
|
}
|
|
|
|
if payload.LogoURL != nil {
|
|
settings.LogoURL = *payload.LogoURL
|
|
}
|
|
|
|
if payload.TemplatesURL != nil {
|
|
settings.TemplatesURL = *payload.TemplatesURL
|
|
}
|
|
|
|
if payload.HelmRepositoryURL != nil {
|
|
newHelmRepo := strings.TrimSuffix(strings.ToLower(*payload.HelmRepositoryURL), "/")
|
|
|
|
if newHelmRepo != settings.HelmRepositoryURL && newHelmRepo != portainer.DefaultHelmRepositoryURL {
|
|
err := libhelm.ValidateHelmRepositoryURL(*payload.HelmRepositoryURL)
|
|
if err != nil {
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid Helm repository URL. Must correspond to a valid URL format", err}
|
|
}
|
|
}
|
|
|
|
settings.HelmRepositoryURL = newHelmRepo
|
|
} else {
|
|
settings.HelmRepositoryURL = ""
|
|
}
|
|
|
|
if payload.BlackListedLabels != nil {
|
|
settings.BlackListedLabels = payload.BlackListedLabels
|
|
}
|
|
|
|
if payload.LDAPSettings != nil {
|
|
ldapReaderDN := settings.LDAPSettings.ReaderDN
|
|
ldapPassword := settings.LDAPSettings.Password
|
|
if payload.LDAPSettings.ReaderDN != "" {
|
|
ldapReaderDN = payload.LDAPSettings.ReaderDN
|
|
}
|
|
if payload.LDAPSettings.Password != "" {
|
|
ldapPassword = payload.LDAPSettings.Password
|
|
}
|
|
settings.LDAPSettings = *payload.LDAPSettings
|
|
settings.LDAPSettings.ReaderDN = ldapReaderDN
|
|
settings.LDAPSettings.Password = ldapPassword
|
|
}
|
|
|
|
if payload.OAuthSettings != nil {
|
|
clientSecret := payload.OAuthSettings.ClientSecret
|
|
if clientSecret == "" {
|
|
clientSecret = settings.OAuthSettings.ClientSecret
|
|
}
|
|
kubeSecret := payload.OAuthSettings.KubeSecretKey
|
|
if kubeSecret == nil {
|
|
kubeSecret = settings.OAuthSettings.KubeSecretKey
|
|
}
|
|
settings.OAuthSettings = *payload.OAuthSettings
|
|
settings.OAuthSettings.ClientSecret = clientSecret
|
|
settings.OAuthSettings.KubeSecretKey = kubeSecret
|
|
}
|
|
|
|
if payload.EnableEdgeComputeFeatures != nil {
|
|
settings.EnableEdgeComputeFeatures = *payload.EnableEdgeComputeFeatures
|
|
}
|
|
|
|
if payload.SnapshotInterval != nil && *payload.SnapshotInterval != settings.SnapshotInterval {
|
|
err := handler.updateSnapshotInterval(settings, *payload.SnapshotInterval)
|
|
if err != nil {
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to update snapshot interval", err}
|
|
}
|
|
}
|
|
|
|
if payload.EdgeAgentCheckinInterval != nil {
|
|
settings.EdgeAgentCheckinInterval = *payload.EdgeAgentCheckinInterval
|
|
}
|
|
|
|
if payload.KubeconfigExpiry != nil {
|
|
settings.KubeconfigExpiry = *payload.KubeconfigExpiry
|
|
}
|
|
|
|
if payload.UserSessionTimeout != nil {
|
|
settings.UserSessionTimeout = *payload.UserSessionTimeout
|
|
|
|
userSessionDuration, _ := time.ParseDuration(*payload.UserSessionTimeout)
|
|
|
|
handler.JWTService.SetUserSessionDuration(userSessionDuration)
|
|
}
|
|
|
|
if payload.EnableTelemetry != nil {
|
|
settings.EnableTelemetry = *payload.EnableTelemetry
|
|
}
|
|
|
|
tlsError := handler.updateTLS(settings)
|
|
if tlsError != nil {
|
|
return tlsError
|
|
}
|
|
|
|
if payload.KubectlShellImage != nil {
|
|
settings.KubectlShellImage = *payload.KubectlShellImage
|
|
}
|
|
|
|
err = handler.DataStore.Settings().UpdateSettings(settings)
|
|
if err != nil {
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist settings changes inside the database", err}
|
|
}
|
|
|
|
return response.JSON(w, settings)
|
|
}
|
|
|
|
func (handler *Handler) updateSnapshotInterval(settings *portainer.Settings, snapshotInterval string) error {
|
|
settings.SnapshotInterval = snapshotInterval
|
|
|
|
err := handler.SnapshotService.SetSnapshotInterval(snapshotInterval)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (handler *Handler) updateTLS(settings *portainer.Settings) *httperror.HandlerError {
|
|
if (settings.LDAPSettings.TLSConfig.TLS || settings.LDAPSettings.StartTLS) && !settings.LDAPSettings.TLSConfig.TLSSkipVerify {
|
|
caCertPath, _ := handler.FileService.GetPathForTLSFile(filesystem.LDAPStorePath, portainer.TLSFileCA)
|
|
settings.LDAPSettings.TLSConfig.TLSCACertPath = caCertPath
|
|
} else {
|
|
settings.LDAPSettings.TLSConfig.TLSCACertPath = ""
|
|
err := handler.FileService.DeleteTLSFiles(filesystem.LDAPStorePath)
|
|
if err != nil {
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove TLS files from disk", err}
|
|
}
|
|
}
|
|
return nil
|
|
}
|