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

Added support for browser-type and browser-executable-path #816

Merged
merged 14 commits into from
Nov 11, 2023

Conversation

eliat123
Copy link
Contributor

@eliat123 eliat123 commented May 3, 2022

Improvement details:

This is the implementation to #815

An additional saml2aws command line argument & configuration to allow to the specification of BrowserTypeLaunchOptions.Channel for 'Browser' idp-provider.

Usage example:

  • This allows opening a chrome browser (instead of Chromium) when running saml2aws.
$ saml2aws configure --idp-provider=Browser --browser-type=chrome --username=${USERNAME} --region=${REGION} --url=${URL} --role=${AWS_ROLE} --idp-account=${IDP_ACCOUNT} --profile=${IDP_ACCOUNT} --skip-prompt
$ ${SAML2AWS} login --idp-account=${IDP_ACCOUNT} --profile=${IDP_ACCOUNT} --skip-prompt

@eliat123 eliat123 changed the title added support for browser-type Added support for browser-type May 3, 2022
@eliat123
Copy link
Contributor Author

@wolfeidau If possible, can I kindly ask you to have a look at this addition to the 'Browser' provider and let me know what you think.

@efernandes-dev-ops
Copy link

@eliat123 I believe this might also resolve 729.

@MatteoJoliveau
Copy link

Hi, I would like to push this PR because it would allow NixOS users like me to use the Browser provider. In NixOS you cannot simply download a random executable from the internet and run it because it doesn't use the Linux Filesystem Hierarchy Standard (FHS), therefore the setup script and subsequently downloaded chromium (after I patched the script by hand) won't work (see screenshots)

image
image

As a rule of thumb downloading executables to the user machine is not a really good practice. I prefer having complete control over what is in my system and installing software through the package manager.

The other thing that bothers me with the downloaded chrome executable is that it is run in Incognito mode, meaning I have to re-authenticate to my Google account every time. If saml2aws were to use the system browser (firefox in my case) it could reuse the active login session.

@mlai
Copy link

mlai commented Jul 11, 2022

Looks like a good feature. Can you help to review and merge?

@efernandes-dev-ops
Copy link

@MatteoJoliveau We have the same usecase with regards to the chrome executable running in incognito mode which is a blocker for us in adopting this atm.

@eliat123
Copy link
Contributor Author

@wolfeidau kind reminder.
If possible, can I kindly ask you to have a look at this addition to the 'Browser' provider and let me know what you think?

@TwiN
Copy link

TwiN commented Jan 9, 2023

That'd definitely be nice, because the chromium browser is launched with incognito, which forces users to re-enter their credentials every single time.

@eliat123 any chance you can pull the recent changes from the base branch so that @wolfeidau can review & merge it in one go when they have time?

@eliat123
Copy link
Contributor Author

eliat123 commented Jan 9, 2023

Thank you @TwiN.
As requested, I pulled the recent changes from the upstream branch so that @wolfeidau can review & merge it in one go when they have time.

