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

Artem1205/lowcode cdk authenticator schemaloader #19718

Merged
merged 18 commits into from
Dec 13, 2022

Conversation

artem1205
Copy link
Collaborator

@artem1205 artem1205 commented Nov 22, 2022

What

#19753 - Declarative schema loader $ref
#19752 - Oauth Authenticator expiry date format

  • DeclarativeOauth2Authenticator expects expires_in in seconds (in get_access_token method). New feature makes it possible to specify date format (if not seconds)
  • Low-code connectors do not resolve schemas with references to another *.json files in shared folder, e.g. {"$ref": "file.json"} now. This PR fixes it.
    • this functionality is moved from original (non low-code) CDK to deal with reference mechanism, which used shared folder to resolve '$ref' links.

How

  • Add token_expiry_date_format field to Declarative OAuth Authenticator.
  • Add Resolve ref schema

Recommended reading order

  1. x.java
  2. y.python

🚨 User Impact 🚨

Are there any breaking changes? What is the end result perceived by the user? If yes, please merge this PR with the 🚨🚨 emoji so changelog authors can further highlight this if needed.

Pre-merge Checklist

Expand the relevant checklist and delete the others.

New Connector

Community member or Airbyter

  • Community member? Grant edit access to maintainers (instructions)
  • Secrets in the connector's spec are annotated with airbyte_secret
  • Unit & integration tests added and passing. Community members, please provide proof of success locally e.g: screenshot or copy-paste unit, integration, and acceptance test output. To run acceptance tests for a Python connector, follow instructions in the README. For java connectors run ./gradlew :airbyte-integrations:connectors:<name>:integrationTest.
  • Code reviews completed
  • Documentation updated
    • Connector's README.md
    • Connector's bootstrap.md. See description and examples
    • docs/integrations/<source or destination>/<name>.md including changelog. See changelog example
    • docs/integrations/README.md
    • airbyte-integrations/builds.md
  • PR name follows PR naming conventions

Airbyter

If this is a community PR, the Airbyte engineer reviewing this PR is responsible for the below items.

  • Create a non-forked branch based on this PR and test the below items on it
  • Build is successful
  • If new credentials are required for use in CI, add them to GSM. Instructions.
  • /test connector=connectors/<name> command is passing
  • New Connector version released on Dockerhub by running the /publish command described here
  • After the connector is published, connector added to connector index as described here
  • Seed specs have been re-generated by building the platform and committing the changes to the seed spec files, as described here
Updating a connector

Community member or Airbyter

  • Grant edit access to maintainers (instructions)
  • Secrets in the connector's spec are annotated with airbyte_secret
  • Unit & integration tests added and passing. Community members, please provide proof of success locally e.g: screenshot or copy-paste unit, integration, and acceptance test output. To run acceptance tests for a Python connector, follow instructions in the README. For java connectors run ./gradlew :airbyte-integrations:connectors:<name>:integrationTest.
  • Code reviews completed
  • Documentation updated
    • Connector's README.md
    • Connector's bootstrap.md. See description and examples
    • Changelog updated in docs/integrations/<source or destination>/<name>.md including changelog. See changelog example
  • PR name follows PR naming conventions

Airbyter

If this is a community PR, the Airbyte engineer reviewing this PR is responsible for the below items.

  • Create a non-forked branch based on this PR and test the below items on it
  • Build is successful
  • If new credentials are required for use in CI, add them to GSM. Instructions.
  • /test connector=connectors/<name> command is passing
  • New Connector version released on Dockerhub and connector version bumped by running the /publish command described here
Connector Generator
  • Issue acceptance criteria met
  • PR name follows PR naming conventions
  • If adding a new generator, add it to the list of scaffold modules being tested
  • The generator test modules (all connectors with -scaffold in their name) have been updated with the latest scaffold by running ./gradlew :airbyte-integrations:connector-templates:generator:testScaffoldTemplates then checking in your changes
  • Documentation which references the generator is updated as needed

Tests

Unit

Put your unit tests output here.

Integration

Put your integration tests output here.

Acceptance

Put your acceptance tests output here.

@artem1205 artem1205 requested a review from a team as a code owner November 22, 2022 16:01
@octavia-squidington-iv octavia-squidington-iv added the CDK Connector Development Kit label Nov 22, 2022
…tor-schemaloader' into artem1205/lowcode-cdk-authenticator-schemaloader

