// validateModelUUID is the common validator for the various // apiserver components that need to check for a valid model // UUID. An empty modelUUID means that the connection has come in at // the root of the URL space and refers to the controller // model. // // It returns the validated model UUID. func validateModelUUID(args validateArgs) (string, error) { ssState := args.statePool.SystemState() if args.modelUUID == "" { // We allow the modelUUID to be empty so that: // TODO: server a limited API at the root (empty modelUUID) // just the user manager and model manager are able to accept // requests that don't require a modelUUID, like add-model. if args.strict { return "", errors.Trace(common.UnknownModelError(args.modelUUID)) } return ssState.ModelUUID(), nil } if args.modelUUID == ssState.ModelUUID() { return args.modelUUID, nil } if args.controllerModelOnly { return "", errors.Unauthorizedf("requested model %q is not the controller model", args.modelUUID) } if !names.IsValidModel(args.modelUUID) { return "", errors.Trace(common.UnknownModelError(args.modelUUID)) } modelTag := names.NewModelTag(args.modelUUID) if _, err := ssState.GetModel(modelTag); err != nil { return "", errors.Wrap(err, common.UnknownModelError(args.modelUUID)) } return args.modelUUID, nil }
// validateModelUUID is the common validator for the various // apiserver components that need to check for a valid model // UUID. An empty modelUUID means that the connection has come in at // the root of the URL space and refers to the controller // model. // // It returns the validated model UUID. func validateModelUUID(args validateArgs) (string, error) { ssState := args.statePool.SystemState() if args.modelUUID == "" { // We allow the modelUUID to be empty for 2 cases // 1) Compatibility with older clients // 2) TODO: server a limited API at the root (empty modelUUID) // with just the user manager and model manager // if the connection comes over a sufficiently up to date // login command. if args.strict { return "", errors.Trace(common.UnknownModelError(args.modelUUID)) } logger.Debugf("validate model uuid: empty modelUUID") return ssState.ModelUUID(), nil } if args.modelUUID == ssState.ModelUUID() { logger.Debugf("validate model uuid: controller model - %s", args.modelUUID) return args.modelUUID, nil } if args.controllerModelOnly { return "", errors.Unauthorizedf("requested model %q is not the controller model", args.modelUUID) } if !names.IsValidModel(args.modelUUID) { return "", errors.Trace(common.UnknownModelError(args.modelUUID)) } modelTag := names.NewModelTag(args.modelUUID) if _, err := ssState.GetModel(modelTag); err != nil { return "", errors.Wrap(err, common.UnknownModelError(args.modelUUID)) } logger.Debugf("validate model uuid: %s", args.modelUUID) return args.modelUUID, nil }
func (s *errorsSuite) TestUnknownModel(c *gc.C) { err := common.UnknownModelError("dead-beef") c.Check(err, gc.ErrorMatches, `unknown model: "dead-beef"`) }
}, status: http.StatusUnauthorized, code: params.CodeDischargeRequired, helperFunc: func(err error) bool { err1, ok := err.(*params.Error) if !ok || err1.Info == nil || err1.Info.Macaroon != sampleMacaroon { return false } return true }, }, { err: unhashableError{"foo"}, status: http.StatusInternalServerError, code: "", }, { err: common.UnknownModelError("dead-beef-123456"), code: params.CodeModelNotFound, status: http.StatusNotFound, helperFunc: params.IsCodeModelNotFound, }, { err: nil, code: "", status: http.StatusOK, }} var sampleMacaroon = func() *macaroon.Macaroon { m, err := macaroon.New([]byte("key"), "id", "loc") if err != nil { panic(err) } return m