/
access_config.go
84 lines (68 loc) · 2.06 KB
/
access_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package triton
import (
"fmt"
"io/ioutil"
"os"
"github.com/joyent/gocommon/client"
"github.com/joyent/gosdc/cloudapi"
"github.com/joyent/gosign/auth"
"github.com/mitchellh/packer/helper/communicator"
"github.com/mitchellh/packer/template/interpolate"
)
// AccessConfig is for common configuration related to Triton access
type AccessConfig struct {
Endpoint string `mapstructure:"sdc_url"`
Account string `mapstructure:"sdc_account"`
KeyID string `mapstructure:"sdc_key_id"`
KeyPath string `mapstructure:"sdc_key_path"`
}
// Prepare performs basic validation on the AccessConfig
func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
var errs []error
if c.Endpoint == "" {
// Use Joyent public cloud as the default endpoint if none is in environment
c.Endpoint = "https://us-east-1.api.joyent.com"
}
if c.Account == "" {
errs = append(errs, fmt.Errorf("sdc_account is required to use the triton builder"))
}
if c.KeyID == "" {
errs = append(errs, fmt.Errorf("sdc_key_id is required to use the triton builder"))
}
if c.KeyPath == "" {
errs = append(errs, fmt.Errorf("sdc_key_path is required to use the triton builder"))
}
if _, err := os.Stat(c.KeyPath); err != nil {
errs = append(errs, fmt.Errorf("Error reading private key: %s", err))
}
if len(errs) > 0 {
return errs
}
return nil
}
// CreateSDCClient returns an SDC client configured with the appropriate client credentials
// or an error if creating the client fails.
func (c *AccessConfig) CreateSDCClient() (*cloudapi.Client, error) {
keyData, err := ioutil.ReadFile(c.KeyPath)
if err != nil {
return nil, err
}
userauth, err := auth.NewAuth(c.Account, string(keyData), "rsa-sha256")
if err != nil {
return nil, err
}
creds := &auth.Credentials{
UserAuthentication: userauth,
SdcKeyId: c.KeyID,
SdcEndpoint: auth.Endpoint{URL: c.Endpoint},
}
return cloudapi.New(client.NewClient(
c.Endpoint,
cloudapi.DefaultAPIVersion,
creds,
&cloudapi.Logger,
)), nil
}
func (c *AccessConfig) Comm() communicator.Config {
return communicator.Config{}
}