Skip to content
This repository was archived by the owner on Aug 20, 2025. It is now read-only.

Conversation

@merrimanr
Copy link
Contributor

Contributor Comments

This PR adds a REST_POST Stellar function with syntax and options similar to the existing REST_GET function for performing REST POST requests. A Stellar map can now be passed into the function and added to the request as query parameters (this feature was added to REST_GET as well). Separate global configs were added for REST_GET and REST_POST in addition to the existing global config for REST functions. For REST_POST requests, there is also an option enforce.json that ensures POST data is well-formed JSON.

Changes Included

  • REST_POST Stellar function was added to RestFunctions class.
  • RestFunctions class was refactored by moving common code used by REST_GET and REST_POST to their own methods. Now code specific to either functions mostly consists of argument parsing and request building.
  • Unit and integration tests were moved to separate tests. The RestFunctions class should be 100% covered.
  • Documentation added for the new REST_POST function and additional options added to both.

Testing

This has been tested in full dev. Since there were significant changes to RestFunctions I regression tested using the test instructions in original Stellar Rest PR. Note a regression was introduced when using the REST_GET function in the enrichment topology (class version error). This is being addressed here.

In addition to this I also followed these instructions:

  1. Assuming full dev has already been spun up during regression testing described above, start the Stellar shell with /usr/metron/0.7.2/bin/stellar --zookeeper node1:2181.

  2. Defined a variable to hold POST data:

[Stellar]>>> data := {'key':'value'}
{key=value}
  1. Send a POST request to a test service (httpbin.org in this example):
[Stellar]>>> REST_POST('http://httpbin.org/post', data)
{args={}, data={"key":"value"}, files={}, form={}, headers={Accept=application/json, Accept-Encoding=gzip,deflate, Content-Length=15, Content-Type=application/json, Host=httpbin.org, User-Agent=Apache-HttpClient/4.3.2 (java 1.5)}, json={key=value}, origin=136.49.31.47, 136.49.31.47, url=https://httpbin.org/post}

The request should succeed with the correct POST data in the response.

  1. Define a variable to hold query parameters:
[Stellar]>>> params := {'param1': 'value1', 'param2': 'value2'}
{param1=value1, param2=value2}
  1. Send a POST request, passing in the query parameter variable:
[Stellar]>>> REST_POST('http://httpbin.org/post', data, {}, params)
{args={param1=value1, param2=value2}, data={"key":"value"}, files={}, form={}, headers={Accept=application/json, Accept-Encoding=gzip,deflate, Content-Length=15, Content-Type=application/json, Host=httpbin.org, User-Agent=Apache-HttpClient/4.3.2 (java 1.5)}, json={key=value}, origin=136.49.31.47, 136.49.31.47, url=https://httpbin.org/post?param1=value1&param2=value2}

The args and url attributes in the response should contain the query parameters.

  1. Test configuration priority by changing the empty response override in various configs. First enable 404 responses and set an override:
%define stellar.rest.settings := {'response.codes.allowed': [200, 404],  'empty.content.override': {}}
[Stellar]>>> REST_POST('http://httpbin.org/status/404', data)
{}

The response should match the override value set in the config.

  1. Configure the override for all REST functions:
[Stellar]>>> REST_POST('http://httpbin.org/status/404', data)
global rest config

The response should match the global override value set in the config.

  1. Configure the override for REST_POST functions:
[Stellar]>>> %define stellar.rest.post.settings := {'empty.content.override': 'rest post config'}
{empty.content.override=rest post config}
[Stellar]>>> REST_POST('http://httpbin.org/status/404', data)
rest post config

The response should match the rest post override value set in the config.

9 Configure the override in a variable that will be passed to the function:

[Stellar]>>> config := {'empty.content.override': 'function config'}
{empty.content.override=function config}
[Stellar]>>> REST_POST('http://httpbin.org/status/404', data, config)
function config

The response should match the override in the config passed to the function.

Pull Request Checklist

Thank you for submitting a contribution to Apache Metron.
Please refer to our Development Guidelines for the complete guide to follow for contributions.
Please refer also to our Build Verification Guidelines for complete smoke testing guides.

In order to streamline the review of the contribution we ask you follow these guidelines and ask you to double check the following:

For all changes:

  • Is there a JIRA ticket associated with this PR? If not one needs to be created at Metron Jira.
  • Does your PR title start with METRON-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
  • Has your PR been rebased against the latest commit within the target branch (typically master)?

For code changes:

  • Have you included steps to reproduce the behavior or problem that is being changed or addressed?

  • Have you included steps or a guide to how the change may be verified and tested manually?

  • Have you ensured that the full suite of tests and checks have been executed in the root metron folder via:

    mvn -q clean integration-test install && dev-utilities/build-utils/verify_licenses.sh 
    
  • Have you written or updated unit tests and or integration tests to verify your changes?

  • If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under ASF 2.0?

  • Have you verified the basic functionality of the build by building and running locally with Vagrant full-dev environment or the equivalent?

For documentation related changes:

  • Have you ensured that format looks appropriate for the output in which it is rendered by building and verifying the site-book? If not then run the following commands and the verify changes via site-book/target/site/index.html:

    cd site-book
    mvn site
    
  • Have you ensured that any documentation diagrams have been updated, along with their source files, using draw.io? See Metron Development Guidelines for instructions.

Note:

Please ensure that once the PR is submitted, you check travis-ci for build issues and submit an update to your PR as soon as possible.
It is also recommended that travis-ci is set up for your personal repository such that your branches are built there before submitting a pull request.

@ottobackwards
Copy link
Contributor

This is cool, but wouldn't https support be needed for real world application?

@ottobackwards
Copy link
Contributor

I understand that if GET doesn't have it, then we've already decided to put that off, but do we have a plan?

@merrimanr
Copy link
Contributor Author

I will do some research into adding SSL support, maybe it's a minimal amount of extra code. I would prefer to add that incrementally in a separate PR since code reviews seem to go faster when split into smaller units of work. I think REST POST support without SSL is an improvement on it's own.

@ottobackwards
Copy link
Contributor

Having worked with the nifi SSL Controller Service, it isn't a small thing. but it is important. I'm fine with doing it later, but please create an investigate jira or something to link to this, so if someone wants to they can pick it up

@mmiklavc
Copy link
Contributor

@merrimanr - did we ever get a Jira for the follow-on @ottobackwards mentioned? Can you link it back here in the comments as well as link it with the Jira for this PR (ie add a "relates-to" relationship to this Jira and the one for REST_GET)?

@merrimanr
Copy link
Contributor Author

@ottobackwards here is the SSL Jira: https://issues.apache.org/jira/browse/METRON-2165. Feel free to add any requirements you want.

Copy link
Contributor

@mmiklavc mmiklavc left a comment

Choose a reason for hiding this comment

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

Only one potential issue stands out to me here, nice work @merrimanr.

httpRequestBase.setConfig(getRequestConfig(restConfig, proxy));

// Schedule a command to abort the request if the timeout is exceeded
ScheduledFuture scheduledFuture = scheduledExecutorService.schedule(httpRequestBase::abort, restConfig.getTimeout(), TimeUnit.MILLISECONDS);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to manage timeouts on our end? e.g. https://www.baeldung.com/httpclient-timeout

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From the link you reference:

HttpClient doesn’t have any configuration that allows us to set an overall timeout for a request; it does, however, provide abort functionality for requests, so we can leverage that mechanism to implement a simple timeout mechanism:

@mmiklavc
Copy link
Contributor

+1 by inspection, thanks @merrimanr!

@asfgit asfgit closed this in 42ce5ad Jul 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants