auto create apikey

This commit is contained in:
Jonathan Shook 2020-11-22 02:55:51 -06:00
parent cfa92eabcc
commit 3d2ff55f1c
5 changed files with 122 additions and 59 deletions

View File

@ -25,13 +25,24 @@ public class GrafanaClientConfig {
private int timeoutms; private int timeoutms;
private final List<Authenticator> authenticators = new ArrayList<>(); private final List<Authenticator> authenticators = new ArrayList<>();
// private LinkedHashMap<String,String> headers = new LinkedHashMap<>();
private final List<Supplier<Map<String, String>>> headerSources = new ArrayList<>(); private final List<Supplier<Map<String, String>>> headerSources = new ArrayList<>();
public GrafanaClientConfig() { public GrafanaClientConfig() {
} }
public void basicAuth(String username, String pw) { private GrafanaClientConfig(URI baseUrl, int timeoutms, List<Authenticator> authenticators,
List<Supplier<Map<String, String>>> headerSources) {
this.baseUrl = baseUrl;
this.timeoutms = timeoutms;
this.authenticators.addAll(authenticators);
this.headerSources.addAll(headerSources);
}
public GrafanaClientConfig copy() {
return new GrafanaClientConfig(baseUrl, timeoutms, authenticators, headerSources);
}
public GrafanaClientConfig basicAuth(String username, String pw) {
Objects.requireNonNull(username); Objects.requireNonNull(username);
String authPw = pw != null ? pw : ""; String authPw = pw != null ? pw : "";
@ -44,9 +55,9 @@ public class GrafanaClientConfig {
addAuthenticator(basicAuth); addAuthenticator(basicAuth);
addHeader("Authorization", encodeBasicAuth(username, authPw)); addHeader("Authorization", encodeBasicAuth(username, authPw));
return this;
} }
public GrafanaClientConfig addAuthenticator(Authenticator authenticator) { public GrafanaClientConfig addAuthenticator(Authenticator authenticator) {
authenticators.add(authenticator); authenticators.add(authenticator);
return this; return this;
@ -87,7 +98,9 @@ public class GrafanaClientConfig {
public HttpClient newClient() { public HttpClient newClient() {
HttpClient.Builder cb = HttpClient.newBuilder(); HttpClient.Builder cb = HttpClient.newBuilder();
if (timeoutms > 0) {
cb.connectTimeout(Duration.ofMillis(timeoutms)); cb.connectTimeout(Duration.ofMillis(timeoutms));
}
for (Authenticator authenticator : authenticators) { for (Authenticator authenticator : authenticators) {
cb.authenticator(authenticator); cb.authenticator(authenticator);
} }
@ -106,8 +119,11 @@ public class GrafanaClientConfig {
public HttpRequest.Builder newRequest(String path) { public HttpRequest.Builder newRequest(String path) {
URI requestUri = makeUri(path); URI requestUri = makeUri(path);
HttpRequest.Builder rqb = HttpRequest.newBuilder(requestUri); HttpRequest.Builder rqb = HttpRequest.newBuilder(requestUri);
if (timeoutms > 0) {
rqb.timeout(Duration.ofMillis(timeoutms)); rqb.timeout(Duration.ofMillis(timeoutms));
}
getHeaders().forEach(rqb::setHeader); getHeaders().forEach(rqb::setHeader);
return rqb; return rqb;
} }

View File

@ -1,4 +1,4 @@
package io.nosqlbench.engine.core.metrics; package io.nosqlbench.engine.clients.grafana;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -14,11 +14,14 @@ public class GrafanaKeyFileReader implements Supplier<String> {
private final Path keyfilePath; private final Path keyfilePath;
public GrafanaKeyFileReader(Path path) {
this.keyfilePath = path;
}
public GrafanaKeyFileReader(String sourcePath) { public GrafanaKeyFileReader(String sourcePath) {
this.keyfilePath = Path.of(sourcePath); this.keyfilePath = Path.of(sourcePath);
} }
@Override @Override
public String get() { public String get() {
if (!Files.exists(keyfilePath)) { if (!Files.exists(keyfilePath)) {
@ -27,6 +30,7 @@ public class GrafanaKeyFileReader implements Supplier<String> {
} else { } else {
try { try {
String apikey = Files.readString(keyfilePath, StandardCharsets.UTF_8); String apikey = Files.readString(keyfilePath, StandardCharsets.UTF_8);
apikey = apikey.trim();
return apikey; return apikey;
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);

View File

@ -1,15 +1,18 @@
package io.nosqlbench.engine.core.metrics; package io.nosqlbench.engine.clients.grafana;
import io.nosqlbench.engine.clients.grafana.GrafanaClient;
import io.nosqlbench.engine.clients.grafana.GrafanaClientConfig;
import io.nosqlbench.engine.clients.grafana.transfer.GrafanaAnnotation; import io.nosqlbench.engine.clients.grafana.transfer.GrafanaAnnotation;
import io.nosqlbench.nb.annotations.Service; import io.nosqlbench.nb.annotations.Service;
import io.nosqlbench.nb.api.Environment;
import io.nosqlbench.nb.api.OnError;
import io.nosqlbench.nb.api.SystemId;
import io.nosqlbench.nb.api.annotations.Annotation; import io.nosqlbench.nb.api.annotations.Annotation;
import io.nosqlbench.nb.api.annotations.Annotator; import io.nosqlbench.nb.api.annotations.Annotator;
import io.nosqlbench.nb.api.config.*; import io.nosqlbench.nb.api.config.*;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
@ -38,16 +41,16 @@ public class GrafanaMetricsAnnotator implements Annotator, ConfigAware {
ga.setTimeEnd(annotation.getEnd()); ga.setTimeEnd(annotation.getEnd());
annotation.getLabels().forEach((k, v) -> { annotation.getLabels().forEach((k, v) -> {
ga.getTags().put(k, v); ga.getTags().add(k + ":" + v);
}); });
ga.getTags().put("layer", annotation.getLayer().toString()); ga.getTags().add("layer:" + annotation.getLayer().toString());
Map<String, String> labels = annotation.getLabels(); Map<String, String> labels = annotation.getLabels();
Optional.ofNullable(labels.get("alertId" )) Optional.ofNullable(labels.get("alertId" ))
.map(Integer::parseInt).ifPresent(ga::setAlertId); .map(Integer::parseInt).ifPresent(ga::setAlertId);
ga.setData(annotation.toString()); ga.setText(annotation.toString());
annotation.getSession(); annotation.getSession();
@ -111,20 +114,6 @@ public class GrafanaMetricsAnnotator implements Annotator, ConfigAware {
this.tags = ParamsParser.parse(cfg.param("tags", String.class), false); this.tags = ParamsParser.parse(cfg.param("tags", String.class), false);
} }
if (cfg.containsKey("apikeyfile")) {
String apikeyfile = cfg.paramEnv("apikeyfile", String.class);
AuthWrapper authHeaderSupplier = new AuthWrapper(
"Authorization",
new GrafanaKeyFileReader(apikeyfile),
s -> "Bearer " + s + ";"
);
gc.addHeaderSource(authHeaderSupplier);
}
if (cfg.containsKey("apikey")) {
gc.addHeaderSource(() -> Map.of("Authorization", "Bearer " + cfg.param("apikey", String.class)));
}
if (cfg.containsKey("username" )) { if (cfg.containsKey("username" )) {
if (cfg.containsKey("password" )) { if (cfg.containsKey("password" )) {
gc.basicAuth( gc.basicAuth(
@ -136,9 +125,44 @@ public class GrafanaMetricsAnnotator implements Annotator, ConfigAware {
} }
} }
Path keyfilePath = null;
if (cfg.containsKey("apikeyfile" )) {
String apikeyfile = cfg.paramEnv("apikeyfile", String.class);
keyfilePath = Path.of(apikeyfile);
} else if (cfg.containsKey("apikey" )) {
gc.addHeaderSource(() -> Map.of("Authorization", "Bearer " + cfg.param("apikey", String.class)));
} else {
Optional<String> apikeyLocation = Environment.INSTANCE.interpolate("$NBSTATEDIR/grafana_apikey" );
keyfilePath = apikeyLocation.map(Path::of).orElseThrow();
}
if (!Files.exists(keyfilePath)) {
logger.info("Auto-configuring grafana apikey." );
GrafanaClientConfig apiClientConf = gc.copy().basicAuth("admin", "admin" );
GrafanaClient apiClient = new GrafanaClient(apiClientConf);
try {
String nodeId = SystemId.getNodeId();
String keyName = "nosqlbench-" + nodeId + "-" + System.currentTimeMillis();
ApiToken apiToken = apiClient.createApiToken(keyName, "Admin", Long.MAX_VALUE);
Files.writeString(keyfilePath, apiToken.getKey());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
AuthWrapper authHeaderSupplier = new AuthWrapper(
"Authorization",
new GrafanaKeyFileReader(keyfilePath),
s -> "Bearer " + s
);
gc.addHeaderSource(authHeaderSupplier);
this.onError = OnError.valueOfName(cfg.get("onerror" ).toString()); this.onError = OnError.valueOfName(cfg.get("onerror" ).toString());
this.client = new GrafanaClient(gc); this.client = new GrafanaClient(gc);
} }
@Override @Override
@ -146,7 +170,7 @@ public class GrafanaMetricsAnnotator implements Annotator, ConfigAware {
return new MutableConfigModel(this) return new MutableConfigModel(this)
.required("baseurl", String.class, .required("baseurl", String.class,
"The base url of the grafana node, like http://localhost:3000/" ) "The base url of the grafana node, like http://localhost:3000/" )
.defaultto("apikeyfile", "$NBSTATEDIR/grafana_key", .defaultto("apikeyfile", "$NBSTATEDIR/grafana_apikey",
"The file that contains the api key, supersedes apikey" ) "The file that contains the api key, supersedes apikey" )
.optional("apikey", String.class, .optional("apikey", String.class,
"The api key to use, supersedes basic username and password" ) "The api key to use, supersedes basic username and password" )

View File

@ -17,7 +17,8 @@ public class GrafanaAnnotation {
private String text; private String text;
private String metric; private String metric;
private String type; private String type;
private Map<String, String> tags = new LinkedHashMap<>(); private final List<String> tags = new ArrayList<>();
// private Map<String, String> tags = new LinkedHashMap<>();
private Object data; private Object data;
public Integer getId() { public Integer getId() {
@ -124,12 +125,22 @@ public class GrafanaAnnotation {
this.type = type; this.type = type;
} }
public Map<String, String> getTags() { public List<String> getTags() {
return tags; return tags;
} }
public void addTag(String tag) {
this.tags.add(tag);
}
public void setTags(List<String> tags) {
tags.forEach(this::addTag);
}
public void setTags(Map<String, String> tags) { public void setTags(Map<String, String> tags) {
this.tags = tags; tags.forEach((k, v) -> {
this.addTag(k + ":" + v);
});
} }
public Object getData() { public Object getData() {

View File

@ -37,4 +37,12 @@ public class ConfigReader extends LinkedHashMap<String, Object> {
T typeCastedValue = type.cast(o); T typeCastedValue = type.cast(o);
return typeCastedValue; return typeCastedValue;
} }
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(this.configModel.getOf().getSimpleName()).append(":" );
sb.append(this.configModel.toString());
return sb.toString();
}
} }