// validateEnvironUUID is the common validator for the various // apiserver components that need to check for a valid environment // UUID. An empty envUUID means that the connection has come in at // the root of the URL space and refers to the state server // environment. // // It returns the validated environment UUID. func validateEnvironUUID(args validateArgs) (string, error) { ssState := args.statePool.SystemState() if args.envUUID == "" { // We allow the environUUID to be empty for 2 cases // 1) Compatibility with older clients // 2) TODO: server a limited API at the root (empty envUUID) // with just the user manager and environment manager // if the connection comes over a sufficiently up to date // login command. if args.strict { return "", errors.Trace(common.UnknownEnvironmentError(args.envUUID)) } logger.Debugf("validate env uuid: empty envUUID") return ssState.EnvironUUID(), nil } if args.envUUID == ssState.EnvironUUID() { logger.Debugf("validate env uuid: state server environment - %s", args.envUUID) return args.envUUID, nil } if args.stateServerEnvOnly { return "", errors.Unauthorizedf("requested environment %q is not the state server environment", args.envUUID) } if !names.IsValidEnvironment(args.envUUID) { return "", errors.Trace(common.UnknownEnvironmentError(args.envUUID)) } envTag := names.NewEnvironTag(args.envUUID) if _, err := ssState.GetEnvironment(envTag); err != nil { return "", errors.Wrap(err, common.UnknownEnvironmentError(args.envUUID)) } logger.Debugf("validate env uuid: %s", args.envUUID) return args.envUUID, nil }
// checkEnvironUUID checks if the expected envionUUID matches the // current environUUID set on this Server. It returns nil for a match // and an error on mismatch. func (srv *Server) checkEnvironUUID(expected string) error { actual := srv.getEnvironUUID() if actual != expected { return common.UnknownEnvironmentError(expected) } return nil }
// validateEnvironUUID is the common validator for the various apiserver // components that need to check for a valid environment UUID. // An empty envUUID means that the connection has come in at the root // of the URL space and refers to the state server environment. // The *state.State parameter is expected to be the state server State // connection. The return *state.State is a connection for the specified // environment UUID if the UUID refers to an environment contained in the // database. If the bool return value is true, the state connection must // be closed by the caller at the end of serving the client connection. func validateEnvironUUID(args validateArgs) (*state.State, bool, error) { if args.envUUID == "" { // We allow the environUUID to be empty for 2 cases // 1) Compatibility with older clients // 2) TODO: server a limited API at the root (empty envUUID) // with just the user manager and environment manager // if the connection comes over a sufficiently up to date // login command. if args.strict { return nil, false, errors.Trace(common.UnknownEnvironmentError(args.envUUID)) } logger.Debugf("validate env uuid: empty envUUID") return args.st, false, nil } if args.envUUID == args.st.EnvironUUID() { logger.Debugf("validate env uuid: state server environment - %s", args.envUUID) return args.st, false, nil } if args.stateServerEnvOnly { return nil, false, errors.Unauthorizedf("requested environment %q is not the state server environment", args.envUUID) } if !names.IsValidEnvironment(args.envUUID) { return nil, false, errors.Trace(common.UnknownEnvironmentError(args.envUUID)) } envTag := names.NewEnvironTag(args.envUUID) if env, err := args.st.GetEnvironment(envTag); err != nil { return nil, false, errors.Wrap(err, common.UnknownEnvironmentError(args.envUUID)) } else if env.Life() != state.Alive { return nil, false, errors.Errorf("environment %q is no longer live", args.envUUID) } logger.Debugf("validate env uuid: %s", args.envUUID) result, err := args.st.ForEnviron(envTag) if err != nil { return nil, false, errors.Trace(err) } return result, true, nil }
func (h *httpHandler) validateEnvironUUID(r *http.Request) error { // Note: this is only true until we have support for multiple // environments. For now, there is only one, so we make sure that is // the one being addressed. envUUID := h.getEnvironUUID(r) logger.Tracef("got a request for env %q", envUUID) if envUUID == "" { return nil } env, err := h.state.Environment() if err != nil { logger.Infof("error looking up environment: %v", err) return err } if env.UUID() != envUUID { logger.Infof("environment uuid mismatch: %v != %v", envUUID, env.UUID()) return common.UnknownEnvironmentError(envUUID) } return nil }
func (s *errorsSuite) TestUnknownEnvironment(c *gc.C) { err := common.UnknownEnvironmentError("dead-beef") c.Check(err, gc.ErrorMatches, `unknown environment: "dead-beef"`) }
}, { err: common.ErrOperationBlocked("test"), code: params.CodeOperationBlocked, helperFunc: params.IsCodeOperationBlocked, }, { err: errors.NotSupportedf("needed feature"), code: params.CodeNotSupported, helperFunc: params.IsCodeNotSupported, }, { err: stderrors.New("an error"), code: "", }, { err: unhashableError{"foo"}, code: "", }, { err: common.UnknownEnvironmentError("dead-beef-123456"), code: params.CodeNotFound, helperFunc: params.IsCodeNotFound, }, { err: nil, code: "", }} type unhashableError []string func (err unhashableError) Error() string { return err[0] } func (s *errorsSuite) TestErrorTransform(c *gc.C) { for _, t := range errorTransformTests {