24
24
import com .google .common .collect .ImmutableMap ;
25
25
import com .snowflake .kafka .connector .internal .BufferThreshold ;
26
26
import com .snowflake .kafka .connector .internal .KCLogger ;
27
+ import com .snowflake .kafka .connector .internal .OAuthConstants ;
27
28
import com .snowflake .kafka .connector .internal .SnowflakeErrors ;
28
29
import com .snowflake .kafka .connector .internal .SnowflakeURL ;
29
30
import com .snowflake .kafka .connector .internal .streaming .IngestionMethodConfig ;
@@ -81,7 +82,8 @@ public class Utils {
81
82
public static final String SF_SSL = "sfssl" ; // for test only
82
83
public static final String SF_WAREHOUSE = "sfwarehouse" ; // for test only
83
84
public static final String PRIVATE_KEY_PASSPHRASE = "snowflake.private.key" + ".passphrase" ;
84
- public static final String SF_AUTHENTICATOR = "snowflake.authenticator" ;
85
+ public static final String SF_AUTHENTICATOR =
86
+ "snowflake.authenticator" ; // TODO: SNOW-889748 change to enum
85
87
public static final String SF_OAUTH_CLIENT_ID = "snowflake.oauth.client.id" ;
86
88
public static final String SF_OAUTH_CLIENT_SECRET = "snowflake.oauth.client.secret" ;
87
89
public static final String SF_OAUTH_REFRESH_TOKEN = "snowflake.oauth.refresh.token" ;
@@ -128,19 +130,6 @@ public class Utils {
128
130
public static final String GET_EXCEPTION_MISSING_MESSAGE = "missing exception message" ;
129
131
public static final String GET_EXCEPTION_MISSING_CAUSE = "missing exception cause" ;
130
132
131
- // OAuth
132
- public static final String TOKEN_REQUEST_ENDPOINT = "/oauth/token-request" ;
133
- public static final String OAUTH_CONTENT_TYPE_HEADER = "application/x-www-form-urlencoded" ;
134
- public static final String BASIC_AUTH_HEADER_PREFIX = "Basic " ;
135
- public static final String GRANT_TYPE_PARAM = "grant_type" ;
136
- public static final String REFRESH_TOKEN = "refresh_token" ;
137
- public static final String ACCESS_TOKEN = "access_token" ;
138
- public static final String SNOWFLAKE_JWT = "snowflake_jwt" ;
139
- public static final String OAUTH = "oauth" ;
140
- public static final String REDIRECT_URI = "redirect_uri" ;
141
- public static final String DEFAULT_REDIRECT_URI = "https://localhost.com/oauth" ;
142
- public static final int OAUTH_MAX_RETRY = 5 ;
143
-
144
133
private static final KCLogger LOGGER = new KCLogger (Utils .class .getName ());
145
134
146
135
/**
@@ -476,41 +465,43 @@ && parseTopicToTableMap(config.get(SnowflakeSinkConnectorConfig.TOPICS_TABLES_MA
476
465
Utils .formatString ("{} cannot be empty." , SnowflakeSinkConnectorConfig .SNOWFLAKE_SCHEMA ));
477
466
}
478
467
479
- switch (config .getOrDefault (SnowflakeSinkConnectorConfig .AUTHENTICATOR_TYPE , SNOWFLAKE_JWT )) {
480
- case SNOWFLAKE_JWT :
468
+ switch (config .getOrDefault (
469
+ SnowflakeSinkConnectorConfig .AUTHENTICATOR_TYPE , OAuthConstants .SNOWFLAKE_JWT )) {
470
+ // TODO: SNOW-889748 change to enum
471
+ case OAuthConstants .SNOWFLAKE_JWT :
481
472
if (!config .containsKey (SnowflakeSinkConnectorConfig .SNOWFLAKE_PRIVATE_KEY )) {
482
473
invalidConfigParams .put (
483
474
SnowflakeSinkConnectorConfig .SNOWFLAKE_PRIVATE_KEY ,
484
475
Utils .formatString (
485
476
"{} cannot be empty when using {} authenticator." ,
486
477
SnowflakeSinkConnectorConfig .SNOWFLAKE_PRIVATE_KEY ,
487
- SNOWFLAKE_JWT ));
478
+ OAuthConstants . SNOWFLAKE_JWT ));
488
479
}
489
480
break ;
490
- case OAUTH :
481
+ case OAuthConstants . OAUTH :
491
482
if (!config .containsKey (SnowflakeSinkConnectorConfig .OAUTH_CLIENT_ID )) {
492
483
invalidConfigParams .put (
493
484
SnowflakeSinkConnectorConfig .OAUTH_CLIENT_ID ,
494
485
Utils .formatString (
495
486
"{} cannot be empty when using {} authenticator." ,
496
487
SnowflakeSinkConnectorConfig .OAUTH_CLIENT_ID ,
497
- OAUTH ));
488
+ OAuthConstants . OAUTH ));
498
489
}
499
490
if (!config .containsKey (SnowflakeSinkConnectorConfig .OAUTH_CLIENT_SECRET )) {
500
491
invalidConfigParams .put (
501
492
SnowflakeSinkConnectorConfig .OAUTH_CLIENT_SECRET ,
502
493
Utils .formatString (
503
494
"{} cannot be empty when using {} authenticator." ,
504
495
SnowflakeSinkConnectorConfig .OAUTH_CLIENT_SECRET ,
505
- OAUTH ));
496
+ OAuthConstants . OAUTH ));
506
497
}
507
498
if (!config .containsKey (SnowflakeSinkConnectorConfig .OAUTH_REFRESH_TOKEN )) {
508
499
invalidConfigParams .put (
509
500
SnowflakeSinkConnectorConfig .OAUTH_REFRESH_TOKEN ,
510
501
Utils .formatString (
511
502
"{} cannot be empty when using {} authenticator." ,
512
503
SnowflakeSinkConnectorConfig .OAUTH_REFRESH_TOKEN ,
513
- OAUTH ));
504
+ OAuthConstants . OAUTH ));
514
505
}
515
506
break ;
516
507
default :
@@ -519,8 +510,8 @@ && parseTopicToTableMap(config.get(SnowflakeSinkConnectorConfig.TOPICS_TABLES_MA
519
510
Utils .formatString (
520
511
"{} should be one of {} or {}." ,
521
512
SnowflakeSinkConnectorConfig .AUTHENTICATOR_TYPE ,
522
- SNOWFLAKE_JWT ,
523
- OAUTH ));
513
+ OAuthConstants . SNOWFLAKE_JWT ,
514
+ OAuthConstants . OAUTH ));
524
515
}
525
516
526
517
if (!config .containsKey (SnowflakeSinkConnectorConfig .SNOWFLAKE_USER )) {
@@ -792,7 +783,13 @@ public static String formatString(String format, Object... vars) {
792
783
public static String getSnowflakeOAuthAccessToken (
793
784
SnowflakeURL url , String clientId , String clientSecret , String refreshToken ) {
794
785
return getSnowflakeOAuthToken (
795
- url , clientId , clientSecret , refreshToken , REFRESH_TOKEN , REFRESH_TOKEN , ACCESS_TOKEN );
786
+ url ,
787
+ clientId ,
788
+ clientSecret ,
789
+ refreshToken ,
790
+ OAuthConstants .REFRESH_TOKEN ,
791
+ OAuthConstants .REFRESH_TOKEN ,
792
+ OAuthConstants .ACCESS_TOKEN );
796
793
}
797
794
798
795
/**
@@ -818,16 +815,16 @@ public static String getSnowflakeOAuthToken(
818
815
String credentialType ,
819
816
String tokenType ) {
820
817
Map <String , String > headers = new HashMap <>();
821
- headers .put (HttpHeaders .CONTENT_TYPE , OAUTH_CONTENT_TYPE_HEADER );
818
+ headers .put (HttpHeaders .CONTENT_TYPE , OAuthConstants . OAUTH_CONTENT_TYPE_HEADER );
822
819
headers .put (
823
820
HttpHeaders .AUTHORIZATION ,
824
- BASIC_AUTH_HEADER_PREFIX
821
+ OAuthConstants . BASIC_AUTH_HEADER_PREFIX
825
822
+ Base64 .getEncoder ().encodeToString ((clientId + ":" + clientSecret ).getBytes ()));
826
823
827
824
Map <String , String > payload = new HashMap <>();
828
- payload .put (GRANT_TYPE_PARAM , grantType );
825
+ payload .put (OAuthConstants . GRANT_TYPE_PARAM , grantType );
829
826
payload .put (credentialType , credential );
830
- payload .put (REDIRECT_URI , DEFAULT_REDIRECT_URI );
827
+ payload .put (OAuthConstants . REDIRECT_URI , OAuthConstants . DEFAULT_REDIRECT_URI );
831
828
832
829
// Encode and convert payload into string entity
833
830
String payloadString =
@@ -844,11 +841,11 @@ public static String getSnowflakeOAuthToken(
844
841
final StringEntity entity =
845
842
new StringEntity (payloadString , ContentType .APPLICATION_FORM_URLENCODED );
846
843
847
- HttpPost post = makeOAuthHttpPost (url , TOKEN_REQUEST_ENDPOINT , headers , entity );
844
+ HttpPost post = makeOAuthHttpPost (url , OAuthConstants . TOKEN_REQUEST_ENDPOINT , headers , entity );
848
845
849
846
// Request access token
850
847
CloseableHttpClient client = HttpClientBuilder .create ().build ();
851
- for (int retries = 0 ; retries < OAUTH_MAX_RETRY ; retries ++) {
848
+ for (int retries = 0 ; retries < OAuthConstants . OAUTH_MAX_RETRY ; retries ++) {
852
849
try (CloseableHttpResponse httpResponse = client .execute (post )) {
853
850
String respBodyString = EntityUtils .toString (httpResponse .getEntity ());
854
851
0 commit comments