-
Notifications
You must be signed in to change notification settings - Fork 943
feat: add external-auth
cli
#10052
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
Merged
Merged
feat: add external-auth
cli
#10052
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
abba0d6
feat: add `external-auth` cli
kylecarbs 1f50431
Add subcommands
kylecarbs df8ac5d
Improve descriptions
kylecarbs 12120d5
Merge branch 'main' into gitauthcli
kylecarbs 64f2241
Add external-auth subcommand
kylecarbs 471e62f
Fix docs
kylecarbs 51519a8
Fix gen
kylecarbs 1f47368
Fix comment
kylecarbs 1537788
Fix golden file
kylecarbs b60f208
Fix docgen
kylecarbs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package cli | ||
|
||
import ( | ||
"os/signal" | ||
|
||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/v2/cli/clibase" | ||
"github.com/coder/coder/v2/cli/cliui" | ||
"github.com/coder/coder/v2/codersdk/agentsdk" | ||
) | ||
|
||
func (r *RootCmd) externalAuth() *clibase.Cmd { | ||
return &clibase.Cmd{ | ||
Use: "external-auth", | ||
Short: "Manage external authentication", | ||
Long: "Authenticate with external services inside of a workspace.", | ||
Handler: func(i *clibase.Invocation) error { | ||
return i.Command.HelpHandler(i) | ||
}, | ||
Children: []*clibase.Cmd{ | ||
r.externalAuthAccessToken(), | ||
}, | ||
} | ||
} | ||
|
||
func (r *RootCmd) externalAuthAccessToken() *clibase.Cmd { | ||
var silent bool | ||
return &clibase.Cmd{ | ||
Use: "access-token <provider>", | ||
Short: "Print auth for an external provider", | ||
Long: "Print an access-token for an external auth provider. " + | ||
"The access-token will be validated and sent to stdout with exit code 0. " + | ||
"If a valid access-token cannot be obtained, the URL to authenticate will be sent to stdout with exit code 1\n" + formatExamples( | ||
example{ | ||
Description: "Ensure that the user is authenticated with GitHub before cloning.", | ||
Command: `#!/usr/bin/env sh | ||
|
||
if coder external-auth access-token github ; then | ||
echo "Authenticated with GitHub" | ||
else | ||
echo "Please authenticate with GitHub:" | ||
coder external-auth url github | ||
fi | ||
`, | ||
}, | ||
), | ||
Options: clibase.OptionSet{{ | ||
Name: "Silent", | ||
Flag: "s", | ||
Description: "Do not print the URL or access token.", | ||
Value: clibase.BoolOf(&silent), | ||
}}, | ||
|
||
Handler: func(inv *clibase.Invocation) error { | ||
ctx := inv.Context() | ||
|
||
ctx, stop := signal.NotifyContext(ctx, InterruptSignals...) | ||
defer stop() | ||
|
||
client, err := r.createAgentClient() | ||
if err != nil { | ||
return xerrors.Errorf("create agent client: %w", err) | ||
} | ||
|
||
token, err := client.ExternalAuth(ctx, agentsdk.ExternalAuthRequest{ | ||
ID: inv.Args[0], | ||
}) | ||
if err != nil { | ||
return xerrors.Errorf("get external auth token: %w", err) | ||
} | ||
|
||
if !silent { | ||
if token.URL != "" { | ||
_, err = inv.Stdout.Write([]byte(token.URL)) | ||
} else { | ||
_, err = inv.Stdout.Write([]byte(token.AccessToken)) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if token.URL != "" { | ||
return cliui.Canceled | ||
} | ||
return nil | ||
}, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package cli_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/coder/coder/v2/cli/clitest" | ||
"github.com/coder/coder/v2/cli/cliui" | ||
"github.com/coder/coder/v2/coderd/httpapi" | ||
"github.com/coder/coder/v2/codersdk/agentsdk" | ||
"github.com/coder/coder/v2/pty/ptytest" | ||
) | ||
|
||
func TestExternalAuth(t *testing.T) { | ||
t.Parallel() | ||
t.Run("CanceledWithURL", func(t *testing.T) { | ||
t.Parallel() | ||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
httpapi.Write(context.Background(), w, http.StatusOK, agentsdk.ExternalAuthResponse{ | ||
URL: "https://github.com", | ||
}) | ||
})) | ||
t.Cleanup(srv.Close) | ||
url := srv.URL | ||
inv, _ := clitest.New(t, "--agent-url", url, "external-auth", "access-token", "github") | ||
pty := ptytest.New(t) | ||
inv.Stdout = pty.Output() | ||
waiter := clitest.StartWithWaiter(t, inv) | ||
pty.ExpectMatch("https://github.com") | ||
waiter.RequireIs(cliui.Canceled) | ||
}) | ||
t.Run("SuccessWithToken", func(t *testing.T) { | ||
t.Parallel() | ||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
httpapi.Write(context.Background(), w, http.StatusOK, agentsdk.ExternalAuthResponse{ | ||
AccessToken: "bananas", | ||
}) | ||
})) | ||
t.Cleanup(srv.Close) | ||
url := srv.URL | ||
inv, _ := clitest.New(t, "--agent-url", url, "external-auth", "access-token", "github") | ||
pty := ptytest.New(t) | ||
inv.Stdout = pty.Output() | ||
clitest.Start(t, inv) | ||
pty.ExpectMatch("bananas") | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
coder v0.0.0-devel | ||
|
||
USAGE: | ||
coder external-auth | ||
|
||
Manage external authentication | ||
|
||
Authenticate with external services inside of a workspace. | ||
|
||
SUBCOMMANDS: | ||
access-token Print auth for an external provider | ||
|
||
——— | ||
Run `coder --help` for a list of global options. |
27 changes: 27 additions & 0 deletions
27
cli/testdata/coder_external-auth_access-token_--help.golden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
coder v0.0.0-devel | ||
|
||
USAGE: | ||
coder external-auth access-token [flags] <provider> | ||
|
||
Print auth for an external provider | ||
|
||
Print an access-token for an external auth provider. The access-token will be | ||
validated and sent to stdout with exit code 0. If a valid access-token cannot | ||
be obtained, the URL to authenticate will be sent to stdout with exit code 1 | ||
- Ensure that the user is authenticated with GitHub before cloning.: | ||
|
||
$ #!/usr/bin/env sh | ||
|
||
if coder external-auth access-token github ; then | ||
echo "Authenticated with GitHub" | ||
else | ||
echo "Please authenticate with GitHub:" | ||
coder external-auth url github | fi | |
|
||
OPTIONS: | ||
--s bool | ||
Do not print the URL or access token. | ||
|
||
——— | ||
Run `coder --help` for a list of global options. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see a
external-auth url
cmd.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good find!