1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2023-2025 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package daemon
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/confdb"
"github.com/snapcore/snapd/features"
"github.com/snapcore/snapd/overlord/assertstate"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/confdbstate"
"github.com/snapcore/snapd/overlord/configstate/config"
"github.com/snapcore/snapd/overlord/devicestate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/strutil"
)
var (
confdbCmd = &Command{
Path: "/v2/confdb/{account}/{confdb-schema}/{view}",
GET: getView,
PUT: setView,
ReadAccess: authenticatedAccess{Polkit: polkitActionManage},
WriteAccess: authenticatedAccess{Polkit: polkitActionManage},
}
confdbControlCmd = &Command{
Path: "/v2/confdb",
POST: handleConfdbControlAction,
Actions: []string{"delegate", "undelegate"},
WriteAccess: authenticatedAccess{Polkit: polkitActionManage},
}
)
func getView(c *Command, r *http.Request, _ *auth.UserState) Response {
st := c.d.state
st.Lock()
defer st.Unlock()
if err := validateFeatureFlag(st, features.Confdb); err != nil {
return err
}
vars := muxVars(r)
account, schemaName, viewName := vars["account"], vars["confdb-schema"], vars["view"]
keysStr := r.URL.Query().Get("keys")
var keys []string
if keysStr != "" {
keys = strutil.CommaSeparatedList(keysStr)
}
view, err := confdbstateGetView(st, account, schemaName, viewName)
if err != nil {
return toAPIError(err)
}
chgID, err := confdbstateLoadConfdbAsync(st, view, keys)
if err != nil {
return toAPIError(err)
}
ensureStateSoon(st)
return AsyncResponse(nil, chgID)
}
func setView(c *Command, r *http.Request, _ *auth.UserState) Response {
st := c.d.state
st.Lock()
defer st.Unlock()
if err := validateFeatureFlag(st, features.Confdb); err != nil {
return err
}
vars := muxVars(r)
account, schemaName, viewName := vars["account"], vars["confdb-schema"], vars["view"]
decoder := json.NewDecoder(r.Body)
var values map[string]any
if err := decoder.Decode(&values); err != nil {
return BadRequest("cannot decode confdb request body: %v", err)
}
view, err := confdbstateGetView(st, account, schemaName, viewName)
if err != nil {
return toAPIError(err)
}
tx, commitTxFunc, err := confdbstateGetTransactionToSet(nil, st, view)
if err != nil {
return toAPIError(err)
}
err = confdbstateSetViaView(tx, view, values)
if err != nil {
return toAPIError(err)
}
changeID, _, err := commitTxFunc()
if err != nil {
return toAPIError(err)
}
return AsyncResponse(nil, changeID)
}
func toAPIError(err error) *apiError {
switch {
case errors.Is(err, &asserts.NotFoundError{}):
return &apiError{
Status: 400,
Message: err.Error(),
Kind: client.ErrorKindAssertionNotFound,
Value: err,
}
case errors.Is(err, &confdb.NoMatchError{}):
return &apiError{
Status: 400,
Message: err.Error(),
Kind: client.ErrorKindConfdbNoMatchingRule,
Value: err,
}
case errors.Is(err, &confdb.NoDataError{}):
return &apiError{
Status: 400,
Message: err.Error(),
Kind: client.ErrorKindConfigNoSuchOption,
Value: err,
}
case errors.Is(err, &confdbstate.NoViewError{}):
return &apiError{
Status: 400,
Message: err.Error(),
Kind: client.ErrorKindConfdbViewNotFound,
Value: err,
}
case errors.Is(err, &confdb.BadRequestError{}):
return BadRequest(err.Error())
default:
return InternalError(err.Error())
}
}
func validateFeatureFlag(st *state.State, feature features.SnapdFeature) *apiError {
tr := config.NewTransaction(st)
enabled, err := features.Flag(tr, feature)
if err != nil && !config.IsNoOption(err) {
return InternalError(
fmt.Sprintf("internal error: cannot check %q feature flag: %s", feature, err),
)
}
if !enabled {
_, confName := feature.ConfigOption()
return BadRequest(
fmt.Sprintf(`feature flag %q is disabled: set '%s' to true`, feature, confName),
)
}
return nil
}
type confdbControlAction struct {
Action string `json:"action"`
OperatorID string `json:"operator-id"`
Authentications []string `json:"authentications"`
Views []string `json:"views"`
}
func handleConfdbControlAction(c *Command, r *http.Request, user *auth.UserState) Response {
st := c.d.state
st.Lock()
defer st.Unlock()
if err := validateFeatureFlag(st, features.Confdb); err != nil {
return err
}
if err := validateFeatureFlag(st, features.ConfdbControl); err != nil {
return err
}
devMgr := c.d.overlord.DeviceManager()
cc, err := devMgr.ConfdbControl()
if err != nil &&
(!errors.Is(err, state.ErrNoState) ||
errors.Is(err, devicestate.ErrNoDeviceIdentityYet)) {
return InternalError(err.Error())
}
var ctrl confdb.Control
var revision int
if cc != nil {
ctrl = cc.Control()
revision = cc.Revision() + 1
}
var a confdbControlAction
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&a); err != nil {
return BadRequest("cannot decode request body: %v", err)
}
switch a.Action {
case "delegate":
err = ctrl.Delegate(a.OperatorID, a.Views, a.Authentications)
case "undelegate":
err = ctrl.Undelegate(a.OperatorID, a.Views, a.Authentications)
default:
return BadRequest("unknown action %q", a.Action)
}
if err != nil {
return BadRequest(err.Error())
}
cc, err = devicestateSignConfdbControl(devMgr, ctrl.Groups(), revision)
if err != nil {
return InternalError(err.Error())
}
if err := assertstate.Add(st, cc); err != nil {
return InternalError(err.Error())
}
return SyncResponse(nil)
}
|