Skip to content

Commit

Permalink
Merge pull request #5 from ZEXSM/bugfix/use-array
Browse files Browse the repository at this point in the history
fix use array. fix check chipher marker
  • Loading branch information
ZEXSM authored Jul 4, 2021
2 parents d2984a7 + a66a3bd commit 5af2e08
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ yarn add node-scc-config
return;
}
const keyArrayMatch = key.match(/^(\S+)\[\d+\]$/);
if (keyArrayMatch) {
const [, keyArray] = keyArrayMatch;
if (!obj[keyArray]) {
obj[keyArray] = [value];
} else {
obj[keyArray].push(value);
}
return;
}
if (keys.length === 0) {
obj[key] = value;
return;
Expand Down
11 changes: 9 additions & 2 deletions example/response.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
"options.service.url": "http://app-service/endpoint",
"options.service.login": "tech",
"options.service.password": "{cipher}5eec033b24bc736fcbdbe1cf496149fbd1db16fd096264326e9c18f80d85b742",
"testOptions.url": "http://app-service/endpoint-test"
"testOptions.url": "http://app-service/endpoint-test",
"options.service.whitelist[0]": 4441,
"options.service.whitelist[1]": 4442,
"options.service.whitelist[2]": 4443
}
},
{
Expand All @@ -23,7 +26,11 @@
"options.service.url": "http://app-service/endpoint",
"options.service.login": "defaultTech",
"options.service.password": "{cipher}5eec033b24bc736fcbdbe1cf496149fbd1db16fd096264326e9c18f80d85b742",
"testOptions.url": "http://app-service/endpoint-test"
"testOptions.url": "http://app-service/endpoint-test",
"options.service.whitelist[0]": 3331,
"options.service.whitelist[1]": 3332,
"options.service.whitelist[2]": 3333,
"options.service.whitelist[3]": 3334
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion example/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express from "express";
import nock from "nock";

import { SpringCloudConfigClient } from "../src";
import { getConfiguration, TConfiguration } from "../src/configuration";
import { getConfiguration } from "../src/configuration";
import { ServiceDecryptor } from "../src/decryptors";

const app = express();
Expand Down
29 changes: 23 additions & 6 deletions src/configuration/configuration-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class ConfigurationStore<T> implements IConfigurationStore {
}

if (this.decryptor) {
source = await this.decriptSource(source);
source = await this.decryptSource(source);
}

let sourceObj: Record<string, any> = {};
Expand All @@ -101,7 +101,7 @@ class ConfigurationStore<T> implements IConfigurationStore {
} else {
for (const [key, value] of Object.entries(source)) {
const keys = key.split('.');
this.createSourceObject(keys, sourceObj, value);
this.toDeepSource(keys, sourceObj, value);
}
}

Expand All @@ -112,13 +112,27 @@ class ConfigurationStore<T> implements IConfigurationStore {
return JSON.parse(process.env[this.storeKey] || '{}') || {};
}

private createSourceObject(keys: string[], obj: Record<string, any>, value: string) {
private toDeepSource(keys: string[], obj: Record<string, any>, value: string) {
const key = keys.shift();

if (!key) {
return;
}

const keyArrayMatch = key.match(/^(\S+)\[\d+\]$/);

if (keyArrayMatch) {
const [, keyArray] = keyArrayMatch;

if (!obj[keyArray]) {
obj[keyArray] = [value];
} else {
obj[keyArray].push(value);
}

return;
}

if (keys.length === 0) {
obj[key] = value;
return;
Expand All @@ -128,12 +142,15 @@ class ConfigurationStore<T> implements IConfigurationStore {
obj[key] = {};
}

this.createSourceObject(keys, obj[key], value);
this.toDeepSource(keys, obj[key], value);
}

private async decriptSource(source: Record<string, any>): Promise<Record<string, any>> {
private async decryptSource(source: Record<string, any>): Promise<Record<string, any>> {
for (const key of Object.keys(source)) {
if (source[key]?.startsWith(this.chipherMarker)) {
if (source[key]
&& typeof source[key] === 'string'
&& source[key].startsWith(this.chipherMarker)) {

const data = source[key].replace(this.chipherMarker, '');

const decriyptingData = await this.decryptor!.decrypt(data);
Expand Down

0 comments on commit 5af2e08

Please sign in to comment.