~ubuntu-branches/ubuntu/trusty/juju-core/trusty-proposed

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/worker/provisioner/authentication.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-29 11:40:20 UTC
  • mfrom: (23.1.1 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20140129114020-ejieitm8smtt5vln
Tags: 1.17.1-0ubuntu2
d/tests/local-provider: Don't fail tests if ~/.juju is present as its
created by the juju version command. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2012, 2013 Canonical Ltd.
2
 
// Licensed under the AGPLv3, see LICENCE file for details.
3
 
 
4
 
package provisioner
5
 
 
6
 
import (
7
 
        "fmt"
8
 
 
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"
14
 
)
15
 
 
16
 
// TaggedPasswordChanger defines an interface for a entity with a
17
 
// Tag() and SetPassword() methods.
18
 
type TaggedPasswordChanger interface {
19
 
        SetPassword(string) error
20
 
        Tag() string
21
 
}
22
 
 
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)
27
 
}
28
 
 
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()
32
 
        if err != nil {
33
 
                return nil, err
34
 
        }
35
 
        return &simpleAuth{stateInfo, apiInfo}, nil
36
 
}
37
 
 
38
 
// NewAPIAuthenticator gets the state and api info once from the
39
 
// provisioner API.
40
 
func NewAPIAuthenticator(st *apiprovisioner.State) (AuthenticationProvider, error) {
41
 
        stateAddresses, err := st.StateAddresses()
42
 
        if err != nil {
43
 
                return nil, err
44
 
        }
45
 
        apiAddresses, err := st.APIAddresses()
46
 
        if err != nil {
47
 
                return nil, err
48
 
        }
49
 
        caCert, err := st.CACert()
50
 
        if err != nil {
51
 
                return nil, err
52
 
        }
53
 
        stateInfo := &state.Info{
54
 
                Addrs:  stateAddresses,
55
 
                CACert: caCert,
56
 
        }
57
 
        apiInfo := &api.Info{
58
 
                Addrs:  apiAddresses,
59
 
                CACert: caCert,
60
 
        }
61
 
        return &simpleAuth{stateInfo, apiInfo}, nil
62
 
}
63
 
 
64
 
type simpleAuth struct {
65
 
        stateInfo *state.Info
66
 
        apiInfo   *api.Info
67
 
}
68
 
 
69
 
func (auth *simpleAuth) SetupAuthentication(machine TaggedPasswordChanger) (*state.Info, *api.Info, error) {
70
 
        password, err := utils.RandomPassword()
71
 
        if err != nil {
72
 
                return nil, nil, fmt.Errorf("cannot make password for machine %v: %v", machine, err)
73
 
        }
74
 
        if err := machine.SetPassword(password); err != nil {
75
 
                return nil, nil, fmt.Errorf("cannot set API password for machine %v: %v", machine, err)
76
 
        }
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
84
 
}