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

Shankiyani/initial popup #181

Merged
merged 36 commits into from
Feb 21, 2023
Merged

Shankiyani/initial popup #181

merged 36 commits into from
Feb 21, 2023

Conversation

shankiyani
Copy link
Contributor

@shankiyani shankiyani commented Feb 6, 2023

This is the initial change to support a UI for JVS. A go template is used to display a form. On submission of this form, a basic validation is made against the input. If the user provides invalid data, an error message is shown and the user's inputs are maintained. Upon successful submission a script is executed to postMessage back to the target origin that triggered the initial call. This origin must exist within an allow list provided as an env variable.

@shankiyani shankiyani requested a review from verbanicm February 6, 2023 16:54
@shankiyani shankiyani self-assigned this Feb 6, 2023
Copy link
Contributor

@sethvargo sethvargo left a comment

Choose a reason for hiding this comment

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

A few things:

  • Should we have a more advanced stylesheet framework (e.g. scss or sass)? If not sass or scss, we should use CSS variables for things like colors and border radii.

  • Let's declare our browser support matrix for Bets Platform somewhere? I can be the approver. We don't need to support IE 7, but we should be forward about which browsers we're going to support. We cannot assume Chrome for all Bets.

  • Always prefer rem to px for responsive designs.

  • Prefer flexbox to float.

  • Use class and id selectors instead of styling elements directly. It provides more flexibility in the future.

I didn't review the Go code, but there's a lot of nuance in getting form parsing correct (and secure). Let me know when you want a review of the Go code.

assets/static/css/style.css Outdated Show resolved Hide resolved
assets/templates/index.gohtml Outdated Show resolved Hide resolved
assets/templates/index.gohtml Outdated Show resolved Hide resolved
assets/templates/index.gohtml Outdated Show resolved Hide resolved
assets/templates/index.gohtml Outdated Show resolved Hide resolved
assets/templates/index.gohtml Outdated Show resolved Hide resolved
pkg/ui/server.go Outdated Show resolved Hide resolved
shankiyani and others added 3 commits February 6, 2023 18:12
Co-authored-by: Seth Vargo <seth@sethvargo.com>
Signed-off-by: Shan Kiyani <shankiyani@google.com>
Co-authored-by: Seth Vargo <seth@sethvargo.com>
Signed-off-by: Shan Kiyani <shankiyani@google.com>
Co-authored-by: Seth Vargo <seth@sethvargo.com>
Signed-off-by: Shan Kiyani <shankiyani@google.com>
@verbanicm
Copy link

Should we have a more advanced stylesheet framework (e.g. scss or sass)? If not sass or scss, we should use CSS variables for things like colors and border radii.

I would rather use a frontend framework, otherwise I would prefer to use CSS variables for this use case.

Let's declare our browser support matrix for Bets Platform somewhere? I can be the approver. We don't need to support IE 7, but we should be forward about which browsers we're going to support. We cannot assume Chrome for all Bets.

Always prefer rem to px for responsive designs.

Prefer flexbox to float.

Use class and id selectors instead of styling elements directly. It provides more flexibility in the future.

Yes, all of this, we should probably create a doc for browser support and best practices for CSS/HTML.

This also leads into the frontend framework vs go template discussion and it will probably come down to use case.

assets/static/css/reset.css Outdated Show resolved Hide resolved
assets/static/js/index.js Outdated Show resolved Hide resolved
assets/templates/success.html.tmpl Outdated Show resolved Hide resolved
cmd/ui/main.go Show resolved Hide resolved
pkg/ui/server.go Outdated Show resolved Hide resolved
pkg/ui/server.go Outdated Show resolved Hide resolved
pkg/ui/server.go Outdated Show resolved Hide resolved
pkg/ui/server.go Outdated Show resolved Hide resolved
pkg/ui/server.go Outdated Show resolved Hide resolved
pkg/ui/server.go Outdated Show resolved Hide resolved
shankiyani and others added 6 commits February 7, 2023 21:14
Co-authored-by: Seth Vargo <seth@sethvargo.com>
Signed-off-by: Shan Kiyani <shan.a.raja@gmail.com>
Co-authored-by: Seth Vargo <seth@sethvargo.com>
Signed-off-by: Shan Kiyani <shan.a.raja@gmail.com>
@sethvargo
Copy link
Contributor

Should we have a more advanced stylesheet framework (e.g. scss or sass)? If not sass or scss, we should use CSS variables for things like colors and border radii.

