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

Commit

Permalink
Merge pull request #400 from secrethub/fix/migrate-item-creation-cli-2
Browse files Browse the repository at this point in the history
Fix item creation with 1Password CLI 2 when migrating secrets from SecretHub
  • Loading branch information
edif2008 authored Nov 11, 2022
2 parents d29a942 + 4609f70 commit 99809b3
Show file tree
Hide file tree
Showing 43 changed files with 184 additions and 138 deletions.
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2.1
jobs:
lint:
docker:
- image: golangci/golangci-lint:v1.41.1-alpine
- image: golangci/golangci-lint:v1.50.1-alpine
steps:
- checkout
- restore_cache:
Expand Down Expand Up @@ -30,13 +30,13 @@ jobs:
arch:
type: string
docker:
- image: circleci/golang:1.14
- image: cimg/go:1.18
steps:
- checkout
- run: GOOS=<< parameters.os >> GOARCH=<< parameters.arch >> go build ./cmd/secrethub
test:
docker:
- image: circleci/golang:1.14
- image: cimg/go:1.18
steps:
- checkout
- restore_cache:
Expand All @@ -50,7 +50,7 @@ jobs:
- run: make test
verify-goreleaser:
docker:
- image: goreleaser/goreleaser:v0.133
- image: goreleaser/goreleaser:v1.12.3
steps:
- checkout
- run: goreleaser check
Expand Down
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ brews:
- name: secrethub-cli
ids:
- default
github:
tap:
owner: secrethub
name: homebrew-tools
folder: Formula
Expand Down
33 changes: 31 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/secrethub/secrethub-cli

go 1.14
go 1.18

require (
bitbucket.org/zombiezen/cardcpx v0.0.0-20150417151802-902f68ff43ef
Expand All @@ -19,10 +19,39 @@ require (
github.com/spf13/pflag v1.0.5
github.com/zalando/go-keyring v0.0.0-20190208082241-fbe81aec3a07
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68
golang.org/x/sys v0.2.0
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf
golang.org/x/text v0.3.2
google.golang.org/api v0.26.0
gopkg.in/yaml.v2 v2.4.0
gotest.tools v2.2.0+incompatible
)

require (
cloud.google.com/go v0.57.0 // indirect
github.com/Azure/go-ntlmssp v0.0.0-20180810175552-4a21cbd618b4 // indirect
github.com/ChrisTrenkamp/goxpath v0.0.0-20170922090931-c385f95c6022 // indirect
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf // indirect
github.com/danieljoos/wincred v1.0.2 // indirect
github.com/docker/docker v1.13.1 // indirect
github.com/godbus/dbus v4.1.0+incompatible // indirect
github.com/gofrs/uuid v3.2.0+incompatible // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.4.0 // indirect
github.com/google/go-cmp v0.4.0 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 // indirect
github.com/masterzen/simplexml v0.0.0-20160608183007-4572e39b1ab9 // indirect
github.com/mattn/go-shellwords v1.0.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
go.opencensus.io v0.22.3 // indirect
golang.org/x/net v0.0.0-20200501053045-e0ff5e5a1de5 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/genproto v0.0.0-20200430143042-b979b6f78d84 // indirect
google.golang.org/grpc v1.29.1 // indirect
google.golang.org/protobuf v1.21.0 // indirect
)
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,9 @@ golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200331124033-c3d80250170d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200501052902-10377860bb8e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf h1:MZ2shdL+ZM/XzY3ZGOnh4Nlpnxz5GSOhOmtHo3iPU6M=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
2 changes: 1 addition & 1 deletion internals/cli/clip/fakeclip/clip.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !production
//go:build !production

// Package fakeclip provides fake implementations of the clip.Clipper interface
// to be used for testing.
Expand Down
2 changes: 1 addition & 1 deletion internals/cli/cloneproc/spawn_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build linux darwin
//go:build linux || darwin

package cloneproc

Expand Down
30 changes: 15 additions & 15 deletions internals/cli/cobra_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,21 +378,21 @@ func numFlags(flagSet pflag.FlagSet) int {

// UsageTemplate is the custom usage template of the command.
// Changes in comparison to cobra's default template:
// 1. Usage section
// a. `[flags]` is placed before the arguments.
// b. All arguments are put in the usage in their proper order.
// c. Where applicable, the argument name is replaced with its placeholder.
// 2. Commands section
// a. For the root command (`secrethub`) the commands are grouped into
// `Management commands` and `Commands`
// 2. Flags section
// a. Flag's type was removed.
// b. The help text for flags is well divided into its own column, thus
// making the visibility of the flags better.
// c. At the end of a flag's help text, the name of its environment variable is
// displayed between brackets.
// d. The section is hidden if the only flag is `--help`.
// 4. Arguments section (created by us)
// 1. Usage section
// a. `[flags]` is placed before the arguments.
// b. All arguments are put in the usage in their proper order.
// c. Where applicable, the argument name is replaced with its placeholder.
// 2. Commands section
// a. For the root command (`secrethub`) the commands are grouped into
// `Management commands` and `Commands`
// 2. Flags section
// a. Flag's type was removed.
// b. The help text for flags is well divided into its own column, thus
// making the visibility of the flags better.
// c. At the end of a flag's help text, the name of its environment variable is
// displayed between brackets.
// d. The section is hidden if the only flag is `--help`.
// 4. Arguments section (created by us)
var UsageTemplate = `Usage:
{{if .Cmd.Runnable}} {{(useLine .Cmd .Args)}}{{end}}
{{- if .Cmd.HasAvailableSubCommands}} {{ .Cmd.CommandPath}} [command]{{end}}
Expand Down
3 changes: 1 addition & 2 deletions internals/cli/masker/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package masker
import (
"bytes"
"io"
"io/ioutil"
"sync"
"time"
)
Expand Down Expand Up @@ -80,7 +79,7 @@ func (s *stream) flush(n int) error {
}

// Drop all bytes until the end of the mask.
_, err = s.buf.writeUpToIndex(ioutil.Discard, i+int64(length))
_, err = s.buf.writeUpToIndex(io.Discard, i+int64(length))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internals/cli/mlock/mlock_unavailable.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build android darwin nacl netbsd plan9 windows
//go:build android || darwin || nacl || netbsd || plan9 || windows

package mlock

Expand Down
2 changes: 1 addition & 1 deletion internals/cli/mlock/mlock_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build dragonfly freebsd linux openbsd solaris
//go:build dragonfly || freebsd || linux || openbsd || solaris

package mlock

Expand Down
2 changes: 1 addition & 1 deletion internals/cli/progress/fakeprogress/progressprinter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !production
//go:build !production

// Package fakeprogress provides an implementation of the progress.Printer interface
// to be used in tests.
Expand Down
7 changes: 3 additions & 4 deletions internals/cli/ui/ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -74,8 +73,8 @@ func AskSecret(io IO, question string) (string, error) {

// AskMultiline prints out the question and reads back the input until an EOF is reached.
// The input is displayed to the user.
func AskMultiline(io IO, question string) ([]byte, error) {
promptIn, promptOut, err := io.Prompts()
func AskMultiline(IO IO, question string) ([]byte, error) {
promptIn, promptOut, err := IO.Prompts()
if err != nil {
return nil, err
}
Expand All @@ -85,7 +84,7 @@ func AskMultiline(io IO, question string) ([]byte, error) {
return nil, err
}

raw, err := ioutil.ReadAll(promptIn)
raw, err := io.ReadAll(promptIn)
if err != nil {
return nil, err
}
Expand Down
13 changes: 6 additions & 7 deletions internals/cli/ui/fakeui/testing.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !production
//go:build !production

package fakeui

Expand All @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"testing"

Expand All @@ -28,11 +27,11 @@ type FakeIO struct {

// NewIO creates a new FakeIO with empty buffers.
func NewIO(t *testing.T) *FakeIO {
tempDir, err := ioutil.TempDir("", "")
tempDir, err := os.MkdirTemp("", "")
assert.OK(t, err)
stdIn, err := ioutil.TempFile(tempDir, "in")
stdIn, err := os.CreateTemp(tempDir, "in")
assert.OK(t, err)
stdOut, err := ioutil.TempFile(tempDir, "out")
stdOut, err := os.CreateTemp(tempDir, "out")
assert.OK(t, err)

t.Cleanup(func() {
Expand Down Expand Up @@ -82,7 +81,7 @@ func (f *FakeIO) Stdout() *os.File {
}

func (f *FakeIO) ReadStdout() ([]byte, error) {
return ioutil.ReadFile(f.StdOut.Name())
return os.ReadFile(f.StdOut.Name())
}

// Prompts returns the mocked prompts and error.
Expand All @@ -99,7 +98,7 @@ func (f *FakeIO) IsOutputPiped() bool {
}

func (f *FakeIO) ReadSecret() ([]byte, error) {
return ioutil.ReadAll(f.PasswordReader)
return io.ReadAll(f.PasswordReader)
}

// FakeReader implements the Reader interface.
Expand Down
2 changes: 1 addition & 1 deletion internals/cli/ui/io_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build darwin linux
//go:build darwin || linux

package ui

Expand Down
4 changes: 2 additions & 2 deletions internals/onepassword/cli_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (op *OPV1CLI) CreateVault(name string) error {
return nil
}

func (op *OPV1CLI) CreateItem(vault string, template *ItemTemplate, title string) error {
func (op *OPV1CLI) CreateItem(vault string, template ItemTemplate, title string) error {
jsonTemplate, err := json.Marshal(template)
if err != nil {
return err
Expand All @@ -45,7 +45,7 @@ func (op *OPV1CLI) SetField(vault, item, field, value string) error {
// section of each item.
func (op *OPV1CLI) GetFields(vault, item string) (map[string]string, error) {
opItem := struct {
Details ItemTemplate `json:"details"`
Details v1ItemTemplate `json:"details"`
}{}
opItemJSON, err := execOP("get", "item", item, "--vault="+vault)
if err != nil {
Expand Down
36 changes: 30 additions & 6 deletions internals/onepassword/cli_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package onepassword
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)

Expand All @@ -21,13 +20,13 @@ func (op *OPV2CLI) CreateVault(name string) error {
return nil
}

func (op *OPV2CLI) CreateItem(vault string, template *ItemTemplate, title string) error {
func (op *OPV2CLI) CreateItem(vault string, template ItemTemplate, title string) error {
jsonTemplate, err := json.Marshal(template)
if err != nil {
return err
}

tempJSONFile, err := ioutil.TempFile(os.TempDir(), "jsonTemplate-")
tempJSONFile, err := os.CreateTemp(os.TempDir(), "jsonTemplate-")
if err != nil {
return err
}
Expand Down Expand Up @@ -77,11 +76,36 @@ func (op *OPV2CLI) GetFields(vault, item string) (map[string]string, error) {
return fields, nil
}

type v2ItemFieldTemplate struct {
type v2ItemTemplate struct {
Sections []v2SectionTemplate `json:"sections"`
Fields []v2ItemFieldTemplate `json:"fields"`
}

type v2SectionTemplate struct {
ID string `json:"id"`
Type string `json:"type"`
Label string `json:"label"`
Value string `json:"value"`
}

type v2ItemFieldTemplate struct {
ID string `json:"id"`
Section v2SectionTemplate `json:"section"`
Type string `json:"type"`
Label string `json:"label"`
Value string `json:"value"`
}

func (tpl *v2ItemTemplate) AddField(name, value string, concealed bool) {
fieldType := "CONCEALED"
if !concealed {
fieldType = "STRING"
}

tpl.Fields = append(tpl.Fields, v2ItemFieldTemplate{
ID: name,
Type: fieldType,
Label: name,
Value: value,
})
}

func (op *OPV2CLI) ExistsVault(vaultName string) (bool, error) {
Expand Down
Loading

0 comments on commit 99809b3

Please sign in to comment.