Skip to content

Commit 6b387f3

Browse files
authored
feat(prometheus): add bearer token support (#823)
* feat(prometheus): add bearer token support * fix(retrofit): revert log level
1 parent 410bde5 commit 6b387f3

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-6
lines changed

kayenta-core/src/main/java/com/netflix/kayenta/retrofit/config/RetrofitClientFactory.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ JacksonConverter jacksonConverterWithMapper(ObjectMapper objectMapper) {
6060
public <T> T createClient(
6161
Class<T> type, Converter converter, RemoteService remoteService, OkHttpClient okHttpClient) {
6262
try {
63-
return createClient(type, converter, remoteService, okHttpClient, null, null, null);
63+
return createClient(type, converter, remoteService, okHttpClient, null, null, null, null);
6464
} catch (IOException e) {
6565
throw new RuntimeException(e);
6666
}
@@ -73,7 +73,31 @@ public <T> T createClient(
7373
OkHttpClient okHttpClient,
7474
String username,
7575
String password,
76-
String usernamePasswordFile)
76+
String usernamePasswordFile) {
77+
try {
78+
return createClient(
79+
type,
80+
converter,
81+
remoteService,
82+
okHttpClient,
83+
username,
84+
password,
85+
usernamePasswordFile,
86+
null);
87+
} catch (IOException e) {
88+
throw new RuntimeException(e);
89+
}
90+
}
91+
92+
public <T> T createClient(
93+
Class<T> type,
94+
Converter converter,
95+
RemoteService remoteService,
96+
OkHttpClient okHttpClient,
97+
String username,
98+
String password,
99+
String usernamePasswordFile,
100+
String bearerToken)
77101
throws IOException {
78102
String baseUrl = remoteService.getBaseUrl();
79103

@@ -83,8 +107,10 @@ public <T> T createClient(
83107

84108
if (!(StringUtils.isEmpty(username)
85109
&& StringUtils.isEmpty(password)
86-
&& StringUtils.isEmpty(usernamePasswordFile))) {
87-
okHttpClient = createAuthenticatedClient(username, password, usernamePasswordFile);
110+
&& StringUtils.isEmpty(usernamePasswordFile)
111+
&& StringUtils.isEmpty(bearerToken))) {
112+
okHttpClient =
113+
createAuthenticatedClient(username, password, usernamePasswordFile, bearerToken);
88114
}
89115

90116
Slf4jRetrofitLogger logger = createRetrofitLogger.apply(type);
@@ -100,14 +126,17 @@ public <T> T createClient(
100126
}
101127

102128
private static OkHttpClient createAuthenticatedClient(
103-
String username, String password, String usernamePasswordFile) throws IOException {
129+
String username, String password, String usernamePasswordFile, String bearerToken)
130+
throws IOException {
104131
final String credential;
105132

106133
if (StringUtils.isNotEmpty(usernamePasswordFile)) {
107134
String trimmedFileContent =
108135
new String(Files.readAllBytes(Paths.get(usernamePasswordFile))).trim();
109136

110137
credential = "Basic " + Base64.encodeBase64String(trimmedFileContent.getBytes());
138+
} else if (StringUtils.isNotEmpty(bearerToken)) {
139+
credential = "Bearer " + bearerToken;
111140
} else {
112141
credential = Credentials.basic(username, password);
113142
}

kayenta-prometheus/src/main/java/com/netflix/kayenta/prometheus/config/PrometheusConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ MetricsService prometheusMetricsService(
9292
.username(prometheusManagedAccount.getUsername())
9393
.password(prometheusManagedAccount.getPassword())
9494
.usernamePasswordFile(prometheusManagedAccount.getUsernamePasswordFile())
95+
.bearerToken(prometheusManagedAccount.getBearerToken())
9596
.build();
9697
PrometheusNamedAccountCredentials.PrometheusNamedAccountCredentialsBuilder
9798
prometheusNamedAccountCredentialsBuilder =
@@ -110,7 +111,8 @@ MetricsService prometheusMetricsService(
110111
okHttpClient,
111112
prometheusManagedAccount.getUsername(),
112113
prometheusManagedAccount.getPassword(),
113-
prometheusManagedAccount.getUsernamePasswordFile());
114+
prometheusManagedAccount.getUsernamePasswordFile(),
115+
prometheusManagedAccount.getBearerToken());
114116

115117
prometheusNamedAccountCredentialsBuilder.prometheusRemoteService(
116118
prometheusRemoteService);

kayenta-prometheus/src/main/java/com/netflix/kayenta/prometheus/config/PrometheusManagedAccount.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,7 @@ public class PrometheusManagedAccount {
4444

4545
private List<AccountCredentials.Type> supportedTypes =
4646
Collections.singletonList(AccountCredentials.Type.METRICS_STORE);
47+
48+
// Optional parameter for use when protecting prometheus with bearer token.
49+
private String bearerToken;
4750
}

kayenta-prometheus/src/main/java/com/netflix/kayenta/prometheus/security/PrometheusCredentials.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ public class PrometheusCredentials {
3535
private String password;
3636

3737
private String usernamePasswordFile;
38+
39+
private String bearerToken;
3840
}

kayenta-web/config/kayenta.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ kayenta:
9797
# - name: my-prometheus-account
9898
# endpoint:
9999
# baseUrl: http://localhost:9090
100+
# username: xxxx # Optional, for use when protecting prometheus with basic auth
101+
# password: xxxx # Optional, for use when protecting prometheus with basic auth
102+
# usernamePasswordFile: xxxx # Optional, for use when protecting prometheus with basic auth
103+
# bearerToken: xxxx # Optional, for use when protecting prometheus with bearer token
100104
# supportedTypes:
101105
# - METRICS_STORE
102106

0 commit comments

Comments
 (0)