2026-04-09 12:12:55 -06:00
|
|
|
package coderd
|
|
|
|
|
|
|
|
|
|
import (
|
2026-05-06 09:27:24 -07:00
|
|
|
"context"
|
2026-04-09 12:12:55 -06:00
|
|
|
"database/sql"
|
|
|
|
|
"errors"
|
2026-05-26 14:42:31 -06:00
|
|
|
"fmt"
|
2026-04-09 12:12:55 -06:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
|
"github.com/google/uuid"
|
2026-04-29 12:57:47 -06:00
|
|
|
"golang.org/x/xerrors"
|
2026-04-09 12:12:55 -06:00
|
|
|
|
2026-04-29 12:57:47 -06:00
|
|
|
"github.com/coder/coder/v2/coderd/audit"
|
2026-04-09 12:12:55 -06:00
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
|
|
|
"github.com/coder/coder/v2/coderd/database/db2sdk"
|
|
|
|
|
"github.com/coder/coder/v2/coderd/httpapi"
|
|
|
|
|
"github.com/coder/coder/v2/coderd/httpmw"
|
|
|
|
|
"github.com/coder/coder/v2/codersdk"
|
|
|
|
|
)
|
|
|
|
|
|
2026-05-19 09:30:31 -07:00
|
|
|
const (
|
2026-05-26 14:42:31 -06:00
|
|
|
// These names are raised by the enforce_user_secrets_per_user_limits
|
|
|
|
|
// trigger with USING CONSTRAINT. They are not table CHECK
|
|
|
|
|
// constraints, so dbgen does not emit them in check_constraint.go.
|
|
|
|
|
userSecretsCountLimitConstraint database.CheckConstraint = "user_secrets_per_user_count_limit"
|
|
|
|
|
userSecretsTotalBytesLimitConstraint database.CheckConstraint = "user_secrets_per_user_total_bytes_limit"
|
|
|
|
|
userSecretsEnvBytesLimitConstraint database.CheckConstraint = "user_secrets_per_user_env_bytes_limit"
|
2026-05-19 09:30:31 -07:00
|
|
|
)
|
|
|
|
|
|
2026-07-28 09:58:33 -06:00
|
|
|
// errUserSecretInjectionTargetRequired signals that a PATCH would leave an
|
|
|
|
|
// enabled secret with both env_name and file_path empty. It is returned
|
|
|
|
|
// from the patchUserSecret transaction so the handler can map it to a 400.
|
|
|
|
|
// Creates enforce the same invariant in
|
|
|
|
|
// codersdk.ValidateCreateUserSecretRequest.
|
|
|
|
|
var errUserSecretInjectionTargetRequired = xerrors.New("enabled user secret must have at least one of env_name or file_path set")
|
|
|
|
|
|
2026-04-09 12:12:55 -06:00
|
|
|
// @Summary Create a new user secret
|
|
|
|
|
// @ID create-a-new-user-secret
|
|
|
|
|
// @Security CoderSessionToken
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Tags Secrets
|
|
|
|
|
// @Param user path string true "User ID, username, or me"
|
|
|
|
|
// @Param request body codersdk.CreateUserSecretRequest true "Create secret request"
|
|
|
|
|
// @Success 201 {object} codersdk.UserSecret
|
2026-05-05 02:54:03 +05:00
|
|
|
// @Router /api/v2/users/{user}/secrets [post]
|
2026-04-09 12:12:55 -06:00
|
|
|
func (api *API) postUserSecret(rw http.ResponseWriter, r *http.Request) {
|
2026-04-29 12:57:47 -06:00
|
|
|
var (
|
|
|
|
|
ctx = r.Context()
|
|
|
|
|
user = httpmw.UserParam(r)
|
|
|
|
|
auditor = api.Auditor.Load()
|
|
|
|
|
aReq, commitAudit = audit.InitRequest[database.UserSecret](rw, &audit.RequestParams{
|
|
|
|
|
Audit: *auditor,
|
|
|
|
|
Log: api.Logger,
|
|
|
|
|
Request: r,
|
|
|
|
|
Action: database.AuditActionCreate,
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
defer commitAudit()
|
2026-04-09 12:12:55 -06:00
|
|
|
|
|
|
|
|
var req codersdk.CreateUserSecretRequest
|
|
|
|
|
if !httpapi.Read(ctx, rw, r, &req) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-22 09:23:38 -04:00
|
|
|
if validations := codersdk.ValidateCreateUserSecretRequest(req); len(validations) > 0 {
|
2026-05-19 09:30:31 -07:00
|
|
|
writeUserSecretValidationErrors(ctx, rw, http.StatusBadRequest, validations)
|
2026-04-09 12:12:55 -06:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-28 09:58:33 -06:00
|
|
|
enabled := true
|
|
|
|
|
if req.Enabled != nil {
|
|
|
|
|
enabled = *req.Enabled
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-09 12:12:55 -06:00
|
|
|
secret, err := api.Database.CreateUserSecret(ctx, database.CreateUserSecretParams{
|
|
|
|
|
ID: uuid.New(),
|
|
|
|
|
UserID: user.ID,
|
|
|
|
|
Name: req.Name,
|
|
|
|
|
Description: req.Description,
|
|
|
|
|
Value: req.Value,
|
|
|
|
|
ValueKeyID: sql.NullString{},
|
|
|
|
|
EnvName: req.EnvName,
|
|
|
|
|
FilePath: req.FilePath,
|
2026-07-28 09:58:33 -06:00
|
|
|
Enabled: enabled,
|
2026-04-09 12:12:55 -06:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
2026-05-19 09:30:31 -07:00
|
|
|
if validations := userSecretConflictValidationErrors(err); len(validations) > 0 {
|
|
|
|
|
writeUserSecretValidationErrors(ctx, rw, http.StatusConflict, validations)
|
2026-04-09 12:12:55 -06:00
|
|
|
return
|
|
|
|
|
}
|
2026-07-28 09:58:33 -06:00
|
|
|
if validations := userSecretInjectionTargetValidationErrors(err); len(validations) > 0 {
|
|
|
|
|
writeUserSecretValidationErrors(ctx, rw, http.StatusBadRequest, validations)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-05-26 14:42:31 -06:00
|
|
|
if resp, ok := userSecretLimitResponse(err); ok {
|
|
|
|
|
httpapi.Write(ctx, rw, http.StatusBadRequest, resp)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-07-23 17:55:34 -04:00
|
|
|
if httpapi.IsUnauthorizedError(err) {
|
|
|
|
|
httpapi.Forbidden(rw)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-04-09 12:12:55 -06:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
|
|
|
|
Message: "Internal error creating secret.",
|
|
|
|
|
Detail: err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-04-29 12:57:47 -06:00
|
|
|
aReq.New = secret
|
2026-04-09 12:12:55 -06:00
|
|
|
|
|
|
|
|
httpapi.Write(ctx, rw, http.StatusCreated, db2sdk.UserSecretFromFull(secret))
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-23 17:55:34 -04:00
|
|
|
// @Summary Import user secrets from a file
|
|
|
|
|
// @ID import-user-secrets-from-a-file
|
|
|
|
|
// @Security CoderSessionToken
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Tags Secrets
|
|
|
|
|
// @Param user path string true "User ID, username, or me"
|
|
|
|
|
// @Param request body codersdk.ImportUserSecretsRequest true "Import secrets request"
|
|
|
|
|
// @Success 201 {array} codersdk.UserSecret
|
|
|
|
|
// @Failure 400 {object} codersdk.Response
|
|
|
|
|
// @Failure 409 {object} codersdk.Response
|
|
|
|
|
// @Failure 413 {object} codersdk.Response
|
|
|
|
|
// @Router /api/v2/users/{user}/secrets/batch [post]
|
|
|
|
|
func (api *API) postUserSecretsBatch(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
user := httpmw.UserParam(r)
|
|
|
|
|
|
|
|
|
|
// Cap body size before reading; worst-case JSON escaping can inflate
|
|
|
|
|
// a max-size file several-fold, so 8x gives comfortable headroom.
|
|
|
|
|
r.Body = http.MaxBytesReader(rw, r.Body, 8*codersdk.MaxSecretsFileBytes)
|
|
|
|
|
var req codersdk.ImportUserSecretsRequest
|
|
|
|
|
if !httpapi.Read(ctx, rw, r, &req) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reqs, err := codersdk.ParseSecretsFile(req.Format, req.Content)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
|
|
|
|
|
Message: "Failed to parse secrets file.",
|
|
|
|
|
Detail: err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate every entry and accumulate all errors so the caller can
|
|
|
|
|
// fix the whole file in one round-trip. Each field is prefixed with
|
|
|
|
|
// the entry index, e.g. "secrets[2].env_name".
|
|
|
|
|
var validations []codersdk.ValidationError
|
|
|
|
|
for i, sreq := range reqs {
|
|
|
|
|
for _, v := range codersdk.ValidateCreateUserSecretRequest(sreq) {
|
|
|
|
|
validations = append(validations, codersdk.ValidationError{
|
|
|
|
|
Field: fmt.Sprintf("secrets[%d].%s", i, v.Field),
|
|
|
|
|
Detail: v.Detail,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if len(validations) > 0 {
|
|
|
|
|
writeUserSecretValidationErrors(ctx, rw, http.StatusBadRequest, validations)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert atomically. The per-user-limit trigger fires per row, and
|
|
|
|
|
// any unique or limit violation aborts the whole transaction, so a
|
|
|
|
|
// failed import creates nothing. failedIndex records which entry
|
|
|
|
|
// failed so the error can be attributed to it after the rollback.
|
|
|
|
|
var created []database.UserSecret
|
|
|
|
|
failedIndex := -1
|
|
|
|
|
err = api.Database.InTx(func(tx database.Store) error {
|
|
|
|
|
for i, sreq := range reqs {
|
2026-07-28 09:58:33 -06:00
|
|
|
enabled := true
|
|
|
|
|
if sreq.Enabled != nil {
|
|
|
|
|
enabled = *sreq.Enabled
|
|
|
|
|
}
|
2026-07-23 17:55:34 -04:00
|
|
|
s, txErr := tx.CreateUserSecret(ctx, database.CreateUserSecretParams{
|
|
|
|
|
ID: uuid.New(),
|
|
|
|
|
UserID: user.ID,
|
|
|
|
|
Name: sreq.Name,
|
|
|
|
|
Description: sreq.Description,
|
|
|
|
|
Value: sreq.Value,
|
|
|
|
|
ValueKeyID: sql.NullString{},
|
|
|
|
|
EnvName: sreq.EnvName,
|
|
|
|
|
FilePath: sreq.FilePath,
|
2026-07-28 09:58:33 -06:00
|
|
|
Enabled: enabled,
|
2026-07-23 17:55:34 -04:00
|
|
|
})
|
|
|
|
|
if txErr != nil {
|
|
|
|
|
failedIndex = i
|
|
|
|
|
return txErr
|
|
|
|
|
}
|
|
|
|
|
created = append(created, s)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
index := failedIndex
|
|
|
|
|
|
|
|
|
|
if conflicts := userSecretConflictValidationErrors(err); len(conflicts) > 0 {
|
|
|
|
|
if index >= 0 {
|
|
|
|
|
for i := range conflicts {
|
|
|
|
|
conflicts[i].Field = fmt.Sprintf("secrets[%d].%s", index, conflicts[i].Field)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
writeUserSecretValidationErrors(ctx, rw, http.StatusConflict, conflicts)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-07-28 09:58:33 -06:00
|
|
|
if validations := userSecretInjectionTargetValidationErrors(err); len(validations) > 0 {
|
|
|
|
|
if index >= 0 {
|
|
|
|
|
for i := range validations {
|
|
|
|
|
validations[i].Field = fmt.Sprintf("secrets[%d].%s", index, validations[i].Field)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
writeUserSecretValidationErrors(ctx, rw, http.StatusBadRequest, validations)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-07-23 17:55:34 -04:00
|
|
|
if resp, ok := userSecretLimitResponse(err); ok {
|
|
|
|
|
if index >= 0 {
|
|
|
|
|
resp.Detail = fmt.Sprintf("Entry secrets[%d] (%q): %s", index, reqs[index].Name, resp.Detail)
|
|
|
|
|
}
|
|
|
|
|
httpapi.Write(ctx, rw, http.StatusBadRequest, resp)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if httpapi.IsUnauthorizedError(err) {
|
|
|
|
|
httpapi.Forbidden(rw)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
|
|
|
|
Message: "Internal error importing secrets.",
|
|
|
|
|
Detail: err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Emit audit logs only after the transaction commits so a rolled-back
|
|
|
|
|
// batch produces zero logs. One create log is emitted per secret
|
|
|
|
|
// because database.UserSecret is registered as auditable.
|
|
|
|
|
auditor := api.Auditor.Load()
|
|
|
|
|
requestID := httpmw.RequestID(r)
|
|
|
|
|
auditCtx := context.WithoutCancel(ctx)
|
|
|
|
|
for _, secret := range created {
|
|
|
|
|
audit.BackgroundAudit(auditCtx, &audit.BackgroundAuditParams[database.UserSecret]{
|
|
|
|
|
Audit: *auditor,
|
|
|
|
|
Log: api.Logger,
|
|
|
|
|
UserID: user.ID,
|
|
|
|
|
RequestID: requestID,
|
|
|
|
|
Status: http.StatusCreated,
|
|
|
|
|
IP: r.RemoteAddr,
|
|
|
|
|
UserAgent: r.UserAgent(),
|
|
|
|
|
Action: database.AuditActionCreate,
|
|
|
|
|
New: secret,
|
|
|
|
|
Old: database.UserSecret{},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out := make([]codersdk.UserSecret, 0, len(created))
|
|
|
|
|
for _, secret := range created {
|
|
|
|
|
out = append(out, db2sdk.UserSecretFromFull(secret))
|
|
|
|
|
}
|
|
|
|
|
httpapi.Write(ctx, rw, http.StatusCreated, out)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-09 12:12:55 -06:00
|
|
|
// @Summary List user secrets
|
|
|
|
|
// @ID list-user-secrets
|
|
|
|
|
// @Security CoderSessionToken
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Tags Secrets
|
|
|
|
|
// @Param user path string true "User ID, username, or me"
|
|
|
|
|
// @Success 200 {array} codersdk.UserSecret
|
2026-05-05 02:54:03 +05:00
|
|
|
// @Router /api/v2/users/{user}/secrets [get]
|
2026-04-09 12:12:55 -06:00
|
|
|
func (api *API) getUserSecrets(rw http.ResponseWriter, r *http.Request) { //nolint:revive // Method name matches route.
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
user := httpmw.UserParam(r)
|
|
|
|
|
|
|
|
|
|
secrets, err := api.Database.ListUserSecrets(ctx, user.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
|
|
|
|
Message: "Internal error listing secrets.",
|
|
|
|
|
Detail: err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
httpapi.Write(ctx, rw, http.StatusOK, db2sdk.UserSecrets(secrets))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @Summary Get a user secret by name
|
|
|
|
|
// @ID get-a-user-secret-by-name
|
|
|
|
|
// @Security CoderSessionToken
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Tags Secrets
|
|
|
|
|
// @Param user path string true "User ID, username, or me"
|
|
|
|
|
// @Param name path string true "Secret name"
|
|
|
|
|
// @Success 200 {object} codersdk.UserSecret
|
2026-05-05 02:54:03 +05:00
|
|
|
// @Router /api/v2/users/{user}/secrets/{name} [get]
|
2026-04-09 12:12:55 -06:00
|
|
|
func (api *API) getUserSecret(rw http.ResponseWriter, r *http.Request) { //nolint:revive // Method name matches route.
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
user := httpmw.UserParam(r)
|
2026-07-22 09:23:38 -04:00
|
|
|
name := chi.URLParam(r, codersdk.UserSecretNameField)
|
2026-04-09 12:12:55 -06:00
|
|
|
|
|
|
|
|
secret, err := api.Database.GetUserSecretByUserIDAndName(ctx, database.GetUserSecretByUserIDAndNameParams{
|
|
|
|
|
UserID: user.ID,
|
|
|
|
|
Name: name,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
|
httpapi.ResourceNotFound(rw)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
|
|
|
|
Message: "Internal error fetching secret.",
|
|
|
|
|
Detail: err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
httpapi.Write(ctx, rw, http.StatusOK, db2sdk.UserSecretFromFull(secret))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @Summary Update a user secret
|
|
|
|
|
// @ID update-a-user-secret
|
|
|
|
|
// @Security CoderSessionToken
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Tags Secrets
|
|
|
|
|
// @Param user path string true "User ID, username, or me"
|
|
|
|
|
// @Param name path string true "Secret name"
|
|
|
|
|
// @Param request body codersdk.UpdateUserSecretRequest true "Update secret request"
|
|
|
|
|
// @Success 200 {object} codersdk.UserSecret
|
2026-05-05 02:54:03 +05:00
|
|
|
// @Router /api/v2/users/{user}/secrets/{name} [patch]
|
2026-04-09 12:12:55 -06:00
|
|
|
func (api *API) patchUserSecret(rw http.ResponseWriter, r *http.Request) {
|
2026-04-29 12:57:47 -06:00
|
|
|
var (
|
|
|
|
|
ctx = r.Context()
|
|
|
|
|
user = httpmw.UserParam(r)
|
2026-07-22 09:23:38 -04:00
|
|
|
name = chi.URLParam(r, codersdk.UserSecretNameField)
|
2026-04-29 12:57:47 -06:00
|
|
|
auditor = api.Auditor.Load()
|
|
|
|
|
aReq, commitAudit = audit.InitRequest[database.UserSecret](rw, &audit.RequestParams{
|
|
|
|
|
Audit: *auditor,
|
|
|
|
|
Log: api.Logger,
|
|
|
|
|
Request: r,
|
|
|
|
|
Action: database.AuditActionWrite,
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
defer commitAudit()
|
2026-04-09 12:12:55 -06:00
|
|
|
|
|
|
|
|
var req codersdk.UpdateUserSecretRequest
|
|
|
|
|
if !httpapi.Read(ctx, rw, r, &req) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-28 09:58:33 -06:00
|
|
|
if req.Value == nil && req.Description == nil && req.EnvName == nil && req.FilePath == nil && req.Enabled == nil {
|
2026-04-09 12:12:55 -06:00
|
|
|
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
|
|
|
|
|
Message: "At least one field must be provided.",
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-05-19 09:30:31 -07:00
|
|
|
if validations := updateUserSecretValidationErrors(req); len(validations) > 0 {
|
|
|
|
|
writeUserSecretValidationErrors(ctx, rw, http.StatusBadRequest, validations)
|
|
|
|
|
return
|
2026-04-29 12:57:47 -06:00
|
|
|
}
|
2026-04-09 12:12:55 -06:00
|
|
|
|
|
|
|
|
params := database.UpdateUserSecretByUserIDAndNameParams{
|
|
|
|
|
UserID: user.ID,
|
|
|
|
|
Name: name,
|
|
|
|
|
UpdateValue: req.Value != nil,
|
|
|
|
|
Value: "",
|
|
|
|
|
ValueKeyID: sql.NullString{},
|
|
|
|
|
UpdateDescription: req.Description != nil,
|
|
|
|
|
Description: "",
|
|
|
|
|
UpdateEnvName: req.EnvName != nil,
|
|
|
|
|
EnvName: "",
|
|
|
|
|
UpdateFilePath: req.FilePath != nil,
|
|
|
|
|
FilePath: "",
|
2026-07-28 09:58:33 -06:00
|
|
|
UpdateEnabled: req.Enabled != nil,
|
|
|
|
|
Enabled: false,
|
2026-04-09 12:12:55 -06:00
|
|
|
}
|
|
|
|
|
if req.Value != nil {
|
|
|
|
|
params.Value = *req.Value
|
|
|
|
|
}
|
|
|
|
|
if req.Description != nil {
|
|
|
|
|
params.Description = *req.Description
|
|
|
|
|
}
|
|
|
|
|
if req.EnvName != nil {
|
|
|
|
|
params.EnvName = *req.EnvName
|
|
|
|
|
}
|
|
|
|
|
if req.FilePath != nil {
|
|
|
|
|
params.FilePath = *req.FilePath
|
|
|
|
|
}
|
2026-07-28 09:58:33 -06:00
|
|
|
if req.Enabled != nil {
|
|
|
|
|
params.Enabled = *req.Enabled
|
|
|
|
|
}
|
2026-04-09 12:12:55 -06:00
|
|
|
|
2026-04-29 12:57:47 -06:00
|
|
|
// Pre-read the secret inside a transaction so the audit diff has both an
|
|
|
|
|
// "old" and "new" snapshot.
|
|
|
|
|
//
|
|
|
|
|
// Under read committed isolation, a concurrent writer between our SELECT
|
|
|
|
|
// and our UPDATE can cause the audit diff to attribute changes to us that
|
|
|
|
|
// we did not make. We accept this race to match other audit log diffs
|
|
|
|
|
// (templates, workspaces, chats, etc). In practice this should be unlikely
|
|
|
|
|
// to hit since a user can only modify their own secrets.
|
|
|
|
|
var secret database.UserSecret
|
|
|
|
|
err := api.Database.InTx(func(tx database.Store) error {
|
|
|
|
|
old, err := tx.GetUserSecretByUserIDAndName(ctx, database.GetUserSecretByUserIDAndNameParams{
|
|
|
|
|
UserID: user.ID,
|
|
|
|
|
Name: name,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return xerrors.Errorf("fetch user secret: %w", err)
|
|
|
|
|
}
|
|
|
|
|
aReq.Old = old
|
|
|
|
|
|
2026-07-28 09:58:33 -06:00
|
|
|
// Reject patches that would leave an enabled secret with both
|
|
|
|
|
// env_name and file_path empty. Evaluated against the post-update
|
|
|
|
|
// state so atomic env<->file swaps still succeed, and so targets
|
|
|
|
|
// can be cleared when the same PATCH also disables the secret.
|
|
|
|
|
postEnvName := old.EnvName
|
|
|
|
|
if req.EnvName != nil {
|
|
|
|
|
postEnvName = *req.EnvName
|
|
|
|
|
}
|
|
|
|
|
postFilePath := old.FilePath
|
|
|
|
|
if req.FilePath != nil {
|
|
|
|
|
postFilePath = *req.FilePath
|
|
|
|
|
}
|
|
|
|
|
postEnabled := old.Enabled
|
|
|
|
|
if req.Enabled != nil {
|
|
|
|
|
postEnabled = *req.Enabled
|
|
|
|
|
}
|
|
|
|
|
if postEnabled && postEnvName == "" && postFilePath == "" {
|
|
|
|
|
return errUserSecretInjectionTargetRequired
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-29 12:57:47 -06:00
|
|
|
updated, err := tx.UpdateUserSecretByUserIDAndName(ctx, params)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return xerrors.Errorf("update user secret: %w", err)
|
|
|
|
|
}
|
|
|
|
|
secret = updated
|
|
|
|
|
aReq.New = updated
|
|
|
|
|
return nil
|
|
|
|
|
}, nil)
|
2026-04-09 12:12:55 -06:00
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
|
httpapi.ResourceNotFound(rw)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-07-28 09:58:33 -06:00
|
|
|
if errors.Is(err, errUserSecretInjectionTargetRequired) {
|
|
|
|
|
writeUserSecretValidationErrors(ctx, rw, http.StatusBadRequest, []codersdk.ValidationError{{
|
|
|
|
|
Field: codersdk.UserSecretEnvNameField,
|
|
|
|
|
Detail: codersdk.UserSecretInjectionTargetRequiredDetail,
|
|
|
|
|
}})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if validations := userSecretInjectionTargetValidationErrors(err); len(validations) > 0 {
|
|
|
|
|
writeUserSecretValidationErrors(ctx, rw, http.StatusBadRequest, validations)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-05-19 09:30:31 -07:00
|
|
|
if validations := userSecretConflictValidationErrors(err); len(validations) > 0 {
|
|
|
|
|
writeUserSecretValidationErrors(ctx, rw, http.StatusConflict, validations)
|
2026-04-09 12:12:55 -06:00
|
|
|
return
|
|
|
|
|
}
|
2026-05-26 14:42:31 -06:00
|
|
|
if resp, ok := userSecretLimitResponse(err); ok {
|
|
|
|
|
httpapi.Write(ctx, rw, http.StatusBadRequest, resp)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-04-09 12:12:55 -06:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
|
|
|
|
Message: "Internal error updating secret.",
|
|
|
|
|
Detail: err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
httpapi.Write(ctx, rw, http.StatusOK, db2sdk.UserSecretFromFull(secret))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @Summary Delete a user secret
|
|
|
|
|
// @ID delete-a-user-secret
|
|
|
|
|
// @Security CoderSessionToken
|
|
|
|
|
// @Tags Secrets
|
|
|
|
|
// @Param user path string true "User ID, username, or me"
|
|
|
|
|
// @Param name path string true "Secret name"
|
|
|
|
|
// @Success 204
|
2026-05-05 02:54:03 +05:00
|
|
|
// @Router /api/v2/users/{user}/secrets/{name} [delete]
|
2026-04-09 12:12:55 -06:00
|
|
|
func (api *API) deleteUserSecret(rw http.ResponseWriter, r *http.Request) {
|
2026-04-29 12:57:47 -06:00
|
|
|
var (
|
|
|
|
|
ctx = r.Context()
|
|
|
|
|
user = httpmw.UserParam(r)
|
2026-07-22 09:23:38 -04:00
|
|
|
name = chi.URLParam(r, codersdk.UserSecretNameField)
|
2026-04-29 12:57:47 -06:00
|
|
|
auditor = api.Auditor.Load()
|
|
|
|
|
aReq, commitAudit = audit.InitRequest[database.UserSecret](rw, &audit.RequestParams{
|
|
|
|
|
Audit: *auditor,
|
|
|
|
|
Log: api.Logger,
|
|
|
|
|
Request: r,
|
|
|
|
|
Action: database.AuditActionDelete,
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
defer commitAudit()
|
2026-04-09 12:12:55 -06:00
|
|
|
|
2026-04-29 12:57:47 -06:00
|
|
|
deleted, err := api.Database.DeleteUserSecretByUserIDAndName(ctx, database.DeleteUserSecretByUserIDAndNameParams{
|
2026-04-09 12:12:55 -06:00
|
|
|
UserID: user.ID,
|
|
|
|
|
Name: name,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2026-04-29 12:57:47 -06:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
|
httpapi.ResourceNotFound(rw)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-04-09 12:12:55 -06:00
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
|
|
|
|
Message: "Internal error deleting secret.",
|
|
|
|
|
Detail: err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-04-29 12:57:47 -06:00
|
|
|
aReq.Old = deleted
|
2026-04-09 12:12:55 -06:00
|
|
|
|
|
|
|
|
rw.WriteHeader(http.StatusNoContent)
|
|
|
|
|
}
|
2026-05-06 09:27:24 -07:00
|
|
|
|
2026-05-19 09:30:31 -07:00
|
|
|
func writeUserSecretValidationErrors(ctx context.Context, rw http.ResponseWriter, status int, validations []codersdk.ValidationError) {
|
|
|
|
|
httpapi.Write(ctx, rw, status, codersdk.Response{
|
|
|
|
|
Message: "Validation failed.",
|
|
|
|
|
Validations: validations,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func updateUserSecretValidationErrors(req codersdk.UpdateUserSecretRequest) []codersdk.ValidationError {
|
|
|
|
|
var validations []codersdk.ValidationError
|
|
|
|
|
if req.Value != nil {
|
2026-07-22 09:23:38 -04:00
|
|
|
validations = appendUserSecretValidationError(validations, codersdk.UserSecretValueField, codersdk.UserSecretValueValid(*req.Value))
|
2026-05-19 09:30:31 -07:00
|
|
|
}
|
|
|
|
|
if req.EnvName != nil {
|
2026-07-22 09:23:38 -04:00
|
|
|
validations = appendUserSecretValidationError(validations, codersdk.UserSecretEnvNameField, codersdk.UserSecretEnvNameValid(*req.EnvName))
|
2026-05-19 09:30:31 -07:00
|
|
|
}
|
|
|
|
|
if req.FilePath != nil {
|
2026-07-22 09:23:38 -04:00
|
|
|
validations = appendUserSecretValidationError(validations, codersdk.UserSecretFilePathField, codersdk.UserSecretFilePathValid(*req.FilePath))
|
2026-05-19 09:30:31 -07:00
|
|
|
}
|
|
|
|
|
return validations
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func appendUserSecretValidationError(validations []codersdk.ValidationError, field string, err error) []codersdk.ValidationError {
|
|
|
|
|
if err == nil {
|
|
|
|
|
return validations
|
|
|
|
|
}
|
|
|
|
|
return append(validations, codersdk.ValidationError{
|
|
|
|
|
Field: field,
|
|
|
|
|
Detail: err.Error(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-26 14:42:31 -06:00
|
|
|
// userSecretLimitResponse maps a per-user-limits trigger violation
|
|
|
|
|
// (raised by enforce_user_secrets_per_user_limits) to a 400. Returns
|
|
|
|
|
// ok=false if err is not such a violation. See
|
|
|
|
|
// codersdk.MaxUserSecretsPerUserCount for the rationale behind the caps.
|
|
|
|
|
func userSecretLimitResponse(err error) (codersdk.Response, bool) {
|
|
|
|
|
switch {
|
|
|
|
|
case database.IsCheckViolation(err, userSecretsCountLimitConstraint):
|
|
|
|
|
return codersdk.Response{
|
|
|
|
|
Message: "User secrets limit reached.",
|
|
|
|
|
Detail: fmt.Sprintf(
|
|
|
|
|
"Each user can have at most %d secrets.",
|
|
|
|
|
codersdk.MaxUserSecretsPerUserCount,
|
|
|
|
|
),
|
|
|
|
|
}, true
|
|
|
|
|
case database.IsCheckViolation(err, userSecretsTotalBytesLimitConstraint):
|
|
|
|
|
return codersdk.Response{
|
|
|
|
|
Message: "User secrets value-bytes limit reached.",
|
|
|
|
|
Detail: fmt.Sprintf(
|
|
|
|
|
"Stored bytes of your secret values exceed the per-user "+
|
|
|
|
|
"budget (%d bytes after encryption, if applicable). "+
|
|
|
|
|
"Reduce the size or number of your secrets.",
|
|
|
|
|
codersdk.MaxUserSecretsTotalValueBytes,
|
|
|
|
|
),
|
|
|
|
|
}, true
|
|
|
|
|
case database.IsCheckViolation(err, userSecretsEnvBytesLimitConstraint):
|
|
|
|
|
return codersdk.Response{
|
|
|
|
|
Message: "Environment-injected user secrets bytes limit reached.",
|
|
|
|
|
Detail: fmt.Sprintf(
|
|
|
|
|
"Stored bytes of env-injected secret values exceed the "+
|
|
|
|
|
"per-user budget (%d bytes after encryption, if applicable). "+
|
|
|
|
|
"Clear env_name on large secrets or use file_path instead.",
|
|
|
|
|
codersdk.MaxUserSecretValueBytes,
|
|
|
|
|
),
|
|
|
|
|
}, true
|
|
|
|
|
}
|
|
|
|
|
return codersdk.Response{}, false
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-28 09:58:33 -06:00
|
|
|
// userSecretInjectionTargetValidationErrors maps the
|
|
|
|
|
// user_secrets_enabled_requires_target CHECK violation to a field-level
|
|
|
|
|
// validation error. The database constraint is the race-safe source of
|
|
|
|
|
// truth for the injection-target invariant: concurrent PATCHes can each
|
|
|
|
|
// clear a different target and pass the handler's own post-state check,
|
|
|
|
|
// so the constraint is what ultimately rejects an enabled target-less row.
|
|
|
|
|
func userSecretInjectionTargetValidationErrors(err error) []codersdk.ValidationError {
|
|
|
|
|
if database.IsCheckViolation(err, database.CheckUserSecretsEnabledRequiresTarget) {
|
|
|
|
|
return []codersdk.ValidationError{{
|
|
|
|
|
Field: codersdk.UserSecretEnvNameField,
|
|
|
|
|
Detail: codersdk.UserSecretInjectionTargetRequiredDetail,
|
|
|
|
|
}}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-19 09:30:31 -07:00
|
|
|
func userSecretConflictValidationErrors(err error) []codersdk.ValidationError {
|
|
|
|
|
switch {
|
|
|
|
|
case database.IsUniqueViolation(err, database.UniqueUserSecretsUserNameIndex):
|
|
|
|
|
return []codersdk.ValidationError{{
|
2026-07-22 09:23:38 -04:00
|
|
|
Field: codersdk.UserSecretNameField,
|
2026-05-19 09:30:31 -07:00
|
|
|
Detail: "name already in use",
|
|
|
|
|
}}
|
|
|
|
|
case database.IsUniqueViolation(err, database.UniqueUserSecretsUserEnvNameIndex):
|
|
|
|
|
return []codersdk.ValidationError{{
|
2026-07-22 09:23:38 -04:00
|
|
|
Field: codersdk.UserSecretEnvNameField,
|
2026-05-19 09:30:31 -07:00
|
|
|
Detail: "environment variable already in use",
|
|
|
|
|
}}
|
|
|
|
|
case database.IsUniqueViolation(err, database.UniqueUserSecretsUserFilePathIndex):
|
|
|
|
|
return []codersdk.ValidationError{{
|
2026-07-22 09:23:38 -04:00
|
|
|
Field: codersdk.UserSecretFilePathField,
|
2026-05-19 09:30:31 -07:00
|
|
|
Detail: "file path already in use",
|
|
|
|
|
}}
|
|
|
|
|
default:
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|