aboutsummaryrefslogtreecommitdiffstats
path: root/aai-traversal/src/main/java/org/onap/aai/config/PropertyPasswordConfiguration.java
blob: 0d2ff88a192e6121c06842620d9d3b4d192ff660 (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
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.aai.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import org.apache.commons.io.IOUtils;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.*;

public class PropertyPasswordConfiguration implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    private static final Pattern decodePasswordPattern = Pattern.compile("password\\((.*?)\\)");
    private PasswordDecoder passwordDecoder = new JettyPasswordDecoder();
    private static final EELFLogger logger = EELFManager.getInstance().getLogger(PropertyPasswordConfiguration.class.getName());

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        String certPath = environment.getProperty("server.certs.location");
        File passwordFile = null;
        File passphrasesFile = null;
        InputStream passwordStream = null;
        InputStream passphrasesStream = null;
        Map<String, Object> sslProps = new LinkedHashMap<>();

        // Override the passwords from application.properties if we find AAF certman files
        if (certPath != null) {
            try {
                passwordFile = new File(certPath + ".password");
                passwordStream = new FileInputStream(passwordFile);

                if (passwordStream != null) {
                    String keystorePassword = null;

                    keystorePassword = IOUtils.toString(passwordStream);
                    if (keystorePassword != null) {
                        keystorePassword = keystorePassword.trim();
                    }
                    sslProps.put("server.ssl.key-store-password", keystorePassword);
                    sslProps.put("schema.service.ssl.key-store-password", keystorePassword);
                } else {
                    logger.info("Not using AAF Certman password file");
                }
            } catch (IOException e) {
                logger.warn("Not using AAF Certman password file, e=" + e.getMessage());
            } finally {
                if (passwordStream != null) {
                    try {
                        passwordStream.close();
                    } catch (Exception e) {
                    }
                }
            }
            try {
                passphrasesFile = new File(certPath + ".passphrases");
                passphrasesStream = new FileInputStream(passphrasesFile);

                if (passphrasesStream != null) {
                    String truststorePassword = null;
                    Properties passphrasesProps = new Properties();
                    passphrasesProps.load(passphrasesStream);
                    truststorePassword = passphrasesProps.getProperty("cadi_truststore_password");
                    if (truststorePassword != null) {
                        truststorePassword = truststorePassword.trim();
                    }
                    sslProps.put("server.ssl.trust-store-password", truststorePassword);
                    sslProps.put("schema.service.ssl.trust-store-password", truststorePassword);
                } else {
                    logger.info("Not using AAF Certman passphrases file");
                }
            } catch (IOException e) {
                logger.warn("Not using AAF Certman passphrases file, e=" + e.getMessage());
            } finally {
                if (passphrasesStream != null) {
                    try {
                        passphrasesStream.close();
                    } catch (Exception e) {
                    }
                }
            }
        }
        for (PropertySource<?> propertySource : environment.getPropertySources()) {
            Map<String, Object> propertyOverrides = new LinkedHashMap<>();
            decodePasswords(propertySource, propertyOverrides);
            if (!propertyOverrides.isEmpty()) {
                PropertySource<?> decodedProperties = new MapPropertySource("decoded "+ propertySource.getName(), propertyOverrides);
                environment.getPropertySources().addBefore(propertySource.getName(), decodedProperties);
            }

        }
        if (!sslProps.isEmpty()) {
            logger.info("Using AAF Certman files");
            PropertySource<?> additionalProperties = new MapPropertySource("additionalProperties", sslProps);
            environment.getPropertySources().addFirst(additionalProperties);
        }
    }

    private void decodePasswords(PropertySource<?> source, Map<String, Object> propertyOverrides) {
        if (source instanceof EnumerablePropertySource) {
            EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) source;
            for (String key : enumerablePropertySource.getPropertyNames()) {
                Object rawValue = source.getProperty(key);
                if (rawValue instanceof String) {
                    String decodedValue = decodePasswordsInString((String) rawValue);
                    propertyOverrides.put(key, decodedValue);
                }
            }
        }
    }

    private String decodePasswordsInString(String input) {
        if (input == null) return null;
        StringBuffer output = new StringBuffer();
        Matcher matcher = decodePasswordPattern.matcher(input);
        while (matcher.find()) {
            String replacement = passwordDecoder.decode(matcher.group(1));
            matcher.appendReplacement(output, replacement);
        }
        matcher.appendTail(output);
        return output.toString();
    }

}