|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +let Edge = process.env.MOCK_AKA_SEC_API == 'true' ? require("../mock/edgeClient") : require("./edgeClient"); |
| 4 | + |
| 5 | +let util = require('util'); |
| 6 | +let URIs = require("./constants").URIS; |
| 7 | +let ConfigProvider = require('./configProvider').configProvider; |
| 8 | +let logger = require("./constants").logger("AppSecConfig"); |
| 9 | + |
1 | 10 | require("string-format");
|
2 |
| -// var URIs = require("../constants").URIS; |
3 | 11 | var fs = require("fs");
|
4 |
| -// var logger = require('pino')({ |
5 |
| -// level: process.env.LOG_LEVEL ? process.env.LOG_LEVEL : "error", |
6 |
| -// prettyPrint: true, |
7 |
| -// name: "AppSecConfig" |
8 |
| -// }); |
9 | 12 | const CRB_TEMPLATE_PATH=__dirname + '/../templates/crbTemplate.json';
|
10 | 13 | const ENCODING='utf8';
|
11 | 14 |
|
12 | 15 | class CRBHandler {
|
| 16 | + |
| 17 | + constructor(auth) { |
| 18 | + this._edge = new Edge(auth); |
| 19 | + this._configProvider = new ConfigProvider(this._edge); |
| 20 | + } |
| 21 | + _getConfigId(configId) { |
| 22 | + if (!configId) { |
| 23 | + if (this.configs().length == 1) { |
| 24 | + configId = this.configs()[0].id; |
| 25 | + } else { |
| 26 | + throw "You have more than one configuration. Please provide a configuration id to work with."; |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
13 | 31 | template() {
|
14 | 32 | return new Promise((resolve) => {
|
15 | 33 | resolve(fs.readFileSync(CRB_TEMPLATE_PATH, ENCODING));
|
16 | 34 | });
|
17 | 35 | }
|
| 36 | + rules(providedConfigId) { |
| 37 | + let configId = this._configProvider.getConfigId(providedConfigId); |
| 38 | + return new Promise((resolve, reject) => { |
| 39 | + let customRulesUrl = util.format(URIs.GET_CRB, configId); |
| 40 | + logger.debug("Attempting to get all custom rules at: " + customRulesUrl); |
| 41 | + let request = { |
| 42 | + method: "GET", |
| 43 | + path: customRulesUrl, |
| 44 | + followRedirect: false |
| 45 | + }; |
| 46 | + this._edge.get(request).then(response => { |
| 47 | + resolve(response); |
| 48 | + }).catch(err => { |
| 49 | + reject(err); |
| 50 | + }); |
| 51 | + }); |
| 52 | + } |
18 | 53 | }
|
19 | 54 | module.exports = {
|
20 | 55 | CRBHandler: CRBHandler
|
|
0 commit comments