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

feat(Session cookie support for Auth to Admin Server) #545

Merged
merged 12 commits into from
Dec 14, 2021

Conversation

rajkong
Copy link
Contributor

@rajkong rajkong commented Dec 10, 2021

Adding support for session cookie file in Netscape cookie file format to use in cookie jar. Depending on the User Agent that was used to generate the cookie file, the User-Agent header in deck may need to be over-ridden

@rajkong rajkong requested a review from a team as a code owner December 10, 2021 18:27
@rajkong rajkong changed the title Session cookie support for Auth to Admin Server feat(Session cookie support for Auth to Admin Server) Dec 10, 2021
@@ -0,0 +1,114 @@
package utils

// Code from https://github.com/ssgelm/cookiejarparser under MIT License
Copy link
Member

Choose a reason for hiding this comment

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

Any reason we are copying this code rather than using it as a dependency?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seemed very small and not sure if it would be maintained. So felt it is better to copy instead of import

Copy link
Member

Choose a reason for hiding this comment

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

If we are not modifying any part of the dependency code, let's introduce the dependency.
Copying code in here is a new pattern and I'd recommend not deviating from current practices (other and future maintainers will wonder why this exception).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

utils/types.go Outdated
@@ -94,6 +94,11 @@ type KongClientConfig struct {
HTTPClient *http.Client

Timeout int

SessionFilePath string
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
SessionFilePath string
CookieJarPath string

Cookies can do more than sessions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

.gitignore Outdated Show resolved Hide resolved
cmd/root.go Outdated
@@ -123,6 +123,13 @@ func init() {
viper.BindPFlag("skip-workspace-crud",
rootCmd.PersistentFlags().Lookup("skip-workspace-crud"))

// Support for Session Cookie
rootCmd.PersistentFlags().String("kong-session-cookie-path", "",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
rootCmd.PersistentFlags().String("kong-session-cookie-path", "",
rootCmd.PersistentFlags().String("kong-cookie-jar-path", "",

Let's avoid the word 'session' in here since the functionality is more generic than that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

cmd/root.go Outdated
// Support for Session Cookie
rootCmd.PersistentFlags().String("kong-session-cookie-path", "",
"Absolute path for the session-cookie file for auth with Admin Server.\n"+
"You may also need to pass in as header the User-Agent that was used to create the session.")
Copy link
Member

Choose a reason for hiding this comment

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

Update this text as well.
Please mention the format of the cookie file path (netscape cookie format I think).

Question: what happens if the user-agent is not specified?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It gives a 401. This took me a while to figure out. Looks like Kong associates sessions and user-agent. My guess is that when Kong sees a different user agent, it assumes that the session was hijacked and sends a 401

Copy link
Member

Choose a reason for hiding this comment

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

Yikes. That may introduce additional friction that we can't solve for. Let's ship this and come back here if this PR doesn't achieve the desired outcome for our customers.

utils/types.go Outdated
SessionFilePath string

// Whether to initialize the Http client with a cookie jar or not
ISSessionClient bool
Copy link
Member

Choose a reason for hiding this comment

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

Is this required? Why not check if the SessionFilePath != ""?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I could do that, felt that the boolean could satisfy multiple flags, so left it. I can remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@codecov-commenter
Copy link

codecov-commenter commented Dec 10, 2021

Codecov Report

Merging #545 (4b4bc86) into main (f834d26) will decrease coverage by 0.01%.
The diff coverage is 43.75%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #545      +/-   ##
==========================================
- Coverage   51.98%   51.96%   -0.02%     
==========================================
  Files          73       73              
  Lines        7714     7730      +16     
==========================================
+ Hits         4010     4017       +7     
- Misses       3354     3363       +9     
  Partials      350      350              
Impacted Files Coverage Δ
utils/types.go 23.80% <0.00%> (-1.45%) ⬇️
cmd/root.go 55.78% <70.00%> (+0.78%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f834d26...4b4bc86. Read the comment docs.

cmd/root.go Outdated
@@ -123,6 +123,13 @@ func init() {
viper.BindPFlag("skip-workspace-crud",
rootCmd.PersistentFlags().Lookup("skip-workspace-crud"))

// Support for Session Cookie
rootCmd.PersistentFlags().String("kong-cookie-jar-path", "",
"Absolute path for cookie-jar file in Netscape file format for auth with Admin Server.\n"+
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"Absolute path for cookie-jar file in Netscape file format for auth with Admin Server.\n"+
"Absolute path to a cookie-jar file in the Netscape cookie format for auth with Admin Server.\n"+

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

utils/types.go Outdated
if opt.CookieJarPath != "" {
jar, err := LoadCookieJarFile(opt.CookieJarPath)
if err != nil {
return nil, fmt.Errorf("failed to initialize cookie-jar:%w", err)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return nil, fmt.Errorf("failed to initialize cookie-jar:%w", err)
return nil, fmt.Errorf("failed to initialize cookie-jar: %w", err)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

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.

3 participants