Skip to content

Commit

Permalink
fix: fix op cli always generating passwords (#127)
Browse files Browse the repository at this point in the history
When invoking the 1Password CLI, the `--generate-password` flag is now appended to the cli args only when the 1Password item has a password recipe.
  • Loading branch information
josh-burton authored Jan 2, 2024
1 parent d705447 commit c51f434
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
9 changes: 3 additions & 6 deletions onepassword/cli/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,9 @@ func (op *OP) create(ctx context.Context, item *onepassword.Item, vaultUuid stri
args := []opArg{p("item"), p("create"), p("-")}
// 'op item create' command doesn't support generating passwords when using templates
// therefore need to use --generate-password flag to set it
if pf := passwordField(item); pf != nil {
recipeStr := "letters,digits,32"
if pf.Recipe != nil {
recipeStr = passwordRecipeToString(pf.Recipe)
}
args = append(args, f("generate-password", recipeStr))
recipe := passwordRecipe(item)
if recipe != "" {
args = append(args, f("generate-password", recipe))
}

err = op.execJson(ctx, &res, payload, args...)
Expand Down
7 changes: 7 additions & 0 deletions onepassword/cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ func passwordField(item *onepassword.Item) *onepassword.ItemField {
return nil
}

func passwordRecipe(item *onepassword.Item) string {
if pf := passwordField(item); pf != nil {
return passwordRecipeToString(pf.Recipe)
}
return ""
}

func passwordRecipeToString(recipe *onepassword.GeneratorRecipe) string {
str := ""
if recipe != nil {
Expand Down
49 changes: 49 additions & 0 deletions onepassword/cli/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,55 @@ func TestPasswordField(t *testing.T) {
}
}

func TestPasswordRecipeExtraction(t *testing.T) {
tests := map[string]struct {
item *onepassword.Item
expectedString string
}{
"should return empty string if item has no fields": {
item: &onepassword.Item{},
expectedString: "",
},
"should return empty string if no password field": {
item: &onepassword.Item{
Fields: []*onepassword.ItemField{
{Purpose: onepassword.FieldPurposeNotes},
},
},
expectedString: "",
},
"should return empty string if no password recipe": {
item: &onepassword.Item{
Fields: []*onepassword.ItemField{
{ID: "username", Purpose: onepassword.FieldPurposeUsername},
{ID: "password", Purpose: onepassword.FieldPurposePassword},
},
},
expectedString: "",
},
"should return recipe string": {
item: &onepassword.Item{
Fields: []*onepassword.ItemField{
{ID: "username", Purpose: onepassword.FieldPurposeUsername},
{ID: "password", Purpose: onepassword.FieldPurposePassword, Recipe: &onepassword.GeneratorRecipe{
Length: 30,
}},
},
},
expectedString: "30",
},
}

for description, test := range tests {
t.Run(description, func(t *testing.T) {
actualString := passwordRecipe(test.item)
if actualString != test.expectedString {
t.Errorf("Unexpected password recipe string. Expected \"%s\", but got \"%s\"", test.expectedString, actualString)
}
})
}
}

func TestPasswordRecipeToString(t *testing.T) {
tests := map[string]struct {
recipe *onepassword.GeneratorRecipe
Expand Down

0 comments on commit c51f434

Please sign in to comment.