I would rather use a frontend framework, otherwise I would prefer to use CSS variables for this use case.

CSS variables are fine with me

Let's declare our browser support matrix for Bets Platform somewhere? I can be the approver. We don't need to support IE 7, but we should be forward about which browsers we're going to support. We cannot assume Chrome for all Bets.

Always prefer rem to px for responsive designs.

Prefer flexbox to float.

Use class and id selectors instead of styling elements directly. It provides more flexibility in the future.

Yes, all of this, we should probably create a doc for browser support and best practices for CSS/HTML.

This also leads into the frontend framework vs go template discussion and it will probably come down to use case.

Yes, I agree we need a doc 😄

assets/static/images/favicons/browserconfig.xml Outdated Show resolved Hide resolved
assets/static/images/favicons/site.webmanifest Outdated Show resolved Hide resolved
assets/static/js/index.js Outdated Show resolved Hide resolved
assets/static/js/index.js Outdated Show resolved Hide resolved
assets/static/js/main.js Outdated Show resolved Hide resolved
pkg/ui/server_test.go Outdated Show resolved Hide resolved
pkg/ui/server_test.go Outdated Show resolved Hide resolved
pkg/ui/server_test.go Outdated Show resolved Hide resolved
pkg/ui/server_test.go Outdated Show resolved Hide resolved
pkg/ui/server_test.go Outdated Show resolved Hide resolved
shankiyani and others added 5 commits February 12, 2023 13:41
Co-authored-by: Seth Vargo <seth@sethvargo.com>
Signed-off-by: Shan Kiyani <shan.a.raja@gmail.com>
rename main script to success to match html file name
rename windowname attribute to window-name
move script tag inside body for both html files
proper indentation on xml and json
add copyright headers
use querySelector over addEventListener
use window for event listener
shorthand err check on config
make regex pattern a global variable
cmd/ui/main.go Outdated Show resolved Hide resolved
pkg/ui/config.go Outdated Show resolved Hide resolved
pkg/ui/server.go Outdated Show resolved Hide resolved
pkg/ui/server.go Outdated Show resolved Hide resolved
pkg/ui/server_test.go Outdated Show resolved Hide resolved
pkg/ui/server_test.go Outdated Show resolved Hide resolved
use logger instead of log
in tests want and got or sufficient
remove .flex-outer in css
pkg/ui/config.go Outdated Show resolved Hide resolved
@shankiyani
Copy link
Contributor Author

Thanks for the feedback so far @verbanicm, @sethvargo, and @yolocs. I did some refactoring to mimic the EN service so I hope its more aligned with expectations (still more improvements in upcoming PRs). I wonder if this is a good point to try to merge what we have so far. I want to follow up with the Remaining tasks in the next PR(s). Ill tackle Followups after that.

Remaining:

  • Setup IAP to get the authenticated user
  • Call the validation service and send the token back (currently hard coding a token which is being sent back to the calling application)
  • Write a walkthrough for a client library
  • Server side callback flow using a custom header

Followups:

  • Browser support matrix doc
  • CSS linter in the pkg repo
  • Better styling of the alert messages
  • More robust testing around the handler, maybe rework the current implementation to reflect how EN does forms (which also handles alert messages).

internal/project/context.go Outdated Show resolved Hide resolved
internal/project/util.go Show resolved Hide resolved
pkg/config/ui_config.go Outdated Show resolved Hide resolved
pkg/config/ui_config_test.go Outdated Show resolved Hide resolved
pkg/controller/controller.go Outdated Show resolved Hide resolved
pkg/controller/controller.go Outdated Show resolved Hide resolved
pkg/controller/controller_test.go Outdated Show resolved Hide resolved
pkg/render/assets.go Show resolved Hide resolved
pkg/render/renderer.go Outdated Show resolved Hide resolved
@shankiyani shankiyani marked this pull request as ready for review February 17, 2023 05:51
@shankiyani shankiyani requested review from a team, myurtoglu and sqin2019 February 17, 2023 05:51
@shankiyani
Copy link
Contributor Author

shankiyani commented Feb 17, 2023

A few things:

  • Should we have a more advanced stylesheet framework (e.g. scss or sass)? If not sass or scss, we should use CSS variables for things like colors and border radii.
  • [followup] Let's declare our browser support matrix for Bets Platform somewhere? I can be the approver. We don't need to support IE 7, but we should be forward about which browsers we're going to support. We cannot assume Chrome for all Bets.
  • Always prefer rem to px for responsive designs.
  • Prefer flexbox to float.
  • Use class and id selectors instead of styling elements directly. It provides more flexibility in the future.

