Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | * Run openscap xccdf evaluation on given nodes |
| 4 | * |
| 5 | * Expected parametes: |
| 6 | * SALT_MASTER_URL Full Salt API address. |
| 7 | * SALT_MASTER_CREDENTIALS Credentials to the Salt API. |
| 8 | * |
| 9 | * XCCDF_BENCHMARKS_DIR The XCCDF benchmarks base directory (default /usr/share/xccdf-benchmarks/mirantis/) |
| 10 | * XCCDF_BENCHMARKS List of pairs XCCDF benchmark filename and corresponding profile separated with ',' |
| 11 | * these pairs are separated with semicolon. |
| 12 | * (e.g. manila/openstack_manila-xccdf.xml,profilename;horizon/openstack_horizon-xccdf.xml,profile) |
| 13 | * XCCDF_VERSION The XCCDF version (default 1.2) |
| 14 | * XCCDF_TAILORING_ID The tailoring id (default None) |
| 15 | * |
| 16 | * TARGET_SERVERS The target Salt nodes (default *) |
| 17 | * |
| 18 | * ARTIFACTORY_URL The artifactory URL |
| 19 | * ARTIFACTORY_NAMESPACE The artifactory namespace (default 'mirantis/openscap') |
| 20 | * ARTIFACTORY_REPO The artifactory repo (default 'binary-dev-local') |
| 21 | * |
| 22 | * UPLOAD_TO_DASHBOARD Boolean. Upload results to the WORP or not |
| 23 | * DASHBOARD_API_URL The WORP api base url. Mandatory if UPLOAD_TO_DASHBOARD is true |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | |
| 28 | /** |
| 29 | * Upload results to the `WORP` dashboard |
| 30 | * |
| 31 | * @param apiUrl The base dashboard api url |
| 32 | * @param cloudName The cloud name (mostly, the given node's domain name) |
| 33 | * @param nodeName The node name |
Pavlo Shchelokovskyy | f1b99b0 | 2018-10-02 20:31:17 +0300 | [diff] [blame^] | 34 | * @param reportType Type of the report to create/use, either 'openscap' or 'cve' |
| 35 | * @param reportId Report Id to re-use, if empty report will be created |
| 36 | * @param results The scanning results as a json file content (string) |
| 37 | * @return reportId The Id of the report created if incoming reportId was empty, otherwise incoming reportId |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 38 | */ |
Pavlo Shchelokovskyy | f1b99b0 | 2018-10-02 20:31:17 +0300 | [diff] [blame^] | 39 | def uploadResultToDashboard(apiUrl, cloudName, nodeName, reportType, reportId, results) { |
Ivan Udovichenko | d1bd28c | 2018-10-02 12:41:04 +0300 | [diff] [blame] | 40 | def common = new com.mirantis.mk.Common() |
| 41 | def http = new com.mirantis.mk.Http() |
| 42 | |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 43 | // Yes, we do not care of performance and will create at least 4 requests per each result |
| 44 | def requestData = [:] |
| 45 | |
| 46 | def cloudId |
| 47 | def nodeId |
| 48 | |
Pavlo Shchelokovskyy | f1b99b0 | 2018-10-02 20:31:17 +0300 | [diff] [blame^] | 49 | def worpApi = [:] |
| 50 | worpApi["url"] = apiUrl |
| 51 | |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 52 | // Let's take a look, may be our minion is already presented on the dashboard |
| 53 | // Get available environments |
Pavlo Shchelokovskyy | f1b99b0 | 2018-10-02 20:31:17 +0300 | [diff] [blame^] | 54 | common.infoMsg("Making GET to ${worpApi.url}/environment/") |
| 55 | environments = http.restGet(worpApi, "/environment/") |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 56 | for (environment in environments) { |
| 57 | if (environment['name'] == cloudName) { |
| 58 | cloudId = environment['uuid'] |
| 59 | break |
| 60 | } |
| 61 | } |
| 62 | // Cloud wasn't presented, let's create it |
| 63 | if (! cloudId ) { |
| 64 | // Create cloud |
Pavlo Shchelokovskyy | f1b99b0 | 2018-10-02 20:31:17 +0300 | [diff] [blame^] | 65 | requestData = [:] |
| 66 | requestData['name'] = cloudName |
| 67 | common.infoMsg("Making POST to ${worpApi.url}/environment/ with ${requestData}") |
| 68 | cloudId = http.restPost(worpApi, "/environment/", requestData)['env']['uuid'] |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 69 | |
| 70 | // And the node |
| 71 | // It was done here to reduce count of requests to the api. |
| 72 | // Because if there was not cloud presented on the dashboard, then the node was not presented as well. |
Pavlo Shchelokovskyy | f1b99b0 | 2018-10-02 20:31:17 +0300 | [diff] [blame^] | 73 | requestData = [:] |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 74 | requestData['nodes'] = [nodeName] |
Pavlo Shchelokovskyy | f1b99b0 | 2018-10-02 20:31:17 +0300 | [diff] [blame^] | 75 | common.infoMsg("Making PUT to ${worpApi.url}/environment/${cloudId}/nodes/ with ${requestData}") |
| 76 | nodeId = http.restCall(worpApi, "/environment/${cloudId}/nodes/", "PUT", requestData)['uuid'] |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | if (! nodeId ) { |
| 80 | // Get available nodes in our environment |
Pavlo Shchelokovskyy | f1b99b0 | 2018-10-02 20:31:17 +0300 | [diff] [blame^] | 81 | common.infoMsg("Making GET to ${worpApi.url}/environment/${cloudId}/nodes/") |
| 82 | nodes = http.restGet(worpApi, "/environment/${cloudId}/nodes/") |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 83 | for (node in nodes) { |
| 84 | if (node['name'] == nodeName) { |
Pavlo Shchelokovskyy | f1b99b0 | 2018-10-02 20:31:17 +0300 | [diff] [blame^] | 85 | nodeId = node['uuid'] |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 86 | break |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Node wasn't presented, let's create it |
| 92 | if (! nodeId ) { |
| 93 | // Create node |
Pavlo Shchelokovskyy | f1b99b0 | 2018-10-02 20:31:17 +0300 | [diff] [blame^] | 94 | requestData = [:] |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 95 | requestData['nodes'] = [nodeName] |
Pavlo Shchelokovskyy | f1b99b0 | 2018-10-02 20:31:17 +0300 | [diff] [blame^] | 96 | common.infoMsg("Making PUT to ${worpApi.url}/environment/${cloudId}/nodes/ with ${requestData}") |
| 97 | nodeId = http.restCall(worpApi, "/environment/${cloudId}/nodes/", "PUT", requestData)['uuid'] |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Pavlo Shchelokovskyy | f1b99b0 | 2018-10-02 20:31:17 +0300 | [diff] [blame^] | 100 | // Create report if needed |
| 101 | if (! reportId ) { |
| 102 | requestData = [:] |
| 103 | requestData['env_uuid'] = cloudId |
| 104 | common.infoMsg("Making POST to ${worpApi.url}/reports/${reportType}/ with ${requestData}") |
| 105 | reportId = http.restPost(worpApi, "/reports/${reportType}/", requestData)['report']['uuid'] |
| 106 | } |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 107 | |
| 108 | // Upload results |
Pavlo Shchelokovskyy | f1b99b0 | 2018-10-02 20:31:17 +0300 | [diff] [blame^] | 109 | // NOTE(pas-ha) results should already be a dict with 'results' key |
| 110 | requestData = common.parseJSON(results) |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 111 | requestData['node_name'] = nodeName |
Pavlo Shchelokovskyy | f1b99b0 | 2018-10-02 20:31:17 +0300 | [diff] [blame^] | 112 | common.infoMsg("First result in results to PUT is ${requestData['results'][0]}") |
| 113 | // NOTE(pas-ha) not logging whole results to be sent, is too large and just spams the logs |
| 114 | common.infoMsg("Making PUT to ${worpApi.url}/reports/${reportType}/${reportId}/ with node name ${requestData['node_name']} and results") |
| 115 | http.restCall(worpApi, "/reports/${reportType}/${reportId}/", "PUT", requestData) |
| 116 | return reportId |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | |
| 120 | node('python') { |
| 121 | def pepperEnv = 'pepperEnv' |
| 122 | |
| 123 | // XCCDF related variables |
| 124 | def benchmarksAndProfilesArray = XCCDF_BENCHMARKS.tokenize(';') |
| 125 | def benchmarksDir = XCCDF_BENCHMARKS_DIR ?: '/usr/share/xccdf-benchmarks/mirantis/' |
| 126 | def xccdfVersion = XCCDF_VERSION ?: '1.2' |
| 127 | def xccdfTailoringId = XCCDF_TAILORING_ID ?: 'None' |
| 128 | def targetServers = TARGET_SERVERS ?: '*' |
| 129 | |
| 130 | def salt = new com.mirantis.mk.Salt() |
| 131 | def python = new com.mirantis.mk.Python() |
| 132 | def common = new com.mirantis.mk.Common() |
| 133 | def http = new com.mirantis.mk.Http() |
| 134 | |
| 135 | // To have an ability to work in heavy concurrency conditions |
| 136 | def scanUUID = UUID.randomUUID().toString() |
| 137 | |
| 138 | def artifactsArchiveName = "openscap-${scanUUID}.zip" |
Ivan Udovichenko | d1bd28c | 2018-10-02 12:41:04 +0300 | [diff] [blame] | 139 | def resultsBaseDir = "/var/log/openscap/${scanUUID}" |
| 140 | def artifactsDir = "openscap" |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 141 | |
| 142 | def liveMinions |
| 143 | |
Ivan Udovichenko | d1bd28c | 2018-10-02 12:41:04 +0300 | [diff] [blame] | 144 | |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 145 | stage ('Setup virtualenv for Pepper') { |
| 146 | python.setupPepperVirtualenv(pepperEnv, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
| 147 | } |
| 148 | |
| 149 | stage ('Run openscap xccdf evaluation and attempt to upload the results to a dashboard') { |
| 150 | liveMinions = salt.getMinions(pepperEnv, targetServers) |
| 151 | |
| 152 | if (liveMinions.isEmpty()) { |
| 153 | throw new Exception('There are no alive minions') |
| 154 | } |
| 155 | |
| 156 | common.infoMsg("Scan UUID: ${scanUUID}") |
| 157 | |
Ivan Udovichenko | d1bd28c | 2018-10-02 12:41:04 +0300 | [diff] [blame] | 158 | // Clean all results before proceeding with results from every minion |
| 159 | dir(artifactsDir) { |
| 160 | deleteDir() |
| 161 | } |
| 162 | |
Pavlo Shchelokovskyy | f1b99b0 | 2018-10-02 20:31:17 +0300 | [diff] [blame^] | 163 | def reportId |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 164 | for (minion in liveMinions) { |
| 165 | |
| 166 | // Iterate oscap evaluation over the benchmarks |
| 167 | for (benchmark in benchmarksAndProfilesArray) { |
| 168 | def (benchmarkFilePath, profile) = benchmark.tokenize(',').collect({it.trim()}) |
| 169 | |
| 170 | // Remove extension from the benchmark name |
| 171 | def benchmarkPathWithoutExtension = benchmarkFilePath.replaceFirst('[.][^.]+$', '') |
Ivan Udovichenko | d1bd28c | 2018-10-02 12:41:04 +0300 | [diff] [blame] | 172 | |
| 173 | // Get benchmark name |
| 174 | def benchmarkName = benchmarkPathWithoutExtension.tokenize('/')[-1] |
| 175 | |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 176 | // And build resultsDir based on this path |
| 177 | def resultsDir = "${resultsBaseDir}/${benchmarkPathWithoutExtension}" |
| 178 | |
| 179 | def benchmarkFile = "${benchmarksDir}${benchmarkFilePath}" |
| 180 | |
Ivan Udovichenko | d1bd28c | 2018-10-02 12:41:04 +0300 | [diff] [blame] | 181 | def nodeShortName = minion.tokenize('.')[0] |
| 182 | |
| 183 | def archiveName = "${scanUUID}_${nodeShortName}_${benchmarkName}.tar" |
| 184 | |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 185 | // Evaluate the benchmark |
| 186 | salt.runSaltProcessStep(pepperEnv, minion, 'oscap.eval', [ |
| 187 | 'xccdf', benchmarkFile, "results_dir=${resultsDir}", |
| 188 | "profile=${profile}", "xccdf_version=${xccdfVersion}", |
| 189 | "tailoring_id=${xccdfTailoringId}" |
| 190 | ]) |
| 191 | |
Ivan Udovichenko | d1bd28c | 2018-10-02 12:41:04 +0300 | [diff] [blame] | 192 | salt.cmdRun(pepperEnv, minion, "tar -cf /tmp/${archiveName} -C ${resultsBaseDir} .") |
| 193 | fileContents = salt.cmdRun(pepperEnv, minion, "cat /tmp/${archiveName}", true, null, false)['return'][0].values()[0].replaceAll('Salt command execution success', '') |
| 194 | |
| 195 | sh "mkdir -p ${artifactsDir}/${scanUUID}/${nodeShortName}" |
| 196 | writeFile file: "${archiveName}", text: fileContents |
| 197 | sh "tar --strip-components 1 -xf ${archiveName} --directory ${artifactsDir}/${scanUUID}/${nodeShortName}; rm -f ${archiveName}" |
| 198 | |
| 199 | // Remove archive which is not needed anymore |
| 200 | salt.runSaltProcessStep(pepperEnv, minion, 'file.remove', "/tmp/${archiveName}") |
| 201 | |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 202 | // Attempt to upload the scanning results to the dashboard |
| 203 | if (UPLOAD_TO_DASHBOARD.toBoolean()) { |
| 204 | if (common.validInputParam('DASHBOARD_API_URL')) { |
| 205 | def cloudName = salt.getGrain(pepperEnv, minion, 'domain')['return'][0].values()[0].values()[0] |
Pavlo Shchelokovskyy | f1b99b0 | 2018-10-02 20:31:17 +0300 | [diff] [blame^] | 206 | reportId = uploadResultToDashboard(DASHBOARD_API_URL, cloudName, minion, "openscap", reportId, salt.getFileContent(pepperEnv, minion, "${resultsDir}/results.json")) |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 207 | } else { |
| 208 | throw new Exception('Uploading to the dashboard is enabled but the DASHBOARD_API_URL was not set') |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
Ivan Udovichenko | d1bd28c | 2018-10-02 12:41:04 +0300 | [diff] [blame] | 213 | |
| 214 | // Prepare archive |
| 215 | sh "tar -cJf ${artifactsDir}.tar.xz ${artifactsDir}" |
| 216 | |
| 217 | // Archive the build output artifacts |
| 218 | archiveArtifacts artifacts: "*.xz" |
Vasyl Saienko | 4e8ec64 | 2018-09-17 10:08:08 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /* // Will be implemented later |
| 222 | stage ('Attempt to upload results to an artifactory') { |
| 223 | if (common.validInputParam('ARTIFACTORY_URL')) { |
| 224 | for (minion in liveMinions) { |
| 225 | def destDir = "${artifactsDir}/${minion}" |
| 226 | def archiveName = "openscap-${scanUUID}.tar.gz" |
| 227 | def tempArchive = "/tmp/${archiveName}" |
| 228 | def destination = "${destDir}/${archiveName}" |
| 229 | |
| 230 | dir(destDir) { |
| 231 | // Archive scanning results on the remote target |
| 232 | salt.runSaltProcessStep(pepperEnv, minion, 'archive.tar', ['czf', tempArchive, resultsBaseDir]) |
| 233 | |
| 234 | // Get it content and save it |
| 235 | writeFile file: destination, text: salt.getFileContent(pepperEnv, minion, tempArchive) |
| 236 | |
| 237 | // Remove scanning results and the temp archive on the remote target |
| 238 | salt.runSaltProcessStep(pepperEnv, minion, 'file.remove', resultsBaseDir) |
| 239 | salt.runSaltProcessStep(pepperEnv, minion, 'file.remove', tempArchive) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | def artifactory = new com.mirantis.mcp.MCPArtifactory() |
| 244 | def artifactoryName = 'mcp-ci' |
| 245 | def artifactoryRepo = ARTIFACTORY_REPO ?: 'binary-dev-local' |
| 246 | def artifactoryNamespace = ARTIFACTORY_NAMESPACE ?: 'mirantis/openscap' |
| 247 | def artifactoryServer = Artifactory.server(artifactoryName) |
| 248 | def publishInfo = true |
| 249 | def buildInfo = Artifactory.newBuildInfo() |
| 250 | def zipName = "${env.WORKSPACE}/openscap/${scanUUID}/results.zip" |
| 251 | |
| 252 | // Zip scan results |
| 253 | zip zipFile: zipName, archive: false, dir: artifactsDir |
| 254 | |
| 255 | // Mandatory and additional properties |
| 256 | def properties = artifactory.getBinaryBuildProperties([ |
| 257 | "scanUuid=${scanUUID}", |
| 258 | "project=openscap" |
| 259 | ]) |
| 260 | |
| 261 | // Build Artifactory spec object |
| 262 | def uploadSpec = """{ |
| 263 | "files": |
| 264 | [ |
| 265 | { |
| 266 | "pattern": "${zipName}", |
| 267 | "target": "${artifactoryRepo}/${artifactoryNamespace}/openscap", |
| 268 | "props": "${properties}" |
| 269 | } |
| 270 | ] |
| 271 | }""" |
| 272 | |
| 273 | // Upload artifacts to the given Artifactory |
| 274 | artifactory.uploadBinariesToArtifactory(artifactoryServer, buildInfo, uploadSpec, publishInfo) |
| 275 | |
| 276 | } else { |
| 277 | common.warningMsg('ARTIFACTORY_URL was not given, skip uploading to artifactory') |
| 278 | } |
| 279 | } |
| 280 | */ |
| 281 | |
| 282 | } |