You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "zeroshade (via GitHub)" <gi...@apache.org> on 2023/06/14 17:49:57 UTC

[GitHub] [arrow] zeroshade opened a new pull request, #36071: GH-36070: [Go][Flight] Add Flight Client Cookie Middleware

zeroshade opened a new pull request, #36071:
URL: https://github.com/apache/arrow/pull/36071

   <!--
   Thanks for opening a pull request!
   If this is your first pull request you can find detailed information on how 
   to contribute here:
     * [New Contributor's Guide](https://arrow.apache.org/docs/dev/developers/guide/step_by_step/pr_lifecycle.html#reviews-and-merge-of-the-pull-request)
     * [Contributing Overview](https://arrow.apache.org/docs/dev/developers/overview.html)
   
   
   If this is not a [minor PR](https://github.com/apache/arrow/blob/main/CONTRIBUTING.md#Minor-Fixes). Could you open an issue for this pull request on GitHub? https://github.com/apache/arrow/issues/new/choose
   
   Opening GitHub issues ahead of time contributes to the [Openness](http://theapacheway.com/open/#:~:text=Openness%20allows%20new%20users%20the,must%20happen%20in%20the%20open.) of the Apache Arrow project.
   
   Then could you also rename the pull request title in the following format?
   
       GH-${GITHUB_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}
   
   or
   
       MINOR: [${COMPONENT}] ${SUMMARY}
   
   In the case of PARQUET issues on JIRA the title also supports:
   
       PARQUET-${JIRA_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}
   
   -->
   
   ### Rationale for this change
   See https://github.com/apache/arrow-adbc/issues/716
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   ### What changes are included in this PR?
   `NewClientCookieMiddleware` function is added to the Flight package which returns a `ClientMiddleware` which can be used with flight and flightsql clients.
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   ### Are these changes tested?
   Yes.
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   ### Are there any user-facing changes?
   No.
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please uncomment the line below and explain which changes are breaking.
   -->
   <!-- **This PR includes breaking changes to public APIs.** -->
   
   <!--
   Please uncomment the line below (and provide explanation) if the changes fix either (a) a security vulnerability, (b) a bug that caused incorrect or invalid data to be produced, or (c) a bug that causes a crash (even when the API contract is upheld). We use this to highlight fixes to issues that may affect users without their knowledge. For this reason, fixing bugs that cause errors don't count, since those are usually obvious.
   -->
   <!-- **This PR contains a "Critical Fix".** -->


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow] zeroshade merged pull request #36071: GH-36070: [Go][Flight] Add Flight Client Cookie Middleware

Posted by "zeroshade (via GitHub)" <gi...@apache.org>.
zeroshade merged PR #36071:
URL: https://github.com/apache/arrow/pull/36071


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow] conbench-apache-arrow[bot] commented on pull request #36071: GH-36070: [Go][Flight] Add Flight Client Cookie Middleware

Posted by "conbench-apache-arrow[bot] (via GitHub)" <gi...@apache.org>.
conbench-apache-arrow[bot] commented on PR #36071:
URL: https://github.com/apache/arrow/pull/36071#issuecomment-1595485192

   Conbench analyzed the 6 benchmark runs on commit `b4ac585e`.
   
   There were 2 benchmark results indicating a performance regression:
   
   - Commit Run on `ursa-thinkcentre-m75q` at [2023-06-15 04:13:19Z](http://conbench.ursa.dev/compare/runs/49360409b392469988252a5c274bdc31...9786ece2d4bb418c9f5f90a6849d25c3/)
     - [params=1048576/100, source=cpp-micro, suite=arrow-acero-aggregate-benchmark](http://conbench.ursa.dev/compare/benchmarks/0648c3756fa67c40800056f536fddfa4...0648a901ed717f138000613a25cc97cb)
     - [params=1048576/10, source=cpp-micro, suite=arrow-acero-aggregate-benchmark](http://conbench.ursa.dev/compare/benchmarks/0648c3756e8771f88000e6fc4dd5c70a...0648a901ec5a74a2800065c5dba1b8f3)
   
   The [full Conbench report](https://github.com/apache/arrow/runs/14336235328) has more details.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow] lidavidm commented on a diff in pull request #36071: GH-36070: [Go][Flight] Add Flight Client Cookie Middleware

Posted by "lidavidm (via GitHub)" <gi...@apache.org>.
lidavidm commented on code in PR #36071:
URL: https://github.com/apache/arrow/pull/36071#discussion_r1230063450


##########
go/arrow/flight/cookie_middleware.go:
##########
@@ -0,0 +1,122 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package flight
+
+import (
+	"context"
+	"net/http"
+	"strings"
+	"sync"
+	"time"
+
+	"google.golang.org/grpc/metadata"
+)
+
+// endOfTime is the time when session (non-persistent) cookies expire.
+// This instant is representable in most date/time formats (not just
+// Go's time.Time) and should be far enough in the future.
+// taken from Go's net/http/cookiejar/jar.go
+var endOfTime = time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC)
+
+// NewClientCookieMiddleware returns a go-routine safe middleware for flight
+// clients which properly handles Set-Cookie headers to store cookies
+// in a cookie jar, and then requests are sent with those cookies added
+// as a Cookie header.
+func NewClientCookieMiddleware() ClientMiddleware {
+	return CreateClientMiddleware(&clientCookieMiddleware{jar: make(map[string]http.Cookie)})
+}
+
+type clientCookieMiddleware struct {
+	jar map[string]http.Cookie
+	mx  sync.Mutex
+}
+
+func (cc *clientCookieMiddleware) StartCall(ctx context.Context) context.Context {
+	cc.mx.Lock()
+	defer cc.mx.Unlock()
+
+	if len(cc.jar) == 0 {
+		return ctx
+	}
+
+	now := time.Now()
+
+	// Per RFC 6265 section 5.4, rather than adding multiple cookie strings
+	// or multiple cookie headers, multiple cookies are all sent as a single
+	// header value separated by semicolons.
+
+	// we will also clear any expired cookies from the jar while we determine
+	// the cookies to send.
+	cookies := make([]string, 0, len(cc.jar))
+	for id, c := range cc.jar {
+		if !c.Expires.After(now) {
+			delete(cc.jar, id)
+			continue
+		}
+
+		cookies = append(cookies, (&http.Cookie{Name: c.Name, Value: c.Value}).String())
+	}
+
+	if len(cookies) == 0 {
+		return ctx
+	}
+
+	return metadata.AppendToOutgoingContext(ctx, "Cookie", strings.Join(cookies, ";"))
+}
+
+func processCookieExpire(c *http.Cookie, now time.Time) (remove bool) {
+	// MaxAge takes precedence over Expires
+	if c.MaxAge < 0 {
+		return true
+	} else if c.MaxAge > 0 {
+		c.Expires = now.Add(time.Duration(c.MaxAge) * time.Second)
+	} else {
+		if c.Expires.IsZero() {
+			c.Expires = endOfTime
+		} else {
+			if !c.Expires.After(now) {
+				return true
+			}
+		}
+	}
+
+	return
+}
+
+func (cc *clientCookieMiddleware) HeadersReceived(ctx context.Context, md metadata.MD) {
+	// instead of replicating the logic for processing the Set-Cookie
+	// header, let's just make a fake response and use the built-in
+	// cookie processing. It's very non-trivial

Review Comment:
   hah, nice. Odd that it's not exposed separately, though



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org