I didn't review the Go code, but there's a lot of nuance in getting form parsing correct (and secure). Let me know when you want a review of the Go code.

All are either addressed or noted as followups

@myurtoglu myurtoglu removed their request for review February 17, 2023 19:45
@shankiyani shankiyani removed the request for review from sqin2019 February 17, 2023 21:28
// limitations under the License.

window.addEventListener("DOMContentLoaded", async () => {
const originElement = document.getElementById("origin");
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit, can be a follow-up. Always prefer document.querySelector.

}

// set values for the following hidden input elements, will be persisted to the next page
originElement.value = targetOrigin;
Copy link
Contributor

Choose a reason for hiding this comment

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

If originElement or windowElement are null, this will NPE.

Comment on lines +19 to +39
if (!targetOrigin) {
alert("You must pass a target origin from your application to successfully retrieve a token.")
window.close()
} else if (!windowName) {
alert("You must pass a window name from your application to successfully retrieve a token.")
} else if (!token) {
alert("Something went wrong, unable to retrieve a token.")
} else {
window.opener.postMessage(
JSON.stringify({
// notify the requestor of the window name that was provided,
// client should check this as a sanity check
source: windowName,
payload: {
token,
},
}),
targetOrigin
);
}
window.close();
Copy link
Contributor

Choose a reason for hiding this comment

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

This flow is a bit weird. I think it'd be better to always return short:

Suggested change
if (!targetOrigin) {
alert("You must pass a target origin from your application to successfully retrieve a token.")
window.close()
} else if (!windowName) {
alert("You must pass a window name from your application to successfully retrieve a token.")
} else if (!token) {
alert("Something went wrong, unable to retrieve a token.")
} else {
window.opener.postMessage(
JSON.stringify({
// notify the requestor of the window name that was provided,
// client should check this as a sanity check
source: windowName,
payload: {
token,
},
}),
targetOrigin
);
}
window.close();
if (!targetOrigin) {
alert("You must pass a target origin from your application to successfully retrieve a token.")
window.close()
return
}
if (!windowName) {
alert("You must pass a window name from your application to successfully retrieve a token.")
window.close()
return
}
if (!token) {
alert("Something went wrong, unable to retrieve a token.")
window.close()
return
}
window.opener.postMessage(
JSON.stringify({
// notify the requestor of the window name that was provided,
// client should check this as a sanity check
source: windowName,
payload: {
token,
},
}),
targetOrigin
);
window.close();

This can be a follow-up fix.

Comment on lines +106 to +110
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
lookuper := envconfig.MapLookuper(tc.envs)
Copy link
Contributor

Choose a reason for hiding this comment

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

Style nit: can be a follow-up (throughout all tests, just marking once here):

Suggested change
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
lookuper := envconfig.MapLookuper(tc.envs)
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
lookuper := envconfig.MapLookuper(tc.envs)

//
// The buffers are fetched via a sync.Pool to reduce allocations and improve
// performance.
func (r *Renderer) RenderHTMLStatus(w http.ResponseWriter, code int, tmpl string, data interface{}) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: prefer any over interface{} in modern Go. This can be a follow-up.

Comment on lines +57 to +58
ctrl := controller.New(s.h)
mux.Handle("/popup", ctrl.HandlePopup(s.allowlist))
Copy link
Contributor

Choose a reason for hiding this comment

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

In general, I would expect all "setup" to be in the controller. So the allowlist would be passed to controller.New. It's not a huge deal and can be a follow-up.

@shankiyani shankiyani merged commit 951d428 into main Feb 21, 2023
@shankiyani shankiyani deleted the shankiyani/initial-popup branch February 21, 2023 20:29
sqin2019 pushed a commit that referenced this pull request Apr 6, 2023
* initial go template and styling

* first pass at the server

* send form labels to template

* add reset button

* remove print statement in server file, label todos

* Update assets/templates/index.gohtml

Co-authored-by: Seth Vargo <seth@sethvargo.com>
Signed-off-by: Shan Kiyani <shankiyani@google.com>

* Update assets/templates/index.gohtml

Co-authored-by: Seth Vargo <seth@sethvargo.com>
Signed-off-by: Shan Kiyani <shankiyani@google.com>

* Update assets/templates/index.gohtml

Co-authored-by: Seth Vargo <seth@sethvargo.com>
Signed-off-by: Shan Kiyani <shankiyani@google.com>

* rename template file, format css file

* set hidden field values via script tag

* Add success page html, used for postMessage on page load

* redo the template, dont use floats in css

* run gofumpt and set timeouts for http server

* Update pkg/ui/server.go

Co-authored-by: Seth Vargo <seth@sethvargo.com>
Signed-off-by: Shan Kiyani <shan.a.raja@gmail.com>

* Update pkg/ui/server.go

Co-authored-by: Seth Vargo <seth@sethvargo.com>
Signed-off-by: Shan Kiyani <shan.a.raja@gmail.com>

* clean up css

* alert user when context is missing

* add additional meta tags, format all files

* turn off detect indentation, set to 2 spaces

* Address comments, add config.go

* add favicons

* format reset.css and remove unused struct

* add a forbidden page

* Support allowlist env variable

* Add some tests for validation methods

* Update pkg/ui/server.go

Co-authored-by: Seth Vargo <seth@sethvargo.com>
Signed-off-by: Shan Kiyani <shan.a.raja@gmail.com>

* add new line to reset.css

rename main script to success to match html file name
rename windowname attribute to window-name
move script tag inside body for both html files
proper indentation on xml and json
add copyright headers
use querySelector over addEventListener
use window for event listener
shorthand err check on config
make regex pattern a global variable

* add testing for config file

* add local ip check, remove helper handler, refactor tests

* add _head.html.tmpl, rename main template file for popup

* add Validate to cfg interface

use logger instead of log
in tests want and got or sufficient
remove .flex-outer in css

* refactor, inspired by EN service

* resolve bugs from manual testing

* run the linter

* add basic testing for the popup handler

* address comments, add a readme

---------

Signed-off-by: Shan Kiyani <shankiyani@google.com>
Signed-off-by: Shan Kiyani <shan.a.raja@gmail.com>
Co-authored-by: Seth Vargo <seth@sethvargo.com>
verbanicm pushed a commit that referenced this pull request Jun 14, 2023
* initial go template and styling

* first pass at the server

* send form labels to template

* add reset button

* remove print statement in server file, label todos

* Update assets/templates/index.gohtml

Co-authored-by: Seth Vargo <seth@sethvargo.com>
Signed-off-by: Shan Kiyani <shankiyani@google.com>

* Update assets/templates/index.gohtml

Co-authored-by: Seth Vargo <seth@sethvargo.com>
Signed-off-by: Shan Kiyani <shankiyani@google.com>

* Update assets/templates/index.gohtml

Co-authored-by: Seth Vargo <seth@sethvargo.com>
Signed-off-by: Shan Kiyani <shankiyani@google.com>

* rename template file, format css file

* set hidden field values via script tag

* Add success page html, used for postMessage on page load

* redo the template, dont use floats in css

* run gofumpt and set timeouts for http server

* Update pkg/ui/server.go

Co-authored-by: Seth Vargo <seth@sethvargo.com>
Signed-off-by: Shan Kiyani <shan.a.raja@gmail.com>

* Update pkg/ui/server.go

Co-authored-by: Seth Vargo <seth@sethvargo.com>
Signed-off-by: Shan Kiyani <shan.a.raja@gmail.com>

* clean up css

* alert user when context is missing

* add additional meta tags, format all files

* turn off detect indentation, set to 2 spaces

* Address comments, add config.go

* add favicons

* format reset.css and remove unused struct

* add a forbidden page

* Support allowlist env variable

* Add some tests for validation methods

* Update pkg/ui/server.go

Co-authored-by: Seth Vargo <seth@sethvargo.com>
Signed-off-by: Shan Kiyani <shan.a.raja@gmail.com>

* add new line to reset.css

rename main script to success to match html file name
rename windowname attribute to window-name
move script tag inside body for both html files
proper indentation on xml and json
add copyright headers
use querySelector over addEventListener
use window for event listener
shorthand err check on config
make regex pattern a global variable

* add testing for config file

* add local ip check, remove helper handler, refactor tests

* add _head.html.tmpl, rename main template file for popup

* add Validate to cfg interface

use logger instead of log
in tests want and got or sufficient
remove .flex-outer in css

* refactor, inspired by EN service

* resolve bugs from manual testing

* run the linter

* add basic testing for the popup handler

* address comments, add a readme

---------

Signed-off-by: Shan Kiyani <shankiyani@google.com>
Signed-off-by: Shan Kiyani <shan.a.raja@gmail.com>
Co-authored-by: Seth Vargo <seth@sethvargo.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

6 participants