LEFT | RIGHT |
(no file at all) | |
| 1 // Copyright 2013 Canonical Ltd. |
| 2 // Licensed under the AGPLv3, see LICENCE file for details. |
| 3 |
| 4 package common |
| 5 |
| 6 import ( |
| 7 "launchpad.net/juju-core/state" |
| 8 "launchpad.net/juju-core/state/api/params" |
| 9 ) |
| 10 |
| 11 type PasswordChanger struct { |
| 12 st authGetter |
| 13 canChange func(tag string) bool |
| 14 } |
| 15 |
| 16 type authGetter interface { |
| 17 Authenticator(tag string) (state.TaggedAuthenticator, error) |
| 18 } |
| 19 |
| 20 func NewPasswordChanger(st *state.State, canChange func(tag string) bool) *Passw
ordChanger { |
| 21 return &PasswordChanger{ |
| 22 st: st, |
| 23 canChange: canChange, |
| 24 } |
| 25 } |
| 26 |
| 27 func (pc *PasswordChanger) SetPasswords(args params.PasswordChanges) params.Erro
rResults { |
| 28 results := params.ErrorResults{ |
| 29 Errors: make([]*params.Error, len(args.Changes)), |
| 30 } |
| 31 for i, param := range args.Changes { |
| 32 if !pc.canChange(param.Tag) { |
| 33 results.Errors[i] = ServerError(ErrPerm) |
| 34 continue |
| 35 } |
| 36 if err := pc.setPassword(param.Tag, param.Password); err != nil
{ |
| 37 results.Errors[i] = ServerError(err) |
| 38 } |
| 39 } |
| 40 return results |
| 41 } |
| 42 |
| 43 func (pc *PasswordChanger) setPassword(tag, password string) error { |
| 44 type mongoPassworder interface { |
| 45 SetMongoPassword(password string) error |
| 46 } |
| 47 entity, err := pc.st.Authenticator(tag) |
| 48 if err != nil { |
| 49 return err |
| 50 } |
| 51 // We set the mongo password first on the grounds that |
| 52 // if it fails, the agent in question should still be able |
| 53 // to authenticate to another API server and ask it to change |
| 54 // its password. |
| 55 if entity, ok := entity.(mongoPassworder); ok { |
| 56 // TODO(rog) when the API is universal, check that the entity is
a |
| 57 // machine with jobs that imply it needs access to the mongo sta
te. |
| 58 if err := entity.SetMongoPassword(password); err != nil { |
| 59 return err |
| 60 } |
| 61 } |
| 62 return entity.SetPassword(password) |
| 63 } |
LEFT | RIGHT |