Skip to content

🎉 Source Gitlab: add OAuth2.0 authentication support #7506

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

Merged
merged 45 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
e1cea7e
added support of oauth2.0
Oct 29, 2021
6934a2d
added OAuthFlow.java part
Oct 31, 2021
ac6dd9e
bumped version of the Dockerfile
Oct 31, 2021
37af9c0
corrected SAT
Oct 31, 2021
5ecff85
added unit_test for Oauth java part, added integration-test for java …
Oct 31, 2021
26612c4
added changelog
Oct 31, 2021
98e03ec
fix java part test
Oct 31, 2021
af49c77
updated after review
Nov 1, 2021
bd67cec
Merge remote-tracking branch 'origin/master' into bazarnov/7480-gitla…
Nov 1, 2021
ec26576
updated spec.json
Nov 3, 2021
ee22f2b
Merge remote-tracking branch 'origin/master' into bazarnov/7480-gitla…
Nov 4, 2021
ac15b82
updated after review
Nov 5, 2021
8ede858
Merge branch 'master' into bazarnov/7480-gitlab-oauth
davydov-d Dec 4, 2022
3ab2f00
Merge branch 'master' into bazarnov/7480-gitlab-oauth
davydov-d Dec 4, 2022
a06e575
#7480 source gitlab: rm empty files
davydov-d Dec 4, 2022
2f31254
#7480 source gitlab: add single use refresh token authenticator
davydov-d Dec 6, 2022
6840a23
#7480 source gitlab: upd oauth implementation
davydov-d Dec 6, 2022
b119ccb
Merge branch 'master' into bazarnov/7480-gitlab-oauth
davydov-d Dec 6, 2022
fc458a2
#7480 source gitlab: refactor auth
davydov-d Dec 7, 2022
5551985
Merge branch 'master' into bazarnov/7480-gitlab-oauth
alafanechere Dec 7, 2022
853c2b7
Merge branch 'master' into bazarnov/7480-gitlab-oauth
davydov-d Dec 7, 2022
fad195e
Merge branch 'master' into bazarnov/7480-gitlab-oauth
davydov-d Dec 8, 2022
8f17e3e
#7480 fix oauth: java
davydov-d Dec 8, 2022
da55d45
Merge branch 'bazarnov/7480-gitlab-oauth' of github.com:airbytehq/air…
davydov-d Dec 8, 2022
39bf3d3
Merge branch 'master' into bazarnov/7480-gitlab-oauth
davydov-d Dec 9, 2022
c2cb032
Merge branch 'master' into bazarnov/7480-gitlab-oauth
davydov-d Dec 9, 2022
229e3c0
#7480 source gitlab: review fixes
davydov-d Dec 9, 2022
52439df
Merge branch 'master' into bazarnov/7480-gitlab-oauth
davydov-d Dec 11, 2022
266eb98
Merge branch 'master' into bazarnov/7480-gitlab-oauth
davydov-d Dec 13, 2022
ae5b0bc
Merge branch 'master' into bazarnov/7480-gitlab-oauth
davydov-d Dec 15, 2022
4be38bc
#7480 source gitlab: upd release stage
davydov-d Dec 15, 2022
880cbfa
#7480 source gitlab: fix unit tests
davydov-d Dec 15, 2022
4d1a6d1
#7480 source gitlab: update expected records
davydov-d Dec 15, 2022
1515ab2
Merge branch 'master' into bazarnov/7480-gitlab-oauth
davydov-d Dec 16, 2022
0b494f1
#7480 source gitlab: upd sats
davydov-d Dec 16, 2022
5000dfb
#7480 source gitlab: upd expected records
davydov-d Dec 16, 2022
67209d4
Merge branch 'master' into bazarnov/7480-gitlab-oauth
davydov-d Jan 10, 2023
41f06cb
Merge branch 'master' into bazarnov/7480-gitlab-oauth
davydov-d Jan 11, 2023
c3dfe00
#7480 source Gitlab: use SingleUseRefreshTokenOauth as is
davydov-d Jan 12, 2023
59b3637
Merge branch 'master' into bazarnov/7480-gitlab-oauth
davydov-d Jan 12, 2023
d13c5d1
Merge branch 'master' into bazarnov/7480-gitlab-oauth
davydov-d Jan 12, 2023
0da8ab6
#7480 sourcte gitlab: upd expected records
davydov-d Jan 18, 2023
7052aed
Merge branch 'bazarnov/7480-gitlab-oauth' of github.com:airbytehq/air…
davydov-d Jan 18, 2023
550f5ad
auto-bump connector version
octavia-squidington-iii Jan 18, 2023
9df8fa1
Merge branch 'master' into bazarnov/7480-gitlab-oauth
davydov-d Jan 18, 2023
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
2 changes: 1 addition & 1 deletion airbyte-integrations/connectors/source-gitlab/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ COPY main.py ./

