-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpath_dag.go
236 lines (202 loc) · 6.2 KB
/
path_dag.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package ipfs
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"sort"
"strings"
"github.com/fatih/structs"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
"github.com/ipfs/go-cid"
)
var dagFields = map[string]*framework.FieldSchema{
"ref": {
Type: framework.TypeString,
Description: "DAG node to pull and serialize from IPFS",
},
"link": {
Type: framework.TypeString,
Description: "optional link of DAG node to pull",
},
}
var dagPutFields = map[string]*framework.FieldSchema{
"plaintext": {
Type: framework.TypeString,
Description: "base64 encoded plaintext data to upload",
},
"format": {
Type: framework.TypeString,
Default: "dag-cbor",
Description: "format that the object will be added as",
},
"input-enc": {
Type: framework.TypeString,
Default: "json",
Description: "format that the input object will be",
},
"pin": {
Type: framework.TypeBool,
Default: true,
Description: "pin the object when added",
},
"hash": {
Type: framework.TypeString,
Default: "sha2-256",
Description: "multihash hashing algorithm to use",
},
}
func (b *backend) dagPaths() []*framework.Path {
return []*framework.Path{
// The order of these paths matters: more specific ones need to be near
// the top, so that path matching does not short-circuit.
{
Pattern: "dag/" + framework.GenericNameRegex("ref") + framework.OptionalParamRegex("link") + "/",
HelpSynopsis: "Return an IPLD node's links",
Fields: dagFields,
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ListOperation: b.pathDAGList,
},
},
{
Pattern: "dag/" + framework.GenericNameRegex("ref") + framework.OptionalParamRegex("link"),
HelpSynopsis: "Return an IPLD node",
Fields: dagFields,
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathDAGGet,
},
},
{
Pattern: "dag/" + framework.GenericNameRegex("ref") + "/",
HelpSynopsis: "Return a list of a node's links",
Fields: dagFields,
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ListOperation: b.pathDAGList,
},
},
{
Pattern: "dag",
HelpSynopsis: "Return a list of a node's links",
Fields: dagPutFields,
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.CreateOperation: b.pathDAGPut,
logical.UpdateOperation: b.pathDAGPut,
},
},
}
}
type DAGLinks struct {
Links []Link `json:"links"`
}
type Link struct {
Name string
Cid Cid
}
type Cid struct {
Target string `json:"/"`
}
// pathDAGGet returns an IPLD node as returned by the network.
func (b *backend) pathDAGGet(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
key := d.Get("ref").(string)
link := d.Get("link").(string)
if link != "" {
key = strings.Join([]string{key, link}, "/")
}
addr := fmt.Sprintf("%s/api/v0/dag/get?arg=%s", ipfsAddr, key)
httpReq, err := http.NewRequest(http.MethodGet, addr, nil)
httpReq.Header.Set("User-Agent", "vault-plugin-ipfs")
if err != nil {
return nil, logical.CodedError(http.StatusInternalServerError, err.Error())
}
res, err := ipfsClient.Do(httpReq)
if err != nil {
return nil, logical.CodedError(http.StatusInternalServerError, err.Error())
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
var data map[string]interface{}
json.Unmarshal(body, &data)
return &logical.Response{
Data: data,
}, nil
}
func (b *backend) pathDAGList(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
key := d.Get("ref").(string)
_, err := cid.Parse(key)
if err != nil {
return logical.ErrorResponse("invalid CID"), nil
}
link := d.Get("link").(string)
if link != "" {
key = strings.Join([]string{key, link}, "/")
}
if key == "" {
return logical.ErrorResponse("Missing ref"), nil
}
addr := fmt.Sprintf("%s/api/v0/dag/get?arg=%s", ipfsAddr, key)
httpReq, err := http.NewRequest(http.MethodGet, addr, nil)
httpReq.Header.Set("User-Agent", "vault-plugin-ipfs")
if err != nil {
return nil, logical.CodedError(http.StatusInternalServerError, err.Error())
}
res, err := ipfsClient.Do(httpReq)
if err != nil {
return nil, logical.CodedError(http.StatusInternalServerError, err.Error())
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
var data DAGLinks
json.Unmarshal(body, &data)
// Pull just the links out of the node
hashes := make([]string, 0, len(data.Links))
for _, link := range data.Links {
hashes = append(hashes, link.Name)
}
sort.Strings(hashes)
return logical.ListResponse(hashes), nil
}
func (b *backend) pathDAGPut(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
plaintext := d.Get("plaintext").(string)
format := d.Get("format").(string)
encoding := d.Get("input-enc").(string)
pin := d.Get("pin").(bool)
hash := d.Get("hash").(string)
if plaintext == "" {
return logical.ErrorResponse("No plaintext provided"), nil
}
addr := fmt.Sprintf("%s/api/v0/dag/put?format=%s&input-enc=%s&pin=%t&hash=%s", ipfsAddr, format, encoding, pin, hash)
requestBody := &bytes.Buffer{}
writer := multipart.NewWriter(requestBody)
err := writer.WriteField("file", plaintext)
if err != nil {
return nil, logical.CodedError(http.StatusInternalServerError, err.Error())
}
// close multipart writer before sending request
err = writer.Close()
if err != nil {
return nil, logical.CodedError(http.StatusInternalServerError, err.Error())
}
httpReq, err := http.NewRequest(http.MethodPost, addr, requestBody)
httpReq.Header.Set("User-Agent", "vault-plugin-ipfs")
httpReq.Header.Set("Content-Type", "multipart/form-data")
res, err := ipfsClient.Do(httpReq)
if err != nil {
return nil, logical.CodedError(http.StatusInternalServerError, err.Error())
}
defer res.Body.Close()
var byteResponse []byte
res.Body.Read(byteResponse)
var obj map[string]interface{}
json.Unmarshal(byteResponse, &obj)
if res.StatusCode != http.StatusOK {
return logical.ErrorResponse(string(byteResponse)), nil
}
var cid Cid
return &logical.Response{
Data: structs.Map(cid),
}, nil
}