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

core: kgw cookie on multi providers #691

Merged
merged 2 commits into from
May 9, 2024

Conversation

Yaiba
Copy link
Contributor

@Yaiba Yaiba commented May 1, 2024

This pr change the saved cookie key for kgw client, to support multi providers.

The behavior before is shown here

This pr is supposed to backported to v0.7 and v0.6.

I'll come back to this pr once #689 is merged

@Yaiba Yaiba force-pushed the feature/kgw-client-multi-provider branch from 11d0430 to a9a902c Compare May 1, 2024 21:22
@Yaiba Yaiba changed the title core: kgw cookie on multi providers [WIP] core: kgw cookie on multi providers May 1, 2024
@Yaiba
Copy link
Contributor Author

Yaiba commented May 1, 2024

@KwilLuke I couldn't find how kwil-js handle this in nodejs, can check that?

@jchappelow jchappelow added this to the v0.8.0 milestone May 2, 2024
@Yaiba Yaiba force-pushed the feature/kgw-client-multi-provider branch from a9a902c to 8110b3f Compare May 8, 2024 19:23
@Yaiba Yaiba changed the title [WIP] core: kgw cookie on multi providers core: kgw cookie on multi providers May 8, 2024
@Yaiba Yaiba requested a review from jchappelow May 8, 2024 19:56
@Yaiba Yaiba marked this pull request as ready for review May 8, 2024 19:56
@Yaiba Yaiba force-pushed the feature/kgw-client-multi-provider branch from 8110b3f to 8aa3934 Compare May 8, 2024 20:03
@@ -98,14 +98,19 @@ func DialClient(ctx context.Context, cmd *cobra.Command, flags uint8, fn RoundTr
return fn(ctx, client, conf)
}

cookie, err := LoadPersistedCookie(KGWAuthTokenFilePath(), clientConfig.Signer.Identity())
providerDomain, err := getDomain(conf.Provider)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When backport, conf.Provider should be conf.GrpcUrl

Copy link
Member

@jchappelow jchappelow left a comment

Choose a reason for hiding this comment

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

All looks, fine, but I'm not certain why we have to use this computed domain (actually scheme://domain[:port]) when doing SaveCookie. Does http.Cookie.Domain not persist in the cookie jar? Or would that not be the exactly right key, missing scheme and probably port?

return "", fmt.Errorf("target is empty")
}

parsedTarget, err := url.Parse(target)
Copy link
Member

Choose a reason for hiding this comment

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

One quirk of url.Parse is how it handles a string like localhost:8080. It does not error. It returns a URL with no host and "Opaque":

&url.URL{Scheme:"localhost", Opaque:"8080", User:(*url.Userinfo)(nil), Host:"", Path:"", RawPath:"", OmitHost:false, ForceQuery:false, RawQuery:"", Fragment:"", RawFragment:""}

We'll probably be rejecting or erroring on such a target before we reach getDomain, but it's still probably an unintended result for getDomain:

  • input localhost:8080
  • output localhost://

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah I got screwed every time I use url.Parse 🤦‍♂️

@Yaiba
Copy link
Contributor Author

Yaiba commented May 9, 2024

All looks, fine, but I'm not certain why we have to use this computed domain (actually scheme://domain[:port]) when doing SaveCookie. Does http.Cookie.Domain not persist in the cookie jar? Or would that not be the exactly right key, missing scheme and probably port?

http.Cookie.Domain will be empty if https + cookie.SameSite=None, see https://github.com/kwilteam/kgw/blob/53159c27d08286bb8bd463cc64c5cbb3abdbfe9a/cookie.go#L82. So we cannot rely on it

@@ -84,6 +85,10 @@ func getDomain(target string) (string, error) {
return "", fmt.Errorf("target is empty")
}

if !(strings.HasPrefix(target, "http://") || strings.HasPrefix(target, "https://")) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For 0.7 or v0.6, this should work, since we require scheme https://docs.kwil.com/docs/kwil-cli/configuration#available-configs.

For v0.8, seems this could break?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

With latest update to that PR, cfg.Provider must be a proper URL now. Another reason to keep that convention I figure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The persistened cfg.Provider could still be without scheme, am I right?

Copy link
Member

Choose a reason for hiding this comment

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

Not any more. It's an error:

$  ./kwil-cli database list
Error: parse url: parse "127.0.0.1:8484": first path segment in URL cannot contain colon
parse url: parse "127.0.0.1:8484": first path segment in URL cannot contain colon
$  cat ~/.kwil-cli/config.json 
{"provider":"127.0.0.1:8484"}

I think we had that same constraint in v0.7?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have some unpleasant constraint, like this :

.build/kwil-cli database list --kwil-provider localhost:8080                                                   
Error: wrap client: chain_info: Get "localhost:8080/api/v1/chain_info": unsupported protocol scheme "localhost"
wrap client: chain_info: Get "localhost:8080/api/v1/chain_info": unsupported protocol scheme "localhost"

It'll get error when create client, that's before getDomain, so I guess we're fine

@Yaiba Yaiba added backport-to-release-v0.7 backport to release-v0.7 branch backport-to-release-v0.6 backport to release-v0.6 branch labels May 9, 2024
@Yaiba Yaiba merged commit 5a4e960 into kwilteam:main May 9, 2024
1 check passed
@Yaiba
Copy link
Contributor Author

Yaiba commented May 9, 2024

eh, backport workflow didn't run

Yaiba added a commit to Yaiba/kwil-db that referenced this pull request May 10, 2024
* core: kgw cookie on multi providers
Yaiba added a commit to Yaiba/kwil-db that referenced this pull request May 10, 2024
* core: kgw cookie on multi providers
Yaiba added a commit that referenced this pull request May 10, 2024
* core: kgw cookie on multi providers (#691)
* fix: use conf.GrpcURL
Yaiba added a commit that referenced this pull request May 10, 2024
* core: kgw cookie on multi providers (#691)
* fix: use conf.GrpcURL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-to-release-v0.6 backport to release-v0.6 branch backport-to-release-v0.7 backport to release-v0.7 branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants