From 6456badbbfc77253ef693da88e83dabe6ba1d515 Mon Sep 17 00:00:00 2001 From: Jorge Aguilera Date: Sat, 2 Mar 2024 12:06:07 +0100 Subject: [PATCH] secure nomad api calls using the provided apiToken Signed-off-by: Jorge Aguilera --- .../nomad/executor/NomadService.groovy | 4 ++ .../nomad/executor/NomadServiceSpec.groovy | 39 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/plugins/nf-nomad/src/main/nextflow/nomad/executor/NomadService.groovy b/plugins/nf-nomad/src/main/nextflow/nomad/executor/NomadService.groovy index e210645..edd527a 100644 --- a/plugins/nf-nomad/src/main/nextflow/nomad/executor/NomadService.groovy +++ b/plugins/nf-nomad/src/main/nextflow/nomad/executor/NomadService.groovy @@ -21,6 +21,7 @@ import groovy.transform.CompileStatic import groovy.util.logging.Slf4j import io.nomadproject.client.ApiClient import io.nomadproject.client.api.JobsApi +import io.nomadproject.client.auth.ApiKeyAuth import io.nomadproject.client.models.Job import io.nomadproject.client.models.JobRegisterRequest import io.nomadproject.client.models.JobRegisterResponse @@ -48,6 +49,9 @@ class NomadService implements Closeable{ this.config = config ApiClient apiClient = new ApiClient() apiClient.basePath = config.clientOpts.address + if( config.clientOpts.token ){ + apiClient.apiKey = config.clientOpts.token + } this.jobsApi = new JobsApi(apiClient); } diff --git a/plugins/nf-nomad/src/test/nextflow/nomad/executor/NomadServiceSpec.groovy b/plugins/nf-nomad/src/test/nextflow/nomad/executor/NomadServiceSpec.groovy index 9fdbc73..3cba2c9 100644 --- a/plugins/nf-nomad/src/test/nextflow/nomad/executor/NomadServiceSpec.groovy +++ b/plugins/nf-nomad/src/test/nextflow/nomad/executor/NomadServiceSpec.groovy @@ -187,4 +187,43 @@ class NomadServiceSpec extends Specification{ state == "Starting" } + + void "should send the token"(){ + given: + def config = new NomadConfig( + client:[ + address : "http://${mockWebServer.hostName}:${mockWebServer.port}", + token: "1234" + ], + jobs:[ + dockerVolume:'test' + ] + ) + def service = new NomadService(config) + + String id = "theId" + String name = "theName" + String image = "theImage" + List args = ["theCommand", "theArgs"] + String workingDir = "a/b/c" + Mapenv = [test:"test"] + + mockWebServer.enqueue(new MockResponse() + .setBody(JsonOutput.toJson(["EvalID":"test"]).toString()) + .addHeader("Content-Type", "application/json")); + when: + + def idJob = service.submitTask(id, name, image, args, workingDir,env) + def recordedRequest = mockWebServer.takeRequest(); + def body = new JsonSlurper().parseText(recordedRequest.body.readUtf8()) + + then: + idJob + + and: + recordedRequest.method == "POST" + recordedRequest.path == "/v1/jobs" + recordedRequest.headers.values('X-Nomad-Token').first()=='1234' + } + }