Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Create a real Okta session & store it in the cache, refresh it on each invocation #102

Merged
merged 13 commits into from
May 17, 2021

Conversation

mipearson
Copy link
Contributor

@mipearson mipearson commented Apr 26, 2021

@rbvigilante rbvigilante force-pushed the mp-use-okta-sessions branch 3 times, most recently from f96f6d4 to 5cd3bc6 Compare May 4, 2021 12:59
cache/cache.go Outdated Show resolved Hide resolved
go.sum Outdated Show resolved Hide resolved
@mipearson mipearson changed the title Scrappy WIP on prototyping session support Create a real Okta session & store it in the cache, refresh it on each invocation May 7, 2021
@mipearson mipearson marked this pull request as ready for review May 7, 2021 06:25
cli/login.go Outdated

if err == nil {
session.ExpiresAt = response.ExpiresAt
cache.Write(oktaSessionCacheKey(), *session, session.ExpiresAt.Sub(time.Now()))
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to cache this for the maximum allowable time? This means it will sit around for something like a week before asking for reauthentication. If we do, perhaps it would be good to be able to configure the session duration separately?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interestingly, I've been getting single day sessions, not entire week ones.

Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting. When I dumped sessions, the expiry date was always in something like a week's time. That's actually why I used WriteDefault initially. I guess there's probably not a lot we can do to control this with Okta, so maybe something in config for, like, a maximum allowable session cache time might be nice? I don't know.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a flag to limit how long we're willing to keep an Okta session in the cache for without refresh, default to 1 day

@@ -293,7 +331,7 @@ func promptLogin() (okta.OktaAuthResponse, error) {

for unauthorised && (retries < maxLoginRetries) {
retries++
username := viper.GetString("okta.username")
username := oktaUsername()
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm totally fine with this approach, but it is different to what we're doing everywhere else in the codebase; I'd generally err on the side of consistency, but I'm also very happy if we think this is a better way to handle viper to just slowly convert stuff over as we go.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oo, probably didn't need this explicit assignment here - was search-replacing.

But yeah - I'm 50/50 on it - I don't like repeated get("string") things because there's nothing there to stop a misspelled word.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I get it. Like I think I've said before, I think there are probably some things we can do to handle the vipers a bit better; we were quite naïve about this whole thing when we first wrote this code.

Copy link
Contributor

Choose a reason for hiding this comment

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

Alright, let's leave it like this for now; we can come back and fix the snakes another time.

return &session, nil
}

// TODO: DRY
Copy link
Contributor

Choose a reason for hiding this comment

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

Has this been left here intentionally, or should we do it as part of this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Intentionally - the way that this code does its HTTP handling needs some thought.

cmd/root.go Outdated

rootCmd.PersistentFlags().StringP("okta-username", "u", "", "Your Okta username")
rootCmd.PersistentFlags().String("okta-domain", "", "The domain to use for requests to Okta")
rootCmd.PersistentFlags().String("okta-aws-saml-endpoint", "", "The app embed path for the AWS app within Okta")
rootCmd.PersistentFlags().String("okta-mfa-type", "", "The Okta MFA type for login")
rootCmd.PersistentFlags().String("okta-mfa-provider", "", "The Okta MFA provider name for login")
rootCmd.PersistentFlags().Int64("okta-session-cache-limit", 86400, "The maximum amount of time we'll cache a session from Okta (in seconds)")
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this would be better as a config file only option; it doesn't really make sense to pass it as a command-line argument?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Eh, no strong opinion either way. I like the pragma of being able to specify things on the CLI when it's easy to do so, especially for testing.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't actually mind much either way, but again I'd prefer consistency - if we want to allow this to be passed as a flag, we should also allow passing the other cache timeouts this way. Perhaps we could consider this another piece of work?

Copy link
Contributor

@rbvigilante rbvigilante left a comment

Choose a reason for hiding this comment

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

OK, I think this is good to go!

@@ -293,7 +331,7 @@ func promptLogin() (okta.OktaAuthResponse, error) {

for unauthorised && (retries < maxLoginRetries) {
retries++
username := viper.GetString("okta.username")
username := oktaUsername()
Copy link
Contributor

Choose a reason for hiding this comment

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

Alright, let's leave it like this for now; we can come back and fix the snakes another time.

@mipearson mipearson merged commit db1ac13 into master May 17, 2021
@lokulin lokulin deleted the mp-use-okta-sessions branch August 17, 2022 10:24
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.

2 participants