Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle absent OTX API key in ThreatIntelPluginConfiguration #54

Merged
merged 1 commit into from
Sep 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public abstract class ThreatIntelPluginConfiguration {
public abstract boolean otxEnabled();

@JsonProperty("otx_api_key")
public abstract Optional<String> otxApiKey();
@Nullable
public abstract String otxApiKey();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this be enough so we can keep the nice properties of java.util.Optional?

diff --git src/main/java/org/graylog/plugins/threatintel/ThreatIntelPluginConfiguration.java src/main/java/org/graylog/plugins/threatintel/ThreatIntelPluginConfiguration.java
index 130fa5d..9b983fa 100644
--- src/main/java/org/graylog/plugins/threatintel/ThreatIntelPluginConfiguration.java
+++ src/main/java/org/graylog/plugins/threatintel/ThreatIntelPluginConfiguration.java
@@ -38,7 +37,7 @@ public abstract class ThreatIntelPluginConfiguration {
                                                         @JsonProperty("abusech_ransom_enabled") boolean abusechRansomEnabled) {
         return builder()
                 .otxEnabled(otxEnabled)
-                .otxApiKey(otxApiKey)
+                .otxApiKey(Optional.ofNullable(otxApiKey))
                 .torEnabled(torEnabled)
                 .spamhausEnabled(spamhausEnabled)
                 .abusechRansomEnabled(abusechRansomEnabled)
@@ -62,7 +61,7 @@ public abstract class ThreatIntelPluginConfiguration {
     public static abstract class Builder {
         public abstract Builder otxEnabled(boolean otxEnabled);
 
-        public abstract Builder otxApiKey(String otxApiKey);
+        public abstract Builder otxApiKey(Optional<String> otxApiKey);
 
         public abstract Builder torEnabled(boolean torEnabled);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, but Optional<T> is pretty useless in this case anyway (check the usages of ThreatIntelPluginConfiguration#otxApiKey()).
In other words, getting rid of the Optional<T> meant having less code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't call it useless, but fair enough.


@JsonProperty("tor_enabled")
public abstract boolean torEnabled();
Expand Down Expand Up @@ -62,7 +63,7 @@ public static ThreatIntelPluginConfiguration defaults() {
public static abstract class Builder {
public abstract Builder otxEnabled(boolean otxEnabled);

public abstract Builder otxApiKey(String otxApiKey);
abstract Builder otxApiKey(String otxApiKey);

public abstract Builder torEnabled(boolean torEnabled);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public void upgrade() {
}

final ThreatIntelPluginConfiguration pluginConfig = clusterConfigService.get(ThreatIntelPluginConfiguration.class);
if (pluginConfig == null || pluginConfig.otxApiKey() == null || !pluginConfig.otxApiKey().isPresent()) {
if (pluginConfig == null || pluginConfig.otxApiKey() == null) {
clusterConfigService.write(MigrationCompleted.notConvertedKey());
return;
}

final String otxApiKey = pluginConfig.otxApiKey().get();
final String otxApiKey = pluginConfig.otxApiKey();
final DataAdapterDto dataAdapterDto = this.dbDataAdapterService.get(OTX_DATA_ADAPTER_NAME)
.orElseThrow(() -> new IllegalStateException("OTX data adapter not present when trying to add API token."));

Expand All @@ -67,7 +67,8 @@ public void upgrade() {
}

final HTTPJSONPathDataAdapter.Config config = (HTTPJSONPathDataAdapter.Config)dataAdapterDto.config();
final Map<String, String> newHeaders = new HashMap<>(config.headers() != null ? config.headers() : Collections.emptyMap());
final Map<String, String> headers = config.headers();
final Map<String, String> newHeaders = new HashMap<>(headers != null ? headers : Collections.emptyMap());
newHeaders.put("X-OTX-API-KEY", otxApiKey);
final HTTPJSONPathDataAdapter.Config.Builder updatedConfigBuilder = HTTPJSONPathDataAdapter.Config.builder()
.type(config.type())
Expand Down