1
// Copyright 2012, 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
9
"launchpad.net/juju-core/environs"
10
"launchpad.net/juju-core/state"
11
"launchpad.net/juju-core/state/api"
12
apiprovisioner "launchpad.net/juju-core/state/api/provisioner"
13
"launchpad.net/juju-core/utils"
16
// TaggedPasswordChanger defines an interface for a entity with a
17
// Tag() and SetPassword() methods.
18
type TaggedPasswordChanger interface {
19
SetPassword(string) error
23
// AuthenticationProvider defines the single method that the provisioner
24
// task needs to set up authentication for a machine.
25
type AuthenticationProvider interface {
26
SetupAuthentication(machine TaggedPasswordChanger) (*state.Info, *api.Info, error)
29
// NewEnvironAuthenticator gets the state and api info once from the environ.
30
func NewEnvironAuthenticator(environ environs.Environ) (AuthenticationProvider, error) {
31
stateInfo, apiInfo, err := environ.StateInfo()
35
return &simpleAuth{stateInfo, apiInfo}, nil
38
// NewAPIAuthenticator gets the state and api info once from the
40
func NewAPIAuthenticator(st *apiprovisioner.State) (AuthenticationProvider, error) {
41
stateAddresses, err := st.StateAddresses()
45
apiAddresses, err := st.APIAddresses()
49
caCert, err := st.CACert()
53
stateInfo := &state.Info{
54
Addrs: stateAddresses,
61
return &simpleAuth{stateInfo, apiInfo}, nil
64
type simpleAuth struct {
69
func (auth *simpleAuth) SetupAuthentication(machine TaggedPasswordChanger) (*state.Info, *api.Info, error) {
70
password, err := utils.RandomPassword()
72
return nil, nil, fmt.Errorf("cannot make password for machine %v: %v", machine, err)
74
if err := machine.SetPassword(password); err != nil {
75
return nil, nil, fmt.Errorf("cannot set API password for machine %v: %v", machine, err)
77
stateInfo := *auth.stateInfo
78
stateInfo.Tag = machine.Tag()
79
stateInfo.Password = password
80
apiInfo := *auth.apiInfo
81
apiInfo.Tag = machine.Tag()
82
apiInfo.Password = password
83
return &stateInfo, &apiInfo, nil