Skip to content

Commit

Permalink
fix(influxdb): Add API token auth for InfluxDB.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Belldina committed Sep 9, 2024
1 parent 7eb6d0b commit fe0ad4f
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.google.common.collect.ImmutableList;
import com.netflix.kayenta.atlas.config.KayentaSerializationConfigurationProperties;
import com.netflix.kayenta.canary.CanaryMetricSetQueryConfig;
import com.netflix.kayenta.metrics.MapBackedMetricsServiceRepository;
import com.netflix.kayenta.metrics.MetricSetMixerService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.netflix.kayenta.atlas.config;
package com.netflix.kayenta.config;

import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package com.netflix.kayenta.retrofit.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.kayenta.atlas.config.KayentaSerializationConfigurationProperties;
import com.netflix.kayenta.config.KayentaConfiguration;
import com.netflix.kayenta.config.KayentaSerializationConfigurationProperties;
import com.netflix.spinnaker.config.OkHttpClientConfiguration;
import com.netflix.spinnaker.orca.retrofit.exceptions.SpinnakerServerExceptionHandler;
import com.squareup.okhttp.ConnectionPool;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ JacksonConverter jacksonConverterWithMapper(ObjectMapper objectMapper) {
public <T> T createClient(
Class<T> type, Converter converter, RemoteService remoteService, OkHttpClient okHttpClient) {
try {
return createClient(type, converter, remoteService, okHttpClient, null, null, null, null);
return createClient(
type, converter, remoteService, okHttpClient, null, null, null, null, null);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -84,6 +85,7 @@ public <T> T createClient(
username,
password,
usernamePasswordFile,
null,
null);
} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -98,7 +100,33 @@ public <T> T createClient(
String username,
String password,
String usernamePasswordFile,
String bearerToken)
String bearerToken) {
try {
return createClient(
type,
converter,
remoteService,
okHttpClient,
username,
password,
usernamePasswordFile,
bearerToken,
null);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public <T> T createClient(
Class<T> type,
Converter converter,
RemoteService remoteService,
OkHttpClient okHttpClient,
String username,
String password,
String usernamePasswordFile,
String bearerToken,
String apiToken)
throws IOException {
String baseUrl = remoteService.getBaseUrl();

Expand All @@ -109,9 +137,11 @@ public <T> T createClient(
if (!(StringUtils.isEmpty(username)
&& StringUtils.isEmpty(password)
&& StringUtils.isEmpty(usernamePasswordFile)
&& StringUtils.isEmpty(bearerToken))) {
&& StringUtils.isEmpty(bearerToken)
&& StringUtils.isEmpty(apiToken))) {
okHttpClient =
createAuthenticatedClient(username, password, usernamePasswordFile, bearerToken);
createAuthenticatedClient(
username, password, usernamePasswordFile, bearerToken, apiToken);
}

Slf4jRetrofitLogger logger = createRetrofitLogger.apply(type);
Expand All @@ -128,7 +158,11 @@ public <T> T createClient(
}

private static OkHttpClient createAuthenticatedClient(
String username, String password, String usernamePasswordFile, String bearerToken)
String username,
String password,
String usernamePasswordFile,
String bearerToken,
String apiToken)
throws IOException {
final String credential;

Expand All @@ -139,6 +173,8 @@ private static OkHttpClient createAuthenticatedClient(
credential = "Basic " + Base64.encodeBase64String(trimmedFileContent.getBytes());
} else if (StringUtils.isNotEmpty(bearerToken)) {
credential = "Bearer " + bearerToken;
} else if (StringUtils.isNotEmpty(apiToken)) {
credential = "Token " + apiToken;
} else {
credential = Credentials.basic(username, password);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.netflix.kayenta.metrics

import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.kayenta.atlas.config.KayentaSerializationConfigurationProperties
import com.netflix.kayenta.config.KayentaSerializationConfigurationProperties
import com.netflix.kayenta.canary.CanaryScope
import com.netflix.kayenta.config.KayentaConfiguration
import spock.lang.Shared
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package com.netflix.kayenta.util

import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.kayenta.atlas.config.KayentaSerializationConfigurationProperties
import com.netflix.kayenta.config.KayentaSerializationConfigurationProperties
import com.netflix.kayenta.config.KayentaConfiguration
import spock.lang.Specification
import spock.lang.Unroll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ MetricsService influxDbMetricsService(
String name = account.getName();
List<AccountCredentials.Type> supportedTypes = account.getSupportedTypes();

InfluxdbCredentials credentials = InfluxdbCredentials.builder().build();
InfluxdbCredentials credentials =
InfluxdbCredentials.builder().apiToken(account.getApiKey()).build();

InfluxDbNamedAccountCredentials.InfluxDbNamedAccountCredentialsBuilder
accountCredentialsBuilder =
Expand All @@ -86,7 +87,12 @@ MetricsService influxDbMetricsService(
InfluxDbRemoteService.class,
influxDbResponseConverter,
account.getEndpoint(),
okHttpClient));
okHttpClient,
null,
null,
null,
null,
credentials.getApiToken()));
}
accountCredentialsBuilder.supportedTypes(supportedTypes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ public class InfluxdbCredentials {
.orElse("Unknown");

private String dbName;
private String apiToken;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.netflix.kayenta.security.AccountCredentials;
import com.netflix.kayenta.security.AccountCredentialsRepository;
import com.squareup.okhttp.OkHttpClient;
import java.io.IOException;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.actuate.health.HealthIndicator;
Expand Down Expand Up @@ -104,7 +103,7 @@ MetricsService prometheusMetricsService(

accountCredentialsRepository.save(name, prometheusManagedAccount);
prometheusMetricsServiceBuilder.accountName(name);
} catch (IOException e) {
} catch (RuntimeException e) {
log.error("Problem registering Prometheus account {}:", name, e);
}
}
Expand Down

0 comments on commit fe0ad4f

Please sign in to comment.