From 5bbf5887b9ab244e999006865a691a74fbd858b9 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Thu, 25 Apr 2024 13:15:05 +0200 Subject: [PATCH 01/13] fix(salix): refs #7272 #7272 Bug when acl not loaded --- front/core/services/auth.js | 69 +++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 15 deletions(-) diff --git a/front/core/services/auth.js b/front/core/services/auth.js index 753bc3fba..55a4cea7e 100644 --- a/front/core/services/auth.js +++ b/front/core/services/auth.js @@ -7,10 +7,12 @@ import UserError from 'core/lib/user-error'; * @property {Boolean} loggedIn Whether the user is currently logged */ export default class Auth { - constructor($http, $q, $state, $transitions, $window, vnToken, vnModules, aclService) { + constructor($http, $q, vnApp, $translate, $state, $transitions, $window, vnToken, vnModules, aclService) { Object.assign(this, { $http, $q, + vnApp, + $translate, $state, $transitions, $window, @@ -39,9 +41,31 @@ export default class Auth { }; if (this.vnToken.token) { - return this.loadAcls() - .then(() => true) - .catch(redirectToLogin); + const maxRetries = 5; + let retryCount = 0; + const retryDelay = 2000; // Milisegundos (1 segundo) + + const loadAclsWithRetry = () => { + return this.loadAcls(maxRetries) + .then(() => true) + .catch(error => { + retryCount++; + if (retryCount < maxRetries) { + return new Promise(resolve => { + setTimeout(() => { + this.vnApp.showMessage(this.$translate.instant('Loading...')); + resolve(loadAclsWithRetry(maxRetries - retryCount)); + }, retryDelay); + }); + } else { + // Retry limit reached, redirect to login + return redirectToLogin(); + } + }); + }; + + // Start loading ACLs with retry + return loadAclsWithRetry(); } else return redirectToLogin(); }); @@ -113,18 +137,33 @@ export default class Auth { return promise; } - loadAcls() { - return this.aclService.load() - .then(() => { - this.loggedIn = true; - this.vnModules.reset(); - }) - .catch(err => { - this.vnToken.unset(); - throw err; - }); + loadAcls(maxRetries = 1) { + const attemptLoad = retryCount => { + return this.aclService.load() + .then(() => { + this.loggedIn = true; + this.vnModules.reset(); + }) + .catch(err => { + if (retryCount >= maxRetries) { + this.vnToken.unset(); + throw err; + } else { + // Retry after delay + return new Promise((resolve, reject) => { + reject(new Error('Error al cargar los ACLs')); + }); + } + }); + }; + + // Start loading with retry + return attemptLoad(1); } } -Auth.$inject = ['$http', '$q', '$state', '$transitions', '$window', 'vnToken', 'vnModules', 'aclService']; +Auth.$inject = [ + '$http', '$q', 'vnApp', '$translate', '$state', + '$transitions', '$window', 'vnToken', 'vnModules', + 'aclService']; ngModule.service('vnAuth', Auth); From ab87929b2d1a236809361d0de60c999a2ac0de47 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Mon, 29 Apr 2024 12:50:17 +0200 Subject: [PATCH 02/13] fix(salix): refs #7272 #7272 i18n Error --- front/core/locale/es.yml | 1 + front/core/services/auth.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/front/core/locale/es.yml b/front/core/locale/es.yml index 17e955ff5..7fcb8c16b 100644 --- a/front/core/locale/es.yml +++ b/front/core/locale/es.yml @@ -69,3 +69,4 @@ Send cau: Enviar cau By sending this ticket, all the data related to the error, the section, the user, etc., are already sent.: Al enviar este cau ya se envían todos los datos relacionados con el error, la sección, el usuario, etc ExplainReason: Explique el motivo por el que no deberia aparecer este fallo You already have the mailAlias: Ya tienes este alias de correo +Error loading ACLs: Error al cargar los ACLs diff --git a/front/core/services/auth.js b/front/core/services/auth.js index 55a4cea7e..fc8ba2d63 100644 --- a/front/core/services/auth.js +++ b/front/core/services/auth.js @@ -151,7 +151,7 @@ export default class Auth { } else { // Retry after delay return new Promise((resolve, reject) => { - reject(new Error('Error al cargar los ACLs')); + reject(new Error(this.$translate.instant('Error loading ACLs'))); }); } }); From 596c0f78d08e214677f7d2b6cc225c62654c37b6 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 30 Apr 2024 11:24:36 +0200 Subject: [PATCH 03/13] fix(salix): refs #7272 #7272 Add aclService in routes.js --- front/salix/routes.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/front/salix/routes.js b/front/salix/routes.js index 8621f83c7..be210b749 100644 --- a/front/salix/routes.js +++ b/front/salix/routes.js @@ -12,7 +12,8 @@ function config($stateProvider, $urlRouterProvider) { template: '', resolve: { config: ['vnConfig', vnConfig => vnConfig.initialize()], - token: ['vnToken', vnToken => vnToken.fetchConfig()] + token: ['vnToken', vnToken => vnToken.fetchConfig()], + acl: ['aclService', aclService => aclService.load()] } }) .state('outLayout', { From 6277c3da0968d2a42249e5d1f3292cd5fc1acc8a Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 30 Apr 2024 11:26:12 +0200 Subject: [PATCH 04/13] fix(salix): refs #7272 #7272 Remove aclService from auth.js --- front/core/services/auth.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/front/core/services/auth.js b/front/core/services/auth.js index fc8ba2d63..c3aa6a3c7 100644 --- a/front/core/services/auth.js +++ b/front/core/services/auth.js @@ -7,18 +7,15 @@ import UserError from 'core/lib/user-error'; * @property {Boolean} loggedIn Whether the user is currently logged */ export default class Auth { - constructor($http, $q, vnApp, $translate, $state, $transitions, $window, vnToken, vnModules, aclService) { + constructor($http, $q, $state, $transitions, $window, vnToken, vnModules) { Object.assign(this, { $http, $q, - vnApp, - $translate, $state, $transitions, $window, vnToken, vnModules, - aclService, loggedIn: false }); } @@ -111,13 +108,11 @@ export default class Auth { headers: {Authorization: json.data.token} }).then(({data}) => { this.vnToken.set(json.data.token, data.multimediaToken.id, now, json.data.ttl, remember); - this.loadAcls().then(() => { - let continueHash = this.$state.params.continue; - if (continueHash) - this.$window.location = continueHash; - else - this.$state.go('home'); - }); + let continueHash = this.$state.params.continue; + if (continueHash) + this.$window.location = continueHash; + else + this.$state.go('home'); }).catch(() => {}); } @@ -131,7 +126,7 @@ export default class Auth { this.vnToken.unset(); this.loggedIn = false; this.vnModules.reset(); - this.aclService.reset(); + this.vnModules.aclService.reset(); this.$state.go('login'); return promise; From 43cedef2597b580d03b1b4e7df08e98486c91d9d Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 30 Apr 2024 11:26:31 +0200 Subject: [PATCH 05/13] fix(salix): refs #7272 #7272 Call validateToken --- front/core/services/auth.js | 67 +++++++++---------------------------- 1 file changed, 16 insertions(+), 51 deletions(-) diff --git a/front/core/services/auth.js b/front/core/services/auth.js index c3aa6a3c7..28feb1090 100644 --- a/front/core/services/auth.js +++ b/front/core/services/auth.js @@ -38,31 +38,12 @@ export default class Auth { }; if (this.vnToken.token) { - const maxRetries = 5; - let retryCount = 0; - const retryDelay = 2000; // Milisegundos (1 segundo) - - const loadAclsWithRetry = () => { - return this.loadAcls(maxRetries) - .then(() => true) - .catch(error => { - retryCount++; - if (retryCount < maxRetries) { - return new Promise(resolve => { - setTimeout(() => { - this.vnApp.showMessage(this.$translate.instant('Loading...')); - resolve(loadAclsWithRetry(maxRetries - retryCount)); - }, retryDelay); - }); - } else { - // Retry limit reached, redirect to login - return redirectToLogin(); - } - }); - }; - - // Start loading ACLs with retry - return loadAclsWithRetry(); + return this.validateToken() + .then(() => true) + .catch(err => { + console.error(err); + redirectToLogin(); + }); } else return redirectToLogin(); }); @@ -132,33 +113,17 @@ export default class Auth { return promise; } - loadAcls(maxRetries = 1) { - const attemptLoad = retryCount => { - return this.aclService.load() - .then(() => { - this.loggedIn = true; - this.vnModules.reset(); - }) - .catch(err => { - if (retryCount >= maxRetries) { - this.vnToken.unset(); - throw err; - } else { - // Retry after delay - return new Promise((resolve, reject) => { - reject(new Error(this.$translate.instant('Error loading ACLs'))); - }); - } - }); - }; - - // Start loading with retry - return attemptLoad(1); + validateToken() { + return this.$http.get('VnUsers/validateToken') + .then(() => { + this.loggedIn = true; + this.vnModules.reset(); + }) + .catch(err => { + throw err; + }); } } -Auth.$inject = [ - '$http', '$q', 'vnApp', '$translate', '$state', - '$transitions', '$window', 'vnToken', 'vnModules', - 'aclService']; +Auth.$inject = ['$http', '$q', '$state', '$transitions', '$window', 'vnToken', 'vnModules']; ngModule.service('vnAuth', Auth); From b4284889e14db631753b607dea1a4423be27d49f Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 30 Apr 2024 11:35:07 +0200 Subject: [PATCH 06/13] fix(salix): refs #7272 #7272 Front retry calls --- front/core/services/auth.js | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/front/core/services/auth.js b/front/core/services/auth.js index 28feb1090..3337dfdb6 100644 --- a/front/core/services/auth.js +++ b/front/core/services/auth.js @@ -7,10 +7,12 @@ import UserError from 'core/lib/user-error'; * @property {Boolean} loggedIn Whether the user is currently logged */ export default class Auth { - constructor($http, $q, $state, $transitions, $window, vnToken, vnModules) { + constructor($http, $q, vnApp, $translate, $state, $transitions, $window, vnToken, vnModules) { Object.assign(this, { $http, $q, + vnApp, + $translate, $state, $transitions, $window, @@ -38,12 +40,28 @@ export default class Auth { }; if (this.vnToken.token) { - return this.validateToken() - .then(() => true) - .catch(err => { - console.error(err); - redirectToLogin(); - }); + const loadWithRetry = () => { + return this.validateToken() + .then(() => true) + .catch(err => { + switch (err.status) { + case 400: + case 401: + redirectToLogin(); + break; + default: + return new Promise(resolve => { + setTimeout(() => { + this.vnApp.showMessage(this.$translate.instant('Loading...')); + + resolve(loadWithRetry()); + }, 2000); + }); + } + console.error(err); + }); + }; + return loadWithRetry(); } else return redirectToLogin(); }); @@ -124,6 +142,8 @@ export default class Auth { }); } } -Auth.$inject = ['$http', '$q', '$state', '$transitions', '$window', 'vnToken', 'vnModules']; +Auth.$inject = [ + '$http', '$q', 'vnApp', '$translate', '$state', + '$transitions', '$window', 'vnToken', 'vnModules']; ngModule.service('vnAuth', Auth); From be3ee43b06183623fbe79c7dca69750c4d345a2c Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 30 Apr 2024 11:35:30 +0200 Subject: [PATCH 07/13] fix(salix): refs #7272 #7272 Back validateToken endpoint --- back/methods/vn-user/renew-token.js | 10 +-------- back/methods/vn-user/validate-token.js | 30 ++++++++++++++++++++++++++ back/models/vn-user.js | 1 + back/models/vn-user.json | 7 ++++++ 4 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 back/methods/vn-user/validate-token.js diff --git a/back/methods/vn-user/renew-token.js b/back/methods/vn-user/renew-token.js index 8e5ffc095..52b8606a4 100644 --- a/back/methods/vn-user/renew-token.js +++ b/back/methods/vn-user/renew-token.js @@ -18,15 +18,7 @@ module.exports = Self => { Self.renewToken = async function(ctx) { const {accessToken: token} = ctx.req; - // Check if current token is valid - - const {renewPeriod, courtesyTime} = await models.AccessTokenConfig.findOne({ - fields: ['renewPeriod', 'courtesyTime'] - }); - const now = Date.now(); - const differenceMilliseconds = now - token.created; - const differenceSeconds = Math.floor(differenceMilliseconds / 1000); - const isNotExceeded = differenceSeconds < renewPeriod - courtesyTime; + const isNotExceeded = await Self.validateToken(ctx); if (isNotExceeded) return token; diff --git a/back/methods/vn-user/validate-token.js b/back/methods/vn-user/validate-token.js new file mode 100644 index 000000000..3b75c7c34 --- /dev/null +++ b/back/methods/vn-user/validate-token.js @@ -0,0 +1,30 @@ +const {models} = require('vn-loopback/server/server'); +module.exports = Self => { + Self.remoteMethodCtx('validateToken', { + description: 'Validates the current logged user token', + accepts: [], + accessType: 'READ', + returns: { + type: 'Boolean', + root: true + }, + http: { + path: `/validateToken`, + verb: 'GET' + } + }); + + Self.validateToken = async function(ctx) { + const {accessToken: token} = ctx.req; + + // Check if current token is valid + const {renewPeriod, courtesyTime} = await models.AccessTokenConfig.findOne({ + fields: ['renewPeriod', 'courtesyTime'] + }); + const now = Date.now(); + const differenceMilliseconds = now - token.created; + const differenceSeconds = Math.floor(differenceMilliseconds / 1000); + const isNotExceeded = differenceSeconds < renewPeriod - courtesyTime; + return isNotExceeded; + }; +}; diff --git a/back/models/vn-user.js b/back/models/vn-user.js index b59f13ffa..d38fe5a92 100644 --- a/back/models/vn-user.js +++ b/back/models/vn-user.js @@ -15,6 +15,7 @@ module.exports = function(Self) { require('../methods/vn-user/renew-token')(Self); require('../methods/vn-user/share-token')(Self); require('../methods/vn-user/update-user')(Self); + require('../methods/vn-user/validate-token')(Self); Self.definition.settings.acls = Self.definition.settings.acls.filter(acl => acl.property !== 'create'); diff --git a/back/models/vn-user.json b/back/models/vn-user.json index 5f6ac3f47..8e3304085 100644 --- a/back/models/vn-user.json +++ b/back/models/vn-user.json @@ -113,6 +113,13 @@ "principalId": "$everyone", "permission": "ALLOW" }, + { + "property": "validateToken", + "accessType": "EXECUTE", + "principalType": "ROLE", + "principalId": "$authenticated", + "permission": "ALLOW" + }, { "property": "privileges", "accessType": "*", From ae914866412b8844a4273c6c3e1db35ee5ed3bf1 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Thu, 2 May 2024 10:36:18 +0200 Subject: [PATCH 08/13] test(salix): refs #7272 #7272 fix renew-token.spec --- back/methods/vn-user/renew-token.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/back/methods/vn-user/renew-token.js b/back/methods/vn-user/renew-token.js index 52b8606a4..5581d19ac 100644 --- a/back/methods/vn-user/renew-token.js +++ b/back/methods/vn-user/renew-token.js @@ -18,6 +18,9 @@ module.exports = Self => { Self.renewToken = async function(ctx) { const {accessToken: token} = ctx.req; + const {courtesyTime} = await models.AccessTokenConfig.findOne({ + fields: ['courtesyTime'] + }); const isNotExceeded = await Self.validateToken(ctx); if (isNotExceeded) return token; From da3c4cb8fcbb3df37ad123f519af93bb92c299af Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 3 May 2024 08:24:10 +0200 Subject: [PATCH 09/13] fix(salix): refs #7272 #7272 Errors when Token not exists --- front/core/services/auth.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/front/core/services/auth.js b/front/core/services/auth.js index 3337dfdb6..0cae4bae8 100644 --- a/front/core/services/auth.js +++ b/front/core/services/auth.js @@ -47,8 +47,7 @@ export default class Auth { switch (err.status) { case 400: case 401: - redirectToLogin(); - break; + return redirectToLogin(); default: return new Promise(resolve => { setTimeout(() => { @@ -58,7 +57,6 @@ export default class Auth { }, 2000); }); } - console.error(err); }); }; return loadWithRetry(); From 5d8340989235ffc8126cb4edf3af9ec98a1f814f Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Mon, 27 May 2024 09:31:42 +0200 Subject: [PATCH 10/13] feat: refs #7442 Kubernetes deploy --- Jenkinsfile | 9 ++++++++- package.json | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 6f0a642b4..661b96073 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -215,7 +215,14 @@ pipeline { def packageJson = readJSON file: 'package.json' env.VERSION = packageJson.version } - sh "docker stack deploy --with-registry-auth --compose-file docker-compose.yml ${env.STACK_NAME}" + withKubeConfig([ + serverUrl: "$KUBERNETES_API", + credentialsId: 'kubernetes', + namespace: 'salix' + ]) { + sh 'kubectl set image deployment/salix-back-$BRANCH salix-back-$BRANCH=$REGISTRY/salix-back:$VERSION' + sh 'kubectl set image deployment/salix-front-$BRANCH salix-front-$BRANCH=$REGISTRY/salix-front:$VERSION' + } } } } diff --git a/package.json b/package.json index 390b61be1..be3dcca04 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "salix-back", - "version": "24.22.0", + "version": "24.22.1", "author": "Verdnatura Levante SL", "description": "Salix backend", "license": "GPL-3.0", From a18df86abdc8035d9686f377b9c600ebd7eb3f63 Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Mon, 27 May 2024 09:37:17 +0200 Subject: [PATCH 11/13] fix: refs #7442 Fix kubernetes deploy --- Jenkinsfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 661b96073..9e032fa21 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -220,8 +220,8 @@ pipeline { credentialsId: 'kubernetes', namespace: 'salix' ]) { - sh 'kubectl set image deployment/salix-back-$BRANCH salix-back-$BRANCH=$REGISTRY/salix-back:$VERSION' - sh 'kubectl set image deployment/salix-front-$BRANCH salix-front-$BRANCH=$REGISTRY/salix-front:$VERSION' + sh 'kubectl set image deployment/salix-back-$BRANCH_NAME salix-back-$BRANCH_NAME=$REGISTRY/salix-back:$VERSION' + sh 'kubectl set image deployment/salix-front-$BRANCH_NAME salix-front-$BRANCH_NAME=$REGISTRY/salix-front:$VERSION' } } } From 52e9c662719ddbbbb13c4e37cd97cbb06f121747 Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Mon, 27 May 2024 09:39:51 +0200 Subject: [PATCH 12/13] fix: refs #7442 Fix kubernetes deploy --- Jenkinsfile | 2 +- docker-compose.yml | 54 ---------------------------------------------- 2 files changed, 1 insertion(+), 55 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 9e032fa21..e9ef4816b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -203,7 +203,7 @@ pipeline { sh 'npx myt push $NODE_ENV --force --commit' } } - stage('Docker') { + stage('Kubernetes') { when { expression { FROM_GIT } } diff --git a/docker-compose.yml b/docker-compose.yml index ec40311c0..923bb5780 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,62 +4,8 @@ services: image: registry.verdnatura.es/salix-front:${VERSION:?} build: context: front - environment: - - TZ - - NODE_ENV - ports: - - 80 - deploy: - replicas: 2 - placement: - constraints: - - node.role == worker - resources: - limits: - memory: 1G back: image: registry.verdnatura.es/salix-back:${VERSION:?} build: context: . dockerfile: back/Dockerfile - environment: - - TZ - - NODE_ENV - - DEBUG - ports: - - 3000 - configs: - - source: datasources - target: /etc/salix/datasources.json - - source: datasources_local - target: /etc/salix/datasources.local.json - - source: print - target: /etc/salix/print.json - - source: print_local - target: /etc/salix/print.local.json - volumes: - - /mnt/appdata/pdfs:/var/lib/salix/pdfs - - /mnt/appdata/dms:/var/lib/salix/dms - - /mnt/appdata/image:/var/lib/salix/image - - /mnt/appdata/vn-access:/var/lib/salix/vn-access - deploy: - replicas: ${BACK_REPLICAS:?} - placement: - constraints: - - node.role == worker - resources: - limits: - memory: 8G -configs: - datasources: - external: true - name: salix_datasources - datasources_local: - external: true - name: salix-${BRANCH_NAME:?}_datasources - print: - external: true - name: salix_print - print_local: - external: true - name: salix-${BRANCH_NAME:?}_print From b675bcd6cedb2d84be6dedc8770158c4d36e37bb Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Mon, 27 May 2024 10:07:44 +0200 Subject: [PATCH 13/13] fix: refs #7442 Fix kubernetes deploy --- Jenkinsfile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index e9ef4816b..9d6c57a64 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -24,6 +24,7 @@ node { FROM_GIT = env.JOB_NAME.startsWith('gitea/') RUN_TESTS = !PROTECTED_BRANCH && FROM_GIT RUN_BUILD = PROTECTED_BRANCH && FROM_GIT + // https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables echo "NODE_NAME: ${env.NODE_NAME}" echo "WORKSPACE: ${env.WORKSPACE}" @@ -207,9 +208,6 @@ pipeline { when { expression { FROM_GIT } } - environment { - DOCKER_HOST = "${env.SWARM_HOST}" - } steps { script { def packageJson = readJSON file: 'package.json'