Skip to content

Commit

Permalink
secure nomad api calls using the provided apiToken
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Aguilera <[email protected]>
  • Loading branch information
jagedn committed Mar 2, 2024
1 parent 21831ac commit 6456bad
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> args = ["theCommand", "theArgs"]
String workingDir = "a/b/c"
Map<String, String>env = [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'
}

}

0 comments on commit 6456bad

Please sign in to comment.