From fe5e8d0853b9f067f8e7a446a0f3a3b450d99458 Mon Sep 17 00:00:00 2001 From: Marco Enrico Piras Date: Thu, 24 May 2018 11:17:07 +0200 Subject: [PATCH 1/2] Fix regexp to globally clean up the RC file --- .../cloud-provider-metadata.service.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/app/shared/service/cloud-provider-metadata/cloud-provider-metadata.service.ts b/src/app/shared/service/cloud-provider-metadata/cloud-provider-metadata.service.ts index f14b313..3e9e043 100644 --- a/src/app/shared/service/cloud-provider-metadata/cloud-provider-metadata.service.ts +++ b/src/app/shared/service/cloud-provider-metadata/cloud-provider-metadata.service.ts @@ -82,17 +82,14 @@ export class CloudProviderMetadataService { public parseRcFile(rcFile: string, password: string): OpenStackCredentials { if (rcFile) { - - // update RC file with the user password and set it as current RC file // console.log("The current RC file...", rcFile); - rcFile = rcFile.replace(/#.*\n/, ''); // remove all comments - rcFile = rcFile.replace(/\becho\b.+\n/, ''); // remove all echo commands - rcFile = rcFile.replace(/\bread\b.+\n/, ''); // remove the read command - rcFile = rcFile.replace(/(\bexport OS_PASSWORD=)(.*)/, // set the password + rcFile = rcFile.replace(/#.*\n/g, ''); // remove all comments + rcFile = rcFile.replace(/\becho\b.+/g, ''); // remove all echo commands + rcFile = rcFile.replace(/\bread\b.+/g, ''); // remove the read command + rcFile = rcFile.replace(/(\bexport OS_PASSWORD=)(.*)/, // set the password "$1" + '"' + password + '"'); - // extract all the required RC file fields required to query the TSI portal let rcVersion = CloudProviderMetadataService.extractPropertyValue(rcFile, "OS_IDENTITY_API_VERSION"); let username = CloudProviderMetadataService.extractPropertyValue(rcFile, "OS_USERNAME"); From 46d83e08c3f6178090f3ee8ac1a6eea1635f47d6 Mon Sep 17 00:00:00 2001 From: Marco Enrico Piras Date: Thu, 24 May 2018 11:24:32 +0200 Subject: [PATCH 2/2] Extract the last value of multi-valued properties --- .../cloud-provider-metadata.service.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/app/shared/service/cloud-provider-metadata/cloud-provider-metadata.service.ts b/src/app/shared/service/cloud-provider-metadata/cloud-provider-metadata.service.ts index 3e9e043..769969a 100644 --- a/src/app/shared/service/cloud-provider-metadata/cloud-provider-metadata.service.ts +++ b/src/app/shared/service/cloud-provider-metadata/cloud-provider-metadata.service.ts @@ -119,13 +119,18 @@ export class CloudProviderMetadataService { private static extractPropertyValue(rcFile: string, propertyName: string): string { let match; let result: string = null; - let pattern = new RegExp(propertyName + "=(.+)"); + let pattern = new RegExp(propertyName + "=(.+)", 'g'); // extract property if (rcFile) { - if ((match = pattern.exec(rcFile)) !== null) { - result = match[1].replace(/\"/g, ""); - } + // search for all matches and use only the last one + do { + match = pattern.exec(rcFile); + if(match) { + // remove single and double quotes + result = match[1].replace(/['"]/g, ""); + } + }while(match); } return result; }