summaryrefslogtreecommitdiffstats
path: root/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/DBLIBResourceProvider.java
blob: 6d36d638970ccdf39a3939db2d4f9edd5b131d86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*-
 * ============LICENSE_START=======================================================
 * onap
 * ================================================================================
 * Copyright (C) 2016 - 2017 ONAP
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.ccsdk.sli.core.dblib;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Optional;
import java.util.Properties;
import java.util.Vector;

import org.onap.ccsdk.sli.core.utils.JREFileResolver;
import org.onap.ccsdk.sli.core.utils.KarafRootFileResolver;
import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver;
import org.onap.ccsdk.sli.core.utils.common.CoreDefaultFileResolver;
import org.onap.ccsdk.sli.core.utils.common.SdncConfigEnvVarFileResolver;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Responsible for determining the properties file to use and instantiating the <code>DBResourceManager</code>
 * Service.  The priority for properties file resolution is as follows:
 *
 * <ol>
 *     <li>A directory identified by the system environment variable <code>SDNC_CONFIG_DIR</code></li>
 *     <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
 *     <li>A directory identified by the JRE argument <code>dblib.properties</code></li>
 *     <li>A <code>dblib.properties</code> file located in the karaf root directory</li>
 * </ol>
 * 
 * Encryption Support
 * <ol>
 *    <li>Uses ecryption provided by <code>AAAEncryptionService</code></li>
 *    <li>AAA Configuration file is <code>aaa-cert-config.xml</code></li>
 * </ol>
 */
public class DBLIBResourceProvider {

    private static final Logger LOG = LoggerFactory.getLogger(DBLIBResourceProvider.class);

    /**
     * The name of the properties file for database configuration
     */
    private static final String DBLIB_PROP_FILE_NAME = "dblib.properties";

    private static final String DBLIB_PROPERTY_NAME = "org.onap.ccsdk.sli.jdbc.password";

    /**
     * A prioritized list of strategies for resolving dblib properties files.
     */
    private Vector<PropertiesFileResolver> dblibPropertiesFileResolvers = new Vector<>();

    /**
     * The configuration properties for the db connection.
     */
    private Properties properties;

    /**
     * Set up the prioritized list of strategies for resolving dblib properties files.
     */
    public DBLIBResourceProvider() {
        dblibPropertiesFileResolvers.add(new SdncConfigEnvVarFileResolver(
                "Using property file (1) from environment variable"
        ));
        dblibPropertiesFileResolvers.add(new CoreDefaultFileResolver(
                "Using property file (2) from default directory"
        ));
        dblibPropertiesFileResolvers.add(new JREFileResolver(
                "Using property file (3) from JRE argument", DBLIBResourceProvider.class
        ));
        dblibPropertiesFileResolvers.add(new KarafRootFileResolver(
                "Using property file (4) from karaf root", this));

        // determines properties file as according to the priority described in the class header comment
        final File propertiesFile = determinePropertiesFile(this);
        if (propertiesFile != null) {
            try(FileInputStream fileInputStream = new FileInputStream(propertiesFile)) {
                properties = new Properties();
                properties.load(fileInputStream);

                if(properties.containsKey(DBLIB_PROPERTY_NAME)) {
                    String sensitive = properties.getProperty(DBLIB_PROPERTY_NAME);
                    if(sensitive != null && sensitive.startsWith("ENC:")) {
                        try {
                            sensitive = sensitive.substring(4);
                            String postsense = decrypt(sensitive);
                            properties.setProperty(DBLIB_PROPERTY_NAME, postsense);
                        } catch(Exception exc) {
                            LOG.error("Failed to translate property", exc);
                        }
                    }
                }

            } catch (final IOException e) {
                LOG.error("Failed to load properties for file: {}", propertiesFile.toString(),
                        new DblibConfigurationException("Failed to load properties for file: "
                                + propertiesFile.toString(), e));
            }
        }
    }

    /**
     *
     * @param value
     * @return decrypted string if successful or the original value if unsuccessful
     */
    private String decrypt(String value) {
        try {
            BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();

            ServiceReference sref = bctx.getServiceReference("org.opendaylight.aaa.encrypt.AAAEncryptionService");
            if(sref == null) {
                LOG.warn("Could not acquire service reference for 'org.opendaylight.aaa.encrypt.AAAEncryptionService'");
                return value;
            }
            Object encrSvc = bctx.getService(sref);
            if(encrSvc == null) {
                LOG.warn("Could not access service for 'org.opendaylight.aaa.encrypt.AAAEncryptionService'");
                return value;
            }

            Method gs2Method = encrSvc.getClass().getMethod("decrypt", new Class[] { "".getClass() });
            Object unmasked = gs2Method.invoke(encrSvc, new Object[] { value });
            return unmasked.toString();

        } catch (Exception exc) {
            LOG.error("Failure", exc);
            return value;
        }
    }

    /**
     * Extract db config properties.
     *
     * @return the db config properties
     */
    public Properties getProperties() {
        return properties;
    }

    /**
     * Reports the method chosen for properties resolution to the <code>Logger</code>.
     *
     * @param message Some user friendly message
     * @param fileOptional The file location of the chosen properties file
     * @return the file location of the chosen properties file
     */
    private static File reportSuccess(final String message, final Optional<File> fileOptional) {
        if(fileOptional.isPresent()) {
            final File file = fileOptional.get();
            LOG.info("{} {}", message, file.getPath());
            return file;
        }
        return null;
    }

    /**
     * Reports fatal errors.  This is the case in which no properties file could be found.
     *
     * @param message An appropriate fatal error message
     * @param dblibConfigurationException An exception describing what went wrong during resolution
     */
    private static void reportFailure(final String message,
                                      final DblibConfigurationException dblibConfigurationException) {

        LOG.error("{}", message, dblibConfigurationException);
    }

    /**
     * Determines the dblib properties file to use based on the following priority:
     * <ol>
     *     <li>A directory identified by the system environment variable <code>SDNC_CONFIG_DIR</code></li>
     *     <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
     *     <li>A directory identified by the JRE argument <code>dblib.properties</code></li>
     *     <li>A <code>dblib.properties</code> file located in the karaf root directory</li>
     * </ol>
     */
    File determinePropertiesFile(final DBLIBResourceProvider dblibResourceProvider) {

        for (final PropertiesFileResolver dblibPropertiesFileResolver : dblibPropertiesFileResolvers) {
            final Optional<File> fileOptional = dblibPropertiesFileResolver.resolveFile(DBLIB_PROP_FILE_NAME);
            if (fileOptional.isPresent()) {
                return reportSuccess(dblibPropertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional);
            }
        }

        reportFailure("Missing configuration properties resource(3)",
                new DblibConfigurationException("Missing configuration properties resource(3): "
                        + DBLIB_PROP_FILE_NAME));
        return null;
    }
}