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

Initial implementation of GitLab OIDC trusted publisher #15275

Merged
merged 3 commits into from
Feb 22, 2024
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
26 changes: 26 additions & 0 deletions tests/common/db/oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
from warehouse.oidc.models import (
ActiveStatePublisher,
GitHubPublisher,
GitLabPublisher,
GooglePublisher,
PendingActiveStatePublisher,
PendingGitHubPublisher,
PendingGitLabPublisher,
PendingGooglePublisher,
)

Expand Down Expand Up @@ -51,6 +53,30 @@ class Meta:
added_by = factory.SubFactory(UserFactory)


class GitLabPublisherFactory(WarehouseFactory):
class Meta:
model = GitLabPublisher

id = factory.Faker("uuid4", cast_to=None)
project = factory.Faker("pystr", max_chars=12)
namespace = factory.Faker("pystr", max_chars=12)
workflow_filepath = "subfolder/example.yml"
environment = "production"


class PendingGitLabPublisherFactory(WarehouseFactory):
class Meta:
model = PendingGitLabPublisher

id = factory.Faker("uuid4", cast_to=None)
project_name = "fake-nonexistent-project"
project = factory.Faker("pystr", max_chars=12)
namespace = factory.Faker("pystr", max_chars=12)
workflow_filepath = "subfolder/example.yml"
environment = "production"
added_by = factory.SubFactory(UserFactory)


class GooglePublisherFactory(WarehouseFactory):
class Meta:
model = GooglePublisher
Expand Down
187 changes: 187 additions & 0 deletions tests/unit/accounts/test_views.py

Large diffs are not rendered by default.

96 changes: 93 additions & 3 deletions tests/unit/manage/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from warehouse.oidc.models import (
ActiveStatePublisher,
GitHubPublisher,
GitLabPublisher,
GooglePublisher,
OIDCPublisher,
)
Expand Down Expand Up @@ -5840,16 +5841,23 @@ def test_manage_project_oidc_publishers(self, monkeypatch):

view = views.ManageOIDCPublisherViews(project, request)
assert view.manage_project_oidc_publishers() == {
"disabled": {"GitHub": False, "Google": False, "ActiveState": False},
"disabled": {
"GitHub": False,
"GitLab": False,
"Google": False,
"ActiveState": False,
},
"project": project,
"github_publisher_form": view.github_publisher_form,
"gitlab_publisher_form": view.gitlab_publisher_form,
"google_publisher_form": view.google_publisher_form,
"activestate_publisher_form": view.activestate_publisher_form,
}