ENTRYPOINT ["python", "/airbyte/integration_code/main.py"]

LABEL io.airbyte.version=0.1.9
LABEL io.airbyte.version=1.0.0
LABEL io.airbyte.name=airbyte/source-gitlab
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,32 @@ connector_image: airbyte/source-gitlab:dev
tests:
spec:
- spec_path: "source_gitlab/spec.json"
backward_compatibility_tests_config:
disable_for_version: "0.1.9"
connection:
- config_path: "secrets/config.json"
status: "succeed"
- config_path: "secrets/config_oauth.json"
status: "succeed"
- config_path: "integration_tests/invalid_config.json"
status: "failed"
discovery:
- config_path: "secrets/config.json"
backward_compatibility_tests_config:
disable_for_version: "0.1.9"
- config_path: "secrets/config_oauth.json"
backward_compatibility_tests_config:
disable_for_version: "0.1.9"
basic_read:
- config_path: "secrets/config.json"
configured_catalog_path: "integration_tests/configured_catalog.json"
empty_streams: ["group_issue_boards"]
expect_records:
path: "integration_tests/expected_records.txt"
# test read with OAuth2.0
- config_path: "secrets/config_oauth.json"
configured_catalog_path: "integration_tests/configured_catalog.json"
empty_streams: ["group_issue_boards"]
# We cannot use these tests for testing Incremental, since for Gitlab the State is saved for each Project separately,
# and the Acceptance Tests at this stage do not support this functionality.
# Therefore, we hardcode the cursor_paths for our config.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"api_url": "gitlab.com",
"private_token": "private_token-fake",
"groups": "new-group",
"projects": "new-ci-test",
"start_date": "2021-01-01T00:00:00Z"
}
"start_date": "2021-01-01T00:00:00Z",
"groups": "new-group-airbute",
"projects": "new-group-airbute/new-ci-test-project",
"credentials": {
"auth_type": "access_token",
"access_token": "migrated_from_old_config"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
#


from typing import Any, List, Mapping, Tuple
from typing import Any, List, MutableMapping, Tuple

import pendulum
import requests
from airbyte_cdk.models import SyncMode
from airbyte_cdk.sources import AbstractSource
from airbyte_cdk.sources.streams import Stream
from airbyte_cdk.sources.streams.http.auth import TokenAuthenticator
from airbyte_cdk.sources.streams.http.requests_native_auth.oauth import SingleUseRefreshTokenOauth2Authenticator
from airbyte_cdk.sources.streams.http.requests_native_auth.token import TokenAuthenticator
from requests.auth import AuthBase

from .streams import (
Branches,
Expand Down Expand Up @@ -39,11 +42,47 @@
)


class SourceGitlab(AbstractSource):
def _generate_main_streams(self, config: Mapping[str, Any]) -> Tuple[GitlabStream, GitlabStream]:
auth = TokenAuthenticator(token=config["private_token"])
auth_params = dict(authenticator=auth, api_url=config["api_url"])
class GitlabOauth2Authenticator(SingleUseRefreshTokenOauth2Authenticator):
def __init__(self, connector_config, *args, **kwargs):
super().__init__(connector_config, *args, **kwargs)
access_token = connector_config["credentials"].get("access_token")
if not access_token:
return
api_url = self._connector_config["api_url"]
token_valid, access_token_info = self.get_access_token_info(api_url, access_token)
if not token_valid:
return
now = pendulum.now()
self.access_token = access_token
self.set_token_expiry_date(now.add(seconds=access_token_info["expires_in"]))

@staticmethod
def get_access_token_info(api_url: str, access_token: str) -> Tuple[bool, MutableMapping]:
response = requests.get(f"https://{api_url}/oauth/token/info?access_token={access_token}")
if response.status_code == 200:
return True, response.json()
return False, {}


def get_authenticator(config: MutableMapping) -> AuthBase:
if config["credentials"]["auth_type"] == "access_token":
return TokenAuthenticator(token=config["credentials"]["access_token"])
return GitlabOauth2Authenticator(config, token_refresh_endpoint=f"https://{config['api_url']}/oauth/token")


class SourceGitlab(AbstractSource):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._auth_params = {}

def auth_params(self, config: MutableMapping[str, Any]) -> MutableMapping[str, Any]:
if not self._auth_params:
auth = get_authenticator(config)
self._auth_params = dict(authenticator=auth, api_url=config["api_url"])
return self._auth_params

def _generate_main_streams(self, config: MutableMapping[str, Any]) -> Tuple[GitlabStream, GitlabStream]:
auth_params = self.auth_params(config)
pids = list(filter(None, config.get("projects", "").split(" ")))
gids = config.get("groups")

Expand Down Expand Up @@ -90,10 +129,8 @@ def check_connection(self, logger, config) -> Tuple[bool, any]:
except Exception as error:
return False, f"Unable to connect to Gitlab API with the provided credentials - {repr(error)}"

def streams(self, config: Mapping[str, Any]) -> List[Stream]:
auth = TokenAuthenticator(token=config["private_token"])
auth_params = dict(authenticator=auth, api_url=config["api_url"])

def streams(self, config: MutableMapping[str, Any]) -> List[Stream]:
auth_params = self.auth_params(config)
groups, projects = self._generate_main_streams(config)
pipelines = Pipelines(parent_stream=projects, start_date=config["start_date"], **auth_params)
merge_requests = MergeRequests(parent_stream=projects, start_date=config["start_date"], **auth_params)
Expand Down
118 changes: 110 additions & 8 deletions airbyte-integrations/connectors/source-gitlab/source_gitlab/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@
"documentationUrl": "https://docs.airbyte.com/integrations/sources/gitlab",
"connectionSpecification": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Source GitLab Singer Spec",
"title": "Source Gitlab Spec",
"type": "object",
"required": ["api_url", "private_token", "start_date"],
"required": ["api_url", "start_date", "credentials"],
"additionalProperties": true,
"properties": {
"api_url": {
"type": "string",
"examples": ["gitlab.com"],
"title": "API URL",
"default": "gitlab.com",
"description": "Please enter your basic URL from GitLab instance."
},
"private_token": {
"type": "string",
"title": "Private Token",
"description": "Log into your GitLab account and then generate a personal Access Token.",
"airbyte_secret": true
},
"groups": {
"type": "string",
"examples": ["airbyte.io"],
Expand All @@ -37,6 +32,113 @@
"description": "The date from which you'd like to replicate data for GitLab API, in the format YYYY-MM-DDT00:00:00Z. All data generated after this date will be replicated.",
"examples": ["2021-03-01T00:00:00Z"],
"pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
},
"credentials": {
"title": "Authorization Method",
"type": "object",
"oneOf": [
{
"type": "object",
"title": "OAuth2.0",
"required": ["client_id", "client_secret", "refresh_token"],
"properties": {
"auth_type": {
"type": "string",
"const": "oauth2.0"
},
"client_id": {
"type": "string",
"description": "The API ID of the Gitlab developer application.",
"airbyte_secret": true
},
"client_secret": {
"type": "string",
"description": "The API Secret the Gitlab developer application.",
"airbyte_secret": true
},
"access_token": {
"type": "string",
"description": "Access Token for making authenticated requests.",
"airbyte_secret": true
},
"refresh_token": {
"type": "string",
"description": "The key to refresh the expired access_token.",
"airbyte_secret": true
}
}
},
{
"title": "Private Token",
"type": "object",
"required": ["access_token"],
"properties": {
"auth_type": {
"type": "string",
"const": "access_token"
},
"access_token": {
"type": "string",
"title": "Private Token",
"description": "Log into your Gitlab account and then generate a personal Access Token.",
"airbyte_secret": true
}
}
}
]
}
}
},
"advanced_auth": {
"auth_flow_type": "oauth2.0",
"predicate_key": ["credentials", "auth_type"],
"predicate_value": "oauth2.0",
"oauth_config_specification": {
"oauth_user_input_from_connector_config_specification": {
"type": "object",
"properties": {
"domain": {
"type": "string",
"path_in_connector_config": ["api_url"]
}
}
},
"complete_oauth_output_specification": {
"type": "object",
"properties": {
"access_token": {
"type": "string",
"path_in_connector_config": ["credentials", "access_token"]
},
"refresh_token": {
"type": "string",
"path_in_connector_config": ["credentials", "refresh_token"]
}
}
},
"complete_oauth_server_input_specification": {
"type": "object",
"properties": {
"client_id": {
"type": "string"
},
"client_secret": {
"type": "string"
}
}
},
"complete_oauth_server_output_specification": {
"type": "object",
"properties": {
"client_id": {
"type": "string",
"path_in_connector_config": ["credentials", "client_id"]
},
"client_secret": {
"type": "string",
"path_in_connector_config": ["credentials", "client_secret"]
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public OAuthImplementationFactory(final ConfigRepository configRepository, final
.put("airbyte/source-strava", new StravaOAuthFlow(configRepository, httpClient))
.put("airbyte/source-surveymonkey", new SurveymonkeyOAuthFlow(configRepository, httpClient))
.put("airbyte/source-trello", new TrelloOAuthFlow(configRepository))
.put("airbyte/source-gitlab", new GitlabOAuthFlow(configRepository, httpClient))
.put("airbyte/source-youtube-analytics", new YouTubeAnalyticsOAuthFlow(configRepository, httpClient))
// revert me
.put("airbyte/source-youtube-analytics-business", new YouTubeAnalyticsBusinessOAuthFlow(configRepository, httpClient))
Expand Down
Loading