@@ -70,6 +70,7 @@ func main() {
app.Flag("config", "Path/filename of saml2aws config file (env: SAML2AWS_CONFIGFILE)").Envar("SAML2AWS_CONFIGFILE").StringVar(&commonFlags.ConfigFile)
app.Flag("idp-account", "The name of the configured IDP account. (env: SAML2AWS_IDP_ACCOUNT)").Envar("SAML2AWS_IDP_ACCOUNT").Short('a').Default("default").StringVar(&commonFlags.IdpAccount)
app.Flag("idp-provider", "The configured IDP provider. (env: SAML2AWS_IDP_PROVIDER)").Envar("SAML2AWS_IDP_PROVIDER").EnumVar(&commonFlags.IdpProvider, "Akamai", "AzureAD", "ADFS", "ADFS2", "Browser", "GoogleApps", "Ping", "JumpCloud", "Okta", "OneLogin", "PSU", "KeyCloak", "F5APM", "Shibboleth", "ShibbolethECP", "NetIQ", "Auth0")
app.Flag("browser-type", "The configured browser type when the IDP provider is set to Browser. (env: SAML2AWS_BROWSER_TYPE)").Envar("SAML2AWS_BROWSER_TYPE").EnumVar(&commonFlags.BrowserType, "chrome", "chrome-beta", "chrome-dev", "chrome-canary", "msedge", "msedge-beta", "msedge-dev", "msedge-canary")
Copy link

Choose a reason for hiding this comment

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

Is there a reason why there's no support for Firefox? 👀

Copy link

@zemberdotnet zemberdotnet Jan 10, 2023

Choose a reason for hiding this comment

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

browser, err := pw.Chromium.Launch(launchOptions)
	if err != nil {
		return "", err
	}

It looks like it always launches a Chromium-based browser, so I'd guess that's the reason why.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@TwiN, @zemberdotnet Added Firefox.

The original values were set based on the comment of the supported values by the playwright Channel; see: https://github.com/playwright-community/playwright-go/blob/8e8f670b5fa7ba5365ae4bfc123fea4aac359763/generated-structs.go#L441-L444

However, it totally made sense to me to add this capability, so I added support for Firefox and Webkit. (msedge is also supported using Chromium which is the default).

Copy link

@zemberdotnet zemberdotnet Jan 11, 2023

Choose a reason for hiding this comment

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

@eliat123 I looked at the code and it looks like the value from the browser-type flag isn't getting passed down when running the login command.

The NewSAMLClient gets constructed here on 78:

loginDetails, err := resolveLoginDetails(account, loginFlags)
if err != nil {
log.Printf("%+v", err)
os.Exit(1)
}
logger.WithField("idpAccount", account).Debug("building provider")
provider, err := saml2aws.NewSAMLClient(account)

And the account that gets passed in is constructed here in buildIdpAccount where I assume we want to make sure the BrowserType property is set.

account, err := buildIdpAccount(loginFlags)
if err != nil {
return errors.Wrap(err, "Error building login details.")
}

I may have a different use case than you, so not sure if this is outside the scope of your PR or not.

For reference I was running this command:

saml2aws login --browser-type=chrome

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@zemberdotnet thank you for noticing that.

The reason for this behaviour is that the commonFlags can be passed both in the configure or other steps such as login. In my case, i have set the --browser-type in the configure and then it was available in the login.

To support providing the flag in the login command, I removed the additional check for commonFlags.IdpProvider == "Browser" since the BrowserType is only used by the Browser Provider, the functionality is still the same.

Please check it again as it should work now.

@eliat123 eliat123 requested review from TwiN and zemberdotnet and removed request for TwiN and zemberdotnet January 10, 2023 22:16
@eliat123 eliat123 requested review from zemberdotnet and TwiN and removed request for TwiN and zemberdotnet January 11, 2023 20:33
@merusso
Copy link

merusso commented Jan 22, 2023

Hi, I would like to push this PR because it would allow NixOS users like me to use the Browser provider. In NixOS you cannot simply download a random executable from the internet and run it because it doesn't use the Linux Filesystem Hierarchy Standard (FHS), therefore the setup script and subsequently downloaded chromium (after I patched the script by hand) won't work (see screenshots)

@MatteoJoliveau, does this PR fix the issue you described? Skimming the code, I'm thinking it still uses a playwright browser, something that was downloaded to the playwright cache directory. It just allows users to change the playwright browser being used instead of only using Chromium.

@MadOx710
Copy link

MadOx710 commented Feb 6, 2023

Definitely looking forward to Provider Browser support outside of Chromium. Context-Aware policies in Google Workspace leverages the Endpoint Verification Chrome plugin, and currently no Provider option for saml2aws works when Context-Aware policies are enabled.

@eliat123
Copy link
Contributor Author

eliat123 commented Mar 3, 2023

@MadOx710, @MatteoJoliveau, @zemberdotnet, @TwiN, looking at the previous comments, are we ok with the playwright browser for now? or do want to wait for non-downloadable browser support?
If we are ok to continue with the first approach, let's merge this PR.
Otherwise, is there a feature request for non-downloadable browser support? Do you have a GitHub issue with its requirements? Is someone actively working on it?

I would be happy to contribute, but I would like to know the direction of this project.

@eliat123
Copy link
Contributor Author

@GuillaumeRoss playwright-go comes with "chromium," "firefox", "webkit", see: https://playwright-community.github.io/playwright-go/

This means that If you don't have one of the three browsers installed, it will download the browser for you and then perform the browser IDP authentication.

I have also added the ability to set the --browser-executable-path.
This means that playwright-go will execute the browser at your local path instead of downloading the browser, this means that you can configure to use the browser of your choice.

In addition, playwright-go supports a set of predefined browser types that are not downloadable, this means that if you have already installed that browser and set the --browser-type to it it will use the installed browser.
The list for that is one of the predefined list:

"chrome", "chrome-beta", "chrome-dev", "chrome-canary", "msedge", "msedge-beta", "msedge-dev", "msedge-canary"

I have tested this with chrome, chrome-canary, msedge and verified that it uses the ExecutablePath instead of the default one.

I believe this resolves most of the issues in #729

@mapkon @gliptak please review

Copy link
Contributor

@mapkon mapkon left a comment

Choose a reason for hiding this comment

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

This looks on. Does that mean that we should close #815?

Copy link
Contributor

@mapkon mapkon left a comment

Choose a reason for hiding this comment

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

Please checkout the linting errors and fix them.

@codecov-commenter
Copy link

Codecov Report

Merging #816 (e5d5822) into master (83489ec) will increase coverage by 0.04%.
The diff coverage is 48.57%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@            Coverage Diff             @@
##           master     #816      +/-   ##
==========================================
+ Coverage   36.29%   36.33%   +0.04%     
==========================================
  Files          53       53              
  Lines        7941     7970      +29     
==========================================
+ Hits         2882     2896      +14     
- Misses       4686     4696      +10     
- Partials      373      378       +5     
Flag Coverage Δ
unittests 36.33% <48.57%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
pkg/cfg/cfg.go 43.84% <ø> (ø)
pkg/flags/flags.go 35.71% <0.00%> (-3.35%) ⬇️
pkg/provider/browser/browser.go 57.14% <58.62%> (+1.14%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@eliat123 eliat123 requested a review from mapkon June 15, 2023 04:34
@eliat123
Copy link
Contributor Author

Please checkout the linting errors and fix them.

@mapkon Thank you for noticing that, I updated the golint version to be able to see the ci lint issue and fixed it.

@eliat123 eliat123 requested a review from zemberdotnet June 15, 2023 23:52
@eliat123 eliat123 changed the title Added support for browser-type Added support for browser-type and browser-executable-path Jun 15, 2023
@eliat123
Copy link
Contributor Author

@mapkon I have made the necessary adjustments as per your request. Hope this will help.

@dangymbol
Copy link

Long comment:
This looks like a big improvement for any nix environment. I'm using Linux Mint which Playwright doesn't currently support due to incorrect detection of the base linux version. That means running aws2saml with the browser provider gives me a delightful segmentation violation! This PR should fix that by allowing me to use the installed version instead.

I'm also not in favour of downloading binary versions of Chromium, Firefox and Webkit to a cache - since I already have the current ones installed through the standard package manager.

Short comment: +1 😄

@mapkon
Copy link
Contributor

mapkon commented Nov 9, 2023

@dangymbol Would you mind taking on this PR and updating it?

@eliat123
Copy link
Contributor Author

eliat123 commented Nov 9, 2023

@mapkon I have updated this PR.
@dangymbol FYI.

@@ -56,10 +72,37 @@ func (cl *Client) Authenticate(loginDetails *creds.LoginDetails) (string, error)
Headless: playwright.Bool(cl.Headless),
}

// currently using Chromium as it is widely supported for Identity providers
validBrowserTypes := []string{"chromium", "firefox", "webkit", "chrome", "chrome-beta", "chrome-dev", "chrome-canary", "msedge", "msedge-beta", "msedge-dev", "msedge-canary"}
Copy link
Contributor

@gliptak gliptak Nov 9, 2023

Choose a reason for hiding this comment

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

why differentiate all these browsertypes if playwright only supports firefox, chrome and webkit?

I see #816 (comment)

maybe the if statement below is to be updated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @gliptak, Thank you for your review.

Playwright does support all of those browser types, see:
https://playwright.dev/docs/browsers and https://github.com/playwright-community/playwright-go/blob/main/generated-structs.go#L691C4-L695

The main idea behind this PR is that the user can choose a browser type which is already installed on their system.

If you choose a browser you do not have on your system, you will get a relevant error message.

For example, I tried to set --browser-type=msedge. Since I did not have msedge installed I got the following notification:

INFO[0000] Setting browser type: msedge                  provider=browser
Error authenticating to IdP.: could not send message: Chromium distribution 'msedge' is not found at /Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge
Run "npx playwright install msedge"

After I installed msedge, it used the installed one:

INFO[0000] Setting browser type: msedge                  provider=browser
INFO[0001] opening browser                               URL="[IDP_URL]" provider=browser
INFO[0021] waiting ...                                   provider=browser
INFO[0049] clean up browser                              provider=browser

Furthermore, If your browser is installed on a non-default location, you can utilize --browser-executable-path, and it will start the browser from that path.

Hope this explains it.

// Default browser is Chromium as it is widely supported for Identity providers,
// It can also be set to the other playwright browsers: Firefox and WebKit
browserType := pw.Chromium
if cl.BrowserType == "firefox" {
Copy link
Contributor

Choose a reason for hiding this comment

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

@eliat123 would there be more browsertypes mapped here?

https://playwright.dev/docs/browsers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gliptak Nope. Only these browsertypes are supported.

Ref: https://github.com/playwright-community/playwright-go/blob/ddc7abdd06db016d63664de1ab08870195085ddb/playwright.go#L21-L23

According to https://playwright.dev/docs/browsers :

  1. Chromium is for "chromium", "chrome", "msedge", "chrome-beta", "msedge-beta" or "msedge-dev".
  2. Firefox is for the recent Firefox Stable build.
  3. Webkit is for the recent WebKit build.

Chromium will be the default option, but if "webkit" or "firefox" are selected, they will be used instead. This ensures the most available options.

Copy link
Contributor

Choose a reason for hiding this comment

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

will be browserType be pw.Chromium when cl.BrowserType == 'msedge' and is this as expected?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gliptak The answer is yes.

Here is the section from Playwright docs about Google Chrome & Microsoft Edge:

While Playwright can download and use the recent Chromium build, 
it can operate against the branded Google Chrome and Microsoft Edge browsers available on the machine (note that Playwright doesn't install them by default). 
In particular, the current Playwright version will support Stable and Beta channels of these browsers.

Available channels are 'chrome', 'msedge', 'chrome-beta', 'msedge-beta' or 'msedge-dev'.

ref: https://playwright.dev/docs/browsers#google-chrome--microsoft-edge

@eliat123 eliat123 requested a review from gliptak November 10, 2023 07:34
@mapkon mapkon merged commit 0b61e6e into Versent:master Nov 11, 2023
8 checks passed
@mikex99
Copy link

mikex99 commented Nov 15, 2023

Can I get a link to this build for testing?

@gliptak
Copy link
Contributor

gliptak commented Nov 15, 2023

@merusso
Copy link

merusso commented Nov 18, 2023

Does this implementation allow use of an system's existing, default browser (#729)? It seems like it enables use of other Playwright browsers, which are still managed separately.

@kbarlowgw
Copy link
Contributor

kbarlowgw commented Dec 8, 2023

I am trying to use this to run a local browser not in incognito mode but, while I can run a local chromium install, it still seems that playwright wants to open in it incognito mode. How do we prevent this?

For settings, I am trying setting SAML2AWS_BROWSER_EXECUTABLE_PATH="chromium" and running --browser-type=chrome but it still opens in incognito mode.

@kbarlowgw
Copy link
Contributor

Following up on this to see if there was a workaround or playwright switch I could use, it seems this ability is still in the works with playwright and we may have to update to use browser.launchPersistentContext() instead of launch() when complete:

microsoft/playwright#11833

If someone knows of another option, please let me know.

Thanks.

@gliptak
Copy link
Contributor

gliptak commented Dec 9, 2023

@merusso consider opening an issue for finding/running with default browser

@kbarlowgw consider opening an issue on updating to browser.launchPersistentContext() are there cases when launch() is applicable? would this be a switch and what is the default?

@eliat123 for awareness

@merusso
Copy link

merusso commented Dec 9, 2023

@gliptak I believe this was already created, #729. I believe the issue was mistakenly closed when this PR was merged.

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.

Using locally installed browser rather than outdated sanboxed chromium from "playwright-go"