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

Create TIF Source Config API #1046

Merged

Conversation

jowg-amazon
Copy link
Collaborator

Description

Preliminary architecture and flow for creating a job index and indexing a source config object.

Issues Resolved

[List any issues this PR will resolve]

Check List

  • New functionality includes testing.
    • All tests pass
  • New functionality has been documented.
    • New functionality has javadoc added
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Signed-off-by: Joanne Wang <[email protected]>
@@ -103,9 +111,11 @@ public class SecurityAnalyticsPlugin extends Plugin implements ActionPlugin, Map
public static final String FINDINGS_CORRELATE_URI = FINDINGS_BASE_URI + "/correlate";
public static final String LIST_CORRELATIONS_URI = PLUGINS_BASE_URI + "/correlations";
public static final String CORRELATION_RULES_BASE_URI = PLUGINS_BASE_URI + "/correlation/rules";

public static final String TIF_CONFIG_URI = PLUGINS_BASE_URI + "/tif";
Copy link
Member

@eirsep eirsep May 24, 2024

Choose a reason for hiding this comment

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

tif source?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

updated to /tif/source

Signed-off-by: Joanne Wang <[email protected]>
Signed-off-by: Joanne Wang <[email protected]>
@@ -103,9 +111,12 @@ public class SecurityAnalyticsPlugin extends Plugin implements ActionPlugin, Map
public static final String FINDINGS_CORRELATE_URI = FINDINGS_BASE_URI + "/correlate";
public static final String LIST_CORRELATIONS_URI = PLUGINS_BASE_URI + "/correlations";
public static final String CORRELATION_RULES_BASE_URI = PLUGINS_BASE_URI + "/correlation/rules";

public static final String TIF_BASE_URI = PLUGINS_BASE_URI + "/tif";
Copy link
Member

@eirsep eirsep May 24, 2024

Choose a reason for hiding this comment

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

threat_intel?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

changed to threat_intel

@@ -103,9 +111,12 @@ public class SecurityAnalyticsPlugin extends Plugin implements ActionPlugin, Map
public static final String FINDINGS_CORRELATE_URI = FINDINGS_BASE_URI + "/correlate";
public static final String LIST_CORRELATIONS_URI = PLUGINS_BASE_URI + "/correlations";
public static final String CORRELATION_RULES_BASE_URI = PLUGINS_BASE_URI + "/correlation/rules";

public static final String TIF_BASE_URI = PLUGINS_BASE_URI + "/tif";
public static final String TIF_SOURCE_CONFIG_URI = PLUGINS_BASE_URI + "/tif/source";
Copy link
Member

Choose a reason for hiding this comment

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

threat_intel/source?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

changed to threat_intel/source

@@ -129,6 +140,9 @@ public class SecurityAnalyticsPlugin extends Plugin implements ActionPlugin, Map
private BuiltinLogTypeLoader builtinLogTypeLoader;

private LogTypeService logTypeService;

private SATIFSourceConfigDao satifSourceConfigDao;
Copy link
Member

Choose a reason for hiding this comment

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

