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

Add publicly visible env vars #7891

Merged
merged 26 commits into from Mar 25, 2021

Conversation

flying-sheep
Copy link
Contributor

@flying-sheep flying-sheep commented Feb 4, 2021

I have no idea what I’m doing but maybe this eventually fixes #7413

Also IDK how to restructure the API to support this.

Since Versions are always associated with a Project, maybe the better abstraction would be to add a environment_variables property to the Version class. That way we wouldn’t have to do all=version.type != EXTERNAL in two places but could derive it in the property body.

@flying-sheep
Copy link
Contributor Author

Huh, why is the field not found? I’m adding it in the migration!

Copy link
Member

@humitos humitos left a comment

Choose a reason for hiding this comment

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

Hi, thanks for the PR!

I left some initial comments about this PR. We may also need to update the APIv3 and its documentation to expose .public field

I also think that at some point we will want to decide if the env variable will be available only in PRIVATE or PUBLIC versions.

readthedocs/projects/models.py Outdated Show resolved Hide resolved
readthedocs/projects/models.py Outdated Show resolved Hide resolved
readthedocs/projects/models.py Outdated Show resolved Hide resolved
@flying-sheep
Copy link
Contributor Author

ha! tests work. why doesn’t the migration check though? the output doesn’t speak to me

Copy link
Member

@stsewd stsewd left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

So, in the build process we don't have direct access to the db for security reasons. Instead we get the information we need for the build from the API (v2).

As you have already seen, we use a wrapper class for data returned from the API.

class APIProject(Project):

To be able to pass only a subset of env variables into the build process (public env vars),
we need to be able to exclude them from the API first.

@stsewd
Copy link
Member

stsewd commented Feb 9, 2021

ha! tests work. why doesn’t the migration check though? the output doesn’t speak to me

That's because you changed the model but didn't update the migration, you can delete the migration and generate it again to have it match the changes.

@flying-sheep
Copy link
Contributor Author

flying-sheep commented Feb 11, 2021

OK, done!

Does anything need to be done to have a checkbox to toggle an env var between private and public?

@stsewd
Copy link
Member

stsewd commented Feb 15, 2021

@flying-sheep as mentioned in my comment (#7891 (review)), you'll need to change the API in order to make this work. The privacy level should be returned in the API, so we can filter them from the builders.

def get_environment_variables(self, obj):
return {
variable.name: variable.value
for variable in obj.environmentvariable_set.all()
}

@flying-sheep
Copy link
Contributor Author

flying-sheep commented Feb 16, 2021

OK!

So the tests pass for ce7dc1f, but if we want db5d9c7, I need help to make the serializer call it with the correct parameter.

Or we could change it so environment_variables is a property again and evaluates to {"VAR_NAME": {"value": "some value", "public": false}}

@flying-sheep flying-sheep changed the title First attempt to add public env vars Add publicly visible env vars Feb 16, 2021
@stsewd
Copy link
Member

stsewd commented Feb 17, 2021

So the tests pass for ce7dc1f, but if we want db5d9c7, I need help to make the serializer call it with the correct parameter.

Not sure to understand. I think we want to set it to public_only=True by default. The API response is filtered in

def environment_variables(self, *, public_only):
return {
name: spec['value']
for name, spec in self._environment_variables.items()
if spec['public'] or not public_only
}

We need to add some tests here for public env vars. And, since the new field is nullable, we need to change the form like this

# Remove the nullable option from the form
self.fields['analytics_disabled'].widget = forms.CheckboxInput()
self.fields['analytics_disabled'].empty_value = False

@stsewd
Copy link
Member

stsewd commented Feb 17, 2021

Let me know if you need more help here, thanks for taking this :)

Copy link
Member

@humitos humitos left a comment

Choose a reason for hiding this comment

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

@flying-sheep This is looking good to me! We are close to have it merged 😄

Let us know if there is something else you need from our side to wrap up this PR.

readthedocs/rtd_tests/tests/test_api.py Show resolved Hide resolved
@flying-sheep
Copy link
Contributor Author

All done!

@flying-sheep
Copy link
Contributor Author

Ha, got it! The Admin API now still returns all env variables, by calling the method with public_only=False

@flying-sheep flying-sheep requested a review from stsewd March 1, 2021 12:28
@flying-sheep
Copy link
Contributor Author

All done!

Copy link
Member

@stsewd stsewd left a comment

Choose a reason for hiding this comment

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

