From f02ede00b35b5c42c3050fb32d10df1b28ca0ea9 Mon Sep 17 00:00:00 2001 From: Oscar Zhou <100548325+oscarzhou-portainer@users.noreply.github.com> Date: Wed, 28 Jun 2023 08:51:58 +1200 Subject: [PATCH] fix(docker/tls): update tls certs for Docker API env [EE-4286] (#9112) --- api/http/handler/endpoints/endpoint_update.go | 25 +++++++- app/portainer/components/index.js | 3 +- .../components/tls-fieldset/index.ts | 22 +++++++ .../views/endpoints/edit/endpoint.html | 18 +++--- .../endpoints/edit/endpointController.js | 63 ++++++++++++++----- .../TLSFieldset}/TLSFieldset.tsx | 54 ++++++++++------ app/react/components/TLSFieldset/index.ts | 1 + app/react/components/TLSFieldset/types.ts | 7 +++ .../portainer/environments/utils/index.ts | 7 +++ .../WizardDocker/APITab/APIForm.tsx | 31 ++++++--- .../APITab/APIForm.validation.tsx | 9 ++- .../WizardDocker/APITab/types.ts | 7 +-- .../WizardDocker/SocketTab/SocketForm.tsx | 1 + 13 files changed, 184 insertions(+), 64 deletions(-) create mode 100644 app/portainer/components/tls-fieldset/index.ts rename app/react/{portainer/environments/wizard/EnvironmentsCreationView/WizardDocker/APITab => components/TLSFieldset}/TLSFieldset.tsx (60%) create mode 100644 app/react/components/TLSFieldset/index.ts create mode 100644 app/react/components/TLSFieldset/types.ts diff --git a/api/http/handler/endpoints/endpoint_update.go b/api/http/handler/endpoints/endpoint_update.go index 8e2400512..6e2857363 100644 --- a/api/http/handler/endpoints/endpoint_update.go +++ b/api/http/handler/endpoints/endpoint_update.go @@ -4,6 +4,7 @@ import ( "net/http" "reflect" "strconv" + "strings" httperror "github.com/portainer/libhttp/error" "github.com/portainer/libhttp/request" @@ -246,7 +247,10 @@ func (handler *Handler) endpointUpdate(w http.ResponseWriter, r *http.Request) * } } - if (payload.URL != nil && *payload.URL != endpoint.URL) || (payload.TLS != nil && endpoint.TLSConfig.TLS != *payload.TLS) || endpoint.Type == portainer.AzureEnvironment { + if (payload.URL != nil && *payload.URL != endpoint.URL) || + (payload.TLS != nil && endpoint.TLSConfig.TLS != *payload.TLS) || + endpoint.Type == portainer.AzureEnvironment || + shouldReloadTLSConfiguration(endpoint, &payload) { handler.ProxyManager.DeleteEndpointProxy(endpoint.ID) _, err = handler.ProxyManager.CreateAndRegisterEndpointProxy(endpoint) if err != nil { @@ -285,3 +289,22 @@ func (handler *Handler) endpointUpdate(w http.ResponseWriter, r *http.Request) * return response.JSON(w, endpoint) } + +func shouldReloadTLSConfiguration(endpoint *portainer.Endpoint, payload *endpointUpdatePayload) bool { + // When updating Docker API environment, as long as TLS is true and TLSSkipVerify is false, + // we assume that new TLS files have been uploaded and we need to reload the TLS configuration. + if endpoint.Type != portainer.DockerEnvironment || + !strings.HasPrefix(*payload.URL, "tcp://") || + payload.TLS == nil || !*payload.TLS { + return false + } + + if payload.TLSSkipVerify != nil && !*payload.TLSSkipVerify { + return true + } + + if payload.TLSSkipClientVerify != nil && !*payload.TLSSkipClientVerify { + return true + } + return false +} diff --git a/app/portainer/components/index.js b/app/portainer/components/index.js index fb0f89632..033af9f17 100644 --- a/app/portainer/components/index.js +++ b/app/portainer/components/index.js @@ -8,8 +8,9 @@ import { boxSelectorModule } from './BoxSelector'; import { beFeatureIndicator } from './BEFeatureIndicator'; import { InformationPanelAngular } from './InformationPanel'; import { gitFormModule } from './forms/git-form'; +import { tlsFieldsetModule } from './tls-fieldset'; export default angular - .module('portainer.app.components', [boxSelectorModule, widgetModule, gitFormModule, porAccessManagementModule, formComponentsModule]) + .module('portainer.app.components', [boxSelectorModule, widgetModule, gitFormModule, porAccessManagementModule, formComponentsModule, tlsFieldsetModule]) .component('informationPanel', InformationPanelAngular) .component('beFeatureIndicator', beFeatureIndicator).name; diff --git a/app/portainer/components/tls-fieldset/index.ts b/app/portainer/components/tls-fieldset/index.ts new file mode 100644 index 000000000..32ab1f820 --- /dev/null +++ b/app/portainer/components/tls-fieldset/index.ts @@ -0,0 +1,22 @@ +import angular from 'angular'; + +import { + TLSFieldset, + tlsConfigValidation, +} from '@/react/components/TLSFieldset'; +import { withFormValidation } from '@/react-tools/withFormValidation'; + +export const ngModule = angular.module( + 'portainer.app.components.tls-fieldset', + [] +); + +export const tlsFieldsetModule = ngModule.name; + +withFormValidation( + ngModule, + TLSFieldset, + 'tlsFieldset', + [], + tlsConfigValidation +); diff --git a/app/portainer/views/endpoints/edit/endpoint.html b/app/portainer/views/endpoints/edit/endpoint.html index 3e32a3cc9..9d969b2b5 100644 --- a/app/portainer/views/endpoints/edit/endpoint.html +++ b/app/portainer/views/endpoints/edit/endpoint.html @@ -72,7 +72,7 @@