Skip to content

Commit 6e42775

Browse files
authored
Merge pull request #413 from codacy-acme/master
add flag to skip ssl verification, default is false
2 parents d6d59e7 + 33a2c60 commit 6e42775

7 files changed

Lines changed: 36 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ Found [Clone] 7 duplicated lines with 10 tokens:
204204
* `--commit-uuid` [default: latest commit of current git branch] - Set the commit UUID that will receive the results on Codacy
205205
* `--skip-commit-uuid-validation` [default: false] - Force using a commit UUID even if it doesn't belong to the current git branch.
206206
* `--upload` [default: false] - Request to push results to Codacy
207+
* `--skip-ssl-verification` [default: false] - Skip the SSL certificate verification when communicating with the Codacy API
207208
* `--parallel` [default: 2] - Number of tools to run in parallel
208209
* `--max-allowed-issues` [default: 0] - Maximum number of issues allowed for the analysis to succeed
209210
* `--fail-if-incomplete` [default: false] - Fail the analysis if any tool fails to run

cli/src/main/scala/com/codacy/analysis/cli/command/AnalyseCommand.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ object AnalyseCommand {
4949
.stripSuffix("/") // a trailing slash breaks the tools fetching mechanism
5050

5151
val codacyClientOpt: Option[CodacyClient] =
52-
Credentials.get(environment, analyze.api, apiUrl).map(CodacyClient.apply)
52+
Credentials
53+
.get(environment, analyze.api, apiUrl)
54+
.map(cred => CodacyClient.apply(cred, analyze.skipSslVerificationValue))
5355
val configuration: CLIConfiguration =
5456
CLIConfiguration(codacyClientOpt, environment, analyze, new CodacyConfigurationFileLoader)
5557
val formatter: Formatter =

cli/src/main/scala/com/codacy/analysis/cli/command/CLIApp.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ final case class Analyze(
146146
skipCommitUuidValidation: Int @@ Counter = Tag.of(0),
147147
@ExtraName("u") @ValueDescription("If the results should be uploaded to the API")
148148
upload: Int @@ Counter = Tag.of(0),
149+
@ExtraName("i") @ValueDescription("[default: false] - Skips ssl verification when comunicating with Codacy API")
150+
skipSslVerification: Int @@ Counter = Tag.of(0),
149151
@ExtraName("p") @ValueDescription("The number of tools to run in parallel")
150152
parallel: Option[Int] = Option.empty,
151153
@ValueDescription("Allow tools to access the network")
@@ -175,6 +177,7 @@ final case class Analyze(
175177
val forceFilePermissionsValue: Boolean = forceFilePermissions.## > 0
176178
val ghCodeScanningCompatValue: Boolean = ghCodeScanningCompat.## > 0
177179
val skipCommitUuidValidationValue: Boolean = skipCommitUuidValidation.## > 0
180+
val skipSslVerificationValue: Boolean = skipSslVerification.## > 0
178181

179182
}
180183

cli/src/test/scala/com/codacy/analysis/cli/CLISpec.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ class CLISpec extends Specification with NoLanguageFeatures with FileMatchers {
5555
"b10790d724e5fd2ca98e8ba3711b6cb10d7f5e38")) must beLike {
5656
case Right((DefaultCommand(_), _, Some(parsed))) => parsed must beRight
5757
}
58+
cli.parse(
59+
Array(
60+
"analyze",
61+
"--directory",
62+
"/tmp",
63+
"--tool",
64+
"pylint",
65+
"--commit-uuid",
66+
"b10790d724e5fd2ca98e8ba3711b6cb10d7f5e38",
67+
"--upload",
68+
"--skip-ssl-verification")) must beLike {
69+
case Right((DefaultCommand(_), _, Some(parsed))) => parsed must beRight
70+
}
5871
}
5972

