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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 to a cookie-jar file in the Netscape cookie format for auth with Admin Server.\n"+
"You may also need to pass in as header the User-Agent that was used to create the cookie-jar.")
viper.BindPFlag("kong-cookie-jar-path",
rootCmd.PersistentFlags().Lookup("kong-cookie-jar-path"))

// konnect-specific flags
rootCmd.PersistentFlags().String("konnect-email", "",
"Email address associated with your Konnect account.")
Expand Down Expand Up @@ -207,6 +214,9 @@ func initConfig() {
rootConfig.Debug = (viper.GetInt("verbose") >= 1)
rootConfig.Timeout = (viper.GetInt("timeout"))

// cookie-jar support
rootConfig.CookieJarPath = viper.GetString("kong-cookie-jar-path")

color.NoColor = (color.NoColor || viper.GetBool("no-color"))

if err := initKonnectConfig(); err != nil {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.10.0
github.com/ssgelm/cookiejarparser v1.0.1
github.com/stretchr/testify v1.7.0
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonschema v1.2.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/spf13/viper v1.8.1/go.mod h1:o0Pch8wJ9BVSWGQMbra6iw0oQ5oktSIBaujf1rJH9Ns=
github.com/spf13/viper v1.10.0 h1:mXH0UwHS4D2HwWZa75im4xIQynLfblmWV7qcWpfv0yk=
github.com/spf13/viper v1.10.0/go.mod h1:SoyBPwAtKDzypXNDFKN5kzH7ppppbGZtls1UpIy5AsM=
github.com/ssgelm/cookiejarparser v1.0.1 h1:cRdXauUbOTFzTPJFaeiWbHnQ+tRGlpKKzvIK9PUekE4=
github.com/ssgelm/cookiejarparser v1.0.1/go.mod h1:DUfC0mpjIzlDN7DzKjXpHj0qMI5m9VrZuz3wSlI+OEI=
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -561,6 +563,7 @@ golang.org/x/net v0.0.0-20190628185345-da137c7871d7/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191027093000-83d349e8ac1a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down
11 changes: 11 additions & 0 deletions utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/kong/deck/konnect"
"github.com/kong/go-kong/kong"
"github.com/kong/go-kong/kong/custom"
"github.com/ssgelm/cookiejarparser"
)

var clientTimeout time.Duration
Expand Down Expand Up @@ -94,6 +95,8 @@ type KongClientConfig struct {
HTTPClient *http.Client

Timeout int

CookieJarPath string
}

type KonnectConfig struct {
Expand Down Expand Up @@ -153,6 +156,14 @@ func GetKongClient(opt KongClientConfig) (*kong.Client, error) {
if opt.Workspace != "" {
url.Path = path.Join(url.Path, opt.Workspace)
}
// Add Session Cookie support if required
if opt.CookieJarPath != "" {
jar, err := cookiejarparser.LoadCookieJarFile(opt.CookieJarPath)
if err != nil {
return nil, fmt.Errorf("failed to initialize cookie-jar: %w", err)
}
c.Jar = jar
}

kongClient, err := kong.NewClient(kong.String(url.String()), c)
if err != nil {
Expand Down