Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow insecure protocol #164

Merged
merged 10 commits into from
Dec 13, 2021
6 changes: 6 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ node {
// Or set to null if you want to add the repository on your own.
distBaseUrl = "https://nodejs.org/dist"

// Specifies whether it is acceptable to communicate with the Node.js repository over an insecure HTTP connection.
// Only used if download is true
// Change it to true if you use a mirror that uses HTTP rather than HTTPS
// Or set to null if you want to use Gradle's default behaviour.
allowInsecureProtocol = null

// The npm command executed by the npmInstall task
// By default it is install but it can be changed to ci
npmInstallCommand = "install"
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/com/github/gradle/node/NodeExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ open class NodeExtension(project: Project) {
*/
val distBaseUrl = project.objects.property<String>()

/**
* Specifies whether it is acceptable to communicate with the Node.js repository over an insecure HTTP connection.
* Only used if download is true
* Change it to true if you use a mirror that uses HTTP rather than HTTPS
* Or set to null if you want to use Gradle's default behaviour.
*/
val allowInsecureProtocol = project.objects.property<Boolean>()

val npmCommand = project.objects.property<String>().convention("npm")
val npxCommand = project.objects.property<String>().convention("npx")
val yarnCommand = project.objects.property<String>().convention("yarn")
Expand Down
8 changes: 6 additions & 2 deletions src/main/kotlin/com/github/gradle/node/NodePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.gradle.api.Project
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.register
import org.gradle.util.GradleVersion
import java.io.File

class NodePlugin : Plugin<Project> {
Expand All @@ -30,7 +31,7 @@ class NodePlugin : Plugin<Project> {
addYarnRule()
project.afterEvaluate {
if (nodeExtension.download.get()) {
nodeExtension.distBaseUrl.orNull?.let { addRepository(it) }
nodeExtension.distBaseUrl.orNull?.let { addRepository(it, nodeExtension.allowInsecureProtocol.orNull) }
configureNodeSetupTask(nodeExtension)
}
}
Expand Down Expand Up @@ -86,7 +87,7 @@ class NodePlugin : Plugin<Project> {
}
}

private fun addRepository(distUrl: String) {
private fun addRepository(distUrl: String, allowInsecureProtocol: Boolean?) {
project.repositories.ivy {
name = "Node.js"
setUrl(distUrl)
Expand All @@ -99,6 +100,9 @@ class NodePlugin : Plugin<Project> {
content {
includeModule("org.nodejs", "node")
}
if (GradleVersion.current() >= GradleVersion.version("6.0")) {
allowInsecureProtocol?.let { isAllowInsecureProtocol = it }
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class RunWithMultipleGradleVersionsExtension extends AbstractAnnotationDrivenExt
private static final GradleVersion MINIMUM_SUPPORTED_GRADLE_VERSION = GradleVersion.version("5.6.4")
private static final GradleVersion MINIMUM_GRADLE_6_VERSION = GradleVersion.version("6.0")
private static final GradleVersion CURRENT_GRADLE_VERSION = GradleVersion.current()
private static final GradleVersion GRADLE_7_VERSION = GradleVersion.version("7.0-rc-2")
private static final GradleVersion GRADLE_7_VERSION = GradleVersion.version("7.0")
private static final GradleVersion[] GRADLE_VERSIONS =
[MINIMUM_SUPPORTED_GRADLE_VERSION, MINIMUM_GRADLE_6_VERSION, CURRENT_GRADLE_VERSION, GRADLE_7_VERSION]
private GradleVersion gradleVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class NodeExtensionTest extends AbstractProjectTest {
nodeExtension.npmCommand.get() == 'npm'
nodeExtension.npxCommand.get() == 'npx'
nodeExtension.distBaseUrl.get() == 'https://nodejs.org/dist'
nodeExtension.allowInsecureProtocol.orNull == null
nodeExtension.workDir.get() != null
nodeExtension.nodeProjectDir.get() != null
nodeExtension.version.get() == DEFAULT_NODE_VERSION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,31 @@ class NodeTask_integTest extends AbstractIntegTest {
result.task(":nodeSetup").outcome == TaskOutcome.SKIPPED
result.task(":hello").outcome == TaskOutcome.SUCCESS
}

def 'make sure build works with allowInsecureProtocol false using a custom repository'() {
given:
Assume.assumeFalse(gradleVersion < GradleVersion.version("6.8"))
copyResources("fixtures/node-disallow-insecure-protocol")

when:
def result = build("hello")

then:
result.task(":nodeSetup").outcome == TaskOutcome.SUCCESS
result.task(":hello").outcome == TaskOutcome.SUCCESS
}

def 'make sure build works with allowInsecureProtocol true using a custom repository'() {
given:
Assume.assumeFalse(gradleVersion < GradleVersion.version("6.8"))
copyResources("fixtures/node-allow-insecure-protocol")

when:
def result = build("hello")

then:
result.task(":nodeSetup").outcome == TaskOutcome.SUCCESS
result.task(":hello").outcome == TaskOutcome.SUCCESS
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins {
id "com.github.node-gradle.node"
}

node {
version = "12.13.0"
distBaseUrl = "http://nodejs.org/dist/"
download = true
allowInsecureProtocol = true
workDir = file("build/node")
}

task hello(type: NodeTask) {
script = file("simple.js")
args = []
outputs.upToDateWhen {
true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Hello World");
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins {
id "com.github.node-gradle.node"
}

node {
version = "12.13.0"
distBaseUrl = "https://nodejs.org/dist/"
download = true
allowInsecureProtocol = false
workDir = file("build/node")
}

task hello(type: NodeTask) {
script = file("simple.js")
args = []
outputs.upToDateWhen {
true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Hello World");