4
4
5
5
package io .airbyte .server .handlers ;
6
6
7
- import com .fasterxml .jackson .databind .JsonNode ;
8
- import com .google .common .annotations .VisibleForTesting ;
9
7
import io .airbyte .analytics .TrackingClient ;
10
8
import io .airbyte .api .model .generated .CompleteDestinationOAuthRequest ;
11
9
import io .airbyte .api .model .generated .CompleteSourceOauthRequest ;
14
12
import io .airbyte .api .model .generated .SetInstancewideDestinationOauthParamsRequestBody ;
15
13
import io .airbyte .api .model .generated .SetInstancewideSourceOauthParamsRequestBody ;
16
14
import io .airbyte .api .model .generated .SourceOauthConsentRequest ;
17
- import io .airbyte .commons .constants .AirbyteSecretConstants ;
18
- import io .airbyte .commons .json .JsonPaths ;
19
15
import io .airbyte .commons .json .Jsons ;
20
- import io .airbyte .config .DestinationConnection ;
21
16
import io .airbyte .config .DestinationOAuthParameter ;
22
- import io .airbyte .config .SourceConnection ;
23
17
import io .airbyte .config .SourceOAuthParameter ;
24
18
import io .airbyte .config .StandardDestinationDefinition ;
25
19
import io .airbyte .config .StandardSourceDefinition ;
26
20
import io .airbyte .config .persistence .ConfigNotFoundException ;
27
21
import io .airbyte .config .persistence .ConfigRepository ;
28
- import io .airbyte .config .persistence .SecretsRepositoryReader ;
29
22
import io .airbyte .oauth .OAuthFlowImplementation ;
30
23
import io .airbyte .oauth .OAuthImplementationFactory ;
31
24
import io .airbyte .persistence .job .factory .OAuthConfigSupplier ;
32
25
import io .airbyte .persistence .job .tracker .TrackingMetadata ;
33
26
import io .airbyte .protocol .models .ConnectorSpecification ;
34
- import io .airbyte .server .handlers .helpers .OAuthPathExtractor ;
35
27
import io .airbyte .validation .json .JsonValidationException ;
36
28
import java .io .IOException ;
37
29
import java .net .http .HttpClient ;
38
- import java .util .HashMap ;
39
- import java .util .List ;
40
30
import java .util .Map ;
41
31
import java .util .UUID ;
42
- import java .util .stream .Collectors ;
43
32
import org .slf4j .Logger ;
44
33
import org .slf4j .LoggerFactory ;
45
34
@@ -51,97 +40,67 @@ public class OAuthHandler {
51
40
private final ConfigRepository configRepository ;
52
41
private final OAuthImplementationFactory oAuthImplementationFactory ;
53
42
private final TrackingClient trackingClient ;
54
- private final SecretsRepositoryReader secretsRepositoryReader ;
55
43
56
44
public OAuthHandler (final ConfigRepository configRepository ,
57
45
final HttpClient httpClient ,
58
- final TrackingClient trackingClient ,
59
- final SecretsRepositoryReader secretsRepositoryReader ) {
46
+ final TrackingClient trackingClient ) {
60
47
this .configRepository = configRepository ;
61
48
this .oAuthImplementationFactory = new OAuthImplementationFactory (configRepository , httpClient );
62
49
this .trackingClient = trackingClient ;
63
- this .secretsRepositoryReader = secretsRepositoryReader ;
64
50
}
65
51
66
- public OAuthConsentRead getSourceOAuthConsent (final SourceOauthConsentRequest sourceOauthConsentRequest )
52
+ public OAuthConsentRead getSourceOAuthConsent (final SourceOauthConsentRequest sourceDefinitionIdRequestBody )
67
53
throws JsonValidationException , ConfigNotFoundException , IOException {
68
54
final StandardSourceDefinition sourceDefinition =
69
- configRepository .getStandardSourceDefinition (sourceOauthConsentRequest .getSourceDefinitionId ());
55
+ configRepository .getStandardSourceDefinition (sourceDefinitionIdRequestBody .getSourceDefinitionId ());
70
56
final OAuthFlowImplementation oAuthFlowImplementation = oAuthImplementationFactory .create (sourceDefinition );
71
57
final ConnectorSpecification spec = sourceDefinition .getSpec ();
72
- final Map <String , Object > metadata = generateSourceMetadata (sourceOauthConsentRequest .getSourceDefinitionId ());
58
+ final Map <String , Object > metadata = generateSourceMetadata (sourceDefinitionIdRequestBody .getSourceDefinitionId ());
73
59
final OAuthConsentRead result ;
74
60
if (OAuthConfigSupplier .hasOAuthConfigSpecification (spec )) {
75
- final JsonNode oAuthInputConfigurationForConsent ;
76
-
77
- if (sourceOauthConsentRequest .getSourceId () == null ) {
78
- oAuthInputConfigurationForConsent = sourceOauthConsentRequest .getoAuthInputConfiguration ();
79
- } else {
80
- final SourceConnection hydratedSourceConnection =
81
- secretsRepositoryReader .getSourceConnectionWithSecrets (sourceOauthConsentRequest .getSourceId ());
82
-
83
- oAuthInputConfigurationForConsent = getOAuthInputConfigurationForConsent (spec ,
84
- hydratedSourceConnection .getConfiguration (),
85
- sourceOauthConsentRequest .getoAuthInputConfiguration ());
86
- }
87
-
88
61
result = new OAuthConsentRead ().consentUrl (oAuthFlowImplementation .getSourceConsentUrl (
89
- sourceOauthConsentRequest .getWorkspaceId (),
90
- sourceOauthConsentRequest .getSourceDefinitionId (),
91
- sourceOauthConsentRequest .getRedirectUrl (),
92
- oAuthInputConfigurationForConsent ,
62
+ sourceDefinitionIdRequestBody .getWorkspaceId (),
63
+ sourceDefinitionIdRequestBody .getSourceDefinitionId (),
64
+ sourceDefinitionIdRequestBody .getRedirectUrl (),
65
+ sourceDefinitionIdRequestBody . getoAuthInputConfiguration () ,
93
66
spec .getAdvancedAuth ().getOauthConfigSpecification ()));
94
67
} else {
95
68
result = new OAuthConsentRead ().consentUrl (oAuthFlowImplementation .getSourceConsentUrl (
96
- sourceOauthConsentRequest .getWorkspaceId (),
97
- sourceOauthConsentRequest .getSourceDefinitionId (),
98
- sourceOauthConsentRequest .getRedirectUrl (), Jsons .emptyObject (), null ));
69
+ sourceDefinitionIdRequestBody .getWorkspaceId (),
70
+ sourceDefinitionIdRequestBody .getSourceDefinitionId (),
71
+ sourceDefinitionIdRequestBody .getRedirectUrl (), Jsons .emptyObject (), null ));
99
72
}
100
73
try {
101
- trackingClient .track (sourceOauthConsentRequest .getWorkspaceId (), "Get Oauth Consent URL - Backend" , metadata );
74
+ trackingClient .track (sourceDefinitionIdRequestBody .getWorkspaceId (), "Get Oauth Consent URL - Backend" , metadata );
102
75
} catch (final Exception e ) {
103
76
LOGGER .error (ERROR_MESSAGE , e );
104
77
}
105
78
return result ;
106
79
}
107
80
108
- public OAuthConsentRead getDestinationOAuthConsent (final DestinationOauthConsentRequest destinationOauthConsentRequest )
81
+ public OAuthConsentRead getDestinationOAuthConsent (final DestinationOauthConsentRequest destinationDefinitionIdRequestBody )
109
82
throws JsonValidationException , ConfigNotFoundException , IOException {
110
83
final StandardDestinationDefinition destinationDefinition =
111
- configRepository .getStandardDestinationDefinition (destinationOauthConsentRequest .getDestinationDefinitionId ());
84
+ configRepository .getStandardDestinationDefinition (destinationDefinitionIdRequestBody .getDestinationDefinitionId ());
112
85
final OAuthFlowImplementation oAuthFlowImplementation = oAuthImplementationFactory .create (destinationDefinition );
113
86
final ConnectorSpecification spec = destinationDefinition .getSpec ();
114
- final Map <String , Object > metadata = generateDestinationMetadata (destinationOauthConsentRequest .getDestinationDefinitionId ());
87
+ final Map <String , Object > metadata = generateDestinationMetadata (destinationDefinitionIdRequestBody .getDestinationDefinitionId ());
115
88
final OAuthConsentRead result ;
116
89
if (OAuthConfigSupplier .hasOAuthConfigSpecification (spec )) {
117
- final JsonNode oAuthInputConfigurationForConsent ;
118
-
119
- if (destinationOauthConsentRequest .getDestinationId () == null ) {
120
- oAuthInputConfigurationForConsent = destinationOauthConsentRequest .getoAuthInputConfiguration ();
121
- } else {
122
- final DestinationConnection hydratedSourceConnection =
123
- secretsRepositoryReader .getDestinationConnectionWithSecrets (destinationOauthConsentRequest .getDestinationId ());
124
-
125
- oAuthInputConfigurationForConsent = getOAuthInputConfigurationForConsent (spec ,
126
- hydratedSourceConnection .getConfiguration (),
127
- destinationOauthConsentRequest .getoAuthInputConfiguration ());
128
-
129
- }
130
-
131
90
result = new OAuthConsentRead ().consentUrl (oAuthFlowImplementation .getDestinationConsentUrl (
132
- destinationOauthConsentRequest .getWorkspaceId (),
133
- destinationOauthConsentRequest .getDestinationDefinitionId (),
134
- destinationOauthConsentRequest .getRedirectUrl (),
135
- oAuthInputConfigurationForConsent ,
91
+ destinationDefinitionIdRequestBody .getWorkspaceId (),
92
+ destinationDefinitionIdRequestBody .getDestinationDefinitionId (),
93
+ destinationDefinitionIdRequestBody .getRedirectUrl (),
94
+ destinationDefinitionIdRequestBody . getoAuthInputConfiguration () ,
136
95
spec .getAdvancedAuth ().getOauthConfigSpecification ()));
137
96
} else {
138
97
result = new OAuthConsentRead ().consentUrl (oAuthFlowImplementation .getDestinationConsentUrl (
139
- destinationOauthConsentRequest .getWorkspaceId (),
140
- destinationOauthConsentRequest .getDestinationDefinitionId (),
141
- destinationOauthConsentRequest .getRedirectUrl (), Jsons .emptyObject (), null ));
98
+ destinationDefinitionIdRequestBody .getWorkspaceId (),
99
+ destinationDefinitionIdRequestBody .getDestinationDefinitionId (),
100
+ destinationDefinitionIdRequestBody .getRedirectUrl (), Jsons .emptyObject (), null ));
142
101
}
143
102
try {
144
- trackingClient .track (destinationOauthConsentRequest .getWorkspaceId (), "Get Oauth Consent URL - Backend" , metadata );
103
+ trackingClient .track (destinationDefinitionIdRequestBody .getWorkspaceId (), "Get Oauth Consent URL - Backend" , metadata );
145
104
} catch (final Exception e ) {
146
105
LOGGER .error (ERROR_MESSAGE , e );
147
106
}
@@ -236,19 +195,6 @@ public void setDestinationInstancewideOauthParams(final SetInstancewideDestinati
236
195
configRepository .writeDestinationOAuthParam (param );
237
196
}
238
197
239
- private JsonNode getOAuthInputConfigurationForConsent (final ConnectorSpecification spec ,
240
- final JsonNode hydratedSourceConnectionConfiguration ,
241
- final JsonNode oAuthInputConfiguration ) {
242
- final Map <String , String > fieldsToGet =
243
- buildJsonPathFromOAuthFlowInitParameters (OAuthPathExtractor .extractOauthConfigurationPaths (
244
- spec .getAdvancedAuth ().getOauthConfigSpecification ().getOauthUserInputFromConnectorConfigSpecification ()));
245
-
246
- final JsonNode oAuthInputConfigurationFromDB = getOAuthInputConfiguration (hydratedSourceConnectionConfiguration , fieldsToGet );
247
-
248
- return getOauthFromDBIfNeeded (oAuthInputConfigurationFromDB ,
249
- oAuthInputConfiguration );
250
- }
251
-
252
198
private Map <String , Object > generateSourceMetadata (final UUID sourceDefinitionId )
253
199
throws JsonValidationException , ConfigNotFoundException , IOException {
254
200
final StandardSourceDefinition sourceDefinition = configRepository .getStandardSourceDefinition (sourceDefinitionId );
@@ -261,40 +207,4 @@ private Map<String, Object> generateDestinationMetadata(final UUID destinationDe
261
207
return TrackingMetadata .generateDestinationDefinitionMetadata (destinationDefinition );
262
208
}
263
209
264
- @ VisibleForTesting
265
- Map <String , String > buildJsonPathFromOAuthFlowInitParameters (final Map <String , List <String >> oAuthFlowInitParameters ) {
266
- return oAuthFlowInitParameters .entrySet ().stream ()
267
- .map (entry -> Map .entry (entry .getKey (), "$." + String .join ("." , entry .getValue ())))
268
- .collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue ));
269
- }
270
-
271
- @ VisibleForTesting
272
- JsonNode getOauthFromDBIfNeeded (final JsonNode oAuthInputConfigurationFromDB , final JsonNode oAuthInputConfigurationFromInput ) {
273
- final Map <String , String > result = new HashMap <>();
274
-
275
- Jsons .deserializeToStringMap (oAuthInputConfigurationFromInput )
276
- .forEach ((k , v ) -> {
277
- if (AirbyteSecretConstants .SECRETS_MASK .equals (v )) {
278
- if (oAuthInputConfigurationFromDB .has (k )) {
279
- result .put (k , oAuthInputConfigurationFromDB .get (k ).textValue ());
280
- } else {
281
- LOGGER .warn ("Missing the key {} in the config store in DB" , k );
282
- }
283
-
284
- } else {
285
- result .put (k , v );
286
- }
287
- });
288
-
289
- return Jsons .jsonNode (result );
290
- }
291
-
292
- @ VisibleForTesting
293
- JsonNode getOAuthInputConfiguration (final JsonNode hydratedSourceConnectionConfiguration , final Map <String , String > pathsToGet ) {
294
- return Jsons .jsonNode (pathsToGet .entrySet ().stream ()
295
- .collect (Collectors .toMap (
296
- Map .Entry ::getKey ,
297
- entry -> JsonPaths .getSingleValue (hydratedSourceConnectionConfiguration , entry .getValue ()).get ())));
298
- }
299
-
300
210
}
0 commit comments