-
Notifications
You must be signed in to change notification settings - Fork 47
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
Remove target input from form if its fixed, add ability to share targ… #1142
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from rest_framework import serializers | ||
from tom_dataproducts.sharing import get_sharing_destination_options | ||
from tom_targets.sharing import share_target_and_all_data | ||
from tom_targets.fields import TargetFilteredPrimaryKeyRelatedField | ||
from tom_targets.models import PersistentShare, Target | ||
|
||
|
||
class PersistentShareSerializer(serializers.ModelSerializer): | ||
destination = serializers.ChoiceField( | ||
choices=get_sharing_destination_options(include_download=False), required=True | ||
) | ||
target = TargetFilteredPrimaryKeyRelatedField(queryset=Target.objects.all(), required=True) | ||
share_existing_data = serializers.BooleanField(default=False, required=False, write_only=True) | ||
|
||
class Meta: | ||
model = PersistentShare | ||
fields = ('id', 'target', 'destination', 'user', 'created', 'share_existing_data') | ||
|
||
def create(self, validated_data): | ||
shared_existing_data = validated_data.pop('share_existing_data', None) | ||
if shared_existing_data: | ||
sharing_feedback = share_target_and_all_data(validated_data['destination'], validated_data['target']) | ||
if 'ERROR' in sharing_feedback.upper(): | ||
raise serializers.ValidationError( | ||
f"Failed to share existing data of target {validated_data['target'].name}: {sharing_feedback}" | ||
) | ||
|
||
return super().create(validated_data) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,71 @@ | |
from django.core.exceptions import ImproperlyConfigured | ||
|
||
from tom_targets.serializers import TargetSerializer | ||
from tom_dataproducts.sharing import get_destination_target | ||
from tom_targets.models import PersistentShare | ||
from tom_dataproducts.sharing import (check_for_share_safe_datums, share_data_with_tom, | ||
get_destination_target, sharing_feedback_converter) | ||
from tom_dataproducts.models import ReducedDatum | ||
from tom_dataproducts.alertstreams.hermes import publish_to_hermes, BuildHermesMessage | ||
|
||
|
||
def share_target_and_all_data(share_destination, target): | ||
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. This is silently breaking for me. 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. Thanks for checking that - I had thought submitter would be autoset by Hermes but it actually doesn't do that for outgoing messages, only incoming. So I've set the submitter to be the 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. Also, the error should be mapped through now so it should stop the creation of a persistent share and show the error on screen if you have a problem when sharing all existing data. If it succeeds, nothing is shown but the persistent share is created and will show up, so that is the signal that it succeeded in sharing existing data too. 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. I'm still getting a silent failure for as well as missing telescope/instrument 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. Hmm I'll look into the error not making it through... What do you mean by missing telescope/instrument though? Is it stored in your datums value dict as 'telescope' and 'instrument'? 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. yeah, not having the telescope/instrument in my data is just another way my hermes message can be rejected. The issue isn't that, it's just one of the two ways I tried to get errors back from hermes. 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. Ah okay I understand what were saying now 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. I've updated it again, and the errors are definitely coming through now! Sorry the error feedback handler stuff had me confused a bit. |
||
""" | ||
Given a sharing destination, shares the target and all its current dataproducts | ||
with that destination. Will raise an Exception is any portion of sharing fails. | ||
:param share_destination: String sharing destination from the DATA_SHARING setting | ||
:param target: Target instance that should be shared with all its data | ||
""" | ||
if 'HERMES' in share_destination.upper(): | ||
hermes_topic = share_destination.split(':')[1] | ||
destination = share_destination.split(':')[0] | ||
filtered_reduced_datums = check_for_share_safe_datums( | ||
destination, ReducedDatum.objects.filter(target=target), topic=hermes_topic) | ||
sharing = getattr(settings, "DATA_SHARING", {}) | ||
tom_name = f"{getattr(settings, 'TOM_NAME', 'TOM Toolkit')}" | ||
message = BuildHermesMessage(title=f"Setting up continuous sharing for {target.name} from " | ||
f"{tom_name}.", | ||
authors=sharing.get('hermes', {}).get('DEFAULT_AUTHORS', None), | ||
submitter='', | ||
message='', | ||
topic=hermes_topic | ||
) | ||
return sharing_feedback_converter(publish_to_hermes(message, filtered_reduced_datums)) | ||
else: | ||
response = share_target_with_tom(share_destination, {'target': target}) | ||
response_feedback = sharing_feedback_converter(response) | ||
if 'ERROR' in response_feedback.upper(): | ||
return response_feedback | ||
return sharing_feedback_converter(share_data_with_tom(share_destination, None, target_id=target.id)) | ||
|
||
|
||
def continuous_share_data(target, reduced_datums): | ||
""" | ||
Triggered when new ReducedDatums are created. | ||
Shares those ReducedDatums to the sharing destination of any PersistentShares on the target. | ||
:param target: Target instance that these reduced_datums belong to | ||
:param reduced_datums: list of ReducedDatum instances to share | ||
""" | ||
persistentshares = PersistentShare.objects.filter(target=target) | ||
for persistentshare in persistentshares: | ||
share_destination = persistentshare.destination | ||
reduced_datum_pks = [rd.pk for rd in reduced_datums] | ||
if 'HERMES' in share_destination.upper(): | ||
hermes_topic = share_destination.split(':')[1] | ||
destination = share_destination.split(':')[0] | ||
filtered_reduced_datums = check_for_share_safe_datums( | ||
destination, ReducedDatum.objects.filter(pk__in=reduced_datum_pks), topic=hermes_topic) | ||
sharing = getattr(settings, "DATA_SHARING", {}) | ||
tom_name = f"{getattr(settings, 'TOM_NAME', 'TOM Toolkit')}" | ||
message = BuildHermesMessage(title=f"Updated data for {target.name} from " | ||
f"{tom_name}.", | ||
authors=sharing.get('hermes', {}).get('DEFAULT_AUTHORS', None), | ||
submitter=tom_name, | ||
message='', | ||
topic=hermes_topic | ||
) | ||
publish_to_hermes(message, filtered_reduced_datums) | ||
else: | ||
share_data_with_tom(share_destination, None, None, None, selected_data=reduced_datum_pks) | ||
|
||
|
||
def share_target_with_tom(share_destination, form_data, target_lists=()): | ||
|
Check warning
Code scanning / CodeQL
Information exposure through an exception Medium
Copilot Autofix AI 12 days ago
To fix the problem, we need to modify the error message to be more generic and avoid exposing sensitive information. Instead of including the target's name and the detailed sharing feedback in the error message, we should log the detailed error message on the server and return a generic error message to the user.
create
method inPersistentShareSerializer
to log the detailed error message and raise a genericValidationError
.