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

[Filebeat][httpjson] httpjson oauth2 authentication mechanism for salesforce events #29087

Merged
merged 10 commits into from
Dec 2, 2021

Conversation

kush-elastic
Copy link
Collaborator

@kush-elastic kush-elastic commented Nov 23, 2021

  • Enhancement

What does this PR do?

Current httpjson doesn’t have grant_type: password.
This PR contains the codebase for the authentication mechanism for httpjson input which is a part of the Salesforce connector.
Authentication Details:
Added grant_type: password in existing method with configurations.
if auth.oauth2.user and auth.oauth2.password parameter are mentioned in oauth2 configuration, code will automatically uses grant_type as password and create client accordingly.

Why is it important?

  • Current httpjson only support one grant_type.
  • Some APIs need multiple kinds of grant_type.
  • salesforce connector also need grant_type password for authentication.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.next.asciidoc or CHANGELOG-developer.next.asciidoc.

Related issues

Use cases

Configuration Example:

filebeat.inputs:
- type: httpjson
  auth.oauth2:
    client.id: 12345678901234567890abcdef
    client.secret: abcdef12345678901234567890
    token_url: http://localhost/oauth2/token
    user: user@domain.tld
    password: P@$$W0₹D
  request.url: http://localhost

Screenshots

Logs

@botelastic botelastic bot added the needs_team Indicates that the issue/PR needs a Team:* label label Nov 23, 2021
@mergify
Copy link
Contributor

mergify bot commented Nov 23, 2021

This pull request does not have a backport label. Could you fix it @kush-elastic? 🙏
To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-v./d./d./d is the label to automatically backport to the 7./d branch. /d is the digit

NOTE: backport-skip has been added to this pull request.

@mergify mergify bot added the backport-skip Skip notification from the automated backport with mergify label Nov 23, 2021
@elasticmachine
Copy link
Collaborator

elasticmachine commented Nov 23, 2021

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2021-12-01T14:10:42.244+0000

  • Duration: 99 min 8 sec

  • Commit: d4d21c1

Test stats 🧪

Test Results
Failed 0
Passed 2413
Skipped 152
Total 2565

💚 Flaky test report

Tests succeeded.

🤖 GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

  • /package : Generate the packages and run the E2E tests.

  • /beats-tester : Run the installation tests with beats-tester.

  • run elasticsearch-ci/docs : Re-trigger the docs validation. (use unformatted text in the comment!)

@kush-elastic kush-elastic changed the title [Filebeat][httpjson] Add grant-type: passoword in httpjson oauth2 [Filebeat][httpjson] httpjson oauth2 authentication mechanism for salesforce events Nov 23, 2021
Copy link
Contributor

@mtojek mtojek left a comment

Choose a reason for hiding this comment

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

We need a changelog entry for this PR.

@@ -82,6 +82,8 @@ type oAuth2Config struct {
// common oauth fields
ClientID string `config:"client.id"`
ClientSecret string `config:"client.secret"`
User string `config:"user"`
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please put these properties in alpha-num order?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure. I will update it.

TokenURL: o.getTokenURL(),
Scopes: o.Scopes,
EndpointParams: o.getEndpointParams(),
if o.User != "" || o.Password != "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it also valid for Azure?

Do you think it's worth considering introducing another type: oauth2ProviderSalesforce?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am not sure but they do support username-password authentication here.
If its valid then that means they will also be able to use another authentication mechanism.
I will look into it if its not ok then we can add oauth2ProviderSalesforce

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think it's worth focusing on the oauth2ProviderSalesforce provider now?

Copy link
Collaborator Author

@kush-elastic kush-elastic Nov 25, 2021

Choose a reason for hiding this comment

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

I think we have another option.
what if we use oAuth2ProviderDefault:

	case oAuth2ProviderDefault:
		if o.User != "" || o.Password != "" {
			conf := &oauth2.Config{
				ClientID:     o.ClientID,
				ClientSecret: o.ClientSecret,
				Endpoint: oauth2.Endpoint{
					TokenURL:  o.TokenURL,
					AuthStyle: AuthStyleInParams,
				},
			}
			token, err := conf.PasswordCredentialsToken(ctx, o.User, o.Password)
			if err != nil {
				return nil, fmt.Errorf("oauth2 client: error loading credentials using user and password: %w", err)
			}
			return conf.Client(ctx, token), nil
		} else {
			creds := clientcredentials.Config{
				ClientID:       o.ClientID,
				ClientSecret:   o.ClientSecret,
				TokenURL:       o.getTokenURL(),
				Scopes:         o.Scopes,
				EndpointParams: o.getEndpointParams(),
			}
			return creds.Client(ctx), nil
		}
	case oAuth2ProviderAzure:
		creds := clientcredentials.Config{
			ClientID:       o.ClientID,
			ClientSecret:   o.ClientSecret,
			TokenURL:       o.getTokenURL(),
			Scopes:         o.Scopes,
			EndpointParams: o.getEndpointParams(),
		}
		return creds.Client(ctx), nil

What do you think if we do this?
Instead creating new provider we can use default one and user will also able use user-password method for other than salesforce.

Copy link
Contributor

@mtojek mtojek Nov 25, 2021

Choose a reason for hiding this comment

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

RFC defines it (Resource Owner Password Credentials Grant), so I don't see a reason why don't apply it also here. Feel free to modify the source code.

x-pack/filebeat/input/httpjson/config_auth.go Show resolved Hide resolved
x-pack/filebeat/input/httpjson/config_auth.go Outdated Show resolved Hide resolved
x-pack/filebeat/input/httpjson/config_auth.go Outdated Show resolved Hide resolved
x-pack/filebeat/input/httpjson/config_auth.go Outdated Show resolved Hide resolved
AuthStyle: 1,
},
}
token, err := conf.PasswordCredentialsToken(ctx, o.User, o.Password)
Copy link
Contributor

