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

fix(oauth): handle fields missing in the stored config when building consent url #20933

Merged
merged 2 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import org.slf4j.Logger;
Expand Down Expand Up @@ -342,10 +343,17 @@ JsonNode getOauthFromDBIfNeeded(final JsonNode oAuthInputConfigurationFromDB, fi

@VisibleForTesting
JsonNode getOAuthInputConfiguration(final JsonNode hydratedSourceConnectionConfiguration, final Map<String, String> pathsToGet) {
return Jsons.jsonNode(pathsToGet.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> JsonPaths.getSingleValue(hydratedSourceConnectionConfiguration, entry.getValue()).get())));
final Map<String, JsonNode> result = new HashMap<>();
pathsToGet.forEach((k, v) -> {
final Optional<JsonNode> configValue = JsonPaths.getSingleValue(hydratedSourceConnectionConfiguration, v);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we instead make sure the hydratedSourceConnectionConfiguration has all the expected paths?

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 expected behavior is actually that hydratedSourceConnectionConfiguration doesn't have the path we're trying to get in this case.

Because of the way the spec is defined, credentials.subdomain is only present in the config if the OAuth authentication type has been selected (since it's only used in the oauth case)

So, if you're coming from an API key auth, your config will look something like {"credentials": {"api_token": "sometoken", "auth_type": "api_token"}}. When this method runs after switching for oauth, we try to get the value of credentials.subdomain from the existing configuration but realize it's not there. Previously since we always expected it to be present an error would be thrown. Now, we just log a warning and don't retrieve the value.

if (configValue.isPresent()) {
result.put(k, configValue.get());
} else {
LOGGER.warn("Missing the key {} from the config stored in DB", k);
}
});

return Jsons.jsonNode(result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ void testGetOAuthInputConfiguration() {
final Map<String, String> pathsToGet = Map.ofEntries(
Map.entry("field1", "$.field1"),
Map.entry("field3_1", "$.field3.field3_1"),
Map.entry("field3_2", "$.field3.field3_2"));
Map.entry("field3_2", "$.field3.field3_2"),
Map.entry("field4", "$.someNonexistentField"));

final JsonNode expected = Jsons.deserialize(
"""
Expand Down