Skip to content

Commit

Permalink
fixed problem when body is not null when unauthorized
Browse files Browse the repository at this point in the history
  • Loading branch information
matjaz99 committed Feb 19, 2023
1 parent d4cd4e1 commit a237909
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
[![Docker Pulls](https://img.shields.io/docker/pulls/matjaz99/alertmonitor.svg)](https://hub.docker.com/r/matjaz99/alertmonitor)
[![GitHub issues](https://img.shields.io/github/issues/matjaz99/alertmonitor.svg)](https://GitHub.com/matjaz99/alertmonitor/issues/)

Alertmonitor is a webapp for displaying active alerts in Prometheus.
Alertmonitor is a webapp for displaying alerts in tabular and colorful way. Plain and simple.
View alerts from one or more Prometheus servers, centrally in one place.
Other datasources will be supported eventually.



Alertmonitor receives alerts from Alertmanager on the HTTP endpoint: `/alertmonitor/webhook`.
Alternatively, if webhook receiver is not configured, Alertmonitor can pull alerts directly from Prometheus.
Expand Down Expand Up @@ -254,6 +258,7 @@ A list of supported environment variables:
| ALERTMONITOR_MONGODB_CONNECTION_STRING | The connection string for MongoDB (username, password and host). |



### Security

#### Secure HTTPS protocol
Expand All @@ -266,7 +271,7 @@ In case of `https`, no certificate validation is checked.

Http client in Alertmonitor supports basic authentication when connecting to the data provider (only for https clients).

Currently, username and password can only be provided via `SERVER` variable using syntax `https://username:password@hostname:port`.
Currently, username and password can only be provided via `ALERTMONITOR_PROMETHEUS_SERVER` variable using syntax `https://username:password@hostname:port`.


### Environment variable substitution
Expand Down
9 changes: 2 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<!-- <primefaces.version>11.0.0</primefaces.version>-->
<primefaces.version>12.0.0</primefaces.version>
<!-- <prometheus.version>0.6.0</prometheus.version>-->
<gson.version>2.8.9</gson.version>
<prometheus.version>0.12.0</prometheus.version>
<mongodb-driver.version>3.11.3</mongodb-driver.version>
</properties>
Expand Down Expand Up @@ -61,7 +62,7 @@
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
<version>${gson.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient -->
<dependency>
Expand All @@ -86,12 +87,6 @@
<artifactId>okhttp</artifactId>
<version>4.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-stdlib -->
<!-- <dependency>-->
<!-- <groupId>org.jetbrains.kotlin</groupId>-->
<!-- <artifactId>kotlin-stdlib</artifactId>-->
<!-- <version>1.3.50</version>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/com.squareup.okio/okio -->
<dependency>
<groupId>com.squareup.okio</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ public class PrometheusHttpClient {
private AbstractDataProvider dataProvider;

/* Extracted from providerConfig */
/** Name of this client - provider name */
/** Name of this client = provider name */
private String name = ".default";
/** connection string in syntax: https://username:password@hostname:port */
private String server;
private String basicAuthUsername;
private String basicAuthPassword;
Expand Down Expand Up @@ -274,15 +275,15 @@ private String execute(Request request) throws PrometheusHttpClientException {

OkHttpClient httpClient = HttpClientFactory.instantiateHttpClient(secureClient, connectTimeout, readTimeout, basicAuthUsername, basicAuthPassword);

logger.info("PrometheusHttpClient[" + name + "]: request[" + reqID + "] " + request.method().toUpperCase() + " " + request.url().toString());
logger.info("PrometheusHttpClient[" + name + "]: request[" + reqID + "] " + request.method().toUpperCase() + " " + request.url());
Response response = httpClient.newCall(request).execute();
logger.info("PrometheusHttpClient[" + name + "]: request[" + reqID + "] code=" + response.code() + ", success=" + response.isSuccessful());

code = Integer.toString(response.code());

if (response.body() != null) {
if (response.body() != null && response.code()/100 == 2) {
responseBody = response.body().string();
logger.debug("PrometheusHttpClient[" + name + "]: request[" + reqID + "] body: " + responseBody);
logger.trace("PrometheusHttpClient[" + name + "]: request[" + reqID + "] body: " + responseBody);
}

if (response.code() >= 300 && response.code() < 400) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public List<String> getActiveTargets() {
* @param msg
* @param severity
*/
public void addWarning(String msgKey, String msg, String severity) {
public synchronized void addWarning(String msgKey, String msg, String severity) {
warnings.remove("success");
warnings.put(msgKey, new DWarning(severity, msg));
}
Expand All @@ -384,14 +384,14 @@ public void addWarning(String msgKey, String msg, String severity) {
* If this is the last warning to be removed from the list, then create a 'success' warning.
* @param msgKey
*/
public void removeWarning(String msgKey) {
public synchronized void removeWarning(String msgKey) {
warnings.remove(msgKey);
if (warnings.size() == 0) {
warnings.put("success", new DWarning(DWarning.DWARNING_SEVERITY_CLEAR, "Working OK"));
}
}

public List<DWarning> getWarnings() {
public synchronized List<DWarning> getWarnings() {
logger.info("----> getWarnings: provider=" + providerConfig.getName() + ", size=" + warnings.size() + ", " + warnings.toString());
return new ArrayList<>(warnings.values());
}
Expand Down

0 comments on commit a237909

Please sign in to comment.