# Conflicts:
#	airbyte-cdk/python/airbyte_cdk/sources/declarative/schema/json_file_schema_loader.py
@artem1205 artem1205 requested a review from girarda November 23, 2022 12:45
@girarda girarda requested review from brianjlai and sherifnada and removed request for pedroslopez November 28, 2022 18:15
@sherifnada
Copy link
Contributor

@artem1205 apologies for the delay in review, will review tomorrow

Copy link
Contributor

@brianjlai brianjlai left a comment

Choose a reason for hiding this comment

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

Echoing Alex, it would be very helpful in the PR description to more concretely explain what the use case is for the reference resolution in the schema loader. Also had some notes around how the authenticator is being refactored

@@ -29,10 +29,9 @@ def get_auth_header(self) -> Mapping[str, Any]:
def get_access_token(self) -> str:
"""Returns the access token"""
if self.token_has_expired():
t0 = pendulum.now()
Copy link
Contributor

Choose a reason for hiding this comment

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

one slight drawback to moving the now() generation into the authenticator set token expiry methods is that we do have a very small corner case where we may attempt a request with a token that has expired.

When we were using pendulum.now() before refreshing the access token, the corner case meant we might think a token is already expired and refresh it earlier than necessary.

However, now with the reverse of calling pendulum.now() after already refreshing the token, then at the very end of expiry, we may attempt a request where the token has already expired.

I'd argue that refreshing the token a little more often than necessary is better than attempting requests with an already expired token so just to be on the safe side, I think we should continue to get the current time before calling self.refresh_access_token() and pass it into self.set_token_expiry_date() Its a little verbose to pass both params in, but we're already changing the interface regardless.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

return resolved


def resolve_ref_links(obj: Any) -> Union[Dict[str, Any], List[Any]]:
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add some unit tests to validate some each of the scenarios that can be recursed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added

@artem1205 artem1205 requested a review from brianjlai November 29, 2022 19:41
Copy link
Contributor

@brianjlai brianjlai left a comment

Choose a reason for hiding this comment

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

The oauth changes look good, but thinking about the references a little more, wanted to get your thoughts on a slight refactor of the file schema loader to reuse some more code.

@@ -63,7 +68,8 @@ def get_json_schema(self) -> Mapping[str, Any]:
raw_schema = json.loads(raw_json_file)
except ValueError as err:
raise RuntimeError(f"Invalid JSON file format for file {json_schema_path}") from err
return raw_schema
self.__resource = resource
return self._resolve_schema_references(raw_schema)
Copy link
Contributor

Choose a reason for hiding this comment

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

Given that we're mostly copying these two methods from the existing code in schema_helpers.py, it might be interesting to make the JsonFileSchemaLoader a subclass of ResourceSchemaLoader. And we can refactor the SchemaLoader interface to name get_json_schema() to get_schema(). And this class json_file_schema_loader would just be responsible for the overriding get_schema() and can reuse the dependent methods that were originally duplicated.

It does expand the scope of changes a little bit, but would help us cut down on the code duplication. Does this all make sense?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Makes sense, I've refactored it.

@artem1205 artem1205 requested a review from brianjlai November 30, 2022 14:51
Copy link
Contributor

@brianjlai brianjlai left a comment

Choose a reason for hiding this comment

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

At the moment, the way the method names are defined, we won't be properly invoking schema retrieval according to the declarative interface SchemaLoader. I mentioned a few alternatives in the comments

@@ -50,7 +51,7 @@ def __post_init__(self, options: Mapping[str, Any]):
self.file_path = _default_file_path()
self.file_path = InterpolatedString.create(self.file_path, options=options)

def get_json_schema(self) -> Mapping[str, Any]:
def get_schema(self) -> Mapping[str, Any]:
Copy link
Contributor

Choose a reason for hiding this comment

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

If we are renaming get_json_schema() to get_schema() here, we will also need to rename it on the parent interface class SchemaLoader not just in DefaultSchemaLoader There are a few places where we use SchemaLoader.get_json_schema() at runtime so we need to properly override it so that it gets used everywhere.

