feat: Hide secrets on api and channels
This commit is contained in:
@@ -90,12 +90,57 @@ func CreateChannel(instanceID string, ch *models.NotificationChannel) (*models.N
|
||||
}
|
||||
|
||||
func UpdateChannel(instanceID, channelID string, upd bson.M) error {
|
||||
if cfg, ok := upd["config"].(map[string]string); ok {
|
||||
merged, err := mergeChannelSecrets(instanceID, channelID, upd, cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
upd["config"] = merged
|
||||
}
|
||||
ctx, cancel := monCtx()
|
||||
defer cancel()
|
||||
_, err := db.Col("notification_channels").UpdateOne(ctx, bson.M{"channel_id": channelID, "instance_id": instanceID}, bson.M{"$set": upd})
|
||||
return err
|
||||
}
|
||||
|
||||
// mergeChannelSecrets resolves models.RedactedSecret back to what it stood for.
|
||||
//
|
||||
// The API hands out a sentinel rather than the credential, and the UI's edit
|
||||
// form round-trips whatever it was given, so an ordinary "rename this channel"
|
||||
// save arrives carrying the sentinel in place of the password. Writing it
|
||||
// through would replace the credential with eight bullet characters and break
|
||||
// delivery on the next alert. A value that is not the sentinel is written
|
||||
// verbatim — including the empty string, which is how a credential is cleared.
|
||||
func mergeChannelSecrets(instanceID, channelID string, upd bson.M, cfg map[string]string) (map[string]string, error) {
|
||||
stored, err := GetChannel(instanceID, channelID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if stored == nil {
|
||||
return cfg, nil
|
||||
}
|
||||
// The secret keys are the ones of the type being saved, which the same
|
||||
// request may be changing.
|
||||
channelType := stored.Type
|
||||
if t, ok := upd["type"].(string); ok && t != "" {
|
||||
channelType = t
|
||||
}
|
||||
out := make(map[string]string, len(cfg))
|
||||
for k, v := range cfg {
|
||||
out[k] = v
|
||||
}
|
||||
for _, k := range models.ChannelSecretKeys(channelType) {
|
||||
if out[k] == models.RedactedSecret {
|
||||
if prev, ok := stored.Config[k]; ok {
|
||||
out[k] = prev
|
||||
} else {
|
||||
delete(out, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func DeleteChannel(instanceID, channelID string) error {
|
||||
ctx, cancel := monCtx()
|
||||
defer cancel()
|
||||
|
||||
Reference in New Issue
Block a user