Datasets:

Modalities:
Text
Formats:
json
Languages:
code
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
File size: 6,589 Bytes
eb67da4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
 * Copyright 2021 ThoughtWorks, Inc.
 *
 * 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.
 */

package com.thoughtworks.go.addon.businesscontinuity.primary.controller;

import com.google.gson.Gson;
import com.thoughtworks.go.addon.businesscontinuity.ConfigFileType;
import com.thoughtworks.go.addon.businesscontinuity.DatabaseStatusProvider;
import com.thoughtworks.go.addon.businesscontinuity.FileDetails;
import com.thoughtworks.go.addon.businesscontinuity.PluginsList;
import com.thoughtworks.go.addon.businesscontinuity.primary.ServerStatusResponse;
import com.thoughtworks.go.addon.businesscontinuity.primary.service.GoFilesStatusProvider;
import com.thoughtworks.go.util.SystemEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.apache.commons.io.IOUtils.copy;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isEmpty;

@Controller
@SuppressWarnings("WeakerAccess")
// BUG: CWE-200 Exposure of Sensitive Information to an Unauthorized Actor
// @RequestMapping(value = "/add-on/business-continuity/api")
// FIXED:
public class PrimaryStatusProviderController {

    private GoFilesStatusProvider goFilesStatusProvider;
    private DatabaseStatusProvider databaseStatusProvider;
    private final SystemEnvironment systemEnvironment;
    private PluginsList pluginsList;

    @Autowired
    public PrimaryStatusProviderController(GoFilesStatusProvider goFilesStatusProvider, DatabaseStatusProvider databaseStatusProvider, PluginsList pluginsList, SystemEnvironment systemEnvironment) {
        this.goFilesStatusProvider = goFilesStatusProvider;
        this.databaseStatusProvider = databaseStatusProvider;
        this.pluginsList = pluginsList;
        this.systemEnvironment = systemEnvironment;
    }

    @RequestMapping(value = "/health-check", method = RequestMethod.GET)
    @ResponseBody
    public String healthCheck() {
        return "OK!";
    }

    @RequestMapping(value = "/latest_database_wal_location", method = RequestMethod.GET)
    @ResponseBody
    public String latestDatabaseWalLocation() {
        return databaseStatusProvider.latestWalLocation();
    }

    @RequestMapping(value = "/config_files_status", method = RequestMethod.GET)
    public void latestStatus(HttpServletResponse response) throws IOException {
        Map<ConfigFileType, String> latestFileStatusMap = goFilesStatusProvider.getLatestStatusMap();

        Map<ConfigFileType, FileDetails> fileDetailsMap = new HashMap<>();
        for (ConfigFileType configFileType : latestFileStatusMap.keySet()) {
            if (!isEmpty(latestFileStatusMap.get(configFileType))) {
                fileDetailsMap.put(configFileType, new FileDetails(latestFileStatusMap.get(configFileType)));
            }
        }

        ServerStatusResponse serverStatusResponse = new ServerStatusResponse(goFilesStatusProvider.updateInterval(), goFilesStatusProvider.getLastUpdateTime(), fileDetailsMap);
        String responseBody = new Gson().toJson(serverStatusResponse);
        response.setContentType("application/json");
        response.getOutputStream().print(responseBody);
    }

    @RequestMapping(value = "/cruise_config", method = RequestMethod.GET)
    public void getLatestCruiseConfigXML(HttpServletResponse response) {
        serveFile(ConfigFileType.CRUISE_CONFIG_XML.load(systemEnvironment), response, "text/xml");
    }

    @RequestMapping(value = "/user_feature_toggle", method = RequestMethod.GET)
    public void geUserFeatureToggleFile(HttpServletResponse response) {
        serveFile(ConfigFileType.USER_FEATURE_TOGGLE.load(systemEnvironment), response, "text/json");
    }

    @RequestMapping(value = "/cipher.aes", method = RequestMethod.GET)
    public void getLatestAESCipher(HttpServletResponse response) {
        serveFile(ConfigFileType.AES_CIPHER.load(systemEnvironment), response, "text/plain");
    }

    @RequestMapping(value = "/jetty_config", method = RequestMethod.GET)
    public void getLatestJettyXML(HttpServletResponse response) {
        serveFile(ConfigFileType.JETTY_XML.load(systemEnvironment), response, "text/xml");
    }

    @RequestMapping(value = "/plugin_files_status", method = RequestMethod.GET)
    public void latest(HttpServletResponse response) throws IOException {
        String pluginsJSON = pluginsList.getPluginsJSON();
        response.setContentType("application/json");
        response.getOutputStream().print(pluginsJSON);
    }

    @RequestMapping(value = "/plugin", method = RequestMethod.GET)
    public void getPluginFile(
            @RequestParam("folderName") String folderName,
            @RequestParam("pluginName") String pluginName,
            HttpServletResponse response) {
        String pluginFolderPath = isBlank(folderName) || folderName.equalsIgnoreCase("bundled") ? systemEnvironment.getBundledPluginAbsolutePath() : systemEnvironment.getExternalPluginAbsolutePath();
        File pluginFile = new File(pluginFolderPath, pluginName);
        serveFile(pluginFile, response, "application/octet-stream");
    }

    private void serveFile(File file, HttpServletResponse response, String contentType) {
        try (FileInputStream inputStream = new FileInputStream(file)) {
            response.setStatus(HttpServletResponse.SC_OK);
            response.setCharacterEncoding("UTF-8");
            response.setContentType(contentType);
            copy(inputStream, response.getOutputStream());
            response.flushBuffer();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}