forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manta.go
124 lines (101 loc) · 2.84 KB
/
manta.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package remote
import (
"crypto/md5"
"encoding/pem"
"fmt"
"io/ioutil"
"log"
"os"
joyentclient "github.com/joyent/gocommon/client"
joyenterrors "github.com/joyent/gocommon/errors"
"github.com/joyent/gomanta/manta"
joyentauth "github.com/joyent/gosign/auth"
)
const DEFAULT_OBJECT_NAME = "terraform.tfstate"
func mantaFactory(conf map[string]string) (Client, error) {
path, ok := conf["path"]
if !ok {
return nil, fmt.Errorf("missing 'path' configuration")
}
objectName, ok := conf["objectName"]
if !ok {
objectName = DEFAULT_OBJECT_NAME
}
creds, err := getCredentialsFromEnvironment()
if err != nil {
return nil, fmt.Errorf("Error getting Manta credentials: %s", err.Error())
}
client := manta.New(joyentclient.NewClient(
creds.MantaEndpoint.URL,
"",
creds,
log.New(os.Stderr, "", log.LstdFlags),
))
return &MantaClient{
Client: client,
Path: path,
ObjectName: objectName,
}, nil
}
type MantaClient struct {
Client *manta.Client
Path string
ObjectName string
}
func (c *MantaClient) Get() (*Payload, error) {
bytes, err := c.Client.GetObject(c.Path, c.ObjectName)
if err != nil {
if joyenterrors.IsResourceNotFound(err.(joyenterrors.Error).Cause()) {
return nil, nil
}
return nil, err
}
md5 := md5.Sum(bytes)
return &Payload{
Data: bytes,
MD5: md5[:],
}, nil
}
func (c *MantaClient) Put(data []byte) error {
return c.Client.PutObject(c.Path, c.ObjectName, data)
}
func (c *MantaClient) Delete() error {
return c.Client.DeleteObject(c.Path, c.ObjectName)
}
func getCredentialsFromEnvironment() (cred *joyentauth.Credentials, err error) {
user := os.Getenv("MANTA_USER")
keyId := os.Getenv("MANTA_KEY_ID")
url := os.Getenv("MANTA_URL")
keyMaterial := os.Getenv("MANTA_KEY_MATERIAL")
if _, err := os.Stat(keyMaterial); err == nil {
// key material is a file path; try to read it
keyBytes, err := ioutil.ReadFile(keyMaterial)
if err != nil {
return nil, fmt.Errorf("Error reading key material from %s: %s",
keyMaterial, err)
} else {
block, _ := pem.Decode(keyBytes)
if block == nil {
return nil, fmt.Errorf(
"Failed to read key material '%s': no key found", keyMaterial)
}
if block.Headers["Proc-Type"] == "4,ENCRYPTED" {
return nil, fmt.Errorf(
"Failed to read key '%s': password protected keys are\n"+
"not currently supported. Please decrypt the key prior to use.", keyMaterial)
}
keyMaterial = string(keyBytes)
}
}
authentication, err := joyentauth.NewAuth(user, keyMaterial, "rsa-sha256")
if err != nil {
return nil, fmt.Errorf("Error constructing authentication for %s: %s", user, err)
}
return &joyentauth.Credentials{
UserAuthentication: authentication,
SdcKeyId: "",
SdcEndpoint: joyentauth.Endpoint{},
MantaKeyId: keyId,
MantaEndpoint: joyentauth.Endpoint{URL: url},
}, nil
}