~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/provider/maas/credentials.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2016 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package maas
 
5
 
 
6
import (
 
7
        "encoding/json"
 
8
        "fmt"
 
9
        "io/ioutil"
 
10
        "path/filepath"
 
11
        "strings"
 
12
 
 
13
        "github.com/juju/errors"
 
14
        "github.com/juju/utils"
 
15
 
 
16
        "github.com/juju/juju/cloud"
 
17
)
 
18
 
 
19
const (
 
20
        credAttrMAASOAuth = "maas-oauth"
 
21
)
 
22
 
 
23
type environProviderCredentials struct{}
 
24
 
 
25
// CredentialSchemas is part of the environs.ProviderCredentials interface.
 
26
func (environProviderCredentials) CredentialSchemas() map[cloud.AuthType]cloud.CredentialSchema {
 
27
        return map[cloud.AuthType]cloud.CredentialSchema{
 
28
                cloud.OAuth1AuthType: {{
 
29
                        credAttrMAASOAuth, cloud.CredentialAttr{
 
30
                                Description: "OAuth/API-key credentials for MAAS",
 
31
                                Hidden:      true,
 
32
                        },
 
33
                }},
 
34
        }
 
35
}
 
36
 
 
37
// DetectCredentials is part of the environs.ProviderCredentials interface.
 
38
func (environProviderCredentials) DetectCredentials() (*cloud.CloudCredential, error) {
 
39
        // MAAS stores credentials in a json file: ~/.maasrc
 
40
        // {"Server": "http://<ip>/MAAS", "OAuth": "<key>"}
 
41
        maasrc := filepath.Join(utils.Home(), ".maasrc")
 
42
        fileBytes, err := ioutil.ReadFile(maasrc)
 
43
        if err != nil {
 
44
                return nil, errors.Trace(err)
 
45
        }
 
46
 
 
47
        details := make(map[string]interface{})
 
48
        err = json.Unmarshal(fileBytes, &details)
 
49
        if err != nil {
 
50
                return nil, errors.Trace(err)
 
51
        }
 
52
        oauthKey := details["OAuth"]
 
53
        if oauthKey == "" {
 
54
                return nil, errors.New("MAAS credentials require a value for OAuth token")
 
55
        }
 
56
        cred := cloud.NewCredential(cloud.OAuth1AuthType, map[string]string{
 
57
                credAttrMAASOAuth: fmt.Sprintf("%v", oauthKey),
 
58
        })
 
59
        server, ok := details["Server"]
 
60
        if server == "" || !ok {
 
61
                server = "unspecified server"
 
62
        }
 
63
        cred.Label = fmt.Sprintf("MAAS credential for %s", server)
 
64
 
 
65
        return &cloud.CloudCredential{
 
66
                AuthCredentials: map[string]cloud.Credential{
 
67
                        "default": cred,
 
68
                },
 
69
        }, nil
 
70
}
 
71
 
 
72
func parseOAuthToken(cred cloud.Credential) (string, error) {
 
73
        oauth := cred.Attributes()[credAttrMAASOAuth]
 
74
        if strings.Count(oauth, ":") != 2 {
 
75
                return "", errMalformedMaasOAuth
 
76
        }
 
77
        return oauth, nil
 
78
}
 
79
 
 
80
var errMalformedMaasOAuth = errors.New("malformed maas-oauth (3 items separated by colons)")