2022-04-06 00:18:26 +00:00
|
|
|
package coderd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/coder/coder/coderd/database"
|
|
|
|
"github.com/coder/coder/coderd/gitsshkey"
|
|
|
|
"github.com/coder/coder/coderd/httpapi"
|
|
|
|
"github.com/coder/coder/coderd/httpmw"
|
2022-05-17 18:43:19 +00:00
|
|
|
"github.com/coder/coder/coderd/rbac"
|
2022-04-06 00:18:26 +00:00
|
|
|
"github.com/coder/coder/codersdk"
|
|
|
|
)
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) regenerateGitSSHKey(rw http.ResponseWriter, r *http.Request) {
|
2022-09-21 22:07:00 +00:00
|
|
|
ctx := r.Context()
|
2022-04-06 00:18:26 +00:00
|
|
|
user := httpmw.UserParam(r)
|
2022-05-17 18:43:19 +00:00
|
|
|
|
2022-06-14 15:14:05 +00:00
|
|
|
if !api.Authorize(r, rbac.ActionUpdate, rbac.ResourceUserData.WithOwner(user.ID.String())) {
|
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-05-17 18:43:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-06 00:18:26 +00:00
|
|
|
privateKey, publicKey, err := gitsshkey.Generate(api.SSHKeygenAlgorithm)
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error generating a new SSH keypair.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-06 00:18:26 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
err = api.Database.UpdateGitSSHKey(ctx, database.UpdateGitSSHKeyParams{
|
2022-04-06 00:18:26 +00:00
|
|
|
UserID: user.ID,
|
|
|
|
UpdatedAt: database.Now(),
|
|
|
|
PrivateKey: privateKey,
|
|
|
|
PublicKey: publicKey,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error updating user's git SSH key.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-06 00:18:26 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
newKey, err := api.Database.GetGitSSHKey(ctx, user.ID)
|
2022-04-06 00:18:26 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching user's git SSH key.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-06 00:18:26 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusOK, codersdk.GitSSHKey{
|
2022-04-06 00:18:26 +00:00
|
|
|
UserID: newKey.UserID,
|
|
|
|
CreatedAt: newKey.CreatedAt,
|
|
|
|
UpdatedAt: newKey.UpdatedAt,
|
|
|
|
// No need to return the private key to the user
|
|
|
|
PublicKey: newKey.PublicKey,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) gitSSHKey(rw http.ResponseWriter, r *http.Request) {
|
2022-09-21 22:07:00 +00:00
|
|
|
ctx := r.Context()
|
2022-04-06 00:18:26 +00:00
|
|
|
user := httpmw.UserParam(r)
|
2022-05-17 18:43:19 +00:00
|
|
|
|
2022-06-14 15:14:05 +00:00
|
|
|
if !api.Authorize(r, rbac.ActionRead, rbac.ResourceUserData.WithOwner(user.ID.String())) {
|
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-05-17 18:43:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
gitSSHKey, err := api.Database.GetGitSSHKey(ctx, user.ID)
|
2022-04-06 00:18:26 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching user's SSH key.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-06 00:18:26 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusOK, codersdk.GitSSHKey{
|
2022-04-06 00:18:26 +00:00
|
|
|
UserID: gitSSHKey.UserID,
|
|
|
|
CreatedAt: gitSSHKey.CreatedAt,
|
|
|
|
UpdatedAt: gitSSHKey.UpdatedAt,
|
|
|
|
// No need to return the private key to the user
|
|
|
|
PublicKey: gitSSHKey.PublicKey,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) agentGitSSHKey(rw http.ResponseWriter, r *http.Request) {
|
2022-09-21 22:07:00 +00:00
|
|
|
ctx := r.Context()
|
2022-04-06 00:18:26 +00:00
|
|
|
agent := httpmw.WorkspaceAgent(r)
|
2022-09-21 22:07:00 +00:00
|
|
|
resource, err := api.Database.GetWorkspaceResourceByID(ctx, agent.ResourceID)
|
2022-04-06 00:18:26 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching workspace resource.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-06 00:18:26 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
job, err := api.Database.GetWorkspaceBuildByJobID(ctx, resource.JobID)
|
2022-04-06 00:18:26 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching workspace build.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-06 00:18:26 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
workspace, err := api.Database.GetWorkspaceByID(ctx, job.WorkspaceID)
|
2022-04-06 00:18:26 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching workspace.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-06 00:18:26 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
gitSSHKey, err := api.Database.GetGitSSHKey(ctx, workspace.OwnerID)
|
2022-04-06 00:18:26 +00:00
|
|
|
if err != nil {
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching git SSH key.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-06 00:18:26 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-21 22:07:00 +00:00
|
|
|
httpapi.Write(ctx, rw, http.StatusOK, codersdk.AgentGitSSHKey{
|
2022-04-25 05:29:15 +00:00
|
|
|
PublicKey: gitSSHKey.PublicKey,
|
2022-04-06 00:18:26 +00:00
|
|
|
PrivateKey: gitSSHKey.PrivateKey,
|
|
|
|
})
|
|
|
|
}
|