Everything looks good to me. I'm not sure how to improve the UI for the detail view, maybe using a read-only form? This is how it looks like right now

Screenshot_2021-03-09 Environment Variables Read the Docs

readthedocs/doc_builder/python_environments.py Outdated Show resolved Hide resolved
readthedocs/projects/tasks.py Outdated Show resolved Hide resolved
@stsewd
Copy link
Member

stsewd commented Mar 10, 2021

Ended up doing this with a readonly form. Not sure about how to handle the value from private env vars. Maybe show **** instead of an empty value? Or maybe remove the field?

Screenshot_2021-03-10 Environment Variables Read the Docs
Screenshot_2021-03-10 Environment Variables Read the Docs(1)

@stsewd stsewd requested a review from a team March 10, 2021 21:23
@flying-sheep
Copy link
Contributor Author

flying-sheep commented Mar 23, 2021

👋 Could anyone please have a look? My company could really use this.

@humitos
Copy link
Member

humitos commented Mar 23, 2021

Maybe show **** instead of an empty value? Or maybe remove the field?

@stsewd I think it's better if we show stars so we keep the message "The value isn't show for security reasons".

Copy link
Member

@humitos humitos left a comment

Choose a reason for hiding this comment

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

I think this is fine.

I'm not sold on showing the value of the environment variable in any case. CircleCI does not show it either. It shows 4 stars and the last 5 chars of the value.

Regarding the UI, we could copy the pattern we have in other places and only have a listing page (without detail page per variable) with the "Delete" button at the right of each element.

The logic is fine to me and I think we can merge this PR. The UI is going to change in the new theme anyways and I think that @agjohnson already built these pages; so worth to ping him here as well.

It seems that some tests are failing. We should take a look at them as well.

Comment on lines 768 to 770
# Remove the nullable option from the form
self.fields['public'].widget = forms.CheckboxInput()
self.fields['public'].empty_value = False
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we write a data migration that set public=False to all the existing ones instead?

Copy link
Member

Choose a reason for hiding this comment

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

yeah, but we need to do the migration in the next deploy so we don't have downtime. There are a lot of these missing a migration, may add a PR migrating all of them

readthedocs/projects/forms.py Outdated Show resolved Hide resolved
public = models.BooleanField(
_('Public'),
default=False,
null=True,
Copy link
Member

Choose a reason for hiding this comment

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

I think it's better if we write a data migration instead (we can remove the form code as well). This way the data will be valid in our DB.

readthedocs/projects/views/private.py Outdated Show resolved Hide resolved
@stsewd
Copy link
Member

stsewd commented Mar 23, 2021

Regarding the UI, we could copy the pattern we have in other places and only have a listing page (without detail page per variable) with the "Delete" button at the right of each element.

Yeah, that option is good as well, but the value won't be shown, which I think is fine.

@stsewd
Copy link
Member

stsewd commented Mar 23, 2021

I went ahead and deleted the detail view, the delete button is on the listing now.

@stsewd
Copy link
Member

stsewd commented Mar 23, 2021

@humitos let me know what you think now, I can add this to https://github.com/orgs/readthedocs/projects/9 to track the template changes

@stsewd stsewd requested a review from humitos March 23, 2021 17:23
Copy link
Member

@humitos humitos left a comment

Choose a reason for hiding this comment

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

@flying-sheep @stsewd looks good to me! 🎉 Thanks! 🙏

@stsewd Please, create a note in the "New theme" project so we can track this.

@stsewd stsewd merged commit 9011005 into readthedocs:master Mar 25, 2021
@stsewd
Copy link
Member

stsewd commented Mar 25, 2021

@flying-sheep thanks for working on this! This should be live by next Tuesday.

@flying-sheep flying-sheep deleted the feature/public-vars branch March 26, 2021 12:53
@flying-sheep
Copy link
Contributor Author

Thank you for helping me figure it all out! How does the rollout work for rtd.org vs rtd.com? Do they get updated in unison?

@stsewd
Copy link
Member

stsewd commented Mar 26, 2021

Thank you for helping me figure it all out! How does the rollout work for rtd.org vs rtd.com? Do they get updated in unison?

yes, we deploy them the same day, first rtd.org and if everything is good we move to rtd.com.

@humitos
Copy link
Member

humitos commented Mar 30, 2021

This is already deployed and it's working.

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

Successfully merging this pull request may close these issues.

Set environment variables in PR builds of private repos
3 participants