Skip to content

Commit

Permalink
Fixes for API Key
Browse files Browse the repository at this point in the history
  • Loading branch information
rowleya committed Jul 4, 2023
1 parent ec790ef commit e79c24f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package uk.ac.manchester.cs.spinnaker;

import static uk.ac.manchester.cs.spinnaker.rest.utils.RestClientUtils.createApiKeyClient;
import static uk.ac.manchester.cs.spinnaker.rest.utils.RestClientUtils.createClient;

import java.net.URL;

Expand Down Expand Up @@ -44,13 +44,12 @@ private TestRestClient() {
*/
public static void main(final String[] args) throws Exception {
var nmpiUrl = new URL("https://nmpi.hbpneuromorphic.eu/");
var nmpiUsername = "uman";
var apiKey = "QWvPf6WzISelx7MhIJqzoi-BgZqj95PPJYnpBuLTKcGN5b8sbP9"
+ "fiUR2UQ6I--PHuoeOIeF0tmKptKC5rbIMRiRlGGG51zDvRDzqoIVTm4LU6L"
+ "fV8MXYRlzXi4Dc75w-";
var queue = createApiKeyClient(nmpiUrl, nmpiUsername, apiKey,
NMPIQueue.class, NMPIQueue.createProvider());
var response = queue.getNextJob("SpiNNaker");
var queue = createClient(nmpiUrl, NMPIQueue.class,
NMPIQueue.createProvider());
var response = queue.getNextJob(apiKey, "SpiNNaker");
if (response instanceof QueueEmpty) {
System.err.println("No items in queue");
} else if (response instanceof Job) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
import static org.joda.time.DateTimeZone.UTC;
import static org.slf4j.LoggerFactory.getLogger;
import static uk.ac.manchester.cs.spinnaker.ThreadUtils.sleep;
import static uk.ac.manchester.cs.spinnaker.rest.utils.RestClientUtils.createApiKeyClient;
import static uk.ac.manchester.cs.spinnaker.rest.utils.RestClientUtils.createBasicClient;
import static uk.ac.manchester.cs.spinnaker.rest.utils.RestClientUtils.createClient;

import java.io.IOException;
import java.io.PrintWriter;
Expand Down Expand Up @@ -98,32 +97,27 @@ public class NMPIQueueManager {
/** The URL from which to load the data. */
@Value("${nmpi.url}")
private URL nmpiUrl;
/** The username to log in to the server with. */
@Value("${nmpi.username}")
private String nmpiUsername;
/** The password or API key to log in to the server with. */
@Value("${nmpi.password}")
private String nmpiPassword;
/**
* True if the password is an API key, False if the password should be used
* to obtain the key.
*/
@Value("${nmpi.passwordIsApiKey}")
private boolean nmpiPasswordIsApiKey;
/** The API key to authenticate against the server. */
@Value("${nmpi.apiKey}")
private String nmpiApiKey;

/**
* Initialise the client.
*/
@PostConstruct
private void initAPIClient() {
var apiKey = nmpiPassword;
if (!nmpiPasswordIsApiKey) {
queue = createBasicClient(nmpiUrl, nmpiUsername, nmpiPassword,
NMPIQueue.class);
apiKey = queue.getToken(nmpiUsername).getKey();
}
queue = createApiKeyClient(nmpiUrl, nmpiUsername, apiKey,
NMPIQueue.class, NMPIQueue.createProvider());
queue = createClient(nmpiUrl, NMPIQueue.class,
NMPIQueue.createProvider());
}

/**
* Gets a job from the server.
*
* @param id The ID of the job.
* @return The job
*/
private Job getJobFromQueue(final int id) {
return queue.getJob(nmpiApiKey, id);
}

/**
Expand All @@ -136,7 +130,7 @@ private void initAPIClient() {
*/
private Job getJob(final int id) {
synchronized (jobCache) {
return jobCache.computeIfAbsent(id, queue::getJob);
return jobCache.computeIfAbsent(id, this::getJobFromQueue);
}
}

Expand All @@ -154,7 +148,7 @@ public void processResponsesFromQueue() {
while (!done) {
try {
// logger.debug("Getting next job");
processResponse(queue.getNextJob(hardware));
processResponse(queue.getNextJob(nmpiApiKey, hardware));
} catch (final Exception e) {
logger.error("Error in getting next job", e);
sleep(EMPTY_QUEUE_SLEEP_MS);
Expand Down Expand Up @@ -197,7 +191,7 @@ private void processResponse(final Job job) {
job.setTimestampCompletion(null);
job.setStatus(STATUS_QUEUED);
logger.debug("Updating job status on server");
queue.updateJob(job.getId(), job);
queue.updateJob(nmpiApiKey, job.getId(), job);
} catch (final IOException e) {
logger.error("Error in executing job", e);
setJobError(job.getId(), null, null, e, null);
Expand All @@ -216,7 +210,7 @@ public void appendJobLog(final int id, final String logToAppend) {
var existingLog = jobLog.computeIfAbsent(id, ignored -> new NMPILog());
existingLog.appendContent(logToAppend);
logger.debug("Job {} log is being updated", id);
queue.updateLog(id, existingLog);
queue.updateLog(nmpiApiKey, id, existingLog);
}

/**
Expand All @@ -230,7 +224,7 @@ public void setJobRunning(final int id) {
final var job = getJob(id);
job.setStatus(STATUS_RUNNING);
logger.debug("Updating job status on server");
queue.updateJob(id, job);
queue.updateJob(nmpiApiKey, id, job);
}

/**
Expand Down Expand Up @@ -261,7 +255,7 @@ public void setJobFinished(final int id, final String logToAppend,
job.setProvenance(provenance);

logger.debug("Updating job status on server");
queue.updateJob(id, job);
queue.updateJob(nmpiApiKey, id, job);

jobLog.remove(id);
jobCache.remove(id);
Expand Down Expand Up @@ -306,7 +300,7 @@ public void setJobError(final int id, final String logToAppend,
job.setProvenance(provenance);

logger.debug("Updating job on server");
queue.updateJob(id, job);
queue.updateJob(nmpiApiKey, id, job);

jobLog.remove(id);
jobCache.remove(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;

import uk.ac.manchester.cs.spinnaker.job.nmpi.Job;
import uk.ac.manchester.cs.spinnaker.job.nmpi.QueueEmpty;
import uk.ac.manchester.cs.spinnaker.job.nmpi.QueueNextResponse;
import uk.ac.manchester.cs.spinnaker.model.APIKeyResponse;
import uk.ac.manchester.cs.spinnaker.model.NMPILog;
import uk.ac.manchester.cs.spinnaker.rest.utils.CustomJacksonJsonProvider;
import uk.ac.manchester.cs.spinnaker.rest.utils.PropertyBasedDeserialiser;
Expand All @@ -37,29 +36,21 @@
* The REST API for the HBP Neuromorphic Platform Interface queue.
*/
public interface NMPIQueue {
/**
* Get the API token.
*
* @param username
* The username.
* @return The token.
*/
@GET
@Path("token/auth")
@Produces("application/json")
APIKeyResponse getToken(@QueryParam("username") String username);

/**
* Get the next queue item for a specific hardware system.
*
* @param api-key
* The API key to use.
* @param hardware
* The hardware ID.
* @return The queue item.
*/
@GET
@Path("queue/submitted/next/{hardware}/")
@Produces("application/json")
QueueNextResponse getNextJob(@PathParam("hardware") String hardware);
QueueNextResponse getNextJob(@HeaderParam("x-api-key") String apiKey,
@PathParam("hardware") String hardware);

/**
* Update the status of a queue item.
Expand All @@ -72,7 +63,8 @@ public interface NMPIQueue {
@PUT
@Path("queue/{id}")
@Consumes("application/json")
void updateJob(@PathParam("id") int id, Job job);
void updateJob(@HeaderParam("x-api-key") String apiKey,
@PathParam("id") int id, Job job);

/**
* Get the queue status.
Expand All @@ -84,7 +76,8 @@ public interface NMPIQueue {
@GET
@Path("queue/{id}")
@Produces("application/json")
Job getJob(@PathParam("id") int id);
Job getJob(@HeaderParam("x-api-key") String apiKey,
@PathParam("id") int id);

/**
* Update the log.
Expand All @@ -97,7 +90,8 @@ public interface NMPIQueue {
@PUT
@Path("log/{id}")
@Consumes("application/json")
void updateLog(@PathParam("id") int id, NMPILog log);
void updateLog(@HeaderParam("x-api-key") String apiKey,
@PathParam("id") int id, NMPILog log);

/**
* Create a JSON provider capable of handling the messages on the NMPI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,22 @@ public static <T> T createBearerClient(final URL url, final String token,
final Class<T> clazz, final Object... providers) {
return createClient(url, "Bearer " + token, clazz, providers);
}

/**
* Create a new REST client without explicit authentication.
*
* @param <T>
* The type of interface to proxy
* @param url
* The URL of the REST service
* @param clazz
* The interface to proxy
* @param providers
* The objects to register with the underlying client
* @return
*/
public static <T> T createClient(final URL url, final Class<T> clazz,
final Object... providers) {
return createClient(url, null, clazz, providers);
}
}

0 comments on commit e79c24f

Please sign in to comment.