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

Add JWT flow #426

Merged
merged 1 commit into from
Dec 28, 2021
Merged

Add JWT flow #426

merged 1 commit into from
Dec 28, 2021

Conversation

tzheleznyak
Copy link
Contributor

Desired Outcome

Added JWT flow to the authenticator

Connected Issue/Story

ONYX-14722

Changelog

  • The CHANGELOG has been updated, or
  • This PR does not include user-facing changes and doesn't require a
    CHANGELOG update

Test coverage

  • This PR includes new unit and integration tests to go with the code
    changes, or
  • The changes in this PR do not require tests

Documentation

  • Docs (e.g. READMEs) were updated in this PR
  • A follow-up issue to update official docs has been filed here: insert issue ID
  • This PR does not require updating any documentation

Behavior

  • This PR changes product behavior and has been reviewed by a PO, or
  • These changes are part of a larger initiative that will be reviewed later, or
  • No behavior was changed with this PR

Security

  • Security architect has reviewed the changes in this PR,
  • These changes are part of a larger initiative with a separate security review, or
  • There are no security aspects to these changes

@tzheleznyak tzheleznyak requested review from szh, doodlesbykumbi, diverdane and a team December 23, 2021 15:44
Copy link
Contributor

@doodlesbykumbi doodlesbykumbi left a comment

Choose a reason for hiding this comment

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

Some comments

pkg/authenticator/authenticator_factory.go Outdated Show resolved Hide resolved
pkg/authenticator/jwt/authenticator.go Outdated Show resolved Hide resolved
@tzheleznyak tzheleznyak force-pushed the add-jwt-flow branch 3 times, most recently from 810c3fa to ec99e32 Compare December 26, 2021 08:39
Comment on lines 34 to 31
req.Header.Set("Content-Type", "text/plain")
req.Header.Set("Content-Length", string(len(formattedJwt)))
req.Header.Set("User-Agent", "k8s")

Choose a reason for hiding this comment

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

Do we want to add support for accept-encoding header?
Conjur is able to return access token already encoded base64 instead of plain json (1, 2)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently the components that use authn-k8s-client do the encode themselves . I think this is good idea but not in scope. I wanted to be aligned with the auhtn-k8s flow

