Skip to content

feat: Add user scoped git ssh keys #834

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 20 commits into from
Apr 6, 2022
Merged
Prev Previous commit
Next Next commit
Fix codersdk
  • Loading branch information
f0ssel committed Apr 5, 2022
commit 89bf77ca5481933f255eadaa1f65d31bda53a02f
4 changes: 1 addition & 3 deletions coderd/gitsshkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ func TestGitSSHKey(t *testing.T) {
key1, err := client.GitSSHKey(ctx, res.UserID)
require.NoError(t, err)
require.NotEmpty(t, key1.PublicKey)
err = client.RegenerateGitSSHKey(ctx, res.UserID)
require.NoError(t, err)
key2, err := client.GitSSHKey(ctx, res.UserID)
key2, err := client.RegenerateGitSSHKey(ctx, res.UserID)
require.NoError(t, err)
require.Greater(t, key2.UpdatedAt, key1.UpdatedAt)
require.NotEmpty(t, key2.PublicKey)
Expand Down
13 changes: 8 additions & 5 deletions codersdk/gitsshkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type AgentGitSSHKey struct {
PublicKey string `json:"public_key"`
}

// GitSSHKey returns the user
// GitSSHKey returns the user's git SSH public key.
func (c *Client) GitSSHKey(ctx context.Context, userID uuid.UUID) (GitSSHKey, error) {
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/users/%s/gitsshkey", userID.String()), nil)
if err != nil {
Expand All @@ -42,20 +42,23 @@ func (c *Client) GitSSHKey(ctx context.Context, userID uuid.UUID) (GitSSHKey, er
return gitsshkey, json.NewDecoder(res.Body).Decode(&gitsshkey)
}

func (c *Client) RegenerateGitSSHKey(ctx context.Context, userID uuid.UUID) error {
// RegenerateGitSSHKey will create a new SSH key pair for the user and return it.
func (c *Client) RegenerateGitSSHKey(ctx context.Context, userID uuid.UUID) (GitSSHKey, error) {
res, err := c.request(ctx, http.MethodPut, fmt.Sprintf("/api/v2/users/%s/gitsshkey", userID.String()), nil)
if err != nil {
return xerrors.Errorf("execute request: %w", err)
return GitSSHKey{}, xerrors.Errorf("execute request: %w", err)
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return readBodyAsError(res)
return GitSSHKey{}, readBodyAsError(res)
}

return nil
var gitsshkey GitSSHKey
return gitsshkey, json.NewDecoder(res.Body).Decode(&gitsshkey)
}

// AgentGitSSHKey will return the user's SSH key pair for the workspace.
func (c *Client) AgentGitSSHKey(ctx context.Context) (AgentGitSSHKey, error) {
res, err := c.request(ctx, http.MethodGet, "/api/v2/workspaceresources/agent/gitsshkey", nil)
if err != nil {
Expand Down