Securing the CLI with OAuth2 Gadget Authorization


Most firms have sturdy exterior safety, e.g. blocking all entry to manufacturing property utilizing a firewall, and requiring a VPN to get “inside” entry to manufacturing environments. Nevertheless, as soon as you might be linked to the VPN, the interior methods are normally very poorly protected, and there may be little to no authentication and authorization for inside instruments and companies.

Two widespread threats to inside safety are compromised worker laptops and provide chain assaults. In these eventualities, the attacker operates behind the firewall, typically with unrestricted community entry.

Companies with an online ui will be secured utilizing an utility load balancer, e.g. an AWS ALB with OIDC, however how do you defend entry to command line interface (CLI) based mostly instruments? Requiring a username and password for each CLI invocation makes it painful to make use of and storing the credentials on the system leaves them huge open in case the pc they reside on is compromised.

The Command Line

Most inside instruments have a CLI to handle the companies which can be used throughout the firm and plenty of are poorly protected. What’s the easiest way to authorize CLIs? And how will you tie authorization into the corporate’s SSO?

One possibility is to deploy Hashicorp Vault, however that’s a variety of setup and upkeep, so except you’ve a group to function it, Vault may not be a very good match.

An alternative choice is the OAuth2 gadget authorization grant (RFC8628), which is what this weblog submit will present you the best way to use.

The OAuth 2.0 gadget authorization grant is designed for Web-connected gadgets that both lack a browser to carry out a user-agent-based authorization or are enter constrained to the extent that requiring the person to enter textual content with the intention to authenticate throughout the authorization circulate is impractical. It permits OAuth purchasers on such gadgets (like good TVs, media consoles, digital image frames, and printers) to acquire person authorization to entry protected sources by utilizing a person agent on a separate gadget.

In the event you ever used the AWS CLI with Single SignOn, that is what it does.

OAuth2 Gadget Stream

The Gadget Authorization Stream comprises two completely different paths; one happens on the gadget requesting authorization (the CLI) and the opposite happens in a browser. The browser circulate path, whereby a tool code is certain to the session within the browser, happens as a parallel path half within the gadget circulate path.


Implementing the OAuth Gadget Stream

Now we’ll take a look at what the above sequence diagram appears to be like like when it’s applied.

The inner CLI instrument at Rockset is known as rsctl and is written in go. Step one is to provoke the gadget circulate to get a JWT entry token.

$ rsctl login
Trying to robotically open the SSO authorization web page in your default browser.
If the browser doesn't open otherwise you want to use a unique gadget to authorize this request, open the next URL:

Then enter the code:

Efficiently logged in!

If you’re utilizing the CLI after logging in to a different laptop, e.g. ssh:ing to a Linux server, and you employ macOS, you’ll be able to configure iTerm to robotically open the hyperlink utilizing a “Run command” set off.

The web page that the hyperlink takes you to appears to be like like this:

Device Confirmation

Upon getting confirmed that the “person code” is right (matches with what the CLI reveals), and also you click on “Verify”, it is going to take you thru the conventional OAuth2 login process (which in our case requires a username, password and {hardware} token).

As soon as the authentication is accomplished, you’ll be redirected and offered with a dialog just like the one beneath, and you may shut the browser window.

Device Confirmation

The CLI has now acquired a jwt entry token which is legitimate for various hours and is used to authenticate through inside companies. The token will be cached on disk and reused between CLI invocations at some point of its lifetime.

Once you situation a brand new rsctl command, it is going to learn the cached Entry Token from disk, and use it to authenticate with the interior APIs.

Below the Hood

Now we have applied and open sourced a go module to carry out the gadget authorization circulate ( It helps each Auth0 and Okta as OAuth suppliers.

Pattern Code

The next code is obtainable within the instance listing within the git repository.

Embedded content material:

We now have a JWT token, which can be utilized to authenticate REST calls by setting the Authorization header to Bearer: <jwt entry token>

Embedded content material:

It’s now as much as the receiving finish to validate the bearer token, which will be accomplished utilizing an AWS ALB with OIDC authentication or a supplier particular API from the API server.

Offline Validation

An alternative choice for entry token validation is “offline validation”. In offline validation, the API server will get the general public key used to signal the JWT token from the supplier (and caches the general public key) and performs the validation within the API server, as a substitute of creating a validation request to the supplier.

Residual Danger

One factor this doesn’t defend towards is an attacker with a foothold on the pc that executes the CLI. They’ll simply wait till the person has accomplished the authentication, and they’ll then be capable of act because the person at some point of the entry token.

To mitigate this danger, you’ll be able to require a one time password (OTP), e.g. a Yubikey, each time the person performs a privileged motion.

$ rsctl delete useful resource foobar
please enter yubikey OTP: ccccccvfbbcddjtuehgnfrbtublkuufbgeebklrubkhf
useful resource foobar deleted

Closing Ideas

On this weblog, we’ve got proven how we constructed and open-sourced a go module to safe the Command Line Interface (CLI) utilizing an OAuth2 gadget authorization circulate that helps each Auth0 and Okta SSO suppliers. You possibly can add this go module to your inside instruments and scale back inside safety threats.


Please enter your comment!
Please enter your name here