@tzheleznyak tzheleznyak force-pushed the add-jwt-flow branch 3 times, most recently from 235938e to 82cfcc7 Compare December 26, 2021 14:45
@tzheleznyak tzheleznyak marked this pull request as ready for review December 26, 2021 14:46
@tzheleznyak tzheleznyak requested review from a team as code owners December 26, 2021 14:46
CHANGELOG.md Outdated
@@ -8,10 +8,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Added
- Add support for tracing with OpenTelemetry. This adds a new function to the authenticator, `AuthenticateWithContext`. The existing funtion, `Authenticate()` is deprecated and will be removed in a future upddate. [cyberark/conjur-authn-k8s-client#423](https://github.com/cyberark/conjur-authn-k8s-client/pull/423)
- Authn JWT flow support [cyberark/conjur-authn-k8s-client#426](https://github.com/cyberark/conjur-authn-k8s-client/pull/426)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@szamir1 @shulifink
May you review my code

@tzheleznyak tzheleznyak force-pushed the add-jwt-flow branch 2 times, most recently from e9d0cd1 to 8adadfe Compare December 27, 2021 08:13
authenticatingIdentity = ""
}

req, err := AuthenticateRequest(

Choose a reason for hiding this comment

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

i think we should align the context handling with this code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This what we talked about and i fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh i see i done it in all error handling in the function

Copy link

@nessiLahav nessiLahav left a comment

Choose a reason for hiding this comment

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

Small comments, great job

@tzheleznyak tzheleznyak force-pushed the add-jwt-flow branch 3 times, most recently from 86f070f to 6a81934 Compare December 27, 2021 11:02
Copy link
Contributor

@szh szh left a comment

Choose a reason for hiding this comment

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

Looking good, left a few comments

pkg/authenticator/jwt/authenticator.go Show resolved Hide resolved
pkg/authenticator/jwt/authenticator.go Show resolved Hide resolved
pkg/authenticator/jwt/tests/authenticator_test.go Outdated Show resolved Hide resolved
pkg/authenticator/jwt/tests/authenticator_test_server.go Outdated Show resolved Hide resolved
szh
szh previously approved these changes Dec 27, 2021
Copy link
Contributor

@szh szh left a comment

Choose a reason for hiding this comment

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

LGTM! 👍

Copy link
Contributor

@diverdane diverdane left a comment

Choose a reason for hiding this comment

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

Looks good!!!
Some editorial comments and a possible missing strconv.Itoa().

config.Common = common.Config{}
config.Common.LoadConfig(settings)

for key, value := range settings {
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of looping through the range of settings, couldn't we see if the "JWT_TOKEN_PATH" entry exists and then use it? Something like this?:

    if path, exists := settings["JWT_TOKEN_PATH"]; exists {
        config.JWTTokenFilePath = path
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point thanks

}

req.Header.Set("Content-Type", "text/plain")
req.Header.Set("Content-Length", string(len(formattedJwt)))
Copy link
Contributor

Choose a reason for hiding this comment

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

When an int is typecast using string(), the int value is interpreted as Unicode. I believe that what we need here is:

    req.Header.Set("Content-Length", strconv.Itoa(len(formattedJwt)))

func createUrl(authnURL string, account string, username string) string {
if len(username) > 0 {
return fmt.Sprintf("%s/%s/%s/authenticate", authnURL, account, url.QueryEscape(username))
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

The if block ends with a return, so the } else { isn't needed. That is, this should be:

	if len(username) > 0 {
		return fmt.Sprintf("%s/%s/%s/authenticate", authnURL, account, url.QueryEscape(username))
	}
	return fmt.Sprintf("%s/%s/authenticate", authnURL, account)

@@ -44,6 +45,8 @@ func ConfigFromEnv(readFileFunc common.ReadFileFunc) (Configuration, error) {
func getConfiguration(url string) (Configuration, error) {
if strings.Contains(url, k8sAuthenticator.AuthnType) {
return &k8sAuthenticator.Config{}, nil
} else if strings.Contains(url, jwtAuthenticator.AuthnType) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of using an if/else block here, you could optionally use switch without a statement (i.e. evaluating expressions in the case statements). I think this makes it a little more obvious that there are 3 possible results for which we're looking. For example:

	switch {
	case strings.Contains(url, k8sAuthenticator.AuthnType):
		return &k8sAuthenticator.Config{}, nil
	case strings.Contains(url, jwtAuthenticator.AuthnType):
		return &jwtAuthenticator.Config{}, nil
	default:
		return nil, fmt.Errorf(log.CAKC063, url)
	}


func configureLogLevel(level string) {
validVal := "true"
if level == validVal {
Copy link
Contributor

@diverdane diverdane Dec 27, 2021

Choose a reason for hiding this comment

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

You could also use a switch statement here. I think this makes it a little more obvious that there are three possible outcomes (2 good, 1 bad):

	switch level {
	case validVal:
		log.EnableDebugMode()
	case "":
		// Log level not configured
		break
	default:
		// Log level is configured but it's invalid
		log.Warn(log.CAKC034, level, validVal)
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Moved it to ConfigFactory to be in one place

nessiLahav
nessiLahav previously approved these changes Dec 28, 2021
Copy link

@nessiLahav nessiLahav left a comment

Choose a reason for hiding this comment

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

LGTM

@tzheleznyak tzheleznyak dismissed diverdane’s stale review December 28, 2021 09:48

I fixed your comments so i will merge. If there anything else let me know and i will open separate PR :)

@tzheleznyak tzheleznyak merged commit 6e8549a into master Dec 28, 2021
@tzheleznyak tzheleznyak deleted the add-jwt-flow branch December 28, 2021 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

7 participants