-
Notifications
You must be signed in to change notification settings - Fork 894
feat: Add Git auth for GitHub, GitLab, Azure DevOps, and BitBucket #4670
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
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
3dc1f00
Add scaffolding
kylecarbs f640283
Merge branch 'main' into gitprovider
kylecarbs fb5195d
Move migration
kylecarbs a10c4d5
Add endpoints for gitauth
kylecarbs 4c37c34
Add configuration files and tests!
kylecarbs f86e26a
Update typesgen
kylecarbs ab9aa48
Merge branch 'main' into gitprovider
kylecarbs 0a2e222
Convert configuration format for git auth
kylecarbs 7237d9b
Fix unclosed database conn
kylecarbs 439d3bc
Add overriding VS Code configuration
kylecarbs a296e31
Fix Git screen
kylecarbs 40b874d
Merge branch 'main' into gitprovider
kylecarbs 03e6e62
Write VS Code special configuration if providers exist
kylecarbs 6bd9733
Enable automatic cloning from VS Code
kylecarbs 1e5c2f7
Add tests for gitaskpass
kylecarbs 4e4f0ba
Fix feature visibiliy
kylecarbs f2c0983
Add banner for too many configurations
kylecarbs b6bfdf1
Fix update loop for oauth token
kylecarbs 8e17cb9
Merge branch 'main' into gitprovider
kylecarbs b12765c
Jon comments
kylecarbs 29c9e47
Add deployment config page
kylecarbs fad5bae
Merge branch 'main' into gitprovider
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
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,56 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"net/url" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/coder/coder/cli/cliui" | ||
"github.com/coder/coder/coderd/gitauth" | ||
|
||
_ "embed" | ||
) | ||
|
||
//go:embed server.yaml | ||
var defaultServer string | ||
|
||
// Server represents a parsed server configuration. | ||
type Server struct { | ||
GitAuth []*gitauth.Config | ||
} | ||
|
||
// ParseServer creates or consumes a server config by path. | ||
// If one does not exist, it will create one. If it fails to create, | ||
// a warning will appear but the server will not fail to start. | ||
// This is to prevent blocking execution on readonly file-systems | ||
// that didn't provide a default config. | ||
func ParseServer(cmd *cobra.Command, accessURL *url.URL, path string) (*Server, error) { | ||
_, err := os.Stat(path) | ||
if errors.Is(err, os.ErrNotExist) { | ||
err = os.WriteFile(path, []byte(defaultServer), 0600) | ||
if err != nil { | ||
cmd.Printf("%s Unable to write the default config file: %s", cliui.Styles.Warn.Render("Warning:"), err) | ||
} | ||
} | ||
data, err := os.ReadFile(path) | ||
if err != nil { | ||
data = []byte(defaultServer) | ||
} | ||
var server struct { | ||
GitAuth []*gitauth.YAML `yaml:"gitauth"` | ||
} | ||
err = yaml.Unmarshal(data, &server) | ||
if err != nil { | ||
return nil, err | ||
} | ||
configs, err := gitauth.ConvertYAML(server.GitAuth, accessURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Server{ | ||
GitAuth: configs, | ||
}, 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,25 @@ | ||
# Coder Server Configuration | ||
|
||
# Automatically authenticate HTTP(s) Git requests. | ||
gitauth: | ||
# Supported: azure-devops, bitbucket, github, gitlab | ||
# - type: github | ||
# client_id: xxxxxx | ||
# client_secret: xxxxxx | ||
|
||
# Multiple providers are an Enterprise feature. | ||
# Contact sales@coder.com for a license. | ||
# | ||
# If multiple providers are used, a unique "id" | ||
# must be provided for each one. | ||
# - id: example | ||
# type: azure-devops | ||
# client_id: xxxxxxx | ||
# client_secret: xxxxxxx | ||
# A custom regex can be used to match a specific | ||
# repository or organization to limit auth scope. | ||
# regex: github.com/coder | ||
# Custom authentication and token URLs should be | ||
# used for self-managed Git provider deployments. | ||
# auth_url: https://example.com/oauth/authorize | ||
# token_url: https://example.com/oauth/token |
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,40 @@ | ||
package config_test | ||
|
||
import ( | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/cli/config" | ||
) | ||
|
||
func TestServer(t *testing.T) { | ||
t.Parallel() | ||
t.Run("WritesDefault", func(t *testing.T) { | ||
t.Parallel() | ||
path := filepath.Join(t.TempDir(), "server.yaml") | ||
_, err := config.ParseServer(&cobra.Command{}, &url.URL{}, path) | ||
require.NoError(t, err) | ||
data, err := os.ReadFile(path) | ||
require.NoError(t, err) | ||
require.Greater(t, len(data), 0) | ||
}) | ||
t.Run("Filled", func(t *testing.T) { | ||
t.Parallel() | ||
path := filepath.Join(t.TempDir(), "server.yaml") | ||
err := os.WriteFile(path, []byte(` | ||
gitauth: | ||
- type: github | ||
client_id: xxx | ||
client_secret: xxx | ||
`), 0600) | ||
require.NoError(t, err) | ||
cfg, err := config.ParseServer(&cobra.Command{}, &url.URL{}, path) | ||
require.NoError(t, err) | ||
require.Len(t, cfg.GitAuth, 1) | ||
}) | ||
} |
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,71 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"os/signal" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/coderd/gitauth" | ||
"github.com/coder/retry" | ||
) | ||
|
||
func gitAskpass() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "gitaskpass", | ||
Hidden: true, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
ctx := cmd.Context() | ||
|
||
ctx, stop := signal.NotifyContext(ctx, interruptSignals...) | ||
defer stop() | ||
|
||
defer func() { | ||
if ctx.Err() != nil { | ||
err = ctx.Err() | ||
} | ||
}() | ||
|
||
user, host, err := gitauth.ParseAskpass(args[0]) | ||
if err != nil { | ||
return xerrors.Errorf("parse host: %w", err) | ||
} | ||
|
||
client, err := createAgentClient(cmd) | ||
if err != nil { | ||
return xerrors.Errorf("create agent client: %w", err) | ||
} | ||
|
||
token, err := client.WorkspaceAgentGitAuth(ctx, host, false) | ||
if err != nil { | ||
return xerrors.Errorf("get git token: %w", err) | ||
} | ||
if token.URL != "" { | ||
cmd.Printf("Visit the following URL to authenticate with Git:\n%s\n", token.URL) | ||
for r := retry.New(time.Second, 10*time.Second); r.Wait(ctx); { | ||
token, err = client.WorkspaceAgentGitAuth(ctx, host, true) | ||
if err != nil { | ||
continue | ||
} | ||
cmd.Printf("\nYou've been authenticated with Git!\n") | ||
break | ||
} | ||
} | ||
|
||
if token.Password != "" { | ||
if user == "" { | ||
fmt.Fprintln(cmd.OutOrStdout(), token.Username) | ||
} else { | ||
fmt.Fprintln(cmd.OutOrStdout(), token.Password) | ||
} | ||
} else { | ||
fmt.Fprintln(cmd.OutOrStdout(), token.Username) | ||
} | ||
|
||
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 @@ | ||
package cli_test |
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
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.
Uh oh!
There was an error while loading. Please reload this page.