Skip to content

Commit

Permalink
chore: basic auth (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
korniltsev authored Apr 23, 2023
1 parent 6b5b56c commit 6b70a16
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
32 changes: 29 additions & 3 deletions agent/src/main/java/io/pyroscope/javaagent/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public final class Config {
private static final String PYROSCOPE_SERVER_ADDRESS_CONFIG = "PYROSCOPE_SERVER_ADDRESS";
private static final String PYROSCOPE_ADHOC_SERVER_ADDRESS_CONFIG = "PYROSCOPE_ADHOC_SERVER_ADDRESS";
private static final String PYROSCOPE_AUTH_TOKEN_CONFIG = "PYROSCOPE_AUTH_TOKEN";
private static final String PYROSCOPE_BASIC_AUTH_USER_CONFIG = "PYROSCOPE_BASIC_AUTH_USER";
private static final String PYROSCOPE_BASIC_AUTH_PASSWORD_CONFIG = "PYROSCOPE_BASIC_AUTH_PASSWORD";
private static final String PYROSCOPE_FORMAT_CONFIG = "PYROSCOPE_FORMAT";
private static final String PYROSCOPE_PUSH_QUEUE_CAPACITY_CONFIG = "PYROSCOPE_PUSH_QUEUE_CAPACITY";
private static final String PYROSCOPE_LABELS = "PYROSCOPE_LABELS";
Expand Down Expand Up @@ -101,6 +103,8 @@ public final class Config {
public final String scopeOrgID;
public final String APLogLevel;
public final String APExtraArguments;
public final String basicAuthUser;
public final String basicAuthPassword;

Config(final String applicationName,
final Duration profilingInterval,
Expand All @@ -123,7 +127,9 @@ public final class Config {
Duration samplingDuration,
String scopeOrgID,
String APLogLevel,
String APExtraArguments) {
String APExtraArguments,
String basicAuthUser,
String basicAuthPassword) {
this.applicationName = applicationName;
this.profilingInterval = profilingInterval;
this.profilingEvent = profilingEvent;
Expand All @@ -143,6 +149,8 @@ public final class Config {
this.scopeOrgID = scopeOrgID;
this.APLogLevel = APLogLevel;
this.APExtraArguments = APExtraArguments;
this.basicAuthUser = basicAuthUser;
this.basicAuthPassword = basicAuthPassword;
this.timeseries = timeseriesName(AppName.parse(applicationName), profilingEvent, format);
this.timeseriesName = timeseries.toString();
this.format = format;
Expand Down Expand Up @@ -224,7 +232,9 @@ public static Config build(ConfigurationProvider cp) {
samplingDuration(cp),
scopeOrgID(cp),
cp.get(PYROSCOPE_AP_LOG_LEVEL_CONFIG),
cp.get(PYROSCOPE_AP_EXTRA_ARGUMENTS_CONFIG));
cp.get(PYROSCOPE_AP_EXTRA_ARGUMENTS_CONFIG),
cp.get(PYROSCOPE_BASIC_AUTH_USER_CONFIG),
cp.get(PYROSCOPE_BASIC_AUTH_PASSWORD_CONFIG));
}

private static String applicationName(ConfigurationProvider configurationProvider) {
Expand Down Expand Up @@ -547,6 +557,8 @@ public static class Builder {
private String scopeOrgID = null;
private String APLogLevel = null;
private String APExtraArguments = null;
private String basicAuthUser;
private String basicAuthPassword;

public Builder() {
}
Expand All @@ -572,6 +584,8 @@ public Builder(Config buildUpon) {
scopeOrgID = buildUpon.scopeOrgID;
APLogLevel = buildUpon.APLogLevel;
APExtraArguments = buildUpon.APExtraArguments;
basicAuthUser = buildUpon.basicAuthUser;
basicAuthPassword = buildUpon.basicAuthPassword;
}

public Builder setApplicationName(String applicationName) {
Expand Down Expand Up @@ -688,6 +702,16 @@ public Builder setAPExtraArguments(String APExtraArguments) {
return this;
}

public Builder setBasicAuthUser(String basicAuthUser) {
this.basicAuthUser = basicAuthUser;
return this;
}

public Builder setBasicAuthPassword(String basicAuthPassword) {
this.basicAuthPassword = basicAuthPassword;
return this;
}

public Config build() {
if (applicationName == null || applicationName.isEmpty()) {
applicationName = generateApplicationName();
Expand All @@ -713,7 +737,9 @@ public Config build() {
samplingDuration,
scopeOrgID,
APLogLevel,
APExtraArguments);
APExtraArguments,
basicAuthUser,
basicAuthPassword);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,14 @@ private static void addAuthHeader(Request.Builder request, HttpUrl url, Config c
request.header("Authorization", "Bearer " + config.authToken);
return;
}
if (config.basicAuthUser != null && !config.basicAuthUser.isEmpty()
&& config.basicAuthPassword != null && !config.basicAuthPassword.isEmpty()) {
request.header("Authorization", Credentials.basic(config.basicAuthUser, config.basicAuthPassword));
return;
}
String u = url.username();
String p = url.password();
if (!u.isEmpty() || !p.isEmpty()) {
if (!u.isEmpty() && !p.isEmpty()) {
request.header("Authorization", Credentials.basic(u, p));
}
}
Expand Down

0 comments on commit 6b70a16

Please sign in to comment.