Alternatively, if we want to simplify this, we could also leave this as get_json_schema(), so the parent interface doesn't have to change. And from the perspective of the declarative components we just don't reuse the parent class ResourceSchemaLoader.get_schema(self, name: str). And this still allows us to reuse the recursive resolver.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

not sure what do mean about renaming, as I override get_schema which present in ResourceSchemaLoader.

@sherifnada , I would kindly ask you to review it.

Copy link
Contributor

Choose a reason for hiding this comment

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

@artem1205
JsonFileSchemaLoader also extends SchemaLoader so it needs to implement get_json_schema too

Copy link
Collaborator Author

@artem1205 artem1205 Dec 10, 2022

Choose a reason for hiding this comment

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

I've overwritten get_json_schema -> get_schema https://github.com/airbytehq/airbyte/pull/19718/files#diff-eebfee995e9a963a52d517c0ebcab9579872dee179cf961719740e6c3fdddc9fR40.
So we do not really need SchemaLoader anymore. I can remove it

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should be getting rid of SchemaLoader. This JsonFileSchemaLoader and the DefaultSchemaLoader are intended to be subclasses of the SchemaLoader. From within parts of the low-code CDK, we invoke this get_json_schema() at runtime. For example here: https://github.com/airbytehq/airbyte/blob/master/airbyte-cdk/python/airbyte_cdk/sources/declarative/declarative_stream.py#L132

If we rename this method in this class, we should also rename it in SchemaLoader as well instead of removing the interface altogether. And then we also rename wherever it's being used like in the file I mentioned. Just renaming this method name will probably break out backend because we're using it in a few different places.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

✅ Refactored, renamed it all back to get_json_schema

@lazebnyi lazebnyi linked an issue Dec 9, 2022 that may be closed by this pull request
@lazebnyi lazebnyi linked an issue Dec 9, 2022 that may be closed by this pull request
@artem1205 artem1205 requested a review from brianjlai December 9, 2022 13:14
Copy link
Contributor

@brianjlai brianjlai left a comment

Choose a reason for hiding this comment

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

Looks good to me and the latest merge of master looks to have fixed some of the unrelated test failures. Let just address the two formatting issues that are failing the CI checks and then I'll approve along with the CDK version bump.

@@ -1,7 +1,6 @@
#
# Copyright (c) 2022 Airbyte, Inc., all rights reserved.
#

Copy link
Contributor

Choose a reason for hiding this comment

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

This will need to be added back otherwise our file formatter in CI is going to continue failing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@@ -94,8 +96,11 @@ def get_refresh_request_body(self) -> Mapping[str, Any]:
def get_token_expiry_date(self) -> pendulum.DateTime:
return self._token_expiry_date

def set_token_expiry_date(self, value: pendulum.DateTime):
self._token_expiry_date = value
def set_token_expiry_date(self, initial_time: pendulum.DateTime, value: Union[str, int]):
Copy link
Contributor

Choose a reason for hiding this comment

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

also an extra space between parameters which is throwing off the formatter

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@artem1205 artem1205 requested a review from brianjlai December 13, 2022 22:44
Copy link
Contributor

@brianjlai brianjlai left a comment

Choose a reason for hiding this comment

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

changes look good! thanks for incorporating the feedback in

@artem1205
Copy link
Collaborator Author

artem1205 commented Dec 13, 2022

/publish-cdk dry-run=true

🕑 https://github.com/airbytehq/airbyte/actions/runs/3690043244
https://github.com/airbytehq/airbyte/actions/runs/3690043244

@artem1205
Copy link
Collaborator Author

artem1205 commented Dec 13, 2022

/publish-cdk dry-run=false

🕑 https://github.com/airbytehq/airbyte/actions/runs/3690089496
https://github.com/airbytehq/airbyte/actions/runs/3690089496

@artem1205 artem1205 dismissed girarda’s stale review December 13, 2022 23:11

fixed & approved

@artem1205 artem1205 merged commit cb26ce5 into master Dec 13, 2022
@artem1205 artem1205 deleted the artem1205/lowcode-cdk-authenticator-schemaloader branch December 13, 2022 23:11
@YowanR
Copy link
Contributor

YowanR commented Dec 14, 2022

How's this one going?

@artem1205
Copy link
Collaborator Author

How's this one going?

Already Merged and closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CDK Connector Development Kit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Declarative schema loader $ref Oauth Authenticator expiry date format
6 participants