assert request.flags.disallow_oidc.calls == [
pretend.call(),
pretend.call(AdminFlagValue.DISALLOW_GITHUB_OIDC),
pretend.call(AdminFlagValue.DISALLOW_GITLAB_OIDC),
pretend.call(AdminFlagValue.DISALLOW_GOOGLE_OIDC),
pretend.call(AdminFlagValue.DISALLOW_ACTIVESTATE_OIDC),
]
Expand All @@ -5876,16 +5884,23 @@ def test_manage_project_oidc_publishers_admin_disabled(
view = views.ManageOIDCPublisherViews(project, pyramid_request)

assert view.manage_project_oidc_publishers() == {
"disabled": {"GitHub": True, "Google": True, "ActiveState": True},
"disabled": {
"GitHub": True,
"GitLab": True,
"Google": True,
"ActiveState": True,
},
"project": project,
"github_publisher_form": view.github_publisher_form,
"gitlab_publisher_form": view.gitlab_publisher_form,
"google_publisher_form": view.google_publisher_form,
"activestate_publisher_form": view.activestate_publisher_form,
}

assert pyramid_request.flags.disallow_oidc.calls == [
pretend.call(),
pretend.call(AdminFlagValue.DISALLOW_GITHUB_OIDC),
pretend.call(AdminFlagValue.DISALLOW_GITLAB_OIDC),
pretend.call(AdminFlagValue.DISALLOW_GOOGLE_OIDC),
pretend.call(AdminFlagValue.DISALLOW_ACTIVESTATE_OIDC),
]
Expand Down Expand Up @@ -5924,6 +5939,27 @@ def test_manage_project_oidc_publishers_admin_disabled(
normalized_environment=publisher.environment,
),
),
(
"add_gitlab_oidc_publisher",
pretend.stub(
id="fakeid",
publisher_name="GitLab",
project="fakerepo",
publisher_url=(
lambda x=None: "https://gitlab.com/fakeowner/fakerepo"
),
namespace="fakeowner",
workflow_filepath="subfolder/fakeworkflow.yml",
environment="some-environment",
),
lambda publisher: pretend.stub(
validate=pretend.call_recorder(lambda: True),
project=pretend.stub(data=publisher.project),
namespace=pretend.stub(data=publisher.namespace),
workflow_filepath=pretend.stub(data=publisher.workflow_filepath),
normalized_environment=publisher.environment,
),
),
(
"add_google_oidc_publisher",
pretend.stub(
Expand Down Expand Up @@ -6002,6 +6038,7 @@ def test_add_oidc_publisher_preexisting(
publisher_form_obj = make_form(publisher)
publisher_form_cls = pretend.call_recorder(lambda *a, **kw: publisher_form_obj)
monkeypatch.setattr(views, "GitHubPublisherForm", publisher_form_cls)
monkeypatch.setattr(views, "GitLabPublisherForm", publisher_form_cls)
monkeypatch.setattr(views, "GooglePublisherForm", publisher_form_cls)
monkeypatch.setattr(views, "ActiveStatePublisherForm", publisher_form_cls)

Expand Down Expand Up @@ -6070,6 +6107,17 @@ def test_add_oidc_publisher_preexisting(
),
pretend.stub(publisher_name="GitHub"),
),
(
"add_gitlab_oidc_publisher",
pretend.stub(
validate=pretend.call_recorder(lambda: True),
project=pretend.stub(data="fakerepo"),
namespace=pretend.stub(data="fakeowner"),
workflow_filepath=pretend.stub(data="subfolder/fakeworkflow.yml"),
normalized_environment="some-environment",
),
pretend.stub(publisher_name="GitLab"),
),
(
"add_google_oidc_publisher",
pretend.stub(
Expand Down Expand Up @@ -6132,6 +6180,7 @@ def test_add_oidc_publisher_created(

publisher_form_cls = pretend.call_recorder(lambda *a, **kw: publisher_form_obj)
monkeypatch.setattr(views, "GitHubPublisherForm", publisher_form_cls)
monkeypatch.setattr(views, "GitLabPublisherForm", publisher_form_cls)
monkeypatch.setattr(views, "GooglePublisherForm", publisher_form_cls)
monkeypatch.setattr(views, "ActiveStatePublisherForm", publisher_form_cls)
monkeypatch.setattr(
Expand Down Expand Up @@ -6223,6 +6272,24 @@ def test_add_oidc_publisher_created(
}
),
),
(
"add_gitlab_oidc_publisher",
"GitLab",
GitLabPublisher(
project="some-repository",
namespace="some-owner",
workflow_filepath="subfolder/some-workflow-filename.yml",
environment="some-environment",
),
MultiDict(
{
"namespace": "some-owner",
"project": "some-repository",
"workflow_filepath": "subfolder/some-workflow-filename.yml",
"environment": "some-environment",
}
),
),
(
"add_google_oidc_publisher",
"Google",
Expand Down Expand Up @@ -6310,9 +6377,15 @@ def test_add_oidc_publisher_already_registered_with_project(
)

assert getattr(view, view_name)() == {
"disabled": {"GitHub": False, "Google": False, "ActiveState": False},
"disabled": {
"GitHub": False,
"GitLab": False,
"Google": False,
"ActiveState": False,
},
"project": project,
"github_publisher_form": view.github_publisher_form,
"gitlab_publisher_form": view.gitlab_publisher_form,
"google_publisher_form": view.google_publisher_form,
"activestate_publisher_form": view.activestate_publisher_form,
}
Expand All @@ -6334,6 +6407,7 @@ def test_add_oidc_publisher_already_registered_with_project(
"view_name, publisher_name",
[
("add_github_oidc_publisher", "GitHub"),
("add_gitlab_oidc_publisher", "GitLab"),
("add_google_oidc_publisher", "Google"),
("add_activestate_oidc_publisher", "ActiveState"),
],
Expand Down Expand Up @@ -6383,6 +6457,7 @@ def test_add_oidc_publisher_ratelimited(
"view_name, publisher_name",
[
("add_github_oidc_publisher", "GitHub"),
("add_gitlab_oidc_publisher", "GitLab"),
("add_google_oidc_publisher", "Google"),
("add_activestate_oidc_publisher", "ActiveState"),
],
Expand Down Expand Up @@ -6425,6 +6500,7 @@ def test_add_oidc_publisher_admin_disabled(
"view_name, publisher_name",
[
("add_github_oidc_publisher", "GitHub"),
("add_gitlab_oidc_publisher", "GitLab"),
("add_google_oidc_publisher", "Google"),
("add_activestate_oidc_publisher", "ActiveState"),
],
Expand All @@ -6450,12 +6526,14 @@ def test_add_oidc_publisher_invalid_form(
)
publisher_form_cls = pretend.call_recorder(lambda *a, **kw: publisher_form_obj)
monkeypatch.setattr(views, "GitHubPublisherForm", publisher_form_cls)
monkeypatch.setattr(views, "GitLabPublisherForm", publisher_form_cls)
monkeypatch.setattr(views, "GooglePublisherForm", publisher_form_cls)
monkeypatch.setattr(views, "ActiveStatePublisherForm", publisher_form_cls)

view = views.ManageOIDCPublisherViews(project, request)
default_response = {
"github_publisher_form": publisher_form_obj,
"gitlab_publisher_form": publisher_form_obj,
"google_publisher_form": publisher_form_obj,
"activestate_publisher_form": publisher_form_obj,
}
Expand Down Expand Up @@ -6490,6 +6568,12 @@ def test_add_oidc_publisher_invalid_form(
workflow_filename="some-workflow-filename.yml",
environment="some-environment",
),
GitLabPublisher(
project="some-repository",
namespace="some-owner",
workflow_filepath="subfolder/some-workflow-filename.yml",
environment="some-environment",
),
GooglePublisher(
email="[email protected]",
sub="some-sub",
Expand Down Expand Up @@ -6598,6 +6682,12 @@ def test_delete_oidc_publisher_registered_to_multiple_projects(
workflow_filename="some-workflow-filename.yml",
environment="some-environment",
),
GitLabPublisher(
project="some-repository",
namespace="some-owner",
workflow_filepath="subfolder/some-workflow-filename.yml",
environment="some-environment",
),
GooglePublisher(
email="[email protected]",
sub="some-sub",
Expand Down
Loading