6073
"fail parse" in {

cli/src/test/scala/com/codacy/analysis/cli/configuration/CLIConfigurationSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class CLIConfigurationSpec extends Specification with NoLanguageFeatures {
5151
commitUuid = commitUuid,
5252
extras = ExtraOptions(analyser = Analyser.defaultAnalyser.name))
5353
private val defaultEnvironment = new Environment(Map.empty)
54-
private val httpHelper = new HttpHelper(remoteUrl, Map.empty)
54+
private val httpHelper = new HttpHelper(remoteUrl, Map.empty, false)
5555

5656
private val noRemoteConfigCodacyClient =
5757
new CodacyClient(apiCredentials, httpHelper)(ExecutionContext.global) {

core/src/main/scala/com/codacy/analysis/core/clients/CodacyClient.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,20 +196,20 @@ class CodacyClient(credentials: Credentials, http: HttpHelper)(implicit context:
196196

197197
object CodacyClient {
198198

199-
def apply(credentials: Credentials)(implicit context: ExecutionContext): CodacyClient = {
199+
def apply(credentials: Credentials, allowUnsafeSSL: Boolean)(implicit context: ExecutionContext): CodacyClient = {
200200
credentials match {
201201
case ProjectToken(token, baseUrl) =>
202202
val headers: Map[String, String] = Map(
203203
("project-token", token),
204204
// This is deprecated and is kept for backward compatibility. It will removed in the context of CY-1272
205205
("project_token", token))
206-
new CodacyClient(credentials, new HttpHelper(baseUrl, headers))
206+
new CodacyClient(credentials, new HttpHelper(baseUrl, headers, allowUnsafeSSL))
207207
case APIToken(token, baseUrl, _, _, _) =>
208208
val headers: Map[String, String] = Map(
209209
("api-token", token),
210210
// This is deprecated and is kept for backward compatibility. It will removed in the context of CY-1272
211211
("api_token", token))
212-
new CodacyClient(credentials, new HttpHelper(baseUrl, headers))
212+
new CodacyClient(credentials, new HttpHelper(baseUrl, headers, allowUnsafeSSL))
213213
}
214214
}
215215

core/src/main/scala/com/codacy/analysis/core/utils/HttpHelper.scala

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,29 @@ package com.codacy.analysis.core.utils
22

33
import io.circe.parser.parse
44
import io.circe.{Json, ParsingFailure}
5-
import scalaj.http.{Http, HttpRequest, HttpResponse}
5+
import scalaj.http.{Http, HttpRequest, HttpResponse, HttpOptions}
66

7-
class HttpHelper(apiUrl: String, extraHeaders: Map[String, String]) {
7+
class HttpHelper(apiUrl: String, extraHeaders: Map[String, String], allowUnsafeSSL: Boolean) {
88

99
private lazy val connectionTimeoutMs = 2000
1010
private lazy val readTimeoutMs = 5000
1111

1212
private val remoteUrl = apiUrl + "/2.0"
1313

14+
private def httpOptions = if (allowUnsafeSSL) Seq(HttpOptions.allowUnsafeSSL) else Seq.empty
15+
1416
def get(endpoint: String): Either[ParsingFailure, Json] = {
1517
val headers: Map[String, String] = Map("Content-Type" -> "application/json") ++ extraHeaders
1618

1719
val response: HttpResponse[String] =
18-
Http(s"$remoteUrl$endpoint").headers(headers).timeout(connectionTimeoutMs, readTimeoutMs).asString
20+
Http(s"$remoteUrl$endpoint")
21+
.headers(headers)
22+
.timeout(connectionTimeoutMs, readTimeoutMs)
23+
.options(httpOptions)
24+
.asString
1925

2026
parse(response.body)
27+
2128
}
2229

2330
def post(endpoint: String, dataOpt: Option[Json] = None): Either[ParsingFailure, Json] = {
@@ -26,7 +33,8 @@ class HttpHelper(apiUrl: String, extraHeaders: Map[String, String]) {
2633
} ++ extraHeaders
2734

2835
val request: HttpRequest = dataOpt.map { data =>
29-
Http(s"$remoteUrl$endpoint").postData(data.toString)
36+
Http(s"$remoteUrl$endpoint").options(httpOptions).postData(data.toString)
37+
3038
}.getOrElse(Http(s"$remoteUrl$endpoint"))
3139
.method("POST")
3240
.headers(headers)

0 commit comments

Comments
 (0)