Skip to content

fix: Allow custom Git OAuth URLs #4758

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 1 commit into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,11 @@ func (a *agent) createTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) (*t
if err != nil {
return nil, xerrors.Errorf("listen on the ssh port: %w", err)
}
a.closeMutex.Lock()
a.connCloseWait.Add(1)
a.closeMutex.Unlock()
go func() {
defer a.connCloseWait.Done()
for {
conn, err := sshListener.Accept()
if err != nil {
Expand All @@ -256,7 +260,11 @@ func (a *agent) createTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) (*t
if err != nil {
return nil, xerrors.Errorf("listen for reconnecting pty: %w", err)
}
a.closeMutex.Lock()
a.connCloseWait.Add(1)
a.closeMutex.Unlock()
go func() {
defer a.connCloseWait.Done()
for {
conn, err := reconnectingPTYListener.Accept()
if err != nil {
Expand Down Expand Up @@ -290,7 +298,11 @@ func (a *agent) createTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) (*t
if err != nil {
return nil, xerrors.Errorf("listen for speedtest: %w", err)
}
a.closeMutex.Lock()
a.connCloseWait.Add(1)
a.closeMutex.Unlock()
go func() {
defer a.connCloseWait.Done()
for {
conn, err := speedtestListener.Accept()
if err != nil {
Expand All @@ -311,7 +323,11 @@ func (a *agent) createTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) (*t
if err != nil {
return nil, xerrors.Errorf("listen for statistics: %w", err)
}
a.closeMutex.Lock()
a.connCloseWait.Add(1)
a.closeMutex.Unlock()
go func() {
defer a.connCloseWait.Done()
defer statisticsListener.Close()
server := &http.Server{
Handler: a.statisticsHandler(),
Expand Down
8 changes: 7 additions & 1 deletion cli/deployment/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,14 +462,20 @@ func readSliceFromViper[T any](vip *viper.Viper, key string, value any) []T {
if prop == "-" {
prop = fve.Tag.Get("yaml")
}
value := vip.Get(fmt.Sprintf("%s.%d.%s", key, entry, prop))
configKey := fmt.Sprintf("%s.%d.%s", key, entry, prop)
value := vip.Get(configKey)
if value == nil {
continue
}
if instance == nil {
newType := reflect.Indirect(reflect.New(elementType))
instance = &newType
}
switch instance.Field(i).Type().String() {
case "[]string":
value = vip.GetStringSlice(configKey)
default:
}
instance.Field(i).Set(reflect.ValueOf(value))
}
if instance == nil {
Expand Down
2 changes: 2 additions & 0 deletions cli/deployment/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func TestConfig(t *testing.T) {
"CODER_GITAUTH_0_AUTH_URL": "https://auth.com",
"CODER_GITAUTH_0_TOKEN_URL": "https://token.com",
"CODER_GITAUTH_0_REGEX": "github.com",
"CODER_GITAUTH_0_SCOPES": "read write",

"CODER_GITAUTH_1_ID": "another",
"CODER_GITAUTH_1_TYPE": "gitlab",
Expand All @@ -177,6 +178,7 @@ func TestConfig(t *testing.T) {
AuthURL: "https://auth.com",
TokenURL: "https://token.com",
Regex: "github.com",
Scopes: []string{"read", "write"},
}, {
ID: "another",
Type: "gitlab",
Expand Down
10 changes: 10 additions & 0 deletions coderd/gitauth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ func ConvertConfig(entries []codersdk.GitAuthConfig, accessURL *url.URL) ([]*Con
Scopes: scope[typ],
}

if entry.AuthURL != "" {
oauth2Config.Endpoint.AuthURL = entry.AuthURL
}
if entry.TokenURL != "" {
oauth2Config.Endpoint.TokenURL = entry.TokenURL
}
if entry.Scopes != nil && len(entry.Scopes) > 0 {
oauth2Config.Scopes = entry.Scopes
}

var oauthConfig httpmw.OAuth2Config = oauth2Config
// Azure DevOps uses JWT token authentication!
if typ == codersdk.GitProviderAzureDevops {
Expand Down
14 changes: 14 additions & 0 deletions coderd/gitauth/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,18 @@ func TestConvertYAML(t *testing.T) {
require.Equal(t, tc.Output, output)
})
}

t.Run("CustomScopesAndEndpoint", func(t *testing.T) {
t.Parallel()
config, err := gitauth.ConvertConfig([]codersdk.GitAuthConfig{{
Type: codersdk.GitProviderGitLab,
ClientID: "id",
ClientSecret: "secret",
AuthURL: "https://auth.com",
TokenURL: "https://token.com",
Scopes: []string{"read"},
}}, &url.URL{})
require.NoError(t, err)
require.Equal(t, "https://auth.com?client_id=id&redirect_uri=%2Fgitauth%2Fgitlab%2Fcallback&response_type=code&scope=read", config[0].AuthCodeURL(""))
})
}
10 changes: 5 additions & 5 deletions codersdk/client_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Test_readBodyAsError(t *testing.T) {
longResponse += "a"
}

unexpectedJSON := marshalJSON(map[string]any{
unexpectedJSON := marshal(map[string]any{
"hello": "world",
"foo": "bar",
})
Expand All @@ -49,7 +49,7 @@ func Test_readBodyAsError(t *testing.T) {
{
name: "JSONWithRequest",
req: httptest.NewRequest(http.MethodGet, exampleURL, nil),
res: newResponse(http.StatusNotFound, jsonCT, marshalJSON(simpleResponse)),
res: newResponse(http.StatusNotFound, jsonCT, marshal(simpleResponse)),
assert: func(t *testing.T, err error) {
sdkErr := assertSDKError(t, err)

Expand All @@ -72,7 +72,7 @@ func Test_readBodyAsError(t *testing.T) {
{
name: "JSONWithoutRequest",
req: nil,
res: newResponse(http.StatusNotFound, jsonCT, marshalJSON(simpleResponse)),
res: newResponse(http.StatusNotFound, jsonCT, marshal(simpleResponse)),
assert: func(t *testing.T, err error) {
sdkErr := assertSDKError(t, err)

Expand All @@ -86,7 +86,7 @@ func Test_readBodyAsError(t *testing.T) {
{
name: "UnauthorizedHelper",
req: nil,
res: newResponse(http.StatusUnauthorized, jsonCT, marshalJSON(simpleResponse)),
res: newResponse(http.StatusUnauthorized, jsonCT, marshal(simpleResponse)),
assert: func(t *testing.T, err error) {
sdkErr := assertSDKError(t, err)

Expand Down Expand Up @@ -190,7 +190,7 @@ func newResponse(status int, contentType string, body interface{}) *http.Respons
}
}

func marshalJSON(res any) string {
func marshal(res any) string {
b, err := json.Marshal(res)
if err != nil {
panic(err)
Expand Down
15 changes: 8 additions & 7 deletions codersdk/deploymentconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,14 @@ type TLSConfig struct {
}

type GitAuthConfig struct {
ID string `json:"id"`
Type string `json:"type"`
ClientID string `json:"client_id"`
ClientSecret string `json:"-" yaml:"client_secret"`
AuthURL string `json:"auth_url"`
TokenURL string `json:"token_url"`
Regex string `json:"regex"`
ID string `json:"id"`
Type string `json:"type"`
ClientID string `json:"client_id"`
ClientSecret string `json:"-" yaml:"client_secret"`
AuthURL string `json:"auth_url"`
TokenURL string `json:"token_url"`
Regex string `json:"regex"`
Scopes []string `json:"scopes"`
Copy link
Member

Choose a reason for hiding this comment

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

Should we consider omitempty here? I noticed typegen needs to be run for this PR, which adds the TypeScript entry for scopes, however, if the API doesn't ensure that Scopes: nil becomes, Scopes: []string{}, then we're lying to TypeScript (at which point using omitempty is appropriate).

Copy link
Member Author

Choose a reason for hiding this comment

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

Ahh, good point!

}

type Flaggable interface {
Expand Down
1 change: 1 addition & 0 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ export interface GitAuthConfig {
readonly auth_url: string
readonly token_url: string
readonly regex: string
readonly scopes: string[]
}

// From codersdk/gitsshkey.go
Expand Down