// Init implements cmd.Command. func (c *accessCommand) Init(args []string) error { if len(args) < 1 { return errors.New("no user specified") } if len(args) < 2 { return errors.New("no permission level specified") } c.User = args[0] c.ModelNames = args[2:] c.Access = args[1] // Special case for backwards compatibility. if c.Access == "addmodel" { c.Access = "add-model" } if len(c.ModelNames) > 0 { if err := permission.ValidateControllerAccess(permission.Access(c.Access)); err == nil { return errors.Errorf("You have specified a controller access permission %q.\n"+ "If you intended to change controller access, do not specify any model names.\n"+ "See 'juju help grant'.", c.Access) } return permission.ValidateModelAccess(permission.Access(c.Access)) } if err := permission.ValidateModelAccess(permission.Access(c.Access)); err == nil { return errors.Errorf("You have specified a model access permission %q.\n"+ "If you intended to change model access, you need to specify one or more model names.\n"+ "See 'juju help grant'.", c.Access) } return nil }
func (s *legacySuite) TestGetControllerAccess(c *gc.C) { controller := s.OpenAPI(c) defer controller.Close() err := controller.GrantController("fred@external", "addmodel") c.Assert(err, jc.ErrorIsNil) access, err := controller.GetControllerAccess("fred@external") c.Assert(err, jc.ErrorIsNil) c.Assert(access, gc.Equals, permission.Access("addmodel")) }
// ModifyModelAccess changes the model access granted to users. func (m *ModelManagerAPI) ModifyModelAccess(args params.ModifyModelAccessRequest) (result params.ErrorResults, _ error) { result = params.ErrorResults{ Results: make([]params.ErrorResult, len(args.Changes)), } canModifyController, err := m.authorizer.HasPermission(permission.SuperuserAccess, m.state.ControllerTag()) if err != nil { return result, errors.Trace(err) } if len(args.Changes) == 0 { return result, nil } for i, arg := range args.Changes { modelAccess := permission.Access(arg.Access) if err := permission.ValidateModelAccess(modelAccess); err != nil { err = errors.Annotate(err, "could not modify model access") result.Results[i].Error = common.ServerError(err) continue } modelTag, err := names.ParseModelTag(arg.ModelTag) if err != nil { result.Results[i].Error = common.ServerError(errors.Annotate(err, "could not modify model access")) continue } canModifyModel, err := m.authorizer.HasPermission(permission.AdminAccess, modelTag) if err != nil { return result, errors.Trace(err) } canModify := canModifyController || canModifyModel if !canModify { result.Results[i].Error = common.ServerError(common.ErrPerm) continue } targetUserTag, err := names.ParseUserTag(arg.UserTag) if err != nil { result.Results[i].Error = common.ServerError(errors.Annotate(err, "could not modify model access")) continue } result.Results[i].Error = common.ServerError( changeModelAccess(m.state, modelTag, m.apiUser, targetUserTag, arg.Action, modelAccess, m.isAdmin)) } return result, nil }
// GetControllerAccess returns the access level the user has on the controller. func (c *Client) GetControllerAccess(user string) (permission.Access, error) { if !names.IsValidUser(user) { return "", errors.Errorf("invalid username: %q", user) } entities := params.Entities{Entities: []params.Entity{{names.NewUserTag(user).String()}}} var results params.UserAccessResults err := c.facade.FacadeCall("GetControllerAccess", entities, &results) if err != nil { return "", errors.Trace(err) } if len(results.Results) != 1 { return "", errors.Errorf("expected 1 result, got %d", len(results.Results)) } if err := results.Results[0].Error; err != nil { return "", errors.Trace(err) } return permission.Access(results.Results[0].Result.Access), nil }
func (c *Client) modifyModelUser(action params.ModelAction, user, access string, modelUUIDs []string) error { var args params.ModifyModelAccessRequest if !names.IsValidUser(user) { return errors.Errorf("invalid username: %q", user) } userTag := names.NewUserTag(user) modelAccess := permission.Access(access) if err := permission.ValidateModelAccess(modelAccess); err != nil { return errors.Trace(err) } for _, model := range modelUUIDs { if !names.IsValidModel(model) { return errors.Errorf("invalid model: %q", model) } modelTag := names.NewModelTag(model) args.Changes = append(args.Changes, params.ModifyModelAccess{ UserTag: userTag.String(), Action: action, Access: params.UserAccessPermission(modelAccess), ModelTag: modelTag.String(), }) } var result params.ErrorResults err := c.facade.FacadeCall("ModifyModelAccess", args, &result) if err != nil { return errors.Trace(err) } if len(result.Results) != len(args.Changes) { return errors.Errorf("expected %d results, got %d", len(args.Changes), len(result.Results)) } for i, r := range result.Results { if r.Error != nil && r.Error.Code == params.CodeAlreadyExists { logger.Warningf("model %q is already shared with %q", modelUUIDs[i], userTag.Canonical()) result.Results[i].Error = nil } } return result.Combine() }
// Init implements cmd.Command. func (c *accessCommand) Init(args []string) error { if len(args) < 1 { return errors.New("no user specified") } if len(args) < 2 { return errors.New("no permission level specified") } c.User = args[0] c.ModelNames = args[2:] c.ModelAccess = args[1] if len(c.ModelNames) > 0 { err := permission.ValidateModelAccess(permission.Access(c.ModelAccess)) if err != nil { return err } } return nil }
func (i *importer) modelUsers() error { i.logger.Debugf("importing users") // The user that was auto-added when we created the model will have // the wrong DateCreated, so we remove it, and add in all the users we // know about. It is also possible that the owner of the model no // longer has access to the model due to changes over time. if err := i.st.RemoveUserAccess(i.dbModel.Owner(), i.dbModel.ModelTag()); err != nil { return errors.Trace(err) } users := i.model.Users() modelUUID := i.dbModel.UUID() var ops []txn.Op for _, user := range users { ops = append(ops, createModelUserOps( modelUUID, user.Name(), user.CreatedBy(), user.DisplayName(), user.DateCreated(), permission.Access(user.Access()))..., ) } if err := i.st.runTransaction(ops); err != nil { return errors.Trace(err) } // Now set their last connection times. for _, user := range users { i.logger.Debugf("user %s", user.Name()) lastConnection := user.LastConnection() if lastConnection.IsZero() { continue } err := i.st.updateLastModelConnection(user.Name(), lastConnection) if err != nil { return errors.Trace(err) } } return nil }
// ModifyControllerAccess changes the model access granted to users. func (c *ControllerAPI) ModifyControllerAccess(args params.ModifyControllerAccessRequest) (params.ErrorResults, error) { result := params.ErrorResults{ Results: make([]params.ErrorResult, len(args.Changes)), } if len(args.Changes) == 0 { return result, nil } hasPermission, err := c.authorizer.HasPermission(permission.SuperuserAccess, c.state.ControllerTag()) if err != nil { return result, errors.Trace(err) } for i, arg := range args.Changes { if !hasPermission { result.Results[i].Error = common.ServerError(common.ErrPerm) continue } controllerAccess := permission.Access(arg.Access) if err := permission.ValidateControllerAccess(controllerAccess); err != nil { result.Results[i].Error = common.ServerError(err) continue } targetUserTag, err := names.ParseUserTag(arg.UserTag) if err != nil { result.Results[i].Error = common.ServerError(errors.Annotate(err, "could not modify controller access")) continue } result.Results[i].Error = common.ServerError( ChangeControllerAccess(c.state, c.apiUser, targetUserTag, arg.Action, controllerAccess)) } return result, nil }
func stringToAccess(a string) permission.Access { return permission.Access(a) }