Choose a reason for hiding this comment

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

Any ideas for covering this logic with unit tests?

Copy link
Contributor

Choose a reason for hiding this comment

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

as an idea, if we want to test this bit in isolation, could get extracted to its own function and validate the resulting token

@mtojek mtojek removed the backport-skip Skip notification from the automated backport with mergify label Nov 23, 2021
@mergify
Copy link
Contributor

mergify bot commented Nov 23, 2021

This pull request does not have a backport label. Could you fix it @kush-elastic? 🙏
To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-v./d./d./d is the label to automatically backport to the 7./d branch. /d is the digit

NOTE: backport-skip has been added to this pull request.

@mergify mergify bot added the backport-skip Skip notification from the automated backport with mergify label Nov 23, 2021
@mtojek mtojek added Team:Integrations Label for the Integrations team and removed backport-skip Skip notification from the automated backport with mergify labels Nov 23, 2021
@botelastic botelastic bot removed the needs_team Indicates that the issue/PR needs a Team:* label label Nov 23, 2021
@mergify
Copy link
Contributor

mergify bot commented Nov 23, 2021

This pull request does not have a backport label. Could you fix it @kush-elastic? 🙏
To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-v./d./d./d is the label to automatically backport to the 7./d branch. /d is the digit

NOTE: backport-skip has been added to this pull request.

@mergify mergify bot added the backport-skip Skip notification from the automated backport with mergify label Nov 23, 2021
@mtojek mtojek added needs_team Indicates that the issue/PR needs a Team:* label Team:SIEM and removed backport-skip Skip notification from the automated backport with mergify labels Nov 23, 2021
@botelastic botelastic bot removed the needs_team Indicates that the issue/PR needs a Team:* label label Nov 23, 2021
@botelastic
Copy link

botelastic bot commented Nov 23, 2021

This pull request doesn't have a Team:<team> label.

@mergify
Copy link
Contributor

mergify bot commented Nov 23, 2021

This pull request does not have a backport label. Could you fix it @kush-elastic? 🙏
To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-v./d./d./d is the label to automatically backport to the 7./d branch. /d is the digit

NOTE: backport-skip has been added to this pull request.

@mergify mergify bot added the backport-skip Skip notification from the automated backport with mergify label Nov 23, 2021
@mtojek mtojek requested a review from marc-gr November 23, 2021 09:37
@mtojek mtojek added the backport-v8.0.0 Automated backport with mergify label Nov 23, 2021
@mergify mergify bot removed the backport-skip Skip notification from the automated backport with mergify label Nov 23, 2021
@mtojek
Copy link
Contributor

mtojek commented Nov 23, 2021

/test

TokenURL: o.getTokenURL(),
Scopes: o.Scopes,
EndpointParams: o.getEndpointParams(),
if o.User != "" || o.Password != "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think it's worth focusing on the oauth2ProviderSalesforce provider now?

x-pack/filebeat/input/httpjson/config_auth.go Outdated Show resolved Hide resolved
@kush-elastic kush-elastic deleted the salesforce_httpjson_input branch November 26, 2021 06:35
@elasticmachine
Copy link
Collaborator

Pinging @elastic/siem (Team:SIEM)

@elasticmachine
Copy link
Collaborator

Pinging @elastic/integrations (Team:Integrations)

Copy link
Contributor

@marc-gr marc-gr left a comment

Choose a reason for hiding this comment

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

Thanks for this! Is looking good so far aside of a couple nits. Also would be nice to add the related docs in https://github.com/elastic/beats/blob/master/x-pack/filebeat/docs/inputs/input-httpjson.asciidoc for the new options and any consideration needed when using them.

@@ -22,6 +22,8 @@ import (
"github.com/elastic/beats/v7/libbeat/common"
)