SaTifSourceConfigDao*

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@@ -211,13 +228,14 @@ public List<RestHandler> getRestHandlers(Settings settings,
new RestSearchCorrelationRuleAction(),
new RestIndexCustomLogTypeAction(),
new RestSearchCustomLogTypeAction(),
new RestDeleteCustomLogTypeAction()
new RestDeleteCustomLogTypeAction(),
new RestIndexTIFConfigAction()
Copy link
Member

Choose a reason for hiding this comment

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

RestIndexTIFSourceConfigAction

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

case FEED_SOURCE_CONFIG_FIELD:
return SATIFSourceConfig.parse(xcp, id, null);
default:
log.warn("Unsupported document was indexed");
Copy link
Member

Choose a reason for hiding this comment

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

log level should be error
re-word message to make it more descriptive

message should include job parser failed, $fieldName (value), in security analytics job registration

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

reworded error message

xcp.nextToken();
switch (fieldName) {
case FEED_SOURCE_CONFIG_FIELD:
return SATIFSourceConfig.parse(xcp, id, null);
Copy link
Member

Choose a reason for hiding this comment

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

why is the third param null

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will address this in a future PR, need to understand what params should be used for parser

public class SAIndexTIFSourceConfigRequest extends ActionRequest implements IndexTIFSourceConfigRequest {
private static final ParameterValidator VALIDATOR = new ParameterValidator();
private String tifSourceConfigId;
private final WriteRequest.RefreshPolicy refreshPolicy;
Copy link
Member

Choose a reason for hiding this comment

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

dont accept param. always IMMEDIATE REFRESH

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

removed param

private final String tifConfigId;
private final Long version;
private final RestStatus status;
private final SATIFSourceConfigDto saTIFConfigDto;
Copy link
Member

Choose a reason for hiding this comment

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

if a word is an a acronym let's make it liek SaTifSourceConfigDto

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed


public void indexTIFSourceConfig(SATIFSourceConfig satifSourceConfig,
TimeValue indexTimeout,
WriteRequest.RefreshPolicy refreshPolicy,
Copy link
Member

Choose a reason for hiding this comment

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

should always be refresh immediate. remove param

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

removed

public void indexTIFSourceConfig(SATIFSourceConfig satifSourceConfig,
TimeValue indexTimeout,
WriteRequest.RefreshPolicy refreshPolicy,
final ActionListener<SATIFSourceConfig> actionListener) throws Exception {
Copy link
Member

Choose a reason for hiding this comment

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

throws Exception is not needed

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

removed

.source(satifSourceConfig.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS))
.timeout(indexTimeout);
log.debug("Indexing tif source config");
client.index(indexRequest, new ActionListener<>() {
Copy link
Member

Choose a reason for hiding this comment

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

use only ActionListener.wrap()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

changed to ActionListener.wrap()

}
@Override
public void onFailure(Exception e) {
throw new SecurityAnalyticsException("Exception saving the tif source config in index", RestStatus.INTERNAL_SERVER_ERROR, e);
Copy link
Member

Choose a reason for hiding this comment

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

use listener.onFailure don't re-throw

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

TimeValue indexTimeout,
WriteRequest.RefreshPolicy refreshPolicy,
final ActionListener<SATIFSourceConfig> actionListener) throws Exception {
IndexRequest indexRequest = new IndexRequest(SecurityAnalyticsPlugin.JOB_INDEX_NAME)
Copy link
Member

Choose a reason for hiding this comment

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

wrap entire logic in try-catch and listener.onFailure + log message in catch block

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

client.index(indexRequest, new ActionListener<>() {
@Override
public void onResponse(IndexResponse response) {
log.debug("TIF source config indexed success.");
Copy link
Member

Choose a reason for hiding this comment

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

we need to log the document id.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

logged id in message

@Override
public void onResponse(IndexResponse response) {
log.debug("TIF source config indexed success.");
satifSourceConfig.setId(response.getId());
Copy link
Member

Choose a reason for hiding this comment

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

copy to new object.

}
} catch (IOException e) {
log.error("Runtime exception when getting the threat intel index mapping", e);
throw new SecurityAnalyticsException("Runtime exception when getting the threat intel index mapping", RestStatus.INTERNAL_SERVER_ERROR, e);
Copy link
Member

@eirsep eirsep May 24, 2024

Choose a reason for hiding this comment

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

Message can be "Failed to get threat intel index mapping"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed message

Signed-off-by: Joanne Wang <[email protected]>
stepListener.onResponse(null);
return;
}
log.error("Failed to create security analytics threat intel job index", e);
Copy link
Member

Choose a reason for hiding this comment

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

nit: log correct index name in message

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed to log the index name in error message


import org.opensearch.core.action.ActionListener;
public interface TIFSourceConfigDao {
IndexTIFSourceConfigResponse indexTIFConfig(IndexTIFSourceConfigRequest request, ActionListener <IndexTIFSourceConfigResponse> listener);
Copy link
Member

Choose a reason for hiding this comment

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

response would be at transport and rest layer?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Renamed to TIFSourceConfigService and changed to abstract class, will follow up on implementation in future PR.

log.error("failed to release lock", exception);
listener.onFailure(exception);
});
SaTifSourceConfigDao.createJobIndexIfNotExists(createIndexStepListener);
Copy link
Member

Choose a reason for hiding this comment

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

this should be done in dao itself before create

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

moved the function invocation to the dao

@@ -179,7 +179,7 @@ public Collection<Object> createComponents(Client client,
TIFJobParameterService tifJobParameterService = new TIFJobParameterService(client, clusterService);
TIFJobUpdateService tifJobUpdateService = new TIFJobUpdateService(clusterService, tifJobParameterService, threatIntelFeedDataService, builtInTIFMetadataLoader);
TIFLockService threatIntelLockService = new TIFLockService(clusterService, client);
SaTifSourceConfigDao = new SATIFSourceConfigDao(client, clusterService, threadPool);
Copy link
Member

Choose a reason for hiding this comment

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

what is this lock service??

actionListener.onFailure(e);
}
}, exception -> {
lockService.releaseLock(lock);
Copy link
Member

Choose a reason for hiding this comment

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

what does this lockservice achieve??

public interface TIFSourceConfigDao {
IndexTIFSourceConfigResponse indexTIFConfig(IndexTIFSourceConfigRequest request, ActionListener <IndexTIFSourceConfigResponse> listener);
public abstract class TIFSourceConfigService {
IndexTIFSourceConfigResponse indexTIFConfig(IndexTIFSourceConfigRequest request, ActionListener <IndexTIFSourceConfigResponse> listener){
Copy link
Member

Choose a reason for hiding this comment

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

method should be abstract

Copy link
Member

@eirsep eirsep left a comment

Choose a reason for hiding this comment

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

plz address comments in followup PR

@eirsep eirsep merged commit 1935ed2 into opensearch-project:feature/threat_intel May 29, 2024
2 checks passed
eirsep pushed a commit that referenced this pull request Jun 3, 2024
* create tif source config api implementation

Signed-off-by: Joanne Wang <[email protected]>

* clean up

Signed-off-by: Joanne Wang <[email protected]>

* tif/source

Signed-off-by: Joanne Wang <[email protected]>

* fix uri

Signed-off-by: Joanne Wang <[email protected]>

* comments

Signed-off-by: Joanne Wang <[email protected]>

* fix error message

Signed-off-by: Joanne Wang <[email protected]>

* moved createIndex invocation and other comments

Signed-off-by: Joanne Wang <[email protected]>

---------

Signed-off-by: Joanne Wang <[email protected]>
jowg-amazon added a commit to jowg-amazon/security-analytics that referenced this pull request Jun 4, 2024
* create tif source config api implementation

Signed-off-by: Joanne Wang <[email protected]>

* clean up

Signed-off-by: Joanne Wang <[email protected]>

* tif/source

Signed-off-by: Joanne Wang <[email protected]>

* fix uri

Signed-off-by: Joanne Wang <[email protected]>

* comments

Signed-off-by: Joanne Wang <[email protected]>

* fix error message

Signed-off-by: Joanne Wang <[email protected]>

* moved createIndex invocation and other comments

Signed-off-by: Joanne Wang <[email protected]>

---------

Signed-off-by: Joanne Wang <[email protected]>
eirsep pushed a commit to eirsep/security-analytics that referenced this pull request Jun 6, 2024
* create tif source config api implementation

Signed-off-by: Joanne Wang <[email protected]>

* clean up

Signed-off-by: Joanne Wang <[email protected]>

* tif/source

Signed-off-by: Joanne Wang <[email protected]>

* fix uri

Signed-off-by: Joanne Wang <[email protected]>

* comments

Signed-off-by: Joanne Wang <[email protected]>

* fix error message

Signed-off-by: Joanne Wang <[email protected]>

* moved createIndex invocation and other comments

Signed-off-by: Joanne Wang <[email protected]>

---------

Signed-off-by: Joanne Wang <[email protected]>
eirsep pushed a commit that referenced this pull request Jun 6, 2024
* create tif source config api implementation

Signed-off-by: Joanne Wang <[email protected]>

* clean up

Signed-off-by: Joanne Wang <[email protected]>

* tif/source

Signed-off-by: Joanne Wang <[email protected]>

* fix uri

Signed-off-by: Joanne Wang <[email protected]>

* comments

Signed-off-by: Joanne Wang <[email protected]>

* fix error message

Signed-off-by: Joanne Wang <[email protected]>

* moved createIndex invocation and other comments

Signed-off-by: Joanne Wang <[email protected]>

---------

Signed-off-by: Joanne Wang <[email protected]>
AWSHurneyt pushed a commit to AWSHurneyt/security-analytics that referenced this pull request Jun 25, 2024
* create tif source config api implementation

Signed-off-by: Joanne Wang <[email protected]>

* clean up

Signed-off-by: Joanne Wang <[email protected]>

* tif/source

Signed-off-by: Joanne Wang <[email protected]>

* fix uri

Signed-off-by: Joanne Wang <[email protected]>

* comments

Signed-off-by: Joanne Wang <[email protected]>

* fix error message

Signed-off-by: Joanne Wang <[email protected]>

* moved createIndex invocation and other comments

Signed-off-by: Joanne Wang <[email protected]>

---------

Signed-off-by: Joanne Wang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants