mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: refresh dynamic parameters on secret changes (#24786)
Publishes user secret create, update, and delete events and subscribes dynamic parameter websockets to authorized owner secret changes. Secret changes trigger fresh renders with monotonic response IDs, with backend tests covering subscription authorization and websocket refresh behavior.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package coderd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/http"
|
||||
@@ -9,11 +10,13 @@ import (
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"cdr.dev/slog/v3"
|
||||
"github.com/coder/coder/v2/coderd/audit"
|
||||
"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/coderd/usersecretspubsub"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
)
|
||||
|
||||
@@ -106,6 +109,14 @@ func (api *API) postUserSecret(rw http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
aReq.New = secret
|
||||
|
||||
api.publishUserSecretEvent(ctx, usersecretspubsub.Event{
|
||||
Kind: usersecretspubsub.EventKindCreated,
|
||||
UserID: secret.UserID,
|
||||
Name: secret.Name,
|
||||
EnvName: secret.EnvName,
|
||||
FilePath: secret.FilePath,
|
||||
})
|
||||
|
||||
httpapi.Write(ctx, rw, http.StatusCreated, db2sdk.UserSecretFromFull(secret))
|
||||
}
|
||||
|
||||
@@ -303,6 +314,14 @@ func (api *API) patchUserSecret(rw http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
api.publishUserSecretEvent(ctx, usersecretspubsub.Event{
|
||||
Kind: usersecretspubsub.EventKindUpdated,
|
||||
UserID: secret.UserID,
|
||||
Name: secret.Name,
|
||||
EnvName: secret.EnvName,
|
||||
FilePath: secret.FilePath,
|
||||
})
|
||||
|
||||
httpapi.Write(ctx, rw, http.StatusOK, db2sdk.UserSecretFromFull(secret))
|
||||
}
|
||||
|
||||
@@ -346,5 +365,22 @@ func (api *API) deleteUserSecret(rw http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
aReq.Old = deleted
|
||||
|
||||
api.publishUserSecretEvent(ctx, usersecretspubsub.Event{
|
||||
Kind: usersecretspubsub.EventKindDeleted,
|
||||
UserID: user.ID,
|
||||
Name: name,
|
||||
})
|
||||
|
||||
rw.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (api *API) publishUserSecretEvent(ctx context.Context, event usersecretspubsub.Event) {
|
||||
if err := usersecretspubsub.Publish(api.Pubsub, event); err != nil {
|
||||
api.Logger.Warn(ctx, "failed to publish user secret event",
|
||||
slog.F("user_id", event.UserID),
|
||||
slog.F("secret_name", event.Name),
|
||||
slog.F("event_kind", event.Kind),
|
||||
slog.Error(err),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user