Chinthakayala, Sheshashailavas (sc2914) | d156997 | 2017-08-28 05:25:46 -0900 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2013, 2014 IBM Corp. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | **/ |
| 16 | |
| 17 | var when = require('when'); |
| 18 | |
| 19 | var storageModule; |
| 20 | var settingsAvailable; |
| 21 | |
| 22 | function moduleSelector(aSettings) { |
| 23 | var toReturn; |
| 24 | if (aSettings.storageModule) { |
| 25 | if (typeof aSettings.storageModule === "string") { |
| 26 | // TODO: allow storage modules to be specified by absolute path |
| 27 | toReturn = require("./"+aSettings.storageModule); |
| 28 | } else { |
| 29 | toReturn = aSettings.storageModule; |
| 30 | } |
| 31 | } else { |
| 32 | toReturn = require("./localfilesystem"); |
| 33 | } |
| 34 | return toReturn; |
| 35 | } |
| 36 | |
| 37 | function is_malicious(path) { |
| 38 | return path.indexOf('../') != -1 || path.indexOf('..\\') != -1; |
| 39 | } |
| 40 | |
| 41 | var storageModuleInterface = { |
| 42 | init: function(settings) { |
| 43 | try { |
| 44 | storageModule = moduleSelector(settings); |
| 45 | settingsAvailable = storageModule.hasOwnProperty("getSettings") && storageModule.hasOwnProperty("saveSettings"); |
| 46 | } catch (e) { |
| 47 | return when.reject(e); |
| 48 | } |
| 49 | return storageModule.init(settings); |
| 50 | }, |
| 51 | getFlows: function() { |
| 52 | return storageModule.getFlows(); |
| 53 | }, |
| 54 | saveFlows: function(flows) { |
| 55 | return storageModule.saveFlows(flows); |
| 56 | }, |
| 57 | getCredentials: function() { |
| 58 | return storageModule.getCredentials(); |
| 59 | }, |
| 60 | saveCredentials: function(credentials) { |
| 61 | return storageModule.saveCredentials(credentials); |
| 62 | }, |
| 63 | getSettings: function() { |
| 64 | if (settingsAvailable) { |
| 65 | return storageModule.getSettings(); |
| 66 | } else { |
| 67 | return when.resolve(null); |
| 68 | } |
| 69 | }, |
| 70 | saveSettings: function(settings) { |
| 71 | if (settingsAvailable) { |
| 72 | return storageModule.saveSettings(settings); |
| 73 | } else { |
| 74 | return when.resolve(); |
| 75 | } |
| 76 | }, |
| 77 | /* Library Functions */ |
| 78 | getAllFlows: function() { |
| 79 | return storageModule.getAllFlows(); |
| 80 | }, |
| 81 | getFlow: function(fn) { |
| 82 | if (is_malicious(fn)) { |
| 83 | return when.reject(new Error('forbidden flow name')); |
| 84 | } |
| 85 | return storageModule.getFlow(fn); |
| 86 | }, |
| 87 | saveFlow: function(fn, data) { |
| 88 | if (is_malicious(fn)) { |
| 89 | return when.reject(new Error('forbidden flow name')); |
| 90 | } |
| 91 | return storageModule.saveFlow(fn, data); |
| 92 | }, |
| 93 | getLibraryEntry: function(type, path) { |
| 94 | if (is_malicious(path)) { |
| 95 | return when.reject(new Error('forbidden flow name')); |
| 96 | } |
| 97 | return storageModule.getLibraryEntry(type, path); |
| 98 | }, |
| 99 | saveLibraryEntry: function(type, path, meta, body) { |
| 100 | if (is_malicious(path)) { |
| 101 | return when.reject(new Error('forbidden flow name')); |
| 102 | } |
| 103 | return storageModule.saveLibraryEntry(type, path, meta, body); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | module.exports = storageModuleInterface; |