Skip to content

feat: archive template versions to hide them from the ui #10086

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

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
fc715fd
prune template versions
Emyrk Oct 5, 2023
611dac1
Implement fakes and mocks
Emyrk Oct 5, 2023
1de0a5d
Exclude template active versions
Emyrk Oct 5, 2023
3e5f91d
Delete versions are unusable
Emyrk Oct 5, 2023
9200779
Merge remote-tracking branch 'origin/main' into stevenmasley/soft_del…
Emyrk Oct 5, 2023
6e6fbc3
migration bump
Emyrk Oct 5, 2023
2e2c092
Spaces to tabs
Emyrk Oct 5, 2023
7f3d805
add template version prune command
Emyrk Oct 9, 2023
02aa085
Rename to archive
Emyrk Oct 9, 2023
9a416c3
Rename to "archive"
Emyrk Oct 9, 2023
c82ff9a
Merge remote-tracking branch 'origin/main' into stevenmasley/soft_del…
Emyrk Oct 9, 2023
c06d63a
Bump migration file
Emyrk Oct 9, 2023
460b0d2
Make update golden files
Emyrk Oct 9, 2023
d9bfea5
Fix swagger id
Emyrk Oct 9, 2023
655c906
Fix swagger accept json
Emyrk Oct 9, 2023
d4e54b2
Make gen
Emyrk Oct 9, 2023
938f256
Add unarchive
Emyrk Oct 9, 2023
7498c73
Add unarchive api
Emyrk Oct 9, 2023
685512a
Add cli command to unarchive a version
Emyrk Oct 9, 2023
f446ae5
Linting
Emyrk Oct 9, 2023
a9ff9d6
Update golden files
Emyrk Oct 9, 2023
b83a15e
Move cmd commands, allow archiuve deleted
Emyrk Oct 9, 2023
0ecbb79
update golden files
Emyrk Oct 9, 2023
bb3571b
Fix cli errors
Emyrk Oct 9, 2023
533e913
Move cmd
Emyrk Oct 9, 2023
02555b1
Implement fake
Emyrk Oct 9, 2023
1606b99
Linting and gen
Emyrk Oct 9, 2023
3818a57
fixup! Linting and gen
Emyrk Oct 9, 2023
48e44b6
fix: properly trim spaces so multi-line shebang executes (#10146)
kylecarbs Oct 9, 2023
3c97681
chore: reorganize storybook (#10144)
aslilac Oct 9, 2023
f01a4b3
chore: bump the golang-x group with 6 updates (#10128)
dependabot[bot] Oct 9, 2023
e9b4d15
chore: bump google.golang.org/api from 0.143.0 to 0.145.0 (#10130)
dependabot[bot] Oct 9, 2023
4236fc8
ci: bump the github-actions group with 2 updates (#10131)
dependabot[bot] Oct 9, 2023
8c86767
chore: add icons for popular programming languages (#10141)
aslilac Oct 9, 2023
02bcd20
chore: run `go mod tidy`
coadler Oct 9, 2023
91a2025
feat: add `external-auth` cli (#10052)
kylecarbs Oct 9, 2023
54509e5
feat: allow storing extra oauth token properties in the database (#10…
kylecarbs Oct 9, 2023
3fe8966
bump migration number:
Emyrk Oct 10, 2023
44bbbfe
Swagger annotations
Emyrk Oct 10, 2023
f0294c3
Merge remote-tracking branch 'origin/main' into stevenmasley/soft_del…
Emyrk Oct 10, 2023
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
Merge remote-tracking branch 'origin/main' into stevenmasley/soft_del…
…ete_versions
  • Loading branch information
Emyrk committed Oct 10, 2023
commit f0294c30d1ca36f6aa6bf5778bd361723eb5fd60
48 changes: 33 additions & 15 deletions cli/externalauth.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package cli

import (
"encoding/json"
"os/signal"

"golang.org/x/xerrors"

"github.com/tidwall/gjson"

"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk/agentsdk"
Expand All @@ -25,7 +28,7 @@ func (r *RootCmd) externalAuth() *clibase.Cmd {
}

func (r *RootCmd) externalAuthAccessToken() *clibase.Cmd {
var silent bool
var extra string
return &clibase.Cmd{
Use: "access-token <provider>",
Short: "Print auth for an external provider",
Expand All @@ -45,12 +48,16 @@ else
fi
`,
},
example{
Description: "Obtain an extra property of an access token for additional metadata.",
Command: "coder external-auth access-token slack --extra \"authed_user.id\"",
},
),
Options: clibase.OptionSet{{
Name: "Silent",
Flag: "s",
Description: "Do not print the URL or access token.",
Value: clibase.BoolOf(&silent),
Name: "Extra",
Flag: "extra",
Description: "Extract a field from the \"extra\" properties of the OAuth token.",
Value: clibase.StringOf(&extra),
}},

Handler: func(inv *clibase.Invocation) error {
Expand All @@ -64,26 +71,37 @@ fi
return xerrors.Errorf("create agent client: %w", err)
}

token, err := client.ExternalAuth(ctx, agentsdk.ExternalAuthRequest{
extAuth, 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 extAuth.URL != "" {
_, err = inv.Stdout.Write([]byte(extAuth.URL))
if err != nil {
return err
}
return cliui.Canceled
}
if extra != "" {
if extAuth.TokenExtra == nil {
return xerrors.Errorf("no extra properties found for token")
}
data, err := json.Marshal(extAuth.TokenExtra)
if err != nil {
return xerrors.Errorf("marshal extra properties: %w", err)
}
result := gjson.GetBytes(data, extra)
_, err = inv.Stdout.Write([]byte(result.String()))
if err != nil {
return err
}
return nil
}

if token.URL != "" {
return cliui.Canceled
_, err = inv.Stdout.Write([]byte(extAuth.AccessToken))
if err != nil {
return err
}
return nil
},
Expand Down
18 changes: 18 additions & 0 deletions cli/externalauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,22 @@ func TestExternalAuth(t *testing.T) {
clitest.Start(t, inv)
pty.ExpectMatch("bananas")
})
t.Run("SuccessWithExtra", 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",
TokenExtra: map[string]interface{}{
"hey": "there",
},
})
}))
t.Cleanup(srv.Close)
url := srv.URL
inv, _ := clitest.New(t, "--agent-url", url, "external-auth", "access-token", "github", "--extra", "hey")
pty := ptytest.New(t)
inv.Stdout = pty.Output()
clitest.Start(t, inv)
pty.ExpectMatch("there")
})
}
8 changes: 6 additions & 2 deletions cli/testdata/coder_external-auth_access-token_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ USAGE:
echo "Please authenticate with GitHub:"
echo $OUTPUT
fi

- Obtain an extra property of an access token for additional metadata.:

$ coder external-auth access-token slack --extra "authed_user.id"

OPTIONS:
--s bool
Do not print the URL or access token.
--extra string
Extract a field from the "extra" properties of the OAuth token.

———
Run `coder --help` for a list of global options.
4 changes: 4 additions & 0 deletions coderd/apidoc/docs.go

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

4 changes: 4 additions & 0 deletions coderd/apidoc/swagger.json

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

36 changes: 29 additions & 7 deletions coderd/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -2178,13 +2178,14 @@ func (api *API) postWorkspaceAppHealth(rw http.ResponseWriter, r *http.Request)
// @Router /workspaceagents/me/external-auth [get]
func (api *API) workspaceAgentsExternalAuth(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
query := r.URL.Query()
// Either match or configID must be provided!
match := r.URL.Query().Get("match")
match := query.Get("match")
if match == "" {
// Support legacy agents!
match = r.URL.Query().Get("url")
match = query.Get("url")
}
id := chi.URLParam(r, "id")
id := query.Get("id")
if match == "" && id == "" {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "'url' or 'id' must be provided!",
Expand Down Expand Up @@ -2306,7 +2307,15 @@ func (api *API) workspaceAgentsExternalAuth(rw http.ResponseWriter, r *http.Requ
if !valid {
continue
}
httpapi.Write(ctx, rw, http.StatusOK, createExternalAuthResponse(externalAuthConfig.Type, externalAuthLink.OAuthAccessToken))
resp, err := createExternalAuthResponse(externalAuthConfig.Type, externalAuthLink.OAuthAccessToken, externalAuthLink.OAuthExtra)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to create external auth response.",
Detail: err.Error(),
})
return
}
httpapi.Write(ctx, rw, http.StatusOK, resp)
return
}
}
Expand Down Expand Up @@ -2354,13 +2363,21 @@ func (api *API) workspaceAgentsExternalAuth(rw http.ResponseWriter, r *http.Requ
})
return
}
httpapi.Write(ctx, rw, http.StatusOK, createExternalAuthResponse(externalAuthConfig.Type, externalAuthLink.OAuthAccessToken))
resp, err := createExternalAuthResponse(externalAuthConfig.Type, externalAuthLink.OAuthAccessToken, externalAuthLink.OAuthExtra)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to create external auth response.",
Detail: err.Error(),
})
return
}
httpapi.Write(ctx, rw, http.StatusOK, resp)
}

// createExternalAuthResponse creates an ExternalAuthResponse based on the
// provider type. This is to support legacy `/workspaceagents/me/gitauth`
// which uses `Username` and `Password`.
func createExternalAuthResponse(typ, token string) agentsdk.ExternalAuthResponse {
func createExternalAuthResponse(typ, token string, extra pqtype.NullRawMessage) (agentsdk.ExternalAuthResponse, error) {
var resp agentsdk.ExternalAuthResponse
switch typ {
case string(codersdk.EnhancedExternalAuthProviderGitLab):
Expand All @@ -2382,7 +2399,12 @@ func createExternalAuthResponse(typ, token string) agentsdk.ExternalAuthResponse
}
resp.AccessToken = token
resp.Type = typ
return resp

var err error
if extra.Valid {
err = json.Unmarshal(extra.RawMessage, &resp.TokenExtra)
}
return resp, err
}

// wsNetConn wraps net.Conn created by websocket.NetConn(). Cancel func
Expand Down
7 changes: 4 additions & 3 deletions codersdk/agentsdk/agentsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,9 +711,10 @@ func (c *Client) GetServiceBanner(ctx context.Context) (codersdk.ServiceBannerCo
}

type ExternalAuthResponse struct {
AccessToken string `json:"access_token"`
URL string `json:"url"`
Type string `json:"type"`
AccessToken string `json:"access_token"`
TokenExtra map[string]interface{} `json:"token_extra"`
URL string `json:"url"`
Type string `json:"type"`

// Deprecated: Only supported on `/workspaceagents/me/gitauth`
// for backwards compatibility.
Expand Down
2 changes: 2 additions & 0 deletions docs/api/agents.md

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

2 changes: 2 additions & 0 deletions docs/api/schemas.md

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

14 changes: 9 additions & 5 deletions docs/cli/external-auth_access-token.md

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

You are viewing a condensed version of this merge commit. You can view the full changes here.