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

SNOW-1868467: Add custom headers #2016

Open
wants to merge 24 commits into
base: master
Choose a base branch
from

Conversation

bpackter
Copy link

@bpackter bpackter commented Dec 25, 2024

Overview

SNOW-1868467

Pre-review self checklist

  • PR branch is updated with all the changes from master branch
  • The code is correctly formatted (run mvn -P check-style validate)
  • New public API is not unnecessary exposed (run mvn verify and inspect target/japicmp/japicmp.html)
  • The pull request name is prefixed with SNOW-XXXX:
  • Code is in compliance with internal logging requirements

External contributors - please answer these questions before submitting a pull request. Thanks!

  1. What GitHub issue is this PR addressing? Make sure that there is an accompanying issue to your PR.

    Issue: #2015

  2. Fill out the following pre-review checklist:

    • I am adding a new automated test(s) to verify correctness of my new code
    • I am adding new logging messages
    • I am modifying authorization mechanisms
    • I am adding new credentials
    • I am modifying OCSP code
    • I am adding a new dependency or upgrading an existing one
    • I am adding new public/protected component not marked with @SnowflakeJdbcInternalApi (note that public/protected methods/fields in classes marked with this annotation are already internal)
  3. Please describe how your code solves the related issue.

There is no options to add custom headers on each snowflake request (login, query, telemetry, heartbeat etc..)
This is needed in our case because we are calling snowflake from proxy and proxy enforce us to add jwt in some custom header.

This change
add an option to add map of custom headers in session coonig.
make sure headers are added on each request

@bpackter bpackter requested a review from a team as a code owner December 25, 2024 14:55
Copy link

github-actions bot commented Dec 25, 2024

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@sfc-gh-dprzybysz sfc-gh-dprzybysz changed the title Add custom headers SNOW-1868467: Add custom headers Jan 7, 2025
Copy link
Collaborator

@sfc-gh-dprzybysz sfc-gh-dprzybysz left a comment

Choose a reason for hiding this comment

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

Hi, I left some comments.
Could you also provide some tests for the provided features?

@@ -1,1507 +1,1507 @@
<?xml version="1.0" encoding="UTF-8"?>
Copy link
Collaborator

Choose a reason for hiding this comment

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

there should not be so many changes in pom.xml files - probably it's an unwanted change

Copy link
Author

Choose a reason for hiding this comment

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

only whitespace, I'll reformat

@@ -63,6 +63,7 @@ public enum SFSessionProperty {
@Deprecated
PRIVATE_KEY_FILE_PWD("private_key_file_pwd", false, String.class),
PRIVATE_KEY_PWD("private_key_pwd", false, String.class),
AdditionalHttpHeaders("additional_http_headers", false, String.class),
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should follow the enum naming convention

Choose a reason for hiding this comment

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

fixed

if (headers != null && !headers.isEmpty()) {
Map<String, String> headersMap = new HashMap<>();
for (String headerKeyPair : headers.split(";")) {
String[] split = headerKeyPair.split(":");
Copy link
Collaborator

Choose a reason for hiding this comment

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

what if the value contains ':' or ';' internally? than the simple split could be not sufficient

Copy link
Author

Choose a reason for hiding this comment

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

yes, we need to agree on some separator, since this is map of string string.
what do you suggest?

Copy link
Collaborator

Choose a reason for hiding this comment

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

the ';' and ':' may be easy to read but e.g. ';' is used in some headers as a separator inside the fields

maybe we can think about url encoded parameters with '&' and = as separators - then we could expect that all property values are encoded separately and also the whole property value is encoded e.g.

static Map<String, String> getHttpHeaders(String headers) {
        if (headers != null && !headers.isEmpty()) {
            Map<String, String> headersMap = new HashMap<>();
            for (String headerKeyPair : java.net.URLDecoder.decode(headers).split("&")) {
                String[] split = headerKeyPair.split("=");
                if (split.length >= 2) {
                    headersMap.put(split[0], java.net.URLDecoder.decode(split[1]));
                }
            }
            return headersMap;
        }
        return null;
    }

    public static void main(String[] args){
        String headers = java.net.URLEncoder.encode("x="+java.net.URLEncoder.encode("test=&123")+"&y="+java.net.URLEncoder.encode("t&12=3"));

        System.out.println(getHttpHeaders(headers));
  }

@sfc-gh-dprzybysz
Copy link
Collaborator

Please note that signing the CLA is also necessary for your PR to be merged

@bpackter
Copy link
Author

bpackter commented Jan 7, 2025

I have read the CLA Document and I hereby sign the CLA

@@ -812,6 +814,29 @@ public synchronized void open() throws SFException, SnowflakeSQLException {
logger.debug("Session {} opened in {} ms.", getSessionId(), stopwatch.elapsedMillis());
}

public Map<String, String> getHttpHeaders() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

if newly added methods need to be public please mark them with @SnowflakeJdbcInternalApi

@brachi-wernick
Copy link

I have read the CLA Document and I hereby sign the CLA

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