23e254b1e81afccebc545365faf8927f285e326c
[nonrtric/plt/ranpm.git] / datafilecollector / src / main / java / org / onap / dcaegen2 / collectors / datafile / ftp / SftpClientSettings.java
1 /*-
2  * ============LICENSE_START======================================================================
3  * Copyright (C) 2020 Nokia. All rights reserved.
4  * ===============================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
6  * except in compliance with the License. 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 distributed under the
11  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12  * either express or implied. See the License for the specific language governing permissions
13  * and limitations under the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.ftp;
18
19 import java.io.File;
20
21 import org.onap.dcaegen2.collectors.datafile.configuration.SftpConfig;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class SftpClientSettings {
26
27     private static final Logger logger = LoggerFactory.getLogger(SftpClientSettings.class);
28
29     private final SftpConfig sftpConfig;
30
31     public SftpClientSettings(SftpConfig sftpConfig) {
32         this.sftpConfig = sftpConfig;
33     }
34
35     public boolean shouldUseStrictHostChecking() {
36         boolean strictHostKeyChecking = false;
37         if (this.sftpConfig.strictHostKeyChecking) {
38             File file = new File(getKnownHostsFilePath());
39             strictHostKeyChecking = file.isFile();
40             logUsageOfStrictHostCheckingFlag(strictHostKeyChecking, file.getAbsolutePath());
41         } else {
42             logger.info("StrictHostKeyChecking will be disabled.");
43         }
44         return strictHostKeyChecking;
45     }
46
47     public String getKnownHostsFilePath() {
48         return this.sftpConfig.knownHostsFilePath;
49     }
50
51     private void logUsageOfStrictHostCheckingFlag(boolean strictHostKeyChecking, String filePath) {
52         if (strictHostKeyChecking) {
53             logger.info("StrictHostKeyChecking will be enabled with KNOWN_HOSTS_FILE_PATH [{}].", filePath);
54         } else {
55             logger.warn(
56                 "StrictHostKeyChecking is enabled but environment variable KNOWN_HOSTS_FILE_PATH is not set or points to not existing file [{}]  -->  falling back to StrictHostKeyChecking='no'.",
57                 filePath);
58         }
59     }
60 }