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

Update UI Config passing to not use an inline script #8645

Merged
merged 3 commits into from
Sep 15, 2020
Merged

Conversation

banks
Copy link
Member

@banks banks commented Sep 10, 2020

This PR slightly modifies the way we pass through runtime configuration to the UI.

It's a precursor/cleanup before some larger changes to UI config.

The main motivation is to remove the inline script which should allow tighter CSP policies to be used with the UI.

Overall, this is both simpler and slightly more hacky than before however it removed the need for inline script and hopefulyl allows us to easily expand the number of config options we pass to the UI significantly without needing changes in multipele JS files and go files just to plumb template placeholders through.

There is a small Enterprise PR needed once this is merged to rename the method.

@banks banks requested review from a team and johncowen September 10, 2020 11:31
@banks banks added this to the 1.9 milestone Sep 10, 2020
@banks
Copy link
Member Author

banks commented Sep 10, 2020

@johncowen I rebuilt the assetfs by running make ui after getting the JS changes. I assume that is the right thing to do to get the index.html changes into Consul but let me know if there are steps I missed!

@johncowen
Copy link
Contributor

LGTM @banks, I checked everything during dev is all ok (we are still able to change these settings via Web Inspector), but I'll leave approval for go folks

Thanks for doing this, looks much cleaner from the frontend side.

agent/http.go Outdated Show resolved Hide resolved
@dnephin dnephin added the theme/ui Anything related to the UI label Sep 10, 2020
Copy link
Contributor

@dnephin dnephin left a comment

Choose a reason for hiding this comment

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

Couple thoughts for minor improvements (not blocking), and one question

// writing out the new version into a buffer and then serving file reads from
// that. It assumes you are modifying a real file and presents the actual file's
// info when queried.
type bufferedFile struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

http.go is quite large and seems to be mostly the "framework" we use for the http API. It might be nice to move this type to a separate file at some point.

Comment on lines +190 to +192
// We want to remove the first and last chars which will be the { and } since
// we are injecting these variabled into the middle of an existing object.
bs = bytes.Trim(bs, "{}")
Copy link
Contributor

Choose a reason for hiding this comment

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

minor: I guess we could probably remove the need for this trim by making the replacement string include the {} and moving the comment around. Possibly even putting it into a separate file, although I'm not sure how easy that is.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm I don't think so - we are trimming the JSON value that is being inserted here.

The KV pair being matched appears somewhere in the middle of a large JSON blob that contains many other variables that ember sets already.

But I'm not sure what you meant about a separate file so perhaps I'm misunderstanding something?

Copy link
Contributor

@dnephin dnephin Sep 10, 2020

Choose a reason for hiding this comment

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

Oh ya, I didn't explain this well and thinking through it again my idea changed slightly.

Roughly I was thinking that some other file (module?) could set a CONSUL_INJECTED_UI_SETTINGS (or whatever we want to call it) variable to nil, and environment.js could use that variable as:

ENV = Object.assign({}, ENV, inject.CONSUL_INJECTED_UI_SETTINGS),

where inject is the name of the module that contains the other file (although I don't fully understand how modules work here, so I'm not sure if we can namespace the variable like that).

Then the replacement could replace inject.CONSUL_INJECTED_UI_SETTINGS. The replacement string would not contain any escaped characters (I think), and the source of the variable wouldn't be replaced because it wouldn't include inject..

That would remove the need to trim {} because the content would be an entire JSON object, not just the values, and it would make it much easier to associate these two strings because you could find or grep for them without having to escape things.

Copy link
Member Author

@banks banks Sep 15, 2020

Choose a reason for hiding this comment

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

Yeah there is certainly room to make this cleaner but I don't think what you're suggesting works. The environment.js is evaluated at Ember build time. The end result is the index.html that we store in assetFS which has this compiled, encoded JSON object in. We can't inject directly into the runtime JS without doing something more complicated like trying to intercept other JS file requests on the HTTP server and inject at a higher level where the JS has already been minified etc. This seems less bad for now and an improvement over what we were doing before at least!

As this is blocking getting something to work I'd prefer to defer an even larger refactor of how we pass variables to JS - there is already a big TODO in the JS side to change how we pass all of this since it should ideally be in the APP config section and handled in the Application.initialize rather than using env() anyway but that's too much for our frontend engineers to take on in this release cycle.

Copy link
Contributor

Choose a reason for hiding this comment

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

If we wanted to inject these things at runtime I think we would probably query them via the API, instead of injecting them into a file, right? I really like that idea, but I assumed there was some reason that wouldn't work. Do we need these settings in order to make API requests?

I think what I was suggesting isn't much different from what you have, but I don't know ember well enough to know if it is possible.

agent/http.go Outdated Show resolved Hide resolved
@banks
Copy link
Member Author

banks commented Sep 15, 2020

@dnephin thanks for catching the typo! Given your 'not blocking' comment and the rationale about a larger refactor above, are you happy to merge this as it is for now or do you think it needs more discussion or another opinion?

Copy link
Contributor

@dnephin dnephin left a comment

Choose a reason for hiding this comment

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

LGTM, seems like a solid improvement.

Finding the replacement string may be difficult because of the escaping, but hopefully we won't need to do that often.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme/ui Anything related to the UI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants