Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit e1c450f

Browse files
committed
Move functionality for op v1 in separate file and rename OPClient to OPCLI
1 parent 65191f4 commit e1c450f

4 files changed

Lines changed: 133 additions & 127 deletions

File tree

internals/onepassword/cli_v1.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package onepassword
2+
3+
import (
4+
"encoding/base64"
5+
"encoding/json"
6+
"fmt"
7+
)
8+
9+
type OPV1CLI struct {
10+
version string
11+
isV2 bool
12+
}
13+
14+
func (op *OPV1CLI) IsV2() bool {
15+
return op.isV2
16+
}
17+
18+
func (op *OPV1CLI) CreateVault(name string) error {
19+
_, err := execOP("create", "vault", name)
20+
if err != nil {
21+
return fmt.Errorf("could not create vault '%s': %s", name, err)
22+
}
23+
return nil
24+
}
25+
26+
func (op *OPV1CLI) CreateItem(vault string, template *ItemTemplate, title string) error {
27+
jsonTemplate, err := json.Marshal(template)
28+
if err != nil {
29+
return err
30+
}
31+
32+
encodedTemplate := base64.RawURLEncoding.EncodeToString(jsonTemplate)
33+
34+
_, err = execOP("create", "item", "apicredential", "--vault="+vault, encodedTemplate, "title="+title)
35+
return err
36+
}
37+
38+
func (op *OPV1CLI) SetField(vault, item, field, value string) error {
39+
_, err := execOP("edit", "item", item, fmt.Sprintf(`%s=%s`, field, value), "--vault="+vault)
40+
if err != nil {
41+
return fmt.Errorf("could not set field '%s'.'%s'.'%s'", vault, item, field)
42+
}
43+
return nil
44+
}
45+
46+
// GetFields returns a title-to-value map of the fields from the first section of the given 1Password item.
47+
// The rest of the fields are ignored as the migration tool only stores information in the first
48+
// section of each item.
49+
func (op *OPV1CLI) GetFields(vault, item string) (map[string]string, error) {
50+
opItem := struct {
51+
Details ItemTemplate `json:"details"`
52+
}{}
53+
opItemJSON, err := execOP("get", "item", item, "--vault="+vault)
54+
if err != nil {
55+
return nil, fmt.Errorf("could not get item '%s'.'%s' from 1Password: %s", vault, item, err)
56+
}
57+
err = json.Unmarshal(opItemJSON, &opItem)
58+
if err != nil {
59+
return nil, fmt.Errorf("unexpected format of 1Password item in `op get item` command output: %s", err)
60+
}
61+
62+
fields := make(map[string]string, len(opItem.Details.Sections[0].Fields))
63+
for _, field := range opItem.Details.Sections[0].Fields {
64+
fields[field.Title] = field.Value
65+
}
66+
return fields, nil
67+
}
68+
69+
func (op *OPV1CLI) ExistsVault(vaultName string) (bool, error) {
70+
vaultsBytes, err := execOP("list", "vaults")
71+
if err != nil {
72+
return false, fmt.Errorf("could not list vaults: %s", err)
73+
}
74+
75+
vaultsJSON := make([]struct {
76+
UUID string `json:"uuid"`
77+
Name string `json:"name"`
78+
}, 0)
79+
80+
err = json.Unmarshal(vaultsBytes, &vaultsJSON)
81+
if err != nil {
82+
return false, fmt.Errorf("unexpected format of `op list vaults`: %s", vaultsBytes)
83+
}
84+
85+
for _, vault := range vaultsJSON {
86+
if vault.Name == vaultName {
87+
return true, nil
88+
}
89+
}
90+
91+
return false, nil
92+
}
93+
94+
func (op *OPV1CLI) ExistsItemInVault(vault string, itemName string) (bool, error) {
95+
itemsBytes, err := execOP("list", "items", "--vault", vault)
96+
if err != nil {
97+
return false, fmt.Errorf("could not list items in vault %s: %s", vault, err)
98+
}
99+
100+
itemsJSON := make([]struct {
101+
Overview struct {
102+
Title string `json:"title"`
103+
} `json:"overview"`
104+
}, 0)
105+
106+
err = json.Unmarshal(itemsBytes, &itemsJSON)
107+
if err != nil {
108+
return false, fmt.Errorf("unexpected format of `op list items`: %s", itemsBytes)
109+
}
110+
111+
for _, item := range itemsJSON {
112+
if item.Overview.Title == itemName {
113+
return true, nil
114+
}
115+
}
116+
117+
return false, nil
118+
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ import (
77
"os"
88
)
99

10-
type OPV2Client struct {
10+
type OPV2CLI struct {
1111
version string
1212
isV2 bool
1313
}
1414

15-
func (op *OPV2Client) IsV2() bool {
15+
func (op *OPV2CLI) IsV2() bool {
1616
return op.isV2
1717
}
1818

19-
func (op *OPV2Client) CreateVault(name string) error {
19+
func (op *OPV2CLI) CreateVault(name string) error {
2020
_, err := execOP("vault", "create", name)
2121
if err != nil {
2222
return fmt.Errorf("could not create vault '%s': %s", name, err)
2323
}
2424
return nil
2525
}
2626

27-
func (op *OPV2Client) CreateItem(vault string, template *ItemTemplate, title string) error {
27+
func (op *OPV2CLI) CreateItem(vault string, template *ItemTemplate, title string) error {
2828
jsonTemplate, err := json.Marshal(template)
2929
if err != nil {
3030
return err
@@ -49,7 +49,7 @@ func (op *OPV2Client) CreateItem(vault string, template *ItemTemplate, title str
4949
return err
5050
}
5151

52-
func (op *OPV2Client) SetField(vault, item, field, value string) error {
52+
func (op *OPV2CLI) SetField(vault, item, field, value string) error {
5353
_, err := execOP("item", "edit", item, fmt.Sprintf(`%s=%s`, field, value), "--vault="+vault)
5454
if err != nil {
5555
return fmt.Errorf("could not set field '%s'.'%s'.'%s'", vault, item, field)
@@ -60,7 +60,7 @@ func (op *OPV2Client) SetField(vault, item, field, value string) error {
6060
// GetFields returns a title-to-value map of the fields from the first section of the given 1Password item.
6161
// The rest of the fields are ignored as the migration tool only stores information in the first
6262
// section of each item.
63-
func (op *OPV2Client) GetFields(vault, item string) (map[string]string, error) {
63+
func (op *OPV2CLI) GetFields(vault, item string) (map[string]string, error) {
6464
opItem := struct {
6565
Fields []v2ItemFieldTemplate `json:"fields"`
6666
}{}
@@ -87,7 +87,7 @@ type v2ItemFieldTemplate struct {
8787
Value string `json:"value"`
8888
}
8989

90-
func (op *OPV2Client) ExistsVault(vaultName string) (bool, error) {
90+
func (op *OPV2CLI) ExistsVault(vaultName string) (bool, error) {
9191
vaultsBytes, err := execOP("vault", "list", "--format=json")
9292
if err != nil {
9393
return false, fmt.Errorf("could not list vaults: %s", err)
@@ -112,7 +112,7 @@ func (op *OPV2Client) ExistsVault(vaultName string) (bool, error) {
112112
return false, nil
113113
}
114114

115-
func (op *OPV2Client) ExistsItemInVault(vault string, itemName string) (bool, error) {
115+
func (op *OPV2CLI) ExistsItemInVault(vault string, itemName string) (bool, error) {
116116
itemsBytes, err := execOP("item", "list", "--vault="+vault, "--format=json")
117117
if err != nil {
118118
return false, fmt.Errorf("could not list items in vault %s: %s", vault, err)

internals/onepassword/onepassword.go

Lines changed: 4 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package onepassword
22

33
import (
44
"bytes"
5-
"encoding/base64"
65
"encoding/json"
76
"fmt"
87
"io/ioutil"
@@ -14,7 +13,7 @@ import (
1413
"github.com/mitchellh/go-homedir"
1514
)
1615

17-
type OPClient interface {
16+
type OPCLI interface {
1817
IsV2() bool
1918
CreateVault(name string) error
2019
CreateItem(vault string, template *ItemTemplate, title string) error
@@ -24,7 +23,7 @@ type OPClient interface {
2423
ExistsItemInVault(vault string, itemName string) (bool, error)
2524
}
2625

27-
func GetOPClient() (OPClient, error) {
26+
func GetOPClient() (OPCLI, error) {
2827
out, err := execOP("--version")
2928
if err != nil {
3029
return nil, err
@@ -33,77 +32,17 @@ func GetOPClient() (OPClient, error) {
3332
version := strings.TrimSpace(string(out))
3433

3534
if strings.HasPrefix(version, "2.") {
36-
return &OPV2Client{
35+
return &OPV2CLI{
3736
version: version,
3837
isV2: true,
3938
}, nil
4039
}
41-
return &OPV1Client{
40+
return &OPV1CLI{
4241
version: version,
4342
isV2: false,
4443
}, nil
4544
}
4645

47-
type OPV1Client struct {
48-
version string
49-
isV2 bool
50-
}
51-
52-
func (op *OPV1Client) IsV2() bool {
53-
return op.isV2
54-
}
55-
56-
func (op *OPV1Client) CreateVault(name string) error {
57-
_, err := execOP("create", "vault", name)
58-
if err != nil {
59-
return fmt.Errorf("could not create vault '%s': %s", name, err)
60-
}
61-
return nil
62-
}
63-
64-
func (op *OPV1Client) CreateItem(vault string, template *ItemTemplate, title string) error {
65-
jsonTemplate, err := json.Marshal(template)
66-
if err != nil {
67-
return err
68-
}
69-
70-
encodedTemplate := base64.RawURLEncoding.EncodeToString(jsonTemplate)
71-
72-
_, err = execOP("create", "item", "apicredential", "--vault="+vault, encodedTemplate, "title="+title)
73-
return err
74-
}
75-
76-
func (op *OPV1Client) SetField(vault, item, field, value string) error {
77-
_, err := execOP("edit", "item", item, fmt.Sprintf(`%s=%s`, field, value), "--vault="+vault)
78-
if err != nil {
79-
return fmt.Errorf("could not set field '%s'.'%s'.'%s'", vault, item, field)
80-
}
81-
return nil
82-
}
83-
84-
// GetFields returns a title-to-value map of the fields from the first section of the given 1Password item.
85-
// The rest of the fields are ignored as the migration tool only stores information in the first
86-
// section of each item.
87-
func (op *OPV1Client) GetFields(vault, item string) (map[string]string, error) {
88-
opItem := struct {
89-
Details ItemTemplate `json:"details"`
90-
}{}
91-
opItemJSON, err := execOP("get", "item", item, "--vault="+vault)
92-
if err != nil {
93-
return nil, fmt.Errorf("could not get item '%s'.'%s' from 1Password: %s", vault, item, err)
94-
}
95-
err = json.Unmarshal(opItemJSON, &opItem)
96-
if err != nil {
97-
return nil, fmt.Errorf("unexpected format of 1Password item in `op get item` command output: %s", err)
98-
}
99-
100-
fields := make(map[string]string, len(opItem.Details.Sections[0].Fields))
101-
for _, field := range opItem.Details.Sections[0].Fields {
102-
fields[field.Title] = field.Value
103-
}
104-
return fields, nil
105-
}
106-
10746
func NewItemTemplate() *ItemTemplate {
10847
return &ItemTemplate{
10948
Sections: []sectionTemplate{
@@ -241,54 +180,3 @@ func GetSignInAddress() (string, error) {
241180

242181
return "", fmt.Errorf("unexpected format of 1password config file at %s: missing account entry for latest used account", path)
243182
}
244-
245-
func (op *OPV1Client) ExistsVault(vaultName string) (bool, error) {
246-
vaultsBytes, err := execOP("list", "vaults")
247-
if err != nil {
248-
return false, fmt.Errorf("could not list vaults: %s", err)
249-
}
250-
251-
vaultsJSON := make([]struct {
252-
UUID string `json:"uuid"`
253-
Name string `json:"name"`
254-
}, 0)
255-
256-
err = json.Unmarshal(vaultsBytes, &vaultsJSON)
257-
if err != nil {
258-
return false, fmt.Errorf("unexpected format of `op list vaults`: %s", vaultsBytes)
259-
}
260-
261-
for _, vault := range vaultsJSON {
262-
if vault.Name == vaultName {
263-
return true, nil
264-
}
265-
}
266-
267-
return false, nil
268-
}
269-
270-
func (op *OPV1Client) ExistsItemInVault(vault string, itemName string) (bool, error) {
271-
itemsBytes, err := execOP("list", "items", "--vault", vault)
272-
if err != nil {
273-
return false, fmt.Errorf("could not list items in vault %s: %s", vault, err)
274-
}
275-
276-
itemsJSON := make([]struct {
277-
Overview struct {
278-
Title string `json:"title"`
279-
} `json:"overview"`
280-
}, 0)
281-
282-
err = json.Unmarshal(itemsBytes, &itemsJSON)
283-
if err != nil {
284-
return false, fmt.Errorf("unexpected format of `op list items`: %s", itemsBytes)
285-
}
286-
287-
for _, item := range itemsJSON {
288-
if item.Overview.Title == itemName {
289-
return true, nil
290-
}
291-
}
292-
293-
return false, nil
294-
}

internals/secrethub/migrate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ type change interface {
472472

473473
type vaultCreation struct {
474474
vault string
475-
opClient onepassword.OPClient
475+
opClient onepassword.OPCLI
476476
}
477477

478478
func (c vaultCreation) Vault() string {
@@ -491,7 +491,7 @@ type itemCreation struct {
491491
vault string
492492
item string
493493
itemTemplate *onepassword.ItemTemplate
494-
opClient onepassword.OPClient
494+
opClient onepassword.OPCLI
495495
}
496496

497497
func (c itemCreation) Vault() string {
@@ -510,7 +510,7 @@ type itemUpdate struct {
510510
vault string
511511
item string
512512
fieldValues map[string]string
513-
opClient onepassword.OPClient
513+
opClient onepassword.OPCLI
514514
}
515515

516516
func (c itemUpdate) Vault() string {

0 commit comments

Comments
 (0)