MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / getAuthScopeHandleCreateDB

Function getAuthScopeHandleCreateDB

rest/admin_api.go:185–221  ·  view source on GitHub ↗

getAuthScopeHandleCreateDB determines the auth scope for a PUT /{db}/ request. This is a special case because we don't have a database initialized yet, so we need to infer the bucket from db config.

(ctx context.Context, h *handler)

Source from the content-addressed store, hash-verified

183// getAuthScopeHandleCreateDB determines the auth scope for a PUT /{db}/ request.
184// This is a special case because we don't have a database initialized yet, so we need to infer the bucket from db config.
185func getAuthScopeHandleCreateDB(ctx context.Context, h *handler) (bucketName string, err error) {
186
187 // grab a copy of the request body and restore body buffer for later handlers
188 bodyJSON, readErr := h.readBody()
189 if readErr != nil {
190 return "", base.HTTPErrorf(http.StatusInternalServerError, "Unable to read body: %v", readErr)
191 }
192 // mark the body as already read to avoid double-counting bytes for stats once it gets read again
193 h.requestBody.bodyRead = true
194 h.requestBody.reader = io.NopCloser(bytes.NewReader(bodyJSON))
195
196 var dbConfigBody struct {
197 Bucket string `json:"bucket"`
198 }
199
200 bodyJSON, err = sanitiseConfig(ctx, bodyJSON, h.server.Config.Unsupported.AllowDbConfigEnvVars)
201 if err != nil {
202 return "", err
203 }
204
205 d := base.JSONDecoder(bytes.NewBuffer(bodyJSON))
206 err = d.Decode(&dbConfigBody)
207 if err != nil {
208 return "", err
209 }
210
211 if dbConfigBody.Bucket == "" {
212 dbName := h.PathVar("newdb")
213 if dbName == "" {
214 return "", base.HTTPErrorf(http.StatusBadRequest, "bucket or db name not specified in request")
215 }
216 // imply bucket name from db name in path if not in body
217 return dbName, nil
218 }
219
220 return dbConfigBody.Bucket, nil
221}
222
223// Take a DB online, first reload the DB config
224func (h *handler) handleDbOnline() error {

Callers

nothing calls this directly

Calls 6

HTTPErrorfFunction · 0.92
JSONDecoderFunction · 0.92
sanitiseConfigFunction · 0.85
readBodyMethod · 0.80
PathVarMethod · 0.80
DecodeMethod · 0.65

Tested by

no test coverage detected