Skip to content

feat: add support for optional external auth providers #12021

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 35 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f9feb97
frontend hacking
aslilac Feb 9, 2024
179adae
pipe through all of the protobuf stuff
aslilac Feb 9, 2024
bdf6142
db migration
aslilac Feb 9, 2024
751400b
💅
aslilac Feb 12, 2024
5068d88
Merge branch 'main' into optional-external-auth
aslilac Feb 12, 2024
eaddb5b
🧹
aslilac Feb 12, 2024
eba3d12
tests!
aslilac Feb 12, 2024
ebb518b
🧪
aslilac Feb 12, 2024
29be417
official release
aslilac Feb 12, 2024
48267a4
add default first in up migration
aslilac Feb 12, 2024
e870667
manually default
aslilac Feb 12, 2024
d58c088
connect with correct version
aslilac Feb 13, 2024
757ea0f
Merge branch 'main' into optional-external-auth
aslilac Feb 14, 2024
79ad9da
land the backend changes separately
aslilac Feb 14, 2024
6746f88
fix migration order
aslilac Feb 14, 2024
cb7ab9a
version.go is not generated
aslilac Feb 14, 2024
3bacc70
it could just all go away
aslilac Feb 15, 2024
28a878d
Revert "it could just all go away"
aslilac Feb 15, 2024
b514292
Merge branch 'main' into optional-external-auth
aslilac Feb 15, 2024
892a79b
change tested version
aslilac Feb 15, 2024
c5e63eb
add `database.ExternalAuthProviders` type
aslilac Feb 15, 2024
6f933b8
Merge branch 'main' into optional-external-auth
aslilac Feb 16, 2024
ea81a1f
`drop` migration functions
aslilac Feb 16, 2024
2376357
`make gen`
aslilac Feb 16, 2024
b9471a5
Merge branch 'main' into optional-external-auth
aslilac Feb 16, 2024
b2b9e95
🆘
aslilac Feb 16, 2024
b159156
Merge branch 'main' into optional-external-auth
aslilac Feb 20, 2024
04579de
no such file
aslilac Feb 20, 2024
7ff56b1
bump version again
aslilac Feb 20, 2024
16ba268
update test
aslilac Feb 20, 2024
bdb875a
Merge branch 'main' into optional-external-auth
aslilac Feb 21, 2024
328202a
fix migration numbers
aslilac Feb 21, 2024
a475ec0
Merge branch 'main' into optional-external-auth
aslilac Feb 21, 2024
353e6a3
`COMMENT ON VIEW`
aslilac Feb 21, 2024
1021bc4
`make gen`
aslilac Feb 21, 2024
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
Prev Previous commit
Next Next commit
🧹
  • Loading branch information