const authStyleInParams = 1
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be nice to add a brief comment in here to know what is used for, and maybe declare it local to where it is going to be used if not expected to be reused anymore.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, Got it.
Thanks

Copy link
Contributor

Choose a reason for hiding this comment

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

Done. added one liner describing the context.

AuthStyle: 1,
},
}
token, err := conf.PasswordCredentialsToken(ctx, o.User, o.Password)
Copy link
Contributor

Choose a reason for hiding this comment

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

as an idea, if we want to test this bit in isolation, could get extracted to its own function and validate the resulting token

Copy link
Contributor

@mtojek mtojek left a comment

Choose a reason for hiding this comment

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

@cla-checker-service
Copy link

cla-checker-service bot commented Nov 30, 2021

💚 CLA has been signed

@sunny-elastic
Copy link
Contributor

Please update also the docs: https://github.com/elastic/beats/blob/c77bf19b66a95c0bb669c06b4ffb8e7ba773a22d/x-pack/filebeat/docs/inputs/input-httpjson.asciidoc

... and I think this would be in a good shape to merge.

Updated the doc input-httpjson.asciidoc as per new extended auth mechanism.

@@ -82,6 +82,8 @@ filebeat.inputs:
client.id: 12345678901234567890abcdef
client.secret: abcdef12345678901234567890
token_url: http://localhost/oauth2/token
user: abc.xyz@mail.com
Copy link
Contributor

Choose a reason for hiding this comment

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

Mail.com is a product https://www.mail.com/int/ :) Let's not suggest anything. How about user@domain.tld?

Copy link
Contributor

Choose a reason for hiding this comment

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

sure :) its updated.

==== `auth.oauth2.user`

The user used as part of the authentication flow. It is required for authentication
- grant type password. It is always required except if using `google` as provider.
Copy link
Contributor

@mtojek mtojek Nov 30, 2021

Choose a reason for hiding this comment

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

It doesn't look to be correct. The user option goes in pair with password for the default provider only. @kush-elastic would you mind proofreading docs?

@mergify
Copy link
Contributor

mergify bot commented Nov 30, 2021

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b salesforce_httpjson_input upstream/salesforce_httpjson_input
git merge upstream/master
git push upstream salesforce_httpjson_input

@kush-elastic kush-elastic requested review from a team as code owners December 1, 2021 05:06
@botelastic botelastic bot added the Team:Automation Label for the Observability productivity team label Dec 1, 2021
@mtojek
Copy link
Contributor

mtojek commented Dec 1, 2021

Wow, 234 files changed. I think you should consider rebasing against the master branch to see only changes relevant to this PR.

@cachedout cachedout removed the request for review from a team December 1, 2021 11:55
@mtojek
Copy link
Contributor

mtojek commented Dec 1, 2021

/test

Copy link
Contributor

@mtojek mtojek left a comment

Choose a reason for hiding this comment

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

LGTM. Let's merge this one if CI is happy.

@mtojek mtojek merged commit 008182a into elastic:master Dec 2, 2021
mergify bot pushed a commit that referenced this pull request Dec 2, 2021
…esforce events (#29087)

* Add grant-type: passoword in httpjson oauth2

* refactor code and add new properties tests in config_test.go

* Add grant_type: password in oAuth2ProviderDefault

* Update CHANGELOG.next.asciidoc

* update input-httpjson.asciidoc with new extended httpjson authentication method

* add comment for authstyleparam variable

* update user dummy value in the doc

* Update input-httpjson.asciidoc - provider should be default only for user-passowrd method

* refactor code and add new properties tests in config_test.go

* Add grant_type: password in oAuth2ProviderDefault

Co-authored-by: Sunny Chaudhari <sunny.chaudhari@elastic.co>
(cherry picked from commit 008182a)
mtojek pushed a commit that referenced this pull request Dec 9, 2021
…esforce events (#29087) (#29246)

* Add grant-type: passoword in httpjson oauth2

* refactor code and add new properties tests in config_test.go

* Add grant_type: password in oAuth2ProviderDefault

* Update CHANGELOG.next.asciidoc

* update input-httpjson.asciidoc with new extended httpjson authentication method

* add comment for authstyleparam variable

* update user dummy value in the doc

* Update input-httpjson.asciidoc - provider should be default only for user-passowrd method

* refactor code and add new properties tests in config_test.go

* Add grant_type: password in oAuth2ProviderDefault

Co-authored-by: Sunny Chaudhari <sunny.chaudhari@elastic.co>
(cherry picked from commit 008182a)

Co-authored-by: Kush Rana <89848966+kush-elastic@users.noreply.github.com>
Co-authored-by: Nicolas Ruflin <spam@ruflin.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-v8.0.0 Automated backport with mergify Team:Automation Label for the Observability productivity team Team:Integrations Label for the Integrations team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants