Repository navigation
Add distribution and action scoping to Cargo tokens #63
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
Open
aKlimau
wants to merge
1
commit into
pulp:main
Choose a base branch
from
aKlimau:add-token-skope
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Cargo tokens can now be restricted to specific distributions and actions. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| from django.conf import settings | ||
|
|
||
|
|
||
| def has_distributions_param_model_or_domain_or_obj_perms(request, view, action, permission): | ||
| """Check the permission against every distribution in the `distributions` parameter.""" | ||
| if request.user.has_perm(permission): | ||
| return True | ||
| if settings.DOMAIN_ENABLED and request.user.has_perm(permission, obj=request.pulp_domain): | ||
| return True | ||
|
|
||
| serializer = view.get_serializer(data=request.data) | ||
| serializer.is_valid(raise_exception=True) | ||
| return all( | ||
| request.user.has_perm(permission, distribution) | ||
| for distribution in serializer.validated_data.get("distributions", []) | ||
| ) |
23 changes: 23 additions & 0 deletions
23
pulp_rust/app/migrations/0005_rustcargotoken_actions_rustcargotoken_distributions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Generated by Django 5.2.14 on 2026-09-29 12:18 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('rust', '0004_alter_rustcargotoken_options_and_more'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name='rustcargotoken', | ||
| name='actions', | ||
| field=models.JSONField(blank=True, default=list), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='rustcargotoken', | ||
| name='distributions', | ||
| field=models.ManyToManyField(blank=True, related_name='cargo_tokens', to='rust.rustdistribution'), | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ | |
|
|
||
| from django.conf import settings | ||
| from django.db import models | ||
| from django.db.models.signals import post_delete, pre_delete | ||
| from django.dispatch import receiver | ||
| from django_lifecycle import AFTER_CREATE, hook | ||
|
|
||
| from pulpcore.plugin.models import ( | ||
|
|
@@ -359,13 +361,40 @@ class Meta: | |
| ] | ||
|
|
||
|
|
||
| CARGO_TOKEN_ACTIONS = ("publish", "yank") | ||
|
|
||
|
|
||
| class RustCargoToken(BaseModel): | ||
| user = models.ForeignKey( | ||
| settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="cargo_tokens" | ||
| ) | ||
| name = models.CharField(max_length=255, blank=False, null=False) | ||
| token_hash = models.CharField(max_length=64, unique=True, db_index=True) | ||
| last_used = models.DateTimeField(null=True, blank=True) | ||
| distributions = models.ManyToManyField( | ||
| RustDistribution, blank=True, related_name="cargo_tokens" | ||
| ) | ||
| actions = models.JSONField(default=list, blank=True) | ||
|
|
||
| class Meta: | ||
| default_related_name = "%(app_label)s_%(model_name)s" | ||
|
|
||
|
|
||
| @receiver(pre_delete, sender=RustDistribution) | ||
| def remember_scoped_cargo_tokens(instance, **kwargs): | ||
| """Record the tokens scoped to this distribution before the m2m rows are cascaded away.""" | ||
| instance._scoped_cargo_token_pks = list(instance.cargo_tokens.values_list("pk", flat=True)) | ||
|
|
||
|
|
||
| @receiver(post_delete, sender=RustDistribution) | ||
| def revoke_emptied_cargo_tokens(instance, **kwargs): | ||
| """Revoke tokens whose distribution scope was emptied by this deletion. | ||
|
|
||
| An empty scope means unrestricted, so a token that loses its last distribution would | ||
| silently widen to everything its owner can reach. Revoking it keeps the deletion from | ||
| granting the bearer reach the owner never delegated. | ||
| """ | ||
| token_pks = getattr(instance, "_scoped_cargo_token_pks", None) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hold up, do these objects only exist in memory? Would restarting pulp impact / prevent revocation from working properly? |
||
| if not token_pks: | ||
| return | ||
| RustCargoToken.objects.filter(pk__in=token_pks, distributions__isnull=True).delete() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| import socket | ||
|
|
||
| CRATES_IO_API_HOSTNAME = "https://" + socket.getfqdn() | ||
|
|
||
| DRF_ACCESS_POLICY = { | ||
| "dynaconf_merge_unique": True, | ||
| "reusable_conditions": ["pulp_rust.app.global_access_conditions"], | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cargo has
publish-newandpublish-updaterather than a single catchall scope. Can you explain why we're changing the scopings - is it because we don't currently track crate ownership? If we could keep it as close as possible to the upstream model (without trying to introduce crate ownership right now) that would be ideal.