aslilac committed Feb 12, 2024
commit eaddb5b5d47ac4250ea1f33dfdd8efe292bd0551
2 changes: 1 addition & 1 deletion cli/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ func TestCreateWithGitAuth(t *testing.T) {
{
Type: &proto.Response_Plan{
Plan: &proto.PlanComplete{
ExternalAuthProviders: []string{"github"},
ExternalAuthProviders: []*proto.ExternalAuthProviderResource{{Id: "github"}},
},
},
},
Expand Down
3 changes: 1 addition & 2 deletions coderd/database/dbauthz/dbauthz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -925,8 +925,7 @@ func (s *MethodTestSuite) TestTemplate() {
JobID: jobID,
})
check.Args(database.UpdateTemplateVersionExternalAuthProvidersByJobIDParams{
JobID: jobID,
ExternalAuthProviders: []string{},
JobID: jobID,
}).Asserts(t1, rbac.ActionUpdate).Returns()
}))
s.Run("GetTemplateInsights", s.Subtest(func(db database.Store, check *expects) {
Expand Down
25 changes: 16 additions & 9 deletions coderd/provisionerdserver/provisionerdserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,14 @@ func TestAcquireJob(t *testing.T) {
// create an API key with an expiration within the bounds of the
// deployment config.
dv := &codersdk.DeploymentValues{MaxTokenLifetime: clibase.Duration(time.Hour)}
gitAuthProvider := "github"
gitAuthProvider := &sdkproto.ExternalAuthProviderResource{
Id: "github",
}

srv, db, ps, _ := setup(t, false, &overrides{
deploymentValues: dv,
externalAuthConfigs: []*externalauth.Config{{
ID: gitAuthProvider,
ID: gitAuthProvider.Id,
InstrumentedOAuth2Config: &testutil.OAuth2Config{},
}},
})
Expand All @@ -191,7 +194,7 @@ func TestAcquireJob(t *testing.T) {
OAuthAccessToken: "access-token",
})
dbgen.ExternalAuthLink(t, db, database.ExternalAuthLink{
ProviderID: gitAuthProvider,
ProviderID: gitAuthProvider.Id,
UserID: user.ID,
})
template := dbgen.Template(t, db, database.Template{
Expand All @@ -207,9 +210,11 @@ func TestAcquireJob(t *testing.T) {
},
JobID: uuid.New(),
})
err := db.UpdateTemplateVersionExternalAuthProvidersByJobID(ctx, database.UpdateTemplateVersionExternalAuthProvidersByJobIDParams{
externalAuthProviders, err := json.Marshal([]*sdkproto.ExternalAuthProviderResource{gitAuthProvider})
require.NoError(t, err)
err = db.UpdateTemplateVersionExternalAuthProvidersByJobID(ctx, database.UpdateTemplateVersionExternalAuthProvidersByJobIDParams{
JobID: version.JobID,
ExternalAuthProviders: []string{gitAuthProvider},
ExternalAuthProviders: json.RawMessage(externalAuthProviders),
UpdatedAt: dbtime.Now(),
})
require.NoError(t, err)
Expand Down Expand Up @@ -321,7 +326,7 @@ func TestAcquireJob(t *testing.T) {
},
},
ExternalAuthProviders: []*sdkproto.ExternalAuthProvider{{
Id: gitAuthProvider,
Id: gitAuthProvider.Id,
AccessToken: "access_token",
}},
Metadata: &sdkproto.Metadata{
Expand Down Expand Up @@ -949,8 +954,10 @@ func TestCompleteJob(t *testing.T) {
Name: "hello",
Type: "aws_instance",
}},
StopResources: []*sdkproto.Resource{},
ExternalAuthProviders: []string{"github"},
StopResources: []*sdkproto.Resource{},
ExternalAuthProviders: []*sdkproto.ExternalAuthProviderResource{{
Id: "github",
}},
},
},
})
Expand Down Expand Up @@ -1002,7 +1009,7 @@ func TestCompleteJob(t *testing.T) {
Type: "aws_instance",
}},
StopResources: []*sdkproto.Resource{},
ExternalAuthProviders: []string{"github"},
ExternalAuthProviders: []*sdkproto.ExternalAuthProviderResource{{Id: "github"}},
},
},
})
Expand Down
2 changes: 1 addition & 1 deletion coderd/templateversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func TestTemplateVersionsExternalAuth(t *testing.T) {
ProvisionPlan: []*proto.Response{{
Type: &proto.Response_Plan{
Plan: &proto.PlanComplete{
ExternalAuthProviders: []string{"github"},
ExternalAuthProviders: []*proto.ExternalAuthProviderResource{{Id: "github"}},
},
},
}},
Expand Down
2 changes: 1 addition & 1 deletion codersdk/templateversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type TemplateVersionExternalAuth struct {
DisplayIcon string `json:"display_icon"`
AuthenticateURL string `json:"authenticate_url"`
Authenticated bool `json:"authenticated"`
Optional bool `json:"optional"`
Optional bool `json:"optional,omitempty"`
}

type ValidationMonotonicOrder string
Expand Down
2 changes: 1 addition & 1 deletion enterprise/coderd/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (api *API) provisionerDaemonServe(rw http.ResponseWriter, r *http.Request)

if err := proto.CurrentVersion.Validate(apiVersion); err != nil {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Incompatible or unparseable version",
Message: "Incompatible or unparsable version",
Validations: []codersdk.ValidationError{
{Field: "version", Detail: err.Error()},
},
Expand Down
4 changes: 2 additions & 2 deletions provisioner/terraform/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,8 +718,8 @@ func ConvertState(modules []*tfjson.StateModule, rawGraph string) (*State, error
}
}
externalAuthProviders := make([]*proto.ExternalAuthProviderResource, 0, len(externalAuthProvidersMap))
for _, provider := range externalAuthProvidersMap {
externalAuthProviders = append(externalAuthProviders, provider)
for _, it := range externalAuthProvidersMap {
externalAuthProviders = append(externalAuthProviders, it)
}

return &State{
Expand Down
23 changes: 15 additions & 8 deletions provisioner/terraform/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"runtime"
"sort"
"strings"
"testing"

tfjson "github.com/hashicorp/terraform-json"
Expand All @@ -23,9 +24,9 @@ func TestConvertResources(t *testing.T) {
// nolint:dogsled
_, filename, _, _ := runtime.Caller(0)
type testCase struct {
resources []*proto.Resource
parameters []*proto.RichParameter
gitAuthProviders []string
resources []*proto.Resource
parameters []*proto.RichParameter
externalAuthProviders []*proto.ExternalAuthProviderResource
}

// If a user doesn't specify 'display_apps' then they default
Expand Down Expand Up @@ -486,7 +487,7 @@ func TestConvertResources(t *testing.T) {
DisplayApps: &displayApps,
}},
}},
gitAuthProviders: []string{"github", "gitlab"},
externalAuthProviders: []*proto.ExternalAuthProviderResource{{Id: "github"}, {Id: "gitlab"}},
},
"display-apps": {
resources: []*proto.Resource{{
Expand Down Expand Up @@ -547,7 +548,7 @@ func TestConvertResources(t *testing.T) {
state, err := terraform.ConvertState(modules, string(tfPlanGraph))
require.NoError(t, err)
sortResources(state.Resources)
sort.Strings(state.ExternalAuthProviders)
sortExternalAuthProviders(state.ExternalAuthProviders)

expectedNoMetadata := make([]*proto.Resource, 0)
for _, resource := range expected.resources {
Expand Down Expand Up @@ -585,7 +586,7 @@ func TestConvertResources(t *testing.T) {
require.Equal(t, string(parametersWant), string(parametersGot))
require.Equal(t, expectedNoMetadataMap, resourcesMap)

require.ElementsMatch(t, expected.gitAuthProviders, state.ExternalAuthProviders)
require.ElementsMatch(t, expected.externalAuthProviders, state.ExternalAuthProviders)
})

t.Run("Provision", func(t *testing.T) {
Expand All @@ -601,7 +602,7 @@ func TestConvertResources(t *testing.T) {
state, err := terraform.ConvertState([]*tfjson.StateModule{tfState.Values.RootModule}, string(tfStateGraph))
require.NoError(t, err)
sortResources(state.Resources)
sort.Strings(state.ExternalAuthProviders)
sortExternalAuthProviders(state.ExternalAuthProviders)
for _, resource := range state.Resources {
for _, agent := range resource.Agents {
agent.Id = ""
Expand All @@ -628,7 +629,7 @@ func TestConvertResources(t *testing.T) {
require.NoError(t, err)

require.Equal(t, expectedMap, resourcesMap)
require.ElementsMatch(t, expected.gitAuthProviders, state.ExternalAuthProviders)
require.ElementsMatch(t, expected.externalAuthProviders, state.ExternalAuthProviders)
})
})
}
Expand Down Expand Up @@ -901,3 +902,9 @@ func sortResources(resources []*proto.Resource) {
})
}
}

func sortExternalAuthProviders(providers []*proto.ExternalAuthProviderResource) {
sort.Slice(providers, func(i, j int) bool {
return strings.Compare(providers[i].Id, providers[j].Id) == -1
})
}
2 changes: 1 addition & 1 deletion site/src/api/typesGenerated.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ export const OptionalExternalAuth: Story = {
authenticate_url: "",
display_icon: "/icon/github.svg",
display_name: "GitHub",
// @ts-expect-error
optional: true,
},
{
Expand All @@ -151,7 +150,6 @@ export const OptionalExternalAuth: Story = {
authenticate_url: "",
display_icon: "/icon/gitlab.svg",
display_name: "GitLab",
// @ts-expect-error
optional: true,
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const ExternalAuthItem: FC<ExternalAuthItemProps> = ({
<span css={styles.providerHeader}>
<ExternalImage src={provider.display_icon} css={styles.providerIcon} />
<strong css={styles.providerName}>{provider.display_name}</strong>
{!provider.authenticated && (provider as any).optional && (
{!provider.authenticated && provider.optional && (
<Pill type="notice">Optional</Pill>
)}
</span>
Expand Down